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