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