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