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