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