Commit | Line | Data |
---|---|---|
b543979c | 1 | /* Remote target communications for serial-line targets in custom GDB protocol |
2e7eeba9 MS |
2 | Copyright 1988, 91, 92, 93, 94, 95, 96, 97, 1998 |
3 | Free Software Foundation, Inc. | |
bd5635a1 RP |
4 | |
5 | This file is part of GDB. | |
6 | ||
b543979c | 7 | This program is free software; you can redistribute it and/or modify |
bd5635a1 | 8 | it under the terms of the GNU General Public License as published by |
b543979c JG |
9 | the Free Software Foundation; either version 2 of the License, or |
10 | (at your option) any later version. | |
bd5635a1 | 11 | |
b543979c | 12 | This program is distributed in the hope that it will be useful, |
bd5635a1 RP |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
b543979c | 18 | along with this program; if not, write to the Free Software |
998cfe7d | 19 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
bd5635a1 RP |
20 | |
21 | /* Remote communication protocol. | |
e50ebec8 JK |
22 | |
23 | A debug packet whose contents are <data> | |
24 | is encapsulated for transmission in the form: | |
25 | ||
26 | $ <data> # CSUM1 CSUM2 | |
27 | ||
28 | <data> must be ASCII alphanumeric and cannot include characters | |
0c993550 JK |
29 | '$' or '#'. If <data> starts with two characters followed by |
30 | ':', then the existing stubs interpret this as a sequence number. | |
e50ebec8 JK |
31 | |
32 | CSUM1 and CSUM2 are ascii hex representation of an 8-bit | |
33 | checksum of <data>, the most significant nibble is sent first. | |
34 | the hex digits 0-9,a-f are used. | |
35 | ||
36 | Receiver responds with: | |
37 | ||
38 | + - if CSUM is correct and ready for next packet | |
39 | - - if CSUM is incorrect | |
40 | ||
41 | <data> is as follows: | |
4cc1b3f7 JK |
42 | Most values are encoded in ascii hex digits. Signal numbers are according |
43 | to the numbering in target.h. | |
bd5635a1 RP |
44 | |
45 | Request Packet | |
46 | ||
4cc1b3f7 JK |
47 | set thread Hct... Set thread for subsequent operations. |
48 | c = 'c' for thread used in step and | |
49 | continue; t... can be -1 for all | |
50 | threads. | |
51 | c = 'g' for thread used in other | |
52 | operations. If zero, pick a thread, | |
53 | any thread. | |
54 | reply OK for success | |
55 | ENN for an error. | |
56 | ||
bd5635a1 RP |
57 | read registers g |
58 | reply XX....X Each byte of register data | |
59 | is described by two hex digits. | |
60 | Registers are in the internal order | |
61 | for GDB, and the bytes in a register | |
62 | are in the same order the machine uses. | |
63 | or ENN for an error. | |
64 | ||
65 | write regs GXX..XX Each byte of register data | |
66 | is described by two hex digits. | |
67 | reply OK for success | |
68 | ENN for an error | |
69 | ||
0c993550 | 70 | write reg Pn...=r... Write register n... with value r..., |
4aa6fe10 JK |
71 | which contains two hex digits for each |
72 | byte in the register (target byte | |
73 | order). | |
74 | reply OK for success | |
75 | ENN for an error | |
76 | (not supported by all stubs). | |
77 | ||
bd5635a1 RP |
78 | read mem mAA..AA,LLLL AA..AA is address, LLLL is length. |
79 | reply XX..XX XX..XX is mem contents | |
d538b510 RP |
80 | Can be fewer bytes than requested |
81 | if able to read only part of the data. | |
bd5635a1 RP |
82 | or ENN NN is errno |
83 | ||
84 | write mem MAA..AA,LLLL:XX..XX | |
85 | AA..AA is address, | |
86 | LLLL is number of bytes, | |
87 | XX..XX is data | |
88 | reply OK for success | |
d538b510 RP |
89 | ENN for an error (this includes the case |
90 | where only part of the data was | |
91 | written). | |
bd5635a1 | 92 | |
4cc1b3f7 | 93 | continue cAA..AA AA..AA is address to resume |
bd5635a1 RP |
94 | If AA..AA is omitted, |
95 | resume at same address. | |
96 | ||
97 | step sAA..AA AA..AA is address to resume | |
98 | If AA..AA is omitted, | |
99 | resume at same address. | |
100 | ||
0a325463 SG |
101 | continue with Csig;AA..AA Continue with signal sig (hex signal |
102 | signal number). If ;AA..AA is omitted, resume | |
103 | at same address. | |
4cc1b3f7 | 104 | |
0a325463 | 105 | step with Ssig;AA..AA Like 'C' but step not continue. |
4cc1b3f7 JK |
106 | signal |
107 | ||
bd5635a1 RP |
108 | last signal ? Reply the current reason for stopping. |
109 | This is the same reply as is generated | |
110 | for step or cont : SAA where AA is the | |
111 | signal number. | |
112 | ||
b52cac6b FF |
113 | detach D Reply OK. |
114 | ||
bd5635a1 RP |
115 | There is no immediate reply to step or cont. |
116 | The reply comes when the machine stops. | |
4cc1b3f7 | 117 | It is SAA AA is the signal number. |
bd5635a1 | 118 | |
4cc1b3f7 | 119 | or... TAAn...:r...;n...:r...;n...:r...; |
e50ebec8 | 120 | AA = signal number |
4cc1b3f7 JK |
121 | n... = register number (hex) |
122 | r... = register contents | |
123 | n... = `thread' | |
124 | r... = thread process ID. This is | |
125 | a hex integer. | |
126 | n... = other string not starting | |
127 | with valid hex digit. | |
128 | gdb should ignore this n,r pair | |
129 | and go on to the next. This way | |
130 | we can extend the protocol. | |
72bba93b | 131 | or... WAA The process exited, and AA is |
758aeb93 ILT |
132 | the exit status. This is only |
133 | applicable for certains sorts of | |
134 | targets. | |
4cc1b3f7 JK |
135 | or... XAA The process terminated with signal |
136 | AA. | |
998cfe7d SC |
137 | or... OXX..XX XX..XX is hex encoding of ASCII data. This |
138 | can happen at any time while the program is | |
4cc1b3f7 JK |
139 | running and the debugger should |
140 | continue to wait for 'W', 'T', etc. | |
141 | ||
2b576293 C |
142 | thread alive TXX Find out if the thread XX is alive. |
143 | reply OK thread is still alive | |
144 | ENN thread is dead | |
145 | ||
146 | remote restart RXX Restart the remote server | |
147 | ||
148 | extended ops ! Use the extended remote protocol. | |
149 | Sticky -- only needs to be set once. | |
150 | ||
d538b510 RP |
151 | kill request k |
152 | ||
153 | toggle debug d toggle debug flag (see 386 & 68k stubs) | |
154 | reset r reset -- see sparc stub. | |
155 | reserved <other> On other requests, the stub should | |
156 | ignore the request and send an empty | |
157 | response ($#<checksum>). This way | |
158 | we can extend the protocol and GDB | |
159 | can tell whether the stub it is | |
160 | talking to uses the old or the new. | |
72bba93b | 161 | search tAA:PP,MM Search backwards starting at address |
94d4b713 JK |
162 | AA for a match with pattern PP and |
163 | mask MM. PP and MM are 4 bytes. | |
164 | Not supported by all stubs. | |
165 | ||
72bba93b SG |
166 | general query qXXXX Request info about XXXX. |
167 | general set QXXXX=yyyy Set value of XXXX to yyyy. | |
168 | query sect offs qOffsets Get section offsets. Reply is | |
169 | Text=xxx;Data=yyy;Bss=zzz | |
72bba93b | 170 | |
94d4b713 | 171 | Responses can be run-length encoded to save space. A '*' means that |
284f4ee9 | 172 | the next character is an ASCII encoding giving a repeat count which |
94d4b713 | 173 | stands for that many repititions of the character preceding the '*'. |
284f4ee9 SC |
174 | The encoding is n+29, yielding a printable character where n >=3 |
175 | (which is where rle starts to win). Don't use an n > 126. | |
176 | ||
177 | So | |
178 | "0* " means the same as "0000". */ | |
bd5635a1 | 179 | |
d747e0af | 180 | #include "defs.h" |
2b576293 | 181 | #include "gdb_string.h" |
bd5635a1 | 182 | #include <fcntl.h> |
bd5635a1 RP |
183 | #include "frame.h" |
184 | #include "inferior.h" | |
e50ebec8 | 185 | #include "bfd.h" |
6b27ebe8 | 186 | #include "symfile.h" |
bd5635a1 RP |
187 | #include "target.h" |
188 | #include "wait.h" | |
0d14c7df | 189 | /*#include "terminal.h"*/ |
8f86a4e4 | 190 | #include "gdbcmd.h" |
758aeb93 ILT |
191 | #include "objfiles.h" |
192 | #include "gdb-stabs.h" | |
cb1709ae | 193 | #include "gdbthread.h" |
bd5635a1 | 194 | |
d538b510 RP |
195 | #include "dcache.h" |
196 | ||
bd5635a1 RP |
197 | #ifdef USG |
198 | #include <sys/types.h> | |
199 | #endif | |
200 | ||
201 | #include <signal.h> | |
ebdb9ade | 202 | #include "serial.h" |
bd5635a1 | 203 | |
b543979c JG |
204 | /* Prototypes for local functions */ |
205 | ||
45993f61 | 206 | static int remote_write_bytes PARAMS ((CORE_ADDR memaddr, |
43fc25c8 | 207 | char *myaddr, int len)); |
b543979c | 208 | |
45993f61 | 209 | static int remote_read_bytes PARAMS ((CORE_ADDR memaddr, |
43fc25c8 | 210 | char *myaddr, int len)); |
b543979c | 211 | |
4ef1f467 | 212 | static void remote_files_info PARAMS ((struct target_ops * ignore)); |
b543979c | 213 | |
4ef1f467 | 214 | static int remote_xfer_memory PARAMS ((CORE_ADDR memaddr, char * myaddr, |
45993f61 | 215 | int len, int should_write, |
4ef1f467 | 216 | struct target_ops * target)); |
b543979c | 217 | |
45993f61 | 218 | static void remote_prepare_to_store PARAMS ((void)); |
b543979c | 219 | |
45993f61 | 220 | static void remote_fetch_registers PARAMS ((int regno)); |
b543979c | 221 | |
45993f61 SC |
222 | static void remote_resume PARAMS ((int pid, int step, |
223 | enum target_signal siggnal)); | |
b543979c | 224 | |
45993f61 | 225 | static int remote_start_remote PARAMS ((char *dummy)); |
7c622b41 | 226 | |
45993f61 | 227 | static void remote_open PARAMS ((char *name, int from_tty)); |
b543979c | 228 | |
2b576293 C |
229 | static void extended_remote_open PARAMS ((char *name, int from_tty)); |
230 | ||
dd0ce8f6 | 231 | static void remote_open_1 PARAMS ((char *, int, struct target_ops *, int extended_p)); |
2b576293 | 232 | |
45993f61 | 233 | static void remote_close PARAMS ((int quitting)); |
b543979c | 234 | |
45993f61 | 235 | static void remote_store_registers PARAMS ((int regno)); |
b543979c | 236 | |
2b576293 C |
237 | static void remote_mourn PARAMS ((void)); |
238 | ||
239 | static void extended_remote_restart PARAMS ((void)); | |
240 | ||
241 | static void extended_remote_mourn PARAMS ((void)); | |
242 | ||
243 | static void extended_remote_create_inferior PARAMS ((char *, char *, char **)); | |
244 | ||
245 | static void remote_mourn_1 PARAMS ((struct target_ops *)); | |
246 | ||
45993f61 | 247 | static void remote_send PARAMS ((char *buf)); |
b543979c | 248 | |
45993f61 | 249 | static int readchar PARAMS ((int timeout)); |
b543979c | 250 | |
4ef1f467 | 251 | static int remote_wait PARAMS ((int pid, struct target_waitstatus * status)); |
b543979c | 252 | |
45993f61 | 253 | static void remote_kill PARAMS ((void)); |
b543979c | 254 | |
45993f61 | 255 | static int tohex PARAMS ((int nib)); |
b543979c | 256 | |
45993f61 | 257 | static void remote_detach PARAMS ((char *args, int from_tty)); |
5af4f5f6 | 258 | |
45993f61 | 259 | static void remote_interrupt PARAMS ((int signo)); |
b543979c | 260 | |
45993f61 | 261 | static void interrupt_query PARAMS ((void)); |
981a3309 | 262 | |
b607efe7 FF |
263 | static void set_thread PARAMS ((int, int)); |
264 | ||
265 | static int remote_thread_alive PARAMS ((int)); | |
266 | ||
267 | static void get_offsets PARAMS ((void)); | |
268 | ||
269 | static int read_frame PARAMS ((char *)); | |
270 | ||
271 | static int remote_insert_breakpoint PARAMS ((CORE_ADDR, char *)); | |
272 | ||
273 | static int remote_remove_breakpoint PARAMS ((CORE_ADDR, char *)); | |
274 | ||
dd0ce8f6 AC |
275 | static int hexnumlen PARAMS ((ULONGEST num)); |
276 | ||
0e05ecee SS |
277 | static void init_remote_ops PARAMS ((void)); |
278 | ||
279 | static void init_extended_remote_ops PARAMS ((void)); | |
280 | ||
2e7eeba9 MS |
281 | static void remote_stop PARAMS ((void)); |
282 | ||
283 | static int hexnumstr PARAMS ((char *, ULONGEST)); | |
284 | ||
285 | static CORE_ADDR remote_address_masked PARAMS ((CORE_ADDR)); | |
286 | ||
287 | static void print_packet PARAMS ((char *)); | |
288 | ||
289 | static unsigned long crc32 PARAMS ((unsigned char *, int, unsigned int)); | |
290 | ||
291 | static void compare_sections_command PARAMS ((char *, int)); | |
292 | ||
293 | static void packet_command PARAMS ((char *, int)); | |
294 | ||
dd0ce8f6 AC |
295 | /* exported functions */ |
296 | ||
297 | extern int fromhex PARAMS ((int a)); | |
2e7eeba9 | 298 | |
dd0ce8f6 | 299 | extern void getpkt PARAMS ((char *buf, int forever)); |
2e7eeba9 | 300 | |
dd0ce8f6 AC |
301 | extern int putpkt PARAMS ((char *buf)); |
302 | ||
2e7eeba9 MS |
303 | void remote_console_output PARAMS ((char *)); |
304 | ||
4ef1f467 DT |
305 | /* Define the target subroutine names */ |
306 | ||
2e7eeba9 MS |
307 | void open_remote_target PARAMS ((char *, int, struct target_ops *, int)); |
308 | ||
309 | void _initialize_remote PARAMS ((void)); | |
310 | ||
311 | /* */ | |
312 | ||
313 | static struct target_ops remote_ops; | |
c719b714 | 314 | |
2e7eeba9 | 315 | static struct target_ops extended_remote_ops; |
c719b714 | 316 | |
ebdb9ade JK |
317 | /* This was 5 seconds, which is a long time to sit and wait. |
318 | Unless this is going though some terminal server or multiplexer or | |
319 | other form of hairy serial connection, I would think 2 seconds would | |
320 | be plenty. */ | |
bd5635a1 | 321 | |
cb1709ae DP |
322 | /* Changed to allow option to set timeout value. |
323 | was static int remote_timeout = 2; */ | |
324 | extern int remote_timeout; | |
bd5635a1 | 325 | |
b52cac6b FF |
326 | /* This variable chooses whether to send a ^C or a break when the user |
327 | requests program interruption. Although ^C is usually what remote | |
328 | systems expect, and that is the default here, sometimes a break is | |
329 | preferable instead. */ | |
330 | ||
331 | static int remote_break; | |
332 | ||
2e7eeba9 MS |
333 | /* Has the user attempted to interrupt the target? If so, then offer |
334 | the user the opportunity to bail out completely if he interrupts | |
335 | again. */ | |
336 | static int interrupted_already = 0; | |
337 | ||
16e1d1d3 | 338 | /* Descriptor for I/O to remote machine. Initialize it to NULL so that |
bd5635a1 RP |
339 | remote_open knows that we don't have a file open when the program |
340 | starts. */ | |
0a325463 | 341 | static serial_t remote_desc = NULL; |
bd5635a1 | 342 | |
4d57c599 JK |
343 | /* Having this larger than 400 causes us to be incompatible with m68k-stub.c |
344 | and i386-stub.c. Normally, no one would notice because it only matters | |
345 | for writing large chunks of memory (e.g. in downloads). Also, this needs | |
346 | to be more than 400 if required to hold the registers (see below, where | |
347 | we round it up based on REGISTER_BYTES). */ | |
348 | #define PBUFSIZ 400 | |
bd5635a1 RP |
349 | |
350 | /* Maximum number of bytes to read/write at once. The value here | |
351 | is chosen to fill up a packet (the headers account for the 32). */ | |
352 | #define MAXBUFBYTES ((PBUFSIZ-32)/2) | |
353 | ||
b543979c | 354 | /* Round up PBUFSIZ to hold all the registers, at least. */ |
2ddeed27 JK |
355 | /* The blank line after the #if seems to be required to work around a |
356 | bug in HP's PA compiler. */ | |
b543979c | 357 | #if REGISTER_BYTES > MAXBUFBYTES |
2ddeed27 JK |
358 | |
359 | #undef PBUFSIZ | |
b543979c | 360 | #define PBUFSIZ (REGISTER_BYTES * 2 + 32) |
bd5635a1 | 361 | #endif |
4aa6fe10 | 362 | |
4ef1f467 | 363 | |
fea17b55 SS |
364 | /* This variable sets the number of bytes to be written to the target |
365 | in a single packet. Normally PBUFSIZ is satisfactory, but some | |
366 | targets need smaller values (perhaps because the receiving end | |
367 | is slow). */ | |
368 | ||
369 | static int remote_write_size = PBUFSIZ; | |
370 | ||
2e7eeba9 MS |
371 | /* This variable sets the number of bits in an address that are to be |
372 | sent in a memory ("M" or "m") packet. Normally, after stripping | |
373 | leading zeros, the entire address would be sent. This variable | |
374 | restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The | |
375 | initial implementation of remote.c restricted the address sent in | |
376 | memory packets to ``host::sizeof long'' bytes - (typically 32 | |
377 | bits). Consequently, for 64 bit targets, the upper 32 bits of an | |
378 | address was never sent. Since fixing this bug may cause a break in | |
379 | some remote targets this variable is principly provided to | |
380 | facilitate backward compatibility. */ | |
381 | ||
382 | static int remote_address_size; | |
383 | ||
0a325463 SG |
384 | /* This is the size (in chars) of the first response to the `g' command. This |
385 | is used to limit the size of the memory read and write commands to prevent | |
dd0ce8f6 AC |
386 | stub buffers from overflowing. The size does not include headers and |
387 | trailers, it is only the payload size. */ | |
0a325463 SG |
388 | |
389 | static int remote_register_buf_size = 0; | |
390 | ||
4aa6fe10 JK |
391 | /* Should we try the 'P' request? If this is set to one when the stub |
392 | doesn't support 'P', the only consequence is some unnecessary traffic. */ | |
393 | static int stub_supports_P = 1; | |
394 | ||
0a325463 SG |
395 | /* These are pointers to hook functions that may be set in order to |
396 | modify resume/wait behavior for a particular architecture. */ | |
397 | ||
398 | void (*target_resume_hook) PARAMS ((void)); | |
399 | void (*target_wait_loop_hook) PARAMS ((void)); | |
400 | ||
4cc1b3f7 | 401 | \f |
4ef1f467 DT |
402 | /* ------- REMOTE Thread (or) Process support ----------------------- */ |
403 | ||
404 | ||
405 | ||
406 | static int | |
407 | stub_unpack_int PARAMS ((char *buff, int fieldlength)); | |
408 | ||
409 | char * | |
410 | unpack_varlen_hex PARAMS ((char *buff, int *result)); | |
411 | ||
412 | ||
413 | static char * | |
414 | unpack_nibble PARAMS ((char *buf, int *val)); | |
415 | ||
416 | static char * | |
417 | unpack_nibble PARAMS ((char *buf, int *val)); | |
418 | ||
419 | static char * | |
420 | pack_hex_byte PARAMS ((char *pkt, unsigned char byte)); | |
421 | ||
422 | static char * | |
423 | unpack_byte PARAMS ((char *buf, int *value)); | |
424 | ||
425 | static char * | |
426 | pack_int PARAMS ((char *buf, int value)); | |
427 | ||
428 | static char * | |
429 | unpack_int PARAMS ((char *buf, int *value)); | |
430 | ||
431 | static char * | |
432 | pack_string PARAMS ((char *pkt, char *string)); | |
433 | ||
434 | static char * | |
435 | unpack_string PARAMS ((char *src, char *dest, int length)); | |
436 | ||
437 | static char * | |
438 | pack_threadid PARAMS ((char *pkt, threadref * id)); | |
439 | ||
440 | static char * | |
441 | unpack_threadid PARAMS ((char *inbuf, threadref * id)); | |
442 | ||
443 | void | |
444 | int_to_threadref PARAMS ((threadref * id, int value)); | |
445 | ||
446 | ||
447 | int | |
448 | threadref_to_int PARAMS ((threadref * ref)); | |
449 | ||
450 | static void | |
451 | copy_threadref PARAMS ((threadref * dest, threadref * src)); | |
452 | ||
453 | static int | |
454 | threadmatch PARAMS ((threadref * dest, threadref * src)); | |
455 | ||
456 | ||
457 | static char * | |
458 | pack_threadinfo_request PARAMS ((char *pkt, | |
459 | int mode, | |
460 | threadref * id)); | |
461 | ||
462 | static int | |
463 | remote_unpack_thread_info_response PARAMS (( | |
464 | char *pkt, | |
465 | threadref * expectedref, | |
466 | struct gdb_ext_thread_info * info)); | |
467 | ||
468 | ||
469 | int | |
470 | remote_get_threadinfo PARAMS (( | |
471 | threadref * threadid, | |
472 | int fieldset, /* TAG mask */ | |
473 | struct gdb_ext_thread_info * info)); | |
474 | ||
475 | int | |
476 | adapt_remote_get_threadinfo PARAMS (( | |
477 | gdb_threadref * ref, | |
478 | int selection, | |
479 | struct gdb_ext_thread_info * info)); | |
480 | static char * | |
481 | pack_threadlist_request PARAMS (( | |
482 | char *pkt, | |
483 | int startflag, | |
484 | int threadcount, | |
485 | threadref * nextthread)); | |
486 | ||
487 | static int | |
488 | parse_threadlist_response PARAMS (( | |
489 | char *pkt, | |
490 | int result_limit, | |
491 | threadref * original_echo, | |
492 | threadref * resultlist, | |
493 | int *doneflag)); | |
494 | static int | |
495 | remote_get_threadlist PARAMS (( | |
496 | int startflag, | |
497 | threadref * nextthread, | |
498 | int result_limit, | |
499 | int *done, | |
500 | int *result_count, | |
501 | threadref * threadlist)); | |
502 | ||
503 | ||
504 | ||
505 | static int | |
506 | remote_newthread_step PARAMS (( | |
507 | threadref * ref, | |
508 | void *context)); | |
509 | ||
510 | int | |
511 | remote_find_new_threads PARAMS ((void)) ; | |
512 | ||
513 | static void | |
514 | threadalive_test PARAMS ((char *cmd, int tty)); | |
515 | ||
516 | ||
517 | static void | |
518 | threadset_test_cmd PARAMS ((char *cmd, int tty)); | |
519 | ||
520 | static void | |
521 | threadlist_test_cmd PARAMS ((char *cmd, | |
522 | int tty)); | |
523 | ||
524 | void | |
525 | display_thread_info PARAMS ((struct gdb_ext_thread_info * info)); | |
526 | ||
527 | ||
528 | int | |
529 | get_and_display_threadinfo PARAMS ((threadref * ref)); | |
530 | ||
531 | ||
532 | static void | |
533 | threadinfo_test_cmd PARAMS ((char *cmd, | |
534 | int tty)); | |
535 | ||
536 | static int | |
537 | thread_display_step PARAMS (( | |
538 | threadref * ref, | |
539 | void *context)); | |
540 | ||
541 | ||
542 | static void | |
543 | threadlist_update_test_cmd PARAMS ((char *cmd, | |
544 | int tty)); | |
545 | ||
546 | ||
547 | static void | |
548 | init_remote_threadtests PARAMS ((void)); | |
549 | ||
4cc1b3f7 JK |
550 | /* These are the threads which we last sent to the remote system. -1 for all |
551 | or -2 for not sent yet. */ | |
552 | int general_thread; | |
553 | int cont_thread; | |
554 | ||
4ef1f467 DT |
555 | /* Call this function as a result of |
556 | 1) A halt indication (T packet) containing a thread id | |
557 | 2) A direct query of currthread | |
558 | 3) Successful execution of set thread | |
559 | */ | |
560 | ||
561 | static void | |
562 | record_currthread (currthread) | |
563 | int currthread; | |
564 | { | |
565 | inferior_pid = currthread; | |
566 | general_thread = currthread; | |
567 | cont_thread = currthread; | |
568 | } | |
569 | ||
4cc1b3f7 JK |
570 | static void |
571 | set_thread (th, gen) | |
572 | int th; | |
573 | int gen; | |
574 | { | |
575 | char buf[PBUFSIZ]; | |
576 | int state = gen ? general_thread : cont_thread; | |
577 | if (state == th) | |
578 | return; | |
579 | buf[0] = 'H'; | |
580 | buf[1] = gen ? 'g' : 'c'; | |
581 | if (th == 42000) | |
582 | { | |
583 | buf[2] = '0'; | |
584 | buf[3] = '\0'; | |
585 | } | |
586 | else if (th < 0) | |
587 | sprintf (&buf[2], "-%x", -th); | |
588 | else | |
589 | sprintf (&buf[2], "%x", th); | |
590 | putpkt (buf); | |
591 | getpkt (buf, 0); | |
592 | if (gen) | |
593 | general_thread = th; | |
594 | else | |
595 | cont_thread = th; | |
596 | } | |
bd5635a1 | 597 | \f |
2b576293 | 598 | /* Return nonzero if the thread TH is still alive on the remote system. */ |
43fc25c8 JL |
599 | |
600 | static int | |
601 | remote_thread_alive (th) | |
602 | int th; | |
603 | { | |
604 | char buf[PBUFSIZ]; | |
605 | ||
606 | buf[0] = 'T'; | |
607 | if (th < 0) | |
4ef1f467 | 608 | sprintf (&buf[1], "-%08x", -th); |
43fc25c8 | 609 | else |
4ef1f467 | 610 | sprintf (&buf[1], "%08x", th); |
43fc25c8 JL |
611 | putpkt (buf); |
612 | getpkt (buf, 0); | |
613 | return (buf[0] == 'O' && buf[1] == 'K'); | |
614 | } | |
2b576293 | 615 | |
4ef1f467 DT |
616 | /* |
617 | About these extended threadlist and threadinfo packets. | |
618 | They are variable length packets but, the fields within them | |
619 | are often fixed length. | |
620 | They are redundent enough to send over UDP as is the remote protocol | |
621 | in general. | |
622 | There is a matching unit test module in libstub. | |
623 | */ | |
624 | ||
625 | ||
626 | #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES*2) | |
627 | /* encode 64 bits in 16 chars of hex */ | |
628 | ||
629 | ||
630 | static const char hexchars[] = "0123456789abcdef"; | |
631 | ||
632 | static int | |
633 | ishex (ch, val) | |
634 | char ch; | |
635 | int *val; | |
636 | { | |
637 | if ((ch >= 'a') && (ch <= 'f')) | |
638 | { | |
639 | *val = ch - 'a' + 10; | |
640 | return 1; | |
641 | } | |
642 | if ((ch >= 'A') && (ch <= 'F')) | |
643 | { | |
644 | *val = ch - 'A' + 10; | |
645 | return 1; | |
646 | } | |
647 | if ((ch >= '0') && (ch <= '9')) | |
648 | { | |
649 | *val = ch - '0'; | |
650 | return 1; | |
651 | } | |
652 | return 0; | |
653 | } | |
654 | ||
655 | static int | |
656 | stubhex (ch) | |
657 | unsigned char ch; | |
658 | { | |
659 | if (ch >= 'a' && ch <= 'f') | |
660 | return ch - 'a' + 10; | |
661 | if (ch >= '0' && ch <= '9') | |
662 | return ch - '0'; | |
663 | if (ch >= 'A' && ch <= 'F') | |
664 | return ch - 'A' + 10; | |
665 | return -1; | |
666 | } | |
667 | ||
668 | static int | |
669 | stub_unpack_int (buff, fieldlength) | |
670 | char *buff; | |
671 | int fieldlength; | |
672 | { | |
673 | int retval = 0; | |
674 | int nibble; | |
675 | while (fieldlength) | |
676 | { | |
677 | nibble = stubhex (*buff++); | |
678 | retval |= nibble; | |
679 | fieldlength--; | |
680 | if (fieldlength) | |
681 | retval = retval << 4; | |
682 | } | |
683 | return retval; | |
684 | } | |
685 | ||
686 | char * | |
687 | unpack_varlen_hex (buff, result) | |
688 | char *buff; /* packet to parse */ | |
689 | int *result; | |
690 | { | |
691 | int nibble; | |
692 | int retval; | |
693 | retval = 0; | |
694 | ||
695 | while (ishex (*buff, &nibble)) | |
696 | { | |
697 | buff++; | |
698 | retval = retval << 4; | |
699 | retval |= nibble & 0x0f; | |
700 | } | |
701 | *result = retval; | |
702 | return buff; | |
703 | } | |
704 | ||
705 | static char * | |
706 | unpack_nibble (buf, val) | |
707 | char *buf; | |
708 | int *val; | |
709 | ||
710 | { | |
711 | ishex (*buf++, val); | |
712 | return buf; | |
713 | } | |
714 | ||
715 | static char * | |
716 | pack_nibble (buf, nibble) | |
717 | char *buf; | |
718 | int nibble; | |
719 | ||
720 | { | |
721 | *buf++ = hexchars[(nibble & 0x0f)]; | |
722 | return buf; | |
723 | } | |
724 | ||
725 | static char * | |
726 | pack_hex_byte (pkt, byte) | |
727 | char *pkt; | |
728 | unsigned char byte; | |
729 | { | |
730 | *pkt++ = hexchars[(byte >> 4) & 0xf]; | |
731 | *pkt++ = hexchars[(byte & 0xf)]; | |
732 | return pkt; | |
733 | } | |
734 | ||
735 | static char * | |
736 | unpack_byte (buf, value) | |
737 | char *buf; | |
738 | int *value; | |
739 | { | |
740 | *value = stub_unpack_int (buf, 2); | |
741 | return buf + 2; | |
742 | } | |
743 | ||
744 | ||
745 | static char * | |
746 | pack_int (buf, value) | |
747 | char *buf; | |
748 | int value; | |
749 | { | |
750 | buf = pack_hex_byte (buf, (value >> 24) & 0xff); | |
751 | buf = pack_hex_byte (buf, (value >> 16) & 0xff); | |
752 | buf = pack_hex_byte (buf, (value >> 8) & 0x0ff); | |
753 | buf = pack_hex_byte (buf, (value & 0xff)); | |
754 | return buf; | |
755 | } | |
756 | ||
757 | ||
758 | static char * | |
759 | unpack_int (buf, value) | |
760 | char *buf; | |
761 | int *value; | |
762 | { | |
763 | *value = stub_unpack_int (buf, 8); | |
764 | return buf + 8; | |
765 | } | |
766 | ||
767 | ||
768 | static char * | |
769 | pack_string (pkt, string) | |
770 | char *pkt; | |
771 | char *string; | |
772 | { | |
773 | char ch; | |
774 | int len; | |
775 | len = strlen (string); | |
776 | if (len > 200) | |
777 | len = 200; /* Bigger than most GDB packets, junk??? */ | |
778 | pkt = pack_hex_byte (pkt, len); | |
779 | while (len-- > 0) | |
780 | { | |
781 | ch = *string++; | |
782 | if ((ch == '\0') || (ch == '#')) | |
783 | ch = '*'; /* Protect encapsulation */ | |
784 | *pkt++ = ch; | |
785 | } | |
786 | return pkt; | |
787 | } | |
788 | ||
789 | static char * | |
790 | unpack_string (src, dest, length) | |
791 | char *src; | |
792 | char *dest; | |
793 | int length; | |
794 | { | |
795 | while (length--) | |
796 | *dest++ = *src++; | |
797 | *dest = '\0'; | |
798 | return src; | |
799 | } | |
800 | ||
801 | static char * | |
802 | pack_threadid (pkt, id) | |
803 | char *pkt; | |
804 | threadref *id; | |
805 | { | |
806 | char *limit; | |
807 | unsigned char *altid; | |
808 | altid = (unsigned char *) id; | |
809 | limit = pkt + BUF_THREAD_ID_SIZE; | |
810 | while (pkt < limit) | |
811 | pkt = pack_hex_byte (pkt, *altid++); | |
812 | return pkt; | |
813 | } | |
814 | ||
815 | ||
816 | static char * | |
817 | unpack_threadid (inbuf, id) | |
818 | char *inbuf; | |
819 | threadref *id; | |
820 | { | |
821 | char *altref; | |
822 | char *limit = inbuf + BUF_THREAD_ID_SIZE; | |
823 | int x, y; | |
824 | altref = (char *) id; | |
825 | ||
826 | while (inbuf < limit) | |
827 | { | |
828 | x = stubhex (*inbuf++); | |
829 | y = stubhex (*inbuf++); | |
830 | *altref++ = (x << 4) | y; | |
831 | } | |
832 | return inbuf; | |
833 | } | |
834 | ||
835 | /* Externally, threadrefs are 64 bits but internally, they are still | |
836 | ints. This is due to a mismatch of specifications. | |
837 | We would like to use 64bit thread references internally. | |
838 | This is an adapter function. | |
839 | */ | |
840 | ||
841 | void | |
842 | int_to_threadref (id, value) | |
843 | threadref *id; | |
844 | int value; | |
845 | { | |
846 | unsigned char *scan; | |
847 | scan = (unsigned char *) id; | |
848 | { | |
849 | int i = 4; | |
850 | while (i--) | |
851 | *scan++ = 0; | |
852 | } | |
853 | *scan++ = (value >> 24) & 0xff; | |
854 | *scan++ = (value >> 16) & 0xff; | |
855 | *scan++ = (value >> 8) & 0xff; | |
856 | *scan++ = (value & 0xff); | |
857 | } | |
858 | ||
859 | int | |
860 | threadref_to_int (ref) | |
861 | threadref *ref; | |
862 | { | |
863 | int value = 0; | |
864 | unsigned char *scan; | |
865 | int i; | |
866 | ||
867 | scan = (char *) ref; | |
868 | scan += 4; | |
869 | i = 4; | |
870 | while (i-- > 0) | |
871 | value = (value << 8) | ((*scan++) & 0xff); | |
872 | return value; | |
873 | } | |
874 | ||
875 | static void | |
876 | copy_threadref (dest, src) | |
877 | threadref *dest; | |
878 | threadref *src; | |
879 | { | |
880 | int i; | |
881 | unsigned char *csrc, *cdest; | |
882 | csrc = (unsigned char *) src; | |
883 | cdest = (unsigned char *) dest; | |
884 | i = 8; | |
885 | while (i--) | |
886 | *cdest++ = *csrc++; | |
887 | } | |
888 | ||
889 | ||
890 | ||
891 | static int | |
892 | threadmatch (dest, src) | |
893 | threadref *dest; | |
894 | threadref *src; | |
895 | { | |
896 | /* things are broken right now, so just assume we got a match */ | |
897 | #if 0 | |
898 | unsigned char *srcp, *destp; | |
899 | int i, result; | |
900 | srcp = (char *) src; | |
901 | destp = (char *) dest; | |
902 | ||
903 | result = 1; | |
904 | while (i-- > 0) | |
905 | result &= (*srcp++ == *destp++) ? 1 : 0; | |
906 | return result; | |
907 | #endif | |
908 | return 1; | |
909 | } | |
910 | ||
911 | #if THREAD_PKT_TRACE | |
912 | #define PKT_TRACE(title,packet) { printf_filtered("%s %s\n", title, packet);} | |
913 | #else | |
914 | #define PKT_TRACE(a,b) {} | |
915 | #endif | |
916 | ||
917 | ||
918 | /* ----- PACK_THREAD_INFO_REQUEST -------------------------------- */ | |
919 | ||
920 | /* | |
921 | threadid:1, # always request threadid | |
922 | context_exists:2, | |
923 | display:4, | |
924 | unique_name:8, | |
925 | more_display:16 | |
926 | */ | |
927 | ||
928 | /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */ | |
929 | ||
930 | static char * | |
931 | pack_threadinfo_request PARAMS ((char *pkt, | |
932 | int mode, | |
933 | threadref * id)); | |
934 | ||
935 | static char * | |
936 | pack_threadinfo_request (pkt, mode, id) | |
937 | char *pkt; | |
938 | int mode; | |
939 | threadref *id; | |
940 | { | |
941 | char *base = pkt; | |
942 | *pkt++ = 'q'; /* Info Query */ | |
943 | *pkt++ = 'P'; /* process or thread info */ | |
944 | pkt = pack_int (pkt, mode); /* mode */ | |
945 | pkt = pack_threadid (pkt, id); /* threadid */ | |
946 | *pkt = '\0'; /* terminate */ | |
947 | PKT_TRACE ("threadinfo-req ", base); | |
948 | return pkt; | |
949 | } | |
950 | ||
951 | ||
952 | /* These values tag the fields in a thread info response packet */ | |
953 | /* Tagging the fields allows us to request specific fields and to | |
954 | add more fields as time goes by */ | |
955 | #define TAG_THREADID 1 /* Echo the thread identifier */ | |
956 | #define TAG_EXISTS 2 /* It this process defined enough to | |
957 | fetch registers and its stack */ | |
958 | #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */ | |
959 | #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is */ | |
960 | #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about the process*/ | |
961 | ||
962 | ||
963 | static int | |
964 | remote_unpack_thread_info_response (pkt, expectedref, info) | |
965 | char *pkt; | |
966 | threadref *expectedref; | |
967 | struct gdb_ext_thread_info *info; | |
968 | { | |
969 | int mask, length; | |
970 | unsigned int tag; | |
971 | threadref ref; | |
972 | char *limit = pkt + PBUFSIZ; /* plausable parsing limit */ | |
973 | int retval = 1; | |
974 | ||
975 | PKT_TRACE ("unpack-threadinfo ", pkt); | |
976 | ||
977 | /* info->threadid = 0; FIXME: implement zero_threadref */ | |
978 | info->active = 0; | |
979 | info->display[0] = '\0'; | |
980 | info->shortname[0] = '\0'; | |
981 | info->more_display[0] = '\0'; | |
982 | ||
983 | /* Assume the characters indicating the packet type have been stripped */ | |
984 | pkt = unpack_int (pkt, &mask); /* arg mask */ | |
985 | pkt = unpack_threadid (pkt, &ref); | |
986 | ||
987 | if (mask == 0) | |
988 | warning("Incomplete response to threadinfo request\n"); | |
989 | if (!threadmatch (&ref, expectedref)) | |
990 | { /* This is an answer to a different request */ | |
991 | warning("ERROR RMT Thread info mismatch\n"); | |
992 | return 0; | |
993 | } | |
994 | copy_threadref (&info->threadid, &ref); | |
995 | ||
996 | /* Loop on tagged fields , try to bail if somthing goes wrong */ | |
997 | ||
998 | while ((pkt < limit) && mask && *pkt) /* packets are terminated with nulls */ | |
999 | { | |
1000 | pkt = unpack_int (pkt, &tag); /* tag */ | |
1001 | pkt = unpack_byte (pkt, &length); /* length */ | |
1002 | if (!(tag & mask)) /* tags out of synch with mask */ | |
1003 | { | |
1004 | warning ("ERROR RMT: threadinfo tag mismatch\n"); | |
1005 | retval = 0; | |
1006 | break; | |
1007 | } | |
1008 | if (tag == TAG_THREADID) | |
1009 | { | |
1010 | if (length != 16) | |
1011 | { | |
1012 | warning ("ERROR RMT: length of threadid is not 16\n"); | |
1013 | retval = 0; | |
1014 | break; | |
1015 | } | |
1016 | pkt = unpack_threadid (pkt, &ref); | |
1017 | mask = mask & ~TAG_THREADID; | |
1018 | continue; | |
1019 | } | |
1020 | if (tag == TAG_EXISTS) | |
1021 | { | |
1022 | info->active = stub_unpack_int (pkt, length); | |
1023 | pkt += length; | |
1024 | mask = mask & ~(TAG_EXISTS); | |
1025 | if (length > 8) | |
1026 | { | |
1027 | warning ("ERROR RMT: 'exists' length too long\n"); | |
1028 | retval = 0; | |
1029 | break; | |
1030 | } | |
1031 | continue; | |
1032 | } | |
1033 | if (tag == TAG_THREADNAME) | |
1034 | { | |
1035 | pkt = unpack_string (pkt, &info->shortname[0], length); | |
1036 | mask = mask & ~TAG_THREADNAME; | |
1037 | continue; | |
1038 | } | |
1039 | if (tag == TAG_DISPLAY) | |
1040 | { | |
1041 | pkt = unpack_string (pkt, &info->display[0], length); | |
1042 | mask = mask & ~TAG_DISPLAY; | |
1043 | continue; | |
1044 | } | |
1045 | if (tag == TAG_MOREDISPLAY) | |
1046 | { | |
1047 | pkt = unpack_string (pkt, &info->more_display[0], length); | |
1048 | mask = mask & ~TAG_MOREDISPLAY; | |
1049 | continue; | |
1050 | } | |
1051 | warning ("ERROR RMT: unknown thread info tag\n"); | |
1052 | break; /* Not a tag we know about */ | |
1053 | } | |
1054 | return retval; | |
1055 | } | |
1056 | ||
1057 | ||
1058 | /* ------ REMOTE_GET_THREADINFO -------------------------------------- */ | |
1059 | ||
1060 | int | |
1061 | remote_get_threadinfo (threadid, fieldset, info) | |
1062 | threadref *threadid; | |
1063 | int fieldset; /* TAG mask */ | |
1064 | struct gdb_ext_thread_info *info; | |
1065 | { | |
1066 | int result; | |
1067 | char threadinfo_pkt[PBUFSIZ]; | |
1068 | pack_threadinfo_request (threadinfo_pkt, fieldset, threadid); | |
1069 | putpkt (threadinfo_pkt); | |
1070 | getpkt (threadinfo_pkt, 0); | |
1071 | result = remote_unpack_thread_info_response (threadinfo_pkt + 2, threadid, info); | |
1072 | return result; | |
1073 | } | |
1074 | ||
1075 | /* ------- ADAPT_remote_GET_THREADINFO - */ | |
1076 | /* Unfortunatly, 61 but thread-ids are bugger than the internal | |
1077 | representation of a threadid. */ | |
1078 | ||
1079 | ||
1080 | int | |
1081 | adapt_remote_get_threadinfo (ref, selection, info) | |
1082 | gdb_threadref *ref; | |
1083 | int selection; | |
1084 | struct gdb_ext_thread_info *info; | |
1085 | { | |
1086 | threadref lclref; | |
1087 | int_to_threadref (&lclref, *ref); | |
1088 | return remote_get_threadinfo (&lclref, selection, info); | |
1089 | } | |
1090 | ||
1091 | ||
1092 | /* -------- PACK_THREADLIST-REQUEST --------------------------------- */ | |
1093 | /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */ | |
1094 | ||
1095 | static char * | |
1096 | pack_threadlist_request (pkt, startflag, threadcount, nextthread) | |
1097 | char *pkt; | |
1098 | int startflag; | |
1099 | int threadcount; | |
1100 | threadref *nextthread; | |
1101 | { | |
1102 | *pkt++ = 'q'; /* info query packet */ | |
1103 | *pkt++ = 'L'; /* Process LIST or threadLIST request */ | |
1104 | pkt = pack_nibble (pkt, startflag); /* initflag 1 bytes */ | |
1105 | pkt = pack_hex_byte (pkt, threadcount); /* threadcount 2 bytes */ | |
1106 | pkt = pack_threadid (pkt, nextthread); /* 64 bit thread identifier */ | |
1107 | *pkt = '\0'; | |
1108 | return pkt; | |
1109 | } | |
1110 | ||
1111 | ||
1112 | /* ---------- PARSE_THREADLIST_RESPONSE ------------------------------------ */ | |
1113 | /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */ | |
1114 | ||
1115 | ||
1116 | static int | |
1117 | parse_threadlist_response (pkt, result_limit, original_echo, | |
1118 | resultlist, doneflag) | |
1119 | char *pkt; | |
1120 | int result_limit; | |
1121 | threadref *original_echo; | |
1122 | threadref *resultlist; | |
1123 | int *doneflag; | |
1124 | { | |
1125 | char *limit; | |
1126 | int count, resultcount, done; | |
1127 | resultcount = 0; | |
1128 | ||
1129 | /* assume the 'q' and 'M chars have been stripped */ | |
1130 | PKT_TRACE ("parse-threadlist-response ", pkt); | |
1131 | limit = pkt + (PBUFSIZ - BUF_THREAD_ID_SIZE); /* done parse past here */ | |
1132 | pkt = unpack_byte (pkt, &count); /* count field */ | |
1133 | pkt = unpack_nibble (pkt, &done); | |
1134 | /* The first threadid is the argument threadid */ | |
1135 | pkt = unpack_threadid (pkt, original_echo); /* should match query packet */ | |
1136 | while ((count-- > 0) && (pkt < limit)) | |
1137 | { | |
1138 | pkt = unpack_threadid (pkt, resultlist++); | |
1139 | if (resultcount++ >= result_limit) | |
1140 | break; | |
1141 | } | |
1142 | if (doneflag) | |
1143 | *doneflag = done; | |
1144 | return resultcount; /* successvalue */ | |
1145 | } | |
1146 | ||
1147 | ||
1148 | ||
1149 | static int | |
1150 | remote_get_threadlist (startflag, nextthread, result_limit, | |
1151 | done, result_count, threadlist) | |
1152 | int startflag; | |
1153 | threadref *nextthread; | |
1154 | int result_limit; | |
1155 | int *done; | |
1156 | int *result_count; | |
1157 | threadref *threadlist; | |
1158 | ||
1159 | { | |
1160 | static threadref echo_nextthread; | |
1161 | char threadlist_packet[PBUFSIZ]; | |
1162 | char t_response[PBUFSIZ]; | |
1163 | int result = 1; | |
1164 | ||
1165 | /* Trancate result limit to be smaller than the packet size */ | |
1166 | if ((((result_limit + 1) * BUF_THREAD_ID_SIZE) + 10) >= PBUFSIZ) | |
1167 | result_limit = (PBUFSIZ / BUF_THREAD_ID_SIZE) - 2; | |
1168 | ||
1169 | pack_threadlist_request (threadlist_packet, | |
1170 | startflag, result_limit, nextthread); | |
1171 | putpkt (threadlist_packet); | |
1172 | getpkt (t_response, 0); | |
1173 | *result_count = parse_threadlist_response ( | |
1174 | t_response + 2, /* strip header */ | |
1175 | result_limit, | |
1176 | &echo_nextthread, | |
1177 | threadlist, | |
1178 | done); | |
1179 | if (!threadmatch (&echo_nextthread, nextthread)) | |
1180 | { | |
1181 | /* FIXME: This is a good reason to drop the packet */ | |
1182 | /* Possably, there is a duplicate response */ | |
1183 | /* Possabilities : | |
1184 | retransmit immediatly - race conditions | |
1185 | retransmit after timeout - yes | |
1186 | exit | |
1187 | wait for packet, then exit | |
1188 | */ | |
1189 | warning ("HMM: threadlist did not echo arg thread, dropping it\n"); | |
1190 | return 0; /* I choose simply exiting */ | |
1191 | } | |
1192 | if (*result_count <= 0) | |
1193 | { | |
1194 | if (*done != 1) | |
1195 | { | |
1196 | warning ("RMT ERROR : failed to get remote thread list\n"); | |
1197 | result = 0; | |
1198 | } | |
1199 | return result; /* break; */ | |
1200 | } | |
1201 | if (*result_count > result_limit) | |
1202 | { | |
1203 | *result_count = 0; | |
1204 | warning ("RMT ERROR: threadlist response longer than requested\n"); | |
1205 | return 0; | |
1206 | } | |
1207 | return result; | |
1208 | } | |
1209 | ||
1210 | ||
1211 | ||
1212 | /* This is the interface between remote and threads, remotes upper interface */ | |
1213 | /* remote_find_new_threads retreives the thread list and for each | |
1214 | thread in the list, looks up the thread in GDB's internal list, | |
1215 | ading the thread if it does not already exist. | |
1216 | This involves getting partial thread lists from the remote target so, | |
1217 | polling the quit_flag is required. | |
1218 | */ | |
1219 | ||
1220 | typedef int (*rmt_thread_action) ( | |
1221 | threadref * ref, | |
1222 | void *context | |
1223 | ); | |
1224 | ||
1225 | #define MAXTHREADLISTRESULTS 32 /* About this many threadisds fit in a packet */ | |
1226 | ||
1227 | static int | |
1228 | remote_threadlist_iterator PARAMS (( | |
1229 | rmt_thread_action stepfunction, | |
1230 | void *context, | |
1231 | int looplimit)); | |
1232 | ||
1233 | static int | |
1234 | remote_threadlist_iterator (stepfunction, context, looplimit) | |
1235 | rmt_thread_action stepfunction; | |
1236 | void *context; | |
1237 | int looplimit; | |
1238 | { | |
1239 | int done, i, result_count; | |
1240 | int startflag = 1; | |
1241 | int result = 1; | |
1242 | int loopcount = 0; | |
1243 | static threadref nextthread; | |
1244 | static threadref echo_nextthread; | |
1245 | static threadref resultthreadlist[MAXTHREADLISTRESULTS]; | |
1246 | ||
1247 | done = 0; | |
1248 | while (!done) | |
1249 | { | |
1250 | if (loopcount++ > looplimit) | |
1251 | { | |
1252 | result = 0; | |
1253 | warning ("Remote fetch threadlist -infinite loop-\n"); | |
1254 | break; | |
1255 | } | |
1256 | if (!remote_get_threadlist (startflag, | |
1257 | &nextthread, | |
1258 | MAXTHREADLISTRESULTS, | |
1259 | &done, | |
1260 | &result_count, | |
1261 | resultthreadlist)) | |
1262 | { | |
1263 | result = 0; | |
1264 | break; | |
1265 | } | |
1266 | startflag = 0; /* clear for later iterations */ | |
1267 | /* Setup to resume next batch of thread references , set nestthread */ | |
1268 | if (result_count >= 1) | |
1269 | copy_threadref (&nextthread, &resultthreadlist[result_count - 1]); | |
1270 | /* output_threadid("last-of-batch",&nextthread); */ | |
1271 | i = 0; | |
1272 | while (result_count--) | |
1273 | if (!(result = (*stepfunction) (&resultthreadlist[i++], context))) | |
1274 | break; | |
1275 | } | |
1276 | return result; | |
1277 | } | |
1278 | ||
1279 | ||
1280 | static int | |
1281 | remote_newthread_step (ref, context) | |
1282 | threadref *ref; | |
1283 | void *context | |
1284 | ; | |
1285 | ||
1286 | { | |
1287 | int pid; | |
1288 | pid = threadref_to_int (ref); | |
1289 | if (!in_thread_list (pid)) | |
1290 | add_thread (pid); | |
1291 | return 1; /* continue iterator */ | |
1292 | } | |
1293 | ||
1294 | #define CRAZY_MAX_THREADS 1000 | |
1295 | ||
1296 | ||
1297 | int | |
1298 | remote_find_new_threads (void) | |
1299 | { | |
1300 | return remote_threadlist_iterator (remote_newthread_step, 0, CRAZY_MAX_THREADS); | |
1301 | } /* remote_find_new_threads */ | |
1302 | ||
1303 | int | |
1304 | remote_update_threads () | |
1305 | { | |
1306 | /* Right now, this is empty. But it is one of the functions | |
1307 | defined for the thread target vector so it gets called. | |
1308 | If we were to allow the modification of the registers of | |
1309 | a suspended process, this would be implemented. */ | |
1310 | return 0; | |
1311 | } | |
1312 | ||
1313 | static struct target_thread_vector remote_thread_vec; | |
1314 | ||
1315 | /* Initialize the thread vector which is used by threads.c */ | |
1316 | /* The thread stubb is a package, it has an initializer */ | |
1317 | void init_remote_threads () | |
1318 | { | |
1319 | remote_thread_vec.find_new_threads = remote_find_new_threads; | |
1320 | remote_thread_vec.get_thread_info = adapt_remote_get_threadinfo; | |
1321 | } | |
1322 | ||
1323 | /* --------- UNIT_TEST for THREAD oriented PACKETS -------------------------- */ | |
1324 | ||
1325 | #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid */ | |
1326 | ||
1327 | ||
1328 | static void | |
1329 | threadset_test_cmd (cmd, tty) | |
1330 | char *cmd; | |
1331 | int tty; | |
1332 | { | |
1333 | int sample_thread = SAMPLE_THREAD; | |
1334 | printf_filtered ("Remote threadset test\n"); | |
1335 | set_thread (sample_thread, 1); | |
1336 | } | |
1337 | ||
1338 | ||
1339 | static void | |
1340 | threadalive_test (cmd, tty) | |
1341 | char *cmd; | |
1342 | int tty; | |
1343 | { | |
1344 | int sample_thread = SAMPLE_THREAD; | |
1345 | if (remote_thread_alive (sample_thread)) | |
1346 | printf_filtered ("PASS: Thread alive test\n"); | |
1347 | else | |
1348 | printf_filtered ("FAIL: Thread alive test\n"); | |
1349 | } | |
1350 | ||
1351 | void | |
1352 | output_threadid PARAMS ((char *title, threadref * ref)); | |
1353 | ||
1354 | void | |
1355 | output_threadid (title, ref) | |
1356 | char *title; | |
1357 | threadref *ref; | |
1358 | { | |
1359 | char hexid[20]; | |
1360 | pack_threadid (&hexid[0], ref); /* Convert threead id into hex */ | |
1361 | hexid[16] = 0; | |
1362 | printf_filtered ("%s %s\n", title, (&hexid[0])); | |
1363 | } | |
1364 | ||
1365 | ||
1366 | static void | |
1367 | threadlist_test_cmd (cmd, tty) | |
1368 | char *cmd; | |
1369 | int tty; | |
1370 | { | |
1371 | int startflag = 1; | |
1372 | threadref nextthread; | |
1373 | int done, result_count; | |
1374 | threadref threadlist[3]; | |
1375 | ||
1376 | printf_filtered ("Remote Threadlist test\n"); | |
1377 | if (!remote_get_threadlist (startflag, &nextthread, 3, &done, | |
1378 | &result_count, &threadlist[0])) | |
1379 | printf_filtered ("FAIL: threadlist test\n"); | |
1380 | else | |
1381 | { | |
1382 | threadref *scan = threadlist; | |
1383 | threadref *limit = scan + result_count; | |
1384 | while (scan < limit) | |
1385 | output_threadid (" thread ", scan++); | |
1386 | } | |
1387 | } | |
1388 | ||
1389 | void | |
1390 | display_thread_info (info) | |
1391 | struct gdb_ext_thread_info *info; | |
1392 | { | |
1393 | ||
1394 | output_threadid ("Threadid: ", &info->threadid); | |
1395 | /* short name */ | |
1396 | printf_filtered ("Name: %s\n ", info->shortname); | |
1397 | /* format display state */ | |
1398 | printf_filtered ("State: %s\n", info->display); | |
1399 | /* additional data */ | |
1400 | printf_filtered ("other: %s\n\n", info->more_display); | |
1401 | } | |
1402 | ||
1403 | int | |
1404 | get_and_display_threadinfo (ref) | |
1405 | threadref *ref; | |
1406 | { | |
1407 | int result; | |
1408 | int set; | |
1409 | struct gdb_ext_thread_info threadinfo; | |
1410 | ||
1411 | set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME | |
1412 | | TAG_MOREDISPLAY | TAG_DISPLAY; | |
1413 | if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo))) | |
1414 | display_thread_info (&threadinfo); | |
1415 | return result; | |
1416 | } | |
1417 | ||
1418 | static void | |
1419 | threadinfo_test_cmd (cmd, tty) | |
1420 | char *cmd; | |
1421 | int tty; | |
1422 | { | |
1423 | int athread = SAMPLE_THREAD; | |
1424 | threadref thread; | |
1425 | int set; | |
1426 | ||
1427 | int_to_threadref (&thread, athread); | |
1428 | printf_filtered ("Remote Threadinfo test\n"); | |
1429 | if (!get_and_display_threadinfo (&thread)) | |
1430 | printf_filtered ("FAIL cannot get thread info\n"); | |
1431 | } | |
1432 | ||
1433 | ||
1434 | static int | |
1435 | thread_display_step (ref, context) | |
1436 | threadref *ref; | |
1437 | void *context; | |
1438 | { | |
1439 | /* output_threadid(" threadstep ",ref); *//* simple test */ | |
1440 | return get_and_display_threadinfo (ref); | |
1441 | } | |
1442 | ||
1443 | ||
1444 | static void | |
1445 | threadlist_update_test_cmd (cmd, tty) | |
1446 | char *cmd; | |
1447 | int tty; | |
1448 | { | |
1449 | printf_filtered ("Remote Threadlist update test\n"); | |
1450 | remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS); | |
1451 | } | |
1452 | ||
1453 | static void | |
1454 | init_remote_threadtests (void) | |
1455 | { | |
1456 | add_com ("tlist", class_obscure, threadlist_test_cmd, | |
1457 | "Fetch and print the remote list of thread identifiers, one pkt only"); | |
1458 | add_com ("tinfo", class_obscure, threadinfo_test_cmd, | |
1459 | "Fetch and display info about one thread"); | |
1460 | add_com ("tset", class_obscure, threadset_test_cmd, | |
1461 | "Test setting to a different thread"); | |
1462 | add_com ("tupd", class_obscure, threadlist_update_test_cmd, | |
1463 | "Iterate through updating all remote thread info"); | |
1464 | add_com ("talive", class_obscure, threadalive_test, | |
1465 | " Remote thread alive test "); | |
1466 | } | |
1467 | ||
1468 | #define INIT_REMOTE_THREADTESTS { init_remote_threadtests();} | |
1469 | /* END OF REMOTE THREAD UNIT TESTS */ | |
1470 | \f | |
1471 | ||
2b576293 C |
1472 | /* Restart the remote side; this is an extended protocol operation. */ |
1473 | ||
1474 | static void | |
1475 | extended_remote_restart () | |
1476 | { | |
1477 | char buf[PBUFSIZ]; | |
1478 | ||
1479 | /* Send the restart command; for reasons I don't understand the | |
1480 | remote side really expects a number after the "R". */ | |
1481 | buf[0] = 'R'; | |
1482 | sprintf (&buf[1], "%x", 0); | |
1483 | putpkt (buf); | |
1484 | ||
1485 | /* Now query for status so this looks just like we restarted | |
1486 | gdbserver from scratch. */ | |
1487 | putpkt ("?"); | |
1488 | getpkt (buf, 0); | |
1489 | } | |
43fc25c8 | 1490 | \f |
bd5635a1 RP |
1491 | /* Clean up connection to a remote debugger. */ |
1492 | ||
e1ce8aa5 | 1493 | /* ARGSUSED */ |
b543979c | 1494 | static void |
bd5635a1 RP |
1495 | remote_close (quitting) |
1496 | int quitting; | |
1497 | { | |
ebdb9ade JK |
1498 | if (remote_desc) |
1499 | SERIAL_CLOSE (remote_desc); | |
1500 | remote_desc = NULL; | |
b543979c JG |
1501 | } |
1502 | ||
72bba93b SG |
1503 | /* Query the remote side for the text, data and bss offsets. */ |
1504 | ||
1505 | static void | |
1506 | get_offsets () | |
1507 | { | |
dd0ce8f6 AC |
1508 | char buf[PBUFSIZ], *ptr; |
1509 | int lose; | |
72bba93b SG |
1510 | CORE_ADDR text_addr, data_addr, bss_addr; |
1511 | struct section_offsets *offs; | |
1512 | ||
1513 | putpkt ("qOffsets"); | |
1514 | ||
1c95d7ab | 1515 | getpkt (buf, 0); |
72bba93b | 1516 | |
1c95d7ab JK |
1517 | if (buf[0] == '\000') |
1518 | return; /* Return silently. Stub doesn't support this | |
1519 | command. */ | |
72bba93b SG |
1520 | if (buf[0] == 'E') |
1521 | { | |
1522 | warning ("Remote failure reply: %s", buf); | |
1523 | return; | |
1524 | } | |
1525 | ||
dd0ce8f6 AC |
1526 | /* Pick up each field in turn. This used to be done with scanf, but |
1527 | scanf will make trouble if CORE_ADDR size doesn't match | |
1528 | conversion directives correctly. The following code will work | |
1529 | with any size of CORE_ADDR. */ | |
1530 | text_addr = data_addr = bss_addr = 0; | |
1531 | ptr = buf; | |
1532 | lose = 0; | |
1533 | ||
1534 | if (strncmp (ptr, "Text=", 5) == 0) | |
1535 | { | |
1536 | ptr += 5; | |
1537 | /* Don't use strtol, could lose on big values. */ | |
1538 | while (*ptr && *ptr != ';') | |
1539 | text_addr = (text_addr << 4) + fromhex (*ptr++); | |
1540 | } | |
1541 | else | |
1542 | lose = 1; | |
1543 | ||
1544 | if (!lose && strncmp (ptr, ";Data=", 6) == 0) | |
1545 | { | |
1546 | ptr += 6; | |
1547 | while (*ptr && *ptr != ';') | |
1548 | data_addr = (data_addr << 4) + fromhex (*ptr++); | |
1549 | } | |
1550 | else | |
1551 | lose = 1; | |
1552 | ||
1553 | if (!lose && strncmp (ptr, ";Bss=", 5) == 0) | |
1554 | { | |
1555 | ptr += 5; | |
1556 | while (*ptr && *ptr != ';') | |
1557 | bss_addr = (bss_addr << 4) + fromhex (*ptr++); | |
1558 | } | |
1559 | else | |
1560 | lose = 1; | |
1561 | ||
1562 | if (lose) | |
72bba93b SG |
1563 | error ("Malformed response to offset query, %s", buf); |
1564 | ||
1565 | if (symfile_objfile == NULL) | |
1566 | return; | |
1567 | ||
1568 | offs = (struct section_offsets *) alloca (sizeof (struct section_offsets) | |
1569 | + symfile_objfile->num_sections | |
1570 | * sizeof (offs->offsets)); | |
1571 | memcpy (offs, symfile_objfile->section_offsets, | |
1572 | sizeof (struct section_offsets) | |
1573 | + symfile_objfile->num_sections | |
1574 | * sizeof (offs->offsets)); | |
1575 | ||
1576 | ANOFFSET (offs, SECT_OFF_TEXT) = text_addr; | |
1624c38f SG |
1577 | |
1578 | /* This is a temporary kludge to force data and bss to use the same offsets | |
1579 | because that's what nlmconv does now. The real solution requires changes | |
1580 | to the stub and remote.c that I don't have time to do right now. */ | |
1581 | ||
72bba93b | 1582 | ANOFFSET (offs, SECT_OFF_DATA) = data_addr; |
1624c38f | 1583 | ANOFFSET (offs, SECT_OFF_BSS) = data_addr; |
72bba93b SG |
1584 | |
1585 | objfile_relocate (symfile_objfile, offs); | |
1586 | } | |
1587 | ||
7c622b41 JG |
1588 | /* Stub for catch_errors. */ |
1589 | ||
1590 | static int | |
1591 | remote_start_remote (dummy) | |
1592 | char *dummy; | |
1593 | { | |
ac7a377f JK |
1594 | immediate_quit = 1; /* Allow user to interrupt it */ |
1595 | ||
7c622b41 | 1596 | /* Ack any packet which the remote side has already sent. */ |
72bba93b SG |
1597 | SERIAL_WRITE (remote_desc, "+", 1); |
1598 | ||
4cc1b3f7 JK |
1599 | /* Let the stub know that we want it to return the thread. */ |
1600 | set_thread (-1, 0); | |
1601 | ||
72bba93b SG |
1602 | get_offsets (); /* Get text, data & bss offsets */ |
1603 | ||
7c622b41 | 1604 | putpkt ("?"); /* initiate a query from remote machine */ |
ac7a377f | 1605 | immediate_quit = 0; |
7c622b41 JG |
1606 | |
1607 | start_remote (); /* Initialize gdb process mechanisms */ | |
1608 | return 1; | |
1609 | } | |
1610 | ||
bd5635a1 RP |
1611 | /* Open a connection to a remote debugger. |
1612 | NAME is the filename used for communication. */ | |
1613 | ||
2b576293 C |
1614 | static void |
1615 | remote_open (name, from_tty) | |
1616 | char *name; | |
1617 | int from_tty; | |
1618 | { | |
dd0ce8f6 | 1619 | remote_open_1 (name, from_tty, &remote_ops, 0); |
2b576293 C |
1620 | } |
1621 | ||
1622 | /* Open a connection to a remote debugger using the extended | |
935e77f5 | 1623 | remote gdb protocol. NAME is the filename used for communication. */ |
2b576293 C |
1624 | |
1625 | static void | |
1626 | extended_remote_open (name, from_tty) | |
1627 | char *name; | |
1628 | int from_tty; | |
1629 | { | |
dd0ce8f6 | 1630 | remote_open_1 (name, from_tty, &extended_remote_ops, 1/*extended_p*/); |
2b576293 C |
1631 | } |
1632 | ||
1633 | /* Generic code for opening a connection to a remote target. */ | |
d538b510 RP |
1634 | static DCACHE *remote_dcache; |
1635 | ||
b543979c | 1636 | static void |
dd0ce8f6 | 1637 | remote_open_1 (name, from_tty, target, extended_p) |
bd5635a1 RP |
1638 | char *name; |
1639 | int from_tty; | |
2b576293 | 1640 | struct target_ops *target; |
dd0ce8f6 | 1641 | int extended_p; |
bd5635a1 | 1642 | { |
bd5635a1 | 1643 | if (name == 0) |
45993f61 | 1644 | error ("To open a remote debug connection, you need to specify what serial\n\ |
bd5635a1 RP |
1645 | device is attached to the remote system (e.g. /dev/ttya)."); |
1646 | ||
f2fc6e7a JK |
1647 | target_preopen (from_tty); |
1648 | ||
2b576293 | 1649 | unpush_target (target); |
bd5635a1 | 1650 | |
d538b510 | 1651 | remote_dcache = dcache_init (remote_read_bytes, remote_write_bytes); |
bd5635a1 | 1652 | |
ebdb9ade JK |
1653 | remote_desc = SERIAL_OPEN (name); |
1654 | if (!remote_desc) | |
bd5635a1 RP |
1655 | perror_with_name (name); |
1656 | ||
94d4b713 | 1657 | if (baud_rate != -1) |
b543979c | 1658 | { |
94d4b713 JK |
1659 | if (SERIAL_SETBAUDRATE (remote_desc, baud_rate)) |
1660 | { | |
1661 | SERIAL_CLOSE (remote_desc); | |
1662 | perror_with_name (name); | |
1663 | } | |
b543979c | 1664 | } |
ebdb9ade | 1665 | |
45993f61 | 1666 | |
ebdb9ade | 1667 | SERIAL_RAW (remote_desc); |
bd5635a1 | 1668 | |
e15f2a54 JK |
1669 | /* If there is something sitting in the buffer we might take it as a |
1670 | response to a command, which would be bad. */ | |
1671 | SERIAL_FLUSH_INPUT (remote_desc); | |
1672 | ||
bd5635a1 | 1673 | if (from_tty) |
7c622b41 JG |
1674 | { |
1675 | puts_filtered ("Remote debugging using "); | |
1676 | puts_filtered (name); | |
1677 | puts_filtered ("\n"); | |
1678 | } | |
2b576293 | 1679 | push_target (target); /* Switch to using remote target now */ |
4ef1f467 DT |
1680 | /* The target vector does not have the thread functions in it yet, |
1681 | so we use this function to call back into the thread module and | |
1682 | register the thread vector and its contained functions. */ | |
1683 | bind_target_thread_vector(&remote_thread_vec); | |
4aa6fe10 JK |
1684 | /* Start out by trying the 'P' request to set registers. We set this each |
1685 | time that we open a new target so that if the user switches from one | |
1686 | stub to another, we can (if the target is closed and reopened) cope. */ | |
1687 | stub_supports_P = 1; | |
1688 | ||
4cc1b3f7 JK |
1689 | general_thread = -2; |
1690 | cont_thread = -2; | |
1691 | ||
a1e0ba7a SG |
1692 | /* Without this, some commands which require an active target (such as kill) |
1693 | won't work. This variable serves (at least) double duty as both the pid | |
1694 | of the target process (if it has such), and as a flag indicating that a | |
1695 | target is active. These functions should be split out into seperate | |
1696 | variables, especially since GDB will someday have a notion of debugging | |
1697 | several processes. */ | |
1698 | ||
4fb7359d | 1699 | inferior_pid = 42000; |
4fb7359d SG |
1700 | /* Start the remote connection; if error (0), discard this target. |
1701 | In particular, if the user quits, be sure to discard it | |
1702 | (we'd be in an inconsistent state otherwise). */ | |
1703 | if (!catch_errors (remote_start_remote, (char *)0, | |
45993f61 | 1704 | "Couldn't establish connection to remote target\n", RETURN_MASK_ALL)) |
b85cf6ae MS |
1705 | { |
1706 | pop_target(); | |
1707 | return; | |
1708 | } | |
1709 | ||
1710 | if (extended_p) | |
1711 | { | |
1712 | /* tell the remote that we're using the extended protocol. */ | |
1713 | char buf[PBUFSIZ]; | |
1714 | putpkt ("!"); | |
1715 | getpkt (buf, 0); | |
1716 | } | |
1717 | } | |
1718 | ||
1719 | /* This takes a program previously attached to and detaches it. After | |
1720 | this is done, GDB can be used to debug some other program. We | |
1721 | better not have left any breakpoints in the target program or it'll | |
1722 | die when it hits one. */ | |
1723 | ||
1724 | static void | |
1725 | remote_detach (args, from_tty) | |
1726 | char *args; | |
1727 | int from_tty; | |
1728 | { | |
1729 | char buf[PBUFSIZ]; | |
1730 | ||
1731 | if (args) | |
1732 | error ("Argument given to \"detach\" when remotely debugging."); | |
1733 | ||
1734 | /* Tell the remote target to detach. */ | |
1735 | strcpy (buf, "D"); | |
1736 | remote_send (buf); | |
1737 | ||
1738 | pop_target (); | |
1739 | if (from_tty) | |
1740 | puts_filtered ("Ending remote debugging.\n"); | |
1741 | } | |
1742 | ||
1743 | /* Convert hex digit A to a number. */ | |
1744 | ||
1745 | int | |
1746 | fromhex (a) | |
1747 | int a; | |
1748 | { | |
1749 | if (a >= '0' && a <= '9') | |
1750 | return a - '0'; | |
1751 | else if (a >= 'a' && a <= 'f') | |
1752 | return a - 'a' + 10; | |
1753 | else if (a >= 'A' && a <= 'F') | |
1754 | return a - 'A' + 10; | |
1755 | else | |
1756 | error ("Reply contains invalid hex digit %d", a); | |
1757 | } | |
1758 | ||
1759 | /* Convert number NIB to a hex digit. */ | |
1760 | ||
1761 | static int | |
1762 | tohex (nib) | |
1763 | int nib; | |
1764 | { | |
1765 | if (nib < 10) | |
1766 | return '0'+nib; | |
1767 | else | |
1768 | return 'a'+nib-10; | |
1769 | } | |
1770 | \f | |
1771 | /* Tell the remote machine to resume. */ | |
1772 | ||
1773 | static enum target_signal last_sent_signal = TARGET_SIGNAL_0; | |
1774 | int last_sent_step; | |
1775 | ||
1776 | static void | |
1777 | remote_resume (pid, step, siggnal) | |
1778 | int pid, step; | |
1779 | enum target_signal siggnal; | |
1780 | { | |
1781 | char buf[PBUFSIZ]; | |
1782 | ||
1783 | if (pid == -1) | |
1784 | set_thread (inferior_pid, 0); | |
1785 | else | |
1786 | set_thread (pid, 0); | |
1787 | ||
1788 | dcache_flush (remote_dcache); | |
1789 | ||
1790 | last_sent_signal = siggnal; | |
1791 | last_sent_step = step; | |
1792 | ||
1793 | /* A hook for when we need to do something at the last moment before | |
1794 | resumption. */ | |
1795 | if (target_resume_hook) | |
1796 | (*target_resume_hook) (); | |
1797 | ||
1798 | if (siggnal != TARGET_SIGNAL_0) | |
1799 | { | |
1800 | buf[0] = step ? 'S' : 'C'; | |
1801 | buf[1] = tohex (((int)siggnal >> 4) & 0xf); | |
1802 | buf[2] = tohex ((int)siggnal & 0xf); | |
1803 | buf[3] = '\0'; | |
1804 | } | |
1805 | else | |
1806 | strcpy (buf, step ? "s": "c"); | |
1807 | ||
1808 | putpkt (buf); | |
1809 | } | |
1810 | \f | |
1811 | /* Send ^C to target to halt it. Target will respond, and send us a | |
1812 | packet. */ | |
2e7eeba9 | 1813 | static void (*ofunc) PARAMS ((int)); |
b85cf6ae MS |
1814 | |
1815 | static void | |
1816 | remote_interrupt (signo) | |
1817 | int signo; | |
1818 | { | |
2e7eeba9 MS |
1819 | remote_stop (); |
1820 | signal (signo, remote_interrupt); | |
b85cf6ae | 1821 | } |
2e7eeba9 | 1822 | |
b85cf6ae | 1823 | static void |
2e7eeba9 | 1824 | remote_stop () |
b85cf6ae | 1825 | { |
2e7eeba9 MS |
1826 | if (!interrupted_already) |
1827 | { | |
1828 | /* Send a break or a ^C, depending on user preference. */ | |
1829 | interrupted_already = 1; | |
b85cf6ae | 1830 | |
2e7eeba9 MS |
1831 | if (remote_debug) |
1832 | printf_unfiltered ("remote_stop called\n"); | |
1833 | ||
1834 | if (remote_break) | |
1835 | SERIAL_SEND_BREAK (remote_desc); | |
1836 | else | |
1837 | SERIAL_WRITE (remote_desc, "\003", 1); | |
1838 | } | |
1839 | else | |
1840 | { | |
1841 | signal (SIGINT, ofunc); | |
1842 | interrupt_query (); | |
1843 | signal (SIGINT, remote_interrupt); | |
1844 | interrupted_already = 0; | |
1845 | } | |
b85cf6ae MS |
1846 | } |
1847 | ||
1848 | /* Ask the user what to do when an interrupt is received. */ | |
1849 | ||
1850 | static void | |
1851 | interrupt_query () | |
1852 | { | |
1853 | target_terminal_ours (); | |
1854 | ||
1855 | if (query ("Interrupted while waiting for the program.\n\ | |
1856 | Give up (and stop debugging it)? ")) | |
1857 | { | |
1858 | target_mourn_inferior (); | |
1859 | return_to_top_level (RETURN_QUIT); | |
1860 | } | |
1861 | ||
1862 | target_terminal_inferior (); | |
1863 | } | |
1864 | ||
1865 | /* If nonzero, ignore the next kill. */ | |
1866 | int kill_kludge; | |
1867 | ||
1868 | void | |
1869 | remote_console_output (msg) | |
1870 | char *msg; | |
1871 | { | |
1872 | char *p; | |
1873 | ||
1874 | for (p = msg; *p; p +=2) | |
1875 | { | |
1876 | char tb[2]; | |
1877 | char c = fromhex (p[0]) * 16 + fromhex (p[1]); | |
1878 | tb[0] = c; | |
1879 | tb[1] = 0; | |
1880 | if (target_output_hook) | |
1881 | target_output_hook (tb); | |
1882 | else | |
1883 | fputs_filtered (tb, gdb_stdout); | |
1884 | } | |
1885 | } | |
1886 | ||
1887 | /* Wait until the remote machine stops, then return, | |
1888 | storing status in STATUS just as `wait' would. | |
1889 | Returns "pid" (though it's not clear what, if anything, that | |
1890 | means in the case of this target). */ | |
1891 | ||
1892 | static int | |
1893 | remote_wait (pid, status) | |
1894 | int pid; | |
1895 | struct target_waitstatus *status; | |
1896 | { | |
1897 | unsigned char buf[PBUFSIZ]; | |
1898 | int thread_num = -1; | |
1899 | ||
1900 | status->kind = TARGET_WAITKIND_EXITED; | |
1901 | status->value.integer = 0; | |
1902 | ||
1903 | while (1) | |
1904 | { | |
1905 | unsigned char *p; | |
1906 | ||
2e7eeba9 MS |
1907 | interrupted_already = 0; |
1908 | ofunc = signal (SIGINT, remote_interrupt); | |
b85cf6ae MS |
1909 | getpkt ((char *) buf, 1); |
1910 | signal (SIGINT, ofunc); | |
1911 | ||
1912 | /* This is a hook for when we need to do something (perhaps the | |
1913 | collection of trace data) every time the target stops. */ | |
1914 | if (target_wait_loop_hook) | |
1915 | (*target_wait_loop_hook) (); | |
1916 | ||
1917 | switch (buf[0]) | |
1918 | { | |
1919 | case 'E': /* Error of some sort */ | |
1920 | warning ("Remote failure reply: %s", buf); | |
1921 | continue; | |
1922 | case 'T': /* Status with PC, SP, FP, ... */ | |
1923 | { | |
1924 | int i; | |
1925 | long regno; | |
1926 | char regs[MAX_REGISTER_RAW_SIZE]; | |
1927 | ||
1928 | /* Expedited reply, containing Signal, {regno, reg} repeat */ | |
1929 | /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where | |
1930 | ss = signal number | |
1931 | n... = register number | |
1932 | r... = register contents | |
1933 | */ | |
1934 | p = &buf[3]; /* after Txx */ | |
1935 | ||
1936 | while (*p) | |
1937 | { | |
1938 | unsigned char *p1; | |
1939 | char *p_temp; | |
1940 | ||
1941 | regno = strtol ((const char *) p, &p_temp, 16); /* Read the register number */ | |
1942 | p1 = (unsigned char *)p_temp; | |
1943 | ||
4ef1f467 | 1944 | if (p1 == p) /* No register number present here */ |
b85cf6ae MS |
1945 | { |
1946 | p1 = (unsigned char *) strchr ((const char *) p, ':'); | |
1947 | if (p1 == NULL) | |
4ef1f467 | 1948 | warning ("Malformed packet(a) (missing colon): %s\n\ |
b85cf6ae MS |
1949 | Packet: '%s'\n", |
1950 | p, buf); | |
1951 | if (strncmp ((const char *) p, "thread", p1 - p) == 0) | |
1952 | { | |
4ef1f467 DT |
1953 | p_temp = unpack_varlen_hex(++p1,&thread_num); |
1954 | record_currthread(thread_num); | |
b85cf6ae MS |
1955 | p = (unsigned char *)p_temp; |
1956 | } | |
1957 | } | |
1958 | else | |
1959 | { | |
1960 | p = p1; | |
1961 | ||
1962 | if (*p++ != ':') | |
4ef1f467 | 1963 | warning ("Malformed packet(b) (missing colon): %s\n\ |
b85cf6ae MS |
1964 | Packet: '%s'\n", |
1965 | p, buf); | |
1966 | ||
1967 | if (regno >= NUM_REGS) | |
1968 | warning ("Remote sent bad register number %ld: %s\n\ | |
1969 | Packet: '%s'\n", | |
1970 | regno, p, buf); | |
1971 | ||
1972 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i++) | |
1973 | { | |
1974 | if (p[0] == 0 || p[1] == 0) | |
1975 | warning ("Remote reply is too short: %s", buf); | |
1976 | regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]); | |
1977 | p += 2; | |
1978 | } | |
1979 | supply_register (regno, regs); | |
1980 | } | |
1981 | ||
1982 | if (*p++ != ';') | |
4ef1f467 DT |
1983 | { |
1984 | warning ("Remote register badly formatted: %s", buf); | |
1985 | warning (" here: %s",p); | |
1986 | } | |
b85cf6ae MS |
1987 | } |
1988 | } | |
1989 | /* fall through */ | |
1990 | case 'S': /* Old style status, just signal only */ | |
1991 | status->kind = TARGET_WAITKIND_STOPPED; | |
1992 | status->value.sig = (enum target_signal) | |
1993 | (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))); | |
1994 | ||
1995 | goto got_status; | |
1996 | case 'W': /* Target exited */ | |
1997 | { | |
1998 | /* The remote process exited. */ | |
1999 | status->kind = TARGET_WAITKIND_EXITED; | |
2000 | status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]); | |
2001 | goto got_status; | |
2002 | } | |
2003 | case 'X': | |
2004 | status->kind = TARGET_WAITKIND_SIGNALLED; | |
2005 | status->value.sig = (enum target_signal) | |
2006 | (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))); | |
2007 | kill_kludge = 1; | |
2008 | ||
2009 | goto got_status; | |
2010 | case 'O': /* Console output */ | |
2011 | remote_console_output (buf + 1); | |
2012 | continue; | |
2013 | case '\0': | |
2014 | if (last_sent_signal != TARGET_SIGNAL_0) | |
2015 | { | |
2016 | /* Zero length reply means that we tried 'S' or 'C' and | |
2017 | the remote system doesn't support it. */ | |
2018 | target_terminal_ours_for_output (); | |
2019 | printf_filtered | |
2020 | ("Can't send signals to this remote system. %s not sent.\n", | |
2021 | target_signal_to_name (last_sent_signal)); | |
2022 | last_sent_signal = TARGET_SIGNAL_0; | |
2023 | target_terminal_inferior (); | |
2024 | ||
2025 | strcpy ((char *) buf, last_sent_step ? "s" : "c"); | |
2026 | putpkt ((char *) buf); | |
2027 | continue; | |
2028 | } | |
2029 | /* else fallthrough */ | |
2030 | default: | |
2031 | warning ("Invalid remote reply: %s", buf); | |
2032 | continue; | |
2033 | } | |
2034 | } | |
2035 | got_status: | |
2036 | if (thread_num != -1) | |
2037 | { | |
2038 | /* Initial thread value can only be acquired via wait, so deal with | |
2039 | this marker which is used before the first thread value is | |
2040 | acquired. */ | |
2041 | if (inferior_pid == 42000) | |
2042 | { | |
2043 | inferior_pid = thread_num; | |
2044 | add_thread (inferior_pid); | |
2045 | } | |
2046 | return thread_num; | |
2047 | } | |
2048 | return inferior_pid; | |
2049 | } | |
2050 | ||
2051 | /* Number of bytes of registers this stub implements. */ | |
2052 | static int register_bytes_found; | |
2053 | ||
2054 | /* Read the remote registers into the block REGS. */ | |
2055 | /* Currently we just read all the registers, so we don't use regno. */ | |
2056 | /* ARGSUSED */ | |
2057 | static void | |
2058 | remote_fetch_registers (regno) | |
2059 | int regno; | |
2060 | { | |
2061 | char buf[PBUFSIZ]; | |
2062 | int i; | |
2063 | char *p; | |
2064 | char regs[REGISTER_BYTES]; | |
2065 | ||
2066 | set_thread (inferior_pid, 1); | |
2067 | ||
2068 | sprintf (buf, "g"); | |
2069 | remote_send (buf); | |
2070 | ||
2071 | if (remote_register_buf_size == 0) | |
2072 | remote_register_buf_size = strlen (buf); | |
2073 | ||
2074 | /* Unimplemented registers read as all bits zero. */ | |
2075 | memset (regs, 0, REGISTER_BYTES); | |
2076 | ||
2077 | /* We can get out of synch in various cases. If the first character | |
2078 | in the buffer is not a hex character, assume that has happened | |
2079 | and try to fetch another packet to read. */ | |
2080 | while ((buf[0] < '0' || buf[0] > '9') | |
2e7eeba9 MS |
2081 | && (buf[0] < 'a' || buf[0] > 'f') |
2082 | && buf[0] != 'x') /* New: unavailable register value */ | |
b85cf6ae MS |
2083 | { |
2084 | if (remote_debug) | |
2085 | printf_unfiltered ("Bad register packet; fetching a new packet\n"); | |
2086 | getpkt (buf, 0); | |
2087 | } | |
2088 | ||
2089 | /* Reply describes registers byte by byte, each byte encoded as two | |
2090 | hex characters. Suck them all up, then supply them to the | |
2091 | register cacheing/storage mechanism. */ | |
2092 | ||
2093 | p = buf; | |
2094 | for (i = 0; i < REGISTER_BYTES; i++) | |
2095 | { | |
2096 | if (p[0] == 0) | |
2097 | break; | |
2098 | if (p[1] == 0) | |
2099 | { | |
2100 | warning ("Remote reply is of odd length: %s", buf); | |
2101 | /* Don't change register_bytes_found in this case, and don't | |
2102 | print a second warning. */ | |
2103 | goto supply_them; | |
2104 | } | |
2e7eeba9 MS |
2105 | if (p[0] == 'x' && p[1] == 'x') |
2106 | regs[i] = 0; /* 'x' */ | |
2107 | else | |
2108 | regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]); | |
b85cf6ae MS |
2109 | p += 2; |
2110 | } | |
2111 | ||
2112 | if (i != register_bytes_found) | |
2113 | { | |
2114 | register_bytes_found = i; | |
2115 | #ifdef REGISTER_BYTES_OK | |
2116 | if (!REGISTER_BYTES_OK (i)) | |
2117 | warning ("Remote reply is too short: %s", buf); | |
2118 | #endif | |
2119 | } | |
2e7eeba9 MS |
2120 | |
2121 | supply_them: | |
b85cf6ae | 2122 | for (i = 0; i < NUM_REGS; i++) |
2e7eeba9 | 2123 | { |
b85cf6ae | 2124 | supply_register (i, ®s[REGISTER_BYTE(i)]); |
2e7eeba9 MS |
2125 | if (buf[REGISTER_BYTE(i) * 2] == 'x') |
2126 | register_valid[i] = -1; /* register value not available */ | |
2127 | } | |
b85cf6ae MS |
2128 | } |
2129 | ||
2130 | /* Prepare to store registers. Since we may send them all (using a | |
2131 | 'G' request), we have to read out the ones we don't want to change | |
2132 | first. */ | |
2133 | ||
2134 | static void | |
2135 | remote_prepare_to_store () | |
2136 | { | |
2137 | /* Make sure the entire registers array is valid. */ | |
2138 | read_register_bytes (0, (char *)NULL, REGISTER_BYTES); | |
2139 | } | |
2140 | ||
2141 | /* Store register REGNO, or all registers if REGNO == -1, from the contents | |
2142 | of REGISTERS. FIXME: ignores errors. */ | |
2143 | ||
2144 | static void | |
2145 | remote_store_registers (regno) | |
2146 | int regno; | |
2147 | { | |
2148 | char buf[PBUFSIZ]; | |
2149 | int i; | |
2150 | char *p; | |
2151 | ||
2152 | set_thread (inferior_pid, 1); | |
2153 | ||
2154 | if (regno >= 0 && stub_supports_P) | |
2155 | { | |
2156 | /* Try storing a single register. */ | |
2157 | char *regp; | |
2158 | ||
2159 | sprintf (buf, "P%x=", regno); | |
2160 | p = buf + strlen (buf); | |
2161 | regp = ®isters[REGISTER_BYTE (regno)]; | |
2162 | for (i = 0; i < REGISTER_RAW_SIZE (regno); ++i) | |
2163 | { | |
2164 | *p++ = tohex ((regp[i] >> 4) & 0xf); | |
2165 | *p++ = tohex (regp[i] & 0xf); | |
2166 | } | |
2167 | *p = '\0'; | |
2168 | remote_send (buf); | |
2169 | if (buf[0] != '\0') | |
2170 | { | |
2171 | /* The stub understands the 'P' request. We are done. */ | |
2172 | return; | |
2173 | } | |
2174 | ||
2175 | /* The stub does not support the 'P' request. Use 'G' instead, | |
2176 | and don't try using 'P' in the future (it will just waste our | |
2177 | time). */ | |
2178 | stub_supports_P = 0; | |
2179 | } | |
2180 | ||
2181 | buf[0] = 'G'; | |
2182 | ||
2183 | /* Command describes registers byte by byte, | |
2184 | each byte encoded as two hex characters. */ | |
2185 | ||
2186 | p = buf + 1; | |
2187 | /* remote_prepare_to_store insures that register_bytes_found gets set. */ | |
2188 | for (i = 0; i < register_bytes_found; i++) | |
2189 | { | |
2190 | *p++ = tohex ((registers[i] >> 4) & 0xf); | |
2191 | *p++ = tohex (registers[i] & 0xf); | |
2192 | } | |
2193 | *p = '\0'; | |
2194 | ||
2195 | remote_send (buf); | |
2196 | } | |
2197 | ||
2198 | /* | |
2199 | Use of the data cache *used* to be disabled because it loses for looking at | |
2200 | and changing hardware I/O ports and the like. Accepting `volatile' | |
2201 | would perhaps be one way to fix it. Another idea would be to use the | |
2202 | executable file for the text segment (for all SEC_CODE sections? | |
2203 | For all SEC_READONLY sections?). This has problems if you want to | |
2204 | actually see what the memory contains (e.g. self-modifying code, | |
2205 | clobbered memory, user downloaded the wrong thing). | |
2206 | ||
2207 | Because it speeds so much up, it's now enabled, if you're playing | |
2208 | with registers you turn it of (set remotecache 0) | |
2209 | */ | |
2210 | ||
2211 | /* Read a word from remote address ADDR and return it. | |
2212 | This goes through the data cache. */ | |
2213 | ||
2214 | #if 0 /* unused? */ | |
2215 | static int | |
2216 | remote_fetch_word (addr) | |
2217 | CORE_ADDR addr; | |
2218 | { | |
2219 | return dcache_fetch (remote_dcache, addr); | |
2220 | } | |
2221 | ||
2222 | /* Write a word WORD into remote address ADDR. | |
2223 | This goes through the data cache. */ | |
2224 | ||
2225 | static void | |
2226 | remote_store_word (addr, word) | |
2227 | CORE_ADDR addr; | |
2228 | int word; | |
2229 | { | |
2230 | dcache_poke (remote_dcache, addr, word); | |
2231 | } | |
2232 | #endif /* 0 (unused?) */ | |
2233 | ||
2234 | \f | |
2235 | ||
2236 | /* Return the number of hex digits in num. */ | |
2237 | ||
2238 | static int | |
2239 | hexnumlen (num) | |
2240 | ULONGEST num; | |
2241 | { | |
2242 | int i; | |
2243 | ||
2244 | for (i = 0; num != 0; i++) | |
2245 | num >>= 4; | |
2246 | ||
2247 | return max (i, 1); | |
2248 | } | |
2249 | ||
2e7eeba9 MS |
2250 | /* Set BUF to the hex digits representing NUM */ |
2251 | ||
2252 | static int | |
2253 | hexnumstr (buf, num) | |
2254 | char *buf; | |
2255 | ULONGEST num; | |
2256 | { | |
2257 | int i; | |
2258 | int len = hexnumlen (num); | |
2259 | ||
2260 | buf[len] = '\0'; | |
2261 | ||
2262 | for (i = len - 1; i >= 0; i--) | |
2263 | { | |
2264 | buf[i] = "0123456789abcdef" [(num & 0xf)]; | |
2265 | num >>= 4; | |
2266 | } | |
2267 | ||
2268 | return len; | |
2269 | } | |
2270 | ||
2271 | /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits */ | |
2272 | ||
2273 | static CORE_ADDR | |
2274 | remote_address_masked (addr) | |
2275 | CORE_ADDR addr; | |
2276 | { | |
2277 | if (remote_address_size > 0 | |
2278 | && remote_address_size < (sizeof (ULONGEST) * 8)) | |
2279 | { | |
2280 | /* Only create a mask when that mask can safely be constructed | |
2281 | in a ULONGEST variable. */ | |
2282 | ULONGEST mask = 1; | |
2283 | mask = (mask << remote_address_size) - 1; | |
2284 | addr &= mask; | |
2285 | } | |
2286 | return addr; | |
2287 | } | |
2288 | ||
b85cf6ae MS |
2289 | /* Write memory data directly to the remote machine. |
2290 | This does not inform the data cache; the data cache uses this. | |
2291 | MEMADDR is the address in the remote memory space. | |
2292 | MYADDR is the address of the buffer in our space. | |
2293 | LEN is the number of bytes. | |
2294 | ||
2295 | Returns number of bytes transferred, or 0 for error. */ | |
2296 | ||
2297 | static int | |
2298 | remote_write_bytes (memaddr, myaddr, len) | |
2299 | CORE_ADDR memaddr; | |
2300 | char *myaddr; | |
2301 | int len; | |
2302 | { | |
2303 | int max_buf_size; /* Max size of packet output buffer */ | |
2304 | int origlen; | |
2305 | ||
2306 | /* Chop the transfer down if necessary */ | |
2307 | ||
2308 | max_buf_size = min (remote_write_size, PBUFSIZ); | |
2309 | if (remote_register_buf_size != 0) | |
2310 | max_buf_size = min (max_buf_size, remote_register_buf_size); | |
2311 | ||
2312 | /* Subtract header overhead from max payload size - $M<memaddr>,<len>:#nn */ | |
2313 | max_buf_size -= 2 + hexnumlen (memaddr + len - 1) + 1 + hexnumlen (len) + 4; | |
2314 | ||
2315 | origlen = len; | |
2316 | while (len > 0) | |
2317 | { | |
2318 | char buf[PBUFSIZ]; | |
2319 | char *p; | |
2320 | int todo; | |
2321 | int i; | |
2322 | ||
2323 | todo = min (len, max_buf_size / 2); /* num bytes that will fit */ | |
2324 | ||
2e7eeba9 MS |
2325 | /* construct "M"<memaddr>","<len>":" */ |
2326 | /* sprintf (buf, "M%lx,%x:", (unsigned long) memaddr, todo); */ | |
2327 | memaddr = remote_address_masked (memaddr); | |
2328 | p = buf; | |
2329 | *p++ = 'M'; | |
2330 | p += hexnumstr (p, (ULONGEST) memaddr); | |
2331 | *p++ = ','; | |
2332 | p += hexnumstr (p, (ULONGEST) todo); | |
2333 | *p++ = ':'; | |
2334 | *p = '\0'; | |
b85cf6ae MS |
2335 | |
2336 | /* We send target system values byte by byte, in increasing byte addresses, | |
2337 | each byte encoded as two hex characters. */ | |
2338 | ||
b85cf6ae MS |
2339 | for (i = 0; i < todo; i++) |
2340 | { | |
2341 | *p++ = tohex ((myaddr[i] >> 4) & 0xf); | |
2342 | *p++ = tohex (myaddr[i] & 0xf); | |
2343 | } | |
2344 | *p = '\0'; | |
2345 | ||
2346 | putpkt (buf); | |
2347 | getpkt (buf, 0); | |
2348 | ||
2349 | if (buf[0] == 'E') | |
2350 | { | |
2351 | /* There is no correspondance between what the remote protocol uses | |
2352 | for errors and errno codes. We would like a cleaner way of | |
2353 | representing errors (big enough to include errno codes, bfd_error | |
2354 | codes, and others). But for now just return EIO. */ | |
2355 | errno = EIO; | |
2356 | return 0; | |
2357 | } | |
2358 | myaddr += todo; | |
2359 | memaddr += todo; | |
2360 | len -= todo; | |
2361 | } | |
2362 | return origlen; | |
2363 | } | |
2364 | ||
2365 | /* Read memory data directly from the remote machine. | |
2366 | This does not use the data cache; the data cache uses this. | |
2367 | MEMADDR is the address in the remote memory space. | |
2368 | MYADDR is the address of the buffer in our space. | |
2369 | LEN is the number of bytes. | |
2370 | ||
2371 | Returns number of bytes transferred, or 0 for error. */ | |
2372 | ||
2373 | static int | |
2374 | remote_read_bytes (memaddr, myaddr, len) | |
2375 | CORE_ADDR memaddr; | |
2376 | char *myaddr; | |
2377 | int len; | |
2378 | { | |
2379 | int max_buf_size; /* Max size of packet output buffer */ | |
2380 | int origlen; | |
2381 | ||
2382 | /* Chop the transfer down if necessary */ | |
2383 | ||
2384 | max_buf_size = min (remote_write_size, PBUFSIZ); | |
2385 | if (remote_register_buf_size != 0) | |
2386 | max_buf_size = min (max_buf_size, remote_register_buf_size); | |
2387 | ||
2388 | origlen = len; | |
2389 | while (len > 0) | |
2390 | { | |
2391 | char buf[PBUFSIZ]; | |
2392 | char *p; | |
2393 | int todo; | |
2394 | int i; | |
2395 | ||
2396 | todo = min (len, max_buf_size / 2); /* num bytes that will fit */ | |
2397 | ||
2e7eeba9 MS |
2398 | /* construct "m"<memaddr>","<len>" */ |
2399 | /* sprintf (buf, "m%lx,%x", (unsigned long) memaddr, todo); */ | |
2400 | memaddr = remote_address_masked (memaddr); | |
2401 | p = buf; | |
2402 | *p++ = 'm'; | |
2403 | p += hexnumstr (p, (ULONGEST) memaddr); | |
2404 | *p++ = ','; | |
2405 | p += hexnumstr (p, (ULONGEST) todo); | |
2406 | *p = '\0'; | |
2407 | ||
b85cf6ae MS |
2408 | putpkt (buf); |
2409 | getpkt (buf, 0); | |
2410 | ||
2411 | if (buf[0] == 'E') | |
2412 | { | |
2413 | /* There is no correspondance between what the remote protocol uses | |
2414 | for errors and errno codes. We would like a cleaner way of | |
2415 | representing errors (big enough to include errno codes, bfd_error | |
2416 | codes, and others). But for now just return EIO. */ | |
2417 | errno = EIO; | |
2418 | return 0; | |
2419 | } | |
2420 | ||
2421 | /* Reply describes memory byte by byte, | |
2422 | each byte encoded as two hex characters. */ | |
2423 | ||
2424 | p = buf; | |
2425 | for (i = 0; i < todo; i++) | |
2426 | { | |
2427 | if (p[0] == 0 || p[1] == 0) | |
2428 | /* Reply is short. This means that we were able to read only part | |
2429 | of what we wanted to. */ | |
2430 | return i + (origlen - len); | |
2431 | myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]); | |
2432 | p += 2; | |
2433 | } | |
2434 | myaddr += todo; | |
2435 | memaddr += todo; | |
2436 | len -= todo; | |
2437 | } | |
2438 | return origlen; | |
2439 | } | |
2440 | \f | |
2441 | /* Read or write LEN bytes from inferior memory at MEMADDR, transferring | |
2442 | to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is | |
2443 | nonzero. Returns length of data written or read; 0 for error. */ | |
2444 | ||
2445 | /* ARGSUSED */ | |
2446 | static int | |
2447 | remote_xfer_memory(memaddr, myaddr, len, should_write, target) | |
2448 | CORE_ADDR memaddr; | |
2449 | char *myaddr; | |
2450 | int len; | |
2451 | int should_write; | |
2452 | struct target_ops *target; /* ignored */ | |
2453 | { | |
2454 | #ifdef REMOTE_TRANSLATE_XFER_ADDRESS | |
2455 | CORE_ADDR targaddr; | |
2456 | int targlen; | |
2457 | REMOTE_TRANSLATE_XFER_ADDRESS (memaddr, len, targaddr, targlen); | |
2458 | if (targlen == 0) | |
2459 | return 0; | |
2460 | memaddr = targaddr; | |
2461 | len = targlen; | |
2462 | #endif | |
2463 | ||
2464 | return dcache_xfer_memory (remote_dcache, memaddr, myaddr, len, should_write); | |
2465 | } | |
2466 | ||
2467 | ||
2468 | #if 0 | |
2469 | /* Enable after 4.12. */ | |
2470 | ||
2471 | void | |
2472 | remote_search (len, data, mask, startaddr, increment, lorange, hirange | |
2473 | addr_found, data_found) | |
2474 | int len; | |
2475 | char *data; | |
2476 | char *mask; | |
2477 | CORE_ADDR startaddr; | |
2478 | int increment; | |
2479 | CORE_ADDR lorange; | |
2480 | CORE_ADDR hirange; | |
2481 | CORE_ADDR *addr_found; | |
2482 | char *data_found; | |
2483 | { | |
2484 | if (increment == -4 && len == 4) | |
2485 | { | |
2486 | long mask_long, data_long; | |
2487 | long data_found_long; | |
2488 | CORE_ADDR addr_we_found; | |
2489 | char buf[PBUFSIZ]; | |
2490 | long returned_long[2]; | |
2491 | char *p; | |
2492 | ||
2493 | mask_long = extract_unsigned_integer (mask, len); | |
2494 | data_long = extract_unsigned_integer (data, len); | |
2495 | sprintf (buf, "t%x:%x,%x", startaddr, data_long, mask_long); | |
2496 | putpkt (buf); | |
2497 | getpkt (buf, 0); | |
2498 | if (buf[0] == '\0') | |
2499 | { | |
2500 | /* The stub doesn't support the 't' request. We might want to | |
2501 | remember this fact, but on the other hand the stub could be | |
2502 | switched on us. Maybe we should remember it only until | |
2503 | the next "target remote". */ | |
2504 | generic_search (len, data, mask, startaddr, increment, lorange, | |
2505 | hirange, addr_found, data_found); | |
2506 | return; | |
2507 | } | |
2508 | ||
2509 | if (buf[0] == 'E') | |
2510 | /* There is no correspondance between what the remote protocol uses | |
2511 | for errors and errno codes. We would like a cleaner way of | |
2512 | representing errors (big enough to include errno codes, bfd_error | |
2513 | codes, and others). But for now just use EIO. */ | |
2514 | memory_error (EIO, startaddr); | |
2515 | p = buf; | |
2516 | addr_we_found = 0; | |
2517 | while (*p != '\0' && *p != ',') | |
2518 | addr_we_found = (addr_we_found << 4) + fromhex (*p++); | |
2519 | if (*p == '\0') | |
2520 | error ("Protocol error: short return for search"); | |
2521 | ||
2522 | data_found_long = 0; | |
2523 | while (*p != '\0' && *p != ',') | |
2524 | data_found_long = (data_found_long << 4) + fromhex (*p++); | |
2525 | /* Ignore anything after this comma, for future extensions. */ | |
2526 | ||
2527 | if (addr_we_found < lorange || addr_we_found >= hirange) | |
2528 | { | |
2529 | *addr_found = 0; | |
2530 | return; | |
2531 | } | |
2532 | ||
2533 | *addr_found = addr_we_found; | |
2534 | *data_found = store_unsigned_integer (data_we_found, len); | |
2535 | return; | |
2536 | } | |
2537 | generic_search (len, data, mask, startaddr, increment, lorange, | |
2538 | hirange, addr_found, data_found); | |
2539 | } | |
2540 | #endif /* 0 */ | |
2541 | \f | |
2542 | static void | |
2543 | remote_files_info (ignore) | |
2544 | struct target_ops *ignore; | |
2545 | { | |
2546 | puts_filtered ("Debugging a target over a serial line.\n"); | |
2547 | } | |
2548 | \f | |
2549 | /* Stuff for dealing with the packets which are part of this protocol. | |
2550 | See comment at top of file for details. */ | |
2551 | ||
2552 | /* Read a single character from the remote end, masking it down to 7 bits. */ | |
2553 | ||
2554 | static int | |
2555 | readchar (timeout) | |
2556 | int timeout; | |
2557 | { | |
2558 | int ch; | |
2559 | ||
2560 | ch = SERIAL_READCHAR (remote_desc, timeout); | |
2561 | ||
2562 | switch (ch) | |
2563 | { | |
2564 | case SERIAL_EOF: | |
2565 | error ("Remote connection closed"); | |
2566 | case SERIAL_ERROR: | |
2567 | perror_with_name ("Remote communication error"); | |
2568 | case SERIAL_TIMEOUT: | |
2569 | return ch; | |
2570 | default: | |
2571 | return ch & 0x7f; | |
2572 | } | |
2573 | } | |
2574 | ||
2575 | /* Send the command in BUF to the remote machine, | |
2576 | and read the reply into BUF. | |
2577 | Report an error if we get an error reply. */ | |
2578 | ||
2579 | static void | |
2580 | remote_send (buf) | |
2581 | char *buf; | |
2582 | { | |
2583 | putpkt (buf); | |
2584 | getpkt (buf, 0); | |
2585 | ||
2586 | if (buf[0] == 'E') | |
2587 | error ("Remote failure reply: %s", buf); | |
2588 | } | |
2589 | ||
ecc6e842 EZ |
2590 | /* Display a null-terminated packet on stdout, for debugging, using C |
2591 | string notation. */ | |
f8059b97 | 2592 | |
ecc6e842 | 2593 | static void |
f8059b97 AC |
2594 | print_packet (buf) |
2595 | char *buf; | |
ecc6e842 EZ |
2596 | { |
2597 | puts_filtered ("\""); | |
2598 | while (*buf) | |
2599 | gdb_printchar (*buf++, gdb_stdout, '"'); | |
2600 | puts_filtered ("\""); | |
2601 | } | |
2602 | ||
2603 | ||
b85cf6ae MS |
2604 | /* Send a packet to the remote machine, with error checking. |
2605 | The data of the packet is in BUF. */ | |
2606 | ||
2607 | int | |
2608 | putpkt (buf) | |
2609 | char *buf; | |
2610 | { | |
2611 | int i; | |
2612 | unsigned char csum = 0; | |
2613 | char buf2[PBUFSIZ]; | |
2614 | int cnt = strlen (buf); | |
2615 | int ch; | |
2616 | int tcount = 0; | |
2617 | char *p; | |
2618 | ||
2619 | /* Copy the packet into buffer BUF2, encapsulating it | |
2620 | and giving it a checksum. */ | |
2621 | ||
2622 | if (cnt > (int) sizeof (buf2) - 5) /* Prosanity check */ | |
2623 | abort(); | |
2624 | ||
2625 | p = buf2; | |
2626 | *p++ = '$'; | |
2627 | ||
2628 | for (i = 0; i < cnt; i++) | |
2629 | { | |
2630 | csum += buf[i]; | |
2631 | *p++ = buf[i]; | |
2632 | } | |
2633 | *p++ = '#'; | |
2634 | *p++ = tohex ((csum >> 4) & 0xf); | |
2635 | *p++ = tohex (csum & 0xf); | |
2636 | ||
2637 | /* Send it over and over until we get a positive ack. */ | |
2638 | ||
2639 | while (1) | |
2640 | { | |
2641 | int started_error_output = 0; | |
2642 | ||
2643 | if (remote_debug) | |
2644 | { | |
2645 | *p = '\0'; | |
2646 | printf_unfiltered ("Sending packet: %s...", buf2); | |
2647 | gdb_flush(gdb_stdout); | |
2648 | } | |
2649 | if (SERIAL_WRITE (remote_desc, buf2, p - buf2)) | |
2650 | perror_with_name ("putpkt: write failed"); | |
2651 | ||
2652 | /* read until either a timeout occurs (-2) or '+' is read */ | |
2653 | while (1) | |
2654 | { | |
2655 | ch = readchar (remote_timeout); | |
2656 | ||
2657 | if (remote_debug) | |
2658 | { | |
2659 | switch (ch) | |
2660 | { | |
2661 | case '+': | |
2662 | case SERIAL_TIMEOUT: | |
2663 | case '$': | |
2664 | if (started_error_output) | |
2665 | { | |
2666 | putchar_unfiltered ('\n'); | |
2667 | started_error_output = 0; | |
2668 | } | |
2669 | } | |
2670 | } | |
2671 | ||
2672 | switch (ch) | |
2673 | { | |
2674 | case '+': | |
2675 | if (remote_debug) | |
2676 | printf_unfiltered("Ack\n"); | |
2677 | return 1; | |
2678 | case SERIAL_TIMEOUT: | |
2679 | tcount ++; | |
2680 | if (tcount > 3) | |
2681 | return 0; | |
2682 | break; /* Retransmit buffer */ | |
2683 | case '$': | |
2684 | { | |
2685 | char junkbuf[PBUFSIZ]; | |
2686 | ||
2687 | /* It's probably an old response, and we're out of sync. Just | |
2688 | gobble up the packet and ignore it. */ | |
2689 | getpkt (junkbuf, 0); | |
2690 | continue; /* Now, go look for + */ | |
2691 | } | |
2692 | default: | |
2693 | if (remote_debug) | |
2694 | { | |
2695 | if (!started_error_output) | |
2696 | { | |
2697 | started_error_output = 1; | |
2698 | printf_unfiltered ("putpkt: Junk: "); | |
2699 | } | |
2700 | putchar_unfiltered (ch & 0177); | |
2701 | } | |
2702 | continue; | |
2703 | } | |
2704 | break; /* Here to retransmit */ | |
2705 | } | |
2706 | ||
2707 | #if 0 | |
2708 | /* This is wrong. If doing a long backtrace, the user should be | |
2709 | able to get out next time we call QUIT, without anything as violent | |
2710 | as interrupt_query. If we want to provide a way out of here | |
2711 | without getting to the next QUIT, it should be based on hitting | |
2712 | ^C twice as in remote_wait. */ | |
2713 | if (quit_flag) | |
2714 | { | |
2715 | quit_flag = 0; | |
2716 | interrupt_query (); | |
2717 | } | |
2718 | #endif | |
2719 | } | |
2720 | } | |
2721 | ||
2722 | /* Come here after finding the start of the frame. Collect the rest into BUF, | |
2723 | verifying the checksum, length, and handling run-length compression. | |
2724 | Returns 0 on any error, 1 on success. */ | |
2725 | ||
2726 | static int | |
2727 | read_frame (buf) | |
2728 | char *buf; | |
2729 | { | |
2730 | unsigned char csum; | |
2731 | char *bp; | |
2732 | int c; | |
2733 | ||
2734 | csum = 0; | |
2735 | bp = buf; | |
2736 | ||
2737 | while (1) | |
2738 | { | |
2739 | c = readchar (remote_timeout); | |
2740 | ||
2741 | switch (c) | |
2742 | { | |
2743 | case SERIAL_TIMEOUT: | |
2744 | if (remote_debug) | |
2745 | puts_filtered ("Timeout in mid-packet, retrying\n"); | |
2746 | return 0; | |
2747 | case '$': | |
2748 | if (remote_debug) | |
2749 | puts_filtered ("Saw new packet start in middle of old one\n"); | |
2750 | return 0; /* Start a new packet, count retries */ | |
2751 | case '#': | |
2752 | { | |
2753 | unsigned char pktcsum; | |
2754 | ||
2755 | *bp = '\000'; | |
2756 | ||
2757 | pktcsum = fromhex (readchar (remote_timeout)) << 4; | |
2758 | pktcsum |= fromhex (readchar (remote_timeout)); | |
2759 | ||
2760 | if (csum == pktcsum) | |
2761 | return 1; | |
2762 | ||
2763 | if (remote_debug) | |
2764 | { | |
2765 | printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=", | |
2766 | pktcsum, csum); | |
2767 | puts_filtered (buf); | |
2768 | puts_filtered ("\n"); | |
2769 | } | |
2770 | return 0; | |
2771 | } | |
2772 | case '*': /* Run length encoding */ | |
2773 | csum += c; | |
2774 | c = readchar (remote_timeout); | |
2775 | csum += c; | |
2776 | c = c - ' ' + 3; /* Compute repeat count */ | |
2777 | ||
2778 | ||
2779 | if (c > 0 && c < 255 && bp + c - 1 < buf + PBUFSIZ - 1) | |
2780 | { | |
2781 | memset (bp, *(bp - 1), c); | |
2782 | bp += c; | |
2783 | continue; | |
2784 | } | |
2785 | ||
2786 | *bp = '\0'; | |
2787 | printf_filtered ("Repeat count %d too large for buffer: ", c); | |
2788 | puts_filtered (buf); | |
2789 | puts_filtered ("\n"); | |
2790 | return 0; | |
2791 | ||
2792 | default: | |
2793 | if (bp < buf + PBUFSIZ - 1) | |
2794 | { | |
2795 | *bp++ = c; | |
2796 | csum += c; | |
2797 | continue; | |
2798 | } | |
2799 | ||
2800 | *bp = '\0'; | |
2801 | puts_filtered ("Remote packet too long: "); | |
2802 | puts_filtered (buf); | |
2803 | puts_filtered ("\n"); | |
2804 | ||
2805 | return 0; | |
2806 | } | |
2807 | } | |
2808 | } | |
2809 | ||
4ef1f467 DT |
2810 | |
2811 | ||
b85cf6ae MS |
2812 | /* Read a packet from the remote machine, with error checking, |
2813 | and store it in BUF. BUF is expected to be of size PBUFSIZ. | |
2814 | If FOREVER, wait forever rather than timing out; this is used | |
2815 | while the target is executing user code. */ | |
2816 | ||
2817 | void | |
2818 | getpkt (buf, forever) | |
2819 | char *buf; | |
2820 | int forever; | |
2821 | { | |
2822 | int c; | |
2823 | int tries; | |
2824 | int timeout; | |
2825 | int val; | |
2826 | ||
2827 | strcpy (buf,"timeout"); | |
2828 | ||
2829 | if (forever) | |
2830 | { | |
2831 | #ifdef MAINTENANCE_CMDS | |
2832 | timeout = watchdog > 0 ? watchdog : -1; | |
2833 | #else | |
2834 | timeout = -1; | |
2835 | #endif | |
2836 | } | |
2837 | ||
2838 | else | |
2839 | timeout = remote_timeout; | |
2840 | ||
2841 | #define MAX_TRIES 3 | |
2842 | ||
2843 | for (tries = 1; tries <= MAX_TRIES; tries++) | |
2844 | { | |
2845 | /* This can loop forever if the remote side sends us characters | |
2846 | continuously, but if it pauses, we'll get a zero from readchar | |
2847 | because of timeout. Then we'll count that as a retry. */ | |
2848 | ||
2849 | /* Note that we will only wait forever prior to the start of a packet. | |
2850 | After that, we expect characters to arrive at a brisk pace. They | |
2851 | should show up within remote_timeout intervals. */ | |
2852 | ||
2853 | do | |
2854 | { | |
2855 | c = readchar (timeout); | |
2856 | ||
2857 | if (c == SERIAL_TIMEOUT) | |
2858 | { | |
2859 | #ifdef MAINTENANCE_CMDS | |
2860 | if (forever) /* Watchdog went off. Kill the target. */ | |
2861 | { | |
2862 | target_mourn_inferior (); | |
2863 | error ("Watchdog has expired. Target detached.\n"); | |
2864 | } | |
2865 | #endif | |
2866 | if (remote_debug) | |
2867 | puts_filtered ("Timed out.\n"); | |
2868 | goto retry; | |
2869 | } | |
2870 | } | |
2871 | while (c != '$'); | |
2872 | ||
2873 | /* We've found the start of a packet, now collect the data. */ | |
2874 | ||
2875 | val = read_frame (buf); | |
2876 | ||
2877 | if (val == 1) | |
2878 | { | |
2879 | if (remote_debug) | |
2880 | fprintf_unfiltered (gdb_stdout, "Packet received: %s\n", buf); | |
2881 | SERIAL_WRITE (remote_desc, "+", 1); | |
2882 | return; | |
2883 | } | |
2884 | ||
2885 | /* Try the whole thing again. */ | |
2886 | retry: | |
2887 | SERIAL_WRITE (remote_desc, "-", 1); | |
2888 | } | |
2889 | ||
2890 | /* We have tried hard enough, and just can't receive the packet. Give up. */ | |
2891 | ||
2892 | printf_unfiltered ("Ignoring packet error, continuing...\n"); | |
2893 | SERIAL_WRITE (remote_desc, "+", 1); | |
2894 | } | |
2895 | \f | |
2896 | static void | |
2897 | remote_kill () | |
2898 | { | |
2899 | /* For some mysterious reason, wait_for_inferior calls kill instead of | |
2900 | mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */ | |
2901 | if (kill_kludge) | |
2902 | { | |
2903 | kill_kludge = 0; | |
2904 | target_mourn_inferior (); | |
2905 | return; | |
2906 | } | |
2907 | ||
2908 | /* Use catch_errors so the user can quit from gdb even when we aren't on | |
2909 | speaking terms with the remote system. */ | |
2910 | catch_errors (putpkt, "k", "", RETURN_MASK_ERROR); | |
2911 | ||
2912 | /* Don't wait for it to die. I'm not really sure it matters whether | |
2913 | we do or not. For the existing stubs, kill is a noop. */ | |
2914 | target_mourn_inferior (); | |
2915 | } | |
2916 | ||
2917 | static void | |
2918 | remote_mourn () | |
2919 | { | |
2920 | remote_mourn_1 (&remote_ops); | |
2921 | } | |
2922 | ||
2923 | static void | |
2924 | extended_remote_mourn () | |
2925 | { | |
2926 | /* We do _not_ want to mourn the target like this; this will | |
2927 | remove the extended remote target from the target stack, | |
2928 | and the next time the user says "run" it'll fail. | |
2929 | ||
2930 | FIXME: What is the right thing to do here? */ | |
2931 | #if 0 | |
2932 | remote_mourn_1 (&extended_remote_ops); | |
2933 | #endif | |
2934 | } | |
2935 | ||
2936 | /* Worker function for remote_mourn. */ | |
2937 | static void | |
2938 | remote_mourn_1 (target) | |
2939 | struct target_ops *target; | |
2940 | { | |
2941 | unpush_target (target); | |
2942 | generic_mourn_inferior (); | |
2943 | } | |
2944 | ||
2945 | /* In the extended protocol we want to be able to do things like | |
2946 | "run" and have them basically work as expected. So we need | |
2947 | a special create_inferior function. | |
2948 | ||
2949 | FIXME: One day add support for changing the exec file | |
2950 | we're debugging, arguments and an environment. */ | |
2951 | ||
2952 | static void | |
2953 | extended_remote_create_inferior (exec_file, args, env) | |
2954 | char *exec_file; | |
2955 | char *args; | |
2956 | char **env; | |
2957 | { | |
2958 | /* Rip out the breakpoints; we'll reinsert them after restarting | |
2959 | the remote server. */ | |
2960 | remove_breakpoints (); | |
2961 | ||
2962 | /* Now restart the remote server. */ | |
2963 | extended_remote_restart (); | |
2964 | ||
2965 | /* Now put the breakpoints back in. This way we're safe if the | |
2966 | restart function works via a unix fork on the remote side. */ | |
2967 | insert_breakpoints (); | |
2968 | ||
2969 | /* Clean up from the last time we were running. */ | |
2970 | clear_proceed_status (); | |
2971 | ||
2972 | /* Let the remote process run. */ | |
2973 | proceed (-1, TARGET_SIGNAL_0, 0); | |
2974 | } | |
2975 | ||
2976 | \f | |
2977 | /* On some machines, e.g. 68k, we may use a different breakpoint instruction | |
2978 | than other targets; in those use REMOTE_BREAKPOINT instead of just | |
2979 | BREAKPOINT. Also, bi-endian targets may define LITTLE_REMOTE_BREAKPOINT | |
2980 | and BIG_REMOTE_BREAKPOINT. If none of these are defined, we just call | |
2981 | the standard routines that are in mem-break.c. */ | |
2982 | ||
2983 | /* FIXME, these ought to be done in a more dynamic fashion. For instance, | |
2984 | the choice of breakpoint instruction affects target program design and | |
2985 | vice versa, and by making it user-tweakable, the special code here | |
2986 | goes away and we need fewer special GDB configurations. */ | |
2987 | ||
2988 | #if defined (LITTLE_REMOTE_BREAKPOINT) && defined (BIG_REMOTE_BREAKPOINT) && !defined(REMOTE_BREAKPOINT) | |
2989 | #define REMOTE_BREAKPOINT | |
2990 | #endif | |
2991 | ||
2992 | #ifdef REMOTE_BREAKPOINT | |
2993 | ||
2994 | /* If the target isn't bi-endian, just pretend it is. */ | |
2995 | #if !defined (LITTLE_REMOTE_BREAKPOINT) && !defined (BIG_REMOTE_BREAKPOINT) | |
2996 | #define LITTLE_REMOTE_BREAKPOINT REMOTE_BREAKPOINT | |
2997 | #define BIG_REMOTE_BREAKPOINT REMOTE_BREAKPOINT | |
2998 | #endif | |
2999 | ||
3000 | static unsigned char big_break_insn[] = BIG_REMOTE_BREAKPOINT; | |
3001 | static unsigned char little_break_insn[] = LITTLE_REMOTE_BREAKPOINT; | |
3002 | ||
3003 | #endif /* REMOTE_BREAKPOINT */ | |
3004 | ||
3005 | /* Insert a breakpoint on targets that don't have any better breakpoint | |
3006 | support. We read the contents of the target location and stash it, | |
3007 | then overwrite it with a breakpoint instruction. ADDR is the target | |
3008 | location in the target machine. CONTENTS_CACHE is a pointer to | |
3009 | memory allocated for saving the target contents. It is guaranteed | |
3010 | by the caller to be long enough to save sizeof BREAKPOINT bytes (this | |
3011 | is accomplished via BREAKPOINT_MAX). */ | |
3012 | ||
3013 | static int | |
3014 | remote_insert_breakpoint (addr, contents_cache) | |
3015 | CORE_ADDR addr; | |
3016 | char *contents_cache; | |
3017 | { | |
3018 | #ifdef REMOTE_BREAKPOINT | |
3019 | int val; | |
3020 | ||
3021 | val = target_read_memory (addr, contents_cache, sizeof big_break_insn); | |
3022 | ||
3023 | if (val == 0) | |
3024 | { | |
3025 | if (TARGET_BYTE_ORDER == BIG_ENDIAN) | |
3026 | val = target_write_memory (addr, (char *) big_break_insn, | |
3027 | sizeof big_break_insn); | |
3028 | else | |
3029 | val = target_write_memory (addr, (char *) little_break_insn, | |
3030 | sizeof little_break_insn); | |
3031 | } | |
3032 | ||
3033 | return val; | |
3034 | #else | |
3035 | return memory_insert_breakpoint (addr, contents_cache); | |
3036 | #endif /* REMOTE_BREAKPOINT */ | |
3037 | } | |
3038 | ||
3039 | static int | |
3040 | remote_remove_breakpoint (addr, contents_cache) | |
3041 | CORE_ADDR addr; | |
3042 | char *contents_cache; | |
3043 | { | |
3044 | #ifdef REMOTE_BREAKPOINT | |
3045 | return target_write_memory (addr, contents_cache, sizeof big_break_insn); | |
3046 | #else | |
3047 | return memory_remove_breakpoint (addr, contents_cache); | |
3048 | #endif /* REMOTE_BREAKPOINT */ | |
3049 | } | |
3050 | ||
3051 | /* Some targets are only capable of doing downloads, and afterwards they switch | |
3052 | to the remote serial protocol. This function provides a clean way to get | |
3053 | from the download target to the remote target. It's basically just a | |
3054 | wrapper so that we don't have to expose any of the internal workings of | |
3055 | remote.c. | |
3056 | ||
3057 | Prior to calling this routine, you should shutdown the current target code, | |
3058 | else you will get the "A program is being debugged already..." message. | |
3059 | Usually a call to pop_target() suffices. | |
3060 | */ | |
3061 | ||
3062 | void | |
3063 | push_remote_target (name, from_tty) | |
3064 | char *name; | |
3065 | int from_tty; | |
3066 | { | |
3067 | printf_filtered ("Switching to remote protocol\n"); | |
3068 | remote_open (name, from_tty); | |
3069 | } | |
3070 | ||
3071 | /* Other targets want to use the entire remote serial module but with | |
3072 | certain remote_ops overridden. */ | |
3073 | ||
3074 | void | |
3075 | open_remote_target (name, from_tty, target, extended_p) | |
3076 | char *name; | |
3077 | int from_tty; | |
3078 | struct target_ops *target; | |
3079 | int extended_p; | |
3080 | { | |
3081 | printf_filtered ("Selecting the %sremote protocol\n", | |
3082 | (extended_p ? "extended-" : "")); | |
3083 | remote_open_1 (name, from_tty, target, extended_p); | |
3084 | } | |
3085 | ||
b85cf6ae MS |
3086 | /* Table used by the crc32 function to calcuate the checksum. */ |
3087 | static unsigned long crc32_table[256] = {0, 0}; | |
3088 | ||
3089 | static unsigned long | |
3090 | crc32 (buf, len, crc) | |
3091 | unsigned char *buf; | |
3092 | int len; | |
3093 | unsigned int crc; | |
3094 | { | |
3095 | if (! crc32_table[1]) | |
3096 | { | |
3097 | /* Initialize the CRC table and the decoding table. */ | |
3098 | int i, j; | |
3099 | unsigned int c; | |
3100 | ||
3101 | for (i = 0; i < 256; i++) | |
3102 | { | |
3103 | for (c = i << 24, j = 8; j > 0; --j) | |
3104 | c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1); | |
3105 | crc32_table[i] = c; | |
3106 | } | |
3107 | } | |
3108 | ||
3109 | while (len--) | |
3110 | { | |
3111 | crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *buf) & 255]; | |
3112 | buf++; | |
3113 | } | |
3114 | return crc; | |
3115 | } | |
3116 | ||
2e7eeba9 MS |
3117 | /* compare-sections command |
3118 | ||
3119 | With no arguments, compares each loadable section in the exec bfd | |
3120 | with the same memory range on the target, and reports mismatches. | |
3121 | Useful for verifying the image on the target against the exec file. | |
3122 | Depends on the target understanding the new "qCRC:" request. */ | |
3123 | ||
b85cf6ae | 3124 | static void |
ecc6e842 | 3125 | compare_sections_command (args, from_tty) |
b85cf6ae MS |
3126 | char *args; |
3127 | int from_tty; | |
3128 | { | |
3129 | asection *s; | |
3130 | unsigned long host_crc, target_crc; | |
3131 | extern bfd *exec_bfd; | |
3132 | struct cleanup *old_chain; | |
3133 | char *tmp, *sectdata, *sectname, buf[PBUFSIZ]; | |
3134 | bfd_size_type size; | |
3135 | bfd_vma lma; | |
3136 | int matched = 0; | |
ecc6e842 | 3137 | int mismatched = 0; |
b85cf6ae MS |
3138 | |
3139 | if (!exec_bfd) | |
3140 | error ("command cannot be used without an exec file"); | |
3141 | if (!current_target.to_shortname || | |
3142 | strcmp (current_target.to_shortname, "remote") != 0) | |
3143 | error ("command can only be used with remote target"); | |
3144 | ||
3145 | for (s = exec_bfd->sections; s; s = s->next) | |
3146 | { | |
3147 | if (!(s->flags & SEC_LOAD)) | |
3148 | continue; /* skip non-loadable section */ | |
3149 | ||
3150 | size = bfd_get_section_size_before_reloc (s); | |
3151 | if (size == 0) | |
3152 | continue; /* skip zero-length section */ | |
3153 | ||
3154 | sectname = (char *) bfd_get_section_name (exec_bfd, s); | |
3155 | if (args && strcmp (args, sectname) != 0) | |
3156 | continue; /* not the section selected by user */ | |
3157 | ||
3158 | matched = 1; /* do this section */ | |
3159 | lma = s->lma; | |
3160 | /* FIXME: assumes lma can fit into long */ | |
3161 | sprintf (buf, "qCRC:%lx,%lx", (long) lma, (long) size); | |
3162 | putpkt (buf); | |
3163 | ||
3164 | /* be clever; compute the host_crc before waiting for target reply */ | |
3165 | sectdata = xmalloc (size); | |
3166 | old_chain = make_cleanup (free, sectdata); | |
3167 | bfd_get_section_contents (exec_bfd, s, sectdata, 0, size); | |
3168 | host_crc = crc32 ((unsigned char *) sectdata, size, 0xffffffff); | |
3169 | ||
3170 | getpkt (buf, 0); | |
3171 | if (buf[0] == 'E') | |
3172 | error ("target memory fault, section %s, range 0x%08x -- 0x%08x", | |
3173 | sectname, lma, lma + size); | |
3174 | if (buf[0] != 'C') | |
3175 | error ("remote target does not support this operation"); | |
3176 | ||
3177 | for (target_crc = 0, tmp = &buf[1]; *tmp; tmp++) | |
3178 | target_crc = target_crc * 16 + fromhex (*tmp); | |
3179 | ||
3180 | printf_filtered ("Section %s, range 0x%08x -- 0x%08x: ", | |
3181 | sectname, lma, lma + size); | |
3182 | if (host_crc == target_crc) | |
3183 | printf_filtered ("matched.\n"); | |
3184 | else | |
ecc6e842 | 3185 | { |
f8059b97 AC |
3186 | printf_filtered ("MIS-MATCHED!\n"); |
3187 | mismatched++; | |
ecc6e842 | 3188 | } |
b85cf6ae MS |
3189 | |
3190 | do_cleanups (old_chain); | |
3191 | } | |
ecc6e842 | 3192 | if (mismatched > 0) |
f8059b97 | 3193 | warning ("One or more sections of the remote executable does not match\nthe loaded file\n"); |
b85cf6ae MS |
3194 | if (args && !matched) |
3195 | printf_filtered ("No loaded section named '%s'.\n", args); | |
b85cf6ae | 3196 | } |
c719b714 | 3197 | |
ecc6e842 EZ |
3198 | static void |
3199 | packet_command (args, from_tty) | |
3200 | char *args; | |
3201 | int from_tty; | |
ecc6e842 EZ |
3202 | { |
3203 | char buf[PBUFSIZ]; | |
3204 | ||
abf80d3b | 3205 | if (! remote_desc) |
ecc6e842 EZ |
3206 | error ("command can only be used with remote target"); |
3207 | ||
3208 | if (! args) | |
3209 | error ("remote-packet command requires packet text as argument"); | |
3210 | ||
3211 | puts_filtered ("sending: "); | |
3212 | print_packet (args); | |
3213 | puts_filtered ("\n"); | |
3214 | putpkt (args); | |
3215 | ||
3216 | getpkt (buf, 0); | |
3217 | puts_filtered ("received: "); | |
3218 | print_packet (buf); | |
3219 | puts_filtered ("\n"); | |
3220 | } | |
3221 | ||
0e05ecee SS |
3222 | static void |
3223 | init_remote_ops () | |
3224 | { | |
3225 | remote_ops.to_shortname = "remote"; | |
3226 | remote_ops.to_longname = "Remote serial target in gdb-specific protocol"; | |
abf80d3b | 3227 | remote_ops.to_doc = "Use a remote computer via a serial line, using a gdb-specific protocol.\n\ |
0e05ecee SS |
3228 | Specify the serial device it is connected to (e.g. /dev/ttya)."; |
3229 | remote_ops.to_open = remote_open; | |
3230 | remote_ops.to_close = remote_close; | |
4ef1f467 | 3231 | remote_ops.to_detach = remote_detach; |
0e05ecee | 3232 | remote_ops.to_resume = remote_resume; |
4ef1f467 | 3233 | remote_ops.to_wait = remote_wait; |
0e05ecee SS |
3234 | remote_ops.to_fetch_registers = remote_fetch_registers; |
3235 | remote_ops.to_store_registers = remote_store_registers; | |
3236 | remote_ops.to_prepare_to_store = remote_prepare_to_store; | |
3237 | remote_ops.to_xfer_memory = remote_xfer_memory; | |
3238 | remote_ops.to_files_info = remote_files_info; | |
3239 | remote_ops.to_insert_breakpoint = remote_insert_breakpoint; | |
3240 | remote_ops.to_remove_breakpoint = remote_remove_breakpoint; | |
3241 | remote_ops.to_kill = remote_kill; | |
3242 | remote_ops.to_load = generic_load; | |
3243 | remote_ops.to_mourn_inferior = remote_mourn; | |
3244 | remote_ops.to_thread_alive = remote_thread_alive; | |
2e7eeba9 | 3245 | remote_ops.to_stop = remote_stop; |
0e05ecee SS |
3246 | remote_ops.to_stratum = process_stratum; |
3247 | remote_ops.to_has_all_memory = 1; | |
3248 | remote_ops.to_has_memory = 1; | |
3249 | remote_ops.to_has_stack = 1; | |
3250 | remote_ops.to_has_registers = 1; | |
3251 | remote_ops.to_has_execution = 1; | |
3252 | remote_ops.to_magic = OPS_MAGIC; | |
3253 | } | |
3254 | ||
3255 | static void | |
4ef1f467 | 3256 | init_extended_remote_ops () |
0e05ecee SS |
3257 | { |
3258 | extended_remote_ops = remote_ops; | |
3259 | ||
3260 | extended_remote_ops.to_shortname = "extended-remote"; | |
3261 | extended_remote_ops.to_longname = "Extended remote serial target in gdb-specific protocol"; | |
abf80d3b | 3262 | extended_remote_ops.to_doc = "Use a remote computer via a serial line, using a gdb-specific protocol.\n\ |
0e05ecee SS |
3263 | Specify the serial device it is connected to (e.g. /dev/ttya).", |
3264 | extended_remote_ops.to_open = extended_remote_open; | |
3265 | extended_remote_ops.to_create_inferior = extended_remote_create_inferior; | |
3266 | extended_remote_ops.to_mourn_inferior = extended_remote_mourn; | |
3267 | } | |
3268 | ||
bd5635a1 RP |
3269 | void |
3270 | _initialize_remote () | |
3271 | { | |
0e05ecee | 3272 | init_remote_ops (); |
bd5635a1 | 3273 | add_target (&remote_ops); |
0e05ecee SS |
3274 | |
3275 | init_extended_remote_ops (); | |
2b576293 | 3276 | add_target (&extended_remote_ops); |
4ef1f467 DT |
3277 | init_remote_threads(); |
3278 | INIT_REMOTE_THREADTESTS /* conditional thread packet unit test */ | |
2c441e7f | 3279 | |
ecc6e842 EZ |
3280 | add_cmd ("compare-sections", class_obscure, compare_sections_command, |
3281 | "Compare section data on target to the exec file.\n\ | |
3282 | Argument is a single section name (default: all loaded sections).", | |
b85cf6ae MS |
3283 | &cmdlist); |
3284 | ||
ecc6e842 EZ |
3285 | add_cmd ("packet", class_maintenance, packet_command, |
3286 | "Send an arbitrary packet to a remote target.\n\ | |
3287 | maintenance packet TEXT\n\ | |
3288 | If GDB is talking to an inferior via the GDB serial protocol, then\n\ | |
3289 | this command sends the string TEXT to the inferior, and displays the\n\ | |
3290 | response packet. GDB supplies the initial `$' character, and the\n\ | |
f8059b97 | 3291 | terminating `#' character and checksum.", |
ecc6e842 EZ |
3292 | &maintenancelist); |
3293 | ||
2c441e7f SS |
3294 | add_show_from_set (add_set_cmd ("remotetimeout", no_class, |
3295 | var_integer, (char *)&remote_timeout, | |
4ef1f467 DT |
3296 | "Set timeout value for remote read.\n", |
3297 | &setlist), | |
2c441e7f | 3298 | &showlist); |
b52cac6b FF |
3299 | |
3300 | add_show_from_set (add_set_cmd ("remotebreak", no_class, | |
3301 | var_integer, (char *)&remote_break, | |
4ef1f467 DT |
3302 | "Set whether to send break if interrupted.\n", |
3303 | &setlist), | |
b52cac6b | 3304 | &showlist); |
fea17b55 SS |
3305 | |
3306 | add_show_from_set (add_set_cmd ("remotewritesize", no_class, | |
3307 | var_integer, (char *)&remote_write_size, | |
4ef1f467 DT |
3308 | "Set the maximum number of bytes in each memory write packet.\n", |
3309 | &setlist), | |
fea17b55 | 3310 | &showlist); |
2e7eeba9 | 3311 | |
4ef1f467 DT |
3312 | |
3313 | ||
2e7eeba9 MS |
3314 | remote_address_size = TARGET_PTR_BIT; |
3315 | add_show_from_set (add_set_cmd ("remoteaddresssize", class_obscure, | |
3316 | var_integer, (char *)&remote_address_size, | |
4ef1f467 DT |
3317 | "Set the maximum size of the address (in bits) in a memory packet.\n", |
3318 | &setlist), | |
3319 | &showlist); | |
976bb0be | 3320 | } |
4ef1f467 | 3321 |