]>
Commit | Line | Data |
---|---|---|
b543979c | 1 | /* Remote target communications for serial-line targets in custom GDB protocol |
0a325463 | 2 | Copyright 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997 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 | 17 | along with this program; if not, write to the Free Software |
998cfe7d | 18 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 | ||
0a325463 SG |
100 | continue with Csig;AA..AA Continue with signal sig (hex signal |
101 | signal number). If ;AA..AA is omitted, resume | |
102 | at same address. | |
4cc1b3f7 | 103 | |
0a325463 | 104 | step with Ssig;AA..AA Like 'C' but step not continue. |
4cc1b3f7 JK |
105 | signal |
106 | ||
bd5635a1 RP |
107 | last signal ? Reply the current reason for stopping. |
108 | This is the same reply as is generated | |
109 | for step or cont : SAA where AA is the | |
110 | signal number. | |
111 | ||
b52cac6b FF |
112 | detach D Reply OK. |
113 | ||
bd5635a1 RP |
114 | There is no immediate reply to step or cont. |
115 | The reply comes when the machine stops. | |
4cc1b3f7 | 116 | It is SAA AA is the signal number. |
bd5635a1 | 117 | |
4cc1b3f7 | 118 | or... TAAn...:r...;n...:r...;n...:r...; |
e50ebec8 | 119 | AA = signal number |
4cc1b3f7 JK |
120 | n... = register number (hex) |
121 | r... = register contents | |
122 | n... = `thread' | |
123 | r... = thread process ID. This is | |
124 | a hex integer. | |
125 | n... = other string not starting | |
126 | with valid hex digit. | |
127 | gdb should ignore this n,r pair | |
128 | and go on to the next. This way | |
129 | we can extend the protocol. | |
72bba93b | 130 | or... WAA The process exited, and AA is |
758aeb93 ILT |
131 | the exit status. This is only |
132 | applicable for certains sorts of | |
133 | targets. | |
4cc1b3f7 JK |
134 | or... XAA The process terminated with signal |
135 | AA. | |
998cfe7d SC |
136 | or... OXX..XX XX..XX is hex encoding of ASCII data. This |
137 | can happen at any time while the program is | |
4cc1b3f7 JK |
138 | running and the debugger should |
139 | continue to wait for 'W', 'T', etc. | |
140 | ||
2b576293 C |
141 | thread alive TXX Find out if the thread XX is alive. |
142 | reply OK thread is still alive | |
143 | ENN thread is dead | |
144 | ||
145 | remote restart RXX Restart the remote server | |
146 | ||
147 | extended ops ! Use the extended remote protocol. | |
148 | Sticky -- only needs to be set once. | |
149 | ||
d538b510 RP |
150 | kill request k |
151 | ||
152 | toggle debug d toggle debug flag (see 386 & 68k stubs) | |
153 | reset r reset -- see sparc stub. | |
154 | reserved <other> On other requests, the stub should | |
155 | ignore the request and send an empty | |
156 | response ($#<checksum>). This way | |
157 | we can extend the protocol and GDB | |
158 | can tell whether the stub it is | |
159 | talking to uses the old or the new. | |
72bba93b | 160 | search tAA:PP,MM Search backwards starting at address |
94d4b713 JK |
161 | AA for a match with pattern PP and |
162 | mask MM. PP and MM are 4 bytes. | |
163 | Not supported by all stubs. | |
164 | ||
72bba93b SG |
165 | general query qXXXX Request info about XXXX. |
166 | general set QXXXX=yyyy Set value of XXXX to yyyy. | |
167 | query sect offs qOffsets Get section offsets. Reply is | |
168 | Text=xxx;Data=yyy;Bss=zzz | |
72bba93b | 169 | |
94d4b713 | 170 | Responses can be run-length encoded to save space. A '*' means that |
284f4ee9 | 171 | the next character is an ASCII encoding giving a repeat count which |
94d4b713 | 172 | stands for that many repititions of the character preceding the '*'. |
284f4ee9 SC |
173 | The encoding is n+29, yielding a printable character where n >=3 |
174 | (which is where rle starts to win). Don't use an n > 126. | |
175 | ||
176 | So | |
177 | "0* " means the same as "0000". */ | |
bd5635a1 | 178 | |
d747e0af | 179 | #include "defs.h" |
2b576293 | 180 | #include "gdb_string.h" |
bd5635a1 | 181 | #include <fcntl.h> |
bd5635a1 RP |
182 | #include "frame.h" |
183 | #include "inferior.h" | |
e50ebec8 | 184 | #include "bfd.h" |
6b27ebe8 | 185 | #include "symfile.h" |
bd5635a1 RP |
186 | #include "target.h" |
187 | #include "wait.h" | |
0d14c7df | 188 | /*#include "terminal.h"*/ |
8f86a4e4 | 189 | #include "gdbcmd.h" |
758aeb93 ILT |
190 | #include "objfiles.h" |
191 | #include "gdb-stabs.h" | |
cb1709ae | 192 | #include "gdbthread.h" |
bd5635a1 | 193 | |
d538b510 RP |
194 | #include "dcache.h" |
195 | ||
bd5635a1 RP |
196 | #ifdef USG |
197 | #include <sys/types.h> | |
198 | #endif | |
199 | ||
200 | #include <signal.h> | |
ebdb9ade | 201 | #include "serial.h" |
bd5635a1 | 202 | |
b543979c JG |
203 | /* Prototypes for local functions */ |
204 | ||
45993f61 | 205 | static int remote_write_bytes PARAMS ((CORE_ADDR memaddr, |
43fc25c8 | 206 | char *myaddr, int len)); |
b543979c | 207 | |
45993f61 | 208 | static int remote_read_bytes PARAMS ((CORE_ADDR memaddr, |
43fc25c8 | 209 | char *myaddr, int len)); |
b543979c | 210 | |
45993f61 | 211 | static void remote_files_info PARAMS ((struct target_ops *ignore)); |
b543979c | 212 | |
45993f61 SC |
213 | static int remote_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr, |
214 | int len, int should_write, | |
215 | struct target_ops *target)); | |
b543979c | 216 | |
45993f61 | 217 | static void remote_prepare_to_store PARAMS ((void)); |
b543979c | 218 | |
45993f61 | 219 | static void remote_fetch_registers PARAMS ((int regno)); |
b543979c | 220 | |
45993f61 SC |
221 | static void remote_resume PARAMS ((int pid, int step, |
222 | enum target_signal siggnal)); | |
b543979c | 223 | |
45993f61 | 224 | static int remote_start_remote PARAMS ((char *dummy)); |
7c622b41 | 225 | |
45993f61 | 226 | static void remote_open PARAMS ((char *name, int from_tty)); |
b543979c | 227 | |
2b576293 C |
228 | static void extended_remote_open PARAMS ((char *name, int from_tty)); |
229 | ||
dd0ce8f6 | 230 | static void remote_open_1 PARAMS ((char *, int, struct target_ops *, int extended_p)); |
2b576293 | 231 | |
45993f61 | 232 | static void remote_close PARAMS ((int quitting)); |
b543979c | 233 | |
45993f61 | 234 | static void remote_store_registers PARAMS ((int regno)); |
b543979c | 235 | |
2b576293 C |
236 | static void remote_mourn PARAMS ((void)); |
237 | ||
238 | static void extended_remote_restart PARAMS ((void)); | |
239 | ||
240 | static void extended_remote_mourn PARAMS ((void)); | |
241 | ||
242 | static void extended_remote_create_inferior PARAMS ((char *, char *, char **)); | |
243 | ||
244 | static void remote_mourn_1 PARAMS ((struct target_ops *)); | |
245 | ||
45993f61 | 246 | static void remote_send PARAMS ((char *buf)); |
b543979c | 247 | |
45993f61 | 248 | static int readchar PARAMS ((int timeout)); |
b543979c | 249 | |
94d4b713 | 250 | static int remote_wait PARAMS ((int pid, struct target_waitstatus *status)); |
b543979c | 251 | |
45993f61 | 252 | static void remote_kill PARAMS ((void)); |
b543979c | 253 | |
45993f61 | 254 | static int tohex PARAMS ((int nib)); |
b543979c | 255 | |
45993f61 | 256 | static void remote_detach PARAMS ((char *args, int from_tty)); |
5af4f5f6 | 257 | |
45993f61 | 258 | static void remote_interrupt PARAMS ((int signo)); |
b543979c | 259 | |
45993f61 SC |
260 | static void remote_interrupt_twice PARAMS ((int signo)); |
261 | ||
262 | static void interrupt_query PARAMS ((void)); | |
981a3309 | 263 | |
b607efe7 FF |
264 | static void set_thread PARAMS ((int, int)); |
265 | ||
266 | static int remote_thread_alive PARAMS ((int)); | |
267 | ||
268 | static void get_offsets PARAMS ((void)); | |
269 | ||
270 | static int read_frame PARAMS ((char *)); | |
271 | ||
272 | static int remote_insert_breakpoint PARAMS ((CORE_ADDR, char *)); | |
273 | ||
274 | static int remote_remove_breakpoint PARAMS ((CORE_ADDR, char *)); | |
275 | ||
dd0ce8f6 AC |
276 | static int hexnumlen PARAMS ((ULONGEST num)); |
277 | ||
0a325463 SG |
278 | static struct target_ops remote_ops; /* Forward decl */ |
279 | static struct target_ops extended_remote_ops; /* Forward decl */ | |
bd5635a1 | 280 | |
dd0ce8f6 AC |
281 | /* exported functions */ |
282 | ||
283 | extern int fromhex PARAMS ((int a)); | |
284 | extern void getpkt PARAMS ((char *buf, int forever)); | |
285 | extern int putpkt PARAMS ((char *buf)); | |
286 | ||
287 | ||
ebdb9ade JK |
288 | /* This was 5 seconds, which is a long time to sit and wait. |
289 | Unless this is going though some terminal server or multiplexer or | |
290 | other form of hairy serial connection, I would think 2 seconds would | |
291 | be plenty. */ | |
bd5635a1 | 292 | |
cb1709ae DP |
293 | /* Changed to allow option to set timeout value. |
294 | was static int remote_timeout = 2; */ | |
295 | extern int remote_timeout; | |
bd5635a1 | 296 | |
b52cac6b FF |
297 | /* This variable chooses whether to send a ^C or a break when the user |
298 | requests program interruption. Although ^C is usually what remote | |
299 | systems expect, and that is the default here, sometimes a break is | |
300 | preferable instead. */ | |
301 | ||
302 | static int remote_break; | |
303 | ||
16e1d1d3 | 304 | /* Descriptor for I/O to remote machine. Initialize it to NULL so that |
bd5635a1 RP |
305 | remote_open knows that we don't have a file open when the program |
306 | starts. */ | |
0a325463 | 307 | static serial_t remote_desc = NULL; |
bd5635a1 | 308 | |
4d57c599 JK |
309 | /* Having this larger than 400 causes us to be incompatible with m68k-stub.c |
310 | and i386-stub.c. Normally, no one would notice because it only matters | |
311 | for writing large chunks of memory (e.g. in downloads). Also, this needs | |
312 | to be more than 400 if required to hold the registers (see below, where | |
313 | we round it up based on REGISTER_BYTES). */ | |
314 | #define PBUFSIZ 400 | |
bd5635a1 RP |
315 | |
316 | /* Maximum number of bytes to read/write at once. The value here | |
317 | is chosen to fill up a packet (the headers account for the 32). */ | |
318 | #define MAXBUFBYTES ((PBUFSIZ-32)/2) | |
319 | ||
b543979c | 320 | /* Round up PBUFSIZ to hold all the registers, at least. */ |
2ddeed27 JK |
321 | /* The blank line after the #if seems to be required to work around a |
322 | bug in HP's PA compiler. */ | |
b543979c | 323 | #if REGISTER_BYTES > MAXBUFBYTES |
2ddeed27 JK |
324 | |
325 | #undef PBUFSIZ | |
b543979c | 326 | #define PBUFSIZ (REGISTER_BYTES * 2 + 32) |
bd5635a1 | 327 | #endif |
4aa6fe10 | 328 | |
fea17b55 SS |
329 | /* This variable sets the number of bytes to be written to the target |
330 | in a single packet. Normally PBUFSIZ is satisfactory, but some | |
331 | targets need smaller values (perhaps because the receiving end | |
332 | is slow). */ | |
333 | ||
334 | static int remote_write_size = PBUFSIZ; | |
335 | ||
0a325463 SG |
336 | /* This is the size (in chars) of the first response to the `g' command. This |
337 | is used to limit the size of the memory read and write commands to prevent | |
dd0ce8f6 AC |
338 | stub buffers from overflowing. The size does not include headers and |
339 | trailers, it is only the payload size. */ | |
0a325463 SG |
340 | |
341 | static int remote_register_buf_size = 0; | |
342 | ||
4aa6fe10 JK |
343 | /* Should we try the 'P' request? If this is set to one when the stub |
344 | doesn't support 'P', the only consequence is some unnecessary traffic. */ | |
345 | static int stub_supports_P = 1; | |
346 | ||
0a325463 SG |
347 | /* These are pointers to hook functions that may be set in order to |
348 | modify resume/wait behavior for a particular architecture. */ | |
349 | ||
350 | void (*target_resume_hook) PARAMS ((void)); | |
351 | void (*target_wait_loop_hook) PARAMS ((void)); | |
352 | ||
4cc1b3f7 JK |
353 | \f |
354 | /* These are the threads which we last sent to the remote system. -1 for all | |
355 | or -2 for not sent yet. */ | |
356 | int general_thread; | |
357 | int cont_thread; | |
358 | ||
359 | static void | |
360 | set_thread (th, gen) | |
361 | int th; | |
362 | int gen; | |
363 | { | |
364 | char buf[PBUFSIZ]; | |
365 | int state = gen ? general_thread : cont_thread; | |
366 | if (state == th) | |
367 | return; | |
368 | buf[0] = 'H'; | |
369 | buf[1] = gen ? 'g' : 'c'; | |
370 | if (th == 42000) | |
371 | { | |
372 | buf[2] = '0'; | |
373 | buf[3] = '\0'; | |
374 | } | |
375 | else if (th < 0) | |
376 | sprintf (&buf[2], "-%x", -th); | |
377 | else | |
378 | sprintf (&buf[2], "%x", th); | |
379 | putpkt (buf); | |
380 | getpkt (buf, 0); | |
381 | if (gen) | |
382 | general_thread = th; | |
383 | else | |
384 | cont_thread = th; | |
385 | } | |
bd5635a1 | 386 | \f |
2b576293 | 387 | /* Return nonzero if the thread TH is still alive on the remote system. */ |
43fc25c8 JL |
388 | |
389 | static int | |
390 | remote_thread_alive (th) | |
391 | int th; | |
392 | { | |
393 | char buf[PBUFSIZ]; | |
394 | ||
395 | buf[0] = 'T'; | |
396 | if (th < 0) | |
397 | sprintf (&buf[1], "-%x", -th); | |
398 | else | |
399 | sprintf (&buf[1], "%x", th); | |
400 | putpkt (buf); | |
401 | getpkt (buf, 0); | |
402 | return (buf[0] == 'O' && buf[1] == 'K'); | |
403 | } | |
2b576293 C |
404 | |
405 | /* Restart the remote side; this is an extended protocol operation. */ | |
406 | ||
407 | static void | |
408 | extended_remote_restart () | |
409 | { | |
410 | char buf[PBUFSIZ]; | |
411 | ||
412 | /* Send the restart command; for reasons I don't understand the | |
413 | remote side really expects a number after the "R". */ | |
414 | buf[0] = 'R'; | |
415 | sprintf (&buf[1], "%x", 0); | |
416 | putpkt (buf); | |
417 | ||
418 | /* Now query for status so this looks just like we restarted | |
419 | gdbserver from scratch. */ | |
420 | putpkt ("?"); | |
421 | getpkt (buf, 0); | |
422 | } | |
43fc25c8 | 423 | \f |
bd5635a1 RP |
424 | /* Clean up connection to a remote debugger. */ |
425 | ||
e1ce8aa5 | 426 | /* ARGSUSED */ |
b543979c | 427 | static void |
bd5635a1 RP |
428 | remote_close (quitting) |
429 | int quitting; | |
430 | { | |
ebdb9ade JK |
431 | if (remote_desc) |
432 | SERIAL_CLOSE (remote_desc); | |
433 | remote_desc = NULL; | |
b543979c JG |
434 | } |
435 | ||
72bba93b SG |
436 | /* Query the remote side for the text, data and bss offsets. */ |
437 | ||
438 | static void | |
439 | get_offsets () | |
440 | { | |
dd0ce8f6 AC |
441 | char buf[PBUFSIZ], *ptr; |
442 | int lose; | |
72bba93b SG |
443 | CORE_ADDR text_addr, data_addr, bss_addr; |
444 | struct section_offsets *offs; | |
445 | ||
446 | putpkt ("qOffsets"); | |
447 | ||
1c95d7ab | 448 | getpkt (buf, 0); |
72bba93b | 449 | |
1c95d7ab JK |
450 | if (buf[0] == '\000') |
451 | return; /* Return silently. Stub doesn't support this | |
452 | command. */ | |
72bba93b SG |
453 | if (buf[0] == 'E') |
454 | { | |
455 | warning ("Remote failure reply: %s", buf); | |
456 | return; | |
457 | } | |
458 | ||
dd0ce8f6 AC |
459 | /* Pick up each field in turn. This used to be done with scanf, but |
460 | scanf will make trouble if CORE_ADDR size doesn't match | |
461 | conversion directives correctly. The following code will work | |
462 | with any size of CORE_ADDR. */ | |
463 | text_addr = data_addr = bss_addr = 0; | |
464 | ptr = buf; | |
465 | lose = 0; | |
466 | ||
467 | if (strncmp (ptr, "Text=", 5) == 0) | |
468 | { | |
469 | ptr += 5; | |
470 | /* Don't use strtol, could lose on big values. */ | |
471 | while (*ptr && *ptr != ';') | |
472 | text_addr = (text_addr << 4) + fromhex (*ptr++); | |
473 | } | |
474 | else | |
475 | lose = 1; | |
476 | ||
477 | if (!lose && strncmp (ptr, ";Data=", 6) == 0) | |
478 | { | |
479 | ptr += 6; | |
480 | while (*ptr && *ptr != ';') | |
481 | data_addr = (data_addr << 4) + fromhex (*ptr++); | |
482 | } | |
483 | else | |
484 | lose = 1; | |
485 | ||
486 | if (!lose && strncmp (ptr, ";Bss=", 5) == 0) | |
487 | { | |
488 | ptr += 5; | |
489 | while (*ptr && *ptr != ';') | |
490 | bss_addr = (bss_addr << 4) + fromhex (*ptr++); | |
491 | } | |
492 | else | |
493 | lose = 1; | |
494 | ||
495 | if (lose) | |
72bba93b SG |
496 | error ("Malformed response to offset query, %s", buf); |
497 | ||
498 | if (symfile_objfile == NULL) | |
499 | return; | |
500 | ||
501 | offs = (struct section_offsets *) alloca (sizeof (struct section_offsets) | |
502 | + symfile_objfile->num_sections | |
503 | * sizeof (offs->offsets)); | |
504 | memcpy (offs, symfile_objfile->section_offsets, | |
505 | sizeof (struct section_offsets) | |
506 | + symfile_objfile->num_sections | |
507 | * sizeof (offs->offsets)); | |
508 | ||
509 | ANOFFSET (offs, SECT_OFF_TEXT) = text_addr; | |
1624c38f SG |
510 | |
511 | /* This is a temporary kludge to force data and bss to use the same offsets | |
512 | because that's what nlmconv does now. The real solution requires changes | |
513 | to the stub and remote.c that I don't have time to do right now. */ | |
514 | ||
72bba93b | 515 | ANOFFSET (offs, SECT_OFF_DATA) = data_addr; |
1624c38f | 516 | ANOFFSET (offs, SECT_OFF_BSS) = data_addr; |
72bba93b SG |
517 | |
518 | objfile_relocate (symfile_objfile, offs); | |
519 | } | |
520 | ||
7c622b41 JG |
521 | /* Stub for catch_errors. */ |
522 | ||
523 | static int | |
524 | remote_start_remote (dummy) | |
525 | char *dummy; | |
526 | { | |
ac7a377f JK |
527 | immediate_quit = 1; /* Allow user to interrupt it */ |
528 | ||
7c622b41 | 529 | /* Ack any packet which the remote side has already sent. */ |
72bba93b SG |
530 | SERIAL_WRITE (remote_desc, "+", 1); |
531 | ||
4cc1b3f7 JK |
532 | /* Let the stub know that we want it to return the thread. */ |
533 | set_thread (-1, 0); | |
534 | ||
72bba93b SG |
535 | get_offsets (); /* Get text, data & bss offsets */ |
536 | ||
7c622b41 | 537 | putpkt ("?"); /* initiate a query from remote machine */ |
ac7a377f | 538 | immediate_quit = 0; |
7c622b41 JG |
539 | |
540 | start_remote (); /* Initialize gdb process mechanisms */ | |
541 | return 1; | |
542 | } | |
543 | ||
bd5635a1 RP |
544 | /* Open a connection to a remote debugger. |
545 | NAME is the filename used for communication. */ | |
546 | ||
2b576293 C |
547 | static void |
548 | remote_open (name, from_tty) | |
549 | char *name; | |
550 | int from_tty; | |
551 | { | |
dd0ce8f6 | 552 | remote_open_1 (name, from_tty, &remote_ops, 0); |
2b576293 C |
553 | } |
554 | ||
555 | /* Open a connection to a remote debugger using the extended | |
935e77f5 | 556 | remote gdb protocol. NAME is the filename used for communication. */ |
2b576293 C |
557 | |
558 | static void | |
559 | extended_remote_open (name, from_tty) | |
560 | char *name; | |
561 | int from_tty; | |
562 | { | |
dd0ce8f6 | 563 | remote_open_1 (name, from_tty, &extended_remote_ops, 1/*extended_p*/); |
2b576293 C |
564 | } |
565 | ||
566 | /* Generic code for opening a connection to a remote target. */ | |
d538b510 RP |
567 | static DCACHE *remote_dcache; |
568 | ||
b543979c | 569 | static void |
dd0ce8f6 | 570 | remote_open_1 (name, from_tty, target, extended_p) |
bd5635a1 RP |
571 | char *name; |
572 | int from_tty; | |
2b576293 | 573 | struct target_ops *target; |
dd0ce8f6 | 574 | int extended_p; |
bd5635a1 | 575 | { |
bd5635a1 | 576 | if (name == 0) |
45993f61 | 577 | error ("To open a remote debug connection, you need to specify what serial\n\ |
bd5635a1 RP |
578 | device is attached to the remote system (e.g. /dev/ttya)."); |
579 | ||
f2fc6e7a JK |
580 | target_preopen (from_tty); |
581 | ||
2b576293 | 582 | unpush_target (target); |
bd5635a1 | 583 | |
d538b510 | 584 | remote_dcache = dcache_init (remote_read_bytes, remote_write_bytes); |
bd5635a1 | 585 | |
ebdb9ade JK |
586 | remote_desc = SERIAL_OPEN (name); |
587 | if (!remote_desc) | |
bd5635a1 RP |
588 | perror_with_name (name); |
589 | ||
94d4b713 | 590 | if (baud_rate != -1) |
b543979c | 591 | { |
94d4b713 JK |
592 | if (SERIAL_SETBAUDRATE (remote_desc, baud_rate)) |
593 | { | |
594 | SERIAL_CLOSE (remote_desc); | |
595 | perror_with_name (name); | |
596 | } | |
b543979c | 597 | } |
ebdb9ade | 598 | |
45993f61 | 599 | |
ebdb9ade | 600 | SERIAL_RAW (remote_desc); |
bd5635a1 | 601 | |
e15f2a54 JK |
602 | /* If there is something sitting in the buffer we might take it as a |
603 | response to a command, which would be bad. */ | |
604 | SERIAL_FLUSH_INPUT (remote_desc); | |
605 | ||
bd5635a1 | 606 | if (from_tty) |
7c622b41 JG |
607 | { |
608 | puts_filtered ("Remote debugging using "); | |
609 | puts_filtered (name); | |
610 | puts_filtered ("\n"); | |
611 | } | |
2b576293 | 612 | push_target (target); /* Switch to using remote target now */ |
bd5635a1 | 613 | |
4aa6fe10 JK |
614 | /* Start out by trying the 'P' request to set registers. We set this each |
615 | time that we open a new target so that if the user switches from one | |
616 | stub to another, we can (if the target is closed and reopened) cope. */ | |
617 | stub_supports_P = 1; | |
618 | ||
4cc1b3f7 JK |
619 | general_thread = -2; |
620 | cont_thread = -2; | |
621 | ||
a1e0ba7a SG |
622 | /* Without this, some commands which require an active target (such as kill) |
623 | won't work. This variable serves (at least) double duty as both the pid | |
624 | of the target process (if it has such), and as a flag indicating that a | |
625 | target is active. These functions should be split out into seperate | |
626 | variables, especially since GDB will someday have a notion of debugging | |
627 | several processes. */ | |
628 | ||
4fb7359d | 629 | inferior_pid = 42000; |
4fb7359d SG |
630 | /* Start the remote connection; if error (0), discard this target. |
631 | In particular, if the user quits, be sure to discard it | |
632 | (we'd be in an inconsistent state otherwise). */ | |
633 | if (!catch_errors (remote_start_remote, (char *)0, | |
45993f61 | 634 | "Couldn't establish connection to remote target\n", RETURN_MASK_ALL)) |
4fb7359d | 635 | pop_target(); |
dd0ce8f6 AC |
636 | |
637 | ||
638 | if (extended_p) | |
639 | { | |
640 | /* tell the remote that we're using the extended protocol. */ | |
641 | char buf[PBUFSIZ]; | |
642 | putpkt ("!"); | |
643 | getpkt (buf, 0); | |
644 | } | |
bd5635a1 RP |
645 | } |
646 | ||
b52cac6b FF |
647 | /* This takes a program previously attached to and detaches it. After |
648 | this is done, GDB can be used to debug some other program. We | |
649 | better not have left any breakpoints in the target program or it'll | |
650 | die when it hits one. */ | |
bd5635a1 RP |
651 | |
652 | static void | |
653 | remote_detach (args, from_tty) | |
654 | char *args; | |
655 | int from_tty; | |
656 | { | |
b52cac6b FF |
657 | char buf[PBUFSIZ]; |
658 | ||
bd5635a1 RP |
659 | if (args) |
660 | error ("Argument given to \"detach\" when remotely debugging."); | |
b52cac6b FF |
661 | |
662 | /* Tell the remote target to detach. */ | |
663 | strcpy (buf, "D"); | |
664 | remote_send (buf); | |
665 | ||
bd5635a1 RP |
666 | pop_target (); |
667 | if (from_tty) | |
7c622b41 | 668 | puts_filtered ("Ending remote debugging.\n"); |
bd5635a1 RP |
669 | } |
670 | ||
671 | /* Convert hex digit A to a number. */ | |
672 | ||
dd0ce8f6 | 673 | int |
bd5635a1 RP |
674 | fromhex (a) |
675 | int a; | |
676 | { | |
677 | if (a >= '0' && a <= '9') | |
678 | return a - '0'; | |
679 | else if (a >= 'a' && a <= 'f') | |
680 | return a - 'a' + 10; | |
dd0ce8f6 AC |
681 | else if (a >= 'A' && a <= 'F') |
682 | return a - 'A' + 10; | |
ec10503a | 683 | else |
6c27841f | 684 | error ("Reply contains invalid hex digit %d", a); |
bd5635a1 RP |
685 | } |
686 | ||
687 | /* Convert number NIB to a hex digit. */ | |
688 | ||
689 | static int | |
690 | tohex (nib) | |
691 | int nib; | |
692 | { | |
693 | if (nib < 10) | |
694 | return '0'+nib; | |
695 | else | |
696 | return 'a'+nib-10; | |
697 | } | |
698 | \f | |
699 | /* Tell the remote machine to resume. */ | |
700 | ||
4cc1b3f7 JK |
701 | static enum target_signal last_sent_signal = TARGET_SIGNAL_0; |
702 | int last_sent_step; | |
703 | ||
b543979c | 704 | static void |
d538b510 | 705 | remote_resume (pid, step, siggnal) |
94d4b713 JK |
706 | int pid, step; |
707 | enum target_signal siggnal; | |
bd5635a1 RP |
708 | { |
709 | char buf[PBUFSIZ]; | |
710 | ||
4cc1b3f7 JK |
711 | if (pid == -1) |
712 | set_thread (inferior_pid, 0); | |
713 | else | |
714 | set_thread (pid, 0); | |
bd5635a1 | 715 | |
d538b510 | 716 | dcache_flush (remote_dcache); |
bd5635a1 | 717 | |
4cc1b3f7 JK |
718 | last_sent_signal = siggnal; |
719 | last_sent_step = step; | |
720 | ||
0a325463 SG |
721 | /* A hook for when we need to do something at the last moment before |
722 | resumption. */ | |
723 | if (target_resume_hook) | |
724 | (*target_resume_hook) (); | |
725 | ||
4cc1b3f7 JK |
726 | if (siggnal != TARGET_SIGNAL_0) |
727 | { | |
728 | buf[0] = step ? 'S' : 'C'; | |
729 | buf[1] = tohex (((int)siggnal >> 4) & 0xf); | |
730 | buf[2] = tohex ((int)siggnal & 0xf); | |
731 | buf[3] = '\0'; | |
732 | } | |
733 | else | |
734 | strcpy (buf, step ? "s": "c"); | |
bd5635a1 RP |
735 | |
736 | putpkt (buf); | |
737 | } | |
ebdb9ade | 738 | \f |
b543979c JG |
739 | /* Send ^C to target to halt it. Target will respond, and send us a |
740 | packet. */ | |
741 | ||
5af4f5f6 JK |
742 | static void |
743 | remote_interrupt (signo) | |
e676a15f | 744 | int signo; |
b543979c | 745 | { |
ebdb9ade JK |
746 | /* If this doesn't work, try more severe steps. */ |
747 | signal (signo, remote_interrupt_twice); | |
8f86a4e4 | 748 | |
d0d8484a | 749 | if (remote_debug) |
199b2450 | 750 | printf_unfiltered ("remote_interrupt called\n"); |
8f86a4e4 | 751 | |
b52cac6b FF |
752 | /* Send a break or a ^C, depending on user preference. */ |
753 | if (remote_break) | |
754 | SERIAL_SEND_BREAK (remote_desc); | |
755 | else | |
756 | SERIAL_WRITE (remote_desc, "\003", 1); | |
b543979c JG |
757 | } |
758 | ||
5af4f5f6 JK |
759 | static void (*ofunc)(); |
760 | ||
ebdb9ade JK |
761 | /* The user typed ^C twice. */ |
762 | static void | |
763 | remote_interrupt_twice (signo) | |
764 | int signo; | |
765 | { | |
766 | signal (signo, ofunc); | |
767 | ||
981a3309 SG |
768 | interrupt_query (); |
769 | ||
770 | signal (signo, remote_interrupt); | |
771 | } | |
772 | ||
773 | /* Ask the user what to do when an interrupt is received. */ | |
774 | ||
775 | static void | |
776 | interrupt_query () | |
777 | { | |
ebdb9ade | 778 | target_terminal_ours (); |
981a3309 | 779 | |
6b27ebe8 | 780 | if (query ("Interrupted while waiting for the program.\n\ |
ebdb9ade JK |
781 | Give up (and stop debugging it)? ")) |
782 | { | |
783 | target_mourn_inferior (); | |
e50ebec8 | 784 | return_to_top_level (RETURN_QUIT); |
ebdb9ade | 785 | } |
981a3309 SG |
786 | |
787 | target_terminal_inferior (); | |
ebdb9ade | 788 | } |
b543979c | 789 | |
4cc1b3f7 JK |
790 | /* If nonzero, ignore the next kill. */ |
791 | int kill_kludge; | |
792 | ||
dd0ce8f6 AC |
793 | void |
794 | remote_console_output (msg) | |
795 | char *msg; | |
796 | { | |
797 | char *p; | |
798 | ||
799 | for (p = msg; *p; p +=2) | |
800 | { | |
801 | char tb[2]; | |
802 | char c = fromhex (p[0]) * 16 + fromhex (p[1]); | |
803 | tb[0] = c; | |
804 | tb[1] = 0; | |
805 | if (target_output_hook) | |
806 | target_output_hook (tb); | |
807 | else | |
808 | fputs_filtered (tb, gdb_stdout); | |
809 | } | |
810 | } | |
811 | ||
bd5635a1 | 812 | /* Wait until the remote machine stops, then return, |
e1ce8aa5 JK |
813 | storing status in STATUS just as `wait' would. |
814 | Returns "pid" (though it's not clear what, if anything, that | |
815 | means in the case of this target). */ | |
bd5635a1 | 816 | |
b543979c | 817 | static int |
d0d8484a SG |
818 | remote_wait (pid, status) |
819 | int pid; | |
94d4b713 | 820 | struct target_waitstatus *status; |
bd5635a1 RP |
821 | { |
822 | unsigned char buf[PBUFSIZ]; | |
4cc1b3f7 | 823 | int thread_num = -1; |
8f86a4e4 | 824 | |
94d4b713 JK |
825 | status->kind = TARGET_WAITKIND_EXITED; |
826 | status->value.integer = 0; | |
b543979c | 827 | |
4f8a48e5 | 828 | while (1) |
8f86a4e4 | 829 | { |
4f8a48e5 | 830 | unsigned char *p; |
a03d4f8e | 831 | |
4f8a48e5 ILT |
832 | ofunc = (void (*)()) signal (SIGINT, remote_interrupt); |
833 | getpkt ((char *) buf, 1); | |
834 | signal (SIGINT, ofunc); | |
4ecee2f9 | 835 | |
0a325463 SG |
836 | /* This is a hook for when we need to do something (perhaps the |
837 | collection of trace data) every time the target stops. */ | |
838 | if (target_wait_loop_hook) | |
839 | (*target_wait_loop_hook) (); | |
840 | ||
754e5da2 | 841 | switch (buf[0]) |
8f86a4e4 | 842 | { |
754e5da2 SG |
843 | case 'E': /* Error of some sort */ |
844 | warning ("Remote failure reply: %s", buf); | |
845 | continue; | |
846 | case 'T': /* Status with PC, SP, FP, ... */ | |
847 | { | |
848 | int i; | |
849 | long regno; | |
850 | char regs[MAX_REGISTER_RAW_SIZE]; | |
a03d4f8e | 851 | |
754e5da2 SG |
852 | /* Expedited reply, containing Signal, {regno, reg} repeat */ |
853 | /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where | |
854 | ss = signal number | |
855 | n... = register number | |
856 | r... = register contents | |
857 | */ | |
754e5da2 | 858 | p = &buf[3]; /* after Txx */ |
5af4f5f6 | 859 | |
754e5da2 SG |
860 | while (*p) |
861 | { | |
862 | unsigned char *p1; | |
45993f61 | 863 | char *p_temp; |
5af4f5f6 | 864 | |
2b576293 | 865 | regno = strtol ((const char *) p, &p_temp, 16); /* Read the register number */ |
45993f61 | 866 | p1 = (unsigned char *)p_temp; |
5af4f5f6 | 867 | |
754e5da2 | 868 | if (p1 == p) |
754e5da2 | 869 | { |
2b576293 | 870 | p1 = (unsigned char *) strchr ((const char *) p, ':'); |
4cc1b3f7 JK |
871 | if (p1 == NULL) |
872 | warning ("Malformed packet (missing colon): %s\n\ | |
873 | Packet: '%s'\n", | |
874 | p, buf); | |
2b576293 | 875 | if (strncmp ((const char *) p, "thread", p1 - p) == 0) |
4cc1b3f7 | 876 | { |
2b576293 | 877 | thread_num = strtol ((const char *) ++p1, &p_temp, 16); |
45993f61 | 878 | p = (unsigned char *)p_temp; |
4cc1b3f7 JK |
879 | } |
880 | } | |
881 | else | |
882 | { | |
883 | p = p1; | |
884 | ||
885 | if (*p++ != ':') | |
886 | warning ("Malformed packet (missing colon): %s\n\ | |
887 | Packet: '%s'\n", | |
888 | p, buf); | |
889 | ||
890 | if (regno >= NUM_REGS) | |
2b576293 | 891 | warning ("Remote sent bad register number %ld: %s\n\ |
4cc1b3f7 JK |
892 | Packet: '%s'\n", |
893 | regno, p, buf); | |
894 | ||
895 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i++) | |
896 | { | |
897 | if (p[0] == 0 || p[1] == 0) | |
898 | warning ("Remote reply is too short: %s", buf); | |
0d14c7df | 899 | regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]); |
4cc1b3f7 JK |
900 | p += 2; |
901 | } | |
902 | supply_register (regno, regs); | |
754e5da2 | 903 | } |
4f8a48e5 | 904 | |
754e5da2 SG |
905 | if (*p++ != ';') |
906 | warning ("Remote register badly formatted: %s", buf); | |
754e5da2 SG |
907 | } |
908 | } | |
909 | /* fall through */ | |
910 | case 'S': /* Old style status, just signal only */ | |
911 | status->kind = TARGET_WAITKIND_STOPPED; | |
912 | status->value.sig = (enum target_signal) | |
913 | (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))); | |
4f8a48e5 | 914 | |
4cc1b3f7 | 915 | goto got_status; |
754e5da2 SG |
916 | case 'W': /* Target exited */ |
917 | { | |
918 | /* The remote process exited. */ | |
919 | status->kind = TARGET_WAITKIND_EXITED; | |
920 | status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]); | |
4cc1b3f7 | 921 | goto got_status; |
754e5da2 | 922 | } |
4cc1b3f7 JK |
923 | case 'X': |
924 | status->kind = TARGET_WAITKIND_SIGNALLED; | |
925 | status->value.sig = (enum target_signal) | |
926 | (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))); | |
927 | kill_kludge = 1; | |
928 | ||
929 | goto got_status; | |
754e5da2 | 930 | case 'O': /* Console output */ |
dd0ce8f6 | 931 | remote_console_output (buf + 1); |
754e5da2 | 932 | continue; |
4cc1b3f7 JK |
933 | case '\0': |
934 | if (last_sent_signal != TARGET_SIGNAL_0) | |
935 | { | |
936 | /* Zero length reply means that we tried 'S' or 'C' and | |
937 | the remote system doesn't support it. */ | |
938 | target_terminal_ours_for_output (); | |
939 | printf_filtered | |
940 | ("Can't send signals to this remote system. %s not sent.\n", | |
941 | target_signal_to_name (last_sent_signal)); | |
942 | last_sent_signal = TARGET_SIGNAL_0; | |
943 | target_terminal_inferior (); | |
944 | ||
2b576293 C |
945 | strcpy ((char *) buf, last_sent_step ? "s" : "c"); |
946 | putpkt ((char *) buf); | |
4cc1b3f7 JK |
947 | continue; |
948 | } | |
949 | /* else fallthrough */ | |
754e5da2 SG |
950 | default: |
951 | warning ("Invalid remote reply: %s", buf); | |
952 | continue; | |
4f8a48e5 | 953 | } |
758aeb93 | 954 | } |
4cc1b3f7 JK |
955 | got_status: |
956 | if (thread_num != -1) | |
957 | { | |
958 | /* Initial thread value can only be acquired via wait, so deal with | |
959 | this marker which is used before the first thread value is | |
960 | acquired. */ | |
961 | if (inferior_pid == 42000) | |
962 | { | |
963 | inferior_pid = thread_num; | |
964 | add_thread (inferior_pid); | |
965 | } | |
966 | return thread_num; | |
967 | } | |
968 | return inferior_pid; | |
bd5635a1 RP |
969 | } |
970 | ||
55fea07b JK |
971 | /* Number of bytes of registers this stub implements. */ |
972 | static int register_bytes_found; | |
973 | ||
bd5635a1 | 974 | /* Read the remote registers into the block REGS. */ |
e1ce8aa5 JK |
975 | /* Currently we just read all the registers, so we don't use regno. */ |
976 | /* ARGSUSED */ | |
b543979c | 977 | static void |
bd5635a1 RP |
978 | remote_fetch_registers (regno) |
979 | int regno; | |
980 | { | |
981 | char buf[PBUFSIZ]; | |
982 | int i; | |
983 | char *p; | |
984 | char regs[REGISTER_BYTES]; | |
985 | ||
4cc1b3f7 JK |
986 | set_thread (inferior_pid, 1); |
987 | ||
bd5635a1 RP |
988 | sprintf (buf, "g"); |
989 | remote_send (buf); | |
990 | ||
0a325463 SG |
991 | if (remote_register_buf_size == 0) |
992 | remote_register_buf_size = strlen (buf); | |
993 | ||
55fea07b JK |
994 | /* Unimplemented registers read as all bits zero. */ |
995 | memset (regs, 0, REGISTER_BYTES); | |
996 | ||
981a3309 SG |
997 | /* We can get out of synch in various cases. If the first character |
998 | in the buffer is not a hex character, assume that has happened | |
999 | and try to fetch another packet to read. */ | |
1000 | while ((buf[0] < '0' || buf[0] > '9') | |
1001 | && (buf[0] < 'a' || buf[0] > 'f')) | |
1002 | { | |
d0d8484a | 1003 | if (remote_debug) |
199b2450 | 1004 | printf_unfiltered ("Bad register packet; fetching a new packet\n"); |
981a3309 SG |
1005 | getpkt (buf, 0); |
1006 | } | |
1007 | ||
bd5635a1 RP |
1008 | /* Reply describes registers byte by byte, each byte encoded as two |
1009 | hex characters. Suck them all up, then supply them to the | |
1010 | register cacheing/storage mechanism. */ | |
1011 | ||
1012 | p = buf; | |
1013 | for (i = 0; i < REGISTER_BYTES; i++) | |
1014 | { | |
55fea07b JK |
1015 | if (p[0] == 0) |
1016 | break; | |
1017 | if (p[1] == 0) | |
1018 | { | |
1019 | warning ("Remote reply is of odd length: %s", buf); | |
1020 | /* Don't change register_bytes_found in this case, and don't | |
1021 | print a second warning. */ | |
1022 | goto supply_them; | |
1023 | } | |
bd5635a1 RP |
1024 | regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]); |
1025 | p += 2; | |
1026 | } | |
55fea07b JK |
1027 | |
1028 | if (i != register_bytes_found) | |
1029 | { | |
1030 | register_bytes_found = i; | |
1031 | #ifdef REGISTER_BYTES_OK | |
1032 | if (!REGISTER_BYTES_OK (i)) | |
1033 | warning ("Remote reply is too short: %s", buf); | |
1034 | #endif | |
1035 | } | |
1036 | ||
1037 | supply_them: | |
bd5635a1 RP |
1038 | for (i = 0; i < NUM_REGS; i++) |
1039 | supply_register (i, ®s[REGISTER_BYTE(i)]); | |
bd5635a1 RP |
1040 | } |
1041 | ||
4aa6fe10 JK |
1042 | /* Prepare to store registers. Since we may send them all (using a |
1043 | 'G' request), we have to read out the ones we don't want to change | |
1044 | first. */ | |
bd5635a1 | 1045 | |
b543979c | 1046 | static void |
bd5635a1 RP |
1047 | remote_prepare_to_store () |
1048 | { | |
34517ebc JG |
1049 | /* Make sure the entire registers array is valid. */ |
1050 | read_register_bytes (0, (char *)NULL, REGISTER_BYTES); | |
bd5635a1 RP |
1051 | } |
1052 | ||
4aa6fe10 JK |
1053 | /* Store register REGNO, or all registers if REGNO == -1, from the contents |
1054 | of REGISTERS. FIXME: ignores errors. */ | |
bd5635a1 | 1055 | |
b543979c | 1056 | static void |
bd5635a1 RP |
1057 | remote_store_registers (regno) |
1058 | int regno; | |
1059 | { | |
1060 | char buf[PBUFSIZ]; | |
1061 | int i; | |
1062 | char *p; | |
1063 | ||
4cc1b3f7 JK |
1064 | set_thread (inferior_pid, 1); |
1065 | ||
4aa6fe10 JK |
1066 | if (regno >= 0 && stub_supports_P) |
1067 | { | |
1068 | /* Try storing a single register. */ | |
1069 | char *regp; | |
1070 | ||
0c993550 | 1071 | sprintf (buf, "P%x=", regno); |
4aa6fe10 JK |
1072 | p = buf + strlen (buf); |
1073 | regp = ®isters[REGISTER_BYTE (regno)]; | |
1074 | for (i = 0; i < REGISTER_RAW_SIZE (regno); ++i) | |
1075 | { | |
1076 | *p++ = tohex ((regp[i] >> 4) & 0xf); | |
1077 | *p++ = tohex (regp[i] & 0xf); | |
1078 | } | |
1079 | *p = '\0'; | |
1080 | remote_send (buf); | |
1081 | if (buf[0] != '\0') | |
1082 | { | |
1083 | /* The stub understands the 'P' request. We are done. */ | |
1084 | return; | |
1085 | } | |
1086 | ||
1087 | /* The stub does not support the 'P' request. Use 'G' instead, | |
1088 | and don't try using 'P' in the future (it will just waste our | |
1089 | time). */ | |
1090 | stub_supports_P = 0; | |
1091 | } | |
1092 | ||
bd5635a1 | 1093 | buf[0] = 'G'; |
4aa6fe10 | 1094 | |
bd5635a1 RP |
1095 | /* Command describes registers byte by byte, |
1096 | each byte encoded as two hex characters. */ | |
1097 | ||
1098 | p = buf + 1; | |
55fea07b JK |
1099 | /* remote_prepare_to_store insures that register_bytes_found gets set. */ |
1100 | for (i = 0; i < register_bytes_found; i++) | |
bd5635a1 RP |
1101 | { |
1102 | *p++ = tohex ((registers[i] >> 4) & 0xf); | |
1103 | *p++ = tohex (registers[i] & 0xf); | |
1104 | } | |
1105 | *p = '\0'; | |
1106 | ||
1107 | remote_send (buf); | |
bd5635a1 RP |
1108 | } |
1109 | ||
45993f61 SC |
1110 | /* |
1111 | Use of the data cache *used* to be disabled because it loses for looking at | |
b43e0347 | 1112 | and changing hardware I/O ports and the like. Accepting `volatile' |
45993f61 SC |
1113 | would perhaps be one way to fix it. Another idea would be to use the |
1114 | executable file for the text segment (for all SEC_CODE sections? | |
1115 | For all SEC_READONLY sections?). This has problems if you want to | |
1116 | actually see what the memory contains (e.g. self-modifying code, | |
1117 | clobbered memory, user downloaded the wrong thing). | |
1118 | ||
1119 | Because it speeds so much up, it's now enabled, if you're playing | |
1120 | with registers you turn it of (set remotecache 0) | |
1121 | */ | |
b43e0347 | 1122 | |
bd5635a1 RP |
1123 | /* Read a word from remote address ADDR and return it. |
1124 | This goes through the data cache. */ | |
1125 | ||
2b576293 | 1126 | #if 0 /* unused? */ |
b43e0347 | 1127 | static int |
bd5635a1 RP |
1128 | remote_fetch_word (addr) |
1129 | CORE_ADDR addr; | |
1130 | { | |
d538b510 | 1131 | return dcache_fetch (remote_dcache, addr); |
bd5635a1 RP |
1132 | } |
1133 | ||
1134 | /* Write a word WORD into remote address ADDR. | |
1135 | This goes through the data cache. */ | |
1136 | ||
b43e0347 | 1137 | static void |
bd5635a1 RP |
1138 | remote_store_word (addr, word) |
1139 | CORE_ADDR addr; | |
1140 | int word; | |
1141 | { | |
d538b510 | 1142 | dcache_poke (remote_dcache, addr, word); |
bd5635a1 | 1143 | } |
2b576293 | 1144 | #endif /* 0 (unused?) */ |
45993f61 | 1145 | |
bd5635a1 | 1146 | \f |
0a325463 SG |
1147 | |
1148 | /* Return the number of hex digits in num. */ | |
1149 | ||
1150 | static int | |
1151 | hexnumlen (num) | |
1152 | ULONGEST num; | |
1153 | { | |
1154 | int i; | |
1155 | ||
1156 | for (i = 0; num != 0; i++) | |
1157 | num >>= 4; | |
1158 | ||
dd0ce8f6 | 1159 | return max (i, 1); |
0a325463 SG |
1160 | } |
1161 | ||
bd5635a1 RP |
1162 | /* Write memory data directly to the remote machine. |
1163 | This does not inform the data cache; the data cache uses this. | |
1164 | MEMADDR is the address in the remote memory space. | |
1165 | MYADDR is the address of the buffer in our space. | |
d538b510 | 1166 | LEN is the number of bytes. |
bd5635a1 | 1167 | |
d538b510 RP |
1168 | Returns number of bytes transferred, or 0 for error. */ |
1169 | ||
1170 | static int | |
bd5635a1 RP |
1171 | remote_write_bytes (memaddr, myaddr, len) |
1172 | CORE_ADDR memaddr; | |
43fc25c8 | 1173 | char *myaddr; |
bd5635a1 RP |
1174 | int len; |
1175 | { | |
0a325463 SG |
1176 | int max_buf_size; /* Max size of packet output buffer */ |
1177 | int origlen; | |
1178 | ||
ec10503a | 1179 | /* Chop the transfer down if necessary */ |
bd5635a1 | 1180 | |
0a325463 | 1181 | max_buf_size = min (remote_write_size, PBUFSIZ); |
dd0ce8f6 AC |
1182 | if (remote_register_buf_size != 0) |
1183 | max_buf_size = min (max_buf_size, remote_register_buf_size); | |
0a325463 | 1184 | |
dd0ce8f6 AC |
1185 | /* Subtract header overhead from max payload size - $M<memaddr>,<len>:#nn */ |
1186 | max_buf_size -= 2 + hexnumlen (memaddr + len - 1) + 1 + hexnumlen (len) + 4; | |
0a325463 SG |
1187 | |
1188 | origlen = len; | |
1189 | while (len > 0) | |
ec10503a | 1190 | { |
0a325463 SG |
1191 | char buf[PBUFSIZ]; |
1192 | char *p; | |
1193 | int todo; | |
1194 | int i; | |
fea17b55 | 1195 | |
0a325463 | 1196 | todo = min (len, max_buf_size / 2); /* num bytes that will fit */ |
bd5635a1 | 1197 | |
ec10503a SC |
1198 | /* FIXME-32x64: Need a version of print_address_numeric which puts the |
1199 | result in a buffer like sprintf. */ | |
0a325463 | 1200 | sprintf (buf, "M%lx,%x:", (unsigned long) memaddr, todo); |
bd5635a1 | 1201 | |
ec10503a SC |
1202 | /* We send target system values byte by byte, in increasing byte addresses, |
1203 | each byte encoded as two hex characters. */ | |
bd5635a1 | 1204 | |
ec10503a SC |
1205 | p = buf + strlen (buf); |
1206 | for (i = 0; i < todo; i++) | |
1207 | { | |
0a325463 SG |
1208 | *p++ = tohex ((myaddr[i] >> 4) & 0xf); |
1209 | *p++ = tohex (myaddr[i] & 0xf); | |
ec10503a SC |
1210 | } |
1211 | *p = '\0'; | |
d538b510 | 1212 | |
ec10503a SC |
1213 | putpkt (buf); |
1214 | getpkt (buf, 0); | |
1215 | ||
1216 | if (buf[0] == 'E') | |
1217 | { | |
1218 | /* There is no correspondance between what the remote protocol uses | |
1219 | for errors and errno codes. We would like a cleaner way of | |
1220 | representing errors (big enough to include errno codes, bfd_error | |
1221 | codes, and others). But for now just return EIO. */ | |
1222 | errno = EIO; | |
1223 | return 0; | |
1224 | } | |
0a325463 SG |
1225 | myaddr += todo; |
1226 | memaddr += todo; | |
1227 | len -= todo; | |
d538b510 | 1228 | } |
0a325463 | 1229 | return origlen; |
bd5635a1 RP |
1230 | } |
1231 | ||
1232 | /* Read memory data directly from the remote machine. | |
1233 | This does not use the data cache; the data cache uses this. | |
1234 | MEMADDR is the address in the remote memory space. | |
1235 | MYADDR is the address of the buffer in our space. | |
d538b510 | 1236 | LEN is the number of bytes. |
bd5635a1 | 1237 | |
d538b510 RP |
1238 | Returns number of bytes transferred, or 0 for error. */ |
1239 | ||
1240 | static int | |
bd5635a1 RP |
1241 | remote_read_bytes (memaddr, myaddr, len) |
1242 | CORE_ADDR memaddr; | |
43fc25c8 | 1243 | char *myaddr; |
bd5635a1 RP |
1244 | int len; |
1245 | { | |
0a325463 SG |
1246 | int max_buf_size; /* Max size of packet output buffer */ |
1247 | int origlen; | |
bd5635a1 | 1248 | |
0a325463 SG |
1249 | /* Chop the transfer down if necessary */ |
1250 | ||
1251 | max_buf_size = min (remote_write_size, PBUFSIZ); | |
dd0ce8f6 AC |
1252 | if (remote_register_buf_size != 0) |
1253 | max_buf_size = min (max_buf_size, remote_register_buf_size); | |
0a325463 SG |
1254 | |
1255 | origlen = len; | |
1256 | while (len > 0) | |
0d14c7df | 1257 | { |
0a325463 SG |
1258 | char buf[PBUFSIZ]; |
1259 | char *p; | |
1260 | int todo; | |
1261 | int i; | |
1262 | ||
1263 | todo = min (len, max_buf_size / 2); /* num bytes that will fit */ | |
bd5635a1 | 1264 | |
0d14c7df FF |
1265 | /* FIXME-32x64: Need a version of print_address_numeric which puts the |
1266 | result in a buffer like sprintf. */ | |
0a325463 | 1267 | sprintf (buf, "m%lx,%x", (unsigned long) memaddr, todo); |
0d14c7df FF |
1268 | putpkt (buf); |
1269 | getpkt (buf, 0); | |
d538b510 | 1270 | |
0d14c7df FF |
1271 | if (buf[0] == 'E') |
1272 | { | |
1273 | /* There is no correspondance between what the remote protocol uses | |
1274 | for errors and errno codes. We would like a cleaner way of | |
1275 | representing errors (big enough to include errno codes, bfd_error | |
1276 | codes, and others). But for now just return EIO. */ | |
1277 | errno = EIO; | |
1278 | return 0; | |
1279 | } | |
bd5635a1 | 1280 | |
b543979c | 1281 | /* Reply describes memory byte by byte, |
bd5635a1 RP |
1282 | each byte encoded as two hex characters. */ |
1283 | ||
0d14c7df FF |
1284 | p = buf; |
1285 | for (i = 0; i < todo; i++) | |
1286 | { | |
1287 | if (p[0] == 0 || p[1] == 0) | |
1288 | /* Reply is short. This means that we were able to read only part | |
1289 | of what we wanted to. */ | |
0a325463 SG |
1290 | return i + (origlen - len); |
1291 | myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]); | |
0d14c7df FF |
1292 | p += 2; |
1293 | } | |
0a325463 SG |
1294 | myaddr += todo; |
1295 | memaddr += todo; | |
1296 | len -= todo; | |
bd5635a1 | 1297 | } |
0a325463 | 1298 | return origlen; |
bd5635a1 RP |
1299 | } |
1300 | \f | |
1301 | /* Read or write LEN bytes from inferior memory at MEMADDR, transferring | |
e1ce8aa5 | 1302 | to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is |
bd5635a1 RP |
1303 | nonzero. Returns length of data written or read; 0 for error. */ |
1304 | ||
b543979c JG |
1305 | /* ARGSUSED */ |
1306 | static int | |
1307 | remote_xfer_memory(memaddr, myaddr, len, should_write, target) | |
bd5635a1 RP |
1308 | CORE_ADDR memaddr; |
1309 | char *myaddr; | |
1310 | int len; | |
e1ce8aa5 | 1311 | int should_write; |
b543979c | 1312 | struct target_ops *target; /* ignored */ |
bd5635a1 | 1313 | { |
45993f61 | 1314 | return dcache_xfer_memory (remote_dcache, memaddr, myaddr, len, should_write); |
bd5635a1 RP |
1315 | } |
1316 | ||
45993f61 | 1317 | |
94d4b713 JK |
1318 | #if 0 |
1319 | /* Enable after 4.12. */ | |
1320 | ||
1321 | void | |
1322 | remote_search (len, data, mask, startaddr, increment, lorange, hirange | |
1323 | addr_found, data_found) | |
1324 | int len; | |
1325 | char *data; | |
1326 | char *mask; | |
1327 | CORE_ADDR startaddr; | |
1328 | int increment; | |
1329 | CORE_ADDR lorange; | |
1330 | CORE_ADDR hirange; | |
1331 | CORE_ADDR *addr_found; | |
1332 | char *data_found; | |
1333 | { | |
1334 | if (increment == -4 && len == 4) | |
1335 | { | |
1336 | long mask_long, data_long; | |
1337 | long data_found_long; | |
1338 | CORE_ADDR addr_we_found; | |
1339 | char buf[PBUFSIZ]; | |
1340 | long returned_long[2]; | |
1341 | char *p; | |
1342 | ||
1343 | mask_long = extract_unsigned_integer (mask, len); | |
1344 | data_long = extract_unsigned_integer (data, len); | |
1345 | sprintf (buf, "t%x:%x,%x", startaddr, data_long, mask_long); | |
1346 | putpkt (buf); | |
1347 | getpkt (buf, 0); | |
1348 | if (buf[0] == '\0') | |
1349 | { | |
1350 | /* The stub doesn't support the 't' request. We might want to | |
1351 | remember this fact, but on the other hand the stub could be | |
1352 | switched on us. Maybe we should remember it only until | |
1353 | the next "target remote". */ | |
1354 | generic_search (len, data, mask, startaddr, increment, lorange, | |
1355 | hirange, addr_found, data_found); | |
1356 | return; | |
1357 | } | |
1358 | ||
1359 | if (buf[0] == 'E') | |
1360 | /* There is no correspondance between what the remote protocol uses | |
1361 | for errors and errno codes. We would like a cleaner way of | |
1362 | representing errors (big enough to include errno codes, bfd_error | |
1363 | codes, and others). But for now just use EIO. */ | |
1364 | memory_error (EIO, startaddr); | |
1365 | p = buf; | |
1366 | addr_we_found = 0; | |
1367 | while (*p != '\0' && *p != ',') | |
1368 | addr_we_found = (addr_we_found << 4) + fromhex (*p++); | |
1369 | if (*p == '\0') | |
1370 | error ("Protocol error: short return for search"); | |
1371 | ||
1372 | data_found_long = 0; | |
1373 | while (*p != '\0' && *p != ',') | |
1374 | data_found_long = (data_found_long << 4) + fromhex (*p++); | |
1375 | /* Ignore anything after this comma, for future extensions. */ | |
1376 | ||
1377 | if (addr_we_found < lorange || addr_we_found >= hirange) | |
1378 | { | |
1379 | *addr_found = 0; | |
1380 | return; | |
1381 | } | |
1382 | ||
1383 | *addr_found = addr_we_found; | |
1384 | *data_found = store_unsigned_integer (data_we_found, len); | |
1385 | return; | |
1386 | } | |
1387 | generic_search (len, data, mask, startaddr, increment, lorange, | |
1388 | hirange, addr_found, data_found); | |
1389 | } | |
1390 | #endif /* 0 */ | |
1391 | \f | |
b543979c | 1392 | static void |
8f86a4e4 | 1393 | remote_files_info (ignore) |
5af4f5f6 | 1394 | struct target_ops *ignore; |
bd5635a1 | 1395 | { |
7c622b41 | 1396 | puts_filtered ("Debugging a target over a serial line.\n"); |
bd5635a1 RP |
1397 | } |
1398 | \f | |
e50ebec8 JK |
1399 | /* Stuff for dealing with the packets which are part of this protocol. |
1400 | See comment at top of file for details. */ | |
bd5635a1 | 1401 | |
ebdb9ade | 1402 | /* Read a single character from the remote end, masking it down to 7 bits. */ |
b543979c | 1403 | |
bd5635a1 | 1404 | static int |
754e5da2 SG |
1405 | readchar (timeout) |
1406 | int timeout; | |
bd5635a1 | 1407 | { |
ebdb9ade | 1408 | int ch; |
bd5635a1 | 1409 | |
ebdb9ade | 1410 | ch = SERIAL_READCHAR (remote_desc, timeout); |
fce7f2d9 | 1411 | |
754e5da2 SG |
1412 | switch (ch) |
1413 | { | |
1414 | case SERIAL_EOF: | |
1415 | error ("Remote connection closed"); | |
1416 | case SERIAL_ERROR: | |
1417 | perror_with_name ("Remote communication error"); | |
1418 | case SERIAL_TIMEOUT: | |
1419 | return ch; | |
1420 | default: | |
1421 | return ch & 0x7f; | |
1422 | } | |
bd5635a1 RP |
1423 | } |
1424 | ||
1425 | /* Send the command in BUF to the remote machine, | |
1426 | and read the reply into BUF. | |
1427 | Report an error if we get an error reply. */ | |
1428 | ||
1429 | static void | |
1430 | remote_send (buf) | |
1431 | char *buf; | |
1432 | { | |
bd5635a1 | 1433 | putpkt (buf); |
7c622b41 | 1434 | getpkt (buf, 0); |
bd5635a1 RP |
1435 | |
1436 | if (buf[0] == 'E') | |
1437 | error ("Remote failure reply: %s", buf); | |
1438 | } | |
1439 | ||
1440 | /* Send a packet to the remote machine, with error checking. | |
1441 | The data of the packet is in BUF. */ | |
1442 | ||
dd0ce8f6 | 1443 | int |
bd5635a1 RP |
1444 | putpkt (buf) |
1445 | char *buf; | |
1446 | { | |
1447 | int i; | |
1448 | unsigned char csum = 0; | |
b543979c | 1449 | char buf2[PBUFSIZ]; |
bd5635a1 | 1450 | int cnt = strlen (buf); |
ebdb9ade | 1451 | int ch; |
45993f61 | 1452 | int tcount = 0; |
bd5635a1 RP |
1453 | char *p; |
1454 | ||
1455 | /* Copy the packet into buffer BUF2, encapsulating it | |
1456 | and giving it a checksum. */ | |
1457 | ||
b52cac6b | 1458 | if (cnt > (int) sizeof (buf2) - 5) /* Prosanity check */ |
b543979c JG |
1459 | abort(); |
1460 | ||
bd5635a1 RP |
1461 | p = buf2; |
1462 | *p++ = '$'; | |
1463 | ||
1464 | for (i = 0; i < cnt; i++) | |
1465 | { | |
1466 | csum += buf[i]; | |
1467 | *p++ = buf[i]; | |
1468 | } | |
1469 | *p++ = '#'; | |
1470 | *p++ = tohex ((csum >> 4) & 0xf); | |
1471 | *p++ = tohex (csum & 0xf); | |
1472 | ||
1473 | /* Send it over and over until we get a positive ack. */ | |
1474 | ||
6b27ebe8 JK |
1475 | while (1) |
1476 | { | |
1624c38f SG |
1477 | int started_error_output = 0; |
1478 | ||
d0d8484a | 1479 | if (remote_debug) |
6b27ebe8 JK |
1480 | { |
1481 | *p = '\0'; | |
1624c38f SG |
1482 | printf_unfiltered ("Sending packet: %s...", buf2); |
1483 | gdb_flush(gdb_stdout); | |
6b27ebe8 JK |
1484 | } |
1485 | if (SERIAL_WRITE (remote_desc, buf2, p - buf2)) | |
1486 | perror_with_name ("putpkt: write failed"); | |
1487 | ||
1488 | /* read until either a timeout occurs (-2) or '+' is read */ | |
1489 | while (1) | |
1490 | { | |
754e5da2 | 1491 | ch = readchar (remote_timeout); |
6b27ebe8 | 1492 | |
45993f61 | 1493 | if (remote_debug) |
1624c38f SG |
1494 | { |
1495 | switch (ch) | |
1496 | { | |
1497 | case '+': | |
1498 | case SERIAL_TIMEOUT: | |
1624c38f SG |
1499 | case '$': |
1500 | if (started_error_output) | |
1501 | { | |
45993f61 | 1502 | putchar_unfiltered ('\n'); |
1624c38f SG |
1503 | started_error_output = 0; |
1504 | } | |
1505 | } | |
1506 | } | |
1507 | ||
6b27ebe8 JK |
1508 | switch (ch) |
1509 | { | |
1510 | case '+': | |
d0d8484a | 1511 | if (remote_debug) |
199b2450 | 1512 | printf_unfiltered("Ack\n"); |
4cc1b3f7 | 1513 | return 1; |
6b27ebe8 | 1514 | case SERIAL_TIMEOUT: |
45993f61 SC |
1515 | tcount ++; |
1516 | if (tcount > 3) | |
1517 | return 0; | |
6b27ebe8 | 1518 | break; /* Retransmit buffer */ |
1624c38f SG |
1519 | case '$': |
1520 | { | |
6c27841f | 1521 | char junkbuf[PBUFSIZ]; |
1624c38f SG |
1522 | |
1523 | /* It's probably an old response, and we're out of sync. Just | |
1524 | gobble up the packet and ignore it. */ | |
1525 | getpkt (junkbuf, 0); | |
1526 | continue; /* Now, go look for + */ | |
1527 | } | |
6b27ebe8 | 1528 | default: |
d0d8484a | 1529 | if (remote_debug) |
1624c38f SG |
1530 | { |
1531 | if (!started_error_output) | |
1532 | { | |
1533 | started_error_output = 1; | |
1534 | printf_unfiltered ("putpkt: Junk: "); | |
1535 | } | |
45993f61 | 1536 | putchar_unfiltered (ch & 0177); |
1624c38f | 1537 | } |
6b27ebe8 JK |
1538 | continue; |
1539 | } | |
1540 | break; /* Here to retransmit */ | |
1541 | } | |
981a3309 | 1542 | |
94d4b713 JK |
1543 | #if 0 |
1544 | /* This is wrong. If doing a long backtrace, the user should be | |
1545 | able to get out next time we call QUIT, without anything as violent | |
1546 | as interrupt_query. If we want to provide a way out of here | |
1547 | without getting to the next QUIT, it should be based on hitting | |
1548 | ^C twice as in remote_wait. */ | |
981a3309 SG |
1549 | if (quit_flag) |
1550 | { | |
1551 | quit_flag = 0; | |
1552 | interrupt_query (); | |
1553 | } | |
94d4b713 | 1554 | #endif |
6b27ebe8 | 1555 | } |
bd5635a1 RP |
1556 | } |
1557 | ||
754e5da2 SG |
1558 | /* Come here after finding the start of the frame. Collect the rest into BUF, |
1559 | verifying the checksum, length, and handling run-length compression. | |
1560 | Returns 0 on any error, 1 on success. */ | |
1561 | ||
1562 | static int | |
1563 | read_frame (buf) | |
1564 | char *buf; | |
1565 | { | |
1566 | unsigned char csum; | |
1567 | char *bp; | |
1568 | int c; | |
1569 | ||
1570 | csum = 0; | |
1571 | bp = buf; | |
1572 | ||
1573 | while (1) | |
1574 | { | |
1575 | c = readchar (remote_timeout); | |
1576 | ||
1577 | switch (c) | |
1578 | { | |
1579 | case SERIAL_TIMEOUT: | |
1580 | if (remote_debug) | |
1581 | puts_filtered ("Timeout in mid-packet, retrying\n"); | |
1582 | return 0; | |
1583 | case '$': | |
1584 | if (remote_debug) | |
1585 | puts_filtered ("Saw new packet start in middle of old one\n"); | |
1586 | return 0; /* Start a new packet, count retries */ | |
1587 | case '#': | |
1588 | { | |
1589 | unsigned char pktcsum; | |
1590 | ||
1591 | *bp = '\000'; | |
1592 | ||
205fc02b SC |
1593 | pktcsum = fromhex (readchar (remote_timeout)) << 4; |
1594 | pktcsum |= fromhex (readchar (remote_timeout)); | |
754e5da2 SG |
1595 | |
1596 | if (csum == pktcsum) | |
1597 | return 1; | |
1598 | ||
6c27841f SG |
1599 | if (remote_debug) |
1600 | { | |
1601 | printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=", | |
1602 | pktcsum, csum); | |
1603 | puts_filtered (buf); | |
1604 | puts_filtered ("\n"); | |
1605 | } | |
754e5da2 SG |
1606 | return 0; |
1607 | } | |
1608 | case '*': /* Run length encoding */ | |
284f4ee9 | 1609 | csum += c; |
754e5da2 SG |
1610 | c = readchar (remote_timeout); |
1611 | csum += c; | |
1612 | c = c - ' ' + 3; /* Compute repeat count */ | |
1613 | ||
6c27841f SG |
1614 | |
1615 | if (c > 0 && c < 255 && bp + c - 1 < buf + PBUFSIZ - 1) | |
754e5da2 SG |
1616 | { |
1617 | memset (bp, *(bp - 1), c); | |
1618 | bp += c; | |
1619 | continue; | |
1620 | } | |
1621 | ||
1622 | *bp = '\0'; | |
1623 | printf_filtered ("Repeat count %d too large for buffer: ", c); | |
1624 | puts_filtered (buf); | |
1625 | puts_filtered ("\n"); | |
754e5da2 | 1626 | return 0; |
284f4ee9 | 1627 | |
754e5da2 SG |
1628 | default: |
1629 | if (bp < buf + PBUFSIZ - 1) | |
1630 | { | |
1631 | *bp++ = c; | |
1632 | csum += c; | |
1633 | continue; | |
1634 | } | |
1635 | ||
1636 | *bp = '\0'; | |
1637 | puts_filtered ("Remote packet too long: "); | |
1638 | puts_filtered (buf); | |
1639 | puts_filtered ("\n"); | |
1640 | ||
1641 | return 0; | |
1642 | } | |
1643 | } | |
1644 | } | |
1645 | ||
bd5635a1 | 1646 | /* Read a packet from the remote machine, with error checking, |
7c622b41 JG |
1647 | and store it in BUF. BUF is expected to be of size PBUFSIZ. |
1648 | If FOREVER, wait forever rather than timing out; this is used | |
1649 | while the target is executing user code. */ | |
bd5635a1 | 1650 | |
dd0ce8f6 | 1651 | void |
754e5da2 SG |
1652 | getpkt (buf, forever) |
1653 | char *buf; | |
ebdb9ade | 1654 | int forever; |
bd5635a1 | 1655 | { |
754e5da2 SG |
1656 | int c; |
1657 | int tries; | |
1658 | int timeout; | |
1659 | int val; | |
94d4b713 | 1660 | |
45993f61 SC |
1661 | strcpy (buf,"timeout"); |
1662 | ||
754e5da2 | 1663 | if (forever) |
45993f61 SC |
1664 | { |
1665 | #ifdef MAINTENANCE_CMDS | |
1666 | timeout = watchdog > 0 ? watchdog : -1; | |
1667 | #else | |
1668 | timeout = -1; | |
1669 | #endif | |
1670 | } | |
1671 | ||
754e5da2 SG |
1672 | else |
1673 | timeout = remote_timeout; | |
bd5635a1 | 1674 | |
45993f61 | 1675 | #define MAX_TRIES 3 |
981a3309 | 1676 | |
754e5da2 SG |
1677 | for (tries = 1; tries <= MAX_TRIES; tries++) |
1678 | { | |
7c622b41 JG |
1679 | /* This can loop forever if the remote side sends us characters |
1680 | continuously, but if it pauses, we'll get a zero from readchar | |
1681 | because of timeout. Then we'll count that as a retry. */ | |
6b27ebe8 | 1682 | |
754e5da2 SG |
1683 | /* Note that we will only wait forever prior to the start of a packet. |
1684 | After that, we expect characters to arrive at a brisk pace. They | |
1685 | should show up within remote_timeout intervals. */ | |
6b27ebe8 | 1686 | |
754e5da2 | 1687 | do |
6b27ebe8 | 1688 | { |
754e5da2 | 1689 | c = readchar (timeout); |
7c622b41 | 1690 | |
ebdb9ade | 1691 | if (c == SERIAL_TIMEOUT) |
7c622b41 | 1692 | { |
45993f61 SC |
1693 | #ifdef MAINTENANCE_CMDS |
1694 | if (forever) /* Watchdog went off. Kill the target. */ | |
1695 | { | |
1696 | target_mourn_inferior (); | |
1697 | error ("Watchdog has expired. Target detached.\n"); | |
1698 | } | |
1699 | #endif | |
d0d8484a | 1700 | if (remote_debug) |
754e5da2 SG |
1701 | puts_filtered ("Timed out.\n"); |
1702 | goto retry; | |
7c622b41 | 1703 | } |
bd5635a1 | 1704 | } |
754e5da2 | 1705 | while (c != '$'); |
bd5635a1 | 1706 | |
754e5da2 | 1707 | /* We've found the start of a packet, now collect the data. */ |
38094c60 | 1708 | |
754e5da2 SG |
1709 | val = read_frame (buf); |
1710 | ||
1711 | if (val == 1) | |
38094c60 | 1712 | { |
754e5da2 | 1713 | if (remote_debug) |
dd0ce8f6 | 1714 | fprintf_unfiltered (gdb_stdout, "Packet received: %s\n", buf); |
754e5da2 SG |
1715 | SERIAL_WRITE (remote_desc, "+", 1); |
1716 | return; | |
38094c60 | 1717 | } |
754e5da2 SG |
1718 | |
1719 | /* Try the whole thing again. */ | |
45993f61 | 1720 | retry: |
754e5da2 | 1721 | SERIAL_WRITE (remote_desc, "-", 1); |
bd5635a1 RP |
1722 | } |
1723 | ||
754e5da2 | 1724 | /* We have tried hard enough, and just can't receive the packet. Give up. */ |
7c622b41 | 1725 | |
754e5da2 | 1726 | printf_unfiltered ("Ignoring packet error, continuing...\n"); |
ebdb9ade | 1727 | SERIAL_WRITE (remote_desc, "+", 1); |
bd5635a1 RP |
1728 | } |
1729 | \f | |
ebdb9ade JK |
1730 | static void |
1731 | remote_kill () | |
1732 | { | |
4cc1b3f7 JK |
1733 | /* For some mysterious reason, wait_for_inferior calls kill instead of |
1734 | mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */ | |
1735 | if (kill_kludge) | |
1736 | { | |
1737 | kill_kludge = 0; | |
1738 | target_mourn_inferior (); | |
1739 | return; | |
1740 | } | |
1741 | ||
1742 | /* Use catch_errors so the user can quit from gdb even when we aren't on | |
1743 | speaking terms with the remote system. */ | |
1744 | catch_errors (putpkt, "k", "", RETURN_MASK_ERROR); | |
1745 | ||
ebdb9ade JK |
1746 | /* Don't wait for it to die. I'm not really sure it matters whether |
1747 | we do or not. For the existing stubs, kill is a noop. */ | |
1748 | target_mourn_inferior (); | |
1749 | } | |
bd5635a1 | 1750 | |
ebdb9ade JK |
1751 | static void |
1752 | remote_mourn () | |
1753 | { | |
2b576293 C |
1754 | remote_mourn_1 (&remote_ops); |
1755 | } | |
1756 | ||
1757 | static void | |
1758 | extended_remote_mourn () | |
1759 | { | |
1760 | /* We do _not_ want to mourn the target like this; this will | |
1761 | remove the extended remote target from the target stack, | |
1762 | and the next time the user says "run" it'll fail. | |
1763 | ||
1764 | FIXME: What is the right thing to do here? */ | |
1765 | #if 0 | |
1766 | remote_mourn_1 (&extended_remote_ops); | |
1767 | #endif | |
1768 | } | |
1769 | ||
1770 | /* Worker function for remote_mourn. */ | |
1771 | static void | |
1772 | remote_mourn_1 (target) | |
1773 | struct target_ops *target; | |
1774 | { | |
1775 | unpush_target (target); | |
ebdb9ade JK |
1776 | generic_mourn_inferior (); |
1777 | } | |
2b576293 C |
1778 | |
1779 | /* In the extended protocol we want to be able to do things like | |
1780 | "run" and have them basically work as expected. So we need | |
1781 | a special create_inferior function. | |
1782 | ||
1783 | FIXME: One day add support for changing the exec file | |
1784 | we're debugging, arguments and an environment. */ | |
1785 | ||
1786 | static void | |
1787 | extended_remote_create_inferior (exec_file, args, env) | |
1788 | char *exec_file; | |
1789 | char *args; | |
1790 | char **env; | |
1791 | { | |
1792 | /* Rip out the breakpoints; we'll reinsert them after restarting | |
1793 | the remote server. */ | |
1794 | remove_breakpoints (); | |
1795 | ||
1796 | /* Now restart the remote server. */ | |
1797 | extended_remote_restart (); | |
1798 | ||
1799 | /* Now put the breakpoints back in. This way we're safe if the | |
1800 | restart function works via a unix fork on the remote side. */ | |
1801 | insert_breakpoints (); | |
1802 | ||
1803 | /* Clean up from the last time we were running. */ | |
1804 | clear_proceed_status (); | |
1805 | ||
1806 | /* Let the remote process run. */ | |
1807 | proceed (-1, TARGET_SIGNAL_0, 0); | |
1808 | } | |
1809 | ||
ebdb9ade | 1810 | \f |
5af4f5f6 | 1811 | /* On some machines, e.g. 68k, we may use a different breakpoint instruction |
fea17b55 SS |
1812 | than other targets; in those use REMOTE_BREAKPOINT instead of just |
1813 | BREAKPOINT. Also, bi-endian targets may define LITTLE_REMOTE_BREAKPOINT | |
1814 | and BIG_REMOTE_BREAKPOINT. If none of these are defined, we just call | |
1815 | the standard routines that are in mem-break.c. */ | |
1816 | ||
1817 | /* FIXME, these ought to be done in a more dynamic fashion. For instance, | |
1818 | the choice of breakpoint instruction affects target program design and | |
1819 | vice versa, and by making it user-tweakable, the special code here | |
1820 | goes away and we need fewer special GDB configurations. */ | |
1821 | ||
1822 | #if defined (LITTLE_REMOTE_BREAKPOINT) && defined (BIG_REMOTE_BREAKPOINT) && !defined(REMOTE_BREAKPOINT) | |
1823 | #define REMOTE_BREAKPOINT | |
1824 | #endif | |
1825 | ||
1826 | #ifdef REMOTE_BREAKPOINT | |
5af4f5f6 | 1827 | |
fea17b55 SS |
1828 | /* If the target isn't bi-endian, just pretend it is. */ |
1829 | #if !defined (LITTLE_REMOTE_BREAKPOINT) && !defined (BIG_REMOTE_BREAKPOINT) | |
1830 | #define LITTLE_REMOTE_BREAKPOINT REMOTE_BREAKPOINT | |
1831 | #define BIG_REMOTE_BREAKPOINT REMOTE_BREAKPOINT | |
1832 | #endif | |
5af4f5f6 | 1833 | |
fea17b55 SS |
1834 | static unsigned char big_break_insn[] = BIG_REMOTE_BREAKPOINT; |
1835 | static unsigned char little_break_insn[] = LITTLE_REMOTE_BREAKPOINT; | |
5af4f5f6 | 1836 | |
fea17b55 | 1837 | #endif /* REMOTE_BREAKPOINT */ |
5af4f5f6 JK |
1838 | |
1839 | /* Insert a breakpoint on targets that don't have any better breakpoint | |
1840 | support. We read the contents of the target location and stash it, | |
1841 | then overwrite it with a breakpoint instruction. ADDR is the target | |
1842 | location in the target machine. CONTENTS_CACHE is a pointer to | |
1843 | memory allocated for saving the target contents. It is guaranteed | |
1844 | by the caller to be long enough to save sizeof BREAKPOINT bytes (this | |
1845 | is accomplished via BREAKPOINT_MAX). */ | |
1846 | ||
d538b510 | 1847 | static int |
5af4f5f6 JK |
1848 | remote_insert_breakpoint (addr, contents_cache) |
1849 | CORE_ADDR addr; | |
1850 | char *contents_cache; | |
1851 | { | |
fea17b55 | 1852 | #ifdef REMOTE_BREAKPOINT |
5af4f5f6 JK |
1853 | int val; |
1854 | ||
fea17b55 | 1855 | val = target_read_memory (addr, contents_cache, sizeof big_break_insn); |
5af4f5f6 JK |
1856 | |
1857 | if (val == 0) | |
fea17b55 SS |
1858 | { |
1859 | if (TARGET_BYTE_ORDER == BIG_ENDIAN) | |
1860 | val = target_write_memory (addr, (char *) big_break_insn, | |
1861 | sizeof big_break_insn); | |
1862 | else | |
1863 | val = target_write_memory (addr, (char *) little_break_insn, | |
1864 | sizeof little_break_insn); | |
1865 | } | |
5af4f5f6 JK |
1866 | |
1867 | return val; | |
fea17b55 SS |
1868 | #else |
1869 | return memory_insert_breakpoint (addr, contents_cache); | |
1870 | #endif /* REMOTE_BREAKPOINT */ | |
5af4f5f6 JK |
1871 | } |
1872 | ||
d538b510 | 1873 | static int |
5af4f5f6 JK |
1874 | remote_remove_breakpoint (addr, contents_cache) |
1875 | CORE_ADDR addr; | |
1876 | char *contents_cache; | |
1877 | { | |
fea17b55 SS |
1878 | #ifdef REMOTE_BREAKPOINT |
1879 | return target_write_memory (addr, contents_cache, sizeof big_break_insn); | |
1880 | #else | |
1881 | return memory_remove_breakpoint (addr, contents_cache); | |
1882 | #endif /* REMOTE_BREAKPOINT */ | |
5af4f5f6 JK |
1883 | } |
1884 | \f | |
bd5635a1 RP |
1885 | /* Define the target subroutine names */ |
1886 | ||
0a325463 SG |
1887 | static struct target_ops remote_ops = |
1888 | { | |
b543979c JG |
1889 | "remote", /* to_shortname */ |
1890 | "Remote serial target in gdb-specific protocol", /* to_longname */ | |
1891 | "Use a remote computer via a serial line, using a gdb-specific protocol.\n\ | |
1892 | Specify the serial device it is connected to (e.g. /dev/ttya).", /* to_doc */ | |
1893 | remote_open, /* to_open */ | |
1894 | remote_close, /* to_close */ | |
1895 | NULL, /* to_attach */ | |
1896 | remote_detach, /* to_detach */ | |
1897 | remote_resume, /* to_resume */ | |
1898 | remote_wait, /* to_wait */ | |
1899 | remote_fetch_registers, /* to_fetch_registers */ | |
1900 | remote_store_registers, /* to_store_registers */ | |
1901 | remote_prepare_to_store, /* to_prepare_to_store */ | |
b543979c JG |
1902 | remote_xfer_memory, /* to_xfer_memory */ |
1903 | remote_files_info, /* to_files_info */ | |
5af4f5f6 JK |
1904 | remote_insert_breakpoint, /* to_insert_breakpoint */ |
1905 | remote_remove_breakpoint, /* to_remove_breakpoint */ | |
b543979c JG |
1906 | NULL, /* to_terminal_init */ |
1907 | NULL, /* to_terminal_inferior */ | |
1908 | NULL, /* to_terminal_ours_for_output */ | |
1909 | NULL, /* to_terminal_ours */ | |
1910 | NULL, /* to_terminal_info */ | |
ebdb9ade | 1911 | remote_kill, /* to_kill */ |
6b27ebe8 | 1912 | generic_load, /* to_load */ |
b543979c JG |
1913 | NULL, /* to_lookup_symbol */ |
1914 | NULL, /* to_create_inferior */ | |
ebdb9ade | 1915 | remote_mourn, /* to_mourn_inferior */ |
34517ebc | 1916 | 0, /* to_can_run */ |
7c622b41 | 1917 | 0, /* to_notice_signals */ |
43fc25c8 | 1918 | remote_thread_alive, /* to_thread_alive */ |
6c27841f | 1919 | 0, /* to_stop */ |
b543979c JG |
1920 | process_stratum, /* to_stratum */ |
1921 | NULL, /* to_next */ | |
1922 | 1, /* to_has_all_memory */ | |
1923 | 1, /* to_has_memory */ | |
1924 | 1, /* to_has_stack */ | |
1925 | 1, /* to_has_registers */ | |
1926 | 1, /* to_has_execution */ | |
1927 | NULL, /* sections */ | |
1928 | NULL, /* sections_end */ | |
1929 | OPS_MAGIC /* to_magic */ | |
bd5635a1 RP |
1930 | }; |
1931 | ||
0a325463 SG |
1932 | static struct target_ops extended_remote_ops = |
1933 | { | |
2b576293 C |
1934 | "extended-remote", /* to_shortname */ |
1935 | "Extended remote serial target in gdb-specific protocol",/* to_longname */ | |
1936 | "Use a remote computer via a serial line, using a gdb-specific protocol.\n\ | |
1937 | Specify the serial device it is connected to (e.g. /dev/ttya).", /* to_doc */ | |
1938 | extended_remote_open, /* to_open */ | |
1939 | remote_close, /* to_close */ | |
1940 | NULL, /* to_attach */ | |
1941 | remote_detach, /* to_detach */ | |
1942 | remote_resume, /* to_resume */ | |
1943 | remote_wait, /* to_wait */ | |
1944 | remote_fetch_registers, /* to_fetch_registers */ | |
1945 | remote_store_registers, /* to_store_registers */ | |
1946 | remote_prepare_to_store, /* to_prepare_to_store */ | |
1947 | remote_xfer_memory, /* to_xfer_memory */ | |
1948 | remote_files_info, /* to_files_info */ | |
1949 | ||
1950 | remote_insert_breakpoint, /* to_insert_breakpoint */ | |
1951 | remote_remove_breakpoint, /* to_remove_breakpoint */ | |
1952 | ||
1953 | NULL, /* to_terminal_init */ | |
1954 | NULL, /* to_terminal_inferior */ | |
1955 | NULL, /* to_terminal_ours_for_output */ | |
1956 | NULL, /* to_terminal_ours */ | |
1957 | NULL, /* to_terminal_info */ | |
1958 | remote_kill, /* to_kill */ | |
1959 | generic_load, /* to_load */ | |
1960 | NULL, /* to_lookup_symbol */ | |
1961 | extended_remote_create_inferior,/* to_create_inferior */ | |
1962 | extended_remote_mourn, /* to_mourn_inferior */ | |
1963 | 0, /* to_can_run */ | |
1964 | 0, /* to_notice_signals */ | |
1965 | remote_thread_alive, /* to_thread_alive */ | |
1966 | 0, /* to_stop */ | |
1967 | process_stratum, /* to_stratum */ | |
1968 | NULL, /* to_next */ | |
1969 | 1, /* to_has_all_memory */ | |
1970 | 1, /* to_has_memory */ | |
1971 | 1, /* to_has_stack */ | |
1972 | 1, /* to_has_registers */ | |
1973 | 1, /* to_has_execution */ | |
1974 | NULL, /* sections */ | |
1975 | NULL, /* sections_end */ | |
1976 | OPS_MAGIC /* to_magic */ | |
1977 | }; | |
1978 | ||
0a325463 SG |
1979 | /* Some targets are only capable of doing downloads, and afterwards they switch |
1980 | to the remote serial protocol. This function provides a clean way to get | |
1981 | from the download target to the remote target. It's basically just a | |
1982 | wrapper so that we don't have to expose any of the internal workings of | |
1983 | remote.c. | |
1984 | ||
1985 | Prior to calling this routine, you should shutdown the current target code, | |
1986 | else you will get the "A program is being debugged already..." message. | |
1987 | Usually a call to pop_target() suffices. | |
1988 | */ | |
1989 | ||
1990 | void | |
1991 | push_remote_target (name, from_tty) | |
1992 | char *name; | |
1993 | int from_tty; | |
1994 | { | |
1995 | printf_filtered ("Switching to remote protocol\n"); | |
1996 | remote_open (name, from_tty); | |
1997 | } | |
1998 | ||
dd0ce8f6 AC |
1999 | /* Other targets want to use the entire remote serial module but with |
2000 | certain remote_ops overridden. */ | |
2001 | ||
2002 | void | |
2003 | open_remote_target (name, from_tty, target, extended_p) | |
2004 | char *name; | |
2005 | int from_tty; | |
2006 | struct target_ops *target; | |
2007 | int extended_p; | |
2008 | { | |
2009 | printf_filtered ("Selecting the %sremote protocol\n", | |
2010 | (extended_p ? "extended-" : "")); | |
2011 | remote_open_1 (name, from_tty, target, extended_p); | |
2012 | } | |
2013 | ||
bd5635a1 RP |
2014 | void |
2015 | _initialize_remote () | |
2016 | { | |
2017 | add_target (&remote_ops); | |
2b576293 | 2018 | add_target (&extended_remote_ops); |
2c441e7f SS |
2019 | |
2020 | add_show_from_set (add_set_cmd ("remotetimeout", no_class, | |
2021 | var_integer, (char *)&remote_timeout, | |
2022 | "Set timeout value for remote read.\n", &setlist), | |
2023 | &showlist); | |
b52cac6b FF |
2024 | |
2025 | add_show_from_set (add_set_cmd ("remotebreak", no_class, | |
2026 | var_integer, (char *)&remote_break, | |
2027 | "Set whether to send break if interrupted.\n", &setlist), | |
2028 | &showlist); | |
fea17b55 SS |
2029 | |
2030 | add_show_from_set (add_set_cmd ("remotewritesize", no_class, | |
2031 | var_integer, (char *)&remote_write_size, | |
2032 | "Set the maximum number of bytes in each memory write packet.\n", &setlist), | |
2033 | &showlist); | |
976bb0be | 2034 | } |