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