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