Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Remote target communications for serial-line targets using SDS' protocol. |
0a65a603 | 2 | |
281b533b | 3 | Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2004, 2006 Free Software |
0a65a603 | 4 | Foundation, Inc. |
c906108c | 5 | |
c5aa993b | 6 | This file is part of GDB. |
c906108c | 7 | |
c5aa993b JM |
8 | This program is free software; you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
c906108c | 12 | |
c5aa993b JM |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
c906108c | 17 | |
c5aa993b JM |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software | |
197e01b6 EZ |
20 | Foundation, Inc., 51 Franklin Street, Fifth Floor, |
21 | Boston, MA 02110-1301, USA. */ | |
c906108c SS |
22 | |
23 | /* This interface was written by studying the behavior of the SDS | |
24 | monitor on an ADS 821/860 board, and by consulting the | |
25 | documentation of the monitor that is available on Motorola's web | |
26 | site. -sts 8/13/97 */ | |
27 | ||
28 | #include "defs.h" | |
29 | #include "gdb_string.h" | |
30 | #include <fcntl.h> | |
31 | #include "frame.h" | |
32 | #include "inferior.h" | |
60250e8b | 33 | #include "exceptions.h" |
c906108c SS |
34 | #include "bfd.h" |
35 | #include "symfile.h" | |
36 | #include "target.h" | |
c906108c SS |
37 | #include "gdbcmd.h" |
38 | #include "objfiles.h" | |
39 | #include "gdb-stabs.h" | |
40 | #include "gdbthread.h" | |
41 | #include "gdbcore.h" | |
4e052eda | 42 | #include "regcache.h" |
c906108c | 43 | |
c906108c SS |
44 | #include <signal.h> |
45 | #include "serial.h" | |
46 | ||
a14ed312 | 47 | extern void _initialize_remote_sds (void); |
c906108c SS |
48 | |
49 | /* Declarations of local functions. */ | |
50 | ||
a14ed312 | 51 | static int sds_write_bytes (CORE_ADDR, char *, int); |
c906108c | 52 | |
a14ed312 | 53 | static int sds_read_bytes (CORE_ADDR, char *, int); |
c906108c | 54 | |
a14ed312 | 55 | static void sds_files_info (struct target_ops *ignore); |
c906108c | 56 | |
29e57380 C |
57 | static int sds_xfer_memory (CORE_ADDR, char *, int, int, |
58 | struct mem_attrib *, struct target_ops *); | |
c906108c | 59 | |
a14ed312 | 60 | static void sds_prepare_to_store (void); |
c906108c | 61 | |
a14ed312 | 62 | static void sds_fetch_registers (int); |
c906108c | 63 | |
39f77062 | 64 | static void sds_resume (ptid_t, int, enum target_signal); |
c906108c | 65 | |
4efb68b1 | 66 | static int sds_start_remote (void *); |
c906108c | 67 | |
a14ed312 | 68 | static void sds_open (char *, int); |
c906108c | 69 | |
a14ed312 | 70 | static void sds_close (int); |
c906108c | 71 | |
a14ed312 | 72 | static void sds_store_registers (int); |
c906108c | 73 | |
a14ed312 | 74 | static void sds_mourn (void); |
c906108c | 75 | |
a14ed312 | 76 | static void sds_load (char *, int); |
c906108c | 77 | |
a14ed312 | 78 | static int getmessage (unsigned char *, int); |
c906108c | 79 | |
a14ed312 | 80 | static int putmessage (unsigned char *, int); |
c906108c | 81 | |
a14ed312 | 82 | static int sds_send (unsigned char *, int); |
c906108c | 83 | |
a14ed312 | 84 | static int readchar (int); |
c906108c | 85 | |
39f77062 | 86 | static ptid_t sds_wait (ptid_t, struct target_waitstatus *); |
c906108c | 87 | |
a14ed312 | 88 | static void sds_kill (void); |
c906108c | 89 | |
a14ed312 | 90 | static int fromhex (int); |
c906108c | 91 | |
a14ed312 | 92 | static void sds_detach (char *, int); |
c906108c | 93 | |
a14ed312 | 94 | static void sds_interrupt (int); |
c906108c | 95 | |
a14ed312 | 96 | static void sds_interrupt_twice (int); |
c906108c | 97 | |
a14ed312 | 98 | static void interrupt_query (void); |
c906108c | 99 | |
a14ed312 | 100 | static int read_frame (char *); |
c906108c | 101 | |
a14ed312 | 102 | static void init_sds_ops (void); |
c906108c | 103 | |
a14ed312 | 104 | static void sds_command (char *args, int from_tty); |
c906108c SS |
105 | |
106 | /* Define the target operations vector. */ | |
107 | ||
108 | static struct target_ops sds_ops; | |
109 | ||
110 | /* This was 5 seconds, which is a long time to sit and wait. | |
111 | Unless this is going though some terminal server or multiplexer or | |
112 | other form of hairy serial connection, I would think 2 seconds would | |
113 | be plenty. */ | |
114 | ||
115 | static int sds_timeout = 2; | |
116 | ||
117 | /* Descriptor for I/O to remote machine. Initialize it to NULL so | |
118 | that sds_open knows that we don't have a file open when the program | |
119 | starts. */ | |
120 | ||
819cc324 | 121 | static struct serial *sds_desc = NULL; |
c906108c SS |
122 | |
123 | /* This limit comes from the monitor. */ | |
124 | ||
125 | #define PBUFSIZ 250 | |
126 | ||
127 | /* Maximum number of bytes to read/write at once. The value here | |
128 | is chosen to fill up a packet (the headers account for the 32). */ | |
129 | #define MAXBUFBYTES ((PBUFSIZ-32)/2) | |
130 | ||
131 | static int next_msg_id; | |
132 | ||
133 | static int just_started; | |
134 | ||
135 | static int message_pending; | |
c906108c | 136 | \f |
c5aa993b | 137 | |
c906108c SS |
138 | /* Clean up connection to a remote debugger. */ |
139 | ||
c906108c | 140 | static void |
fba45db2 | 141 | sds_close (int quitting) |
c906108c SS |
142 | { |
143 | if (sds_desc) | |
2cd58942 | 144 | serial_close (sds_desc); |
c906108c SS |
145 | sds_desc = NULL; |
146 | } | |
147 | ||
148 | /* Stub for catch_errors. */ | |
149 | ||
150 | static int | |
8621d6a9 | 151 | sds_start_remote (void *from_tty_p) |
c906108c | 152 | { |
8621d6a9 | 153 | int from_tty = * (int *) from_tty; |
29372230 | 154 | int c; |
c906108c SS |
155 | unsigned char buf[200]; |
156 | ||
8edbea78 | 157 | immediate_quit++; /* Allow user to interrupt it */ |
c906108c SS |
158 | |
159 | /* Ack any packet which the remote side has already sent. */ | |
2cd58942 AC |
160 | serial_write (sds_desc, "{#*\r\n", 5); |
161 | serial_write (sds_desc, "{#}\r\n", 5); | |
c906108c SS |
162 | |
163 | while ((c = readchar (1)) >= 0) | |
164 | printf_unfiltered ("%c", c); | |
165 | printf_unfiltered ("\n"); | |
166 | ||
167 | next_msg_id = 251; | |
168 | ||
169 | buf[0] = 26; | |
170 | sds_send (buf, 1); | |
171 | ||
172 | buf[0] = 0; | |
173 | sds_send (buf, 1); | |
174 | ||
8edbea78 | 175 | immediate_quit--; |
c906108c | 176 | |
8621d6a9 | 177 | start_remote (from_tty); /* Initialize gdb process mechanisms */ |
c906108c SS |
178 | return 1; |
179 | } | |
180 | ||
181 | /* Open a connection to a remote debugger. | |
182 | NAME is the filename used for communication. */ | |
183 | ||
c906108c | 184 | static void |
fba45db2 | 185 | sds_open (char *name, int from_tty) |
c906108c SS |
186 | { |
187 | if (name == 0) | |
188 | error ("To open a remote debug connection, you need to specify what serial\n\ | |
189 | device is attached to the remote system (e.g. /dev/ttya)."); | |
190 | ||
191 | target_preopen (from_tty); | |
192 | ||
193 | unpush_target (&sds_ops); | |
194 | ||
2cd58942 | 195 | sds_desc = serial_open (name); |
c906108c SS |
196 | if (!sds_desc) |
197 | perror_with_name (name); | |
198 | ||
199 | if (baud_rate != -1) | |
200 | { | |
2cd58942 | 201 | if (serial_setbaudrate (sds_desc, baud_rate)) |
c906108c | 202 | { |
2cd58942 | 203 | serial_close (sds_desc); |
c906108c SS |
204 | perror_with_name (name); |
205 | } | |
206 | } | |
207 | ||
208 | ||
2cd58942 | 209 | serial_raw (sds_desc); |
c906108c SS |
210 | |
211 | /* If there is something sitting in the buffer we might take it as a | |
212 | response to a command, which would be bad. */ | |
2cd58942 | 213 | serial_flush_input (sds_desc); |
c906108c SS |
214 | |
215 | if (from_tty) | |
216 | { | |
217 | puts_filtered ("Remote debugging using "); | |
218 | puts_filtered (name); | |
219 | puts_filtered ("\n"); | |
220 | } | |
221 | push_target (&sds_ops); /* Switch to using remote target now */ | |
222 | ||
223 | just_started = 1; | |
224 | ||
225 | /* Start the remote connection; if error (0), discard this target. | |
226 | In particular, if the user quits, be sure to discard it (we'd be | |
227 | in an inconsistent state otherwise). */ | |
8621d6a9 | 228 | if (!catch_errors (sds_start_remote, &from_tty, |
c906108c SS |
229 | "Couldn't establish connection to remote target\n", |
230 | RETURN_MASK_ALL)) | |
231 | pop_target (); | |
232 | } | |
233 | ||
234 | /* This takes a program previously attached to and detaches it. After | |
235 | this is done, GDB can be used to debug some other program. We | |
236 | better not have left any breakpoints in the target program or it'll | |
237 | die when it hits one. */ | |
238 | ||
239 | static void | |
fba45db2 | 240 | sds_detach (char *args, int from_tty) |
c906108c SS |
241 | { |
242 | char buf[PBUFSIZ]; | |
243 | ||
244 | if (args) | |
245 | error ("Argument given to \"detach\" when remotely debugging."); | |
246 | ||
247 | #if 0 | |
248 | /* Tell the remote target to detach. */ | |
249 | strcpy (buf, "D"); | |
250 | sds_send (buf, 1); | |
251 | #endif | |
252 | ||
253 | pop_target (); | |
254 | if (from_tty) | |
255 | puts_filtered ("Ending remote debugging.\n"); | |
256 | } | |
257 | ||
258 | /* Convert hex digit A to a number. */ | |
259 | ||
260 | static int | |
fba45db2 | 261 | fromhex (int a) |
c906108c SS |
262 | { |
263 | if (a >= '0' && a <= '9') | |
264 | return a - '0'; | |
265 | else if (a >= 'a' && a <= 'f') | |
266 | return a - 'a' + 10; | |
c5aa993b | 267 | else |
c906108c SS |
268 | error ("Reply contains invalid hex digit %d", a); |
269 | } | |
270 | ||
c906108c | 271 | static int |
fba45db2 | 272 | tob64 (unsigned char *inbuf, char *outbuf, int len) |
c906108c SS |
273 | { |
274 | int i, sum; | |
275 | char *p; | |
276 | ||
277 | if (len % 3 != 0) | |
278 | error ("bad length"); | |
279 | ||
280 | p = outbuf; | |
281 | for (i = 0; i < len; i += 3) | |
282 | { | |
283 | /* Collect the next three bytes into a number. */ | |
284 | sum = ((long) *inbuf++) << 16; | |
c5aa993b | 285 | sum |= ((long) *inbuf++) << 8; |
c906108c SS |
286 | sum |= ((long) *inbuf++); |
287 | ||
288 | /* Spit out 4 6-bit encodings. */ | |
289 | *p++ = ((sum >> 18) & 0x3f) + '0'; | |
290 | *p++ = ((sum >> 12) & 0x3f) + '0'; | |
c5aa993b | 291 | *p++ = ((sum >> 6) & 0x3f) + '0'; |
c906108c SS |
292 | *p++ = (sum & 0x3f) + '0'; |
293 | } | |
294 | return (p - outbuf); | |
295 | } | |
296 | ||
297 | static int | |
fba45db2 | 298 | fromb64 (char *inbuf, char *outbuf, int len) |
c906108c SS |
299 | { |
300 | int i, sum; | |
301 | ||
302 | if (len % 4 != 0) | |
303 | error ("bad length"); | |
304 | ||
305 | for (i = 0; i < len; i += 4) | |
306 | { | |
307 | /* Collect 4 6-bit digits. */ | |
c5aa993b | 308 | sum = (*inbuf++ - '0') << 18; |
c906108c | 309 | sum |= (*inbuf++ - '0') << 12; |
c5aa993b | 310 | sum |= (*inbuf++ - '0') << 6; |
c906108c SS |
311 | sum |= (*inbuf++ - '0'); |
312 | ||
313 | /* Now take the resulting 24-bit number and get three bytes out | |
314 | of it. */ | |
315 | *outbuf++ = (sum >> 16) & 0xff; | |
c5aa993b | 316 | *outbuf++ = (sum >> 8) & 0xff; |
c906108c SS |
317 | *outbuf++ = sum & 0xff; |
318 | } | |
319 | ||
320 | return (len / 4) * 3; | |
321 | } | |
c906108c | 322 | \f |
c5aa993b | 323 | |
c906108c SS |
324 | /* Tell the remote machine to resume. */ |
325 | ||
326 | static enum target_signal last_sent_signal = TARGET_SIGNAL_0; | |
327 | int last_sent_step; | |
328 | ||
329 | static void | |
39f77062 | 330 | sds_resume (ptid_t ptid, int step, enum target_signal siggnal) |
c906108c SS |
331 | { |
332 | unsigned char buf[PBUFSIZ]; | |
333 | ||
c906108c SS |
334 | last_sent_signal = siggnal; |
335 | last_sent_step = step; | |
336 | ||
337 | buf[0] = (step ? 21 : 20); | |
c5aa993b | 338 | buf[1] = 0; /* (should be signal?) */ |
c906108c SS |
339 | |
340 | sds_send (buf, 2); | |
341 | } | |
342 | \f | |
343 | /* Send a message to target to halt it. Target will respond, and send | |
344 | us a message pending notice. */ | |
345 | ||
346 | static void | |
fba45db2 | 347 | sds_interrupt (int signo) |
c906108c SS |
348 | { |
349 | unsigned char buf[PBUFSIZ]; | |
350 | ||
351 | /* If this doesn't work, try more severe steps. */ | |
352 | signal (signo, sds_interrupt_twice); | |
c5aa993b | 353 | |
c906108c | 354 | if (remote_debug) |
9846de1b | 355 | fprintf_unfiltered (gdb_stdlog, "sds_interrupt called\n"); |
c906108c SS |
356 | |
357 | buf[0] = 25; | |
358 | sds_send (buf, 1); | |
359 | } | |
360 | ||
c5aa993b | 361 | static void (*ofunc) (); |
c906108c SS |
362 | |
363 | /* The user typed ^C twice. */ | |
364 | ||
365 | static void | |
fba45db2 | 366 | sds_interrupt_twice (int signo) |
c906108c SS |
367 | { |
368 | signal (signo, ofunc); | |
c5aa993b | 369 | |
c906108c SS |
370 | interrupt_query (); |
371 | ||
372 | signal (signo, sds_interrupt); | |
373 | } | |
374 | ||
375 | /* Ask the user what to do when an interrupt is received. */ | |
376 | ||
377 | static void | |
fba45db2 | 378 | interrupt_query (void) |
c906108c SS |
379 | { |
380 | target_terminal_ours (); | |
381 | ||
382 | if (query ("Interrupted while waiting for the program.\n\ | |
383 | Give up (and stop debugging it)? ")) | |
384 | { | |
385 | target_mourn_inferior (); | |
315a522e | 386 | deprecated_throw_reason (RETURN_QUIT); |
c906108c SS |
387 | } |
388 | ||
389 | target_terminal_inferior (); | |
390 | } | |
391 | ||
392 | /* If nonzero, ignore the next kill. */ | |
393 | int kill_kludge; | |
394 | ||
395 | /* Wait until the remote machine stops, then return, storing status in | |
396 | STATUS just as `wait' would. Returns "pid" (though it's not clear | |
397 | what, if anything, that means in the case of this target). */ | |
398 | ||
39f77062 KB |
399 | static ptid_t |
400 | sds_wait (ptid_t ptid, struct target_waitstatus *status) | |
c906108c SS |
401 | { |
402 | unsigned char buf[PBUFSIZ]; | |
403 | int retlen; | |
404 | ||
405 | status->kind = TARGET_WAITKIND_EXITED; | |
406 | status->value.integer = 0; | |
407 | ||
408 | ofunc = (void (*)()) signal (SIGINT, sds_interrupt); | |
409 | ||
410 | signal (SIGINT, ofunc); | |
411 | ||
412 | if (just_started) | |
413 | { | |
414 | just_started = 0; | |
415 | status->kind = TARGET_WAITKIND_STOPPED; | |
39f77062 | 416 | return inferior_ptid; |
c906108c SS |
417 | } |
418 | ||
419 | while (1) | |
420 | { | |
421 | getmessage (buf, 1); | |
422 | ||
423 | if (message_pending) | |
424 | { | |
425 | buf[0] = 26; | |
426 | retlen = sds_send (buf, 1); | |
427 | if (remote_debug) | |
428 | { | |
9846de1b | 429 | fprintf_unfiltered (gdb_stdlog, "Signals: %02x%02x %02x %02x\n", |
7a292a7a | 430 | buf[0], buf[1], |
c906108c SS |
431 | buf[2], buf[3]); |
432 | } | |
433 | message_pending = 0; | |
434 | status->kind = TARGET_WAITKIND_STOPPED; | |
435 | status->value.sig = TARGET_SIGNAL_TRAP; | |
436 | goto got_status; | |
437 | } | |
438 | } | |
c5aa993b | 439 | got_status: |
39f77062 | 440 | return inferior_ptid; |
c906108c SS |
441 | } |
442 | ||
443 | static unsigned char sprs[16]; | |
444 | ||
445 | /* Read the remote registers into the block REGS. */ | |
446 | /* Currently we just read all the registers, so we don't use regno. */ | |
447 | ||
c906108c | 448 | static void |
fba45db2 | 449 | sds_fetch_registers (int regno) |
c906108c SS |
450 | { |
451 | unsigned char buf[PBUFSIZ]; | |
452 | int i, retlen; | |
f42accbe | 453 | char *regs = alloca (deprecated_register_bytes ()); |
c906108c SS |
454 | |
455 | /* Unimplemented registers read as all bits zero. */ | |
f42accbe | 456 | memset (regs, 0, deprecated_register_bytes ()); |
c906108c SS |
457 | |
458 | buf[0] = 18; | |
459 | buf[1] = 1; | |
460 | buf[2] = 0; | |
461 | retlen = sds_send (buf, 3); | |
462 | ||
463 | for (i = 0; i < 4 * 6; ++i) | |
464 | regs[i + 4 * 32 + 8 * 32] = buf[i]; | |
465 | for (i = 0; i < 4 * 4; ++i) | |
466 | sprs[i] = buf[i + 4 * 7]; | |
467 | ||
468 | buf[0] = 18; | |
469 | buf[1] = 2; | |
470 | buf[2] = 0; | |
471 | retlen = sds_send (buf, 3); | |
472 | ||
473 | for (i = 0; i < retlen; i++) | |
474 | regs[i] = buf[i]; | |
475 | ||
476 | /* (should warn about reply too short) */ | |
477 | ||
478 | for (i = 0; i < NUM_REGS; i++) | |
23a6d369 AC |
479 | regcache_raw_supply (current_regcache, i, |
480 | ®s[DEPRECATED_REGISTER_BYTE (i)]); | |
c906108c SS |
481 | } |
482 | ||
483 | /* Prepare to store registers. Since we may send them all, we have to | |
484 | read out the ones we don't want to change first. */ | |
485 | ||
c5aa993b | 486 | static void |
fba45db2 | 487 | sds_prepare_to_store (void) |
c906108c SS |
488 | { |
489 | /* Make sure the entire registers array is valid. */ | |
f42accbe | 490 | deprecated_read_register_bytes (0, (char *) NULL, deprecated_register_bytes ()); |
c906108c SS |
491 | } |
492 | ||
493 | /* Store register REGNO, or all registers if REGNO == -1, from the contents | |
494 | of REGISTERS. FIXME: ignores errors. */ | |
495 | ||
496 | static void | |
fba45db2 | 497 | sds_store_registers (int regno) |
c906108c SS |
498 | { |
499 | unsigned char *p, buf[PBUFSIZ]; | |
500 | int i; | |
501 | ||
502 | /* Store all the special-purpose registers. */ | |
503 | p = buf; | |
504 | *p++ = 19; | |
505 | *p++ = 1; | |
506 | *p++ = 0; | |
507 | *p++ = 0; | |
508 | for (i = 0; i < 4 * 6; i++) | |
524d7c18 | 509 | *p++ = deprecated_registers[i + 4 * 32 + 8 * 32]; |
c906108c SS |
510 | for (i = 0; i < 4 * 1; i++) |
511 | *p++ = 0; | |
512 | for (i = 0; i < 4 * 4; i++) | |
513 | *p++ = sprs[i]; | |
514 | ||
515 | sds_send (buf, p - buf); | |
516 | ||
517 | /* Store all the general-purpose registers. */ | |
518 | p = buf; | |
519 | *p++ = 19; | |
520 | *p++ = 2; | |
521 | *p++ = 0; | |
522 | *p++ = 0; | |
523 | for (i = 0; i < 4 * 32; i++) | |
524d7c18 | 524 | *p++ = deprecated_registers[i]; |
c906108c SS |
525 | |
526 | sds_send (buf, p - buf); | |
527 | ||
528 | } | |
529 | \f | |
530 | /* Write memory data directly to the remote machine. This does not | |
531 | inform the data cache; the data cache uses this. MEMADDR is the | |
532 | address in the remote memory space. MYADDR is the address of the | |
533 | buffer in our space. LEN is the number of bytes. | |
534 | ||
535 | Returns number of bytes transferred, or 0 for error. */ | |
536 | ||
537 | static int | |
fba45db2 | 538 | sds_write_bytes (CORE_ADDR memaddr, char *myaddr, int len) |
c906108c SS |
539 | { |
540 | int max_buf_size; /* Max size of packet output buffer */ | |
541 | int origlen; | |
542 | unsigned char buf[PBUFSIZ]; | |
543 | int todo; | |
544 | int i; | |
545 | ||
546 | /* Chop the transfer down if necessary */ | |
547 | ||
548 | max_buf_size = 150; | |
549 | ||
550 | origlen = len; | |
551 | while (len > 0) | |
552 | { | |
553 | todo = min (len, max_buf_size); | |
554 | ||
555 | buf[0] = 13; | |
556 | buf[1] = 0; | |
557 | buf[2] = (int) (memaddr >> 24) & 0xff; | |
558 | buf[3] = (int) (memaddr >> 16) & 0xff; | |
c5aa993b JM |
559 | buf[4] = (int) (memaddr >> 8) & 0xff; |
560 | buf[5] = (int) (memaddr) & 0xff; | |
c906108c SS |
561 | buf[6] = 1; |
562 | buf[7] = 0; | |
563 | ||
564 | for (i = 0; i < todo; i++) | |
565 | buf[i + 8] = myaddr[i]; | |
566 | ||
567 | sds_send (buf, 8 + todo); | |
568 | ||
569 | /* (should look at result) */ | |
570 | ||
571 | myaddr += todo; | |
572 | memaddr += todo; | |
573 | len -= todo; | |
574 | } | |
575 | return origlen; | |
576 | } | |
577 | ||
578 | /* Read memory data directly from the remote machine. This does not | |
579 | use the data cache; the data cache uses this. MEMADDR is the | |
580 | address in the remote memory space. MYADDR is the address of the | |
581 | buffer in our space. LEN is the number of bytes. | |
582 | ||
583 | Returns number of bytes transferred, or 0 for error. */ | |
584 | ||
585 | static int | |
fba45db2 | 586 | sds_read_bytes (CORE_ADDR memaddr, char *myaddr, int len) |
c906108c SS |
587 | { |
588 | int max_buf_size; /* Max size of packet output buffer */ | |
589 | int origlen, retlen; | |
590 | unsigned char buf[PBUFSIZ]; | |
591 | int todo; | |
592 | int i; | |
593 | ||
594 | /* Chop the transfer down if necessary */ | |
595 | ||
596 | max_buf_size = 150; | |
597 | ||
598 | origlen = len; | |
599 | while (len > 0) | |
600 | { | |
601 | todo = min (len, max_buf_size); | |
602 | ||
603 | buf[0] = 12; | |
604 | buf[1] = 0; | |
605 | buf[2] = (int) (memaddr >> 24) & 0xff; | |
606 | buf[3] = (int) (memaddr >> 16) & 0xff; | |
c5aa993b JM |
607 | buf[4] = (int) (memaddr >> 8) & 0xff; |
608 | buf[5] = (int) (memaddr) & 0xff; | |
c906108c | 609 | buf[6] = (int) (todo >> 8) & 0xff; |
c5aa993b | 610 | buf[7] = (int) (todo) & 0xff; |
c906108c SS |
611 | buf[8] = 1; |
612 | ||
613 | retlen = sds_send (buf, 9); | |
614 | ||
615 | if (retlen - 2 != todo) | |
616 | { | |
617 | return 0; | |
618 | } | |
619 | ||
620 | /* Reply describes memory byte by byte. */ | |
621 | ||
622 | for (i = 0; i < todo; i++) | |
623 | myaddr[i] = buf[i + 2]; | |
624 | ||
625 | myaddr += todo; | |
626 | memaddr += todo; | |
627 | len -= todo; | |
628 | } | |
629 | ||
630 | return origlen; | |
631 | } | |
632 | \f | |
633 | /* Read or write LEN bytes from inferior memory at MEMADDR, | |
634 | transferring to or from debugger address MYADDR. Write to inferior | |
635 | if SHOULD_WRITE is nonzero. Returns length of data written or | |
d93bce06 | 636 | read; 0 for error. TARGET is unused. */ |
c906108c | 637 | |
c906108c | 638 | static int |
d93bce06 | 639 | sds_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int should_write, |
0a65a603 | 640 | struct mem_attrib *attrib, struct target_ops *target) |
c906108c | 641 | { |
4930751a C |
642 | int res; |
643 | ||
644 | if (should_write) | |
645 | res = sds_write_bytes (memaddr, myaddr, len); | |
646 | else | |
647 | res = sds_read_bytes (memaddr, myaddr, len); | |
648 | ||
649 | return res; | |
c906108c | 650 | } |
c906108c | 651 | \f |
c5aa993b | 652 | |
c906108c | 653 | static void |
fba45db2 | 654 | sds_files_info (struct target_ops *ignore) |
c906108c SS |
655 | { |
656 | puts_filtered ("Debugging over a serial connection, using SDS protocol.\n"); | |
657 | } | |
658 | \f | |
659 | /* Stuff for dealing with the packets which are part of this protocol. | |
660 | See comment at top of file for details. */ | |
661 | ||
662 | /* Read a single character from the remote end, masking it down to 7 bits. */ | |
663 | ||
664 | static int | |
fba45db2 | 665 | readchar (int timeout) |
c906108c SS |
666 | { |
667 | int ch; | |
668 | ||
2cd58942 | 669 | ch = serial_readchar (sds_desc, timeout); |
c906108c SS |
670 | |
671 | if (remote_debug > 1 && ch >= 0) | |
9846de1b | 672 | fprintf_unfiltered (gdb_stdlog, "%c(%x)", ch, ch); |
c906108c SS |
673 | |
674 | switch (ch) | |
675 | { | |
676 | case SERIAL_EOF: | |
677 | error ("Remote connection closed"); | |
678 | case SERIAL_ERROR: | |
679 | perror_with_name ("Remote communication error"); | |
680 | case SERIAL_TIMEOUT: | |
681 | return ch; | |
682 | default: | |
683 | return ch & 0x7f; | |
684 | } | |
685 | } | |
686 | ||
687 | /* An SDS-style checksum is a sum of the bytes modulo 253. (Presumably | |
688 | because 253, 254, and 255 are special flags in the protocol.) */ | |
689 | ||
690 | static int | |
fba45db2 | 691 | compute_checksum (int csum, char *buf, int len) |
c906108c SS |
692 | { |
693 | int i; | |
694 | ||
695 | for (i = 0; i < len; ++i) | |
696 | csum += (unsigned char) buf[i]; | |
697 | ||
698 | csum %= 253; | |
699 | return csum; | |
700 | } | |
701 | ||
702 | /* Send the command in BUF to the remote machine, and read the reply | |
703 | into BUF also. */ | |
704 | ||
705 | static int | |
fba45db2 | 706 | sds_send (unsigned char *buf, int len) |
c906108c SS |
707 | { |
708 | putmessage (buf, len); | |
709 | ||
710 | return getmessage (buf, 0); | |
711 | } | |
712 | ||
713 | /* Send a message to the remote machine. */ | |
714 | ||
715 | static int | |
fba45db2 | 716 | putmessage (unsigned char *buf, int len) |
c906108c SS |
717 | { |
718 | int i, enclen; | |
719 | unsigned char csum = 0; | |
720 | char buf2[PBUFSIZ], buf3[PBUFSIZ]; | |
721 | unsigned char header[3]; | |
c906108c SS |
722 | char *p; |
723 | ||
724 | /* Copy the packet into buffer BUF2, encapsulating it | |
725 | and giving it a checksum. */ | |
726 | ||
727 | if (len > 170) /* Prosanity check */ | |
e2e0b3e5 | 728 | internal_error (__FILE__, __LINE__, _("failed internal consistency check")); |
c906108c SS |
729 | |
730 | if (remote_debug) | |
731 | { | |
9846de1b | 732 | fprintf_unfiltered (gdb_stdlog, "Message to send: \""); |
c906108c | 733 | for (i = 0; i < len; ++i) |
9846de1b JM |
734 | fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]); |
735 | fprintf_unfiltered (gdb_stdlog, "\"\n"); | |
c906108c SS |
736 | } |
737 | ||
738 | p = buf2; | |
739 | *p++ = '$'; | |
740 | ||
741 | if (len % 3 != 0) | |
742 | { | |
743 | buf[len] = '\0'; | |
c5aa993b | 744 | buf[len + 1] = '\0'; |
c906108c SS |
745 | } |
746 | ||
747 | header[1] = next_msg_id; | |
748 | ||
749 | header[2] = len; | |
750 | ||
751 | csum = compute_checksum (csum, buf, len); | |
752 | csum = compute_checksum (csum, header + 1, 2); | |
753 | ||
754 | header[0] = csum; | |
755 | ||
756 | tob64 (header, p, 3); | |
757 | p += 4; | |
758 | enclen = tob64 (buf, buf3, ((len + 2) / 3) * 3); | |
759 | ||
760 | for (i = 0; i < enclen; ++i) | |
761 | *p++ = buf3[i]; | |
762 | *p++ = '\r'; | |
763 | *p++ = '\n'; | |
764 | ||
765 | next_msg_id = (next_msg_id + 3) % 245; | |
766 | ||
767 | /* Send it over and over until we get a positive ack. */ | |
768 | ||
769 | while (1) | |
770 | { | |
c906108c SS |
771 | if (remote_debug) |
772 | { | |
773 | *p = '\0'; | |
9846de1b JM |
774 | fprintf_unfiltered (gdb_stdlog, "Sending encoded: \"%s\"", buf2); |
775 | fprintf_unfiltered (gdb_stdlog, | |
776 | " (Checksum %d, id %d, length %d)\n", | |
777 | header[0], header[1], header[2]); | |
778 | gdb_flush (gdb_stdlog); | |
c906108c | 779 | } |
2cd58942 | 780 | if (serial_write (sds_desc, buf2, p - buf2)) |
c906108c SS |
781 | perror_with_name ("putmessage: write failed"); |
782 | ||
783 | return 1; | |
c906108c | 784 | } |
c906108c SS |
785 | } |
786 | ||
787 | /* Come here after finding the start of the frame. Collect the rest | |
788 | into BUF. Returns 0 on any error, 1 on success. */ | |
789 | ||
790 | static int | |
fba45db2 | 791 | read_frame (char *buf) |
c906108c SS |
792 | { |
793 | char *bp; | |
794 | int c; | |
795 | ||
796 | bp = buf; | |
797 | ||
798 | while (1) | |
799 | { | |
800 | c = readchar (sds_timeout); | |
801 | ||
802 | switch (c) | |
803 | { | |
804 | case SERIAL_TIMEOUT: | |
805 | if (remote_debug) | |
9846de1b | 806 | fputs_filtered ("Timeout in mid-message, retrying\n", gdb_stdlog); |
c906108c SS |
807 | return 0; |
808 | case '$': | |
809 | if (remote_debug) | |
9846de1b JM |
810 | fputs_filtered ("Saw new packet start in middle of old one\n", |
811 | gdb_stdlog); | |
c906108c SS |
812 | return 0; /* Start a new packet, count retries */ |
813 | case '\r': | |
814 | break; | |
815 | ||
816 | case '\n': | |
817 | { | |
818 | *bp = '\000'; | |
819 | if (remote_debug) | |
9846de1b | 820 | fprintf_unfiltered (gdb_stdlog, "Received encoded: \"%s\"\n", |
c906108c SS |
821 | buf); |
822 | return 1; | |
823 | } | |
824 | ||
825 | default: | |
826 | if (bp < buf + PBUFSIZ - 1) | |
827 | { | |
828 | *bp++ = c; | |
829 | continue; | |
830 | } | |
831 | ||
832 | *bp = '\0'; | |
833 | puts_filtered ("Message too long: "); | |
834 | puts_filtered (buf); | |
835 | puts_filtered ("\n"); | |
836 | ||
837 | return 0; | |
838 | } | |
839 | } | |
840 | } | |
841 | ||
842 | /* Read a packet from the remote machine, with error checking, | |
843 | and store it in BUF. BUF is expected to be of size PBUFSIZ. | |
844 | If FOREVER, wait forever rather than timing out; this is used | |
845 | while the target is executing user code. */ | |
846 | ||
847 | static int | |
fba45db2 | 848 | getmessage (unsigned char *buf, int forever) |
c906108c SS |
849 | { |
850 | int c, c2, c3; | |
851 | int tries; | |
852 | int timeout; | |
853 | int val, i, len, csum; | |
854 | unsigned char header[3]; | |
855 | unsigned char inbuf[500]; | |
856 | ||
857 | strcpy (buf, "timeout"); | |
858 | ||
859 | if (forever) | |
860 | { | |
c906108c | 861 | timeout = watchdog > 0 ? watchdog : -1; |
c906108c SS |
862 | } |
863 | ||
864 | else | |
865 | timeout = sds_timeout; | |
866 | ||
867 | #define MAX_TRIES 3 | |
868 | ||
869 | for (tries = 1; tries <= MAX_TRIES; tries++) | |
870 | { | |
871 | /* This can loop forever if the remote side sends us characters | |
c5aa993b JM |
872 | continuously, but if it pauses, we'll get a zero from readchar |
873 | because of timeout. Then we'll count that as a retry. */ | |
c906108c SS |
874 | |
875 | /* Note that we will only wait forever prior to the start of a packet. | |
c5aa993b JM |
876 | After that, we expect characters to arrive at a brisk pace. They |
877 | should show up within sds_timeout intervals. */ | |
c906108c SS |
878 | |
879 | do | |
880 | { | |
881 | c = readchar (timeout); | |
882 | ||
883 | if (c == SERIAL_TIMEOUT) | |
884 | { | |
c906108c SS |
885 | if (forever) /* Watchdog went off. Kill the target. */ |
886 | { | |
887 | target_mourn_inferior (); | |
888 | error ("Watchdog has expired. Target detached.\n"); | |
889 | } | |
c906108c | 890 | if (remote_debug) |
9846de1b | 891 | fputs_filtered ("Timed out.\n", gdb_stdlog); |
c906108c SS |
892 | goto retry; |
893 | } | |
894 | } | |
895 | while (c != '$' && c != '{'); | |
896 | ||
897 | /* We might have seen a "trigraph", a sequence of three characters | |
c5aa993b | 898 | that indicate various sorts of communication state. */ |
c906108c SS |
899 | |
900 | if (c == '{') | |
901 | { | |
902 | /* Read the other two chars of the trigraph. */ | |
903 | c2 = readchar (timeout); | |
904 | c3 = readchar (timeout); | |
905 | if (remote_debug) | |
9846de1b | 906 | fprintf_unfiltered (gdb_stdlog, "Trigraph %c%c%c received\n", |
c906108c SS |
907 | c, c2, c3); |
908 | if (c3 == '+') | |
909 | { | |
910 | message_pending = 1; | |
c5aa993b | 911 | return 0; /*???? */ |
c906108c SS |
912 | } |
913 | continue; | |
914 | } | |
915 | ||
916 | val = read_frame (inbuf); | |
917 | ||
918 | if (val == 1) | |
919 | { | |
920 | fromb64 (inbuf, header, 4); | |
921 | /* (should check out other bits) */ | |
922 | fromb64 (inbuf + 4, buf, strlen (inbuf) - 4); | |
923 | ||
924 | len = header[2]; | |
925 | ||
926 | csum = 0; | |
927 | csum = compute_checksum (csum, buf, len); | |
928 | csum = compute_checksum (csum, header + 1, 2); | |
929 | ||
930 | if (csum != header[0]) | |
931 | fprintf_unfiltered (gdb_stderr, | |
c5aa993b | 932 | "Checksum mismatch: computed %d, received %d\n", |
c906108c SS |
933 | csum, header[0]); |
934 | ||
935 | if (header[2] == 0xff) | |
936 | fprintf_unfiltered (gdb_stderr, "Requesting resend...\n"); | |
937 | ||
938 | if (remote_debug) | |
939 | { | |
9846de1b | 940 | fprintf_unfiltered (gdb_stdlog, |
c5aa993b | 941 | "... (Got checksum %d, id %d, length %d)\n", |
c906108c | 942 | header[0], header[1], header[2]); |
9846de1b | 943 | fprintf_unfiltered (gdb_stdlog, "Message received: \""); |
c906108c SS |
944 | for (i = 0; i < len; ++i) |
945 | { | |
9846de1b | 946 | fprintf_unfiltered (gdb_stdlog, "%02x", (unsigned char) buf[i]); |
c906108c | 947 | } |
9846de1b | 948 | fprintf_unfiltered (gdb_stdlog, "\"\n"); |
c906108c SS |
949 | } |
950 | ||
951 | /* no ack required? */ | |
952 | return len; | |
953 | } | |
954 | ||
955 | /* Try the whole thing again. */ | |
956 | retry: | |
957 | /* need to do something here */ | |
bdf64bac | 958 | ; |
c906108c SS |
959 | } |
960 | ||
961 | /* We have tried hard enough, and just can't receive the packet. Give up. */ | |
962 | ||
963 | printf_unfiltered ("Ignoring packet error, continuing...\n"); | |
964 | return 0; | |
965 | } | |
966 | \f | |
967 | static void | |
fba45db2 | 968 | sds_kill (void) |
c906108c SS |
969 | { |
970 | /* Don't try to do anything to the target. */ | |
971 | } | |
972 | ||
973 | static void | |
fba45db2 | 974 | sds_mourn (void) |
c906108c SS |
975 | { |
976 | unpush_target (&sds_ops); | |
977 | generic_mourn_inferior (); | |
978 | } | |
979 | ||
980 | static void | |
c27cda74 | 981 | sds_create_inferior (char *exec_file, char *args, char **env, int from_tty) |
c906108c | 982 | { |
39f77062 | 983 | inferior_ptid = pid_to_ptid (42000); |
c906108c SS |
984 | |
985 | /* Clean up from the last time we were running. */ | |
986 | clear_proceed_status (); | |
987 | ||
281b533b | 988 | write_pc (bfd_get_start_address (exec_bfd)); |
c906108c SS |
989 | } |
990 | ||
991 | static void | |
fba45db2 | 992 | sds_load (char *filename, int from_tty) |
c906108c SS |
993 | { |
994 | generic_load (filename, from_tty); | |
995 | ||
39f77062 | 996 | inferior_ptid = null_ptid; |
c906108c SS |
997 | } |
998 | \f | |
999 | /* The SDS monitor has commands for breakpoint insertion, although it | |
1000 | it doesn't actually manage the breakpoints, it just returns the | |
1001 | replaced instruction back to the debugger. */ | |
1002 | ||
1003 | static int | |
8181d85f | 1004 | sds_insert_breakpoint (struct bp_target_info *bp_tgt) |
c906108c | 1005 | { |
8181d85f | 1006 | CORE_ADDR addr = bp_tgt->placed_address; |
c906108c SS |
1007 | int i, retlen; |
1008 | unsigned char *p, buf[PBUFSIZ]; | |
1009 | ||
1010 | p = buf; | |
1011 | *p++ = 16; | |
1012 | *p++ = 0; | |
1013 | *p++ = (int) (addr >> 24) & 0xff; | |
1014 | *p++ = (int) (addr >> 16) & 0xff; | |
c5aa993b JM |
1015 | *p++ = (int) (addr >> 8) & 0xff; |
1016 | *p++ = (int) (addr) & 0xff; | |
1017 | ||
c906108c SS |
1018 | retlen = sds_send (buf, p - buf); |
1019 | ||
1020 | for (i = 0; i < 4; ++i) | |
8181d85f | 1021 | bp_tgt->shadow_contents[i] = buf[i + 2]; |
c906108c SS |
1022 | |
1023 | return 0; | |
1024 | } | |
1025 | ||
1026 | static int | |
8181d85f | 1027 | sds_remove_breakpoint (struct bp_target_info *bp_tgt) |
c906108c | 1028 | { |
8181d85f | 1029 | CORE_ADDR addr = bp_tgt->placed_address; |
c906108c SS |
1030 | int i, retlen; |
1031 | unsigned char *p, buf[PBUFSIZ]; | |
1032 | ||
1033 | p = buf; | |
1034 | *p++ = 17; | |
1035 | *p++ = 0; | |
1036 | *p++ = (int) (addr >> 24) & 0xff; | |
1037 | *p++ = (int) (addr >> 16) & 0xff; | |
c5aa993b JM |
1038 | *p++ = (int) (addr >> 8) & 0xff; |
1039 | *p++ = (int) (addr) & 0xff; | |
c906108c | 1040 | for (i = 0; i < 4; ++i) |
8181d85f | 1041 | *p++ = bp_tgt->shadow_contents[i]; |
c906108c SS |
1042 | |
1043 | retlen = sds_send (buf, p - buf); | |
1044 | ||
1045 | return 0; | |
1046 | } | |
1047 | \f | |
c5aa993b | 1048 | static void |
fba45db2 | 1049 | init_sds_ops (void) |
c906108c SS |
1050 | { |
1051 | sds_ops.to_shortname = "sds"; | |
1052 | sds_ops.to_longname = "Remote serial target with SDS protocol"; | |
1053 | sds_ops.to_doc = "Use a remote computer via a serial line; using the SDS protocol.\n\ | |
1054 | Specify the serial device it is connected to (e.g. /dev/ttya)."; | |
1055 | sds_ops.to_open = sds_open; | |
1056 | sds_ops.to_close = sds_close; | |
1057 | sds_ops.to_detach = sds_detach; | |
1058 | sds_ops.to_resume = sds_resume; | |
1059 | sds_ops.to_wait = sds_wait; | |
1060 | sds_ops.to_fetch_registers = sds_fetch_registers; | |
1061 | sds_ops.to_store_registers = sds_store_registers; | |
1062 | sds_ops.to_prepare_to_store = sds_prepare_to_store; | |
c8e73a31 | 1063 | sds_ops.deprecated_xfer_memory = sds_xfer_memory; |
c906108c SS |
1064 | sds_ops.to_files_info = sds_files_info; |
1065 | sds_ops.to_insert_breakpoint = sds_insert_breakpoint; | |
1066 | sds_ops.to_remove_breakpoint = sds_remove_breakpoint; | |
1067 | sds_ops.to_kill = sds_kill; | |
1068 | sds_ops.to_load = sds_load; | |
1069 | sds_ops.to_create_inferior = sds_create_inferior; | |
1070 | sds_ops.to_mourn_inferior = sds_mourn; | |
1071 | sds_ops.to_stratum = process_stratum; | |
1072 | sds_ops.to_has_all_memory = 1; | |
1073 | sds_ops.to_has_memory = 1; | |
1074 | sds_ops.to_has_stack = 1; | |
1075 | sds_ops.to_has_registers = 1; | |
1076 | sds_ops.to_has_execution = 1; | |
1077 | sds_ops.to_magic = OPS_MAGIC; | |
1078 | } | |
1079 | ||
1080 | /* Put a command string, in args, out to the monitor and display the | |
1081 | reply message. */ | |
1082 | ||
1083 | static void | |
fba45db2 | 1084 | sds_command (char *args, int from_tty) |
c906108c SS |
1085 | { |
1086 | char *p; | |
1087 | int i, len, retlen; | |
1088 | unsigned char buf[1000]; | |
1089 | ||
1090 | /* Convert hexadecimal chars into a byte buffer. */ | |
1091 | p = args; | |
1092 | len = 0; | |
1093 | while (*p != '\0') | |
1094 | { | |
1095 | buf[len++] = fromhex (p[0]) * 16 + fromhex (p[1]); | |
1096 | if (p[1] == '\0') | |
1097 | break; | |
1098 | p += 2; | |
1099 | } | |
1100 | ||
1101 | retlen = sds_send (buf, len); | |
1102 | ||
1103 | printf_filtered ("Reply is "); | |
1104 | for (i = 0; i < retlen; ++i) | |
1105 | { | |
1106 | printf_filtered ("%02x", buf[i]); | |
c5aa993b | 1107 | } |
c906108c SS |
1108 | printf_filtered ("\n"); |
1109 | } | |
1110 | ||
1111 | void | |
fba45db2 | 1112 | _initialize_remote_sds (void) |
c906108c SS |
1113 | { |
1114 | init_sds_ops (); | |
1115 | add_target (&sds_ops); | |
1116 | ||
4d28ad1e AC |
1117 | add_setshow_integer_cmd ("sdstimeout", no_class, &sds_timeout, _("\ |
1118 | Set timeout value for sds read."), _("\ | |
1119 | Show timeout value for sds read."), NULL, | |
1120 | NULL, | |
1121 | NULL, /* FIXME: i18n: */ | |
1122 | &setlist, &showlist); | |
c906108c SS |
1123 | |
1124 | add_com ("sds", class_obscure, sds_command, | |
1bedd215 | 1125 | _("Send a command to the SDS monitor.")); |
c906108c | 1126 | } |