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(); |
a1e0ba7a SG |
417 | |
418 | /* Without this, some commands which require an active target (such as kill) | |
419 | won't work. This variable serves (at least) double duty as both the pid | |
420 | of the target process (if it has such), and as a flag indicating that a | |
421 | target is active. These functions should be split out into seperate | |
422 | variables, especially since GDB will someday have a notion of debugging | |
423 | several processes. */ | |
424 | ||
425 | inferior_pid = -1; | |
bd5635a1 RP |
426 | } |
427 | ||
428 | /* remote_detach() | |
429 | takes a program previously attached to and detaches it. | |
430 | We better not have left any breakpoints | |
431 | in the program or it'll die when it hits one. | |
432 | Close the open connection to the remote debugger. | |
433 | Use this when you want to detach and do something else | |
434 | with your gdb. */ | |
435 | ||
436 | static void | |
437 | remote_detach (args, from_tty) | |
438 | char *args; | |
439 | int from_tty; | |
440 | { | |
441 | if (args) | |
442 | error ("Argument given to \"detach\" when remotely debugging."); | |
443 | ||
444 | pop_target (); | |
445 | if (from_tty) | |
7c622b41 | 446 | puts_filtered ("Ending remote debugging.\n"); |
bd5635a1 RP |
447 | } |
448 | ||
449 | /* Convert hex digit A to a number. */ | |
450 | ||
451 | static int | |
452 | fromhex (a) | |
453 | int a; | |
454 | { | |
455 | if (a >= '0' && a <= '9') | |
456 | return a - '0'; | |
457 | else if (a >= 'a' && a <= 'f') | |
458 | return a - 'a' + 10; | |
459 | else | |
460 | error ("Reply contains invalid hex digit"); | |
bd5635a1 RP |
461 | } |
462 | ||
463 | /* Convert number NIB to a hex digit. */ | |
464 | ||
465 | static int | |
466 | tohex (nib) | |
467 | int nib; | |
468 | { | |
469 | if (nib < 10) | |
470 | return '0'+nib; | |
471 | else | |
472 | return 'a'+nib-10; | |
473 | } | |
474 | \f | |
475 | /* Tell the remote machine to resume. */ | |
476 | ||
b543979c | 477 | static void |
d538b510 | 478 | remote_resume (pid, step, siggnal) |
94d4b713 JK |
479 | int pid, step; |
480 | enum target_signal siggnal; | |
bd5635a1 RP |
481 | { |
482 | char buf[PBUFSIZ]; | |
483 | ||
484 | if (siggnal) | |
ebdb9ade | 485 | { |
ebdb9ade | 486 | target_terminal_ours_for_output (); |
94d4b713 JK |
487 | printf_filtered |
488 | ("Can't send signals to a remote system. %s not sent.\n", | |
489 | target_signal_to_name (siggnal)); | |
ebdb9ade JK |
490 | target_terminal_inferior (); |
491 | } | |
bd5635a1 | 492 | |
d538b510 | 493 | dcache_flush (remote_dcache); |
bd5635a1 RP |
494 | |
495 | strcpy (buf, step ? "s": "c"); | |
496 | ||
497 | putpkt (buf); | |
498 | } | |
ebdb9ade | 499 | \f |
b543979c JG |
500 | /* Send ^C to target to halt it. Target will respond, and send us a |
501 | packet. */ | |
502 | ||
5af4f5f6 JK |
503 | static void |
504 | remote_interrupt (signo) | |
e676a15f | 505 | int signo; |
b543979c | 506 | { |
ebdb9ade JK |
507 | /* If this doesn't work, try more severe steps. */ |
508 | signal (signo, remote_interrupt_twice); | |
8f86a4e4 | 509 | |
d0d8484a | 510 | if (remote_debug) |
199b2450 | 511 | printf_unfiltered ("remote_interrupt called\n"); |
8f86a4e4 | 512 | |
ebdb9ade | 513 | SERIAL_WRITE (remote_desc, "\003", 1); /* Send a ^C */ |
b543979c JG |
514 | } |
515 | ||
5af4f5f6 JK |
516 | static void (*ofunc)(); |
517 | ||
ebdb9ade JK |
518 | /* The user typed ^C twice. */ |
519 | static void | |
520 | remote_interrupt_twice (signo) | |
521 | int signo; | |
522 | { | |
523 | signal (signo, ofunc); | |
524 | ||
981a3309 SG |
525 | interrupt_query (); |
526 | ||
527 | signal (signo, remote_interrupt); | |
528 | } | |
529 | ||
530 | /* Ask the user what to do when an interrupt is received. */ | |
531 | ||
532 | static void | |
533 | interrupt_query () | |
534 | { | |
ebdb9ade | 535 | target_terminal_ours (); |
981a3309 | 536 | |
6b27ebe8 | 537 | if (query ("Interrupted while waiting for the program.\n\ |
ebdb9ade JK |
538 | Give up (and stop debugging it)? ")) |
539 | { | |
540 | target_mourn_inferior (); | |
e50ebec8 | 541 | return_to_top_level (RETURN_QUIT); |
ebdb9ade | 542 | } |
981a3309 SG |
543 | |
544 | target_terminal_inferior (); | |
ebdb9ade | 545 | } |
b543979c | 546 | |
bd5635a1 | 547 | /* Wait until the remote machine stops, then return, |
e1ce8aa5 JK |
548 | storing status in STATUS just as `wait' would. |
549 | Returns "pid" (though it's not clear what, if anything, that | |
550 | means in the case of this target). */ | |
bd5635a1 | 551 | |
b543979c | 552 | static int |
d0d8484a SG |
553 | remote_wait (pid, status) |
554 | int pid; | |
94d4b713 | 555 | struct target_waitstatus *status; |
bd5635a1 RP |
556 | { |
557 | unsigned char buf[PBUFSIZ]; | |
8f86a4e4 | 558 | |
94d4b713 JK |
559 | status->kind = TARGET_WAITKIND_EXITED; |
560 | status->value.integer = 0; | |
b543979c | 561 | |
4f8a48e5 | 562 | while (1) |
8f86a4e4 | 563 | { |
4f8a48e5 | 564 | unsigned char *p; |
a03d4f8e | 565 | |
4f8a48e5 ILT |
566 | ofunc = (void (*)()) signal (SIGINT, remote_interrupt); |
567 | getpkt ((char *) buf, 1); | |
568 | signal (SIGINT, ofunc); | |
4ecee2f9 | 569 | |
754e5da2 | 570 | switch (buf[0]) |
8f86a4e4 | 571 | { |
754e5da2 SG |
572 | case 'E': /* Error of some sort */ |
573 | warning ("Remote failure reply: %s", buf); | |
574 | continue; | |
575 | case 'T': /* Status with PC, SP, FP, ... */ | |
576 | { | |
577 | int i; | |
578 | long regno; | |
579 | char regs[MAX_REGISTER_RAW_SIZE]; | |
a03d4f8e | 580 | |
754e5da2 SG |
581 | /* Expedited reply, containing Signal, {regno, reg} repeat */ |
582 | /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where | |
583 | ss = signal number | |
584 | n... = register number | |
585 | r... = register contents | |
586 | */ | |
5af4f5f6 | 587 | |
754e5da2 | 588 | p = &buf[3]; /* after Txx */ |
5af4f5f6 | 589 | |
754e5da2 SG |
590 | while (*p) |
591 | { | |
592 | unsigned char *p1; | |
5af4f5f6 | 593 | |
754e5da2 | 594 | regno = strtol (p, &p1, 16); /* Read the register number */ |
5af4f5f6 | 595 | |
754e5da2 SG |
596 | if (p1 == p) |
597 | warning ("Remote sent badly formed register number: %s\nPacket: '%s'\n", | |
598 | p1, buf); | |
4ecee2f9 | 599 | |
754e5da2 | 600 | p = p1; |
4ecee2f9 | 601 | |
754e5da2 SG |
602 | if (*p++ != ':') |
603 | warning ("Malformed packet (missing colon): %s\nPacket: '%s'\n", | |
604 | p, buf); | |
a03d4f8e | 605 | |
754e5da2 SG |
606 | if (regno >= NUM_REGS) |
607 | warning ("Remote sent bad register number %d: %s\nPacket: '%s'\n", | |
608 | regno, p, buf); | |
4f8a48e5 | 609 | |
754e5da2 SG |
610 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i++) |
611 | { | |
612 | if (p[0] == 0 || p[1] == 0) | |
613 | warning ("Remote reply is too short: %s", buf); | |
614 | regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]); | |
615 | p += 2; | |
616 | } | |
4f8a48e5 | 617 | |
754e5da2 SG |
618 | if (*p++ != ';') |
619 | warning ("Remote register badly formatted: %s", buf); | |
620 | ||
621 | supply_register (regno, regs); | |
622 | } | |
623 | } | |
624 | /* fall through */ | |
625 | case 'S': /* Old style status, just signal only */ | |
626 | status->kind = TARGET_WAITKIND_STOPPED; | |
627 | status->value.sig = (enum target_signal) | |
628 | (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))); | |
4f8a48e5 | 629 | |
4f8a48e5 | 630 | return 0; |
754e5da2 SG |
631 | case 'W': /* Target exited */ |
632 | { | |
633 | /* The remote process exited. */ | |
634 | status->kind = TARGET_WAITKIND_EXITED; | |
635 | status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]); | |
636 | return 0; | |
637 | } | |
638 | case 'O': /* Console output */ | |
639 | fputs_filtered (buf + 1, gdb_stdout); | |
640 | continue; | |
641 | default: | |
642 | warning ("Invalid remote reply: %s", buf); | |
643 | continue; | |
4f8a48e5 | 644 | } |
758aeb93 | 645 | } |
a1e0ba7a | 646 | return inferior_pid; |
bd5635a1 RP |
647 | } |
648 | ||
55fea07b JK |
649 | /* Number of bytes of registers this stub implements. */ |
650 | static int register_bytes_found; | |
651 | ||
bd5635a1 | 652 | /* Read the remote registers into the block REGS. */ |
e1ce8aa5 JK |
653 | /* Currently we just read all the registers, so we don't use regno. */ |
654 | /* ARGSUSED */ | |
b543979c | 655 | static void |
bd5635a1 RP |
656 | remote_fetch_registers (regno) |
657 | int regno; | |
658 | { | |
659 | char buf[PBUFSIZ]; | |
660 | int i; | |
661 | char *p; | |
662 | char regs[REGISTER_BYTES]; | |
663 | ||
664 | sprintf (buf, "g"); | |
665 | remote_send (buf); | |
666 | ||
55fea07b JK |
667 | /* Unimplemented registers read as all bits zero. */ |
668 | memset (regs, 0, REGISTER_BYTES); | |
669 | ||
981a3309 SG |
670 | /* We can get out of synch in various cases. If the first character |
671 | in the buffer is not a hex character, assume that has happened | |
672 | and try to fetch another packet to read. */ | |
673 | while ((buf[0] < '0' || buf[0] > '9') | |
674 | && (buf[0] < 'a' || buf[0] > 'f')) | |
675 | { | |
d0d8484a | 676 | if (remote_debug) |
199b2450 | 677 | printf_unfiltered ("Bad register packet; fetching a new packet\n"); |
981a3309 SG |
678 | getpkt (buf, 0); |
679 | } | |
680 | ||
bd5635a1 RP |
681 | /* Reply describes registers byte by byte, each byte encoded as two |
682 | hex characters. Suck them all up, then supply them to the | |
683 | register cacheing/storage mechanism. */ | |
684 | ||
685 | p = buf; | |
686 | for (i = 0; i < REGISTER_BYTES; i++) | |
687 | { | |
55fea07b JK |
688 | if (p[0] == 0) |
689 | break; | |
690 | if (p[1] == 0) | |
691 | { | |
692 | warning ("Remote reply is of odd length: %s", buf); | |
693 | /* Don't change register_bytes_found in this case, and don't | |
694 | print a second warning. */ | |
695 | goto supply_them; | |
696 | } | |
bd5635a1 RP |
697 | regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]); |
698 | p += 2; | |
699 | } | |
55fea07b JK |
700 | |
701 | if (i != register_bytes_found) | |
702 | { | |
703 | register_bytes_found = i; | |
704 | #ifdef REGISTER_BYTES_OK | |
705 | if (!REGISTER_BYTES_OK (i)) | |
706 | warning ("Remote reply is too short: %s", buf); | |
707 | #endif | |
708 | } | |
709 | ||
710 | supply_them: | |
bd5635a1 RP |
711 | for (i = 0; i < NUM_REGS; i++) |
712 | supply_register (i, ®s[REGISTER_BYTE(i)]); | |
bd5635a1 RP |
713 | } |
714 | ||
4aa6fe10 JK |
715 | /* Prepare to store registers. Since we may send them all (using a |
716 | 'G' request), we have to read out the ones we don't want to change | |
717 | first. */ | |
bd5635a1 | 718 | |
b543979c | 719 | static void |
bd5635a1 RP |
720 | remote_prepare_to_store () |
721 | { | |
34517ebc JG |
722 | /* Make sure the entire registers array is valid. */ |
723 | read_register_bytes (0, (char *)NULL, REGISTER_BYTES); | |
bd5635a1 RP |
724 | } |
725 | ||
4aa6fe10 JK |
726 | /* Store register REGNO, or all registers if REGNO == -1, from the contents |
727 | of REGISTERS. FIXME: ignores errors. */ | |
bd5635a1 | 728 | |
b543979c | 729 | static void |
bd5635a1 RP |
730 | remote_store_registers (regno) |
731 | int regno; | |
732 | { | |
733 | char buf[PBUFSIZ]; | |
734 | int i; | |
735 | char *p; | |
736 | ||
4aa6fe10 JK |
737 | if (regno >= 0 && stub_supports_P) |
738 | { | |
739 | /* Try storing a single register. */ | |
740 | char *regp; | |
741 | ||
0c993550 | 742 | sprintf (buf, "P%x=", regno); |
4aa6fe10 JK |
743 | p = buf + strlen (buf); |
744 | regp = ®isters[REGISTER_BYTE (regno)]; | |
745 | for (i = 0; i < REGISTER_RAW_SIZE (regno); ++i) | |
746 | { | |
747 | *p++ = tohex ((regp[i] >> 4) & 0xf); | |
748 | *p++ = tohex (regp[i] & 0xf); | |
749 | } | |
750 | *p = '\0'; | |
751 | remote_send (buf); | |
752 | if (buf[0] != '\0') | |
753 | { | |
754 | /* The stub understands the 'P' request. We are done. */ | |
755 | return; | |
756 | } | |
757 | ||
758 | /* The stub does not support the 'P' request. Use 'G' instead, | |
759 | and don't try using 'P' in the future (it will just waste our | |
760 | time). */ | |
761 | stub_supports_P = 0; | |
762 | } | |
763 | ||
bd5635a1 | 764 | buf[0] = 'G'; |
4aa6fe10 | 765 | |
bd5635a1 RP |
766 | /* Command describes registers byte by byte, |
767 | each byte encoded as two hex characters. */ | |
768 | ||
769 | p = buf + 1; | |
55fea07b JK |
770 | /* remote_prepare_to_store insures that register_bytes_found gets set. */ |
771 | for (i = 0; i < register_bytes_found; i++) | |
bd5635a1 RP |
772 | { |
773 | *p++ = tohex ((registers[i] >> 4) & 0xf); | |
774 | *p++ = tohex (registers[i] & 0xf); | |
775 | } | |
776 | *p = '\0'; | |
777 | ||
778 | remote_send (buf); | |
bd5635a1 RP |
779 | } |
780 | ||
b43e0347 JK |
781 | #if 0 |
782 | ||
783 | /* Use of the data cache is disabled because it loses for looking at | |
784 | and changing hardware I/O ports and the like. Accepting `volatile' | |
785 | would perhaps be one way to fix it, but a better way which would | |
786 | win for more cases would be to use the executable file for the text | |
787 | segment, like the `icache' code below but done cleanly (in some | |
788 | target-independent place, perhaps in target_xfer_memory, perhaps | |
789 | based on assigning each target a speed or perhaps by some simpler | |
790 | mechanism). */ | |
791 | ||
bd5635a1 RP |
792 | /* Read a word from remote address ADDR and return it. |
793 | This goes through the data cache. */ | |
794 | ||
b43e0347 | 795 | static int |
bd5635a1 RP |
796 | remote_fetch_word (addr) |
797 | CORE_ADDR addr; | |
798 | { | |
d538b510 | 799 | #if 0 |
bd5635a1 RP |
800 | if (icache) |
801 | { | |
802 | extern CORE_ADDR text_start, text_end; | |
803 | ||
804 | if (addr >= text_start && addr < text_end) | |
805 | { | |
806 | int buffer; | |
807 | xfer_core_file (addr, &buffer, sizeof (int)); | |
808 | return buffer; | |
809 | } | |
810 | } | |
d538b510 RP |
811 | #endif |
812 | return dcache_fetch (remote_dcache, addr); | |
bd5635a1 RP |
813 | } |
814 | ||
815 | /* Write a word WORD into remote address ADDR. | |
816 | This goes through the data cache. */ | |
817 | ||
b43e0347 | 818 | static void |
bd5635a1 RP |
819 | remote_store_word (addr, word) |
820 | CORE_ADDR addr; | |
821 | int word; | |
822 | { | |
d538b510 | 823 | dcache_poke (remote_dcache, addr, word); |
bd5635a1 | 824 | } |
b43e0347 | 825 | #endif /* 0 */ |
bd5635a1 RP |
826 | \f |
827 | /* Write memory data directly to the remote machine. | |
828 | This does not inform the data cache; the data cache uses this. | |
829 | MEMADDR is the address in the remote memory space. | |
830 | MYADDR is the address of the buffer in our space. | |
d538b510 | 831 | LEN is the number of bytes. |
bd5635a1 | 832 | |
d538b510 RP |
833 | Returns number of bytes transferred, or 0 for error. */ |
834 | ||
835 | static int | |
bd5635a1 RP |
836 | remote_write_bytes (memaddr, myaddr, len) |
837 | CORE_ADDR memaddr; | |
d538b510 | 838 | unsigned char *myaddr; |
bd5635a1 RP |
839 | int len; |
840 | { | |
841 | char buf[PBUFSIZ]; | |
842 | int i; | |
843 | char *p; | |
844 | ||
d24c0599 JK |
845 | /* FIXME-32x64: Need a version of print_address_numeric which puts the |
846 | result in a buffer like sprintf. */ | |
4aa6fe10 | 847 | sprintf (buf, "M%lx,%x:", (unsigned long) memaddr, len); |
bd5635a1 | 848 | |
b543979c | 849 | /* We send target system values byte by byte, in increasing byte addresses, |
bd5635a1 RP |
850 | each byte encoded as two hex characters. */ |
851 | ||
852 | p = buf + strlen (buf); | |
853 | for (i = 0; i < len; i++) | |
854 | { | |
855 | *p++ = tohex ((myaddr[i] >> 4) & 0xf); | |
856 | *p++ = tohex (myaddr[i] & 0xf); | |
857 | } | |
858 | *p = '\0'; | |
859 | ||
d538b510 RP |
860 | putpkt (buf); |
861 | getpkt (buf, 0); | |
862 | ||
863 | if (buf[0] == 'E') | |
864 | { | |
865 | /* There is no correspondance between what the remote protocol uses | |
866 | for errors and errno codes. We would like a cleaner way of | |
867 | representing errors (big enough to include errno codes, bfd_error | |
868 | codes, and others). But for now just return EIO. */ | |
869 | errno = EIO; | |
870 | return 0; | |
871 | } | |
872 | return len; | |
bd5635a1 RP |
873 | } |
874 | ||
875 | /* Read memory data directly from the remote machine. | |
876 | This does not use the data cache; the data cache uses this. | |
877 | MEMADDR is the address in the remote memory space. | |
878 | MYADDR is the address of the buffer in our space. | |
d538b510 | 879 | LEN is the number of bytes. |
bd5635a1 | 880 | |
d538b510 RP |
881 | Returns number of bytes transferred, or 0 for error. */ |
882 | ||
883 | static int | |
bd5635a1 RP |
884 | remote_read_bytes (memaddr, myaddr, len) |
885 | CORE_ADDR memaddr; | |
d538b510 | 886 | unsigned char *myaddr; |
bd5635a1 RP |
887 | int len; |
888 | { | |
889 | char buf[PBUFSIZ]; | |
890 | int i; | |
891 | char *p; | |
892 | ||
893 | if (len > PBUFSIZ / 2 - 1) | |
894 | abort (); | |
895 | ||
d24c0599 JK |
896 | /* FIXME-32x64: Need a version of print_address_numeric which puts the |
897 | result in a buffer like sprintf. */ | |
4aa6fe10 | 898 | sprintf (buf, "m%lx,%x", (unsigned long) memaddr, len); |
d538b510 RP |
899 | putpkt (buf); |
900 | getpkt (buf, 0); | |
901 | ||
902 | if (buf[0] == 'E') | |
903 | { | |
904 | /* There is no correspondance between what the remote protocol uses | |
905 | for errors and errno codes. We would like a cleaner way of | |
906 | representing errors (big enough to include errno codes, bfd_error | |
907 | codes, and others). But for now just return EIO. */ | |
908 | errno = EIO; | |
909 | return 0; | |
910 | } | |
bd5635a1 | 911 | |
b543979c | 912 | /* Reply describes memory byte by byte, |
bd5635a1 RP |
913 | each byte encoded as two hex characters. */ |
914 | ||
915 | p = buf; | |
916 | for (i = 0; i < len; i++) | |
917 | { | |
918 | if (p[0] == 0 || p[1] == 0) | |
d538b510 RP |
919 | /* Reply is short. This means that we were able to read only part |
920 | of what we wanted to. */ | |
921 | break; | |
bd5635a1 RP |
922 | myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]); |
923 | p += 2; | |
924 | } | |
d538b510 | 925 | return i; |
bd5635a1 RP |
926 | } |
927 | \f | |
928 | /* Read or write LEN bytes from inferior memory at MEMADDR, transferring | |
e1ce8aa5 | 929 | to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is |
bd5635a1 RP |
930 | nonzero. Returns length of data written or read; 0 for error. */ |
931 | ||
b543979c JG |
932 | /* ARGSUSED */ |
933 | static int | |
934 | remote_xfer_memory(memaddr, myaddr, len, should_write, target) | |
bd5635a1 RP |
935 | CORE_ADDR memaddr; |
936 | char *myaddr; | |
937 | int len; | |
e1ce8aa5 | 938 | int should_write; |
b543979c | 939 | struct target_ops *target; /* ignored */ |
bd5635a1 | 940 | { |
bd5635a1 | 941 | int xfersize; |
d538b510 RP |
942 | int bytes_xferred; |
943 | int total_xferred = 0; | |
944 | ||
bd5635a1 RP |
945 | while (len > 0) |
946 | { | |
947 | if (len > MAXBUFBYTES) | |
948 | xfersize = MAXBUFBYTES; | |
949 | else | |
950 | xfersize = len; | |
951 | ||
e1ce8aa5 | 952 | if (should_write) |
94d4b713 JK |
953 | bytes_xferred = remote_write_bytes (memaddr, |
954 | (unsigned char *)myaddr, xfersize); | |
bd5635a1 | 955 | else |
94d4b713 JK |
956 | bytes_xferred = remote_read_bytes (memaddr, |
957 | (unsigned char *)myaddr, xfersize); | |
d538b510 RP |
958 | |
959 | /* If we get an error, we are done xferring. */ | |
960 | if (bytes_xferred == 0) | |
961 | break; | |
962 | ||
963 | memaddr += bytes_xferred; | |
964 | myaddr += bytes_xferred; | |
965 | len -= bytes_xferred; | |
966 | total_xferred += bytes_xferred; | |
bd5635a1 | 967 | } |
d538b510 | 968 | return total_xferred; |
bd5635a1 RP |
969 | } |
970 | ||
94d4b713 JK |
971 | #if 0 |
972 | /* Enable after 4.12. */ | |
973 | ||
974 | void | |
975 | remote_search (len, data, mask, startaddr, increment, lorange, hirange | |
976 | addr_found, data_found) | |
977 | int len; | |
978 | char *data; | |
979 | char *mask; | |
980 | CORE_ADDR startaddr; | |
981 | int increment; | |
982 | CORE_ADDR lorange; | |
983 | CORE_ADDR hirange; | |
984 | CORE_ADDR *addr_found; | |
985 | char *data_found; | |
986 | { | |
987 | if (increment == -4 && len == 4) | |
988 | { | |
989 | long mask_long, data_long; | |
990 | long data_found_long; | |
991 | CORE_ADDR addr_we_found; | |
992 | char buf[PBUFSIZ]; | |
993 | long returned_long[2]; | |
994 | char *p; | |
995 | ||
996 | mask_long = extract_unsigned_integer (mask, len); | |
997 | data_long = extract_unsigned_integer (data, len); | |
998 | sprintf (buf, "t%x:%x,%x", startaddr, data_long, mask_long); | |
999 | putpkt (buf); | |
1000 | getpkt (buf, 0); | |
1001 | if (buf[0] == '\0') | |
1002 | { | |
1003 | /* The stub doesn't support the 't' request. We might want to | |
1004 | remember this fact, but on the other hand the stub could be | |
1005 | switched on us. Maybe we should remember it only until | |
1006 | the next "target remote". */ | |
1007 | generic_search (len, data, mask, startaddr, increment, lorange, | |
1008 | hirange, addr_found, data_found); | |
1009 | return; | |
1010 | } | |
1011 | ||
1012 | if (buf[0] == 'E') | |
1013 | /* There is no correspondance between what the remote protocol uses | |
1014 | for errors and errno codes. We would like a cleaner way of | |
1015 | representing errors (big enough to include errno codes, bfd_error | |
1016 | codes, and others). But for now just use EIO. */ | |
1017 | memory_error (EIO, startaddr); | |
1018 | p = buf; | |
1019 | addr_we_found = 0; | |
1020 | while (*p != '\0' && *p != ',') | |
1021 | addr_we_found = (addr_we_found << 4) + fromhex (*p++); | |
1022 | if (*p == '\0') | |
1023 | error ("Protocol error: short return for search"); | |
1024 | ||
1025 | data_found_long = 0; | |
1026 | while (*p != '\0' && *p != ',') | |
1027 | data_found_long = (data_found_long << 4) + fromhex (*p++); | |
1028 | /* Ignore anything after this comma, for future extensions. */ | |
1029 | ||
1030 | if (addr_we_found < lorange || addr_we_found >= hirange) | |
1031 | { | |
1032 | *addr_found = 0; | |
1033 | return; | |
1034 | } | |
1035 | ||
1036 | *addr_found = addr_we_found; | |
1037 | *data_found = store_unsigned_integer (data_we_found, len); | |
1038 | return; | |
1039 | } | |
1040 | generic_search (len, data, mask, startaddr, increment, lorange, | |
1041 | hirange, addr_found, data_found); | |
1042 | } | |
1043 | #endif /* 0 */ | |
1044 | \f | |
b543979c | 1045 | static void |
8f86a4e4 | 1046 | remote_files_info (ignore) |
5af4f5f6 | 1047 | struct target_ops *ignore; |
bd5635a1 | 1048 | { |
7c622b41 | 1049 | puts_filtered ("Debugging a target over a serial line.\n"); |
bd5635a1 RP |
1050 | } |
1051 | \f | |
e50ebec8 JK |
1052 | /* Stuff for dealing with the packets which are part of this protocol. |
1053 | See comment at top of file for details. */ | |
bd5635a1 | 1054 | |
ebdb9ade | 1055 | /* Read a single character from the remote end, masking it down to 7 bits. */ |
b543979c | 1056 | |
bd5635a1 | 1057 | static int |
754e5da2 SG |
1058 | readchar (timeout) |
1059 | int timeout; | |
bd5635a1 | 1060 | { |
ebdb9ade | 1061 | int ch; |
bd5635a1 | 1062 | |
ebdb9ade | 1063 | ch = SERIAL_READCHAR (remote_desc, timeout); |
fce7f2d9 | 1064 | |
754e5da2 SG |
1065 | switch (ch) |
1066 | { | |
1067 | case SERIAL_EOF: | |
1068 | error ("Remote connection closed"); | |
1069 | case SERIAL_ERROR: | |
1070 | perror_with_name ("Remote communication error"); | |
1071 | case SERIAL_TIMEOUT: | |
1072 | return ch; | |
1073 | default: | |
1074 | return ch & 0x7f; | |
1075 | } | |
bd5635a1 RP |
1076 | } |
1077 | ||
1078 | /* Send the command in BUF to the remote machine, | |
1079 | and read the reply into BUF. | |
1080 | Report an error if we get an error reply. */ | |
1081 | ||
1082 | static void | |
1083 | remote_send (buf) | |
1084 | char *buf; | |
1085 | { | |
1086 | ||
1087 | putpkt (buf); | |
7c622b41 | 1088 | getpkt (buf, 0); |
bd5635a1 RP |
1089 | |
1090 | if (buf[0] == 'E') | |
1091 | error ("Remote failure reply: %s", buf); | |
1092 | } | |
1093 | ||
1094 | /* Send a packet to the remote machine, with error checking. | |
1095 | The data of the packet is in BUF. */ | |
1096 | ||
1097 | static void | |
1098 | putpkt (buf) | |
1099 | char *buf; | |
1100 | { | |
1101 | int i; | |
1102 | unsigned char csum = 0; | |
b543979c | 1103 | char buf2[PBUFSIZ]; |
bd5635a1 | 1104 | int cnt = strlen (buf); |
ebdb9ade | 1105 | int ch; |
bd5635a1 RP |
1106 | char *p; |
1107 | ||
1108 | /* Copy the packet into buffer BUF2, encapsulating it | |
1109 | and giving it a checksum. */ | |
1110 | ||
b543979c JG |
1111 | if (cnt > sizeof(buf2) - 5) /* Prosanity check */ |
1112 | abort(); | |
1113 | ||
bd5635a1 RP |
1114 | p = buf2; |
1115 | *p++ = '$'; | |
1116 | ||
1117 | for (i = 0; i < cnt; i++) | |
1118 | { | |
1119 | csum += buf[i]; | |
1120 | *p++ = buf[i]; | |
1121 | } | |
1122 | *p++ = '#'; | |
1123 | *p++ = tohex ((csum >> 4) & 0xf); | |
1124 | *p++ = tohex (csum & 0xf); | |
1125 | ||
1126 | /* Send it over and over until we get a positive ack. */ | |
1127 | ||
6b27ebe8 JK |
1128 | while (1) |
1129 | { | |
1624c38f SG |
1130 | int started_error_output = 0; |
1131 | ||
d0d8484a | 1132 | if (remote_debug) |
6b27ebe8 JK |
1133 | { |
1134 | *p = '\0'; | |
1624c38f SG |
1135 | printf_unfiltered ("Sending packet: %s...", buf2); |
1136 | gdb_flush(gdb_stdout); | |
6b27ebe8 JK |
1137 | } |
1138 | if (SERIAL_WRITE (remote_desc, buf2, p - buf2)) | |
1139 | perror_with_name ("putpkt: write failed"); | |
1140 | ||
1141 | /* read until either a timeout occurs (-2) or '+' is read */ | |
1142 | while (1) | |
1143 | { | |
754e5da2 | 1144 | ch = readchar (remote_timeout); |
6b27ebe8 | 1145 | |
1624c38f SG |
1146 | if (remote_debug) |
1147 | { | |
1148 | switch (ch) | |
1149 | { | |
1150 | case '+': | |
1151 | case SERIAL_TIMEOUT: | |
1624c38f SG |
1152 | case '$': |
1153 | if (started_error_output) | |
1154 | { | |
1155 | putc_unfiltered ('\n'); | |
1156 | started_error_output = 0; | |
1157 | } | |
1158 | } | |
1159 | } | |
1160 | ||
6b27ebe8 JK |
1161 | switch (ch) |
1162 | { | |
1163 | case '+': | |
d0d8484a | 1164 | if (remote_debug) |
199b2450 | 1165 | printf_unfiltered("Ack\n"); |
6b27ebe8 JK |
1166 | return; |
1167 | case SERIAL_TIMEOUT: | |
1168 | break; /* Retransmit buffer */ | |
1624c38f SG |
1169 | case '$': |
1170 | { | |
1171 | unsigned char junkbuf[PBUFSIZ]; | |
1172 | ||
1173 | /* It's probably an old response, and we're out of sync. Just | |
1174 | gobble up the packet and ignore it. */ | |
1175 | getpkt (junkbuf, 0); | |
1176 | continue; /* Now, go look for + */ | |
1177 | } | |
6b27ebe8 | 1178 | default: |
d0d8484a | 1179 | if (remote_debug) |
1624c38f SG |
1180 | { |
1181 | if (!started_error_output) | |
1182 | { | |
1183 | started_error_output = 1; | |
1184 | printf_unfiltered ("putpkt: Junk: "); | |
1185 | } | |
1186 | putc_unfiltered (ch & 0177); | |
1187 | } | |
6b27ebe8 JK |
1188 | continue; |
1189 | } | |
1190 | break; /* Here to retransmit */ | |
1191 | } | |
981a3309 | 1192 | |
94d4b713 JK |
1193 | #if 0 |
1194 | /* This is wrong. If doing a long backtrace, the user should be | |
1195 | able to get out next time we call QUIT, without anything as violent | |
1196 | as interrupt_query. If we want to provide a way out of here | |
1197 | without getting to the next QUIT, it should be based on hitting | |
1198 | ^C twice as in remote_wait. */ | |
981a3309 SG |
1199 | if (quit_flag) |
1200 | { | |
1201 | quit_flag = 0; | |
1202 | interrupt_query (); | |
1203 | } | |
94d4b713 | 1204 | #endif |
6b27ebe8 | 1205 | } |
bd5635a1 RP |
1206 | } |
1207 | ||
754e5da2 SG |
1208 | /* Come here after finding the start of the frame. Collect the rest into BUF, |
1209 | verifying the checksum, length, and handling run-length compression. | |
1210 | Returns 0 on any error, 1 on success. */ | |
1211 | ||
1212 | static int | |
1213 | read_frame (buf) | |
1214 | char *buf; | |
1215 | { | |
1216 | unsigned char csum; | |
1217 | char *bp; | |
1218 | int c; | |
1219 | ||
1220 | csum = 0; | |
1221 | bp = buf; | |
1222 | ||
1223 | while (1) | |
1224 | { | |
1225 | c = readchar (remote_timeout); | |
1226 | ||
1227 | switch (c) | |
1228 | { | |
1229 | case SERIAL_TIMEOUT: | |
1230 | if (remote_debug) | |
1231 | puts_filtered ("Timeout in mid-packet, retrying\n"); | |
1232 | return 0; | |
1233 | case '$': | |
1234 | if (remote_debug) | |
1235 | puts_filtered ("Saw new packet start in middle of old one\n"); | |
1236 | return 0; /* Start a new packet, count retries */ | |
1237 | case '#': | |
1238 | { | |
1239 | unsigned char pktcsum; | |
1240 | ||
1241 | *bp = '\000'; | |
1242 | ||
205fc02b SC |
1243 | pktcsum = fromhex (readchar (remote_timeout)) << 4; |
1244 | pktcsum |= fromhex (readchar (remote_timeout)); | |
754e5da2 SG |
1245 | |
1246 | if (csum == pktcsum) | |
1247 | return 1; | |
1248 | ||
1249 | printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=", | |
1250 | pktcsum, csum); | |
1251 | puts_filtered (buf); | |
1252 | puts_filtered ("\n"); | |
1253 | ||
1254 | return 0; | |
1255 | } | |
1256 | case '*': /* Run length encoding */ | |
1257 | c = readchar (remote_timeout); | |
1258 | csum += c; | |
1259 | c = c - ' ' + 3; /* Compute repeat count */ | |
1260 | ||
1261 | if (bp + c - 1 < buf + PBUFSIZ - 1) | |
1262 | { | |
1263 | memset (bp, *(bp - 1), c); | |
1264 | bp += c; | |
1265 | continue; | |
1266 | } | |
1267 | ||
1268 | *bp = '\0'; | |
1269 | printf_filtered ("Repeat count %d too large for buffer: ", c); | |
1270 | puts_filtered (buf); | |
1271 | puts_filtered ("\n"); | |
1272 | ||
1273 | return 0; | |
1274 | default: | |
1275 | if (bp < buf + PBUFSIZ - 1) | |
1276 | { | |
1277 | *bp++ = c; | |
1278 | csum += c; | |
1279 | continue; | |
1280 | } | |
1281 | ||
1282 | *bp = '\0'; | |
1283 | puts_filtered ("Remote packet too long: "); | |
1284 | puts_filtered (buf); | |
1285 | puts_filtered ("\n"); | |
1286 | ||
1287 | return 0; | |
1288 | } | |
1289 | } | |
1290 | } | |
1291 | ||
bd5635a1 | 1292 | /* Read a packet from the remote machine, with error checking, |
7c622b41 JG |
1293 | and store it in BUF. BUF is expected to be of size PBUFSIZ. |
1294 | If FOREVER, wait forever rather than timing out; this is used | |
1295 | while the target is executing user code. */ | |
bd5635a1 RP |
1296 | |
1297 | static void | |
754e5da2 SG |
1298 | getpkt (buf, forever) |
1299 | char *buf; | |
ebdb9ade | 1300 | int forever; |
bd5635a1 RP |
1301 | { |
1302 | char *bp; | |
754e5da2 SG |
1303 | int c; |
1304 | int tries; | |
1305 | int timeout; | |
1306 | int val; | |
94d4b713 | 1307 | |
754e5da2 SG |
1308 | if (forever) |
1309 | timeout = -1; | |
1310 | else | |
1311 | timeout = remote_timeout; | |
bd5635a1 | 1312 | |
754e5da2 | 1313 | #define MAX_TRIES 10 |
981a3309 | 1314 | |
754e5da2 SG |
1315 | for (tries = 1; tries <= MAX_TRIES; tries++) |
1316 | { | |
7c622b41 JG |
1317 | /* This can loop forever if the remote side sends us characters |
1318 | continuously, but if it pauses, we'll get a zero from readchar | |
1319 | because of timeout. Then we'll count that as a retry. */ | |
6b27ebe8 | 1320 | |
754e5da2 SG |
1321 | /* Note that we will only wait forever prior to the start of a packet. |
1322 | After that, we expect characters to arrive at a brisk pace. They | |
1323 | should show up within remote_timeout intervals. */ | |
6b27ebe8 | 1324 | |
754e5da2 | 1325 | do |
6b27ebe8 | 1326 | { |
754e5da2 | 1327 | c = readchar (timeout); |
7c622b41 | 1328 | |
ebdb9ade | 1329 | if (c == SERIAL_TIMEOUT) |
7c622b41 | 1330 | { |
d0d8484a | 1331 | if (remote_debug) |
754e5da2 SG |
1332 | puts_filtered ("Timed out.\n"); |
1333 | goto retry; | |
7c622b41 | 1334 | } |
bd5635a1 | 1335 | } |
754e5da2 | 1336 | while (c != '$'); |
bd5635a1 | 1337 | |
754e5da2 | 1338 | /* We've found the start of a packet, now collect the data. */ |
38094c60 | 1339 | |
754e5da2 SG |
1340 | val = read_frame (buf); |
1341 | ||
1342 | if (val == 1) | |
38094c60 | 1343 | { |
754e5da2 SG |
1344 | if (remote_debug) |
1345 | fprintf_unfiltered (gdb_stderr, "Packet received: %s\n", buf); | |
1346 | SERIAL_WRITE (remote_desc, "+", 1); | |
1347 | return; | |
38094c60 | 1348 | } |
754e5da2 SG |
1349 | |
1350 | /* Try the whole thing again. */ | |
1351 | retry: | |
1352 | SERIAL_WRITE (remote_desc, "-", 1); | |
bd5635a1 RP |
1353 | } |
1354 | ||
754e5da2 | 1355 | /* We have tried hard enough, and just can't receive the packet. Give up. */ |
7c622b41 | 1356 | |
754e5da2 | 1357 | printf_unfiltered ("Ignoring packet error, continuing...\n"); |
ebdb9ade | 1358 | SERIAL_WRITE (remote_desc, "+", 1); |
bd5635a1 RP |
1359 | } |
1360 | \f | |
ebdb9ade JK |
1361 | static void |
1362 | remote_kill () | |
1363 | { | |
1364 | putpkt ("k"); | |
1365 | /* Don't wait for it to die. I'm not really sure it matters whether | |
1366 | we do or not. For the existing stubs, kill is a noop. */ | |
1367 | target_mourn_inferior (); | |
1368 | } | |
bd5635a1 | 1369 | |
ebdb9ade JK |
1370 | static void |
1371 | remote_mourn () | |
1372 | { | |
1373 | unpush_target (&remote_ops); | |
1374 | generic_mourn_inferior (); | |
1375 | } | |
1376 | \f | |
5af4f5f6 JK |
1377 | #ifdef REMOTE_BREAKPOINT |
1378 | ||
1379 | /* On some machines, e.g. 68k, we may use a different breakpoint instruction | |
1380 | than other targets. */ | |
1381 | static unsigned char break_insn[] = REMOTE_BREAKPOINT; | |
1382 | ||
1383 | /* Check that it fits in BREAKPOINT_MAX bytes. */ | |
1384 | static unsigned char check_break_insn_size[BREAKPOINT_MAX] = REMOTE_BREAKPOINT; | |
1385 | ||
1386 | #else /* No REMOTE_BREAKPOINT. */ | |
1387 | ||
1388 | /* Same old breakpoint instruction. This code does nothing different | |
1389 | than mem-break.c. */ | |
1390 | static unsigned char break_insn[] = BREAKPOINT; | |
1391 | ||
1392 | #endif /* No REMOTE_BREAKPOINT. */ | |
1393 | ||
1394 | /* Insert a breakpoint on targets that don't have any better breakpoint | |
1395 | support. We read the contents of the target location and stash it, | |
1396 | then overwrite it with a breakpoint instruction. ADDR is the target | |
1397 | location in the target machine. CONTENTS_CACHE is a pointer to | |
1398 | memory allocated for saving the target contents. It is guaranteed | |
1399 | by the caller to be long enough to save sizeof BREAKPOINT bytes (this | |
1400 | is accomplished via BREAKPOINT_MAX). */ | |
1401 | ||
d538b510 | 1402 | static int |
5af4f5f6 JK |
1403 | remote_insert_breakpoint (addr, contents_cache) |
1404 | CORE_ADDR addr; | |
1405 | char *contents_cache; | |
1406 | { | |
1407 | int val; | |
1408 | ||
1409 | val = target_read_memory (addr, contents_cache, sizeof break_insn); | |
1410 | ||
1411 | if (val == 0) | |
1412 | val = target_write_memory (addr, (char *)break_insn, sizeof break_insn); | |
1413 | ||
1414 | return val; | |
1415 | } | |
1416 | ||
d538b510 | 1417 | static int |
5af4f5f6 JK |
1418 | remote_remove_breakpoint (addr, contents_cache) |
1419 | CORE_ADDR addr; | |
1420 | char *contents_cache; | |
1421 | { | |
1422 | return target_write_memory (addr, contents_cache, sizeof break_insn); | |
1423 | } | |
1424 | \f | |
bd5635a1 RP |
1425 | /* Define the target subroutine names */ |
1426 | ||
1427 | struct target_ops remote_ops = { | |
b543979c JG |
1428 | "remote", /* to_shortname */ |
1429 | "Remote serial target in gdb-specific protocol", /* to_longname */ | |
1430 | "Use a remote computer via a serial line, using a gdb-specific protocol.\n\ | |
1431 | Specify the serial device it is connected to (e.g. /dev/ttya).", /* to_doc */ | |
1432 | remote_open, /* to_open */ | |
1433 | remote_close, /* to_close */ | |
1434 | NULL, /* to_attach */ | |
1435 | remote_detach, /* to_detach */ | |
1436 | remote_resume, /* to_resume */ | |
1437 | remote_wait, /* to_wait */ | |
1438 | remote_fetch_registers, /* to_fetch_registers */ | |
1439 | remote_store_registers, /* to_store_registers */ | |
1440 | remote_prepare_to_store, /* to_prepare_to_store */ | |
b543979c JG |
1441 | remote_xfer_memory, /* to_xfer_memory */ |
1442 | remote_files_info, /* to_files_info */ | |
5af4f5f6 JK |
1443 | |
1444 | remote_insert_breakpoint, /* to_insert_breakpoint */ | |
1445 | remote_remove_breakpoint, /* to_remove_breakpoint */ | |
1446 | ||
b543979c JG |
1447 | NULL, /* to_terminal_init */ |
1448 | NULL, /* to_terminal_inferior */ | |
1449 | NULL, /* to_terminal_ours_for_output */ | |
1450 | NULL, /* to_terminal_ours */ | |
1451 | NULL, /* to_terminal_info */ | |
ebdb9ade | 1452 | remote_kill, /* to_kill */ |
6b27ebe8 | 1453 | generic_load, /* to_load */ |
b543979c JG |
1454 | NULL, /* to_lookup_symbol */ |
1455 | NULL, /* to_create_inferior */ | |
ebdb9ade | 1456 | remote_mourn, /* to_mourn_inferior */ |
34517ebc | 1457 | 0, /* to_can_run */ |
7c622b41 | 1458 | 0, /* to_notice_signals */ |
b543979c JG |
1459 | process_stratum, /* to_stratum */ |
1460 | NULL, /* to_next */ | |
1461 | 1, /* to_has_all_memory */ | |
1462 | 1, /* to_has_memory */ | |
1463 | 1, /* to_has_stack */ | |
1464 | 1, /* to_has_registers */ | |
1465 | 1, /* to_has_execution */ | |
1466 | NULL, /* sections */ | |
1467 | NULL, /* sections_end */ | |
1468 | OPS_MAGIC /* to_magic */ | |
bd5635a1 RP |
1469 | }; |
1470 | ||
1471 | void | |
1472 | _initialize_remote () | |
1473 | { | |
1474 | add_target (&remote_ops); | |
976bb0be | 1475 | } |