]>
Commit | Line | Data |
---|---|---|
b543979c | 1 | /* Remote target communications for serial-line targets in custom GDB protocol |
7c622b41 | 2 | Copyright 1988, 1991, 1992, 1993 Free Software Foundation, Inc. |
bd5635a1 RP |
3 | |
4 | This file is part of GDB. | |
5 | ||
b543979c | 6 | This program is free software; you can redistribute it and/or modify |
bd5635a1 | 7 | it under the terms of the GNU General Public License as published by |
b543979c JG |
8 | the Free Software Foundation; either version 2 of the License, or |
9 | (at your option) any later version. | |
bd5635a1 | 10 | |
b543979c | 11 | This program is distributed in the hope that it will be useful, |
bd5635a1 RP |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
b543979c JG |
17 | along with this program; if not, write to the Free Software |
18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
bd5635a1 RP |
19 | |
20 | /* Remote communication protocol. | |
e50ebec8 JK |
21 | |
22 | A debug packet whose contents are <data> | |
23 | is encapsulated for transmission in the form: | |
24 | ||
25 | $ <data> # CSUM1 CSUM2 | |
26 | ||
27 | <data> must be ASCII alphanumeric and cannot include characters | |
28 | '$' or '#' | |
29 | ||
30 | CSUM1 and CSUM2 are ascii hex representation of an 8-bit | |
31 | checksum of <data>, the most significant nibble is sent first. | |
32 | the hex digits 0-9,a-f are used. | |
33 | ||
34 | Receiver responds with: | |
35 | ||
36 | + - if CSUM is correct and ready for next packet | |
37 | - - if CSUM is incorrect | |
38 | ||
39 | <data> is as follows: | |
bd5635a1 RP |
40 | All values are encoded in ascii hex digits. |
41 | ||
42 | Request Packet | |
43 | ||
44 | read registers g | |
45 | reply XX....X Each byte of register data | |
46 | is described by two hex digits. | |
47 | Registers are in the internal order | |
48 | for GDB, and the bytes in a register | |
49 | are in the same order the machine uses. | |
50 | or ENN for an error. | |
51 | ||
52 | write regs GXX..XX Each byte of register data | |
53 | is described by two hex digits. | |
54 | reply OK for success | |
55 | ENN for an error | |
56 | ||
57 | read mem mAA..AA,LLLL AA..AA is address, LLLL is length. | |
58 | reply XX..XX XX..XX is mem contents | |
59 | or ENN NN is errno | |
60 | ||
61 | write mem MAA..AA,LLLL:XX..XX | |
62 | AA..AA is address, | |
63 | LLLL is number of bytes, | |
64 | XX..XX is data | |
65 | reply OK for success | |
66 | ENN for an error | |
67 | ||
68 | cont cAA..AA AA..AA is address to resume | |
69 | If AA..AA is omitted, | |
70 | resume at same address. | |
71 | ||
72 | step sAA..AA AA..AA is address to resume | |
73 | If AA..AA is omitted, | |
74 | resume at same address. | |
75 | ||
76 | last signal ? Reply the current reason for stopping. | |
77 | This is the same reply as is generated | |
78 | for step or cont : SAA where AA is the | |
79 | signal number. | |
80 | ||
81 | There is no immediate reply to step or cont. | |
82 | The reply comes when the machine stops. | |
83 | It is SAA AA is the "signal number" | |
84 | ||
e50ebec8 JK |
85 | or... TAAn...:r...;n:r...;n...:r...; |
86 | AA = signal number | |
87 | n... = register number | |
88 | r... = register contents | |
8f86a4e4 | 89 | |
bd5635a1 RP |
90 | kill req k |
91 | */ | |
92 | ||
d747e0af | 93 | #include "defs.h" |
bd5635a1 RP |
94 | #include <string.h> |
95 | #include <fcntl.h> | |
bd5635a1 RP |
96 | #include "frame.h" |
97 | #include "inferior.h" | |
e50ebec8 | 98 | #include "bfd.h" |
6b27ebe8 | 99 | #include "symfile.h" |
bd5635a1 RP |
100 | #include "target.h" |
101 | #include "wait.h" | |
102 | #include "terminal.h" | |
8f86a4e4 | 103 | #include "gdbcmd.h" |
bd5635a1 | 104 | |
8f86a4e4 | 105 | #if !defined(DONT_USE_REMOTE) |
bd5635a1 RP |
106 | #ifdef USG |
107 | #include <sys/types.h> | |
108 | #endif | |
109 | ||
110 | #include <signal.h> | |
ebdb9ade | 111 | #include "serial.h" |
bd5635a1 | 112 | |
b543979c JG |
113 | /* Prototypes for local functions */ |
114 | ||
115 | static void | |
116 | remote_write_bytes PARAMS ((CORE_ADDR, char *, int)); | |
117 | ||
118 | static void | |
119 | remote_read_bytes PARAMS ((CORE_ADDR, char *, int)); | |
120 | ||
121 | static void | |
122 | remote_files_info PARAMS ((struct target_ops *)); | |
123 | ||
124 | static int | |
125 | remote_xfer_memory PARAMS ((CORE_ADDR, char *, int, int, struct target_ops *)); | |
126 | ||
127 | static void | |
128 | remote_prepare_to_store PARAMS ((void)); | |
129 | ||
130 | static void | |
131 | remote_fetch_registers PARAMS ((int)); | |
132 | ||
133 | static void | |
134 | remote_resume PARAMS ((int, int)); | |
135 | ||
7c622b41 JG |
136 | static int |
137 | remote_start_remote PARAMS ((char *)); | |
138 | ||
b543979c JG |
139 | static void |
140 | remote_open PARAMS ((char *, int)); | |
141 | ||
142 | static void | |
143 | remote_close PARAMS ((int)); | |
144 | ||
145 | static void | |
146 | remote_store_registers PARAMS ((int)); | |
147 | ||
148 | static void | |
7c622b41 | 149 | getpkt PARAMS ((char *, int)); |
b543979c JG |
150 | |
151 | static void | |
152 | putpkt PARAMS ((char *)); | |
153 | ||
154 | static void | |
155 | remote_send PARAMS ((char *)); | |
156 | ||
157 | static int | |
158 | readchar PARAMS ((void)); | |
159 | ||
160 | static int | |
161 | remote_wait PARAMS ((WAITTYPE *)); | |
162 | ||
163 | static int | |
164 | tohex PARAMS ((int)); | |
165 | ||
166 | static int | |
167 | fromhex PARAMS ((int)); | |
168 | ||
169 | static void | |
170 | remote_detach PARAMS ((char *, int)); | |
171 | ||
bd5635a1 RP |
172 | extern struct target_ops remote_ops; /* Forward decl */ |
173 | ||
8f86a4e4 | 174 | static int kiodebug = 0; |
ebdb9ade JK |
175 | /* This was 5 seconds, which is a long time to sit and wait. |
176 | Unless this is going though some terminal server or multiplexer or | |
177 | other form of hairy serial connection, I would think 2 seconds would | |
178 | be plenty. */ | |
179 | static int timeout = 2; | |
bd5635a1 RP |
180 | |
181 | #if 0 | |
182 | int icache; | |
183 | #endif | |
184 | ||
185 | /* Descriptor for I/O to remote machine. Initialize it to -1 so that | |
186 | remote_open knows that we don't have a file open when the program | |
187 | starts. */ | |
ebdb9ade | 188 | serial_t remote_desc = NULL; |
bd5635a1 | 189 | |
b543979c | 190 | #define PBUFSIZ 1024 |
bd5635a1 RP |
191 | |
192 | /* Maximum number of bytes to read/write at once. The value here | |
193 | is chosen to fill up a packet (the headers account for the 32). */ | |
194 | #define MAXBUFBYTES ((PBUFSIZ-32)/2) | |
195 | ||
b543979c JG |
196 | /* Round up PBUFSIZ to hold all the registers, at least. */ |
197 | #if REGISTER_BYTES > MAXBUFBYTES | |
198 | #undef PBUFSIZ | |
199 | #define PBUFSIZ (REGISTER_BYTES * 2 + 32) | |
bd5635a1 | 200 | #endif |
bd5635a1 | 201 | \f |
bd5635a1 RP |
202 | /* Clean up connection to a remote debugger. */ |
203 | ||
e1ce8aa5 | 204 | /* ARGSUSED */ |
b543979c | 205 | static void |
bd5635a1 RP |
206 | remote_close (quitting) |
207 | int quitting; | |
208 | { | |
ebdb9ade JK |
209 | if (remote_desc) |
210 | SERIAL_CLOSE (remote_desc); | |
211 | remote_desc = NULL; | |
b543979c JG |
212 | } |
213 | ||
7c622b41 JG |
214 | /* Stub for catch_errors. */ |
215 | ||
216 | static int | |
217 | remote_start_remote (dummy) | |
218 | char *dummy; | |
219 | { | |
220 | /* Ack any packet which the remote side has already sent. */ | |
ebdb9ade | 221 | SERIAL_WRITE (remote_desc, "+\r", 2); |
7c622b41 JG |
222 | putpkt ("?"); /* initiate a query from remote machine */ |
223 | ||
224 | start_remote (); /* Initialize gdb process mechanisms */ | |
225 | return 1; | |
226 | } | |
227 | ||
bd5635a1 RP |
228 | /* Open a connection to a remote debugger. |
229 | NAME is the filename used for communication. */ | |
230 | ||
b543979c | 231 | static void |
bd5635a1 RP |
232 | remote_open (name, from_tty) |
233 | char *name; | |
234 | int from_tty; | |
235 | { | |
bd5635a1 RP |
236 | if (name == 0) |
237 | error ( | |
238 | "To open a remote debug connection, you need to specify what serial\n\ | |
239 | device is attached to the remote system (e.g. /dev/ttya)."); | |
240 | ||
f2fc6e7a JK |
241 | target_preopen (from_tty); |
242 | ||
ebdb9ade | 243 | unpush_target (&remote_ops); |
bd5635a1 RP |
244 | |
245 | #if 0 | |
246 | dcache_init (); | |
247 | #endif | |
248 | ||
ebdb9ade JK |
249 | remote_desc = SERIAL_OPEN (name); |
250 | if (!remote_desc) | |
bd5635a1 RP |
251 | perror_with_name (name); |
252 | ||
b543979c JG |
253 | if (baud_rate) |
254 | { | |
ebdb9ade | 255 | int rate; |
b543979c | 256 | |
ebdb9ade JK |
257 | if (sscanf (baud_rate, "%d", &rate) == 1) |
258 | if (SERIAL_SETBAUDRATE (remote_desc, rate)) | |
259 | { | |
260 | SERIAL_CLOSE (remote_desc); | |
261 | perror_with_name (name); | |
262 | } | |
b543979c | 263 | } |
ebdb9ade JK |
264 | |
265 | SERIAL_RAW (remote_desc); | |
bd5635a1 RP |
266 | |
267 | if (from_tty) | |
7c622b41 JG |
268 | { |
269 | puts_filtered ("Remote debugging using "); | |
270 | puts_filtered (name); | |
271 | puts_filtered ("\n"); | |
272 | } | |
bd5635a1 | 273 | push_target (&remote_ops); /* Switch to using remote target now */ |
bd5635a1 | 274 | |
7c622b41 JG |
275 | /* Start the remote connection; if error (0), discard this target. */ |
276 | immediate_quit++; /* Allow user to interrupt it */ | |
277 | if (!catch_errors (remote_start_remote, (char *)0, | |
e50ebec8 | 278 | "Couldn't establish connection to remote target\n", RETURN_MASK_ALL)) |
7c622b41 | 279 | pop_target(); |
bd5635a1 RP |
280 | } |
281 | ||
282 | /* remote_detach() | |
283 | takes a program previously attached to and detaches it. | |
284 | We better not have left any breakpoints | |
285 | in the program or it'll die when it hits one. | |
286 | Close the open connection to the remote debugger. | |
287 | Use this when you want to detach and do something else | |
288 | with your gdb. */ | |
289 | ||
290 | static void | |
291 | remote_detach (args, from_tty) | |
292 | char *args; | |
293 | int from_tty; | |
294 | { | |
295 | if (args) | |
296 | error ("Argument given to \"detach\" when remotely debugging."); | |
297 | ||
298 | pop_target (); | |
299 | if (from_tty) | |
7c622b41 | 300 | puts_filtered ("Ending remote debugging.\n"); |
bd5635a1 RP |
301 | } |
302 | ||
303 | /* Convert hex digit A to a number. */ | |
304 | ||
305 | static int | |
306 | fromhex (a) | |
307 | int a; | |
308 | { | |
309 | if (a >= '0' && a <= '9') | |
310 | return a - '0'; | |
311 | else if (a >= 'a' && a <= 'f') | |
312 | return a - 'a' + 10; | |
313 | else | |
314 | error ("Reply contains invalid hex digit"); | |
315 | return -1; | |
316 | } | |
317 | ||
318 | /* Convert number NIB to a hex digit. */ | |
319 | ||
320 | static int | |
321 | tohex (nib) | |
322 | int nib; | |
323 | { | |
324 | if (nib < 10) | |
325 | return '0'+nib; | |
326 | else | |
327 | return 'a'+nib-10; | |
328 | } | |
329 | \f | |
330 | /* Tell the remote machine to resume. */ | |
331 | ||
b543979c | 332 | static void |
bd5635a1 RP |
333 | remote_resume (step, siggnal) |
334 | int step, siggnal; | |
335 | { | |
336 | char buf[PBUFSIZ]; | |
337 | ||
338 | if (siggnal) | |
ebdb9ade JK |
339 | { |
340 | char *name; | |
341 | target_terminal_ours_for_output (); | |
342 | printf_filtered ("Can't send signals to a remote system. "); | |
343 | name = strsigno (siggnal); | |
344 | if (name) | |
345 | printf_filtered (name); | |
346 | else | |
347 | printf_filtered ("Signal %d", siggnal); | |
348 | printf_filtered (" not sent.\n"); | |
349 | target_terminal_inferior (); | |
350 | } | |
bd5635a1 RP |
351 | |
352 | #if 0 | |
353 | dcache_flush (); | |
354 | #endif | |
355 | ||
356 | strcpy (buf, step ? "s": "c"); | |
357 | ||
358 | putpkt (buf); | |
359 | } | |
ebdb9ade JK |
360 | \f |
361 | static void remote_interrupt_twice PARAMS ((int)); | |
362 | static void (*ofunc)(); | |
bd5635a1 | 363 | |
b543979c JG |
364 | /* Send ^C to target to halt it. Target will respond, and send us a |
365 | packet. */ | |
366 | ||
e676a15f FF |
367 | void remote_interrupt(signo) |
368 | int signo; | |
b543979c | 369 | { |
ebdb9ade JK |
370 | /* If this doesn't work, try more severe steps. */ |
371 | signal (signo, remote_interrupt_twice); | |
8f86a4e4 JG |
372 | |
373 | if (kiodebug) | |
374 | printf ("remote_interrupt called\n"); | |
375 | ||
ebdb9ade | 376 | SERIAL_WRITE (remote_desc, "\003", 1); /* Send a ^C */ |
b543979c JG |
377 | } |
378 | ||
ebdb9ade JK |
379 | /* The user typed ^C twice. */ |
380 | static void | |
381 | remote_interrupt_twice (signo) | |
382 | int signo; | |
383 | { | |
384 | signal (signo, ofunc); | |
385 | ||
386 | target_terminal_ours (); | |
6b27ebe8 | 387 | if (query ("Interrupted while waiting for the program.\n\ |
ebdb9ade JK |
388 | Give up (and stop debugging it)? ")) |
389 | { | |
390 | target_mourn_inferior (); | |
e50ebec8 | 391 | return_to_top_level (RETURN_QUIT); |
ebdb9ade JK |
392 | } |
393 | else | |
394 | { | |
395 | signal (signo, remote_interrupt); | |
396 | target_terminal_inferior (); | |
397 | } | |
398 | } | |
b543979c | 399 | |
bd5635a1 | 400 | /* Wait until the remote machine stops, then return, |
e1ce8aa5 JK |
401 | storing status in STATUS just as `wait' would. |
402 | Returns "pid" (though it's not clear what, if anything, that | |
403 | means in the case of this target). */ | |
bd5635a1 | 404 | |
b543979c | 405 | static int |
bd5635a1 RP |
406 | remote_wait (status) |
407 | WAITTYPE *status; | |
408 | { | |
409 | unsigned char buf[PBUFSIZ]; | |
8f86a4e4 JG |
410 | unsigned char *p; |
411 | int i; | |
a03d4f8e | 412 | long regno; |
34517ebc | 413 | char regs[MAX_REGISTER_RAW_SIZE]; |
8f86a4e4 | 414 | |
bd5635a1 | 415 | WSETEXIT ((*status), 0); |
b543979c | 416 | |
38094c60 | 417 | ofunc = (void (*)()) signal (SIGINT, remote_interrupt); |
7c622b41 | 418 | getpkt ((char *) buf, 1); |
b543979c JG |
419 | signal (SIGINT, ofunc); |
420 | ||
bd5635a1 RP |
421 | if (buf[0] == 'E') |
422 | error ("Remote failure reply: %s", buf); | |
8f86a4e4 JG |
423 | if (buf[0] == 'T') |
424 | { | |
4ecee2f9 | 425 | /* Expedited reply, containing Signal, {regno, reg} repeat */ |
a03d4f8e JG |
426 | /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where |
427 | ss = signal number | |
428 | n... = register number | |
429 | r... = register contents | |
430 | */ | |
431 | ||
8f86a4e4 | 432 | p = &buf[3]; /* after Txx */ |
4ecee2f9 SG |
433 | |
434 | while (*p) | |
8f86a4e4 | 435 | { |
a03d4f8e JG |
436 | regno = strtol (p, &p, 16); /* Read the register number */ |
437 | ||
438 | if (*p++ != ':' | |
439 | || regno >= NUM_REGS) | |
440 | error ("Remote sent bad register number %s", buf); | |
4ecee2f9 SG |
441 | |
442 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i++) | |
443 | { | |
444 | if (p[0] == 0 || p[1] == 0) | |
445 | error ("Remote reply is too short: %s", buf); | |
446 | regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]); | |
447 | p += 2; | |
448 | } | |
449 | ||
a03d4f8e JG |
450 | if (*p++ != ';') |
451 | error("Remote register badly formatted: %s", buf); | |
452 | ||
4ecee2f9 | 453 | supply_register (regno, regs); |
8f86a4e4 | 454 | } |
8f86a4e4 JG |
455 | } |
456 | else if (buf[0] != 'S') | |
bd5635a1 | 457 | error ("Invalid remote reply: %s", buf); |
8f86a4e4 | 458 | |
bd5635a1 | 459 | WSETSTOP ((*status), (((fromhex (buf[1])) << 4) + (fromhex (buf[2])))); |
8f86a4e4 | 460 | |
e1ce8aa5 | 461 | return 0; |
bd5635a1 RP |
462 | } |
463 | ||
464 | /* Read the remote registers into the block REGS. */ | |
e1ce8aa5 JK |
465 | /* Currently we just read all the registers, so we don't use regno. */ |
466 | /* ARGSUSED */ | |
b543979c | 467 | static void |
bd5635a1 RP |
468 | remote_fetch_registers (regno) |
469 | int regno; | |
470 | { | |
471 | char buf[PBUFSIZ]; | |
472 | int i; | |
473 | char *p; | |
474 | char regs[REGISTER_BYTES]; | |
475 | ||
476 | sprintf (buf, "g"); | |
477 | remote_send (buf); | |
478 | ||
479 | /* Reply describes registers byte by byte, each byte encoded as two | |
480 | hex characters. Suck them all up, then supply them to the | |
481 | register cacheing/storage mechanism. */ | |
482 | ||
483 | p = buf; | |
484 | for (i = 0; i < REGISTER_BYTES; i++) | |
485 | { | |
486 | if (p[0] == 0 || p[1] == 0) | |
487 | error ("Remote reply is too short: %s", buf); | |
488 | regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]); | |
489 | p += 2; | |
490 | } | |
491 | for (i = 0; i < NUM_REGS; i++) | |
492 | supply_register (i, ®s[REGISTER_BYTE(i)]); | |
bd5635a1 RP |
493 | } |
494 | ||
495 | /* Prepare to store registers. Since we send them all, we have to | |
496 | read out the ones we don't want to change first. */ | |
497 | ||
b543979c | 498 | static void |
bd5635a1 RP |
499 | remote_prepare_to_store () |
500 | { | |
34517ebc JG |
501 | /* Make sure the entire registers array is valid. */ |
502 | read_register_bytes (0, (char *)NULL, REGISTER_BYTES); | |
bd5635a1 RP |
503 | } |
504 | ||
505 | /* Store the remote registers from the contents of the block REGISTERS. | |
506 | FIXME, eventually just store one register if that's all that is needed. */ | |
507 | ||
e1ce8aa5 | 508 | /* ARGSUSED */ |
b543979c | 509 | static void |
bd5635a1 RP |
510 | remote_store_registers (regno) |
511 | int regno; | |
512 | { | |
513 | char buf[PBUFSIZ]; | |
514 | int i; | |
515 | char *p; | |
516 | ||
517 | buf[0] = 'G'; | |
518 | ||
519 | /* Command describes registers byte by byte, | |
520 | each byte encoded as two hex characters. */ | |
521 | ||
522 | p = buf + 1; | |
523 | for (i = 0; i < REGISTER_BYTES; i++) | |
524 | { | |
525 | *p++ = tohex ((registers[i] >> 4) & 0xf); | |
526 | *p++ = tohex (registers[i] & 0xf); | |
527 | } | |
528 | *p = '\0'; | |
529 | ||
530 | remote_send (buf); | |
bd5635a1 RP |
531 | } |
532 | ||
533 | #if 0 | |
534 | /* Read a word from remote address ADDR and return it. | |
535 | This goes through the data cache. */ | |
536 | ||
537 | int | |
538 | remote_fetch_word (addr) | |
539 | CORE_ADDR addr; | |
540 | { | |
541 | if (icache) | |
542 | { | |
543 | extern CORE_ADDR text_start, text_end; | |
544 | ||
545 | if (addr >= text_start && addr < text_end) | |
546 | { | |
547 | int buffer; | |
548 | xfer_core_file (addr, &buffer, sizeof (int)); | |
549 | return buffer; | |
550 | } | |
551 | } | |
552 | return dcache_fetch (addr); | |
553 | } | |
554 | ||
555 | /* Write a word WORD into remote address ADDR. | |
556 | This goes through the data cache. */ | |
557 | ||
558 | void | |
559 | remote_store_word (addr, word) | |
560 | CORE_ADDR addr; | |
561 | int word; | |
562 | { | |
563 | dcache_poke (addr, word); | |
564 | } | |
565 | #endif /* 0 */ | |
566 | \f | |
567 | /* Write memory data directly to the remote machine. | |
568 | This does not inform the data cache; the data cache uses this. | |
569 | MEMADDR is the address in the remote memory space. | |
570 | MYADDR is the address of the buffer in our space. | |
571 | LEN is the number of bytes. */ | |
572 | ||
b543979c | 573 | static void |
bd5635a1 RP |
574 | remote_write_bytes (memaddr, myaddr, len) |
575 | CORE_ADDR memaddr; | |
576 | char *myaddr; | |
577 | int len; | |
578 | { | |
579 | char buf[PBUFSIZ]; | |
580 | int i; | |
581 | char *p; | |
582 | ||
583 | if (len > PBUFSIZ / 2 - 20) | |
584 | abort (); | |
585 | ||
586 | sprintf (buf, "M%x,%x:", memaddr, len); | |
587 | ||
b543979c | 588 | /* We send target system values byte by byte, in increasing byte addresses, |
bd5635a1 RP |
589 | each byte encoded as two hex characters. */ |
590 | ||
591 | p = buf + strlen (buf); | |
592 | for (i = 0; i < len; i++) | |
593 | { | |
594 | *p++ = tohex ((myaddr[i] >> 4) & 0xf); | |
595 | *p++ = tohex (myaddr[i] & 0xf); | |
596 | } | |
597 | *p = '\0'; | |
598 | ||
599 | remote_send (buf); | |
600 | } | |
601 | ||
602 | /* Read memory data directly from the remote machine. | |
603 | This does not use the data cache; the data cache uses this. | |
604 | MEMADDR is the address in the remote memory space. | |
605 | MYADDR is the address of the buffer in our space. | |
606 | LEN is the number of bytes. */ | |
607 | ||
b543979c | 608 | static void |
bd5635a1 RP |
609 | remote_read_bytes (memaddr, myaddr, len) |
610 | CORE_ADDR memaddr; | |
611 | char *myaddr; | |
612 | int len; | |
613 | { | |
614 | char buf[PBUFSIZ]; | |
615 | int i; | |
616 | char *p; | |
617 | ||
618 | if (len > PBUFSIZ / 2 - 1) | |
619 | abort (); | |
620 | ||
621 | sprintf (buf, "m%x,%x", memaddr, len); | |
622 | remote_send (buf); | |
623 | ||
b543979c | 624 | /* Reply describes memory byte by byte, |
bd5635a1 RP |
625 | each byte encoded as two hex characters. */ |
626 | ||
627 | p = buf; | |
628 | for (i = 0; i < len; i++) | |
629 | { | |
630 | if (p[0] == 0 || p[1] == 0) | |
631 | error ("Remote reply is too short: %s", buf); | |
632 | myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]); | |
633 | p += 2; | |
634 | } | |
635 | } | |
636 | \f | |
637 | /* Read or write LEN bytes from inferior memory at MEMADDR, transferring | |
e1ce8aa5 | 638 | to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is |
bd5635a1 RP |
639 | nonzero. Returns length of data written or read; 0 for error. */ |
640 | ||
b543979c JG |
641 | /* ARGSUSED */ |
642 | static int | |
643 | remote_xfer_memory(memaddr, myaddr, len, should_write, target) | |
bd5635a1 RP |
644 | CORE_ADDR memaddr; |
645 | char *myaddr; | |
646 | int len; | |
e1ce8aa5 | 647 | int should_write; |
b543979c | 648 | struct target_ops *target; /* ignored */ |
bd5635a1 RP |
649 | { |
650 | int origlen = len; | |
651 | int xfersize; | |
652 | while (len > 0) | |
653 | { | |
654 | if (len > MAXBUFBYTES) | |
655 | xfersize = MAXBUFBYTES; | |
656 | else | |
657 | xfersize = len; | |
658 | ||
e1ce8aa5 | 659 | if (should_write) |
bd5635a1 RP |
660 | remote_write_bytes(memaddr, myaddr, xfersize); |
661 | else | |
662 | remote_read_bytes (memaddr, myaddr, xfersize); | |
663 | memaddr += xfersize; | |
664 | myaddr += xfersize; | |
665 | len -= xfersize; | |
666 | } | |
667 | return origlen; /* no error possible */ | |
668 | } | |
669 | ||
b543979c | 670 | static void |
8f86a4e4 JG |
671 | remote_files_info (ignore) |
672 | struct target_ops *ignore; | |
bd5635a1 | 673 | { |
7c622b41 | 674 | puts_filtered ("Debugging a target over a serial line.\n"); |
bd5635a1 RP |
675 | } |
676 | \f | |
e50ebec8 JK |
677 | /* Stuff for dealing with the packets which are part of this protocol. |
678 | See comment at top of file for details. */ | |
bd5635a1 | 679 | |
ebdb9ade | 680 | /* Read a single character from the remote end, masking it down to 7 bits. */ |
b543979c | 681 | |
bd5635a1 RP |
682 | static int |
683 | readchar () | |
684 | { | |
ebdb9ade | 685 | int ch; |
bd5635a1 | 686 | |
ebdb9ade | 687 | ch = SERIAL_READCHAR (remote_desc, timeout); |
fce7f2d9 | 688 | |
ebdb9ade JK |
689 | if (ch < 0) |
690 | return ch; | |
bd5635a1 | 691 | |
ebdb9ade | 692 | return ch & 0x7f; |
bd5635a1 RP |
693 | } |
694 | ||
695 | /* Send the command in BUF to the remote machine, | |
696 | and read the reply into BUF. | |
697 | Report an error if we get an error reply. */ | |
698 | ||
699 | static void | |
700 | remote_send (buf) | |
701 | char *buf; | |
702 | { | |
703 | ||
704 | putpkt (buf); | |
7c622b41 | 705 | getpkt (buf, 0); |
bd5635a1 RP |
706 | |
707 | if (buf[0] == 'E') | |
708 | error ("Remote failure reply: %s", buf); | |
709 | } | |
710 | ||
711 | /* Send a packet to the remote machine, with error checking. | |
712 | The data of the packet is in BUF. */ | |
713 | ||
714 | static void | |
715 | putpkt (buf) | |
716 | char *buf; | |
717 | { | |
718 | int i; | |
719 | unsigned char csum = 0; | |
b543979c | 720 | char buf2[PBUFSIZ]; |
bd5635a1 | 721 | int cnt = strlen (buf); |
ebdb9ade | 722 | int ch; |
bd5635a1 RP |
723 | char *p; |
724 | ||
725 | /* Copy the packet into buffer BUF2, encapsulating it | |
726 | and giving it a checksum. */ | |
727 | ||
b543979c JG |
728 | if (cnt > sizeof(buf2) - 5) /* Prosanity check */ |
729 | abort(); | |
730 | ||
bd5635a1 RP |
731 | p = buf2; |
732 | *p++ = '$'; | |
733 | ||
734 | for (i = 0; i < cnt; i++) | |
735 | { | |
736 | csum += buf[i]; | |
737 | *p++ = buf[i]; | |
738 | } | |
739 | *p++ = '#'; | |
740 | *p++ = tohex ((csum >> 4) & 0xf); | |
741 | *p++ = tohex (csum & 0xf); | |
742 | ||
743 | /* Send it over and over until we get a positive ack. */ | |
744 | ||
6b27ebe8 JK |
745 | while (1) |
746 | { | |
747 | if (kiodebug) | |
748 | { | |
749 | *p = '\0'; | |
750 | printf ("Sending packet: %s...", buf2); fflush(stdout); | |
751 | } | |
752 | if (SERIAL_WRITE (remote_desc, buf2, p - buf2)) | |
753 | perror_with_name ("putpkt: write failed"); | |
754 | ||
755 | /* read until either a timeout occurs (-2) or '+' is read */ | |
756 | while (1) | |
757 | { | |
758 | ch = readchar (); | |
759 | ||
760 | switch (ch) | |
761 | { | |
762 | case '+': | |
763 | if (kiodebug) | |
764 | printf("Ack\n"); | |
765 | return; | |
766 | case SERIAL_TIMEOUT: | |
767 | break; /* Retransmit buffer */ | |
768 | case SERIAL_ERROR: | |
769 | perror_with_name ("putpkt: couldn't read ACK"); | |
770 | case SERIAL_EOF: | |
771 | error ("putpkt: EOF while trying to read ACK"); | |
772 | default: | |
773 | if (kiodebug) | |
774 | printf ("%02X %c ", ch&0xFF, ch); | |
775 | continue; | |
776 | } | |
777 | break; /* Here to retransmit */ | |
778 | } | |
779 | } | |
bd5635a1 RP |
780 | } |
781 | ||
782 | /* Read a packet from the remote machine, with error checking, | |
7c622b41 JG |
783 | and store it in BUF. BUF is expected to be of size PBUFSIZ. |
784 | If FOREVER, wait forever rather than timing out; this is used | |
785 | while the target is executing user code. */ | |
bd5635a1 RP |
786 | |
787 | static void | |
7c622b41 | 788 | getpkt (buf, forever) |
bd5635a1 | 789 | char *buf; |
ebdb9ade | 790 | int forever; |
bd5635a1 RP |
791 | { |
792 | char *bp; | |
793 | unsigned char csum; | |
7c622b41 | 794 | int c = 0; |
bd5635a1 | 795 | unsigned char c1, c2; |
38094c60 JG |
796 | int retries = 0; |
797 | #define MAX_RETRIES 10 | |
bd5635a1 | 798 | |
bd5635a1 RP |
799 | while (1) |
800 | { | |
7c622b41 JG |
801 | /* This can loop forever if the remote side sends us characters |
802 | continuously, but if it pauses, we'll get a zero from readchar | |
803 | because of timeout. Then we'll count that as a retry. */ | |
6b27ebe8 JK |
804 | |
805 | c = readchar(); | |
806 | if (c > 0 && c != '$') | |
807 | continue; | |
808 | ||
809 | if (c == SERIAL_TIMEOUT) | |
810 | { | |
811 | if (forever) | |
812 | continue; | |
813 | if (++retries >= MAX_RETRIES) | |
814 | if (kiodebug) puts_filtered ("Timed out.\n"); | |
815 | goto out; | |
816 | } | |
817 | ||
818 | if (c == SERIAL_EOF) | |
819 | error ("Remote connection closed"); | |
820 | if (c == SERIAL_ERROR) | |
821 | perror_with_name ("Remote communication error"); | |
7c622b41 | 822 | |
bd5635a1 RP |
823 | /* Force csum to be zero here because of possible error retry. */ |
824 | csum = 0; | |
bd5635a1 | 825 | bp = buf; |
7c622b41 | 826 | |
bd5635a1 RP |
827 | while (1) |
828 | { | |
829 | c = readchar (); | |
ebdb9ade | 830 | if (c == SERIAL_TIMEOUT) |
7c622b41 JG |
831 | { |
832 | if (kiodebug) | |
833 | puts_filtered ("Timeout in mid-packet, retrying\n"); | |
834 | goto whole; /* Start a new packet, count retries */ | |
835 | } | |
836 | if (c == '$') | |
837 | { | |
838 | if (kiodebug) | |
839 | puts_filtered ("Saw new packet start in middle of old one\n"); | |
840 | goto whole; /* Start a new packet, count retries */ | |
841 | } | |
bd5635a1 RP |
842 | if (c == '#') |
843 | break; | |
8f86a4e4 JG |
844 | if (bp >= buf+PBUFSIZ-1) |
845 | { | |
846 | *bp = '\0'; | |
7c622b41 JG |
847 | puts_filtered ("Remote packet too long: "); |
848 | puts_filtered (buf); | |
849 | puts_filtered ("\n"); | |
8f86a4e4 JG |
850 | goto whole; |
851 | } | |
bd5635a1 RP |
852 | *bp++ = c; |
853 | csum += c; | |
854 | } | |
855 | *bp = 0; | |
856 | ||
857 | c1 = fromhex (readchar ()); | |
858 | c2 = fromhex (readchar ()); | |
859 | if ((csum & 0xff) == (c1 << 4) + c2) | |
860 | break; | |
7c622b41 JG |
861 | printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=", |
862 | (c1 << 4) + c2, csum & 0xff); | |
863 | puts_filtered (buf); | |
864 | puts_filtered ("\n"); | |
38094c60 | 865 | |
8f86a4e4 JG |
866 | /* Try the whole thing again. */ |
867 | whole: | |
38094c60 JG |
868 | if (++retries < MAX_RETRIES) |
869 | { | |
ebdb9ade | 870 | SERIAL_WRITE (remote_desc, "-", 1); |
38094c60 JG |
871 | } |
872 | else | |
873 | { | |
874 | printf ("Ignoring packet error, continuing...\n"); | |
875 | break; | |
876 | } | |
bd5635a1 RP |
877 | } |
878 | ||
7c622b41 JG |
879 | out: |
880 | ||
ebdb9ade | 881 | SERIAL_WRITE (remote_desc, "+", 1); |
bd5635a1 RP |
882 | |
883 | if (kiodebug) | |
8f86a4e4 | 884 | fprintf (stderr,"Packet received: %s\n", buf); |
bd5635a1 RP |
885 | } |
886 | \f | |
887 | /* The data cache leads to incorrect results because it doesn't know about | |
888 | volatile variables, thus making it impossible to debug functions which | |
889 | use hardware registers. Therefore it is #if 0'd out. Effect on | |
890 | performance is some, for backtraces of functions with a few | |
891 | arguments each. For functions with many arguments, the stack | |
892 | frames don't fit in the cache blocks, which makes the cache less | |
893 | helpful. Disabling the cache is a big performance win for fetching | |
894 | large structures, because the cache code fetched data in 16-byte | |
895 | chunks. */ | |
896 | #if 0 | |
897 | /* The data cache records all the data read from the remote machine | |
898 | since the last time it stopped. | |
899 | ||
900 | Each cache block holds 16 bytes of data | |
901 | starting at a multiple-of-16 address. */ | |
902 | ||
903 | #define DCACHE_SIZE 64 /* Number of cache blocks */ | |
904 | ||
905 | struct dcache_block { | |
906 | struct dcache_block *next, *last; | |
907 | unsigned int addr; /* Address for which data is recorded. */ | |
908 | int data[4]; | |
909 | }; | |
910 | ||
911 | struct dcache_block dcache_free, dcache_valid; | |
912 | ||
913 | /* Free all the data cache blocks, thus discarding all cached data. */ | |
914 | ||
915 | static void | |
916 | dcache_flush () | |
917 | { | |
918 | register struct dcache_block *db; | |
919 | ||
920 | while ((db = dcache_valid.next) != &dcache_valid) | |
921 | { | |
922 | remque (db); | |
923 | insque (db, &dcache_free); | |
924 | } | |
925 | } | |
926 | ||
927 | /* | |
928 | * If addr is present in the dcache, return the address of the block | |
929 | * containing it. | |
930 | */ | |
931 | ||
932 | struct dcache_block * | |
933 | dcache_hit (addr) | |
934 | { | |
935 | register struct dcache_block *db; | |
936 | ||
937 | if (addr & 3) | |
938 | abort (); | |
939 | ||
940 | /* Search all cache blocks for one that is at this address. */ | |
941 | db = dcache_valid.next; | |
942 | while (db != &dcache_valid) | |
943 | { | |
944 | if ((addr & 0xfffffff0) == db->addr) | |
945 | return db; | |
946 | db = db->next; | |
947 | } | |
948 | return NULL; | |
949 | } | |
950 | ||
951 | /* Return the int data at address ADDR in dcache block DC. */ | |
952 | ||
953 | int | |
954 | dcache_value (db, addr) | |
955 | struct dcache_block *db; | |
956 | unsigned int addr; | |
957 | { | |
958 | if (addr & 3) | |
959 | abort (); | |
960 | return (db->data[(addr>>2)&3]); | |
961 | } | |
962 | ||
963 | /* Get a free cache block, put it on the valid list, | |
964 | and return its address. The caller should store into the block | |
965 | the address and data that it describes. */ | |
966 | ||
967 | struct dcache_block * | |
968 | dcache_alloc () | |
969 | { | |
970 | register struct dcache_block *db; | |
971 | ||
972 | if ((db = dcache_free.next) == &dcache_free) | |
973 | /* If we can't get one from the free list, take last valid */ | |
974 | db = dcache_valid.last; | |
975 | ||
976 | remque (db); | |
977 | insque (db, &dcache_valid); | |
978 | return (db); | |
979 | } | |
980 | ||
981 | /* Return the contents of the word at address ADDR in the remote machine, | |
982 | using the data cache. */ | |
983 | ||
984 | int | |
985 | dcache_fetch (addr) | |
986 | CORE_ADDR addr; | |
987 | { | |
988 | register struct dcache_block *db; | |
989 | ||
990 | db = dcache_hit (addr); | |
991 | if (db == 0) | |
992 | { | |
993 | db = dcache_alloc (); | |
994 | remote_read_bytes (addr & ~0xf, db->data, 16); | |
995 | db->addr = addr & ~0xf; | |
996 | } | |
997 | return (dcache_value (db, addr)); | |
998 | } | |
999 | ||
1000 | /* Write the word at ADDR both in the data cache and in the remote machine. */ | |
1001 | ||
1002 | dcache_poke (addr, data) | |
1003 | CORE_ADDR addr; | |
1004 | int data; | |
1005 | { | |
1006 | register struct dcache_block *db; | |
1007 | ||
1008 | /* First make sure the word is IN the cache. DB is its cache block. */ | |
1009 | db = dcache_hit (addr); | |
1010 | if (db == 0) | |
1011 | { | |
1012 | db = dcache_alloc (); | |
1013 | remote_read_bytes (addr & ~0xf, db->data, 16); | |
1014 | db->addr = addr & ~0xf; | |
1015 | } | |
1016 | ||
1017 | /* Modify the word in the cache. */ | |
1018 | db->data[(addr>>2)&3] = data; | |
1019 | ||
1020 | /* Send the changed word. */ | |
1021 | remote_write_bytes (addr, &data, 4); | |
1022 | } | |
1023 | ||
1024 | /* Initialize the data cache. */ | |
1025 | ||
1026 | dcache_init () | |
1027 | { | |
1028 | register i; | |
1029 | register struct dcache_block *db; | |
1030 | ||
1031 | db = (struct dcache_block *) xmalloc (sizeof (struct dcache_block) * | |
1032 | DCACHE_SIZE); | |
1033 | dcache_free.next = dcache_free.last = &dcache_free; | |
1034 | dcache_valid.next = dcache_valid.last = &dcache_valid; | |
1035 | for (i=0;i<DCACHE_SIZE;i++,db++) | |
1036 | insque (db, &dcache_free); | |
1037 | } | |
1038 | #endif /* 0 */ | |
ebdb9ade JK |
1039 | \f |
1040 | static void | |
1041 | remote_kill () | |
1042 | { | |
1043 | putpkt ("k"); | |
1044 | /* Don't wait for it to die. I'm not really sure it matters whether | |
1045 | we do or not. For the existing stubs, kill is a noop. */ | |
1046 | target_mourn_inferior (); | |
1047 | } | |
bd5635a1 | 1048 | |
ebdb9ade JK |
1049 | static void |
1050 | remote_mourn () | |
1051 | { | |
1052 | unpush_target (&remote_ops); | |
1053 | generic_mourn_inferior (); | |
1054 | } | |
1055 | \f | |
bd5635a1 RP |
1056 | /* Define the target subroutine names */ |
1057 | ||
1058 | struct target_ops remote_ops = { | |
b543979c JG |
1059 | "remote", /* to_shortname */ |
1060 | "Remote serial target in gdb-specific protocol", /* to_longname */ | |
1061 | "Use a remote computer via a serial line, using a gdb-specific protocol.\n\ | |
1062 | Specify the serial device it is connected to (e.g. /dev/ttya).", /* to_doc */ | |
1063 | remote_open, /* to_open */ | |
1064 | remote_close, /* to_close */ | |
1065 | NULL, /* to_attach */ | |
1066 | remote_detach, /* to_detach */ | |
1067 | remote_resume, /* to_resume */ | |
1068 | remote_wait, /* to_wait */ | |
1069 | remote_fetch_registers, /* to_fetch_registers */ | |
1070 | remote_store_registers, /* to_store_registers */ | |
1071 | remote_prepare_to_store, /* to_prepare_to_store */ | |
b543979c JG |
1072 | remote_xfer_memory, /* to_xfer_memory */ |
1073 | remote_files_info, /* to_files_info */ | |
1074 | NULL, /* to_insert_breakpoint */ | |
1075 | NULL, /* to_remove_breakpoint */ | |
1076 | NULL, /* to_terminal_init */ | |
1077 | NULL, /* to_terminal_inferior */ | |
1078 | NULL, /* to_terminal_ours_for_output */ | |
1079 | NULL, /* to_terminal_ours */ | |
1080 | NULL, /* to_terminal_info */ | |
ebdb9ade | 1081 | remote_kill, /* to_kill */ |
6b27ebe8 | 1082 | generic_load, /* to_load */ |
b543979c JG |
1083 | NULL, /* to_lookup_symbol */ |
1084 | NULL, /* to_create_inferior */ | |
ebdb9ade | 1085 | remote_mourn, /* to_mourn_inferior */ |
34517ebc | 1086 | 0, /* to_can_run */ |
7c622b41 | 1087 | 0, /* to_notice_signals */ |
b543979c JG |
1088 | process_stratum, /* to_stratum */ |
1089 | NULL, /* to_next */ | |
1090 | 1, /* to_has_all_memory */ | |
1091 | 1, /* to_has_memory */ | |
1092 | 1, /* to_has_stack */ | |
1093 | 1, /* to_has_registers */ | |
1094 | 1, /* to_has_execution */ | |
1095 | NULL, /* sections */ | |
1096 | NULL, /* sections_end */ | |
1097 | OPS_MAGIC /* to_magic */ | |
bd5635a1 RP |
1098 | }; |
1099 | ||
1100 | void | |
1101 | _initialize_remote () | |
1102 | { | |
1103 | add_target (&remote_ops); | |
8f86a4e4 JG |
1104 | |
1105 | add_show_from_set ( | |
1106 | add_set_cmd ("remotedebug", no_class, var_boolean, (char *)&kiodebug, | |
1107 | "Set debugging of remote serial I/O.\n\ | |
1108 | When enabled, each packet sent or received with the remote target\n\ | |
1109 | is displayed.", &setlist), | |
1110 | &showlist); | |
bd5635a1 | 1111 | } |
8f86a4e4 JG |
1112 | |
1113 | #endif |