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