Commit | Line | Data |
---|---|---|
b543979c | 1 | /* Remote target communications for serial-line targets in custom GDB protocol |
94d4b713 | 2 | Copyright 1988, 1991, 1992, 1993, 1994 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 | |
0c993550 JK |
28 | '$' or '#'. If <data> starts with two characters followed by |
29 | ':', then the existing stubs interpret this as a sequence number. | |
e50ebec8 JK |
30 | |
31 | CSUM1 and CSUM2 are ascii hex representation of an 8-bit | |
32 | checksum of <data>, the most significant nibble is sent first. | |
33 | the hex digits 0-9,a-f are used. | |
34 | ||
35 | Receiver responds with: | |
36 | ||
37 | + - if CSUM is correct and ready for next packet | |
38 | - - if CSUM is incorrect | |
39 | ||
40 | <data> is as follows: | |
bd5635a1 RP |
41 | All values are encoded in ascii hex digits. |
42 | ||
43 | Request Packet | |
44 | ||
45 | read registers g | |
46 | reply XX....X Each byte of register data | |
47 | is described by two hex digits. | |
48 | Registers are in the internal order | |
49 | for GDB, and the bytes in a register | |
50 | are in the same order the machine uses. | |
51 | or ENN for an error. | |
52 | ||
53 | write regs GXX..XX Each byte of register data | |
54 | is described by two hex digits. | |
55 | reply OK for success | |
56 | ENN for an error | |
57 | ||
0c993550 | 58 | write reg Pn...=r... Write register n... with value r..., |
4aa6fe10 JK |
59 | which contains two hex digits for each |
60 | byte in the register (target byte | |
61 | order). | |
62 | reply OK for success | |
63 | ENN for an error | |
64 | (not supported by all stubs). | |
65 | ||
bd5635a1 RP |
66 | read mem mAA..AA,LLLL AA..AA is address, LLLL is length. |
67 | reply XX..XX XX..XX is mem contents | |
d538b510 RP |
68 | Can be fewer bytes than requested |
69 | if able to read only part of the data. | |
bd5635a1 RP |
70 | or ENN NN is errno |
71 | ||
72 | write mem MAA..AA,LLLL:XX..XX | |
73 | AA..AA is address, | |
74 | LLLL is number of bytes, | |
75 | XX..XX is data | |
76 | reply OK for success | |
d538b510 RP |
77 | ENN for an error (this includes the case |
78 | where only part of the data was | |
79 | written). | |
bd5635a1 RP |
80 | |
81 | cont cAA..AA AA..AA is address to resume | |
82 | If AA..AA is omitted, | |
83 | resume at same address. | |
84 | ||
85 | step sAA..AA AA..AA is address to resume | |
86 | If AA..AA is omitted, | |
87 | resume at same address. | |
88 | ||
89 | last signal ? Reply the current reason for stopping. | |
90 | This is the same reply as is generated | |
91 | for step or cont : SAA where AA is the | |
92 | signal number. | |
93 | ||
94 | There is no immediate reply to step or cont. | |
95 | The reply comes when the machine stops. | |
96 | It is SAA AA is the "signal number" | |
97 | ||
e50ebec8 JK |
98 | or... TAAn...:r...;n:r...;n...:r...; |
99 | AA = signal number | |
100 | n... = register number | |
101 | r... = register contents | |
72bba93b | 102 | or... WAA The process exited, and AA is |
758aeb93 ILT |
103 | the exit status. This is only |
104 | applicable for certains sorts of | |
105 | targets. | |
d538b510 RP |
106 | kill request k |
107 | ||
108 | toggle debug d toggle debug flag (see 386 & 68k stubs) | |
109 | reset r reset -- see sparc stub. | |
110 | reserved <other> On other requests, the stub should | |
111 | ignore the request and send an empty | |
112 | response ($#<checksum>). This way | |
113 | we can extend the protocol and GDB | |
114 | can tell whether the stub it is | |
115 | talking to uses the old or the new. | |
72bba93b | 116 | search tAA:PP,MM Search backwards starting at address |
94d4b713 JK |
117 | AA for a match with pattern PP and |
118 | mask MM. PP and MM are 4 bytes. | |
119 | Not supported by all stubs. | |
120 | ||
72bba93b SG |
121 | general query qXXXX Request info about XXXX. |
122 | general set QXXXX=yyyy Set value of XXXX to yyyy. | |
123 | query sect offs qOffsets Get section offsets. Reply is | |
124 | Text=xxx;Data=yyy;Bss=zzz | |
754e5da2 SG |
125 | console output Otext Send text to stdout. Only comes from |
126 | remote target. | |
72bba93b | 127 | |
94d4b713 JK |
128 | Responses can be run-length encoded to save space. A '*' means that |
129 | the next two characters are hex digits giving a repeat count which | |
130 | stands for that many repititions of the character preceding the '*'. | |
131 | Note that this means that responses cannot contain '*'. Example: | |
4aa6fe10 | 132 | "0*03" means the same as "0000". */ |
bd5635a1 | 133 | |
d747e0af | 134 | #include "defs.h" |
bd5635a1 RP |
135 | #include <string.h> |
136 | #include <fcntl.h> | |
bd5635a1 RP |
137 | #include "frame.h" |
138 | #include "inferior.h" | |
e50ebec8 | 139 | #include "bfd.h" |
6b27ebe8 | 140 | #include "symfile.h" |
bd5635a1 RP |
141 | #include "target.h" |
142 | #include "wait.h" | |
143 | #include "terminal.h" | |
8f86a4e4 | 144 | #include "gdbcmd.h" |
758aeb93 ILT |
145 | #include "objfiles.h" |
146 | #include "gdb-stabs.h" | |
bd5635a1 | 147 | |
d538b510 RP |
148 | #include "dcache.h" |
149 | ||
bd5635a1 RP |
150 | #ifdef USG |
151 | #include <sys/types.h> | |
152 | #endif | |
153 | ||
154 | #include <signal.h> | |
ebdb9ade | 155 | #include "serial.h" |
bd5635a1 | 156 | |
b543979c JG |
157 | /* Prototypes for local functions */ |
158 | ||
d538b510 RP |
159 | static int |
160 | remote_write_bytes PARAMS ((CORE_ADDR memaddr, unsigned char *myaddr, int len)); | |
b543979c | 161 | |
d538b510 RP |
162 | static int |
163 | remote_read_bytes PARAMS ((CORE_ADDR memaddr, unsigned char *myaddr, int len)); | |
b543979c JG |
164 | |
165 | static void | |
5af4f5f6 | 166 | remote_files_info PARAMS ((struct target_ops *ignore)); |
b543979c JG |
167 | |
168 | static int | |
5af4f5f6 JK |
169 | remote_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr, int len, |
170 | int should_write, struct target_ops *target)); | |
b543979c JG |
171 | |
172 | static void | |
173 | remote_prepare_to_store PARAMS ((void)); | |
174 | ||
175 | static void | |
5af4f5f6 | 176 | remote_fetch_registers PARAMS ((int regno)); |
b543979c JG |
177 | |
178 | static void | |
94d4b713 | 179 | remote_resume PARAMS ((int pid, int step, enum target_signal siggnal)); |
b543979c | 180 | |
7c622b41 | 181 | static int |
5af4f5f6 | 182 | remote_start_remote PARAMS ((char *dummy)); |
7c622b41 | 183 | |
b543979c | 184 | static void |
5af4f5f6 | 185 | remote_open PARAMS ((char *name, int from_tty)); |
b543979c JG |
186 | |
187 | static void | |
5af4f5f6 | 188 | remote_close PARAMS ((int quitting)); |
b543979c JG |
189 | |
190 | static void | |
5af4f5f6 | 191 | remote_store_registers PARAMS ((int regno)); |
b543979c JG |
192 | |
193 | static void | |
5af4f5f6 | 194 | getpkt PARAMS ((char *buf, int forever)); |
b543979c JG |
195 | |
196 | static void | |
5af4f5f6 | 197 | putpkt PARAMS ((char *buf)); |
b543979c JG |
198 | |
199 | static void | |
5af4f5f6 | 200 | remote_send PARAMS ((char *buf)); |
b543979c JG |
201 | |
202 | static int | |
754e5da2 | 203 | readchar PARAMS ((int timeout)); |
b543979c | 204 | |
94d4b713 | 205 | static int remote_wait PARAMS ((int pid, struct target_waitstatus *status)); |
b543979c JG |
206 | |
207 | static int | |
5af4f5f6 | 208 | tohex PARAMS ((int nib)); |
b543979c JG |
209 | |
210 | static int | |
5af4f5f6 | 211 | fromhex PARAMS ((int a)); |
b543979c JG |
212 | |
213 | static void | |
5af4f5f6 JK |
214 | remote_detach PARAMS ((char *args, int from_tty)); |
215 | ||
216 | static void | |
217 | remote_interrupt PARAMS ((int signo)); | |
218 | ||
219 | static void | |
220 | remote_interrupt_twice PARAMS ((int signo)); | |
b543979c | 221 | |
981a3309 SG |
222 | static void |
223 | interrupt_query PARAMS ((void)); | |
224 | ||
bd5635a1 RP |
225 | extern struct target_ops remote_ops; /* Forward decl */ |
226 | ||
ebdb9ade JK |
227 | /* This was 5 seconds, which is a long time to sit and wait. |
228 | Unless this is going though some terminal server or multiplexer or | |
229 | other form of hairy serial connection, I would think 2 seconds would | |
230 | be plenty. */ | |
754e5da2 | 231 | static int remote_timeout = 2; |
bd5635a1 RP |
232 | |
233 | #if 0 | |
234 | int icache; | |
235 | #endif | |
236 | ||
16e1d1d3 | 237 | /* Descriptor for I/O to remote machine. Initialize it to NULL so that |
bd5635a1 RP |
238 | remote_open knows that we don't have a file open when the program |
239 | starts. */ | |
ebdb9ade | 240 | serial_t remote_desc = NULL; |
bd5635a1 | 241 | |
4d57c599 JK |
242 | /* Having this larger than 400 causes us to be incompatible with m68k-stub.c |
243 | and i386-stub.c. Normally, no one would notice because it only matters | |
244 | for writing large chunks of memory (e.g. in downloads). Also, this needs | |
245 | to be more than 400 if required to hold the registers (see below, where | |
246 | we round it up based on REGISTER_BYTES). */ | |
247 | #define PBUFSIZ 400 | |
bd5635a1 RP |
248 | |
249 | /* Maximum number of bytes to read/write at once. The value here | |
250 | is chosen to fill up a packet (the headers account for the 32). */ | |
251 | #define MAXBUFBYTES ((PBUFSIZ-32)/2) | |
252 | ||
b543979c | 253 | /* Round up PBUFSIZ to hold all the registers, at least. */ |
2ddeed27 JK |
254 | /* The blank line after the #if seems to be required to work around a |
255 | bug in HP's PA compiler. */ | |
b543979c | 256 | #if REGISTER_BYTES > MAXBUFBYTES |
2ddeed27 JK |
257 | |
258 | #undef PBUFSIZ | |
b543979c | 259 | #define PBUFSIZ (REGISTER_BYTES * 2 + 32) |
bd5635a1 | 260 | #endif |
4aa6fe10 JK |
261 | |
262 | /* Should we try the 'P' request? If this is set to one when the stub | |
263 | doesn't support 'P', the only consequence is some unnecessary traffic. */ | |
264 | static int stub_supports_P = 1; | |
265 | ||
bd5635a1 | 266 | \f |
bd5635a1 RP |
267 | /* Clean up connection to a remote debugger. */ |
268 | ||
e1ce8aa5 | 269 | /* ARGSUSED */ |
b543979c | 270 | static void |
bd5635a1 RP |
271 | remote_close (quitting) |
272 | int quitting; | |
273 | { | |
ebdb9ade JK |
274 | if (remote_desc) |
275 | SERIAL_CLOSE (remote_desc); | |
276 | remote_desc = NULL; | |
b543979c JG |
277 | } |
278 | ||
72bba93b SG |
279 | /* Query the remote side for the text, data and bss offsets. */ |
280 | ||
281 | static void | |
282 | get_offsets () | |
283 | { | |
1c95d7ab | 284 | unsigned char buf[PBUFSIZ]; |
72bba93b SG |
285 | int nvals; |
286 | CORE_ADDR text_addr, data_addr, bss_addr; | |
287 | struct section_offsets *offs; | |
288 | ||
289 | putpkt ("qOffsets"); | |
290 | ||
1c95d7ab | 291 | getpkt (buf, 0); |
72bba93b | 292 | |
1c95d7ab JK |
293 | if (buf[0] == '\000') |
294 | return; /* Return silently. Stub doesn't support this | |
295 | command. */ | |
72bba93b SG |
296 | if (buf[0] == 'E') |
297 | { | |
298 | warning ("Remote failure reply: %s", buf); | |
299 | return; | |
300 | } | |
301 | ||
302 | nvals = sscanf (buf, "Text=%lx;Data=%lx;Bss=%lx", &text_addr, &data_addr, | |
303 | &bss_addr); | |
304 | if (nvals != 3) | |
305 | error ("Malformed response to offset query, %s", buf); | |
306 | ||
307 | if (symfile_objfile == NULL) | |
308 | return; | |
309 | ||
310 | offs = (struct section_offsets *) alloca (sizeof (struct section_offsets) | |
311 | + symfile_objfile->num_sections | |
312 | * sizeof (offs->offsets)); | |
313 | memcpy (offs, symfile_objfile->section_offsets, | |
314 | sizeof (struct section_offsets) | |
315 | + symfile_objfile->num_sections | |
316 | * sizeof (offs->offsets)); | |
317 | ||
4aa6fe10 JK |
318 | /* FIXME: This code assumes gdb-stabs.h is being used; it's broken |
319 | for xcoff, dwarf, sdb-coff, etc. But there is no simple | |
320 | canonical representation for this stuff. (Just what does "text" | |
321 | as seen by the stub mean, anyway? I think it means all sections | |
322 | with SEC_CODE set, but we currently have no way to deal with that). */ | |
323 | ||
72bba93b | 324 | ANOFFSET (offs, SECT_OFF_TEXT) = text_addr; |
1624c38f SG |
325 | |
326 | /* This is a temporary kludge to force data and bss to use the same offsets | |
327 | because that's what nlmconv does now. The real solution requires changes | |
328 | to the stub and remote.c that I don't have time to do right now. */ | |
329 | ||
72bba93b | 330 | ANOFFSET (offs, SECT_OFF_DATA) = data_addr; |
1624c38f | 331 | ANOFFSET (offs, SECT_OFF_BSS) = data_addr; |
72bba93b SG |
332 | |
333 | objfile_relocate (symfile_objfile, offs); | |
334 | } | |
335 | ||
7c622b41 JG |
336 | /* Stub for catch_errors. */ |
337 | ||
338 | static int | |
339 | remote_start_remote (dummy) | |
340 | char *dummy; | |
341 | { | |
ac7a377f JK |
342 | immediate_quit = 1; /* Allow user to interrupt it */ |
343 | ||
7c622b41 | 344 | /* Ack any packet which the remote side has already sent. */ |
72bba93b SG |
345 | |
346 | SERIAL_WRITE (remote_desc, "+", 1); | |
347 | ||
348 | get_offsets (); /* Get text, data & bss offsets */ | |
349 | ||
7c622b41 | 350 | putpkt ("?"); /* initiate a query from remote machine */ |
ac7a377f | 351 | immediate_quit = 0; |
7c622b41 JG |
352 | |
353 | start_remote (); /* Initialize gdb process mechanisms */ | |
72bba93b | 354 | |
7c622b41 JG |
355 | return 1; |
356 | } | |
357 | ||
bd5635a1 RP |
358 | /* Open a connection to a remote debugger. |
359 | NAME is the filename used for communication. */ | |
360 | ||
d538b510 RP |
361 | static DCACHE *remote_dcache; |
362 | ||
b543979c | 363 | static void |
bd5635a1 RP |
364 | remote_open (name, from_tty) |
365 | char *name; | |
366 | int from_tty; | |
367 | { | |
bd5635a1 RP |
368 | if (name == 0) |
369 | error ( | |
370 | "To open a remote debug connection, you need to specify what serial\n\ | |
371 | device is attached to the remote system (e.g. /dev/ttya)."); | |
372 | ||
f2fc6e7a JK |
373 | target_preopen (from_tty); |
374 | ||
ebdb9ade | 375 | unpush_target (&remote_ops); |
bd5635a1 | 376 | |
d538b510 | 377 | remote_dcache = dcache_init (remote_read_bytes, remote_write_bytes); |
bd5635a1 | 378 | |
ebdb9ade JK |
379 | remote_desc = SERIAL_OPEN (name); |
380 | if (!remote_desc) | |
bd5635a1 RP |
381 | perror_with_name (name); |
382 | ||
94d4b713 | 383 | if (baud_rate != -1) |
b543979c | 384 | { |
94d4b713 JK |
385 | if (SERIAL_SETBAUDRATE (remote_desc, baud_rate)) |
386 | { | |
387 | SERIAL_CLOSE (remote_desc); | |
388 | perror_with_name (name); | |
389 | } | |
b543979c | 390 | } |
ebdb9ade JK |
391 | |
392 | SERIAL_RAW (remote_desc); | |
bd5635a1 | 393 | |
e15f2a54 JK |
394 | /* If there is something sitting in the buffer we might take it as a |
395 | response to a command, which would be bad. */ | |
396 | SERIAL_FLUSH_INPUT (remote_desc); | |
397 | ||
bd5635a1 | 398 | if (from_tty) |
7c622b41 JG |
399 | { |
400 | puts_filtered ("Remote debugging using "); | |
401 | puts_filtered (name); | |
402 | puts_filtered ("\n"); | |
403 | } | |
bd5635a1 | 404 | push_target (&remote_ops); /* Switch to using remote target now */ |
bd5635a1 | 405 | |
4aa6fe10 JK |
406 | /* Start out by trying the 'P' request to set registers. We set this each |
407 | time that we open a new target so that if the user switches from one | |
408 | stub to another, we can (if the target is closed and reopened) cope. */ | |
409 | stub_supports_P = 1; | |
410 | ||
ac7a377f JK |
411 | /* Start the remote connection; if error (0), discard this target. |
412 | In particular, if the user quits, be sure to discard it | |
413 | (we'd be in an inconsistent state otherwise). */ | |
7c622b41 | 414 | if (!catch_errors (remote_start_remote, (char *)0, |
e50ebec8 | 415 | "Couldn't establish connection to remote target\n", RETURN_MASK_ALL)) |
7c622b41 | 416 | pop_target(); |
bd5635a1 RP |
417 | } |
418 | ||
419 | /* remote_detach() | |
420 | takes a program previously attached to and detaches it. | |
421 | We better not have left any breakpoints | |
422 | in the program or it'll die when it hits one. | |
423 | Close the open connection to the remote debugger. | |
424 | Use this when you want to detach and do something else | |
425 | with your gdb. */ | |
426 | ||
427 | static void | |
428 | remote_detach (args, from_tty) | |
429 | char *args; | |
430 | int from_tty; | |
431 | { | |
432 | if (args) | |
433 | error ("Argument given to \"detach\" when remotely debugging."); | |
434 | ||
435 | pop_target (); | |
436 | if (from_tty) | |
7c622b41 | 437 | puts_filtered ("Ending remote debugging.\n"); |
bd5635a1 RP |
438 | } |
439 | ||
440 | /* Convert hex digit A to a number. */ | |
441 | ||
442 | static int | |
443 | fromhex (a) | |
444 | int a; | |
445 | { | |
446 | if (a >= '0' && a <= '9') | |
447 | return a - '0'; | |
448 | else if (a >= 'a' && a <= 'f') | |
449 | return a - 'a' + 10; | |
450 | else | |
451 | error ("Reply contains invalid hex digit"); | |
bd5635a1 RP |
452 | } |
453 | ||
454 | /* Convert number NIB to a hex digit. */ | |
455 | ||
456 | static int | |
457 | tohex (nib) | |
458 | int nib; | |
459 | { | |
460 | if (nib < 10) | |
461 | return '0'+nib; | |
462 | else | |
463 | return 'a'+nib-10; | |
464 | } | |
465 | \f | |
466 | /* Tell the remote machine to resume. */ | |
467 | ||
b543979c | 468 | static void |
d538b510 | 469 | remote_resume (pid, step, siggnal) |
94d4b713 JK |
470 | int pid, step; |
471 | enum target_signal siggnal; | |
bd5635a1 RP |
472 | { |
473 | char buf[PBUFSIZ]; | |
474 | ||
475 | if (siggnal) | |
ebdb9ade | 476 | { |
ebdb9ade | 477 | target_terminal_ours_for_output (); |
94d4b713 JK |
478 | printf_filtered |
479 | ("Can't send signals to a remote system. %s not sent.\n", | |
480 | target_signal_to_name (siggnal)); | |
ebdb9ade JK |
481 | target_terminal_inferior (); |
482 | } | |
bd5635a1 | 483 | |
d538b510 | 484 | dcache_flush (remote_dcache); |
bd5635a1 RP |
485 | |
486 | strcpy (buf, step ? "s": "c"); | |
487 | ||
488 | putpkt (buf); | |
489 | } | |
ebdb9ade | 490 | \f |
b543979c JG |
491 | /* Send ^C to target to halt it. Target will respond, and send us a |
492 | packet. */ | |
493 | ||
5af4f5f6 JK |
494 | static void |
495 | remote_interrupt (signo) | |
e676a15f | 496 | int signo; |
b543979c | 497 | { |
ebdb9ade JK |
498 | /* If this doesn't work, try more severe steps. */ |
499 | signal (signo, remote_interrupt_twice); | |
8f86a4e4 | 500 | |
d0d8484a | 501 | if (remote_debug) |
199b2450 | 502 | printf_unfiltered ("remote_interrupt called\n"); |
8f86a4e4 | 503 | |
ebdb9ade | 504 | SERIAL_WRITE (remote_desc, "\003", 1); /* Send a ^C */ |
b543979c JG |
505 | } |
506 | ||
5af4f5f6 JK |
507 | static void (*ofunc)(); |
508 | ||
ebdb9ade JK |
509 | /* The user typed ^C twice. */ |
510 | static void | |
511 | remote_interrupt_twice (signo) | |
512 | int signo; | |
513 | { | |
514 | signal (signo, ofunc); | |
515 | ||
981a3309 SG |
516 | interrupt_query (); |
517 | ||
518 | signal (signo, remote_interrupt); | |
519 | } | |
520 | ||
521 | /* Ask the user what to do when an interrupt is received. */ | |
522 | ||
523 | static void | |
524 | interrupt_query () | |
525 | { | |
ebdb9ade | 526 | target_terminal_ours (); |
981a3309 | 527 | |
6b27ebe8 | 528 | if (query ("Interrupted while waiting for the program.\n\ |
ebdb9ade JK |
529 | Give up (and stop debugging it)? ")) |
530 | { | |
531 | target_mourn_inferior (); | |
e50ebec8 | 532 | return_to_top_level (RETURN_QUIT); |
ebdb9ade | 533 | } |
981a3309 SG |
534 | |
535 | target_terminal_inferior (); | |
ebdb9ade | 536 | } |
b543979c | 537 | |
bd5635a1 | 538 | /* Wait until the remote machine stops, then return, |
e1ce8aa5 JK |
539 | storing status in STATUS just as `wait' would. |
540 | Returns "pid" (though it's not clear what, if anything, that | |
541 | means in the case of this target). */ | |
bd5635a1 | 542 | |
b543979c | 543 | static int |
d0d8484a SG |
544 | remote_wait (pid, status) |
545 | int pid; | |
94d4b713 | 546 | struct target_waitstatus *status; |
bd5635a1 RP |
547 | { |
548 | unsigned char buf[PBUFSIZ]; | |
8f86a4e4 | 549 | |
94d4b713 JK |
550 | status->kind = TARGET_WAITKIND_EXITED; |
551 | status->value.integer = 0; | |
b543979c | 552 | |
4f8a48e5 | 553 | while (1) |
8f86a4e4 | 554 | { |
4f8a48e5 | 555 | unsigned char *p; |
a03d4f8e | 556 | |
4f8a48e5 ILT |
557 | ofunc = (void (*)()) signal (SIGINT, remote_interrupt); |
558 | getpkt ((char *) buf, 1); | |
559 | signal (SIGINT, ofunc); | |
4ecee2f9 | 560 | |
754e5da2 | 561 | switch (buf[0]) |
8f86a4e4 | 562 | { |
754e5da2 SG |
563 | case 'E': /* Error of some sort */ |
564 | warning ("Remote failure reply: %s", buf); | |
565 | continue; | |
566 | case 'T': /* Status with PC, SP, FP, ... */ | |
567 | { | |
568 | int i; | |
569 | long regno; | |
570 | char regs[MAX_REGISTER_RAW_SIZE]; | |
a03d4f8e | 571 | |
754e5da2 SG |
572 | /* Expedited reply, containing Signal, {regno, reg} repeat */ |
573 | /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where | |
574 | ss = signal number | |
575 | n... = register number | |
576 | r... = register contents | |
577 | */ | |
5af4f5f6 | 578 | |
754e5da2 | 579 | p = &buf[3]; /* after Txx */ |
5af4f5f6 | 580 | |
754e5da2 SG |
581 | while (*p) |
582 | { | |
583 | unsigned char *p1; | |
5af4f5f6 | 584 | |
754e5da2 | 585 | regno = strtol (p, &p1, 16); /* Read the register number */ |
5af4f5f6 | 586 | |
754e5da2 SG |
587 | if (p1 == p) |
588 | warning ("Remote sent badly formed register number: %s\nPacket: '%s'\n", | |
589 | p1, buf); | |
4ecee2f9 | 590 | |
754e5da2 | 591 | p = p1; |
4ecee2f9 | 592 | |
754e5da2 SG |
593 | if (*p++ != ':') |
594 | warning ("Malformed packet (missing colon): %s\nPacket: '%s'\n", | |
595 | p, buf); | |
a03d4f8e | 596 | |
754e5da2 SG |
597 | if (regno >= NUM_REGS) |
598 | warning ("Remote sent bad register number %d: %s\nPacket: '%s'\n", | |
599 | regno, p, buf); | |
4f8a48e5 | 600 | |
754e5da2 SG |
601 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i++) |
602 | { | |
603 | if (p[0] == 0 || p[1] == 0) | |
604 | warning ("Remote reply is too short: %s", buf); | |
605 | regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]); | |
606 | p += 2; | |
607 | } | |
4f8a48e5 | 608 | |
754e5da2 SG |
609 | if (*p++ != ';') |
610 | warning ("Remote register badly formatted: %s", buf); | |
611 | ||
612 | supply_register (regno, regs); | |
613 | } | |
614 | } | |
615 | /* fall through */ | |
616 | case 'S': /* Old style status, just signal only */ | |
617 | status->kind = TARGET_WAITKIND_STOPPED; | |
618 | status->value.sig = (enum target_signal) | |
619 | (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))); | |
4f8a48e5 | 620 | |
4f8a48e5 | 621 | return 0; |
754e5da2 SG |
622 | case 'W': /* Target exited */ |
623 | { | |
624 | /* The remote process exited. */ | |
625 | status->kind = TARGET_WAITKIND_EXITED; | |
626 | status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]); | |
627 | return 0; | |
628 | } | |
629 | case 'O': /* Console output */ | |
630 | fputs_filtered (buf + 1, gdb_stdout); | |
631 | continue; | |
632 | default: | |
633 | warning ("Invalid remote reply: %s", buf); | |
634 | continue; | |
4f8a48e5 | 635 | } |
758aeb93 | 636 | } |
e1ce8aa5 | 637 | return 0; |
bd5635a1 RP |
638 | } |
639 | ||
55fea07b JK |
640 | /* Number of bytes of registers this stub implements. */ |
641 | static int register_bytes_found; | |
642 | ||
bd5635a1 | 643 | /* Read the remote registers into the block REGS. */ |
e1ce8aa5 JK |
644 | /* Currently we just read all the registers, so we don't use regno. */ |
645 | /* ARGSUSED */ | |
b543979c | 646 | static void |
bd5635a1 RP |
647 | remote_fetch_registers (regno) |
648 | int regno; | |
649 | { | |
650 | char buf[PBUFSIZ]; | |
651 | int i; | |
652 | char *p; | |
653 | char regs[REGISTER_BYTES]; | |
654 | ||
655 | sprintf (buf, "g"); | |
656 | remote_send (buf); | |
657 | ||
55fea07b JK |
658 | /* Unimplemented registers read as all bits zero. */ |
659 | memset (regs, 0, REGISTER_BYTES); | |
660 | ||
981a3309 SG |
661 | /* We can get out of synch in various cases. If the first character |
662 | in the buffer is not a hex character, assume that has happened | |
663 | and try to fetch another packet to read. */ | |
664 | while ((buf[0] < '0' || buf[0] > '9') | |
665 | && (buf[0] < 'a' || buf[0] > 'f')) | |
666 | { | |
d0d8484a | 667 | if (remote_debug) |
199b2450 | 668 | printf_unfiltered ("Bad register packet; fetching a new packet\n"); |
981a3309 SG |
669 | getpkt (buf, 0); |
670 | } | |
671 | ||
bd5635a1 RP |
672 | /* Reply describes registers byte by byte, each byte encoded as two |
673 | hex characters. Suck them all up, then supply them to the | |
674 | register cacheing/storage mechanism. */ | |
675 | ||
676 | p = buf; | |
677 | for (i = 0; i < REGISTER_BYTES; i++) | |
678 | { | |
55fea07b JK |
679 | if (p[0] == 0) |
680 | break; | |
681 | if (p[1] == 0) | |
682 | { | |
683 | warning ("Remote reply is of odd length: %s", buf); | |
684 | /* Don't change register_bytes_found in this case, and don't | |
685 | print a second warning. */ | |
686 | goto supply_them; | |
687 | } | |
bd5635a1 RP |
688 | regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]); |
689 | p += 2; | |
690 | } | |
55fea07b JK |
691 | |
692 | if (i != register_bytes_found) | |
693 | { | |
694 | register_bytes_found = i; | |
695 | #ifdef REGISTER_BYTES_OK | |
696 | if (!REGISTER_BYTES_OK (i)) | |
697 | warning ("Remote reply is too short: %s", buf); | |
698 | #endif | |
699 | } | |
700 | ||
701 | supply_them: | |
bd5635a1 RP |
702 | for (i = 0; i < NUM_REGS; i++) |
703 | supply_register (i, ®s[REGISTER_BYTE(i)]); | |
bd5635a1 RP |
704 | } |
705 | ||
4aa6fe10 JK |
706 | /* Prepare to store registers. Since we may send them all (using a |
707 | 'G' request), we have to read out the ones we don't want to change | |
708 | first. */ | |
bd5635a1 | 709 | |
b543979c | 710 | static void |
bd5635a1 RP |
711 | remote_prepare_to_store () |
712 | { | |
34517ebc JG |
713 | /* Make sure the entire registers array is valid. */ |
714 | read_register_bytes (0, (char *)NULL, REGISTER_BYTES); | |
bd5635a1 RP |
715 | } |
716 | ||
4aa6fe10 JK |
717 | /* Store register REGNO, or all registers if REGNO == -1, from the contents |
718 | of REGISTERS. FIXME: ignores errors. */ | |
bd5635a1 | 719 | |
b543979c | 720 | static void |
bd5635a1 RP |
721 | remote_store_registers (regno) |
722 | int regno; | |
723 | { | |
724 | char buf[PBUFSIZ]; | |
725 | int i; | |
726 | char *p; | |
727 | ||
4aa6fe10 JK |
728 | if (regno >= 0 && stub_supports_P) |
729 | { | |
730 | /* Try storing a single register. */ | |
731 | char *regp; | |
732 | ||
0c993550 | 733 | sprintf (buf, "P%x=", regno); |
4aa6fe10 JK |
734 | p = buf + strlen (buf); |
735 | regp = ®isters[REGISTER_BYTE (regno)]; | |
736 | for (i = 0; i < REGISTER_RAW_SIZE (regno); ++i) | |
737 | { | |
738 | *p++ = tohex ((regp[i] >> 4) & 0xf); | |
739 | *p++ = tohex (regp[i] & 0xf); | |
740 | } | |
741 | *p = '\0'; | |
742 | remote_send (buf); | |
743 | if (buf[0] != '\0') | |
744 | { | |
745 | /* The stub understands the 'P' request. We are done. */ | |
746 | return; | |
747 | } | |
748 | ||
749 | /* The stub does not support the 'P' request. Use 'G' instead, | |
750 | and don't try using 'P' in the future (it will just waste our | |
751 | time). */ | |
752 | stub_supports_P = 0; | |
753 | } | |
754 | ||
bd5635a1 | 755 | buf[0] = 'G'; |
4aa6fe10 | 756 | |
bd5635a1 RP |
757 | /* Command describes registers byte by byte, |
758 | each byte encoded as two hex characters. */ | |
759 | ||
760 | p = buf + 1; | |
55fea07b JK |
761 | /* remote_prepare_to_store insures that register_bytes_found gets set. */ |
762 | for (i = 0; i < register_bytes_found; i++) | |
bd5635a1 RP |
763 | { |
764 | *p++ = tohex ((registers[i] >> 4) & 0xf); | |
765 | *p++ = tohex (registers[i] & 0xf); | |
766 | } | |
767 | *p = '\0'; | |
768 | ||
769 | remote_send (buf); | |
bd5635a1 RP |
770 | } |
771 | ||
b43e0347 JK |
772 | #if 0 |
773 | ||
774 | /* Use of the data cache is disabled because it loses for looking at | |
775 | and changing hardware I/O ports and the like. Accepting `volatile' | |
776 | would perhaps be one way to fix it, but a better way which would | |
777 | win for more cases would be to use the executable file for the text | |
778 | segment, like the `icache' code below but done cleanly (in some | |
779 | target-independent place, perhaps in target_xfer_memory, perhaps | |
780 | based on assigning each target a speed or perhaps by some simpler | |
781 | mechanism). */ | |
782 | ||
bd5635a1 RP |
783 | /* Read a word from remote address ADDR and return it. |
784 | This goes through the data cache. */ | |
785 | ||
b43e0347 | 786 | static int |
bd5635a1 RP |
787 | remote_fetch_word (addr) |
788 | CORE_ADDR addr; | |
789 | { | |
d538b510 | 790 | #if 0 |
bd5635a1 RP |
791 | if (icache) |
792 | { | |
793 | extern CORE_ADDR text_start, text_end; | |
794 | ||
795 | if (addr >= text_start && addr < text_end) | |
796 | { | |
797 | int buffer; | |
798 | xfer_core_file (addr, &buffer, sizeof (int)); | |
799 | return buffer; | |
800 | } | |
801 | } | |
d538b510 RP |
802 | #endif |
803 | return dcache_fetch (remote_dcache, addr); | |
bd5635a1 RP |
804 | } |
805 | ||
806 | /* Write a word WORD into remote address ADDR. | |
807 | This goes through the data cache. */ | |
808 | ||
b43e0347 | 809 | static void |
bd5635a1 RP |
810 | remote_store_word (addr, word) |
811 | CORE_ADDR addr; | |
812 | int word; | |
813 | { | |
d538b510 | 814 | dcache_poke (remote_dcache, addr, word); |
bd5635a1 | 815 | } |
b43e0347 | 816 | #endif /* 0 */ |
bd5635a1 RP |
817 | \f |
818 | /* Write memory data directly to the remote machine. | |
819 | This does not inform the data cache; the data cache uses this. | |
820 | MEMADDR is the address in the remote memory space. | |
821 | MYADDR is the address of the buffer in our space. | |
d538b510 | 822 | LEN is the number of bytes. |
bd5635a1 | 823 | |
d538b510 RP |
824 | Returns number of bytes transferred, or 0 for error. */ |
825 | ||
826 | static int | |
bd5635a1 RP |
827 | remote_write_bytes (memaddr, myaddr, len) |
828 | CORE_ADDR memaddr; | |
d538b510 | 829 | unsigned char *myaddr; |
bd5635a1 RP |
830 | int len; |
831 | { | |
832 | char buf[PBUFSIZ]; | |
833 | int i; | |
834 | char *p; | |
835 | ||
d24c0599 JK |
836 | /* FIXME-32x64: Need a version of print_address_numeric which puts the |
837 | result in a buffer like sprintf. */ | |
4aa6fe10 | 838 | sprintf (buf, "M%lx,%x:", (unsigned long) memaddr, len); |
bd5635a1 | 839 | |
b543979c | 840 | /* We send target system values byte by byte, in increasing byte addresses, |
bd5635a1 RP |
841 | each byte encoded as two hex characters. */ |
842 | ||
843 | p = buf + strlen (buf); | |
844 | for (i = 0; i < len; i++) | |
845 | { | |
846 | *p++ = tohex ((myaddr[i] >> 4) & 0xf); | |
847 | *p++ = tohex (myaddr[i] & 0xf); | |
848 | } | |
849 | *p = '\0'; | |
850 | ||
d538b510 RP |
851 | putpkt (buf); |
852 | getpkt (buf, 0); | |
853 | ||
854 | if (buf[0] == 'E') | |
855 | { | |
856 | /* There is no correspondance between what the remote protocol uses | |
857 | for errors and errno codes. We would like a cleaner way of | |
858 | representing errors (big enough to include errno codes, bfd_error | |
859 | codes, and others). But for now just return EIO. */ | |
860 | errno = EIO; | |
861 | return 0; | |
862 | } | |
863 | return len; | |
bd5635a1 RP |
864 | } |
865 | ||
866 | /* Read memory data directly from the remote machine. | |
867 | This does not use the data cache; the data cache uses this. | |
868 | MEMADDR is the address in the remote memory space. | |
869 | MYADDR is the address of the buffer in our space. | |
d538b510 | 870 | LEN is the number of bytes. |
bd5635a1 | 871 | |
d538b510 RP |
872 | Returns number of bytes transferred, or 0 for error. */ |
873 | ||
874 | static int | |
bd5635a1 RP |
875 | remote_read_bytes (memaddr, myaddr, len) |
876 | CORE_ADDR memaddr; | |
d538b510 | 877 | unsigned char *myaddr; |
bd5635a1 RP |
878 | int len; |
879 | { | |
880 | char buf[PBUFSIZ]; | |
881 | int i; | |
882 | char *p; | |
883 | ||
884 | if (len > PBUFSIZ / 2 - 1) | |
885 | abort (); | |
886 | ||
d24c0599 JK |
887 | /* FIXME-32x64: Need a version of print_address_numeric which puts the |
888 | result in a buffer like sprintf. */ | |
4aa6fe10 | 889 | sprintf (buf, "m%lx,%x", (unsigned long) memaddr, len); |
d538b510 RP |
890 | putpkt (buf); |
891 | getpkt (buf, 0); | |
892 | ||
893 | if (buf[0] == 'E') | |
894 | { | |
895 | /* There is no correspondance between what the remote protocol uses | |
896 | for errors and errno codes. We would like a cleaner way of | |
897 | representing errors (big enough to include errno codes, bfd_error | |
898 | codes, and others). But for now just return EIO. */ | |
899 | errno = EIO; | |
900 | return 0; | |
901 | } | |
bd5635a1 | 902 | |
b543979c | 903 | /* Reply describes memory byte by byte, |
bd5635a1 RP |
904 | each byte encoded as two hex characters. */ |
905 | ||
906 | p = buf; | |
907 | for (i = 0; i < len; i++) | |
908 | { | |
909 | if (p[0] == 0 || p[1] == 0) | |
d538b510 RP |
910 | /* Reply is short. This means that we were able to read only part |
911 | of what we wanted to. */ | |
912 | break; | |
bd5635a1 RP |
913 | myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]); |
914 | p += 2; | |
915 | } | |
d538b510 | 916 | return i; |
bd5635a1 RP |
917 | } |
918 | \f | |
919 | /* Read or write LEN bytes from inferior memory at MEMADDR, transferring | |
e1ce8aa5 | 920 | to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is |
bd5635a1 RP |
921 | nonzero. Returns length of data written or read; 0 for error. */ |
922 | ||
b543979c JG |
923 | /* ARGSUSED */ |
924 | static int | |
925 | remote_xfer_memory(memaddr, myaddr, len, should_write, target) | |
bd5635a1 RP |
926 | CORE_ADDR memaddr; |
927 | char *myaddr; | |
928 | int len; | |
e1ce8aa5 | 929 | int should_write; |
b543979c | 930 | struct target_ops *target; /* ignored */ |
bd5635a1 | 931 | { |
bd5635a1 | 932 | int xfersize; |
d538b510 RP |
933 | int bytes_xferred; |
934 | int total_xferred = 0; | |
935 | ||
bd5635a1 RP |
936 | while (len > 0) |
937 | { | |
938 | if (len > MAXBUFBYTES) | |
939 | xfersize = MAXBUFBYTES; | |
940 | else | |
941 | xfersize = len; | |
942 | ||
e1ce8aa5 | 943 | if (should_write) |
94d4b713 JK |
944 | bytes_xferred = remote_write_bytes (memaddr, |
945 | (unsigned char *)myaddr, xfersize); | |
bd5635a1 | 946 | else |
94d4b713 JK |
947 | bytes_xferred = remote_read_bytes (memaddr, |
948 | (unsigned char *)myaddr, xfersize); | |
d538b510 RP |
949 | |
950 | /* If we get an error, we are done xferring. */ | |
951 | if (bytes_xferred == 0) | |
952 | break; | |
953 | ||
954 | memaddr += bytes_xferred; | |
955 | myaddr += bytes_xferred; | |
956 | len -= bytes_xferred; | |
957 | total_xferred += bytes_xferred; | |
bd5635a1 | 958 | } |
d538b510 | 959 | return total_xferred; |
bd5635a1 RP |
960 | } |
961 | ||
94d4b713 JK |
962 | #if 0 |
963 | /* Enable after 4.12. */ | |
964 | ||
965 | void | |
966 | remote_search (len, data, mask, startaddr, increment, lorange, hirange | |
967 | addr_found, data_found) | |
968 | int len; | |
969 | char *data; | |
970 | char *mask; | |
971 | CORE_ADDR startaddr; | |
972 | int increment; | |
973 | CORE_ADDR lorange; | |
974 | CORE_ADDR hirange; | |
975 | CORE_ADDR *addr_found; | |
976 | char *data_found; | |
977 | { | |
978 | if (increment == -4 && len == 4) | |
979 | { | |
980 | long mask_long, data_long; | |
981 | long data_found_long; | |
982 | CORE_ADDR addr_we_found; | |
983 | char buf[PBUFSIZ]; | |
984 | long returned_long[2]; | |
985 | char *p; | |
986 | ||
987 | mask_long = extract_unsigned_integer (mask, len); | |
988 | data_long = extract_unsigned_integer (data, len); | |
989 | sprintf (buf, "t%x:%x,%x", startaddr, data_long, mask_long); | |
990 | putpkt (buf); | |
991 | getpkt (buf, 0); | |
992 | if (buf[0] == '\0') | |
993 | { | |
994 | /* The stub doesn't support the 't' request. We might want to | |
995 | remember this fact, but on the other hand the stub could be | |
996 | switched on us. Maybe we should remember it only until | |
997 | the next "target remote". */ | |
998 | generic_search (len, data, mask, startaddr, increment, lorange, | |
999 | hirange, addr_found, data_found); | |
1000 | return; | |
1001 | } | |
1002 | ||
1003 | if (buf[0] == 'E') | |
1004 | /* There is no correspondance between what the remote protocol uses | |
1005 | for errors and errno codes. We would like a cleaner way of | |
1006 | representing errors (big enough to include errno codes, bfd_error | |
1007 | codes, and others). But for now just use EIO. */ | |
1008 | memory_error (EIO, startaddr); | |
1009 | p = buf; | |
1010 | addr_we_found = 0; | |
1011 | while (*p != '\0' && *p != ',') | |
1012 | addr_we_found = (addr_we_found << 4) + fromhex (*p++); | |
1013 | if (*p == '\0') | |
1014 | error ("Protocol error: short return for search"); | |
1015 | ||
1016 | data_found_long = 0; | |
1017 | while (*p != '\0' && *p != ',') | |
1018 | data_found_long = (data_found_long << 4) + fromhex (*p++); | |
1019 | /* Ignore anything after this comma, for future extensions. */ | |
1020 | ||
1021 | if (addr_we_found < lorange || addr_we_found >= hirange) | |
1022 | { | |
1023 | *addr_found = 0; | |
1024 | return; | |
1025 | } | |
1026 | ||
1027 | *addr_found = addr_we_found; | |
1028 | *data_found = store_unsigned_integer (data_we_found, len); | |
1029 | return; | |
1030 | } | |
1031 | generic_search (len, data, mask, startaddr, increment, lorange, | |
1032 | hirange, addr_found, data_found); | |
1033 | } | |
1034 | #endif /* 0 */ | |
1035 | \f | |
b543979c | 1036 | static void |
8f86a4e4 | 1037 | remote_files_info (ignore) |
5af4f5f6 | 1038 | struct target_ops *ignore; |
bd5635a1 | 1039 | { |
7c622b41 | 1040 | puts_filtered ("Debugging a target over a serial line.\n"); |
bd5635a1 RP |
1041 | } |
1042 | \f | |
e50ebec8 JK |
1043 | /* Stuff for dealing with the packets which are part of this protocol. |
1044 | See comment at top of file for details. */ | |
bd5635a1 | 1045 | |
ebdb9ade | 1046 | /* Read a single character from the remote end, masking it down to 7 bits. */ |
b543979c | 1047 | |
bd5635a1 | 1048 | static int |
754e5da2 SG |
1049 | readchar (timeout) |
1050 | int timeout; | |
bd5635a1 | 1051 | { |
ebdb9ade | 1052 | int ch; |
bd5635a1 | 1053 | |
ebdb9ade | 1054 | ch = SERIAL_READCHAR (remote_desc, timeout); |
fce7f2d9 | 1055 | |
754e5da2 SG |
1056 | switch (ch) |
1057 | { | |
1058 | case SERIAL_EOF: | |
1059 | error ("Remote connection closed"); | |
1060 | case SERIAL_ERROR: | |
1061 | perror_with_name ("Remote communication error"); | |
1062 | case SERIAL_TIMEOUT: | |
1063 | return ch; | |
1064 | default: | |
1065 | return ch & 0x7f; | |
1066 | } | |
bd5635a1 RP |
1067 | } |
1068 | ||
1069 | /* Send the command in BUF to the remote machine, | |
1070 | and read the reply into BUF. | |
1071 | Report an error if we get an error reply. */ | |
1072 | ||
1073 | static void | |
1074 | remote_send (buf) | |
1075 | char *buf; | |
1076 | { | |
1077 | ||
1078 | putpkt (buf); | |
7c622b41 | 1079 | getpkt (buf, 0); |
bd5635a1 RP |
1080 | |
1081 | if (buf[0] == 'E') | |
1082 | error ("Remote failure reply: %s", buf); | |
1083 | } | |
1084 | ||
1085 | /* Send a packet to the remote machine, with error checking. | |
1086 | The data of the packet is in BUF. */ | |
1087 | ||
1088 | static void | |
1089 | putpkt (buf) | |
1090 | char *buf; | |
1091 | { | |
1092 | int i; | |
1093 | unsigned char csum = 0; | |
b543979c | 1094 | char buf2[PBUFSIZ]; |
bd5635a1 | 1095 | int cnt = strlen (buf); |
ebdb9ade | 1096 | int ch; |
bd5635a1 RP |
1097 | char *p; |
1098 | ||
1099 | /* Copy the packet into buffer BUF2, encapsulating it | |
1100 | and giving it a checksum. */ | |
1101 | ||
b543979c JG |
1102 | if (cnt > sizeof(buf2) - 5) /* Prosanity check */ |
1103 | abort(); | |
1104 | ||
bd5635a1 RP |
1105 | p = buf2; |
1106 | *p++ = '$'; | |
1107 | ||
1108 | for (i = 0; i < cnt; i++) | |
1109 | { | |
1110 | csum += buf[i]; | |
1111 | *p++ = buf[i]; | |
1112 | } | |
1113 | *p++ = '#'; | |
1114 | *p++ = tohex ((csum >> 4) & 0xf); | |
1115 | *p++ = tohex (csum & 0xf); | |
1116 | ||
1117 | /* Send it over and over until we get a positive ack. */ | |
1118 | ||
6b27ebe8 JK |
1119 | while (1) |
1120 | { | |
1624c38f SG |
1121 | int started_error_output = 0; |
1122 | ||
d0d8484a | 1123 | if (remote_debug) |
6b27ebe8 JK |
1124 | { |
1125 | *p = '\0'; | |
1624c38f SG |
1126 | printf_unfiltered ("Sending packet: %s...", buf2); |
1127 | gdb_flush(gdb_stdout); | |
6b27ebe8 JK |
1128 | } |
1129 | if (SERIAL_WRITE (remote_desc, buf2, p - buf2)) | |
1130 | perror_with_name ("putpkt: write failed"); | |
1131 | ||
1132 | /* read until either a timeout occurs (-2) or '+' is read */ | |
1133 | while (1) | |
1134 | { | |
754e5da2 | 1135 | ch = readchar (remote_timeout); |
6b27ebe8 | 1136 | |
1624c38f SG |
1137 | if (remote_debug) |
1138 | { | |
1139 | switch (ch) | |
1140 | { | |
1141 | case '+': | |
1142 | case SERIAL_TIMEOUT: | |
1624c38f SG |
1143 | case '$': |
1144 | if (started_error_output) | |
1145 | { | |
1146 | putc_unfiltered ('\n'); | |
1147 | started_error_output = 0; | |
1148 | } | |
1149 | } | |
1150 | } | |
1151 | ||
6b27ebe8 JK |
1152 | switch (ch) |
1153 | { | |
1154 | case '+': | |
d0d8484a | 1155 | if (remote_debug) |
199b2450 | 1156 | printf_unfiltered("Ack\n"); |
6b27ebe8 JK |
1157 | return; |
1158 | case SERIAL_TIMEOUT: | |
1159 | break; /* Retransmit buffer */ | |
1624c38f SG |
1160 | case '$': |
1161 | { | |
1162 | unsigned char junkbuf[PBUFSIZ]; | |
1163 | ||
1164 | /* It's probably an old response, and we're out of sync. Just | |
1165 | gobble up the packet and ignore it. */ | |
1166 | getpkt (junkbuf, 0); | |
1167 | continue; /* Now, go look for + */ | |
1168 | } | |
6b27ebe8 | 1169 | default: |
d0d8484a | 1170 | if (remote_debug) |
1624c38f SG |
1171 | { |
1172 | if (!started_error_output) | |
1173 | { | |
1174 | started_error_output = 1; | |
1175 | printf_unfiltered ("putpkt: Junk: "); | |
1176 | } | |
1177 | putc_unfiltered (ch & 0177); | |
1178 | } | |
6b27ebe8 JK |
1179 | continue; |
1180 | } | |
1181 | break; /* Here to retransmit */ | |
1182 | } | |
981a3309 | 1183 | |
94d4b713 JK |
1184 | #if 0 |
1185 | /* This is wrong. If doing a long backtrace, the user should be | |
1186 | able to get out next time we call QUIT, without anything as violent | |
1187 | as interrupt_query. If we want to provide a way out of here | |
1188 | without getting to the next QUIT, it should be based on hitting | |
1189 | ^C twice as in remote_wait. */ | |
981a3309 SG |
1190 | if (quit_flag) |
1191 | { | |
1192 | quit_flag = 0; | |
1193 | interrupt_query (); | |
1194 | } | |
94d4b713 | 1195 | #endif |
6b27ebe8 | 1196 | } |
bd5635a1 RP |
1197 | } |
1198 | ||
754e5da2 SG |
1199 | /* Come here after finding the start of the frame. Collect the rest into BUF, |
1200 | verifying the checksum, length, and handling run-length compression. | |
1201 | Returns 0 on any error, 1 on success. */ | |
1202 | ||
1203 | static int | |
1204 | read_frame (buf) | |
1205 | char *buf; | |
1206 | { | |
1207 | unsigned char csum; | |
1208 | char *bp; | |
1209 | int c; | |
1210 | ||
1211 | csum = 0; | |
1212 | bp = buf; | |
1213 | ||
1214 | while (1) | |
1215 | { | |
1216 | c = readchar (remote_timeout); | |
1217 | ||
1218 | switch (c) | |
1219 | { | |
1220 | case SERIAL_TIMEOUT: | |
1221 | if (remote_debug) | |
1222 | puts_filtered ("Timeout in mid-packet, retrying\n"); | |
1223 | return 0; | |
1224 | case '$': | |
1225 | if (remote_debug) | |
1226 | puts_filtered ("Saw new packet start in middle of old one\n"); | |
1227 | return 0; /* Start a new packet, count retries */ | |
1228 | case '#': | |
1229 | { | |
1230 | unsigned char pktcsum; | |
1231 | ||
1232 | *bp = '\000'; | |
1233 | ||
1234 | pktcsum = fromhex (readchar (remote_timeout)) << 4 | |
1235 | | fromhex (readchar (remote_timeout)); | |
1236 | ||
1237 | if (csum == pktcsum) | |
1238 | return 1; | |
1239 | ||
1240 | printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=", | |
1241 | pktcsum, csum); | |
1242 | puts_filtered (buf); | |
1243 | puts_filtered ("\n"); | |
1244 | ||
1245 | return 0; | |
1246 | } | |
1247 | case '*': /* Run length encoding */ | |
1248 | c = readchar (remote_timeout); | |
1249 | csum += c; | |
1250 | c = c - ' ' + 3; /* Compute repeat count */ | |
1251 | ||
1252 | if (bp + c - 1 < buf + PBUFSIZ - 1) | |
1253 | { | |
1254 | memset (bp, *(bp - 1), c); | |
1255 | bp += c; | |
1256 | continue; | |
1257 | } | |
1258 | ||
1259 | *bp = '\0'; | |
1260 | printf_filtered ("Repeat count %d too large for buffer: ", c); | |
1261 | puts_filtered (buf); | |
1262 | puts_filtered ("\n"); | |
1263 | ||
1264 | return 0; | |
1265 | default: | |
1266 | if (bp < buf + PBUFSIZ - 1) | |
1267 | { | |
1268 | *bp++ = c; | |
1269 | csum += c; | |
1270 | continue; | |
1271 | } | |
1272 | ||
1273 | *bp = '\0'; | |
1274 | puts_filtered ("Remote packet too long: "); | |
1275 | puts_filtered (buf); | |
1276 | puts_filtered ("\n"); | |
1277 | ||
1278 | return 0; | |
1279 | } | |
1280 | } | |
1281 | } | |
1282 | ||
bd5635a1 | 1283 | /* Read a packet from the remote machine, with error checking, |
7c622b41 JG |
1284 | and store it in BUF. BUF is expected to be of size PBUFSIZ. |
1285 | If FOREVER, wait forever rather than timing out; this is used | |
1286 | while the target is executing user code. */ | |
bd5635a1 RP |
1287 | |
1288 | static void | |
754e5da2 SG |
1289 | getpkt (buf, forever) |
1290 | char *buf; | |
ebdb9ade | 1291 | int forever; |
bd5635a1 RP |
1292 | { |
1293 | char *bp; | |
754e5da2 SG |
1294 | int c; |
1295 | int tries; | |
1296 | int timeout; | |
1297 | int val; | |
94d4b713 | 1298 | |
754e5da2 SG |
1299 | if (forever) |
1300 | timeout = -1; | |
1301 | else | |
1302 | timeout = remote_timeout; | |
bd5635a1 | 1303 | |
754e5da2 | 1304 | #define MAX_TRIES 10 |
981a3309 | 1305 | |
754e5da2 SG |
1306 | for (tries = 1; tries <= MAX_TRIES; tries++) |
1307 | { | |
7c622b41 JG |
1308 | /* This can loop forever if the remote side sends us characters |
1309 | continuously, but if it pauses, we'll get a zero from readchar | |
1310 | because of timeout. Then we'll count that as a retry. */ | |
6b27ebe8 | 1311 | |
754e5da2 SG |
1312 | /* Note that we will only wait forever prior to the start of a packet. |
1313 | After that, we expect characters to arrive at a brisk pace. They | |
1314 | should show up within remote_timeout intervals. */ | |
6b27ebe8 | 1315 | |
754e5da2 | 1316 | do |
6b27ebe8 | 1317 | { |
754e5da2 | 1318 | c = readchar (timeout); |
7c622b41 | 1319 | |
ebdb9ade | 1320 | if (c == SERIAL_TIMEOUT) |
7c622b41 | 1321 | { |
d0d8484a | 1322 | if (remote_debug) |
754e5da2 SG |
1323 | puts_filtered ("Timed out.\n"); |
1324 | goto retry; | |
7c622b41 | 1325 | } |
bd5635a1 | 1326 | } |
754e5da2 | 1327 | while (c != '$'); |
bd5635a1 | 1328 | |
754e5da2 | 1329 | /* We've found the start of a packet, now collect the data. */ |
38094c60 | 1330 | |
754e5da2 SG |
1331 | val = read_frame (buf); |
1332 | ||
1333 | if (val == 1) | |
38094c60 | 1334 | { |
754e5da2 SG |
1335 | if (remote_debug) |
1336 | fprintf_unfiltered (gdb_stderr, "Packet received: %s\n", buf); | |
1337 | SERIAL_WRITE (remote_desc, "+", 1); | |
1338 | return; | |
38094c60 | 1339 | } |
754e5da2 SG |
1340 | |
1341 | /* Try the whole thing again. */ | |
1342 | retry: | |
1343 | SERIAL_WRITE (remote_desc, "-", 1); | |
bd5635a1 RP |
1344 | } |
1345 | ||
754e5da2 | 1346 | /* We have tried hard enough, and just can't receive the packet. Give up. */ |
7c622b41 | 1347 | |
754e5da2 | 1348 | printf_unfiltered ("Ignoring packet error, continuing...\n"); |
ebdb9ade | 1349 | SERIAL_WRITE (remote_desc, "+", 1); |
bd5635a1 RP |
1350 | } |
1351 | \f | |
ebdb9ade JK |
1352 | static void |
1353 | remote_kill () | |
1354 | { | |
1355 | putpkt ("k"); | |
1356 | /* Don't wait for it to die. I'm not really sure it matters whether | |
1357 | we do or not. For the existing stubs, kill is a noop. */ | |
1358 | target_mourn_inferior (); | |
1359 | } | |
bd5635a1 | 1360 | |
ebdb9ade JK |
1361 | static void |
1362 | remote_mourn () | |
1363 | { | |
1364 | unpush_target (&remote_ops); | |
1365 | generic_mourn_inferior (); | |
1366 | } | |
1367 | \f | |
5af4f5f6 JK |
1368 | #ifdef REMOTE_BREAKPOINT |
1369 | ||
1370 | /* On some machines, e.g. 68k, we may use a different breakpoint instruction | |
1371 | than other targets. */ | |
1372 | static unsigned char break_insn[] = REMOTE_BREAKPOINT; | |
1373 | ||
1374 | /* Check that it fits in BREAKPOINT_MAX bytes. */ | |
1375 | static unsigned char check_break_insn_size[BREAKPOINT_MAX] = REMOTE_BREAKPOINT; | |
1376 | ||
1377 | #else /* No REMOTE_BREAKPOINT. */ | |
1378 | ||
1379 | /* Same old breakpoint instruction. This code does nothing different | |
1380 | than mem-break.c. */ | |
1381 | static unsigned char break_insn[] = BREAKPOINT; | |
1382 | ||
1383 | #endif /* No REMOTE_BREAKPOINT. */ | |
1384 | ||
1385 | /* Insert a breakpoint on targets that don't have any better breakpoint | |
1386 | support. We read the contents of the target location and stash it, | |
1387 | then overwrite it with a breakpoint instruction. ADDR is the target | |
1388 | location in the target machine. CONTENTS_CACHE is a pointer to | |
1389 | memory allocated for saving the target contents. It is guaranteed | |
1390 | by the caller to be long enough to save sizeof BREAKPOINT bytes (this | |
1391 | is accomplished via BREAKPOINT_MAX). */ | |
1392 | ||
d538b510 | 1393 | static int |
5af4f5f6 JK |
1394 | remote_insert_breakpoint (addr, contents_cache) |
1395 | CORE_ADDR addr; | |
1396 | char *contents_cache; | |
1397 | { | |
1398 | int val; | |
1399 | ||
1400 | val = target_read_memory (addr, contents_cache, sizeof break_insn); | |
1401 | ||
1402 | if (val == 0) | |
1403 | val = target_write_memory (addr, (char *)break_insn, sizeof break_insn); | |
1404 | ||
1405 | return val; | |
1406 | } | |
1407 | ||
d538b510 | 1408 | static int |
5af4f5f6 JK |
1409 | remote_remove_breakpoint (addr, contents_cache) |
1410 | CORE_ADDR addr; | |
1411 | char *contents_cache; | |
1412 | { | |
1413 | return target_write_memory (addr, contents_cache, sizeof break_insn); | |
1414 | } | |
1415 | \f | |
bd5635a1 RP |
1416 | /* Define the target subroutine names */ |
1417 | ||
1418 | struct target_ops remote_ops = { | |
b543979c JG |
1419 | "remote", /* to_shortname */ |
1420 | "Remote serial target in gdb-specific protocol", /* to_longname */ | |
1421 | "Use a remote computer via a serial line, using a gdb-specific protocol.\n\ | |
1422 | Specify the serial device it is connected to (e.g. /dev/ttya).", /* to_doc */ | |
1423 | remote_open, /* to_open */ | |
1424 | remote_close, /* to_close */ | |
1425 | NULL, /* to_attach */ | |
1426 | remote_detach, /* to_detach */ | |
1427 | remote_resume, /* to_resume */ | |
1428 | remote_wait, /* to_wait */ | |
1429 | remote_fetch_registers, /* to_fetch_registers */ | |
1430 | remote_store_registers, /* to_store_registers */ | |
1431 | remote_prepare_to_store, /* to_prepare_to_store */ | |
b543979c JG |
1432 | remote_xfer_memory, /* to_xfer_memory */ |
1433 | remote_files_info, /* to_files_info */ | |
5af4f5f6 JK |
1434 | |
1435 | remote_insert_breakpoint, /* to_insert_breakpoint */ | |
1436 | remote_remove_breakpoint, /* to_remove_breakpoint */ | |
1437 | ||
b543979c JG |
1438 | NULL, /* to_terminal_init */ |
1439 | NULL, /* to_terminal_inferior */ | |
1440 | NULL, /* to_terminal_ours_for_output */ | |
1441 | NULL, /* to_terminal_ours */ | |
1442 | NULL, /* to_terminal_info */ | |
ebdb9ade | 1443 | remote_kill, /* to_kill */ |
6b27ebe8 | 1444 | generic_load, /* to_load */ |
b543979c JG |
1445 | NULL, /* to_lookup_symbol */ |
1446 | NULL, /* to_create_inferior */ | |
ebdb9ade | 1447 | remote_mourn, /* to_mourn_inferior */ |
34517ebc | 1448 | 0, /* to_can_run */ |
7c622b41 | 1449 | 0, /* to_notice_signals */ |
b543979c JG |
1450 | process_stratum, /* to_stratum */ |
1451 | NULL, /* to_next */ | |
1452 | 1, /* to_has_all_memory */ | |
1453 | 1, /* to_has_memory */ | |
1454 | 1, /* to_has_stack */ | |
1455 | 1, /* to_has_registers */ | |
1456 | 1, /* to_has_execution */ | |
1457 | NULL, /* sections */ | |
1458 | NULL, /* sections_end */ | |
1459 | OPS_MAGIC /* to_magic */ | |
bd5635a1 RP |
1460 | }; |
1461 | ||
1462 | void | |
1463 | _initialize_remote () | |
1464 | { | |
1465 | add_target (&remote_ops); | |
976bb0be | 1466 | } |