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