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