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