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