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