]>
Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Remote target communications for serial-line targets in custom GDB protocol |
43ff13b4 | 2 | Copyright 1988, 91, 92, 93, 94, 95, 96, 97, 98, 1999 |
c906108c SS |
3 | Free Software Foundation, Inc. |
4 | ||
c5aa993b JM |
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, | |
20 | Boston, MA 02111-1307, USA. */ | |
c5aa993b | 21 | |
96baa820 | 22 | /* See the GDB User Guide for details of the GDB remote protocol. */ |
c5aa993b | 23 | |
c906108c SS |
24 | #include "defs.h" |
25 | #include "gdb_string.h" | |
26 | #include <ctype.h> | |
27 | #include <fcntl.h> | |
28 | #include "frame.h" | |
29 | #include "inferior.h" | |
30 | #include "bfd.h" | |
31 | #include "symfile.h" | |
32 | #include "target.h" | |
33 | #include "wait.h" | |
c5aa993b | 34 | /*#include "terminal.h" */ |
c906108c SS |
35 | #include "gdbcmd.h" |
36 | #include "objfiles.h" | |
37 | #include "gdb-stabs.h" | |
38 | #include "gdbthread.h" | |
39 | ||
40 | #include "dcache.h" | |
41 | ||
7a292a7a | 42 | #include <ctype.h> |
9846de1b | 43 | #include <sys/time.h> |
c906108c SS |
44 | #ifdef USG |
45 | #include <sys/types.h> | |
46 | #endif | |
47 | ||
43ff13b4 JM |
48 | #include "event-loop.h" |
49 | ||
c906108c SS |
50 | #include <signal.h> |
51 | #include "serial.h" | |
52 | ||
53 | /* Prototypes for local functions */ | |
43ff13b4 JM |
54 | static void initialize_sigint_signal_handler PARAMS ((void)); |
55 | static void handle_remote_sigint PARAMS ((int)); | |
56 | static void handle_remote_sigint_twice PARAMS ((int)); | |
57 | static void async_remote_interrupt PARAMS ((gdb_client_data)); | |
58 | static void async_remote_interrupt_twice PARAMS ((gdb_client_data)); | |
59 | ||
60 | static void set_extended_protocol PARAMS ((struct continuation_arg *)); | |
c906108c | 61 | |
0f71a2f6 JM |
62 | static void build_remote_gdbarch_data PARAMS ((void)); |
63 | ||
c906108c SS |
64 | static int remote_write_bytes PARAMS ((CORE_ADDR memaddr, |
65 | char *myaddr, int len)); | |
66 | ||
67 | static int remote_read_bytes PARAMS ((CORE_ADDR memaddr, | |
68 | char *myaddr, int len)); | |
69 | ||
c5aa993b | 70 | static void remote_files_info PARAMS ((struct target_ops * ignore)); |
c906108c | 71 | |
c5aa993b | 72 | static int remote_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr, |
c906108c SS |
73 | int len, int should_write, |
74 | struct target_ops * target)); | |
75 | ||
76 | static void remote_prepare_to_store PARAMS ((void)); | |
77 | ||
78 | static void remote_fetch_registers PARAMS ((int regno)); | |
79 | ||
80 | static void remote_resume PARAMS ((int pid, int step, | |
81 | enum target_signal siggnal)); | |
43ff13b4 | 82 | static void remote_async_resume PARAMS ((int pid, int step, |
c5aa993b | 83 | enum target_signal siggnal)); |
c906108c SS |
84 | |
85 | static int remote_start_remote PARAMS ((PTR)); | |
86 | ||
87 | static void remote_open PARAMS ((char *name, int from_tty)); | |
43ff13b4 | 88 | static void remote_async_open PARAMS ((char *name, int from_tty)); |
c906108c SS |
89 | |
90 | static void extended_remote_open PARAMS ((char *name, int from_tty)); | |
43ff13b4 | 91 | static void extended_remote_async_open PARAMS ((char *name, int from_tty)); |
c906108c SS |
92 | |
93 | static void remote_open_1 PARAMS ((char *, int, struct target_ops *, | |
94 | int extended_p)); | |
43ff13b4 | 95 | static void remote_async_open_1 PARAMS ((char *, int, struct target_ops *, |
c5aa993b | 96 | int extended_p)); |
c906108c SS |
97 | |
98 | static void remote_close PARAMS ((int quitting)); | |
99 | ||
100 | static void remote_store_registers PARAMS ((int regno)); | |
101 | ||
102 | static void remote_mourn PARAMS ((void)); | |
103 | ||
104 | static void extended_remote_restart PARAMS ((void)); | |
105 | ||
106 | static void extended_remote_mourn PARAMS ((void)); | |
107 | ||
108 | static void extended_remote_create_inferior PARAMS ((char *, char *, char **)); | |
43ff13b4 | 109 | static void extended_remote_async_create_inferior PARAMS ((char *, char *, char **)); |
c906108c SS |
110 | |
111 | static void remote_mourn_1 PARAMS ((struct target_ops *)); | |
112 | ||
113 | static void remote_send PARAMS ((char *buf)); | |
114 | ||
115 | static int readchar PARAMS ((int timeout)); | |
116 | ||
117 | static int remote_wait PARAMS ((int pid, struct target_waitstatus * status)); | |
43ff13b4 | 118 | static int remote_async_wait PARAMS ((int pid, struct target_waitstatus * status)); |
c906108c SS |
119 | |
120 | static void remote_kill PARAMS ((void)); | |
43ff13b4 | 121 | static void remote_async_kill PARAMS ((void)); |
c906108c SS |
122 | |
123 | static int tohex PARAMS ((int nib)); | |
124 | ||
125 | static void remote_detach PARAMS ((char *args, int from_tty)); | |
43ff13b4 | 126 | static void remote_async_detach PARAMS ((char *args, int from_tty)); |
c906108c SS |
127 | |
128 | static void remote_interrupt PARAMS ((int signo)); | |
129 | ||
7a292a7a SS |
130 | static void remote_interrupt_twice PARAMS ((int signo)); |
131 | ||
c906108c SS |
132 | static void interrupt_query PARAMS ((void)); |
133 | ||
134 | static void set_thread PARAMS ((int, int)); | |
135 | ||
136 | static int remote_thread_alive PARAMS ((int)); | |
137 | ||
138 | static void get_offsets PARAMS ((void)); | |
139 | ||
140 | static int read_frame PARAMS ((char *)); | |
141 | ||
142 | static int remote_insert_breakpoint PARAMS ((CORE_ADDR, char *)); | |
143 | ||
144 | static int remote_remove_breakpoint PARAMS ((CORE_ADDR, char *)); | |
145 | ||
146 | static int hexnumlen PARAMS ((ULONGEST num)); | |
147 | ||
148 | static void init_remote_ops PARAMS ((void)); | |
149 | ||
150 | static void init_extended_remote_ops PARAMS ((void)); | |
151 | ||
0f71a2f6 JM |
152 | static void init_remote_cisco_ops PARAMS ((void)); |
153 | ||
154 | static struct target_ops remote_cisco_ops; | |
155 | ||
c906108c SS |
156 | static void remote_stop PARAMS ((void)); |
157 | ||
158 | static int ishex PARAMS ((int ch, int *val)); | |
159 | ||
160 | static int stubhex PARAMS ((int ch)); | |
161 | ||
c5aa993b | 162 | static int remote_query PARAMS ((int /*char */ , char *, char *, int *)); |
c906108c SS |
163 | |
164 | static int hexnumstr PARAMS ((char *, ULONGEST)); | |
165 | ||
166 | static CORE_ADDR remote_address_masked PARAMS ((CORE_ADDR)); | |
167 | ||
168 | static void print_packet PARAMS ((char *)); | |
169 | ||
170 | static unsigned long crc32 PARAMS ((unsigned char *, int, unsigned int)); | |
171 | ||
172 | static void compare_sections_command PARAMS ((char *, int)); | |
173 | ||
174 | static void packet_command PARAMS ((char *, int)); | |
175 | ||
176 | static int stub_unpack_int PARAMS ((char *buff, int fieldlength)); | |
177 | ||
c906108c SS |
178 | static int remote_current_thread PARAMS ((int oldpid)); |
179 | ||
cce74817 | 180 | static void remote_find_new_threads PARAMS ((void)); |
c906108c SS |
181 | |
182 | static void record_currthread PARAMS ((int currthread)); | |
183 | ||
c906108c SS |
184 | /* exported functions */ |
185 | ||
186 | extern int fromhex PARAMS ((int a)); | |
187 | ||
188 | extern void getpkt PARAMS ((char *buf, int forever)); | |
189 | ||
190 | extern int putpkt PARAMS ((char *buf)); | |
191 | ||
192 | static int putpkt_binary PARAMS ((char *buf, int cnt)); | |
193 | ||
194 | void remote_console_output PARAMS ((char *)); | |
195 | ||
196 | static void check_binary_download PARAMS ((CORE_ADDR addr)); | |
197 | ||
5a2468f5 JM |
198 | #if __STDC__ |
199 | struct packet_config; | |
200 | #endif | |
201 | ||
202 | static void show_packet_config_cmd PARAMS ((struct packet_config * config)); | |
203 | ||
204 | static void set_packet_config_cmd PARAMS ((struct packet_config * config, | |
205 | struct cmd_list_element * c)); | |
206 | ||
207 | static void add_packet_config_cmd PARAMS ((struct packet_config * config, | |
208 | char *name, | |
209 | char *title, | |
210 | void (*set_func) (char *args, int from_tty, struct cmd_list_element * c), | |
211 | void (*show_func) (char *name, int from_tty), | |
212 | struct cmd_list_element **setlist, | |
213 | struct cmd_list_element **showlist)); | |
214 | ||
215 | static void init_packet_config PARAMS ((struct packet_config * config)); | |
216 | ||
217 | static void set_remote_protocol_P_packet_cmd PARAMS ((char *args, | |
218 | int from_tty, | |
219 | struct cmd_list_element * c)); | |
220 | ||
221 | static void show_remote_protocol_P_packet_cmd PARAMS ((char *args, | |
222 | int from_tty)); | |
223 | ||
96baa820 JM |
224 | static void set_remote_protocol_Z_packet_cmd PARAMS ((char *args, |
225 | int from_tty, | |
226 | struct cmd_list_element * c)); | |
227 | ||
228 | static void show_remote_protocol_Z_packet_cmd PARAMS ((char *args, | |
229 | int from_tty)); | |
230 | ||
5a2468f5 JM |
231 | |
232 | ||
233 | ||
c906108c SS |
234 | /* Define the target subroutine names */ |
235 | ||
236 | void open_remote_target PARAMS ((char *, int, struct target_ops *, int)); | |
237 | ||
238 | void _initialize_remote PARAMS ((void)); | |
239 | ||
240 | /* */ | |
241 | ||
242 | static struct target_ops remote_ops; | |
243 | ||
244 | static struct target_ops extended_remote_ops; | |
245 | ||
43ff13b4 JM |
246 | /* Temporary target ops. Just like the remote_ops and |
247 | extended_remote_ops, but with asynchronous support. */ | |
248 | static struct target_ops remote_async_ops; | |
249 | ||
250 | static struct target_ops extended_async_remote_ops; | |
251 | ||
c906108c SS |
252 | /* This was 5 seconds, which is a long time to sit and wait. |
253 | Unless this is going though some terminal server or multiplexer or | |
254 | other form of hairy serial connection, I would think 2 seconds would | |
255 | be plenty. */ | |
256 | ||
257 | /* Changed to allow option to set timeout value. | |
258 | was static int remote_timeout = 2; */ | |
259 | extern int remote_timeout; | |
260 | ||
261 | /* This variable chooses whether to send a ^C or a break when the user | |
262 | requests program interruption. Although ^C is usually what remote | |
263 | systems expect, and that is the default here, sometimes a break is | |
264 | preferable instead. */ | |
265 | ||
266 | static int remote_break; | |
267 | ||
c906108c SS |
268 | /* Descriptor for I/O to remote machine. Initialize it to NULL so that |
269 | remote_open knows that we don't have a file open when the program | |
270 | starts. */ | |
271 | static serial_t remote_desc = NULL; | |
272 | ||
0f71a2f6 JM |
273 | /* This is set by the target (thru the 'S' message) |
274 | to denote that the target is in kernel mode. */ | |
275 | static int cisco_kernel_mode = 0; | |
276 | ||
cce74817 JM |
277 | /* Maximum number of bytes to read/write at once. The value here |
278 | is chosen to fill up a packet (the headers account for the 32). */ | |
279 | #define MAXBUFBYTES(N) (((N)-32)/2) | |
280 | ||
c906108c SS |
281 | /* Having this larger than 400 causes us to be incompatible with m68k-stub.c |
282 | and i386-stub.c. Normally, no one would notice because it only matters | |
283 | for writing large chunks of memory (e.g. in downloads). Also, this needs | |
284 | to be more than 400 if required to hold the registers (see below, where | |
285 | we round it up based on REGISTER_BYTES). */ | |
c906108c | 286 | /* Round up PBUFSIZ to hold all the registers, at least. */ |
cce74817 JM |
287 | #define PBUFSIZ ((REGISTER_BYTES > MAXBUFBYTES (400)) \ |
288 | ? (REGISTER_BYTES * 2 + 32) \ | |
289 | : 400) | |
c906108c SS |
290 | |
291 | ||
292 | /* This variable sets the number of bytes to be written to the target | |
293 | in a single packet. Normally PBUFSIZ is satisfactory, but some | |
294 | targets need smaller values (perhaps because the receiving end | |
295 | is slow). */ | |
296 | ||
cce74817 | 297 | static int remote_write_size; |
c906108c SS |
298 | |
299 | /* This variable sets the number of bits in an address that are to be | |
300 | sent in a memory ("M" or "m") packet. Normally, after stripping | |
301 | leading zeros, the entire address would be sent. This variable | |
302 | restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The | |
303 | initial implementation of remote.c restricted the address sent in | |
304 | memory packets to ``host::sizeof long'' bytes - (typically 32 | |
305 | bits). Consequently, for 64 bit targets, the upper 32 bits of an | |
306 | address was never sent. Since fixing this bug may cause a break in | |
307 | some remote targets this variable is principly provided to | |
308 | facilitate backward compatibility. */ | |
309 | ||
310 | static int remote_address_size; | |
311 | ||
312 | /* This is the size (in chars) of the first response to the `g' command. This | |
313 | is used to limit the size of the memory read and write commands to prevent | |
314 | stub buffers from overflowing. The size does not include headers and | |
315 | trailers, it is only the payload size. */ | |
316 | ||
317 | static int remote_register_buf_size = 0; | |
318 | ||
5a2468f5 JM |
319 | /* Generic configuration support for packets the stub optionally |
320 | supports. Allows the user to specify the use of the packet as well | |
321 | as allowing GDB to auto-detect support in the remote stub. */ | |
322 | ||
323 | enum packet_support | |
324 | { | |
325 | PACKET_SUPPORT_UNKNOWN = 0, | |
326 | PACKET_ENABLE, | |
327 | PACKET_DISABLE | |
328 | }; | |
329 | ||
330 | enum packet_detect | |
331 | { | |
332 | PACKET_AUTO_DETECT = 0, | |
333 | PACKET_MANUAL_DETECT | |
334 | }; | |
335 | ||
336 | struct packet_config | |
337 | { | |
338 | char *state; | |
339 | char *name; | |
340 | char *title; | |
341 | enum packet_detect detect; | |
342 | enum packet_support support; | |
343 | }; | |
344 | ||
345 | static char packet_support_auto[] = "auto"; | |
346 | static char packet_enable[] = "enable"; | |
347 | static char packet_disable[] = "disable"; | |
348 | static char *packet_support_enums[] = | |
349 | { | |
350 | packet_support_auto, | |
351 | packet_enable, | |
352 | packet_disable, | |
353 | 0, | |
354 | }; | |
355 | ||
356 | static void | |
357 | set_packet_config_cmd (config, c) | |
358 | struct packet_config *config; | |
359 | struct cmd_list_element *c; | |
360 | { | |
361 | if (config->state == packet_enable) | |
362 | { | |
363 | config->detect = PACKET_MANUAL_DETECT; | |
364 | config->support = PACKET_ENABLE; | |
365 | } | |
366 | else if (config->state == packet_disable) | |
367 | { | |
368 | config->detect = PACKET_MANUAL_DETECT; | |
369 | config->support = PACKET_DISABLE; | |
370 | } | |
371 | else if (config->state == packet_support_auto) | |
372 | { | |
373 | config->detect = PACKET_AUTO_DETECT; | |
374 | config->support = PACKET_SUPPORT_UNKNOWN; | |
375 | } | |
376 | else | |
96baa820 | 377 | internal_error ("Bad enum value"); |
5a2468f5 JM |
378 | } |
379 | ||
380 | static void | |
381 | show_packet_config_cmd (config) | |
382 | struct packet_config *config; | |
383 | { | |
384 | char *support = "internal-error"; | |
385 | switch (config->support) | |
386 | { | |
387 | case PACKET_ENABLE: | |
388 | support = "enabled"; | |
389 | break; | |
390 | case PACKET_DISABLE: | |
391 | support = "disabled"; | |
392 | break; | |
393 | case PACKET_SUPPORT_UNKNOWN: | |
394 | support = "unknown"; | |
395 | break; | |
396 | } | |
397 | switch (config->detect) | |
398 | { | |
399 | case PACKET_AUTO_DETECT: | |
400 | printf_filtered ("Support for remote protocol `%s' (%s) packet is auto-detected, currently %s.\n", | |
401 | config->name, config->title, support); | |
402 | break; | |
403 | case PACKET_MANUAL_DETECT: | |
404 | printf_filtered ("Support for remote protocol `%s' (%s) is currently %s.\n", | |
405 | config->name, config->title, support); | |
406 | } | |
407 | } | |
408 | ||
409 | static void | |
410 | add_packet_config_cmd (config, name, title, set_func, show_func, | |
411 | setlist, showlist) | |
412 | struct packet_config *config; | |
413 | char *name; | |
414 | char *title; | |
415 | void (*set_func) PARAMS ((char *args, int from_tty, | |
416 | struct cmd_list_element * c)); | |
417 | void (*show_func) PARAMS ((char *name, int from_tty)); | |
418 | struct cmd_list_element **setlist; | |
419 | struct cmd_list_element **showlist; | |
420 | { | |
421 | struct cmd_list_element *c; | |
422 | char *set_doc; | |
423 | char *show_doc; | |
424 | char *full_name; | |
425 | config->name = name; | |
426 | config->title = title; | |
427 | asprintf (&set_doc, "Set use of remote protocol `%s' (%s) packet", | |
428 | name, title); | |
429 | asprintf (&show_doc, "Show current use of remote protocol `%s' (%s) packet", | |
430 | name, title); | |
431 | asprintf (&full_name, "%s-packet", name); | |
432 | c = add_set_enum_cmd (full_name, | |
433 | class_obscure, packet_support_enums, | |
434 | (char *) &config->state, | |
435 | set_doc, setlist); | |
436 | c->function.sfunc = set_func; | |
437 | add_cmd (full_name, class_obscure, show_func, show_doc, showlist); | |
438 | } | |
439 | ||
440 | static void | |
441 | init_packet_config (config) | |
442 | struct packet_config *config; | |
443 | { | |
444 | switch (config->detect) | |
445 | { | |
446 | case PACKET_AUTO_DETECT: | |
447 | config->support = PACKET_SUPPORT_UNKNOWN; | |
448 | break; | |
449 | case PACKET_MANUAL_DETECT: | |
450 | /* let the user beware */ | |
451 | break; | |
452 | } | |
453 | } | |
454 | ||
455 | /* Should we try the 'P' (set register) request? */ | |
456 | ||
457 | static struct packet_config remote_protocol_P; | |
458 | ||
459 | static void | |
460 | set_remote_protocol_P_packet_cmd (args, from_tty, c) | |
461 | char *args; | |
462 | int from_tty; | |
463 | struct cmd_list_element *c; | |
464 | { | |
465 | set_packet_config_cmd (&remote_protocol_P, c); | |
466 | } | |
467 | ||
468 | static void | |
469 | show_remote_protocol_P_packet_cmd (args, from_tty) | |
470 | char *args; | |
471 | int from_tty; | |
472 | { | |
473 | show_packet_config_cmd (&remote_protocol_P); | |
474 | } | |
475 | ||
96baa820 JM |
476 | /* Should we try the 'Z' (set breakpoint) request? */ |
477 | ||
478 | static struct packet_config remote_protocol_Z; | |
479 | ||
480 | static void | |
481 | set_remote_protocol_Z_packet_cmd (args, from_tty, c) | |
482 | char *args; | |
483 | int from_tty; | |
484 | struct cmd_list_element *c; | |
485 | { | |
486 | set_packet_config_cmd (&remote_protocol_Z, c); | |
487 | } | |
488 | ||
489 | static void | |
490 | show_remote_protocol_Z_packet_cmd (args, from_tty) | |
491 | char *args; | |
492 | int from_tty; | |
493 | { | |
494 | show_packet_config_cmd (&remote_protocol_Z); | |
495 | } | |
496 | ||
497 | /* Should we try the 'X' (remote binary download) packet? | |
498 | ||
499 | This variable (available to the user via "set remote X-packet") | |
500 | dictates whether downloads are sent in binary (via the 'X' packet). | |
501 | We assume that the stub can, and attempt to do it. This will be | |
502 | cleared if the stub does not understand it. This switch is still | |
503 | needed, though in cases when the packet is supported in the stub, | |
504 | but the connection does not allow it (i.e., 7-bit serial connection | |
505 | only). */ | |
506 | ||
507 | static struct packet_config remote_protocol_binary_download; | |
508 | ||
509 | static void | |
510 | set_remote_protocol_binary_download_cmd (char *args, | |
511 | int from_tty, | |
512 | struct cmd_list_element *c) | |
513 | { | |
514 | set_packet_config_cmd (&remote_protocol_binary_download, c); | |
515 | } | |
516 | ||
517 | static void | |
518 | show_remote_protocol_binary_download_cmd (char *args, | |
519 | int from_tty) | |
520 | { | |
521 | show_packet_config_cmd (&remote_protocol_binary_download); | |
522 | } | |
523 | ||
c906108c | 524 | |
43ff13b4 JM |
525 | /* Tokens for use by the asynchronous signal handlers for SIGINT */ |
526 | PTR sigint_remote_twice_token; | |
527 | PTR sigint_remote_token; | |
528 | ||
c906108c SS |
529 | /* These are pointers to hook functions that may be set in order to |
530 | modify resume/wait behavior for a particular architecture. */ | |
531 | ||
532 | void (*target_resume_hook) PARAMS ((void)); | |
533 | void (*target_wait_loop_hook) PARAMS ((void)); | |
c906108c SS |
534 | \f |
535 | ||
c5aa993b | 536 | |
c906108c SS |
537 | /* These are the threads which we last sent to the remote system. |
538 | -1 for all or -2 for not sent yet. */ | |
539 | static int general_thread; | |
cce74817 | 540 | static int continue_thread; |
c906108c SS |
541 | |
542 | /* Call this function as a result of | |
543 | 1) A halt indication (T packet) containing a thread id | |
544 | 2) A direct query of currthread | |
545 | 3) Successful execution of set thread | |
546 | */ | |
547 | ||
548 | static void | |
549 | record_currthread (currthread) | |
550 | int currthread; | |
551 | { | |
c906108c | 552 | general_thread = currthread; |
cce74817 | 553 | |
c906108c SS |
554 | /* If this is a new thread, add it to GDB's thread list. |
555 | If we leave it up to WFI to do this, bad things will happen. */ | |
556 | if (!in_thread_list (currthread)) | |
0f71a2f6 JM |
557 | { |
558 | add_thread (currthread); | |
559 | printf_filtered ("[New %s]\n", target_pid_to_str (currthread)); | |
560 | } | |
c906108c SS |
561 | } |
562 | ||
563 | #define MAGIC_NULL_PID 42000 | |
564 | ||
565 | static void | |
566 | set_thread (th, gen) | |
567 | int th; | |
568 | int gen; | |
569 | { | |
085dd6e6 | 570 | char *buf = alloca (PBUFSIZ); |
cce74817 | 571 | int state = gen ? general_thread : continue_thread; |
c906108c SS |
572 | |
573 | if (state == th) | |
574 | return; | |
575 | ||
576 | buf[0] = 'H'; | |
577 | buf[1] = gen ? 'g' : 'c'; | |
578 | if (th == MAGIC_NULL_PID) | |
579 | { | |
580 | buf[2] = '0'; | |
581 | buf[3] = '\0'; | |
582 | } | |
583 | else if (th < 0) | |
584 | sprintf (&buf[2], "-%x", -th); | |
585 | else | |
586 | sprintf (&buf[2], "%x", th); | |
587 | putpkt (buf); | |
588 | getpkt (buf, 0); | |
589 | if (gen) | |
c5aa993b | 590 | general_thread = th; |
c906108c | 591 | else |
cce74817 | 592 | continue_thread = th; |
c906108c SS |
593 | } |
594 | \f | |
595 | /* Return nonzero if the thread TH is still alive on the remote system. */ | |
596 | ||
597 | static int | |
cce74817 JM |
598 | remote_thread_alive (tid) |
599 | int tid; | |
c906108c | 600 | { |
cce74817 | 601 | char buf[16]; |
c906108c | 602 | |
cce74817 JM |
603 | if (tid < 0) |
604 | sprintf (buf, "T-%08x", -tid); | |
c906108c | 605 | else |
cce74817 | 606 | sprintf (buf, "T%08x", tid); |
c906108c SS |
607 | putpkt (buf); |
608 | getpkt (buf, 0); | |
609 | return (buf[0] == 'O' && buf[1] == 'K'); | |
610 | } | |
611 | ||
612 | /* About these extended threadlist and threadinfo packets. They are | |
613 | variable length packets but, the fields within them are often fixed | |
614 | length. They are redundent enough to send over UDP as is the | |
615 | remote protocol in general. There is a matching unit test module | |
616 | in libstub. */ | |
617 | ||
cce74817 JM |
618 | #define OPAQUETHREADBYTES 8 |
619 | ||
620 | /* a 64 bit opaque identifier */ | |
621 | typedef unsigned char threadref[OPAQUETHREADBYTES]; | |
622 | ||
623 | /* WARNING: This threadref data structure comes from the remote O.S., libstub | |
624 | protocol encoding, and remote.c. it is not particularly changable */ | |
625 | ||
626 | /* Right now, the internal structure is int. We want it to be bigger. | |
627 | Plan to fix this. | |
c5aa993b | 628 | */ |
cce74817 | 629 | |
c5aa993b | 630 | typedef int gdb_threadref; /* internal GDB thread reference */ |
cce74817 JM |
631 | |
632 | /* gdb_ext_thread_info is an internal GDB data structure which is | |
633 | equivalint to the reply of the remote threadinfo packet */ | |
634 | ||
635 | struct gdb_ext_thread_info | |
c5aa993b JM |
636 | { |
637 | threadref threadid; /* External form of thread reference */ | |
638 | int active; /* Has state interesting to GDB? , regs, stack */ | |
639 | char display[256]; /* Brief state display, name, blocked/syspended */ | |
640 | char shortname[32]; /* To be used to name threads */ | |
641 | char more_display[256]; /* Long info, statistics, queue depth, whatever */ | |
642 | }; | |
cce74817 JM |
643 | |
644 | /* The volume of remote transfers can be limited by submitting | |
645 | a mask containing bits specifying the desired information. | |
646 | Use a union of these values as the 'selection' parameter to | |
647 | get_thread_info. FIXME: Make these TAG names more thread specific. | |
c5aa993b | 648 | */ |
cce74817 JM |
649 | |
650 | #define TAG_THREADID 1 | |
651 | #define TAG_EXISTS 2 | |
652 | #define TAG_DISPLAY 4 | |
653 | #define TAG_THREADNAME 8 | |
c5aa993b | 654 | #define TAG_MOREDISPLAY 16 |
cce74817 | 655 | |
c906108c SS |
656 | #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES*2) |
657 | ||
cce74817 JM |
658 | char *unpack_varlen_hex PARAMS ((char *buff, int *result)); |
659 | ||
660 | static char *unpack_nibble PARAMS ((char *buf, int *val)); | |
661 | ||
662 | static char *pack_nibble PARAMS ((char *buf, int nibble)); | |
663 | ||
c5aa993b | 664 | static char *pack_hex_byte PARAMS ((char *pkt, int /*unsigned char */ byte)); |
cce74817 JM |
665 | |
666 | static char *unpack_byte PARAMS ((char *buf, int *value)); | |
667 | ||
668 | static char *pack_int PARAMS ((char *buf, int value)); | |
669 | ||
670 | static char *unpack_int PARAMS ((char *buf, int *value)); | |
671 | ||
672 | static char *unpack_string PARAMS ((char *src, char *dest, int length)); | |
673 | ||
c5aa993b | 674 | static char *pack_threadid PARAMS ((char *pkt, threadref * id)); |
cce74817 | 675 | |
c5aa993b | 676 | static char *unpack_threadid PARAMS ((char *inbuf, threadref * id)); |
cce74817 | 677 | |
c5aa993b | 678 | void int_to_threadref PARAMS ((threadref * id, int value)); |
cce74817 | 679 | |
c5aa993b | 680 | static int threadref_to_int PARAMS ((threadref * ref)); |
cce74817 | 681 | |
c5aa993b | 682 | static void copy_threadref PARAMS ((threadref * dest, threadref * src)); |
cce74817 | 683 | |
c5aa993b | 684 | static int threadmatch PARAMS ((threadref * dest, threadref * src)); |
cce74817 JM |
685 | |
686 | static char *pack_threadinfo_request PARAMS ((char *pkt, int mode, | |
c5aa993b | 687 | threadref * id)); |
cce74817 JM |
688 | |
689 | static int remote_unpack_thread_info_response PARAMS ((char *pkt, | |
c5aa993b JM |
690 | threadref * expectedref, |
691 | struct gdb_ext_thread_info * info)); | |
cce74817 JM |
692 | |
693 | ||
c5aa993b JM |
694 | static int remote_get_threadinfo PARAMS ((threadref * threadid, |
695 | int fieldset, /*TAG mask */ | |
696 | struct gdb_ext_thread_info * info)); | |
cce74817 | 697 | |
c5aa993b | 698 | static int adapt_remote_get_threadinfo PARAMS ((gdb_threadref * ref, |
cce74817 | 699 | int selection, |
c5aa993b | 700 | struct gdb_ext_thread_info * info)); |
cce74817 JM |
701 | |
702 | static char *pack_threadlist_request PARAMS ((char *pkt, int startflag, | |
703 | int threadcount, | |
c5aa993b | 704 | threadref * nextthread)); |
cce74817 JM |
705 | |
706 | static int parse_threadlist_response PARAMS ((char *pkt, | |
707 | int result_limit, | |
c5aa993b JM |
708 | threadref * original_echo, |
709 | threadref * resultlist, | |
cce74817 JM |
710 | int *doneflag)); |
711 | ||
712 | static int remote_get_threadlist PARAMS ((int startflag, | |
c5aa993b | 713 | threadref * nextthread, |
cce74817 JM |
714 | int result_limit, |
715 | int *done, | |
716 | int *result_count, | |
c5aa993b | 717 | threadref * threadlist)); |
cce74817 | 718 | |
c5aa993b | 719 | typedef int (*rmt_thread_action) (threadref * ref, void *context); |
cce74817 JM |
720 | |
721 | static int remote_threadlist_iterator PARAMS ((rmt_thread_action stepfunction, | |
c5aa993b | 722 | void *context, int looplimit)); |
cce74817 | 723 | |
c5aa993b | 724 | static int remote_newthread_step PARAMS ((threadref * ref, void *context)); |
cce74817 | 725 | |
c906108c SS |
726 | /* encode 64 bits in 16 chars of hex */ |
727 | ||
728 | static const char hexchars[] = "0123456789abcdef"; | |
729 | ||
730 | static int | |
731 | ishex (ch, val) | |
732 | int ch; | |
733 | int *val; | |
734 | { | |
735 | if ((ch >= 'a') && (ch <= 'f')) | |
736 | { | |
737 | *val = ch - 'a' + 10; | |
738 | return 1; | |
739 | } | |
740 | if ((ch >= 'A') && (ch <= 'F')) | |
741 | { | |
742 | *val = ch - 'A' + 10; | |
743 | return 1; | |
744 | } | |
745 | if ((ch >= '0') && (ch <= '9')) | |
746 | { | |
747 | *val = ch - '0'; | |
748 | return 1; | |
749 | } | |
750 | return 0; | |
751 | } | |
752 | ||
753 | static int | |
754 | stubhex (ch) | |
755 | int ch; | |
756 | { | |
757 | if (ch >= 'a' && ch <= 'f') | |
758 | return ch - 'a' + 10; | |
759 | if (ch >= '0' && ch <= '9') | |
760 | return ch - '0'; | |
761 | if (ch >= 'A' && ch <= 'F') | |
762 | return ch - 'A' + 10; | |
763 | return -1; | |
764 | } | |
765 | ||
766 | static int | |
767 | stub_unpack_int (buff, fieldlength) | |
768 | char *buff; | |
769 | int fieldlength; | |
770 | { | |
771 | int nibble; | |
772 | int retval = 0; | |
773 | ||
774 | while (fieldlength) | |
775 | { | |
776 | nibble = stubhex (*buff++); | |
777 | retval |= nibble; | |
778 | fieldlength--; | |
779 | if (fieldlength) | |
780 | retval = retval << 4; | |
781 | } | |
782 | return retval; | |
783 | } | |
784 | ||
785 | char * | |
786 | unpack_varlen_hex (buff, result) | |
787 | char *buff; /* packet to parse */ | |
788 | int *result; | |
789 | { | |
790 | int nibble; | |
791 | int retval = 0; | |
792 | ||
793 | while (ishex (*buff, &nibble)) | |
794 | { | |
795 | buff++; | |
796 | retval = retval << 4; | |
797 | retval |= nibble & 0x0f; | |
798 | } | |
799 | *result = retval; | |
800 | return buff; | |
801 | } | |
802 | ||
803 | static char * | |
804 | unpack_nibble (buf, val) | |
805 | char *buf; | |
806 | int *val; | |
807 | { | |
808 | ishex (*buf++, val); | |
809 | return buf; | |
810 | } | |
811 | ||
812 | static char * | |
813 | pack_nibble (buf, nibble) | |
814 | char *buf; | |
815 | int nibble; | |
816 | { | |
817 | *buf++ = hexchars[(nibble & 0x0f)]; | |
818 | return buf; | |
819 | } | |
820 | ||
821 | static char * | |
822 | pack_hex_byte (pkt, byte) | |
823 | char *pkt; | |
824 | int byte; | |
825 | { | |
826 | *pkt++ = hexchars[(byte >> 4) & 0xf]; | |
827 | *pkt++ = hexchars[(byte & 0xf)]; | |
828 | return pkt; | |
829 | } | |
830 | ||
831 | static char * | |
832 | unpack_byte (buf, value) | |
833 | char *buf; | |
834 | int *value; | |
835 | { | |
836 | *value = stub_unpack_int (buf, 2); | |
837 | return buf + 2; | |
838 | } | |
839 | ||
840 | static char * | |
841 | pack_int (buf, value) | |
842 | char *buf; | |
843 | int value; | |
844 | { | |
845 | buf = pack_hex_byte (buf, (value >> 24) & 0xff); | |
846 | buf = pack_hex_byte (buf, (value >> 16) & 0xff); | |
847 | buf = pack_hex_byte (buf, (value >> 8) & 0x0ff); | |
848 | buf = pack_hex_byte (buf, (value & 0xff)); | |
849 | return buf; | |
850 | } | |
851 | ||
852 | static char * | |
853 | unpack_int (buf, value) | |
854 | char *buf; | |
855 | int *value; | |
856 | { | |
857 | *value = stub_unpack_int (buf, 8); | |
858 | return buf + 8; | |
859 | } | |
860 | ||
c5aa993b | 861 | #if 0 /* currently unused, uncomment when needed */ |
c906108c SS |
862 | static char *pack_string PARAMS ((char *pkt, char *string)); |
863 | ||
864 | static char * | |
865 | pack_string (pkt, string) | |
866 | char *pkt; | |
867 | char *string; | |
868 | { | |
869 | char ch; | |
870 | int len; | |
871 | ||
872 | len = strlen (string); | |
873 | if (len > 200) | |
874 | len = 200; /* Bigger than most GDB packets, junk??? */ | |
875 | pkt = pack_hex_byte (pkt, len); | |
876 | while (len-- > 0) | |
877 | { | |
878 | ch = *string++; | |
879 | if ((ch == '\0') || (ch == '#')) | |
880 | ch = '*'; /* Protect encapsulation */ | |
881 | *pkt++ = ch; | |
882 | } | |
883 | return pkt; | |
884 | } | |
885 | #endif /* 0 (unused) */ | |
886 | ||
887 | static char * | |
888 | unpack_string (src, dest, length) | |
889 | char *src; | |
890 | char *dest; | |
891 | int length; | |
892 | { | |
893 | while (length--) | |
894 | *dest++ = *src++; | |
895 | *dest = '\0'; | |
896 | return src; | |
897 | } | |
898 | ||
899 | static char * | |
900 | pack_threadid (pkt, id) | |
901 | char *pkt; | |
902 | threadref *id; | |
903 | { | |
904 | char *limit; | |
905 | unsigned char *altid; | |
906 | ||
907 | altid = (unsigned char *) id; | |
908 | limit = pkt + BUF_THREAD_ID_SIZE; | |
909 | while (pkt < limit) | |
910 | pkt = pack_hex_byte (pkt, *altid++); | |
911 | return pkt; | |
912 | } | |
913 | ||
914 | ||
915 | static char * | |
916 | unpack_threadid (inbuf, id) | |
917 | char *inbuf; | |
918 | threadref *id; | |
919 | { | |
920 | char *altref; | |
921 | char *limit = inbuf + BUF_THREAD_ID_SIZE; | |
922 | int x, y; | |
923 | ||
924 | altref = (char *) id; | |
925 | ||
926 | while (inbuf < limit) | |
927 | { | |
928 | x = stubhex (*inbuf++); | |
929 | y = stubhex (*inbuf++); | |
930 | *altref++ = (x << 4) | y; | |
931 | } | |
932 | return inbuf; | |
933 | } | |
934 | ||
935 | /* Externally, threadrefs are 64 bits but internally, they are still | |
936 | ints. This is due to a mismatch of specifications. We would like | |
937 | to use 64bit thread references internally. This is an adapter | |
938 | function. */ | |
939 | ||
940 | void | |
941 | int_to_threadref (id, value) | |
942 | threadref *id; | |
943 | int value; | |
944 | { | |
945 | unsigned char *scan; | |
946 | ||
947 | scan = (unsigned char *) id; | |
948 | { | |
949 | int i = 4; | |
950 | while (i--) | |
951 | *scan++ = 0; | |
952 | } | |
953 | *scan++ = (value >> 24) & 0xff; | |
954 | *scan++ = (value >> 16) & 0xff; | |
955 | *scan++ = (value >> 8) & 0xff; | |
956 | *scan++ = (value & 0xff); | |
957 | } | |
958 | ||
959 | static int | |
960 | threadref_to_int (ref) | |
961 | threadref *ref; | |
962 | { | |
963 | int i, value = 0; | |
964 | unsigned char *scan; | |
965 | ||
966 | scan = (char *) ref; | |
967 | scan += 4; | |
968 | i = 4; | |
969 | while (i-- > 0) | |
970 | value = (value << 8) | ((*scan++) & 0xff); | |
971 | return value; | |
972 | } | |
973 | ||
974 | static void | |
975 | copy_threadref (dest, src) | |
976 | threadref *dest; | |
977 | threadref *src; | |
978 | { | |
979 | int i; | |
980 | unsigned char *csrc, *cdest; | |
981 | ||
982 | csrc = (unsigned char *) src; | |
983 | cdest = (unsigned char *) dest; | |
984 | i = 8; | |
985 | while (i--) | |
986 | *cdest++ = *csrc++; | |
987 | } | |
988 | ||
989 | static int | |
990 | threadmatch (dest, src) | |
991 | threadref *dest; | |
992 | threadref *src; | |
993 | { | |
994 | /* things are broken right now, so just assume we got a match */ | |
995 | #if 0 | |
996 | unsigned char *srcp, *destp; | |
997 | int i, result; | |
998 | srcp = (char *) src; | |
999 | destp = (char *) dest; | |
1000 | ||
1001 | result = 1; | |
1002 | while (i-- > 0) | |
1003 | result &= (*srcp++ == *destp++) ? 1 : 0; | |
1004 | return result; | |
1005 | #endif | |
1006 | return 1; | |
1007 | } | |
1008 | ||
1009 | /* | |
c5aa993b JM |
1010 | threadid:1, # always request threadid |
1011 | context_exists:2, | |
1012 | display:4, | |
1013 | unique_name:8, | |
1014 | more_display:16 | |
1015 | */ | |
c906108c SS |
1016 | |
1017 | /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */ | |
1018 | ||
1019 | static char * | |
1020 | pack_threadinfo_request (pkt, mode, id) | |
1021 | char *pkt; | |
1022 | int mode; | |
1023 | threadref *id; | |
1024 | { | |
1025 | *pkt++ = 'q'; /* Info Query */ | |
1026 | *pkt++ = 'P'; /* process or thread info */ | |
1027 | pkt = pack_int (pkt, mode); /* mode */ | |
1028 | pkt = pack_threadid (pkt, id); /* threadid */ | |
1029 | *pkt = '\0'; /* terminate */ | |
1030 | return pkt; | |
1031 | } | |
1032 | ||
1033 | /* These values tag the fields in a thread info response packet */ | |
1034 | /* Tagging the fields allows us to request specific fields and to | |
1035 | add more fields as time goes by */ | |
1036 | ||
c5aa993b JM |
1037 | #define TAG_THREADID 1 /* Echo the thread identifier */ |
1038 | #define TAG_EXISTS 2 /* Is this process defined enough to | |
1039 | fetch registers and its stack */ | |
1040 | #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */ | |
1041 | #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is */ | |
1042 | #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about | |
1043 | the process */ | |
c906108c SS |
1044 | |
1045 | static int | |
1046 | remote_unpack_thread_info_response (pkt, expectedref, info) | |
1047 | char *pkt; | |
1048 | threadref *expectedref; | |
1049 | struct gdb_ext_thread_info *info; | |
1050 | { | |
1051 | int mask, length; | |
1052 | unsigned int tag; | |
1053 | threadref ref; | |
c5aa993b | 1054 | char *limit = pkt + PBUFSIZ; /* plausable parsing limit */ |
c906108c SS |
1055 | int retval = 1; |
1056 | ||
1057 | /* info->threadid = 0; FIXME: implement zero_threadref */ | |
1058 | info->active = 0; | |
1059 | info->display[0] = '\0'; | |
1060 | info->shortname[0] = '\0'; | |
1061 | info->more_display[0] = '\0'; | |
1062 | ||
1063 | /* Assume the characters indicating the packet type have been stripped */ | |
1064 | pkt = unpack_int (pkt, &mask); /* arg mask */ | |
1065 | pkt = unpack_threadid (pkt, &ref); | |
1066 | ||
1067 | if (mask == 0) | |
1068 | warning ("Incomplete response to threadinfo request\n"); | |
1069 | if (!threadmatch (&ref, expectedref)) | |
1070 | { /* This is an answer to a different request */ | |
1071 | warning ("ERROR RMT Thread info mismatch\n"); | |
1072 | return 0; | |
1073 | } | |
1074 | copy_threadref (&info->threadid, &ref); | |
1075 | ||
1076 | /* Loop on tagged fields , try to bail if somthing goes wrong */ | |
1077 | ||
c5aa993b | 1078 | while ((pkt < limit) && mask && *pkt) /* packets are terminated with nulls */ |
c906108c SS |
1079 | { |
1080 | pkt = unpack_int (pkt, &tag); /* tag */ | |
1081 | pkt = unpack_byte (pkt, &length); /* length */ | |
1082 | if (!(tag & mask)) /* tags out of synch with mask */ | |
1083 | { | |
1084 | warning ("ERROR RMT: threadinfo tag mismatch\n"); | |
1085 | retval = 0; | |
1086 | break; | |
1087 | } | |
1088 | if (tag == TAG_THREADID) | |
1089 | { | |
1090 | if (length != 16) | |
1091 | { | |
1092 | warning ("ERROR RMT: length of threadid is not 16\n"); | |
1093 | retval = 0; | |
1094 | break; | |
1095 | } | |
1096 | pkt = unpack_threadid (pkt, &ref); | |
1097 | mask = mask & ~TAG_THREADID; | |
1098 | continue; | |
1099 | } | |
1100 | if (tag == TAG_EXISTS) | |
1101 | { | |
1102 | info->active = stub_unpack_int (pkt, length); | |
1103 | pkt += length; | |
1104 | mask = mask & ~(TAG_EXISTS); | |
1105 | if (length > 8) | |
1106 | { | |
1107 | warning ("ERROR RMT: 'exists' length too long\n"); | |
1108 | retval = 0; | |
1109 | break; | |
1110 | } | |
1111 | continue; | |
1112 | } | |
1113 | if (tag == TAG_THREADNAME) | |
1114 | { | |
1115 | pkt = unpack_string (pkt, &info->shortname[0], length); | |
1116 | mask = mask & ~TAG_THREADNAME; | |
1117 | continue; | |
1118 | } | |
1119 | if (tag == TAG_DISPLAY) | |
1120 | { | |
1121 | pkt = unpack_string (pkt, &info->display[0], length); | |
1122 | mask = mask & ~TAG_DISPLAY; | |
1123 | continue; | |
1124 | } | |
1125 | if (tag == TAG_MOREDISPLAY) | |
1126 | { | |
1127 | pkt = unpack_string (pkt, &info->more_display[0], length); | |
1128 | mask = mask & ~TAG_MOREDISPLAY; | |
1129 | continue; | |
1130 | } | |
1131 | warning ("ERROR RMT: unknown thread info tag\n"); | |
1132 | break; /* Not a tag we know about */ | |
1133 | } | |
1134 | return retval; | |
1135 | } | |
1136 | ||
1137 | static int | |
1138 | remote_get_threadinfo (threadid, fieldset, info) | |
1139 | threadref *threadid; | |
1140 | int fieldset; /* TAG mask */ | |
1141 | struct gdb_ext_thread_info *info; | |
1142 | { | |
1143 | int result; | |
085dd6e6 | 1144 | char *threadinfo_pkt = alloca (PBUFSIZ); |
c906108c SS |
1145 | |
1146 | pack_threadinfo_request (threadinfo_pkt, fieldset, threadid); | |
1147 | putpkt (threadinfo_pkt); | |
1148 | getpkt (threadinfo_pkt, 0); | |
1149 | result = remote_unpack_thread_info_response (threadinfo_pkt + 2, threadid, | |
1150 | info); | |
1151 | return result; | |
1152 | } | |
1153 | ||
1154 | /* Unfortunately, 61 bit thread-ids are bigger than the internal | |
1155 | representation of a threadid. */ | |
1156 | ||
1157 | static int | |
1158 | adapt_remote_get_threadinfo (ref, selection, info) | |
1159 | gdb_threadref *ref; | |
1160 | int selection; | |
1161 | struct gdb_ext_thread_info *info; | |
1162 | { | |
1163 | threadref lclref; | |
1164 | ||
1165 | int_to_threadref (&lclref, *ref); | |
1166 | return remote_get_threadinfo (&lclref, selection, info); | |
1167 | } | |
1168 | ||
1169 | /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */ | |
1170 | ||
1171 | static char * | |
1172 | pack_threadlist_request (pkt, startflag, threadcount, nextthread) | |
1173 | char *pkt; | |
1174 | int startflag; | |
1175 | int threadcount; | |
1176 | threadref *nextthread; | |
1177 | { | |
1178 | *pkt++ = 'q'; /* info query packet */ | |
1179 | *pkt++ = 'L'; /* Process LIST or threadLIST request */ | |
1180 | pkt = pack_nibble (pkt, startflag); /* initflag 1 bytes */ | |
1181 | pkt = pack_hex_byte (pkt, threadcount); /* threadcount 2 bytes */ | |
1182 | pkt = pack_threadid (pkt, nextthread); /* 64 bit thread identifier */ | |
1183 | *pkt = '\0'; | |
1184 | return pkt; | |
1185 | } | |
1186 | ||
1187 | /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */ | |
1188 | ||
1189 | static int | |
1190 | parse_threadlist_response (pkt, result_limit, original_echo, resultlist, | |
1191 | doneflag) | |
1192 | char *pkt; | |
1193 | int result_limit; | |
1194 | threadref *original_echo; | |
1195 | threadref *resultlist; | |
1196 | int *doneflag; | |
1197 | { | |
1198 | char *limit; | |
1199 | int count, resultcount, done; | |
1200 | ||
1201 | resultcount = 0; | |
1202 | /* Assume the 'q' and 'M chars have been stripped. */ | |
c5aa993b | 1203 | limit = pkt + (PBUFSIZ - BUF_THREAD_ID_SIZE); /* done parse past here */ |
c906108c SS |
1204 | pkt = unpack_byte (pkt, &count); /* count field */ |
1205 | pkt = unpack_nibble (pkt, &done); | |
1206 | /* The first threadid is the argument threadid. */ | |
1207 | pkt = unpack_threadid (pkt, original_echo); /* should match query packet */ | |
1208 | while ((count-- > 0) && (pkt < limit)) | |
1209 | { | |
1210 | pkt = unpack_threadid (pkt, resultlist++); | |
1211 | if (resultcount++ >= result_limit) | |
1212 | break; | |
1213 | } | |
1214 | if (doneflag) | |
1215 | *doneflag = done; | |
1216 | return resultcount; | |
1217 | } | |
1218 | ||
1219 | static int | |
1220 | remote_get_threadlist (startflag, nextthread, result_limit, | |
1221 | done, result_count, threadlist) | |
1222 | int startflag; | |
1223 | threadref *nextthread; | |
1224 | int result_limit; | |
1225 | int *done; | |
1226 | int *result_count; | |
1227 | threadref *threadlist; | |
1228 | ||
1229 | { | |
1230 | static threadref echo_nextthread; | |
085dd6e6 JM |
1231 | char *threadlist_packet = alloca (PBUFSIZ); |
1232 | char *t_response = alloca (PBUFSIZ); | |
c906108c SS |
1233 | int result = 1; |
1234 | ||
1235 | /* Trancate result limit to be smaller than the packet size */ | |
1236 | if ((((result_limit + 1) * BUF_THREAD_ID_SIZE) + 10) >= PBUFSIZ) | |
1237 | result_limit = (PBUFSIZ / BUF_THREAD_ID_SIZE) - 2; | |
1238 | ||
1239 | pack_threadlist_request (threadlist_packet, | |
1240 | startflag, result_limit, nextthread); | |
1241 | putpkt (threadlist_packet); | |
1242 | getpkt (t_response, 0); | |
1243 | ||
1244 | *result_count = | |
1245 | parse_threadlist_response (t_response + 2, result_limit, &echo_nextthread, | |
1246 | threadlist, done); | |
1247 | ||
1248 | if (!threadmatch (&echo_nextthread, nextthread)) | |
1249 | { | |
1250 | /* FIXME: This is a good reason to drop the packet */ | |
1251 | /* Possably, there is a duplicate response */ | |
1252 | /* Possabilities : | |
1253 | retransmit immediatly - race conditions | |
1254 | retransmit after timeout - yes | |
1255 | exit | |
1256 | wait for packet, then exit | |
1257 | */ | |
1258 | warning ("HMM: threadlist did not echo arg thread, dropping it\n"); | |
1259 | return 0; /* I choose simply exiting */ | |
1260 | } | |
1261 | if (*result_count <= 0) | |
1262 | { | |
1263 | if (*done != 1) | |
1264 | { | |
1265 | warning ("RMT ERROR : failed to get remote thread list\n"); | |
1266 | result = 0; | |
1267 | } | |
1268 | return result; /* break; */ | |
1269 | } | |
1270 | if (*result_count > result_limit) | |
1271 | { | |
1272 | *result_count = 0; | |
1273 | warning ("RMT ERROR: threadlist response longer than requested\n"); | |
1274 | return 0; | |
1275 | } | |
1276 | return result; | |
1277 | } | |
1278 | ||
1279 | /* This is the interface between remote and threads, remotes upper interface */ | |
1280 | ||
1281 | /* remote_find_new_threads retrieves the thread list and for each | |
1282 | thread in the list, looks up the thread in GDB's internal list, | |
1283 | ading the thread if it does not already exist. This involves | |
1284 | getting partial thread lists from the remote target so, polling the | |
1285 | quit_flag is required. */ | |
1286 | ||
1287 | ||
1288 | /* About this many threadisds fit in a packet. */ | |
1289 | ||
1290 | #define MAXTHREADLISTRESULTS 32 | |
1291 | ||
1292 | static int | |
1293 | remote_threadlist_iterator (stepfunction, context, looplimit) | |
1294 | rmt_thread_action stepfunction; | |
1295 | void *context; | |
1296 | int looplimit; | |
1297 | { | |
1298 | int done, i, result_count; | |
1299 | int startflag = 1; | |
1300 | int result = 1; | |
1301 | int loopcount = 0; | |
1302 | static threadref nextthread; | |
1303 | static threadref resultthreadlist[MAXTHREADLISTRESULTS]; | |
1304 | ||
1305 | done = 0; | |
1306 | while (!done) | |
1307 | { | |
1308 | if (loopcount++ > looplimit) | |
1309 | { | |
1310 | result = 0; | |
1311 | warning ("Remote fetch threadlist -infinite loop-\n"); | |
1312 | break; | |
1313 | } | |
1314 | if (!remote_get_threadlist (startflag, &nextthread, MAXTHREADLISTRESULTS, | |
1315 | &done, &result_count, resultthreadlist)) | |
1316 | { | |
1317 | result = 0; | |
1318 | break; | |
1319 | } | |
1320 | /* clear for later iterations */ | |
1321 | startflag = 0; | |
1322 | /* Setup to resume next batch of thread references, set nextthread. */ | |
1323 | if (result_count >= 1) | |
1324 | copy_threadref (&nextthread, &resultthreadlist[result_count - 1]); | |
1325 | i = 0; | |
1326 | while (result_count--) | |
1327 | if (!(result = (*stepfunction) (&resultthreadlist[i++], context))) | |
1328 | break; | |
1329 | } | |
1330 | return result; | |
1331 | } | |
1332 | ||
1333 | static int | |
1334 | remote_newthread_step (ref, context) | |
1335 | threadref *ref; | |
1336 | void *context; | |
1337 | { | |
1338 | int pid; | |
1339 | ||
1340 | pid = threadref_to_int (ref); | |
1341 | if (!in_thread_list (pid)) | |
1342 | add_thread (pid); | |
1343 | return 1; /* continue iterator */ | |
1344 | } | |
1345 | ||
1346 | #define CRAZY_MAX_THREADS 1000 | |
1347 | ||
1348 | static int | |
1349 | remote_current_thread (oldpid) | |
1350 | int oldpid; | |
1351 | { | |
085dd6e6 | 1352 | char *buf = alloca (PBUFSIZ); |
c906108c SS |
1353 | |
1354 | putpkt ("qC"); | |
1355 | getpkt (buf, 0); | |
1356 | if (buf[0] == 'Q' && buf[1] == 'C') | |
1357 | return strtol (&buf[2], NULL, 16); | |
1358 | else | |
1359 | return oldpid; | |
1360 | } | |
1361 | ||
cce74817 JM |
1362 | /* Find new threads for info threads command. */ |
1363 | ||
1364 | static void | |
c906108c SS |
1365 | remote_find_new_threads () |
1366 | { | |
c5aa993b JM |
1367 | remote_threadlist_iterator (remote_newthread_step, 0, |
1368 | CRAZY_MAX_THREADS); | |
c906108c SS |
1369 | if (inferior_pid == MAGIC_NULL_PID) /* ack ack ack */ |
1370 | inferior_pid = remote_current_thread (inferior_pid); | |
c906108c SS |
1371 | } |
1372 | ||
0f71a2f6 JM |
1373 | static void |
1374 | remote_threads_info (void) | |
1375 | { | |
085dd6e6 JM |
1376 | char *buf = alloca (PBUFSIZ); |
1377 | char *bufp; | |
0f71a2f6 JM |
1378 | int tid; |
1379 | ||
1380 | if (remote_desc == 0) /* paranoia */ | |
1381 | error ("Command can only be used when connected to the remote target."); | |
1382 | ||
1383 | putpkt ("qfThreadInfo"); | |
1384 | getpkt (bufp = buf, 0); | |
1385 | if (bufp[0] == '\0') /* q packet not recognized! */ | |
1386 | { /* try old jmetzler method */ | |
1387 | remote_find_new_threads (); | |
1388 | return; | |
1389 | } | |
1390 | else /* try new 'q' method */ | |
c5aa993b | 1391 | while (*bufp++ == 'm') /* reply contains one or more TID */ |
0f71a2f6 | 1392 | { |
c5aa993b JM |
1393 | do |
1394 | { | |
1395 | tid = strtol (bufp, &bufp, 16); | |
1396 | if (tid != 0 && !in_thread_list (tid)) | |
1397 | add_thread (tid); | |
1398 | } | |
1399 | while (*bufp++ == ','); /* comma-separated list */ | |
0f71a2f6 JM |
1400 | putpkt ("qsThreadInfo"); |
1401 | getpkt (bufp = buf, 0); | |
1402 | } | |
1403 | } | |
c906108c | 1404 | \f |
c5aa993b | 1405 | |
c906108c SS |
1406 | /* Restart the remote side; this is an extended protocol operation. */ |
1407 | ||
1408 | static void | |
1409 | extended_remote_restart () | |
1410 | { | |
085dd6e6 | 1411 | char *buf = alloca (PBUFSIZ); |
c906108c SS |
1412 | |
1413 | /* Send the restart command; for reasons I don't understand the | |
1414 | remote side really expects a number after the "R". */ | |
1415 | buf[0] = 'R'; | |
1416 | sprintf (&buf[1], "%x", 0); | |
1417 | putpkt (buf); | |
1418 | ||
1419 | /* Now query for status so this looks just like we restarted | |
1420 | gdbserver from scratch. */ | |
1421 | putpkt ("?"); | |
1422 | getpkt (buf, 0); | |
1423 | } | |
1424 | \f | |
1425 | /* Clean up connection to a remote debugger. */ | |
1426 | ||
1427 | /* ARGSUSED */ | |
1428 | static void | |
1429 | remote_close (quitting) | |
1430 | int quitting; | |
1431 | { | |
1432 | if (remote_desc) | |
1433 | SERIAL_CLOSE (remote_desc); | |
1434 | remote_desc = NULL; | |
1435 | } | |
1436 | ||
1437 | /* Query the remote side for the text, data and bss offsets. */ | |
1438 | ||
1439 | static void | |
1440 | get_offsets () | |
1441 | { | |
085dd6e6 JM |
1442 | char *buf = alloca (PBUFSIZ); |
1443 | char *ptr; | |
c906108c SS |
1444 | int lose; |
1445 | CORE_ADDR text_addr, data_addr, bss_addr; | |
1446 | struct section_offsets *offs; | |
1447 | ||
1448 | putpkt ("qOffsets"); | |
1449 | ||
1450 | getpkt (buf, 0); | |
1451 | ||
1452 | if (buf[0] == '\000') | |
1453 | return; /* Return silently. Stub doesn't support | |
1454 | this command. */ | |
1455 | if (buf[0] == 'E') | |
1456 | { | |
1457 | warning ("Remote failure reply: %s", buf); | |
1458 | return; | |
1459 | } | |
1460 | ||
1461 | /* Pick up each field in turn. This used to be done with scanf, but | |
1462 | scanf will make trouble if CORE_ADDR size doesn't match | |
1463 | conversion directives correctly. The following code will work | |
1464 | with any size of CORE_ADDR. */ | |
1465 | text_addr = data_addr = bss_addr = 0; | |
1466 | ptr = buf; | |
1467 | lose = 0; | |
1468 | ||
1469 | if (strncmp (ptr, "Text=", 5) == 0) | |
1470 | { | |
1471 | ptr += 5; | |
1472 | /* Don't use strtol, could lose on big values. */ | |
1473 | while (*ptr && *ptr != ';') | |
1474 | text_addr = (text_addr << 4) + fromhex (*ptr++); | |
1475 | } | |
1476 | else | |
1477 | lose = 1; | |
1478 | ||
1479 | if (!lose && strncmp (ptr, ";Data=", 6) == 0) | |
1480 | { | |
1481 | ptr += 6; | |
1482 | while (*ptr && *ptr != ';') | |
1483 | data_addr = (data_addr << 4) + fromhex (*ptr++); | |
1484 | } | |
1485 | else | |
1486 | lose = 1; | |
1487 | ||
1488 | if (!lose && strncmp (ptr, ";Bss=", 5) == 0) | |
1489 | { | |
1490 | ptr += 5; | |
1491 | while (*ptr && *ptr != ';') | |
1492 | bss_addr = (bss_addr << 4) + fromhex (*ptr++); | |
1493 | } | |
1494 | else | |
1495 | lose = 1; | |
1496 | ||
1497 | if (lose) | |
1498 | error ("Malformed response to offset query, %s", buf); | |
1499 | ||
1500 | if (symfile_objfile == NULL) | |
1501 | return; | |
1502 | ||
085dd6e6 | 1503 | offs = alloca (sizeof (struct section_offsets) |
c5aa993b JM |
1504 | + symfile_objfile->num_sections |
1505 | * sizeof (offs->offsets)); | |
c906108c SS |
1506 | memcpy (offs, symfile_objfile->section_offsets, |
1507 | sizeof (struct section_offsets) | |
1508 | + symfile_objfile->num_sections | |
1509 | * sizeof (offs->offsets)); | |
1510 | ||
1511 | ANOFFSET (offs, SECT_OFF_TEXT) = text_addr; | |
1512 | ||
1513 | /* This is a temporary kludge to force data and bss to use the same offsets | |
1514 | because that's what nlmconv does now. The real solution requires changes | |
1515 | to the stub and remote.c that I don't have time to do right now. */ | |
1516 | ||
1517 | ANOFFSET (offs, SECT_OFF_DATA) = data_addr; | |
1518 | ANOFFSET (offs, SECT_OFF_BSS) = data_addr; | |
1519 | ||
1520 | objfile_relocate (symfile_objfile, offs); | |
1521 | } | |
1522 | ||
0f71a2f6 JM |
1523 | /* |
1524 | * Cisco version of section offsets: | |
1525 | * | |
1526 | * Instead of having GDB query the target for the section offsets, | |
1527 | * Cisco lets the target volunteer the information! It's also in | |
1528 | * a different format, so here are the functions that will decode | |
1529 | * a section offset packet from a Cisco target. | |
1530 | */ | |
1531 | ||
1532 | /* | |
1533 | * Function: remote_cisco_section_offsets | |
1534 | * | |
1535 | * Returns: zero for success, non-zero for failure | |
1536 | */ | |
1537 | ||
c5aa993b JM |
1538 | static int |
1539 | remote_cisco_section_offsets (text_addr, data_addr, bss_addr, | |
0f71a2f6 JM |
1540 | text_offs, data_offs, bss_offs) |
1541 | bfd_vma text_addr; | |
1542 | bfd_vma data_addr; | |
1543 | bfd_vma bss_addr; | |
c5aa993b JM |
1544 | bfd_signed_vma *text_offs; |
1545 | bfd_signed_vma *data_offs; | |
1546 | bfd_signed_vma *bss_offs; | |
0f71a2f6 JM |
1547 | { |
1548 | bfd_vma text_base, data_base, bss_base; | |
1549 | struct minimal_symbol *start; | |
1550 | asection *sect; | |
c5aa993b | 1551 | bfd *abfd; |
0f71a2f6 JM |
1552 | int len; |
1553 | char *p; | |
1554 | ||
1555 | if (symfile_objfile == NULL) | |
c5aa993b | 1556 | return -1; /* no can do nothin' */ |
0f71a2f6 JM |
1557 | |
1558 | start = lookup_minimal_symbol ("_start", NULL, NULL); | |
1559 | if (start == NULL) | |
c5aa993b | 1560 | return -1; /* Can't find "_start" symbol */ |
0f71a2f6 JM |
1561 | |
1562 | data_base = bss_base = 0; | |
1563 | text_base = SYMBOL_VALUE_ADDRESS (start); | |
1564 | ||
1565 | abfd = symfile_objfile->obfd; | |
c5aa993b | 1566 | for (sect = abfd->sections; |
0f71a2f6 JM |
1567 | sect != 0; |
1568 | sect = sect->next) | |
1569 | { | |
c5aa993b | 1570 | p = (unsigned char *) bfd_get_section_name (abfd, sect); |
0f71a2f6 JM |
1571 | len = strlen (p); |
1572 | if (strcmp (p + len - 4, "data") == 0) /* ends in "data" */ | |
1573 | if (data_base == 0 || | |
1574 | data_base > bfd_get_section_vma (abfd, sect)) | |
1575 | data_base = bfd_get_section_vma (abfd, sect); | |
1576 | if (strcmp (p + len - 3, "bss") == 0) /* ends in "bss" */ | |
c5aa993b | 1577 | if (bss_base == 0 || |
0f71a2f6 JM |
1578 | bss_base > bfd_get_section_vma (abfd, sect)) |
1579 | bss_base = bfd_get_section_vma (abfd, sect); | |
1580 | } | |
1581 | *text_offs = text_addr - text_base; | |
1582 | *data_offs = data_addr - data_base; | |
c5aa993b | 1583 | *bss_offs = bss_addr - bss_base; |
0f71a2f6 JM |
1584 | if (remote_debug) |
1585 | { | |
1586 | char tmp[128]; | |
1587 | ||
1588 | sprintf (tmp, "VMA: text = 0x"); | |
1589 | sprintf_vma (tmp + strlen (tmp), text_addr); | |
c5aa993b | 1590 | sprintf (tmp + strlen (tmp), " data = 0x"); |
0f71a2f6 | 1591 | sprintf_vma (tmp + strlen (tmp), data_addr); |
c5aa993b | 1592 | sprintf (tmp + strlen (tmp), " bss = 0x"); |
0f71a2f6 JM |
1593 | sprintf_vma (tmp + strlen (tmp), bss_addr); |
1594 | fprintf_filtered (gdb_stdlog, tmp); | |
1595 | fprintf_filtered (gdb_stdlog, | |
1596 | "Reloc offset: text = 0x%x data = 0x%x bss = 0x%x\n", | |
c5aa993b | 1597 | (long) *text_offs, (long) *data_offs, (long) *bss_offs); |
0f71a2f6 JM |
1598 | } |
1599 | ||
1600 | return 0; | |
1601 | } | |
1602 | ||
1603 | /* | |
1604 | * Function: remote_cisco_objfile_relocate | |
1605 | * | |
1606 | * Relocate the symbol file for a remote target. | |
1607 | */ | |
1608 | ||
96baa820 | 1609 | void |
0f71a2f6 JM |
1610 | remote_cisco_objfile_relocate (text_off, data_off, bss_off) |
1611 | bfd_signed_vma text_off; | |
1612 | bfd_signed_vma data_off; | |
1613 | bfd_signed_vma bss_off; | |
1614 | { | |
1615 | struct section_offsets *offs; | |
1616 | ||
c5aa993b | 1617 | if (text_off != 0 || data_off != 0 || bss_off != 0) |
0f71a2f6 JM |
1618 | { |
1619 | /* FIXME: This code assumes gdb-stabs.h is being used; it's | |
c5aa993b JM |
1620 | broken for xcoff, dwarf, sdb-coff, etc. But there is no |
1621 | simple canonical representation for this stuff. */ | |
0f71a2f6 JM |
1622 | |
1623 | offs = ((struct section_offsets *) | |
1624 | alloca (sizeof (struct section_offsets) | |
1625 | + (symfile_objfile->num_sections | |
1626 | * sizeof (offs->offsets)))); | |
1627 | ||
1628 | memcpy (offs, symfile_objfile->section_offsets, | |
1629 | (sizeof (struct section_offsets) | |
1630 | + (symfile_objfile->num_sections | |
1631 | * sizeof (offs->offsets)))); | |
1632 | ||
1633 | ANOFFSET (offs, SECT_OFF_TEXT) = text_off; | |
1634 | ANOFFSET (offs, SECT_OFF_DATA) = data_off; | |
c5aa993b | 1635 | ANOFFSET (offs, SECT_OFF_BSS) = bss_off; |
0f71a2f6 JM |
1636 | |
1637 | /* First call the standard objfile_relocate. */ | |
1638 | objfile_relocate (symfile_objfile, offs); | |
1639 | ||
1640 | /* Now we need to fix up the section entries already attached to | |
c5aa993b JM |
1641 | the exec target. These entries will control memory transfers |
1642 | from the exec file. */ | |
0f71a2f6 JM |
1643 | |
1644 | exec_set_section_offsets (text_off, data_off, bss_off); | |
1645 | } | |
1646 | } | |
1647 | ||
c906108c SS |
1648 | /* Stub for catch_errors. */ |
1649 | ||
0f71a2f6 JM |
1650 | static int |
1651 | remote_start_remote_dummy (dummy) | |
1652 | char *dummy; | |
1653 | { | |
1654 | start_remote (); /* Initialize gdb process mechanisms */ | |
1655 | return 1; | |
1656 | } | |
1657 | ||
c906108c SS |
1658 | static int |
1659 | remote_start_remote (dummy) | |
1660 | PTR dummy; | |
1661 | { | |
1662 | immediate_quit = 1; /* Allow user to interrupt it */ | |
1663 | ||
1664 | /* Ack any packet which the remote side has already sent. */ | |
1665 | SERIAL_WRITE (remote_desc, "+", 1); | |
1666 | ||
1667 | /* Let the stub know that we want it to return the thread. */ | |
1668 | set_thread (-1, 0); | |
1669 | ||
1670 | inferior_pid = remote_current_thread (inferior_pid); | |
1671 | ||
1672 | get_offsets (); /* Get text, data & bss offsets */ | |
1673 | ||
1674 | putpkt ("?"); /* initiate a query from remote machine */ | |
1675 | immediate_quit = 0; | |
1676 | ||
0f71a2f6 | 1677 | return remote_start_remote_dummy (dummy); |
c906108c SS |
1678 | } |
1679 | ||
1680 | /* Open a connection to a remote debugger. | |
1681 | NAME is the filename used for communication. */ | |
1682 | ||
1683 | static void | |
1684 | remote_open (name, from_tty) | |
1685 | char *name; | |
1686 | int from_tty; | |
1687 | { | |
1688 | remote_open_1 (name, from_tty, &remote_ops, 0); | |
1689 | } | |
1690 | ||
43ff13b4 JM |
1691 | /* Just like remote_open, but with asynchronous support. */ |
1692 | static void | |
1693 | remote_async_open (name, from_tty) | |
1694 | char *name; | |
1695 | int from_tty; | |
1696 | { | |
1697 | remote_async_open_1 (name, from_tty, &remote_async_ops, 0); | |
1698 | } | |
1699 | ||
c906108c SS |
1700 | /* Open a connection to a remote debugger using the extended |
1701 | remote gdb protocol. NAME is the filename used for communication. */ | |
1702 | ||
1703 | static void | |
1704 | extended_remote_open (name, from_tty) | |
1705 | char *name; | |
1706 | int from_tty; | |
1707 | { | |
c5aa993b | 1708 | remote_open_1 (name, from_tty, &extended_remote_ops, 1 /*extended_p */ ); |
c906108c SS |
1709 | } |
1710 | ||
43ff13b4 JM |
1711 | /* Just like extended_remote_open, but with asynchronous support. */ |
1712 | static void | |
1713 | extended_remote_async_open (name, from_tty) | |
1714 | char *name; | |
1715 | int from_tty; | |
1716 | { | |
c5aa993b | 1717 | remote_async_open_1 (name, from_tty, &extended_async_remote_ops, 1 /*extended_p */ ); |
43ff13b4 JM |
1718 | } |
1719 | ||
c906108c SS |
1720 | /* Generic code for opening a connection to a remote target. */ |
1721 | ||
1722 | static DCACHE *remote_dcache; | |
1723 | ||
1724 | static void | |
1725 | remote_open_1 (name, from_tty, target, extended_p) | |
1726 | char *name; | |
1727 | int from_tty; | |
1728 | struct target_ops *target; | |
1729 | int extended_p; | |
1730 | { | |
1731 | if (name == 0) | |
1732 | error ("To open a remote debug connection, you need to specify what\n\ | |
1733 | serial device is attached to the remote system (e.g. /dev/ttya)."); | |
1734 | ||
1735 | target_preopen (from_tty); | |
1736 | ||
1737 | unpush_target (target); | |
1738 | ||
1739 | remote_dcache = dcache_init (remote_read_bytes, remote_write_bytes); | |
1740 | ||
1741 | remote_desc = SERIAL_OPEN (name); | |
1742 | if (!remote_desc) | |
1743 | perror_with_name (name); | |
1744 | ||
1745 | if (baud_rate != -1) | |
1746 | { | |
1747 | if (SERIAL_SETBAUDRATE (remote_desc, baud_rate)) | |
1748 | { | |
1749 | SERIAL_CLOSE (remote_desc); | |
1750 | perror_with_name (name); | |
1751 | } | |
1752 | } | |
1753 | ||
c906108c SS |
1754 | SERIAL_RAW (remote_desc); |
1755 | ||
1756 | /* If there is something sitting in the buffer we might take it as a | |
1757 | response to a command, which would be bad. */ | |
1758 | SERIAL_FLUSH_INPUT (remote_desc); | |
1759 | ||
1760 | if (from_tty) | |
1761 | { | |
1762 | puts_filtered ("Remote debugging using "); | |
1763 | puts_filtered (name); | |
1764 | puts_filtered ("\n"); | |
1765 | } | |
c5aa993b | 1766 | push_target (target); /* Switch to using remote target now */ |
c906108c | 1767 | |
5a2468f5 | 1768 | init_packet_config (&remote_protocol_P); |
96baa820 JM |
1769 | init_packet_config (&remote_protocol_Z); |
1770 | ||
c5aa993b | 1771 | general_thread = -2; |
cce74817 | 1772 | continue_thread = -2; |
c906108c SS |
1773 | |
1774 | /* Force remote_write_bytes to check whether target supports | |
1775 | binary downloading. */ | |
96baa820 | 1776 | init_packet_config (&remote_protocol_binary_download); |
c906108c SS |
1777 | |
1778 | /* Without this, some commands which require an active target (such | |
1779 | as kill) won't work. This variable serves (at least) double duty | |
1780 | as both the pid of the target process (if it has such), and as a | |
1781 | flag indicating that a target is active. These functions should | |
1782 | be split out into seperate variables, especially since GDB will | |
1783 | someday have a notion of debugging several processes. */ | |
1784 | ||
1785 | inferior_pid = MAGIC_NULL_PID; | |
1786 | /* Start the remote connection; if error (0), discard this target. | |
1787 | In particular, if the user quits, be sure to discard it | |
1788 | (we'd be in an inconsistent state otherwise). */ | |
c5aa993b JM |
1789 | if (!catch_errors (remote_start_remote, NULL, |
1790 | "Couldn't establish connection to remote target\n", | |
c906108c SS |
1791 | RETURN_MASK_ALL)) |
1792 | { | |
1793 | pop_target (); | |
1794 | return; | |
1795 | } | |
1796 | ||
1797 | if (extended_p) | |
1798 | { | |
1799 | /* tell the remote that we're using the extended protocol. */ | |
085dd6e6 | 1800 | char *buf = alloca (PBUFSIZ); |
c906108c SS |
1801 | putpkt ("!"); |
1802 | getpkt (buf, 0); | |
1803 | } | |
1804 | } | |
1805 | ||
43ff13b4 JM |
1806 | /* Just like remote_open but with asynchronous support. */ |
1807 | static void | |
1808 | remote_async_open_1 (name, from_tty, target, extended_p) | |
1809 | char *name; | |
1810 | int from_tty; | |
1811 | struct target_ops *target; | |
1812 | int extended_p; | |
1813 | { | |
1814 | if (name == 0) | |
1815 | error ("To open a remote debug connection, you need to specify what\n\ | |
1816 | serial device is attached to the remote system (e.g. /dev/ttya)."); | |
1817 | ||
1818 | target_preopen (from_tty); | |
1819 | ||
1820 | unpush_target (target); | |
1821 | ||
1822 | remote_dcache = dcache_init (remote_read_bytes, remote_write_bytes); | |
1823 | ||
1824 | remote_desc = SERIAL_OPEN (name); | |
1825 | if (!remote_desc) | |
1826 | perror_with_name (name); | |
1827 | ||
1828 | if (baud_rate != -1) | |
1829 | { | |
1830 | if (SERIAL_SETBAUDRATE (remote_desc, baud_rate)) | |
1831 | { | |
1832 | SERIAL_CLOSE (remote_desc); | |
1833 | perror_with_name (name); | |
1834 | } | |
1835 | } | |
1836 | ||
1837 | SERIAL_RAW (remote_desc); | |
1838 | ||
1839 | /* If there is something sitting in the buffer we might take it as a | |
1840 | response to a command, which would be bad. */ | |
1841 | SERIAL_FLUSH_INPUT (remote_desc); | |
1842 | ||
1843 | if (from_tty) | |
1844 | { | |
1845 | puts_filtered ("Remote debugging using "); | |
1846 | puts_filtered (name); | |
1847 | puts_filtered ("\n"); | |
1848 | } | |
1849 | ||
1850 | /* If running in asynchronous mode, register the target with the | |
1851 | event loop. Set things up so that when there is an event on the | |
1852 | file descriptor, the event loop will call fetch_inferior_event, | |
1853 | which will do the proper analysis to determine what happened. */ | |
1854 | if (async_p) | |
1855 | add_file_handler (remote_desc->fd, (file_handler_func *) fetch_inferior_event, 0); | |
1856 | ||
c5aa993b | 1857 | push_target (target); /* Switch to using remote target now */ |
43ff13b4 | 1858 | |
5a2468f5 | 1859 | init_packet_config (&remote_protocol_P); |
96baa820 | 1860 | init_packet_config (&remote_protocol_Z); |
43ff13b4 | 1861 | |
c5aa993b | 1862 | general_thread = -2; |
43ff13b4 JM |
1863 | continue_thread = -2; |
1864 | ||
1865 | /* Force remote_write_bytes to check whether target supports | |
1866 | binary downloading. */ | |
96baa820 | 1867 | init_packet_config (&remote_protocol_binary_download); |
43ff13b4 JM |
1868 | |
1869 | /* If running asynchronously, set things up for telling the target | |
1870 | to use the extended protocol. This will happen only after the | |
1871 | target has been connected to, in fetch_inferior_event. */ | |
1872 | if (extended_p && async_p) | |
1873 | add_continuation (set_extended_protocol, NULL); | |
1874 | ||
1875 | /* Without this, some commands which require an active target (such | |
1876 | as kill) won't work. This variable serves (at least) double duty | |
1877 | as both the pid of the target process (if it has such), and as a | |
1878 | flag indicating that a target is active. These functions should | |
1879 | be split out into seperate variables, especially since GDB will | |
1880 | someday have a notion of debugging several processes. */ | |
1881 | ||
1882 | inferior_pid = MAGIC_NULL_PID; | |
1883 | /* Start the remote connection; if error (0), discard this target. | |
1884 | In particular, if the user quits, be sure to discard it | |
1885 | (we'd be in an inconsistent state otherwise). */ | |
c5aa993b JM |
1886 | if (!catch_errors (remote_start_remote, NULL, |
1887 | "Couldn't establish connection to remote target\n", | |
43ff13b4 JM |
1888 | RETURN_MASK_ALL)) |
1889 | { | |
1890 | /* Unregister the file descriptor from the event loop. */ | |
1891 | if (async_p) | |
1892 | delete_file_handler (remote_desc->fd); | |
1893 | pop_target (); | |
1894 | return; | |
1895 | } | |
1896 | ||
1897 | if (!async_p) | |
1898 | { | |
c5aa993b JM |
1899 | if (extended_p) |
1900 | { | |
1901 | /* tell the remote that we're using the extended protocol. */ | |
1902 | char *buf = alloca (PBUFSIZ); | |
1903 | putpkt ("!"); | |
1904 | getpkt (buf, 0); | |
1905 | } | |
43ff13b4 JM |
1906 | } |
1907 | } | |
1908 | ||
1909 | /* This will be called by fetch_inferior_event, via the | |
1910 | cmd_continuation pointer, only after the target has stopped. */ | |
c5aa993b | 1911 | static void |
43ff13b4 | 1912 | set_extended_protocol (arg) |
c5aa993b | 1913 | struct continuation_arg *arg; |
43ff13b4 JM |
1914 | { |
1915 | /* tell the remote that we're using the extended protocol. */ | |
1916 | char *buf = alloca (PBUFSIZ); | |
1917 | putpkt ("!"); | |
1918 | getpkt (buf, 0); | |
1919 | } | |
1920 | ||
c906108c SS |
1921 | /* This takes a program previously attached to and detaches it. After |
1922 | this is done, GDB can be used to debug some other program. We | |
1923 | better not have left any breakpoints in the target program or it'll | |
1924 | die when it hits one. */ | |
1925 | ||
1926 | static void | |
1927 | remote_detach (args, from_tty) | |
1928 | char *args; | |
1929 | int from_tty; | |
1930 | { | |
085dd6e6 | 1931 | char *buf = alloca (PBUFSIZ); |
c906108c SS |
1932 | |
1933 | if (args) | |
1934 | error ("Argument given to \"detach\" when remotely debugging."); | |
1935 | ||
1936 | /* Tell the remote target to detach. */ | |
1937 | strcpy (buf, "D"); | |
1938 | remote_send (buf); | |
1939 | ||
1940 | pop_target (); | |
1941 | if (from_tty) | |
1942 | puts_filtered ("Ending remote debugging.\n"); | |
96baa820 | 1943 | |
c906108c SS |
1944 | } |
1945 | ||
43ff13b4 JM |
1946 | /* Same as remote_detach, but with async support. */ |
1947 | static void | |
1948 | remote_async_detach (args, from_tty) | |
1949 | char *args; | |
1950 | int from_tty; | |
1951 | { | |
1952 | char *buf = alloca (PBUFSIZ); | |
1953 | ||
1954 | if (args) | |
1955 | error ("Argument given to \"detach\" when remotely debugging."); | |
1956 | ||
1957 | /* Tell the remote target to detach. */ | |
1958 | strcpy (buf, "D"); | |
1959 | remote_send (buf); | |
1960 | ||
1961 | /* Unregister the file descriptor from the event loop. */ | |
1962 | if (async_p) | |
1963 | delete_file_handler (remote_desc->fd); | |
1964 | ||
1965 | pop_target (); | |
1966 | if (from_tty) | |
1967 | puts_filtered ("Ending remote debugging.\n"); | |
1968 | } | |
1969 | ||
c906108c SS |
1970 | /* Convert hex digit A to a number. */ |
1971 | ||
1972 | int | |
1973 | fromhex (a) | |
1974 | int a; | |
1975 | { | |
1976 | if (a >= '0' && a <= '9') | |
1977 | return a - '0'; | |
1978 | else if (a >= 'a' && a <= 'f') | |
1979 | return a - 'a' + 10; | |
1980 | else if (a >= 'A' && a <= 'F') | |
1981 | return a - 'A' + 10; | |
c5aa993b | 1982 | else |
c906108c SS |
1983 | error ("Reply contains invalid hex digit %d", a); |
1984 | } | |
1985 | ||
1986 | /* Convert number NIB to a hex digit. */ | |
1987 | ||
1988 | static int | |
1989 | tohex (nib) | |
1990 | int nib; | |
1991 | { | |
1992 | if (nib < 10) | |
c5aa993b | 1993 | return '0' + nib; |
c906108c | 1994 | else |
c5aa993b | 1995 | return 'a' + nib - 10; |
c906108c SS |
1996 | } |
1997 | \f | |
1998 | /* Tell the remote machine to resume. */ | |
1999 | ||
2000 | static enum target_signal last_sent_signal = TARGET_SIGNAL_0; | |
2001 | ||
2002 | static int last_sent_step; | |
2003 | ||
2004 | static void | |
2005 | remote_resume (pid, step, siggnal) | |
2006 | int pid, step; | |
2007 | enum target_signal siggnal; | |
2008 | { | |
085dd6e6 | 2009 | char *buf = alloca (PBUFSIZ); |
c906108c SS |
2010 | |
2011 | if (pid == -1) | |
2012 | set_thread (0, 0); /* run any thread */ | |
2013 | else | |
2014 | set_thread (pid, 0); /* run this thread */ | |
2015 | ||
2016 | dcache_flush (remote_dcache); | |
2017 | ||
2018 | last_sent_signal = siggnal; | |
2019 | last_sent_step = step; | |
2020 | ||
2021 | /* A hook for when we need to do something at the last moment before | |
2022 | resumption. */ | |
2023 | if (target_resume_hook) | |
2024 | (*target_resume_hook) (); | |
2025 | ||
2026 | if (siggnal != TARGET_SIGNAL_0) | |
2027 | { | |
2028 | buf[0] = step ? 'S' : 'C'; | |
c5aa993b JM |
2029 | buf[1] = tohex (((int) siggnal >> 4) & 0xf); |
2030 | buf[2] = tohex ((int) siggnal & 0xf); | |
c906108c SS |
2031 | buf[3] = '\0'; |
2032 | } | |
2033 | else | |
c5aa993b | 2034 | strcpy (buf, step ? "s" : "c"); |
c906108c SS |
2035 | |
2036 | putpkt (buf); | |
2037 | } | |
43ff13b4 JM |
2038 | |
2039 | /* Same as remote_resume, but with async support. */ | |
2040 | static void | |
2041 | remote_async_resume (pid, step, siggnal) | |
2042 | int pid, step; | |
2043 | enum target_signal siggnal; | |
2044 | { | |
2045 | char *buf = alloca (PBUFSIZ); | |
2046 | ||
2047 | if (pid == -1) | |
2048 | set_thread (0, 0); /* run any thread */ | |
2049 | else | |
2050 | set_thread (pid, 0); /* run this thread */ | |
2051 | ||
2052 | dcache_flush (remote_dcache); | |
2053 | ||
2054 | last_sent_signal = siggnal; | |
2055 | last_sent_step = step; | |
2056 | ||
2057 | /* A hook for when we need to do something at the last moment before | |
2058 | resumption. */ | |
2059 | if (target_resume_hook) | |
2060 | (*target_resume_hook) (); | |
2061 | ||
2062 | /* Set things up before execution starts for async commands. */ | |
2063 | /* This function can be entered more than once for the same execution | |
2064 | command, because it is also called by handle_inferior_event. So | |
2065 | we make sure that we don't do the initialization for sync | |
2066 | execution more than once. */ | |
2067 | if (async_p && !target_executing) | |
2068 | { | |
2069 | target_executing = 1; | |
2070 | ||
2071 | /* If the command must look synchronous, fake it, by making gdb | |
2072 | display an empty prompt after the command has completed. Also | |
2073 | disable input. */ | |
2074 | if (sync_execution) | |
2075 | { | |
2076 | push_prompt ("", "", ""); | |
2077 | delete_file_handler (input_fd); | |
2078 | initialize_sigint_signal_handler (); | |
2079 | } | |
2080 | } | |
2081 | ||
2082 | if (siggnal != TARGET_SIGNAL_0) | |
2083 | { | |
2084 | buf[0] = step ? 'S' : 'C'; | |
c5aa993b JM |
2085 | buf[1] = tohex (((int) siggnal >> 4) & 0xf); |
2086 | buf[2] = tohex ((int) siggnal & 0xf); | |
43ff13b4 JM |
2087 | buf[3] = '\0'; |
2088 | } | |
2089 | else | |
c5aa993b | 2090 | strcpy (buf, step ? "s" : "c"); |
43ff13b4 JM |
2091 | |
2092 | putpkt (buf); | |
2093 | } | |
c906108c | 2094 | \f |
43ff13b4 JM |
2095 | |
2096 | /* Set up the signal handler for SIGINT, while the target is | |
2097 | executing, ovewriting the 'regular' SIGINT signal handler. */ | |
2098 | static void | |
2099 | initialize_sigint_signal_handler () | |
2100 | { | |
c5aa993b | 2101 | sigint_remote_token = |
43ff13b4 JM |
2102 | create_async_signal_handler (async_remote_interrupt, NULL); |
2103 | signal (SIGINT, handle_remote_sigint); | |
2104 | } | |
2105 | ||
2106 | /* Signal handler for SIGINT, while the target is executing. */ | |
2107 | static void | |
2108 | handle_remote_sigint (sig) | |
2109 | int sig; | |
2110 | { | |
2111 | signal (sig, handle_remote_sigint_twice); | |
c5aa993b | 2112 | sigint_remote_twice_token = |
43ff13b4 JM |
2113 | create_async_signal_handler (async_remote_interrupt_twice, NULL); |
2114 | mark_async_signal_handler_wrapper (sigint_remote_token); | |
2115 | } | |
2116 | ||
2117 | /* Signal handler for SIGINT, installed after SIGINT has already been | |
2118 | sent once. It will take effect the second time that the user sends | |
2119 | a ^C. */ | |
2120 | static void | |
2121 | handle_remote_sigint_twice (sig) | |
2122 | int sig; | |
2123 | { | |
2124 | signal (sig, handle_sigint); | |
c5aa993b | 2125 | sigint_remote_twice_token = |
43ff13b4 JM |
2126 | create_async_signal_handler (async_remote_interrupt, NULL); |
2127 | mark_async_signal_handler_wrapper (sigint_remote_twice_token); | |
2128 | } | |
2129 | ||
2130 | /* Perform the real interruption of hte target execution, in response | |
2131 | to a ^C. */ | |
c5aa993b | 2132 | static void |
43ff13b4 JM |
2133 | async_remote_interrupt (arg) |
2134 | gdb_client_data arg; | |
2135 | { | |
2136 | if (remote_debug) | |
2137 | fprintf_unfiltered (gdb_stdlog, "remote_interrupt called\n"); | |
2138 | ||
2139 | target_stop (); | |
2140 | } | |
2141 | ||
2142 | /* Perform interrupt, if the first attempt did not succeed. Just give | |
2143 | up on the target alltogether. */ | |
c5aa993b | 2144 | static void |
43ff13b4 JM |
2145 | async_remote_interrupt_twice (arg) |
2146 | gdb_client_data arg; | |
2147 | { | |
2148 | interrupt_query (); | |
2149 | signal (SIGINT, handle_remote_sigint); | |
2150 | } | |
2151 | ||
2152 | /* Reinstall the usual SIGINT handlers, after the target has | |
2153 | stopped. */ | |
2154 | void | |
2155 | cleanup_sigint_signal_handler () | |
2156 | { | |
2157 | signal (SIGINT, handle_sigint); | |
2158 | if (sigint_remote_twice_token) | |
c5aa993b | 2159 | delete_async_signal_handler ((async_signal_handler **) & sigint_remote_twice_token); |
43ff13b4 | 2160 | if (sigint_remote_token) |
c5aa993b | 2161 | delete_async_signal_handler ((async_signal_handler **) & sigint_remote_token); |
43ff13b4 JM |
2162 | } |
2163 | ||
c906108c SS |
2164 | /* Send ^C to target to halt it. Target will respond, and send us a |
2165 | packet. */ | |
c906108c SS |
2166 | static void (*ofunc) PARAMS ((int)); |
2167 | ||
7a292a7a SS |
2168 | /* The command line interface's stop routine. This function is installed |
2169 | as a signal handler for SIGINT. The first time a user requests a | |
2170 | stop, we call remote_stop to send a break or ^C. If there is no | |
2171 | response from the target (it didn't stop when the user requested it), | |
2172 | we ask the user if he'd like to detach from the target. */ | |
c906108c SS |
2173 | static void |
2174 | remote_interrupt (signo) | |
2175 | int signo; | |
2176 | { | |
7a292a7a SS |
2177 | /* If this doesn't work, try more severe steps. */ |
2178 | signal (signo, remote_interrupt_twice); | |
2179 | ||
2180 | if (remote_debug) | |
0f71a2f6 | 2181 | fprintf_unfiltered (gdb_stdlog, "remote_interrupt called\n"); |
7a292a7a SS |
2182 | |
2183 | target_stop (); | |
2184 | } | |
2185 | ||
2186 | /* The user typed ^C twice. */ | |
2187 | ||
2188 | static void | |
2189 | remote_interrupt_twice (signo) | |
2190 | int signo; | |
2191 | { | |
2192 | signal (signo, ofunc); | |
2193 | interrupt_query (); | |
c906108c SS |
2194 | signal (signo, remote_interrupt); |
2195 | } | |
7a292a7a SS |
2196 | |
2197 | /* This is the generic stop called via the target vector. When a target | |
2198 | interrupt is requested, either by the command line or the GUI, we | |
2199 | will eventually end up here. */ | |
c906108c SS |
2200 | static void |
2201 | remote_stop () | |
2202 | { | |
7a292a7a SS |
2203 | /* Send a break or a ^C, depending on user preference. */ |
2204 | if (remote_debug) | |
0f71a2f6 | 2205 | fprintf_unfiltered (gdb_stdlog, "remote_stop called\n"); |
c906108c | 2206 | |
7a292a7a SS |
2207 | if (remote_break) |
2208 | SERIAL_SEND_BREAK (remote_desc); | |
c906108c | 2209 | else |
7a292a7a | 2210 | SERIAL_WRITE (remote_desc, "\003", 1); |
c906108c SS |
2211 | } |
2212 | ||
2213 | /* Ask the user what to do when an interrupt is received. */ | |
2214 | ||
2215 | static void | |
2216 | interrupt_query () | |
2217 | { | |
2218 | target_terminal_ours (); | |
2219 | ||
2220 | if (query ("Interrupted while waiting for the program.\n\ | |
2221 | Give up (and stop debugging it)? ")) | |
2222 | { | |
2223 | target_mourn_inferior (); | |
2224 | return_to_top_level (RETURN_QUIT); | |
2225 | } | |
2226 | ||
2227 | target_terminal_inferior (); | |
2228 | } | |
2229 | ||
2230 | /* If nonzero, ignore the next kill. */ | |
2231 | ||
2232 | int kill_kludge; | |
2233 | ||
2234 | void | |
2235 | remote_console_output (msg) | |
2236 | char *msg; | |
2237 | { | |
2238 | char *p; | |
2239 | ||
c5aa993b | 2240 | for (p = msg; p[0] && p[1]; p += 2) |
c906108c SS |
2241 | { |
2242 | char tb[2]; | |
2243 | char c = fromhex (p[0]) * 16 + fromhex (p[1]); | |
2244 | tb[0] = c; | |
2245 | tb[1] = 0; | |
43ff13b4 | 2246 | fputs_unfiltered (tb, gdb_stdtarg); |
c906108c SS |
2247 | } |
2248 | } | |
2249 | ||
0f71a2f6 JM |
2250 | /* Wait until the remote machine stops, then return, |
2251 | storing status in STATUS just as `wait' would. | |
2252 | Returns "pid", which in the case of a multi-threaded | |
2253 | remote OS, is the thread-id. */ | |
c906108c SS |
2254 | |
2255 | static int | |
2256 | remote_wait (pid, status) | |
2257 | int pid; | |
2258 | struct target_waitstatus *status; | |
2259 | { | |
085dd6e6 | 2260 | unsigned char *buf = alloca (PBUFSIZ); |
c906108c SS |
2261 | int thread_num = -1; |
2262 | ||
2263 | status->kind = TARGET_WAITKIND_EXITED; | |
2264 | status->value.integer = 0; | |
2265 | ||
2266 | while (1) | |
2267 | { | |
2268 | unsigned char *p; | |
2269 | ||
c906108c SS |
2270 | ofunc = signal (SIGINT, remote_interrupt); |
2271 | getpkt ((char *) buf, 1); | |
2272 | signal (SIGINT, ofunc); | |
2273 | ||
2274 | /* This is a hook for when we need to do something (perhaps the | |
c5aa993b | 2275 | collection of trace data) every time the target stops. */ |
c906108c SS |
2276 | if (target_wait_loop_hook) |
2277 | (*target_wait_loop_hook) (); | |
2278 | ||
2279 | switch (buf[0]) | |
2280 | { | |
2281 | case 'E': /* Error of some sort */ | |
2282 | warning ("Remote failure reply: %s", buf); | |
2283 | continue; | |
2284 | case 'T': /* Status with PC, SP, FP, ... */ | |
2285 | { | |
2286 | int i; | |
2287 | long regno; | |
2288 | char regs[MAX_REGISTER_RAW_SIZE]; | |
2289 | ||
2290 | /* Expedited reply, containing Signal, {regno, reg} repeat */ | |
2291 | /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where | |
c5aa993b JM |
2292 | ss = signal number |
2293 | n... = register number | |
2294 | r... = register contents | |
2295 | */ | |
c906108c SS |
2296 | p = &buf[3]; /* after Txx */ |
2297 | ||
2298 | while (*p) | |
2299 | { | |
2300 | unsigned char *p1; | |
2301 | char *p_temp; | |
2302 | ||
2303 | /* Read the register number */ | |
2304 | regno = strtol ((const char *) p, &p_temp, 16); | |
c5aa993b | 2305 | p1 = (unsigned char *) p_temp; |
c906108c | 2306 | |
c5aa993b | 2307 | if (p1 == p) /* No register number present here */ |
c906108c SS |
2308 | { |
2309 | p1 = (unsigned char *) strchr ((const char *) p, ':'); | |
2310 | if (p1 == NULL) | |
2311 | warning ("Malformed packet(a) (missing colon): %s\n\ | |
2312 | Packet: '%s'\n", | |
2313 | p, buf); | |
2314 | if (strncmp ((const char *) p, "thread", p1 - p) == 0) | |
2315 | { | |
2316 | p_temp = unpack_varlen_hex (++p1, &thread_num); | |
2317 | record_currthread (thread_num); | |
2318 | p = (unsigned char *) p_temp; | |
2319 | } | |
2320 | } | |
2321 | else | |
2322 | { | |
2323 | p = p1; | |
2324 | ||
2325 | if (*p++ != ':') | |
2326 | warning ("Malformed packet(b) (missing colon): %s\n\ | |
2327 | Packet: '%s'\n", | |
2328 | p, buf); | |
2329 | ||
2330 | if (regno >= NUM_REGS) | |
2331 | warning ("Remote sent bad register number %ld: %s\n\ | |
2332 | Packet: '%s'\n", | |
2333 | regno, p, buf); | |
2334 | ||
2335 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i++) | |
2336 | { | |
2337 | if (p[0] == 0 || p[1] == 0) | |
2338 | warning ("Remote reply is too short: %s", buf); | |
2339 | regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]); | |
2340 | p += 2; | |
2341 | } | |
2342 | supply_register (regno, regs); | |
2343 | } | |
2344 | ||
2345 | if (*p++ != ';') | |
2346 | { | |
2347 | warning ("Remote register badly formatted: %s", buf); | |
c5aa993b | 2348 | warning (" here: %s", p); |
c906108c SS |
2349 | } |
2350 | } | |
2351 | } | |
2352 | /* fall through */ | |
2353 | case 'S': /* Old style status, just signal only */ | |
2354 | status->kind = TARGET_WAITKIND_STOPPED; | |
2355 | status->value.sig = (enum target_signal) | |
2356 | (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))); | |
2357 | ||
0f71a2f6 JM |
2358 | if (buf[3] == 'p') |
2359 | { | |
2360 | /* Export Cisco kernel mode as a convenience variable | |
c5aa993b | 2361 | (so that it can be used in the GDB prompt if desired). */ |
0f71a2f6 JM |
2362 | |
2363 | if (cisco_kernel_mode == 1) | |
c5aa993b | 2364 | set_internalvar (lookup_internalvar ("cisco_kernel_mode"), |
0f71a2f6 JM |
2365 | value_from_string ("PDEBUG-")); |
2366 | cisco_kernel_mode = 0; | |
2367 | thread_num = strtol ((const char *) &buf[4], NULL, 16); | |
2368 | record_currthread (thread_num); | |
2369 | } | |
2370 | else if (buf[3] == 'k') | |
2371 | { | |
2372 | /* Export Cisco kernel mode as a convenience variable | |
c5aa993b | 2373 | (so that it can be used in the GDB prompt if desired). */ |
0f71a2f6 JM |
2374 | |
2375 | if (cisco_kernel_mode == 1) | |
c5aa993b | 2376 | set_internalvar (lookup_internalvar ("cisco_kernel_mode"), |
0f71a2f6 JM |
2377 | value_from_string ("KDEBUG-")); |
2378 | cisco_kernel_mode = 1; | |
2379 | } | |
c906108c | 2380 | goto got_status; |
0f71a2f6 JM |
2381 | case 'N': /* Cisco special: status and offsets */ |
2382 | { | |
2383 | bfd_vma text_addr, data_addr, bss_addr; | |
2384 | bfd_signed_vma text_off, data_off, bss_off; | |
2385 | unsigned char *p1; | |
2386 | ||
2387 | status->kind = TARGET_WAITKIND_STOPPED; | |
2388 | status->value.sig = (enum target_signal) | |
2389 | (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))); | |
2390 | ||
c5aa993b | 2391 | if (symfile_objfile == NULL) |
0f71a2f6 JM |
2392 | { |
2393 | warning ("Relocation packet recieved with no symbol file. \ | |
2394 | Packet Dropped"); | |
2395 | goto got_status; | |
2396 | } | |
2397 | ||
2398 | /* Relocate object file. Buffer format is NAATT;DD;BB | |
2399 | * where AA is the signal number, TT is the new text | |
2400 | * address, DD * is the new data address, and BB is the | |
2401 | * new bss address. */ | |
2402 | ||
2403 | p = &buf[3]; | |
2404 | text_addr = strtoul (p, (char **) &p1, 16); | |
2405 | if (p1 == p || *p1 != ';') | |
2406 | warning ("Malformed relocation packet: Packet '%s'", buf); | |
2407 | p = p1 + 1; | |
2408 | data_addr = strtoul (p, (char **) &p1, 16); | |
2409 | if (p1 == p || *p1 != ';') | |
2410 | warning ("Malformed relocation packet: Packet '%s'", buf); | |
2411 | p = p1 + 1; | |
2412 | bss_addr = strtoul (p, (char **) &p1, 16); | |
c5aa993b | 2413 | if (p1 == p) |
0f71a2f6 JM |
2414 | warning ("Malformed relocation packet: Packet '%s'", buf); |
2415 | ||
2416 | if (remote_cisco_section_offsets (text_addr, data_addr, bss_addr, | |
2417 | &text_off, &data_off, &bss_off) | |
2418 | == 0) | |
c5aa993b | 2419 | if (text_off != 0 || data_off != 0 || bss_off != 0) |
0f71a2f6 JM |
2420 | remote_cisco_objfile_relocate (text_off, data_off, bss_off); |
2421 | ||
2422 | goto got_status; | |
2423 | } | |
c906108c SS |
2424 | case 'W': /* Target exited */ |
2425 | { | |
2426 | /* The remote process exited. */ | |
2427 | status->kind = TARGET_WAITKIND_EXITED; | |
2428 | status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]); | |
2429 | goto got_status; | |
2430 | } | |
2431 | case 'X': | |
2432 | status->kind = TARGET_WAITKIND_SIGNALLED; | |
2433 | status->value.sig = (enum target_signal) | |
2434 | (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))); | |
2435 | kill_kludge = 1; | |
2436 | ||
2437 | goto got_status; | |
2438 | case 'O': /* Console output */ | |
2439 | remote_console_output (buf + 1); | |
2440 | continue; | |
2441 | case '\0': | |
2442 | if (last_sent_signal != TARGET_SIGNAL_0) | |
2443 | { | |
2444 | /* Zero length reply means that we tried 'S' or 'C' and | |
c5aa993b | 2445 | the remote system doesn't support it. */ |
c906108c SS |
2446 | target_terminal_ours_for_output (); |
2447 | printf_filtered | |
2448 | ("Can't send signals to this remote system. %s not sent.\n", | |
2449 | target_signal_to_name (last_sent_signal)); | |
2450 | last_sent_signal = TARGET_SIGNAL_0; | |
2451 | target_terminal_inferior (); | |
2452 | ||
2453 | strcpy ((char *) buf, last_sent_step ? "s" : "c"); | |
2454 | putpkt ((char *) buf); | |
2455 | continue; | |
2456 | } | |
2457 | /* else fallthrough */ | |
2458 | default: | |
2459 | warning ("Invalid remote reply: %s", buf); | |
2460 | continue; | |
2461 | } | |
2462 | } | |
c5aa993b | 2463 | got_status: |
c906108c SS |
2464 | if (thread_num != -1) |
2465 | { | |
c906108c SS |
2466 | return thread_num; |
2467 | } | |
2468 | return inferior_pid; | |
2469 | } | |
2470 | ||
43ff13b4 JM |
2471 | /* Async version of remote_wait. */ |
2472 | static int | |
2473 | remote_async_wait (pid, status) | |
2474 | int pid; | |
2475 | struct target_waitstatus *status; | |
2476 | { | |
2477 | unsigned char *buf = alloca (PBUFSIZ); | |
2478 | int thread_num = -1; | |
2479 | ||
2480 | status->kind = TARGET_WAITKIND_EXITED; | |
2481 | status->value.integer = 0; | |
2482 | ||
2483 | while (1) | |
2484 | { | |
2485 | unsigned char *p; | |
c5aa993b | 2486 | |
43ff13b4 JM |
2487 | if (!async_p) |
2488 | ofunc = signal (SIGINT, remote_interrupt); | |
2489 | getpkt ((char *) buf, 1); | |
2490 | if (!async_p) | |
2491 | signal (SIGINT, ofunc); | |
2492 | ||
2493 | /* This is a hook for when we need to do something (perhaps the | |
c5aa993b | 2494 | collection of trace data) every time the target stops. */ |
43ff13b4 JM |
2495 | if (target_wait_loop_hook) |
2496 | (*target_wait_loop_hook) (); | |
2497 | ||
2498 | switch (buf[0]) | |
2499 | { | |
2500 | case 'E': /* Error of some sort */ | |
2501 | warning ("Remote failure reply: %s", buf); | |
2502 | continue; | |
2503 | case 'T': /* Status with PC, SP, FP, ... */ | |
2504 | { | |
2505 | int i; | |
2506 | long regno; | |
2507 | char regs[MAX_REGISTER_RAW_SIZE]; | |
2508 | ||
2509 | /* Expedited reply, containing Signal, {regno, reg} repeat */ | |
2510 | /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where | |
c5aa993b JM |
2511 | ss = signal number |
2512 | n... = register number | |
2513 | r... = register contents | |
2514 | */ | |
43ff13b4 JM |
2515 | p = &buf[3]; /* after Txx */ |
2516 | ||
2517 | while (*p) | |
2518 | { | |
2519 | unsigned char *p1; | |
2520 | char *p_temp; | |
2521 | ||
2522 | /* Read the register number */ | |
2523 | regno = strtol ((const char *) p, &p_temp, 16); | |
c5aa993b | 2524 | p1 = (unsigned char *) p_temp; |
43ff13b4 | 2525 | |
c5aa993b | 2526 | if (p1 == p) /* No register number present here */ |
43ff13b4 JM |
2527 | { |
2528 | p1 = (unsigned char *) strchr ((const char *) p, ':'); | |
2529 | if (p1 == NULL) | |
2530 | warning ("Malformed packet(a) (missing colon): %s\n\ | |
2531 | Packet: '%s'\n", | |
2532 | p, buf); | |
2533 | if (strncmp ((const char *) p, "thread", p1 - p) == 0) | |
2534 | { | |
2535 | p_temp = unpack_varlen_hex (++p1, &thread_num); | |
2536 | record_currthread (thread_num); | |
2537 | p = (unsigned char *) p_temp; | |
2538 | } | |
2539 | } | |
2540 | else | |
2541 | { | |
2542 | p = p1; | |
2543 | ||
2544 | if (*p++ != ':') | |
2545 | warning ("Malformed packet(b) (missing colon): %s\n\ | |
2546 | Packet: '%s'\n", | |
2547 | p, buf); | |
2548 | ||
2549 | if (regno >= NUM_REGS) | |
2550 | warning ("Remote sent bad register number %ld: %s\n\ | |
2551 | Packet: '%s'\n", | |
2552 | regno, p, buf); | |
2553 | ||
2554 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i++) | |
2555 | { | |
2556 | if (p[0] == 0 || p[1] == 0) | |
2557 | warning ("Remote reply is too short: %s", buf); | |
2558 | regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]); | |
2559 | p += 2; | |
2560 | } | |
2561 | supply_register (regno, regs); | |
2562 | } | |
2563 | ||
2564 | if (*p++ != ';') | |
2565 | { | |
2566 | warning ("Remote register badly formatted: %s", buf); | |
c5aa993b | 2567 | warning (" here: %s", p); |
43ff13b4 JM |
2568 | } |
2569 | } | |
2570 | } | |
2571 | /* fall through */ | |
2572 | case 'S': /* Old style status, just signal only */ | |
2573 | status->kind = TARGET_WAITKIND_STOPPED; | |
2574 | status->value.sig = (enum target_signal) | |
2575 | (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))); | |
2576 | ||
2577 | if (buf[3] == 'p') | |
2578 | { | |
2579 | /* Export Cisco kernel mode as a convenience variable | |
c5aa993b | 2580 | (so that it can be used in the GDB prompt if desired). */ |
43ff13b4 JM |
2581 | |
2582 | if (cisco_kernel_mode == 1) | |
c5aa993b | 2583 | set_internalvar (lookup_internalvar ("cisco_kernel_mode"), |
43ff13b4 JM |
2584 | value_from_string ("PDEBUG-")); |
2585 | cisco_kernel_mode = 0; | |
2586 | thread_num = strtol ((const char *) &buf[4], NULL, 16); | |
2587 | record_currthread (thread_num); | |
2588 | } | |
2589 | else if (buf[3] == 'k') | |
2590 | { | |
2591 | /* Export Cisco kernel mode as a convenience variable | |
c5aa993b | 2592 | (so that it can be used in the GDB prompt if desired). */ |
43ff13b4 JM |
2593 | |
2594 | if (cisco_kernel_mode == 1) | |
c5aa993b | 2595 | set_internalvar (lookup_internalvar ("cisco_kernel_mode"), |
43ff13b4 JM |
2596 | value_from_string ("KDEBUG-")); |
2597 | cisco_kernel_mode = 1; | |
2598 | } | |
2599 | goto got_status; | |
2600 | case 'N': /* Cisco special: status and offsets */ | |
2601 | { | |
2602 | bfd_vma text_addr, data_addr, bss_addr; | |
2603 | bfd_signed_vma text_off, data_off, bss_off; | |
2604 | unsigned char *p1; | |
2605 | ||
2606 | status->kind = TARGET_WAITKIND_STOPPED; | |
2607 | status->value.sig = (enum target_signal) | |
2608 | (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))); | |
2609 | ||
c5aa993b | 2610 | if (symfile_objfile == NULL) |
43ff13b4 JM |
2611 | { |
2612 | warning ("Relocation packet recieved with no symbol file. \ | |
2613 | Packet Dropped"); | |
2614 | goto got_status; | |
2615 | } | |
2616 | ||
2617 | /* Relocate object file. Buffer format is NAATT;DD;BB | |
2618 | * where AA is the signal number, TT is the new text | |
2619 | * address, DD * is the new data address, and BB is the | |
2620 | * new bss address. */ | |
2621 | ||
2622 | p = &buf[3]; | |
2623 | text_addr = strtoul (p, (char **) &p1, 16); | |
2624 | if (p1 == p || *p1 != ';') | |
2625 | warning ("Malformed relocation packet: Packet '%s'", buf); | |
2626 | p = p1 + 1; | |
2627 | data_addr = strtoul (p, (char **) &p1, 16); | |
2628 | if (p1 == p || *p1 != ';') | |
2629 | warning ("Malformed relocation packet: Packet '%s'", buf); | |
2630 | p = p1 + 1; | |
2631 | bss_addr = strtoul (p, (char **) &p1, 16); | |
c5aa993b | 2632 | if (p1 == p) |
43ff13b4 JM |
2633 | warning ("Malformed relocation packet: Packet '%s'", buf); |
2634 | ||
2635 | if (remote_cisco_section_offsets (text_addr, data_addr, bss_addr, | |
2636 | &text_off, &data_off, &bss_off) | |
2637 | == 0) | |
c5aa993b | 2638 | if (text_off != 0 || data_off != 0 || bss_off != 0) |
43ff13b4 JM |
2639 | remote_cisco_objfile_relocate (text_off, data_off, bss_off); |
2640 | ||
2641 | goto got_status; | |
2642 | } | |
2643 | case 'W': /* Target exited */ | |
2644 | { | |
2645 | /* The remote process exited. */ | |
2646 | status->kind = TARGET_WAITKIND_EXITED; | |
2647 | status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]); | |
2648 | goto got_status; | |
2649 | } | |
2650 | case 'X': | |
2651 | status->kind = TARGET_WAITKIND_SIGNALLED; | |
2652 | status->value.sig = (enum target_signal) | |
2653 | (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))); | |
2654 | kill_kludge = 1; | |
2655 | ||
2656 | goto got_status; | |
2657 | case 'O': /* Console output */ | |
2658 | remote_console_output (buf + 1); | |
2659 | continue; | |
2660 | case '\0': | |
2661 | if (last_sent_signal != TARGET_SIGNAL_0) | |
2662 | { | |
2663 | /* Zero length reply means that we tried 'S' or 'C' and | |
c5aa993b | 2664 | the remote system doesn't support it. */ |
43ff13b4 JM |
2665 | target_terminal_ours_for_output (); |
2666 | printf_filtered | |
2667 | ("Can't send signals to this remote system. %s not sent.\n", | |
2668 | target_signal_to_name (last_sent_signal)); | |
2669 | last_sent_signal = TARGET_SIGNAL_0; | |
2670 | target_terminal_inferior (); | |
2671 | ||
2672 | strcpy ((char *) buf, last_sent_step ? "s" : "c"); | |
2673 | putpkt ((char *) buf); | |
2674 | continue; | |
2675 | } | |
2676 | /* else fallthrough */ | |
2677 | default: | |
2678 | warning ("Invalid remote reply: %s", buf); | |
2679 | continue; | |
2680 | } | |
2681 | } | |
c5aa993b | 2682 | got_status: |
43ff13b4 JM |
2683 | if (thread_num != -1) |
2684 | { | |
2685 | return thread_num; | |
2686 | } | |
2687 | return inferior_pid; | |
2688 | } | |
2689 | ||
c906108c SS |
2690 | /* Number of bytes of registers this stub implements. */ |
2691 | ||
2692 | static int register_bytes_found; | |
2693 | ||
2694 | /* Read the remote registers into the block REGS. */ | |
2695 | /* Currently we just read all the registers, so we don't use regno. */ | |
2696 | ||
2697 | /* ARGSUSED */ | |
2698 | static void | |
2699 | remote_fetch_registers (regno) | |
2700 | int regno; | |
2701 | { | |
085dd6e6 | 2702 | char *buf = alloca (PBUFSIZ); |
c906108c SS |
2703 | int i; |
2704 | char *p; | |
2705 | char regs[REGISTER_BYTES]; | |
2706 | ||
2707 | set_thread (inferior_pid, 1); | |
2708 | ||
2709 | sprintf (buf, "g"); | |
2710 | remote_send (buf); | |
2711 | ||
2712 | if (remote_register_buf_size == 0) | |
2713 | remote_register_buf_size = strlen (buf); | |
2714 | ||
2715 | /* Unimplemented registers read as all bits zero. */ | |
2716 | memset (regs, 0, REGISTER_BYTES); | |
2717 | ||
2718 | /* We can get out of synch in various cases. If the first character | |
2719 | in the buffer is not a hex character, assume that has happened | |
2720 | and try to fetch another packet to read. */ | |
2721 | while ((buf[0] < '0' || buf[0] > '9') | |
2722 | && (buf[0] < 'a' || buf[0] > 'f') | |
2723 | && buf[0] != 'x') /* New: unavailable register value */ | |
2724 | { | |
2725 | if (remote_debug) | |
0f71a2f6 JM |
2726 | fprintf_unfiltered (gdb_stdlog, |
2727 | "Bad register packet; fetching a new packet\n"); | |
c906108c SS |
2728 | getpkt (buf, 0); |
2729 | } | |
2730 | ||
2731 | /* Reply describes registers byte by byte, each byte encoded as two | |
2732 | hex characters. Suck them all up, then supply them to the | |
2733 | register cacheing/storage mechanism. */ | |
2734 | ||
2735 | p = buf; | |
2736 | for (i = 0; i < REGISTER_BYTES; i++) | |
2737 | { | |
2738 | if (p[0] == 0) | |
2739 | break; | |
2740 | if (p[1] == 0) | |
2741 | { | |
2742 | warning ("Remote reply is of odd length: %s", buf); | |
2743 | /* Don't change register_bytes_found in this case, and don't | |
2744 | print a second warning. */ | |
2745 | goto supply_them; | |
2746 | } | |
2747 | if (p[0] == 'x' && p[1] == 'x') | |
c5aa993b | 2748 | regs[i] = 0; /* 'x' */ |
c906108c SS |
2749 | else |
2750 | regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]); | |
2751 | p += 2; | |
2752 | } | |
2753 | ||
2754 | if (i != register_bytes_found) | |
2755 | { | |
2756 | register_bytes_found = i; | |
2757 | #ifdef REGISTER_BYTES_OK | |
2758 | if (!REGISTER_BYTES_OK (i)) | |
2759 | warning ("Remote reply is too short: %s", buf); | |
2760 | #endif | |
2761 | } | |
c5aa993b JM |
2762 | |
2763 | supply_them: | |
c906108c | 2764 | for (i = 0; i < NUM_REGS; i++) |
c5aa993b JM |
2765 | { |
2766 | supply_register (i, ®s[REGISTER_BYTE (i)]); | |
2767 | if (buf[REGISTER_BYTE (i) * 2] == 'x') | |
2768 | register_valid[i] = -1; /* register value not available */ | |
2769 | } | |
c906108c SS |
2770 | } |
2771 | ||
2772 | /* Prepare to store registers. Since we may send them all (using a | |
2773 | 'G' request), we have to read out the ones we don't want to change | |
2774 | first. */ | |
2775 | ||
c5aa993b | 2776 | static void |
c906108c SS |
2777 | remote_prepare_to_store () |
2778 | { | |
2779 | /* Make sure the entire registers array is valid. */ | |
5a2468f5 JM |
2780 | switch (remote_protocol_P.support) |
2781 | { | |
2782 | case PACKET_DISABLE: | |
2783 | case PACKET_SUPPORT_UNKNOWN: | |
2784 | read_register_bytes (0, (char *) NULL, REGISTER_BYTES); | |
2785 | break; | |
2786 | case PACKET_ENABLE: | |
2787 | break; | |
2788 | } | |
2789 | } | |
2790 | ||
2791 | /* Helper: Attempt to store REGNO using the P packet. Return fail IFF | |
2792 | packet was not recognized. */ | |
2793 | ||
2794 | static int | |
2795 | store_register_using_P (regno) | |
2796 | int regno; | |
2797 | { | |
2798 | /* Try storing a single register. */ | |
2799 | char *buf = alloca (PBUFSIZ); | |
2800 | char *regp; | |
2801 | char *p; | |
2802 | int i; | |
2803 | ||
2804 | sprintf (buf, "P%x=", regno); | |
2805 | p = buf + strlen (buf); | |
2806 | regp = ®isters[REGISTER_BYTE (regno)]; | |
2807 | for (i = 0; i < REGISTER_RAW_SIZE (regno); ++i) | |
2808 | { | |
2809 | *p++ = tohex ((regp[i] >> 4) & 0xf); | |
2810 | *p++ = tohex (regp[i] & 0xf); | |
2811 | } | |
2812 | *p = '\0'; | |
2813 | remote_send (buf); | |
2814 | ||
2815 | return buf[0] != '\0'; | |
c906108c SS |
2816 | } |
2817 | ||
5a2468f5 | 2818 | |
c906108c SS |
2819 | /* Store register REGNO, or all registers if REGNO == -1, from the contents |
2820 | of REGISTERS. FIXME: ignores errors. */ | |
2821 | ||
2822 | static void | |
2823 | remote_store_registers (regno) | |
2824 | int regno; | |
2825 | { | |
085dd6e6 | 2826 | char *buf = alloca (PBUFSIZ); |
c906108c SS |
2827 | int i; |
2828 | char *p; | |
2829 | ||
2830 | set_thread (inferior_pid, 1); | |
2831 | ||
5a2468f5 | 2832 | if (regno >= 0) |
c906108c | 2833 | { |
5a2468f5 | 2834 | switch (remote_protocol_P.support) |
c906108c | 2835 | { |
5a2468f5 JM |
2836 | case PACKET_DISABLE: |
2837 | break; | |
2838 | case PACKET_ENABLE: | |
2839 | if (store_register_using_P (regno)) | |
2840 | return; | |
2841 | else | |
2842 | error ("Protocol error: P packet not recognized by stub"); | |
2843 | case PACKET_SUPPORT_UNKNOWN: | |
2844 | if (store_register_using_P (regno)) | |
2845 | { | |
2846 | /* The stub recognized the 'P' packet. Remember this. */ | |
2847 | remote_protocol_P.support = PACKET_ENABLE; | |
2848 | return; | |
2849 | } | |
2850 | else | |
2851 | { | |
2852 | /* The stub does not support the 'P' packet. Use 'G' | |
2853 | instead, and don't try using 'P' in the future (it | |
2854 | will just waste our time). */ | |
2855 | remote_protocol_P.support = PACKET_DISABLE; | |
2856 | break; | |
2857 | } | |
c906108c | 2858 | } |
c906108c SS |
2859 | } |
2860 | ||
2861 | buf[0] = 'G'; | |
2862 | ||
2863 | /* Command describes registers byte by byte, | |
2864 | each byte encoded as two hex characters. */ | |
2865 | ||
2866 | p = buf + 1; | |
2867 | /* remote_prepare_to_store insures that register_bytes_found gets set. */ | |
2868 | for (i = 0; i < register_bytes_found; i++) | |
2869 | { | |
2870 | *p++ = tohex ((registers[i] >> 4) & 0xf); | |
2871 | *p++ = tohex (registers[i] & 0xf); | |
2872 | } | |
2873 | *p = '\0'; | |
2874 | ||
2875 | remote_send (buf); | |
2876 | } | |
2877 | ||
2878 | /* Use of the data cache *used* to be disabled because it loses for looking | |
2879 | at and changing hardware I/O ports and the like. Accepting `volatile' | |
2880 | would perhaps be one way to fix it. Another idea would be to use the | |
2881 | executable file for the text segment (for all SEC_CODE sections? | |
2882 | For all SEC_READONLY sections?). This has problems if you want to | |
2883 | actually see what the memory contains (e.g. self-modifying code, | |
2884 | clobbered memory, user downloaded the wrong thing). | |
2885 | ||
2886 | Because it speeds so much up, it's now enabled, if you're playing | |
2887 | with registers you turn it of (set remotecache 0). */ | |
2888 | ||
2889 | /* Read a word from remote address ADDR and return it. | |
2890 | This goes through the data cache. */ | |
2891 | ||
c5aa993b | 2892 | #if 0 /* unused? */ |
c906108c SS |
2893 | static int |
2894 | remote_fetch_word (addr) | |
2895 | CORE_ADDR addr; | |
2896 | { | |
2897 | return dcache_fetch (remote_dcache, addr); | |
2898 | } | |
2899 | ||
2900 | /* Write a word WORD into remote address ADDR. | |
2901 | This goes through the data cache. */ | |
2902 | ||
2903 | static void | |
2904 | remote_store_word (addr, word) | |
2905 | CORE_ADDR addr; | |
2906 | int word; | |
2907 | { | |
2908 | dcache_poke (remote_dcache, addr, word); | |
2909 | } | |
c5aa993b | 2910 | #endif /* 0 (unused?) */ |
c906108c SS |
2911 | \f |
2912 | ||
c5aa993b | 2913 | |
c906108c SS |
2914 | /* Return the number of hex digits in num. */ |
2915 | ||
2916 | static int | |
2917 | hexnumlen (num) | |
2918 | ULONGEST num; | |
2919 | { | |
2920 | int i; | |
2921 | ||
2922 | for (i = 0; num != 0; i++) | |
2923 | num >>= 4; | |
2924 | ||
2925 | return max (i, 1); | |
2926 | } | |
2927 | ||
2928 | /* Set BUF to the hex digits representing NUM. */ | |
2929 | ||
2930 | static int | |
2931 | hexnumstr (buf, num) | |
2932 | char *buf; | |
2933 | ULONGEST num; | |
2934 | { | |
2935 | int i; | |
2936 | int len = hexnumlen (num); | |
2937 | ||
2938 | buf[len] = '\0'; | |
2939 | ||
2940 | for (i = len - 1; i >= 0; i--) | |
2941 | { | |
c5aa993b | 2942 | buf[i] = "0123456789abcdef"[(num & 0xf)]; |
c906108c SS |
2943 | num >>= 4; |
2944 | } | |
2945 | ||
2946 | return len; | |
2947 | } | |
2948 | ||
2949 | /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */ | |
2950 | ||
2951 | static CORE_ADDR | |
2952 | remote_address_masked (addr) | |
2953 | CORE_ADDR addr; | |
2954 | { | |
2955 | if (remote_address_size > 0 | |
2956 | && remote_address_size < (sizeof (ULONGEST) * 8)) | |
2957 | { | |
2958 | /* Only create a mask when that mask can safely be constructed | |
2959 | in a ULONGEST variable. */ | |
2960 | ULONGEST mask = 1; | |
2961 | mask = (mask << remote_address_size) - 1; | |
2962 | addr &= mask; | |
2963 | } | |
2964 | return addr; | |
2965 | } | |
2966 | ||
2967 | /* Determine whether the remote target supports binary downloading. | |
2968 | This is accomplished by sending a no-op memory write of zero length | |
2969 | to the target at the specified address. It does not suffice to send | |
2970 | the whole packet, since many stubs strip the eighth bit and subsequently | |
7a292a7a SS |
2971 | compute a wrong checksum, which causes real havoc with remote_write_bytes. |
2972 | ||
96baa820 JM |
2973 | NOTE: This can still lose if the serial line is not eight-bit |
2974 | clean. In cases like this, the user should clear "remote | |
2975 | X-packet". */ | |
2976 | ||
c906108c SS |
2977 | static void |
2978 | check_binary_download (addr) | |
2979 | CORE_ADDR addr; | |
2980 | { | |
96baa820 | 2981 | switch (remote_protocol_binary_download.support) |
c906108c | 2982 | { |
96baa820 JM |
2983 | case PACKET_DISABLE: |
2984 | break; | |
2985 | case PACKET_ENABLE: | |
2986 | break; | |
2987 | case PACKET_SUPPORT_UNKNOWN: | |
2988 | { | |
2989 | char *buf = alloca (PBUFSIZ); | |
2990 | char *p; | |
2991 | ||
2992 | p = buf; | |
2993 | *p++ = 'X'; | |
2994 | p += hexnumstr (p, (ULONGEST) addr); | |
2995 | *p++ = ','; | |
2996 | p += hexnumstr (p, (ULONGEST) 0); | |
2997 | *p++ = ':'; | |
2998 | *p = '\0'; | |
2999 | ||
3000 | putpkt_binary (buf, (int) (p - buf)); | |
3001 | getpkt (buf, 0); | |
c906108c | 3002 | |
96baa820 JM |
3003 | if (buf[0] == '\0') |
3004 | { | |
3005 | if (remote_debug) | |
3006 | fprintf_unfiltered (gdb_stdlog, | |
3007 | "binary downloading NOT suppported by target\n"); | |
3008 | remote_protocol_binary_download.support = PACKET_DISABLE; | |
3009 | } | |
3010 | else | |
3011 | { | |
3012 | if (remote_debug) | |
3013 | fprintf_unfiltered (gdb_stdlog, | |
3014 | "binary downloading suppported by target\n"); | |
3015 | remote_protocol_binary_download.support = PACKET_ENABLE; | |
3016 | } | |
3017 | break; | |
3018 | } | |
c906108c SS |
3019 | } |
3020 | } | |
3021 | ||
3022 | /* Write memory data directly to the remote machine. | |
3023 | This does not inform the data cache; the data cache uses this. | |
3024 | MEMADDR is the address in the remote memory space. | |
3025 | MYADDR is the address of the buffer in our space. | |
3026 | LEN is the number of bytes. | |
3027 | ||
3028 | Returns number of bytes transferred, or 0 for error. */ | |
3029 | ||
3030 | static int | |
3031 | remote_write_bytes (memaddr, myaddr, len) | |
3032 | CORE_ADDR memaddr; | |
3033 | char *myaddr; | |
3034 | int len; | |
3035 | { | |
085dd6e6 | 3036 | unsigned char *buf = alloca (PBUFSIZ); |
c906108c SS |
3037 | int max_buf_size; /* Max size of packet output buffer */ |
3038 | int origlen; | |
3039 | ||
3040 | /* Verify that the target can support a binary download */ | |
3041 | check_binary_download (memaddr); | |
3042 | ||
3043 | /* Chop the transfer down if necessary */ | |
3044 | ||
3045 | max_buf_size = min (remote_write_size, PBUFSIZ); | |
3046 | if (remote_register_buf_size != 0) | |
3047 | max_buf_size = min (max_buf_size, remote_register_buf_size); | |
3048 | ||
7a292a7a | 3049 | /* Subtract header overhead from max payload size - $M<memaddr>,<len>:#nn */ |
c906108c SS |
3050 | max_buf_size -= 2 + hexnumlen (memaddr + len - 1) + 1 + hexnumlen (len) + 4; |
3051 | ||
3052 | origlen = len; | |
3053 | while (len > 0) | |
3054 | { | |
c906108c SS |
3055 | unsigned char *p, *plen; |
3056 | int todo; | |
3057 | int i; | |
3058 | ||
3059 | /* construct "M"<memaddr>","<len>":" */ | |
3060 | /* sprintf (buf, "M%lx,%x:", (unsigned long) memaddr, todo); */ | |
3061 | memaddr = remote_address_masked (memaddr); | |
3062 | p = buf; | |
96baa820 | 3063 | switch (remote_protocol_binary_download.support) |
c906108c | 3064 | { |
96baa820 | 3065 | case PACKET_ENABLE: |
c906108c SS |
3066 | *p++ = 'X'; |
3067 | todo = min (len, max_buf_size); | |
96baa820 JM |
3068 | break; |
3069 | case PACKET_DISABLE: | |
c906108c | 3070 | *p++ = 'M'; |
c5aa993b | 3071 | todo = min (len, max_buf_size / 2); /* num bytes that will fit */ |
96baa820 JM |
3072 | break; |
3073 | case PACKET_SUPPORT_UNKNOWN: | |
3074 | internal_error ("remote_write_bytes: bad switch"); | |
c906108c SS |
3075 | } |
3076 | ||
3077 | p += hexnumstr (p, (ULONGEST) memaddr); | |
3078 | *p++ = ','; | |
3079 | ||
7a292a7a | 3080 | plen = p; /* remember where len field goes */ |
c906108c SS |
3081 | p += hexnumstr (p, (ULONGEST) todo); |
3082 | *p++ = ':'; | |
3083 | *p = '\0'; | |
3084 | ||
3085 | /* We send target system values byte by byte, in increasing byte | |
c5aa993b JM |
3086 | addresses, each byte encoded as two hex characters (or one |
3087 | binary character). */ | |
96baa820 | 3088 | switch (remote_protocol_binary_download.support) |
c906108c | 3089 | { |
96baa820 JM |
3090 | case PACKET_ENABLE: |
3091 | { | |
3092 | int escaped = 0; | |
3093 | for (i = 0; | |
3094 | (i < todo) && (i + escaped) < (max_buf_size - 2); | |
3095 | i++) | |
3096 | { | |
3097 | switch (myaddr[i] & 0xff) | |
3098 | { | |
3099 | case '$': | |
3100 | case '#': | |
3101 | case 0x7d: | |
3102 | /* These must be escaped */ | |
3103 | escaped++; | |
3104 | *p++ = 0x7d; | |
3105 | *p++ = (myaddr[i] & 0xff) ^ 0x20; | |
3106 | break; | |
3107 | default: | |
3108 | *p++ = myaddr[i] & 0xff; | |
3109 | break; | |
3110 | } | |
3111 | } | |
3112 | ||
3113 | if (i < todo) | |
3114 | { | |
3115 | /* Escape chars have filled up the buffer prematurely, | |
3116 | and we have actually sent fewer bytes than planned. | |
3117 | Fix-up the length field of the packet. */ | |
3118 | ||
3119 | /* FIXME: will fail if new len is a shorter string than | |
3120 | old len. */ | |
3121 | ||
3122 | plen += hexnumstr (plen, (ULONGEST) i); | |
3123 | *plen++ = ':'; | |
3124 | } | |
3125 | break; | |
3126 | } | |
3127 | case PACKET_DISABLE: | |
3128 | { | |
3129 | for (i = 0; i < todo; i++) | |
3130 | { | |
3131 | *p++ = tohex ((myaddr[i] >> 4) & 0xf); | |
3132 | *p++ = tohex (myaddr[i] & 0xf); | |
3133 | } | |
3134 | *p = '\0'; | |
3135 | break; | |
3136 | } | |
3137 | case PACKET_SUPPORT_UNKNOWN: | |
3138 | internal_error ("remote_write_bytes: bad switch"); | |
c906108c SS |
3139 | } |
3140 | ||
3141 | putpkt_binary (buf, (int) (p - buf)); | |
3142 | getpkt (buf, 0); | |
3143 | ||
3144 | if (buf[0] == 'E') | |
3145 | { | |
3146 | /* There is no correspondance between what the remote protocol uses | |
3147 | for errors and errno codes. We would like a cleaner way of | |
3148 | representing errors (big enough to include errno codes, bfd_error | |
3149 | codes, and others). But for now just return EIO. */ | |
3150 | errno = EIO; | |
3151 | return 0; | |
3152 | } | |
3153 | ||
3154 | /* Increment by i, not by todo, in case escape chars | |
c5aa993b JM |
3155 | caused us to send fewer bytes than we'd planned. */ |
3156 | myaddr += i; | |
c906108c | 3157 | memaddr += i; |
c5aa993b | 3158 | len -= i; |
c906108c SS |
3159 | } |
3160 | return origlen; | |
3161 | } | |
3162 | ||
3163 | /* Read memory data directly from the remote machine. | |
3164 | This does not use the data cache; the data cache uses this. | |
3165 | MEMADDR is the address in the remote memory space. | |
3166 | MYADDR is the address of the buffer in our space. | |
3167 | LEN is the number of bytes. | |
3168 | ||
3169 | Returns number of bytes transferred, or 0 for error. */ | |
3170 | ||
3171 | static int | |
3172 | remote_read_bytes (memaddr, myaddr, len) | |
3173 | CORE_ADDR memaddr; | |
3174 | char *myaddr; | |
3175 | int len; | |
3176 | { | |
085dd6e6 | 3177 | char *buf = alloca (PBUFSIZ); |
c906108c SS |
3178 | int max_buf_size; /* Max size of packet output buffer */ |
3179 | int origlen; | |
3180 | ||
3181 | /* Chop the transfer down if necessary */ | |
3182 | ||
3183 | max_buf_size = min (remote_write_size, PBUFSIZ); | |
3184 | if (remote_register_buf_size != 0) | |
3185 | max_buf_size = min (max_buf_size, remote_register_buf_size); | |
3186 | ||
3187 | origlen = len; | |
3188 | while (len > 0) | |
3189 | { | |
c906108c SS |
3190 | char *p; |
3191 | int todo; | |
3192 | int i; | |
3193 | ||
c5aa993b | 3194 | todo = min (len, max_buf_size / 2); /* num bytes that will fit */ |
c906108c SS |
3195 | |
3196 | /* construct "m"<memaddr>","<len>" */ | |
3197 | /* sprintf (buf, "m%lx,%x", (unsigned long) memaddr, todo); */ | |
3198 | memaddr = remote_address_masked (memaddr); | |
3199 | p = buf; | |
3200 | *p++ = 'm'; | |
3201 | p += hexnumstr (p, (ULONGEST) memaddr); | |
3202 | *p++ = ','; | |
3203 | p += hexnumstr (p, (ULONGEST) todo); | |
3204 | *p = '\0'; | |
3205 | ||
3206 | putpkt (buf); | |
3207 | getpkt (buf, 0); | |
3208 | ||
3209 | if (buf[0] == 'E') | |
3210 | { | |
3211 | /* There is no correspondance between what the remote protocol uses | |
3212 | for errors and errno codes. We would like a cleaner way of | |
3213 | representing errors (big enough to include errno codes, bfd_error | |
3214 | codes, and others). But for now just return EIO. */ | |
3215 | errno = EIO; | |
3216 | return 0; | |
3217 | } | |
3218 | ||
c5aa993b JM |
3219 | /* Reply describes memory byte by byte, |
3220 | each byte encoded as two hex characters. */ | |
c906108c SS |
3221 | |
3222 | p = buf; | |
3223 | for (i = 0; i < todo; i++) | |
3224 | { | |
3225 | if (p[0] == 0 || p[1] == 0) | |
3226 | /* Reply is short. This means that we were able to read | |
3227 | only part of what we wanted to. */ | |
3228 | return i + (origlen - len); | |
3229 | myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]); | |
3230 | p += 2; | |
3231 | } | |
3232 | myaddr += todo; | |
3233 | memaddr += todo; | |
3234 | len -= todo; | |
3235 | } | |
3236 | return origlen; | |
3237 | } | |
3238 | \f | |
3239 | /* Read or write LEN bytes from inferior memory at MEMADDR, | |
392a587b JM |
3240 | transferring to or from debugger address BUFFER. Write to inferior if |
3241 | SHOULD_WRITE is nonzero. Returns length of data written or read; 0 | |
3242 | for error. */ | |
3243 | ||
3244 | #ifndef REMOTE_TRANSLATE_XFER_ADDRESS | |
3245 | #define REMOTE_TRANSLATE_XFER_ADDRESS(MEM_ADDR, MEM_LEN, TARG_ADDR, TARG_LEN) \ | |
3246 | (*(TARG_ADDR) = (MEM_ADDR), *(TARG_LEN) = (MEM_LEN)) | |
3247 | #endif | |
c906108c SS |
3248 | |
3249 | /* ARGSUSED */ | |
3250 | static int | |
392a587b JM |
3251 | remote_xfer_memory (mem_addr, buffer, mem_len, should_write, target) |
3252 | CORE_ADDR mem_addr; | |
3253 | char *buffer; | |
3254 | int mem_len; | |
c906108c | 3255 | int should_write; |
c5aa993b | 3256 | struct target_ops *target; /* ignored */ |
c906108c | 3257 | { |
392a587b JM |
3258 | CORE_ADDR targ_addr; |
3259 | int targ_len; | |
3260 | REMOTE_TRANSLATE_XFER_ADDRESS (mem_addr, mem_len, &targ_addr, &targ_len); | |
3261 | if (targ_len <= 0) | |
c906108c | 3262 | return 0; |
c906108c | 3263 | |
392a587b JM |
3264 | return dcache_xfer_memory (remote_dcache, targ_addr, buffer, |
3265 | targ_len, should_write); | |
c906108c SS |
3266 | } |
3267 | ||
c5aa993b | 3268 | |
c906108c SS |
3269 | #if 0 |
3270 | /* Enable after 4.12. */ | |
3271 | ||
3272 | void | |
3273 | remote_search (len, data, mask, startaddr, increment, lorange, hirange | |
3274 | addr_found, data_found) | |
3275 | int len; | |
3276 | char *data; | |
3277 | char *mask; | |
3278 | CORE_ADDR startaddr; | |
3279 | int increment; | |
3280 | CORE_ADDR lorange; | |
3281 | CORE_ADDR hirange; | |
3282 | CORE_ADDR *addr_found; | |
3283 | char *data_found; | |
3284 | { | |
3285 | if (increment == -4 && len == 4) | |
3286 | { | |
3287 | long mask_long, data_long; | |
3288 | long data_found_long; | |
3289 | CORE_ADDR addr_we_found; | |
085dd6e6 | 3290 | char *buf = alloca (PBUFSIZ); |
c906108c SS |
3291 | long returned_long[2]; |
3292 | char *p; | |
3293 | ||
3294 | mask_long = extract_unsigned_integer (mask, len); | |
3295 | data_long = extract_unsigned_integer (data, len); | |
3296 | sprintf (buf, "t%x:%x,%x", startaddr, data_long, mask_long); | |
3297 | putpkt (buf); | |
3298 | getpkt (buf, 0); | |
3299 | if (buf[0] == '\0') | |
3300 | { | |
3301 | /* The stub doesn't support the 't' request. We might want to | |
3302 | remember this fact, but on the other hand the stub could be | |
3303 | switched on us. Maybe we should remember it only until | |
3304 | the next "target remote". */ | |
3305 | generic_search (len, data, mask, startaddr, increment, lorange, | |
3306 | hirange, addr_found, data_found); | |
3307 | return; | |
3308 | } | |
3309 | ||
3310 | if (buf[0] == 'E') | |
3311 | /* There is no correspondance between what the remote protocol uses | |
3312 | for errors and errno codes. We would like a cleaner way of | |
3313 | representing errors (big enough to include errno codes, bfd_error | |
3314 | codes, and others). But for now just use EIO. */ | |
3315 | memory_error (EIO, startaddr); | |
3316 | p = buf; | |
3317 | addr_we_found = 0; | |
3318 | while (*p != '\0' && *p != ',') | |
3319 | addr_we_found = (addr_we_found << 4) + fromhex (*p++); | |
3320 | if (*p == '\0') | |
3321 | error ("Protocol error: short return for search"); | |
3322 | ||
3323 | data_found_long = 0; | |
3324 | while (*p != '\0' && *p != ',') | |
3325 | data_found_long = (data_found_long << 4) + fromhex (*p++); | |
3326 | /* Ignore anything after this comma, for future extensions. */ | |
3327 | ||
3328 | if (addr_we_found < lorange || addr_we_found >= hirange) | |
3329 | { | |
3330 | *addr_found = 0; | |
3331 | return; | |
3332 | } | |
3333 | ||
3334 | *addr_found = addr_we_found; | |
3335 | *data_found = store_unsigned_integer (data_we_found, len); | |
3336 | return; | |
3337 | } | |
3338 | generic_search (len, data, mask, startaddr, increment, lorange, | |
3339 | hirange, addr_found, data_found); | |
3340 | } | |
3341 | #endif /* 0 */ | |
3342 | \f | |
3343 | static void | |
3344 | remote_files_info (ignore) | |
3345 | struct target_ops *ignore; | |
3346 | { | |
3347 | puts_filtered ("Debugging a target over a serial line.\n"); | |
3348 | } | |
3349 | \f | |
3350 | /* Stuff for dealing with the packets which are part of this protocol. | |
3351 | See comment at top of file for details. */ | |
3352 | ||
3353 | /* Read a single character from the remote end, masking it down to 7 bits. */ | |
3354 | ||
3355 | static int | |
3356 | readchar (timeout) | |
3357 | int timeout; | |
3358 | { | |
3359 | int ch; | |
3360 | ||
3361 | ch = SERIAL_READCHAR (remote_desc, timeout); | |
3362 | ||
3363 | switch (ch) | |
3364 | { | |
3365 | case SERIAL_EOF: | |
3366 | error ("Remote connection closed"); | |
3367 | case SERIAL_ERROR: | |
3368 | perror_with_name ("Remote communication error"); | |
3369 | case SERIAL_TIMEOUT: | |
3370 | return ch; | |
3371 | default: | |
3372 | return ch & 0x7f; | |
3373 | } | |
3374 | } | |
3375 | ||
3376 | /* Send the command in BUF to the remote machine, and read the reply | |
3377 | into BUF. Report an error if we get an error reply. */ | |
3378 | ||
3379 | static void | |
3380 | remote_send (buf) | |
3381 | char *buf; | |
3382 | { | |
3383 | putpkt (buf); | |
3384 | getpkt (buf, 0); | |
3385 | ||
3386 | if (buf[0] == 'E') | |
3387 | error ("Remote failure reply: %s", buf); | |
3388 | } | |
3389 | ||
3390 | /* Display a null-terminated packet on stdout, for debugging, using C | |
3391 | string notation. */ | |
3392 | ||
3393 | static void | |
3394 | print_packet (buf) | |
3395 | char *buf; | |
3396 | { | |
3397 | puts_filtered ("\""); | |
43e526b9 | 3398 | fputstr_filtered (buf, '"', gdb_stdout); |
c906108c SS |
3399 | puts_filtered ("\""); |
3400 | } | |
3401 | ||
3402 | int | |
3403 | putpkt (buf) | |
3404 | char *buf; | |
3405 | { | |
3406 | return putpkt_binary (buf, strlen (buf)); | |
3407 | } | |
3408 | ||
3409 | /* Send a packet to the remote machine, with error checking. The data | |
3410 | of the packet is in BUF. The string in BUF can be at most PBUFSIZ - 5 | |
3411 | to account for the $, # and checksum, and for a possible /0 if we are | |
3412 | debugging (remote_debug) and want to print the sent packet as a string */ | |
3413 | ||
3414 | static int | |
3415 | putpkt_binary (buf, cnt) | |
3416 | char *buf; | |
3417 | int cnt; | |
3418 | { | |
3419 | int i; | |
3420 | unsigned char csum = 0; | |
085dd6e6 JM |
3421 | char *buf2 = alloca (PBUFSIZ); |
3422 | char *junkbuf = alloca (PBUFSIZ); | |
3423 | ||
c906108c SS |
3424 | int ch; |
3425 | int tcount = 0; | |
3426 | char *p; | |
3427 | ||
3428 | /* Copy the packet into buffer BUF2, encapsulating it | |
3429 | and giving it a checksum. */ | |
3430 | ||
43ff13b4 | 3431 | if (cnt > BUFSIZ - 5) /* Prosanity check */ |
c906108c SS |
3432 | abort (); |
3433 | ||
3434 | p = buf2; | |
3435 | *p++ = '$'; | |
3436 | ||
3437 | for (i = 0; i < cnt; i++) | |
3438 | { | |
3439 | csum += buf[i]; | |
3440 | *p++ = buf[i]; | |
3441 | } | |
3442 | *p++ = '#'; | |
3443 | *p++ = tohex ((csum >> 4) & 0xf); | |
3444 | *p++ = tohex (csum & 0xf); | |
3445 | ||
3446 | /* Send it over and over until we get a positive ack. */ | |
3447 | ||
3448 | while (1) | |
3449 | { | |
3450 | int started_error_output = 0; | |
3451 | ||
3452 | if (remote_debug) | |
3453 | { | |
3454 | *p = '\0'; | |
43e526b9 JM |
3455 | fprintf_unfiltered (gdb_stdlog, "Sending packet: "); |
3456 | fputstrn_unfiltered (buf2, p - buf2, 0, gdb_stdlog); | |
3457 | fprintf_unfiltered (gdb_stdlog, "...", buf2); | |
0f71a2f6 | 3458 | gdb_flush (gdb_stdlog); |
c906108c SS |
3459 | } |
3460 | if (SERIAL_WRITE (remote_desc, buf2, p - buf2)) | |
3461 | perror_with_name ("putpkt: write failed"); | |
3462 | ||
3463 | /* read until either a timeout occurs (-2) or '+' is read */ | |
3464 | while (1) | |
3465 | { | |
3466 | ch = readchar (remote_timeout); | |
3467 | ||
c5aa993b | 3468 | if (remote_debug) |
c906108c SS |
3469 | { |
3470 | switch (ch) | |
3471 | { | |
3472 | case '+': | |
3473 | case SERIAL_TIMEOUT: | |
3474 | case '$': | |
3475 | if (started_error_output) | |
3476 | { | |
3477 | putchar_unfiltered ('\n'); | |
3478 | started_error_output = 0; | |
3479 | } | |
3480 | } | |
3481 | } | |
3482 | ||
3483 | switch (ch) | |
3484 | { | |
3485 | case '+': | |
3486 | if (remote_debug) | |
0f71a2f6 | 3487 | fprintf_unfiltered (gdb_stdlog, "Ack\n"); |
c906108c SS |
3488 | return 1; |
3489 | case SERIAL_TIMEOUT: | |
c5aa993b | 3490 | tcount++; |
c906108c SS |
3491 | if (tcount > 3) |
3492 | return 0; | |
3493 | break; /* Retransmit buffer */ | |
3494 | case '$': | |
3495 | { | |
c5aa993b JM |
3496 | /* It's probably an old response, and we're out of sync. |
3497 | Just gobble up the packet and ignore it. */ | |
c906108c | 3498 | getpkt (junkbuf, 0); |
c5aa993b | 3499 | continue; /* Now, go look for + */ |
c906108c SS |
3500 | } |
3501 | default: | |
3502 | if (remote_debug) | |
3503 | { | |
3504 | if (!started_error_output) | |
3505 | { | |
3506 | started_error_output = 1; | |
0f71a2f6 | 3507 | fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: "); |
c906108c | 3508 | } |
0f71a2f6 | 3509 | fputc_unfiltered (ch & 0177, gdb_stdlog); |
c906108c SS |
3510 | } |
3511 | continue; | |
3512 | } | |
3513 | break; /* Here to retransmit */ | |
3514 | } | |
3515 | ||
3516 | #if 0 | |
3517 | /* This is wrong. If doing a long backtrace, the user should be | |
c5aa993b JM |
3518 | able to get out next time we call QUIT, without anything as |
3519 | violent as interrupt_query. If we want to provide a way out of | |
3520 | here without getting to the next QUIT, it should be based on | |
3521 | hitting ^C twice as in remote_wait. */ | |
c906108c SS |
3522 | if (quit_flag) |
3523 | { | |
3524 | quit_flag = 0; | |
3525 | interrupt_query (); | |
3526 | } | |
3527 | #endif | |
3528 | } | |
3529 | } | |
3530 | ||
0f71a2f6 JM |
3531 | static int remote_cisco_mode; |
3532 | ||
c5aa993b JM |
3533 | static void |
3534 | remote_cisco_expand (src, dest) | |
0f71a2f6 JM |
3535 | char *src; |
3536 | char *dest; | |
3537 | { | |
3538 | int i; | |
3539 | int repeat; | |
3540 | ||
c5aa993b JM |
3541 | do |
3542 | { | |
3543 | if (*src == '*') | |
3544 | { | |
3545 | repeat = (fromhex (src[1]) << 4) + fromhex (src[2]); | |
3546 | for (i = 0; i < repeat; i++) | |
3547 | { | |
3548 | *dest++ = *(src - 1); | |
3549 | } | |
3550 | src += 2; | |
3551 | } | |
3552 | else | |
3553 | { | |
3554 | *dest++ = *src; | |
3555 | } | |
3556 | } | |
3557 | while (*src++); | |
0f71a2f6 JM |
3558 | } |
3559 | ||
c906108c SS |
3560 | /* Come here after finding the start of the frame. Collect the rest |
3561 | into BUF, verifying the checksum, length, and handling run-length | |
3562 | compression. Returns 0 on any error, 1 on success. */ | |
3563 | ||
3564 | static int | |
3565 | read_frame (buf) | |
3566 | char *buf; | |
3567 | { | |
3568 | unsigned char csum; | |
3569 | char *bp; | |
3570 | int c; | |
3571 | ||
3572 | csum = 0; | |
3573 | bp = buf; | |
3574 | ||
3575 | while (1) | |
3576 | { | |
3577 | c = readchar (remote_timeout); | |
3578 | ||
3579 | switch (c) | |
3580 | { | |
3581 | case SERIAL_TIMEOUT: | |
3582 | if (remote_debug) | |
0f71a2f6 | 3583 | fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog); |
c906108c SS |
3584 | return 0; |
3585 | case '$': | |
3586 | if (remote_debug) | |
0f71a2f6 JM |
3587 | fputs_filtered ("Saw new packet start in middle of old one\n", |
3588 | gdb_stdlog); | |
c906108c SS |
3589 | return 0; /* Start a new packet, count retries */ |
3590 | case '#': | |
3591 | { | |
3592 | unsigned char pktcsum; | |
3593 | ||
3594 | *bp = '\000'; | |
3595 | ||
3596 | pktcsum = fromhex (readchar (remote_timeout)) << 4; | |
3597 | pktcsum |= fromhex (readchar (remote_timeout)); | |
3598 | ||
3599 | if (csum == pktcsum) | |
0f71a2f6 JM |
3600 | { |
3601 | if (remote_cisco_mode) /* variant run-length-encoding */ | |
3602 | { | |
085dd6e6 | 3603 | char *tmp_buf = alloca (PBUFSIZ); |
0f71a2f6 JM |
3604 | |
3605 | remote_cisco_expand (buf, tmp_buf); | |
3606 | strcpy (buf, tmp_buf); | |
3607 | } | |
3608 | return 1; | |
3609 | } | |
c906108c | 3610 | |
c5aa993b | 3611 | if (remote_debug) |
c906108c | 3612 | { |
0f71a2f6 | 3613 | fprintf_filtered (gdb_stdlog, |
c5aa993b | 3614 | "Bad checksum, sentsum=0x%x, csum=0x%x, buf=", |
0f71a2f6 JM |
3615 | pktcsum, csum); |
3616 | fputs_filtered (buf, gdb_stdlog); | |
3617 | fputs_filtered ("\n", gdb_stdlog); | |
c906108c SS |
3618 | } |
3619 | return 0; | |
3620 | } | |
3621 | case '*': /* Run length encoding */ | |
0f71a2f6 | 3622 | if (remote_cisco_mode == 0) /* variant run-length-encoding */ |
c906108c | 3623 | { |
0f71a2f6 JM |
3624 | csum += c; |
3625 | c = readchar (remote_timeout); | |
3626 | csum += c; | |
3627 | c = c - ' ' + 3; /* Compute repeat count */ | |
c906108c | 3628 | |
0f71a2f6 JM |
3629 | if (c > 0 && c < 255 && bp + c - 1 < buf + PBUFSIZ - 1) |
3630 | { | |
3631 | memset (bp, *(bp - 1), c); | |
3632 | bp += c; | |
3633 | continue; | |
3634 | } | |
c906108c | 3635 | |
0f71a2f6 JM |
3636 | *bp = '\0'; |
3637 | printf_filtered ("Repeat count %d too large for buffer: ", c); | |
3638 | puts_filtered (buf); | |
3639 | puts_filtered ("\n"); | |
3640 | return 0; | |
3641 | } | |
3642 | /* else fall thru to treat like default */ | |
c906108c SS |
3643 | default: |
3644 | if (bp < buf + PBUFSIZ - 1) | |
3645 | { | |
3646 | *bp++ = c; | |
3647 | csum += c; | |
3648 | continue; | |
3649 | } | |
3650 | ||
3651 | *bp = '\0'; | |
3652 | puts_filtered ("Remote packet too long: "); | |
3653 | puts_filtered (buf); | |
3654 | puts_filtered ("\n"); | |
3655 | ||
3656 | return 0; | |
3657 | } | |
3658 | } | |
3659 | } | |
3660 | ||
3661 | /* Read a packet from the remote machine, with error checking, and | |
3662 | store it in BUF. BUF is expected to be of size PBUFSIZ. If | |
3663 | FOREVER, wait forever rather than timing out; this is used while | |
3664 | the target is executing user code. */ | |
3665 | ||
3666 | void | |
3667 | getpkt (buf, forever) | |
3668 | char *buf; | |
3669 | int forever; | |
3670 | { | |
3671 | int c; | |
3672 | int tries; | |
3673 | int timeout; | |
3674 | int val; | |
3675 | ||
c5aa993b | 3676 | strcpy (buf, "timeout"); |
c906108c SS |
3677 | |
3678 | if (forever) | |
3679 | { | |
c906108c | 3680 | timeout = watchdog > 0 ? watchdog : -1; |
c906108c SS |
3681 | } |
3682 | ||
3683 | else | |
3684 | timeout = remote_timeout; | |
3685 | ||
3686 | #define MAX_TRIES 3 | |
3687 | ||
3688 | for (tries = 1; tries <= MAX_TRIES; tries++) | |
3689 | { | |
3690 | /* This can loop forever if the remote side sends us characters | |
c5aa993b JM |
3691 | continuously, but if it pauses, we'll get a zero from readchar |
3692 | because of timeout. Then we'll count that as a retry. */ | |
c906108c SS |
3693 | |
3694 | /* Note that we will only wait forever prior to the start of a packet. | |
c5aa993b JM |
3695 | After that, we expect characters to arrive at a brisk pace. They |
3696 | should show up within remote_timeout intervals. */ | |
c906108c SS |
3697 | |
3698 | do | |
3699 | { | |
3700 | c = readchar (timeout); | |
3701 | ||
3702 | if (c == SERIAL_TIMEOUT) | |
3703 | { | |
c906108c SS |
3704 | if (forever) /* Watchdog went off. Kill the target. */ |
3705 | { | |
3706 | target_mourn_inferior (); | |
3707 | error ("Watchdog has expired. Target detached.\n"); | |
3708 | } | |
c906108c | 3709 | if (remote_debug) |
0f71a2f6 | 3710 | fputs_filtered ("Timed out.\n", gdb_stdlog); |
c906108c SS |
3711 | goto retry; |
3712 | } | |
3713 | } | |
3714 | while (c != '$'); | |
3715 | ||
3716 | /* We've found the start of a packet, now collect the data. */ | |
3717 | ||
3718 | val = read_frame (buf); | |
3719 | ||
3720 | if (val == 1) | |
3721 | { | |
3722 | if (remote_debug) | |
43e526b9 JM |
3723 | { |
3724 | fprintf_unfiltered (gdb_stdlog, "Packet received: "); | |
3725 | fputstr_unfiltered (buf, 0, gdb_stdlog); | |
3726 | fprintf_unfiltered (gdb_stdlog, "\n"); | |
3727 | } | |
c906108c SS |
3728 | SERIAL_WRITE (remote_desc, "+", 1); |
3729 | return; | |
3730 | } | |
3731 | ||
3732 | /* Try the whole thing again. */ | |
3733 | retry: | |
3734 | SERIAL_WRITE (remote_desc, "-", 1); | |
3735 | } | |
3736 | ||
3737 | /* We have tried hard enough, and just can't receive the packet. Give up. */ | |
3738 | ||
3739 | printf_unfiltered ("Ignoring packet error, continuing...\n"); | |
3740 | SERIAL_WRITE (remote_desc, "+", 1); | |
3741 | } | |
3742 | \f | |
3743 | static void | |
3744 | remote_kill () | |
3745 | { | |
3746 | /* For some mysterious reason, wait_for_inferior calls kill instead of | |
3747 | mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */ | |
3748 | if (kill_kludge) | |
3749 | { | |
3750 | kill_kludge = 0; | |
3751 | target_mourn_inferior (); | |
3752 | return; | |
3753 | } | |
3754 | ||
3755 | /* Use catch_errors so the user can quit from gdb even when we aren't on | |
3756 | speaking terms with the remote system. */ | |
c5aa993b | 3757 | catch_errors ((catch_errors_ftype *) putpkt, "k", "", RETURN_MASK_ERROR); |
c906108c SS |
3758 | |
3759 | /* Don't wait for it to die. I'm not really sure it matters whether | |
3760 | we do or not. For the existing stubs, kill is a noop. */ | |
3761 | target_mourn_inferior (); | |
3762 | } | |
3763 | ||
43ff13b4 JM |
3764 | /* Async version of remote_kill. */ |
3765 | static void | |
3766 | remote_async_kill () | |
3767 | { | |
3768 | /* Unregister the file descriptor from the event loop. */ | |
3769 | if (async_p) | |
3770 | delete_file_handler (remote_desc->fd); | |
3771 | ||
3772 | /* For some mysterious reason, wait_for_inferior calls kill instead of | |
3773 | mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */ | |
3774 | if (kill_kludge) | |
3775 | { | |
3776 | kill_kludge = 0; | |
3777 | target_mourn_inferior (); | |
3778 | return; | |
3779 | } | |
3780 | ||
3781 | /* Use catch_errors so the user can quit from gdb even when we aren't on | |
3782 | speaking terms with the remote system. */ | |
c5aa993b | 3783 | catch_errors ((catch_errors_ftype *) putpkt, "k", "", RETURN_MASK_ERROR); |
43ff13b4 JM |
3784 | |
3785 | /* Don't wait for it to die. I'm not really sure it matters whether | |
3786 | we do or not. For the existing stubs, kill is a noop. */ | |
3787 | target_mourn_inferior (); | |
3788 | } | |
3789 | ||
c906108c SS |
3790 | static void |
3791 | remote_mourn () | |
3792 | { | |
3793 | remote_mourn_1 (&remote_ops); | |
3794 | } | |
3795 | ||
3796 | static void | |
3797 | extended_remote_mourn () | |
3798 | { | |
3799 | /* We do _not_ want to mourn the target like this; this will | |
3800 | remove the extended remote target from the target stack, | |
3801 | and the next time the user says "run" it'll fail. | |
3802 | ||
3803 | FIXME: What is the right thing to do here? */ | |
3804 | #if 0 | |
3805 | remote_mourn_1 (&extended_remote_ops); | |
3806 | #endif | |
3807 | } | |
3808 | ||
3809 | /* Worker function for remote_mourn. */ | |
3810 | static void | |
3811 | remote_mourn_1 (target) | |
3812 | struct target_ops *target; | |
3813 | { | |
3814 | unpush_target (target); | |
3815 | generic_mourn_inferior (); | |
3816 | } | |
3817 | ||
3818 | /* In the extended protocol we want to be able to do things like | |
3819 | "run" and have them basically work as expected. So we need | |
3820 | a special create_inferior function. | |
3821 | ||
3822 | FIXME: One day add support for changing the exec file | |
3823 | we're debugging, arguments and an environment. */ | |
3824 | ||
3825 | static void | |
3826 | extended_remote_create_inferior (exec_file, args, env) | |
3827 | char *exec_file; | |
3828 | char *args; | |
3829 | char **env; | |
3830 | { | |
3831 | /* Rip out the breakpoints; we'll reinsert them after restarting | |
3832 | the remote server. */ | |
3833 | remove_breakpoints (); | |
3834 | ||
3835 | /* Now restart the remote server. */ | |
3836 | extended_remote_restart (); | |
3837 | ||
3838 | /* Now put the breakpoints back in. This way we're safe if the | |
3839 | restart function works via a unix fork on the remote side. */ | |
3840 | insert_breakpoints (); | |
3841 | ||
3842 | /* Clean up from the last time we were running. */ | |
3843 | clear_proceed_status (); | |
3844 | ||
3845 | /* Let the remote process run. */ | |
3846 | proceed (-1, TARGET_SIGNAL_0, 0); | |
3847 | } | |
3848 | ||
43ff13b4 JM |
3849 | /* Async version of extended_remote_create_inferior. */ |
3850 | static void | |
3851 | extended_remote_async_create_inferior (exec_file, args, env) | |
3852 | char *exec_file; | |
3853 | char *args; | |
3854 | char **env; | |
3855 | { | |
3856 | /* Rip out the breakpoints; we'll reinsert them after restarting | |
3857 | the remote server. */ | |
3858 | remove_breakpoints (); | |
3859 | ||
3860 | /* If running asynchronously, register the target file descriptor | |
3861 | with the event loop. */ | |
3862 | if (async_p) | |
3863 | add_file_handler (remote_desc->fd, (file_handler_func *) fetch_inferior_event, 0); | |
3864 | ||
3865 | /* Now restart the remote server. */ | |
3866 | extended_remote_restart (); | |
3867 | ||
3868 | /* Now put the breakpoints back in. This way we're safe if the | |
3869 | restart function works via a unix fork on the remote side. */ | |
3870 | insert_breakpoints (); | |
3871 | ||
3872 | /* Clean up from the last time we were running. */ | |
3873 | clear_proceed_status (); | |
3874 | ||
3875 | /* Let the remote process run. */ | |
3876 | proceed (-1, TARGET_SIGNAL_0, 0); | |
3877 | } | |
c906108c | 3878 | \f |
c5aa993b | 3879 | |
c906108c SS |
3880 | /* On some machines, e.g. 68k, we may use a different breakpoint instruction |
3881 | than other targets; in those use REMOTE_BREAKPOINT instead of just | |
3882 | BREAKPOINT. Also, bi-endian targets may define LITTLE_REMOTE_BREAKPOINT | |
3883 | and BIG_REMOTE_BREAKPOINT. If none of these are defined, we just call | |
3884 | the standard routines that are in mem-break.c. */ | |
3885 | ||
3886 | /* FIXME, these ought to be done in a more dynamic fashion. For instance, | |
3887 | the choice of breakpoint instruction affects target program design and | |
3888 | vice versa, and by making it user-tweakable, the special code here | |
3889 | goes away and we need fewer special GDB configurations. */ | |
3890 | ||
3891 | #if defined (LITTLE_REMOTE_BREAKPOINT) && defined (BIG_REMOTE_BREAKPOINT) && !defined(REMOTE_BREAKPOINT) | |
3892 | #define REMOTE_BREAKPOINT | |
3893 | #endif | |
3894 | ||
3895 | #ifdef REMOTE_BREAKPOINT | |
3896 | ||
3897 | /* If the target isn't bi-endian, just pretend it is. */ | |
3898 | #if !defined (LITTLE_REMOTE_BREAKPOINT) && !defined (BIG_REMOTE_BREAKPOINT) | |
3899 | #define LITTLE_REMOTE_BREAKPOINT REMOTE_BREAKPOINT | |
3900 | #define BIG_REMOTE_BREAKPOINT REMOTE_BREAKPOINT | |
3901 | #endif | |
3902 | ||
3903 | static unsigned char big_break_insn[] = BIG_REMOTE_BREAKPOINT; | |
3904 | static unsigned char little_break_insn[] = LITTLE_REMOTE_BREAKPOINT; | |
3905 | ||
3906 | #endif /* REMOTE_BREAKPOINT */ | |
3907 | ||
3908 | /* Insert a breakpoint on targets that don't have any better breakpoint | |
3909 | support. We read the contents of the target location and stash it, | |
3910 | then overwrite it with a breakpoint instruction. ADDR is the target | |
3911 | location in the target machine. CONTENTS_CACHE is a pointer to | |
3912 | memory allocated for saving the target contents. It is guaranteed | |
3913 | by the caller to be long enough to save sizeof BREAKPOINT bytes (this | |
3914 | is accomplished via BREAKPOINT_MAX). */ | |
3915 | ||
3916 | static int | |
3917 | remote_insert_breakpoint (addr, contents_cache) | |
3918 | CORE_ADDR addr; | |
3919 | char *contents_cache; | |
3920 | { | |
3921 | #ifdef REMOTE_BREAKPOINT | |
3922 | int val; | |
96baa820 JM |
3923 | #endif |
3924 | int bp_size; | |
3925 | ||
3926 | /* Try the "Z" packet if it is not already disabled. | |
3927 | If it succeeds, then set the support to PACKET_ENABLE. | |
3928 | If it fails, and the user has explicitly requested the Z support | |
3929 | then report an error, otherwise, mark it disabled and go on. */ | |
3930 | ||
3931 | if ((remote_protocol_Z.support == PACKET_ENABLE) | |
3932 | || (remote_protocol_Z.support == PACKET_SUPPORT_UNKNOWN)) | |
3933 | { | |
3934 | char buf[PBUFSIZ], *p = buf; | |
3935 | ||
3936 | addr = remote_address_masked (addr); | |
3937 | *(p++) = 'Z'; | |
3938 | *(p++) = '0'; | |
3939 | *(p++) = ','; | |
3940 | p += hexnumstr (p, (ULONGEST) addr); | |
3941 | BREAKPOINT_FROM_PC (&addr, &bp_size); | |
3942 | sprintf (p, ",%d", bp_size); | |
3943 | ||
3944 | putpkt (buf); | |
3945 | getpkt (buf, 0); | |
3946 | ||
3947 | if (buf[0] != '\0') | |
3948 | { | |
3949 | remote_protocol_Z.support = PACKET_ENABLE; | |
3950 | return (buf[0] == 'E'); | |
3951 | } | |
3952 | ||
3953 | /* The stub does not support the 'Z' request. If the user has | |
3954 | explicitly requested the Z support, or if the stub previously | |
3955 | said it supported the packet, this is an error, | |
3956 | otherwise, mark it disabled. */ | |
3957 | ||
3958 | else if (remote_protocol_Z.support == PACKET_ENABLE) | |
3959 | { | |
3960 | error ("Protocol error: Z packet not recognized by stub"); | |
3961 | } | |
3962 | else | |
3963 | { | |
3964 | remote_protocol_Z.support = PACKET_DISABLE; | |
3965 | } | |
3966 | } | |
c906108c | 3967 | |
96baa820 | 3968 | #ifdef REMOTE_BREAKPOINT |
c906108c SS |
3969 | val = target_read_memory (addr, contents_cache, sizeof big_break_insn); |
3970 | ||
3971 | if (val == 0) | |
3972 | { | |
3973 | if (TARGET_BYTE_ORDER == BIG_ENDIAN) | |
3974 | val = target_write_memory (addr, (char *) big_break_insn, | |
3975 | sizeof big_break_insn); | |
3976 | else | |
3977 | val = target_write_memory (addr, (char *) little_break_insn, | |
3978 | sizeof little_break_insn); | |
3979 | } | |
3980 | ||
3981 | return val; | |
3982 | #else | |
3983 | return memory_insert_breakpoint (addr, contents_cache); | |
3984 | #endif /* REMOTE_BREAKPOINT */ | |
3985 | } | |
3986 | ||
3987 | static int | |
3988 | remote_remove_breakpoint (addr, contents_cache) | |
3989 | CORE_ADDR addr; | |
3990 | char *contents_cache; | |
3991 | { | |
96baa820 JM |
3992 | char buf[PBUFSIZ]; |
3993 | int bp_size; | |
3994 | ||
3995 | if ((remote_protocol_Z.support == PACKET_ENABLE) | |
3996 | || (remote_protocol_Z.support == PACKET_SUPPORT_UNKNOWN)) | |
3997 | { | |
3998 | char buf[PBUFSIZ], *p = buf; | |
3999 | ||
4000 | *(p++) = 'z'; | |
4001 | *(p++) = '0'; | |
4002 | *(p++) = ','; | |
4003 | ||
4004 | addr = remote_address_masked (addr); | |
4005 | p += hexnumstr (p, (ULONGEST) addr); | |
4006 | BREAKPOINT_FROM_PC (&addr, &bp_size); | |
4007 | sprintf (p, ",%d", bp_size); | |
4008 | ||
4009 | putpkt (buf); | |
4010 | getpkt (buf, 0); | |
4011 | ||
4012 | return (buf[0] == 'E'); | |
4013 | } | |
4014 | ||
c906108c SS |
4015 | #ifdef REMOTE_BREAKPOINT |
4016 | return target_write_memory (addr, contents_cache, sizeof big_break_insn); | |
4017 | #else | |
4018 | return memory_remove_breakpoint (addr, contents_cache); | |
4019 | #endif /* REMOTE_BREAKPOINT */ | |
4020 | } | |
4021 | ||
96baa820 JM |
4022 | #ifdef TARGET_HAS_HARDWARE_WATCHPOINTS |
4023 | int | |
4024 | remote_insert_watchpoint (addr, len, type) | |
4025 | CORE_ADDR addr; | |
4026 | int len; | |
4027 | int type; | |
4028 | { | |
4029 | char buf[PBUFSIZ], *p; | |
4030 | ||
4031 | if (remote_protocol_Z.support == PACKET_DISABLE) | |
4032 | error ("Can't set hardware watchpoints without the 'Z' packet\n"); | |
4033 | ||
4034 | sprintf (buf, "Z%x,", type + 2 ); | |
4035 | p = strchr (buf, '\0'); | |
4036 | addr = remote_address_masked (addr); | |
4037 | p += hexnumstr (p, (ULONGEST) addr); | |
4038 | sprintf (p, ",%lx", len); | |
4039 | ||
4040 | putpkt (buf); | |
4041 | getpkt (buf, 0); | |
4042 | ||
4043 | if (buf[0] == '\0' || buf [0] == 'E') | |
4044 | return -1; | |
4045 | ||
4046 | return 0; | |
4047 | } | |
4048 | ||
4049 | int | |
4050 | remote_remove_watchpoint (addr, len, type) | |
4051 | CORE_ADDR addr; | |
4052 | int len; | |
4053 | int type; | |
4054 | { | |
4055 | char buf[PBUFSIZ], *p; | |
4056 | ||
4057 | sprintf (buf, "z%x,", type + 2 ); | |
4058 | p = strchr (buf, '\0'); | |
4059 | addr = remote_address_masked (addr); | |
4060 | p += hexnumstr (p, (ULONGEST) addr); | |
4061 | sprintf (p, ",%lx", len); | |
4062 | putpkt (buf); | |
4063 | getpkt (buf, 0); | |
4064 | ||
4065 | if (buf[0] == '\0' || buf [0] == 'E') | |
4066 | return -1; | |
4067 | ||
4068 | return 0; | |
4069 | } | |
4070 | ||
4071 | int | |
4072 | remote_insert_hw_breakpoint (addr, len) | |
4073 | CORE_ADDR addr; | |
4074 | int len; | |
4075 | { | |
4076 | char buf[PBUFSIZ], *p = buf; | |
4077 | ||
4078 | if (remote_protocol_Z.support == PACKET_DISABLE) | |
4079 | error ("Can't set hardware breakpoints without the 'Z' packet\n"); | |
4080 | ||
4081 | *(p++) = 'Z'; | |
4082 | *(p++) = '1'; | |
4083 | *(p++) = ','; | |
4084 | ||
4085 | addr = remote_address_masked (addr); | |
4086 | p += hexnumstr (p, (ULONGEST) addr); | |
4087 | *p = '\0'; | |
4088 | ||
4089 | putpkt (buf); | |
4090 | getpkt (buf, 0); | |
4091 | ||
4092 | if (buf[0] == '\0' || buf [0] == 'E') | |
4093 | return -1; | |
4094 | ||
4095 | return 0; | |
4096 | } | |
4097 | ||
4098 | int | |
4099 | remote_remove_hw_breakpoint (addr, len) | |
4100 | CORE_ADDR addr; | |
4101 | int len; | |
4102 | { | |
4103 | char buf[PBUFSIZ], *p = buf; | |
4104 | ||
4105 | *(p++) = 'z'; | |
4106 | *(p++) = '1'; | |
4107 | *(p++) = ','; | |
4108 | ||
4109 | addr = remote_address_masked (addr); | |
4110 | p += hexnumstr (p, (ULONGEST) addr); | |
4111 | *p = '\0'; | |
4112 | ||
4113 | putpkt(buf); | |
4114 | getpkt (buf, 0); | |
4115 | ||
4116 | if (buf[0] == '\0' || buf [0] == 'E') | |
4117 | return -1; | |
4118 | ||
4119 | return 0; | |
4120 | } | |
4121 | #endif | |
4122 | ||
c906108c SS |
4123 | /* Some targets are only capable of doing downloads, and afterwards |
4124 | they switch to the remote serial protocol. This function provides | |
4125 | a clean way to get from the download target to the remote target. | |
4126 | It's basically just a wrapper so that we don't have to expose any | |
4127 | of the internal workings of remote.c. | |
4128 | ||
4129 | Prior to calling this routine, you should shutdown the current | |
4130 | target code, else you will get the "A program is being debugged | |
4131 | already..." message. Usually a call to pop_target() suffices. */ | |
4132 | ||
4133 | void | |
4134 | push_remote_target (name, from_tty) | |
4135 | char *name; | |
4136 | int from_tty; | |
4137 | { | |
4138 | printf_filtered ("Switching to remote protocol\n"); | |
4139 | remote_open (name, from_tty); | |
4140 | } | |
4141 | ||
4142 | /* Other targets want to use the entire remote serial module but with | |
4143 | certain remote_ops overridden. */ | |
4144 | ||
4145 | void | |
4146 | open_remote_target (name, from_tty, target, extended_p) | |
4147 | char *name; | |
4148 | int from_tty; | |
4149 | struct target_ops *target; | |
4150 | int extended_p; | |
4151 | { | |
4152 | printf_filtered ("Selecting the %sremote protocol\n", | |
4153 | (extended_p ? "extended-" : "")); | |
4154 | remote_open_1 (name, from_tty, target, extended_p); | |
4155 | } | |
4156 | ||
4157 | /* Table used by the crc32 function to calcuate the checksum. */ | |
4158 | ||
c5aa993b JM |
4159 | static unsigned long crc32_table[256] = |
4160 | {0, 0}; | |
c906108c SS |
4161 | |
4162 | static unsigned long | |
4163 | crc32 (buf, len, crc) | |
4164 | unsigned char *buf; | |
4165 | int len; | |
4166 | unsigned int crc; | |
4167 | { | |
c5aa993b | 4168 | if (!crc32_table[1]) |
c906108c SS |
4169 | { |
4170 | /* Initialize the CRC table and the decoding table. */ | |
4171 | int i, j; | |
4172 | unsigned int c; | |
4173 | ||
4174 | for (i = 0; i < 256; i++) | |
c5aa993b JM |
4175 | { |
4176 | for (c = i << 24, j = 8; j > 0; --j) | |
4177 | c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1); | |
4178 | crc32_table[i] = c; | |
4179 | } | |
c906108c SS |
4180 | } |
4181 | ||
4182 | while (len--) | |
4183 | { | |
4184 | crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *buf) & 255]; | |
4185 | buf++; | |
4186 | } | |
4187 | return crc; | |
4188 | } | |
4189 | ||
4190 | /* compare-sections command | |
4191 | ||
4192 | With no arguments, compares each loadable section in the exec bfd | |
4193 | with the same memory range on the target, and reports mismatches. | |
4194 | Useful for verifying the image on the target against the exec file. | |
4195 | Depends on the target understanding the new "qCRC:" request. */ | |
4196 | ||
4197 | static void | |
4198 | compare_sections_command (args, from_tty) | |
4199 | char *args; | |
4200 | int from_tty; | |
4201 | { | |
4202 | asection *s; | |
4203 | unsigned long host_crc, target_crc; | |
4204 | extern bfd *exec_bfd; | |
4205 | struct cleanup *old_chain; | |
085dd6e6 JM |
4206 | char *tmp; |
4207 | char *sectdata; | |
4208 | char *sectname; | |
4209 | char *buf = alloca (PBUFSIZ); | |
c906108c SS |
4210 | bfd_size_type size; |
4211 | bfd_vma lma; | |
4212 | int matched = 0; | |
4213 | int mismatched = 0; | |
4214 | ||
4215 | if (!exec_bfd) | |
4216 | error ("command cannot be used without an exec file"); | |
4217 | if (!current_target.to_shortname || | |
4218 | strcmp (current_target.to_shortname, "remote") != 0) | |
4219 | error ("command can only be used with remote target"); | |
4220 | ||
c5aa993b | 4221 | for (s = exec_bfd->sections; s; s = s->next) |
c906108c SS |
4222 | { |
4223 | if (!(s->flags & SEC_LOAD)) | |
c5aa993b | 4224 | continue; /* skip non-loadable section */ |
c906108c SS |
4225 | |
4226 | size = bfd_get_section_size_before_reloc (s); | |
4227 | if (size == 0) | |
c5aa993b | 4228 | continue; /* skip zero-length section */ |
c906108c SS |
4229 | |
4230 | sectname = (char *) bfd_get_section_name (exec_bfd, s); | |
4231 | if (args && strcmp (args, sectname) != 0) | |
c5aa993b | 4232 | continue; /* not the section selected by user */ |
c906108c | 4233 | |
c5aa993b | 4234 | matched = 1; /* do this section */ |
c906108c SS |
4235 | lma = s->lma; |
4236 | /* FIXME: assumes lma can fit into long */ | |
4237 | sprintf (buf, "qCRC:%lx,%lx", (long) lma, (long) size); | |
4238 | putpkt (buf); | |
4239 | ||
4240 | /* be clever; compute the host_crc before waiting for target reply */ | |
4241 | sectdata = xmalloc (size); | |
4242 | old_chain = make_cleanup (free, sectdata); | |
4243 | bfd_get_section_contents (exec_bfd, s, sectdata, 0, size); | |
4244 | host_crc = crc32 ((unsigned char *) sectdata, size, 0xffffffff); | |
4245 | ||
4246 | getpkt (buf, 0); | |
4247 | if (buf[0] == 'E') | |
4248 | error ("target memory fault, section %s, range 0x%08x -- 0x%08x", | |
4249 | sectname, lma, lma + size); | |
4250 | if (buf[0] != 'C') | |
4251 | error ("remote target does not support this operation"); | |
4252 | ||
4253 | for (target_crc = 0, tmp = &buf[1]; *tmp; tmp++) | |
4254 | target_crc = target_crc * 16 + fromhex (*tmp); | |
4255 | ||
4256 | printf_filtered ("Section %s, range 0x%08x -- 0x%08x: ", | |
4257 | sectname, lma, lma + size); | |
4258 | if (host_crc == target_crc) | |
4259 | printf_filtered ("matched.\n"); | |
4260 | else | |
c5aa993b JM |
4261 | { |
4262 | printf_filtered ("MIS-MATCHED!\n"); | |
4263 | mismatched++; | |
4264 | } | |
c906108c SS |
4265 | |
4266 | do_cleanups (old_chain); | |
4267 | } | |
4268 | if (mismatched > 0) | |
4269 | warning ("One or more sections of the remote executable does not match\n\ | |
4270 | the loaded file\n"); | |
4271 | if (args && !matched) | |
4272 | printf_filtered ("No loaded section named '%s'.\n", args); | |
4273 | } | |
4274 | ||
4275 | static int | |
4276 | remote_query (query_type, buf, outbuf, bufsiz) | |
4277 | int query_type; | |
4278 | char *buf; | |
4279 | char *outbuf; | |
4280 | int *bufsiz; | |
4281 | { | |
4282 | int i; | |
085dd6e6 | 4283 | char *buf2 = alloca (PBUFSIZ); |
c906108c SS |
4284 | char *p2 = &buf2[0]; |
4285 | char *p = buf; | |
4286 | ||
c5aa993b | 4287 | if (!bufsiz) |
c906108c SS |
4288 | error ("null pointer to remote bufer size specified"); |
4289 | ||
cce74817 | 4290 | /* minimum outbuf size is PBUFSIZ - if bufsiz is not large enough let |
c906108c SS |
4291 | the caller know and return what the minimum size is */ |
4292 | /* Note: a zero bufsiz can be used to query the minimum buffer size */ | |
c5aa993b | 4293 | if (*bufsiz < PBUFSIZ) |
c906108c SS |
4294 | { |
4295 | *bufsiz = PBUFSIZ; | |
4296 | return -1; | |
4297 | } | |
4298 | ||
4299 | /* except for querying the minimum buffer size, target must be open */ | |
c5aa993b | 4300 | if (!remote_desc) |
c906108c SS |
4301 | error ("remote query is only available after target open"); |
4302 | ||
4303 | /* we only take uppercase letters as query types, at least for now */ | |
c5aa993b | 4304 | if ((query_type < 'A') || (query_type > 'Z')) |
c906108c SS |
4305 | error ("invalid remote query type"); |
4306 | ||
c5aa993b | 4307 | if (!buf) |
c906108c SS |
4308 | error ("null remote query specified"); |
4309 | ||
c5aa993b | 4310 | if (!outbuf) |
c906108c SS |
4311 | error ("remote query requires a buffer to receive data"); |
4312 | ||
4313 | outbuf[0] = '\0'; | |
4314 | ||
4315 | *p2++ = 'q'; | |
4316 | *p2++ = query_type; | |
4317 | ||
4318 | /* we used one buffer char for the remote protocol q command and another | |
4319 | for the query type. As the remote protocol encapsulation uses 4 chars | |
4320 | plus one extra in case we are debugging (remote_debug), | |
4321 | we have PBUFZIZ - 7 left to pack the query string */ | |
4322 | i = 0; | |
c5aa993b | 4323 | while (buf[i] && (i < (PBUFSIZ - 8))) |
c906108c SS |
4324 | { |
4325 | /* bad caller may have sent forbidden characters */ | |
c5aa993b JM |
4326 | if ((!isprint (buf[i])) || (buf[i] == '$') || (buf[i] == '#')) |
4327 | error ("illegal characters in query string"); | |
c906108c SS |
4328 | |
4329 | *p2++ = buf[i]; | |
4330 | i++; | |
4331 | } | |
4332 | *p2 = buf[i]; | |
4333 | ||
c5aa993b | 4334 | if (buf[i]) |
c906108c SS |
4335 | error ("query larger than available buffer"); |
4336 | ||
4337 | i = putpkt (buf2); | |
c5aa993b JM |
4338 | if (i < 0) |
4339 | return i; | |
c906108c SS |
4340 | |
4341 | getpkt (outbuf, 0); | |
4342 | ||
4343 | return 0; | |
4344 | } | |
4345 | ||
96baa820 JM |
4346 | static void |
4347 | remote_rcmd (char *command, | |
4348 | struct gdb_file *outbuf) | |
4349 | { | |
4350 | int i; | |
4351 | char *buf = alloca (PBUFSIZ); | |
4352 | char *p = buf; | |
4353 | ||
4354 | if (!remote_desc) | |
4355 | error ("remote rcmd is only available after target open"); | |
4356 | ||
4357 | /* The query prefix */ | |
4358 | strcpy (buf, "qRcmd,"); | |
4359 | p = strchr (buf, '\0'); | |
4360 | ||
4361 | if ((strlen (buf) + strlen (command) * 2 + 8/*misc*/) > PBUFSIZ) | |
4362 | error ("\"monitor\" command ``%s'' is too long\n", command); | |
4363 | ||
4364 | /* Encode the actual command */ | |
4365 | for (i = 0; command[i]; i++) | |
4366 | { | |
4367 | *p++ = tohex ((command[i] >> 4) & 0xf); | |
4368 | *p++ = tohex (command[i] & 0xf); | |
4369 | } | |
4370 | *p = '\0'; | |
4371 | ||
4372 | if (putpkt (buf) < 0) | |
4373 | error ("Communication problem with target\n"); | |
4374 | ||
4375 | /* get/display the response */ | |
4376 | while (1) | |
4377 | { | |
4378 | /* XXX - see also tracepoint.c:remote_get_noisy_reply() */ | |
4379 | buf[0] = '\0'; | |
4380 | getpkt (buf, 0); | |
4381 | if (buf[0] == '\0') | |
4382 | error ("Target does not support this command\n"); | |
4383 | if (buf[0] == 'O' && buf[1] != 'K') | |
4384 | { | |
4385 | remote_console_output (buf + 1); /* 'O' message from stub */ | |
4386 | continue; | |
4387 | } | |
4388 | if (strcmp (buf, "OK") == 0) | |
4389 | break; | |
4390 | for (p = buf; p[0] != '\0' && p[1] != '\0'; p += 2) | |
4391 | { | |
4392 | char c = (fromhex (p[0]) << 4) + fromhex (p[1]); | |
4393 | fputc_unfiltered (c, outbuf); | |
4394 | } | |
4395 | break; | |
4396 | } | |
4397 | } | |
4398 | ||
c906108c SS |
4399 | static void |
4400 | packet_command (args, from_tty) | |
4401 | char *args; | |
4402 | int from_tty; | |
4403 | { | |
085dd6e6 | 4404 | char *buf = alloca (PBUFSIZ); |
c906108c | 4405 | |
c5aa993b | 4406 | if (!remote_desc) |
c906108c SS |
4407 | error ("command can only be used with remote target"); |
4408 | ||
c5aa993b | 4409 | if (!args) |
c906108c SS |
4410 | error ("remote-packet command requires packet text as argument"); |
4411 | ||
4412 | puts_filtered ("sending: "); | |
4413 | print_packet (args); | |
4414 | puts_filtered ("\n"); | |
4415 | putpkt (args); | |
4416 | ||
4417 | getpkt (buf, 0); | |
4418 | puts_filtered ("received: "); | |
4419 | print_packet (buf); | |
4420 | puts_filtered ("\n"); | |
4421 | } | |
4422 | ||
4423 | #if 0 | |
4424 | /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------------- */ | |
4425 | ||
c5aa993b | 4426 | static void display_thread_info PARAMS ((struct gdb_ext_thread_info * info)); |
c906108c SS |
4427 | |
4428 | static void threadset_test_cmd PARAMS ((char *cmd, int tty)); | |
4429 | ||
4430 | static void threadalive_test PARAMS ((char *cmd, int tty)); | |
4431 | ||
4432 | static void threadlist_test_cmd PARAMS ((char *cmd, int tty)); | |
4433 | ||
c5aa993b | 4434 | int get_and_display_threadinfo PARAMS ((threadref * ref)); |
c906108c SS |
4435 | |
4436 | static void threadinfo_test_cmd PARAMS ((char *cmd, int tty)); | |
4437 | ||
c5aa993b | 4438 | static int thread_display_step PARAMS ((threadref * ref, void *context)); |
c906108c SS |
4439 | |
4440 | static void threadlist_update_test_cmd PARAMS ((char *cmd, int tty)); | |
4441 | ||
4442 | static void init_remote_threadtests PARAMS ((void)); | |
4443 | ||
c5aa993b | 4444 | #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid */ |
c906108c SS |
4445 | |
4446 | static void | |
4447 | threadset_test_cmd (cmd, tty) | |
4448 | char *cmd; | |
4449 | int tty; | |
4450 | { | |
4451 | int sample_thread = SAMPLE_THREAD; | |
4452 | ||
4453 | printf_filtered ("Remote threadset test\n"); | |
4454 | set_thread (sample_thread, 1); | |
4455 | } | |
4456 | ||
4457 | ||
4458 | static void | |
4459 | threadalive_test (cmd, tty) | |
4460 | char *cmd; | |
4461 | int tty; | |
4462 | { | |
4463 | int sample_thread = SAMPLE_THREAD; | |
4464 | ||
4465 | if (remote_thread_alive (sample_thread)) | |
4466 | printf_filtered ("PASS: Thread alive test\n"); | |
4467 | else | |
4468 | printf_filtered ("FAIL: Thread alive test\n"); | |
4469 | } | |
4470 | ||
4471 | void output_threadid PARAMS ((char *title, threadref * ref)); | |
4472 | ||
4473 | void | |
4474 | output_threadid (title, ref) | |
4475 | char *title; | |
4476 | threadref *ref; | |
4477 | { | |
4478 | char hexid[20]; | |
4479 | ||
4480 | pack_threadid (&hexid[0], ref); /* Convert threead id into hex */ | |
4481 | hexid[16] = 0; | |
4482 | printf_filtered ("%s %s\n", title, (&hexid[0])); | |
4483 | } | |
4484 | ||
4485 | static void | |
4486 | threadlist_test_cmd (cmd, tty) | |
4487 | char *cmd; | |
4488 | int tty; | |
4489 | { | |
4490 | int startflag = 1; | |
4491 | threadref nextthread; | |
4492 | int done, result_count; | |
4493 | threadref threadlist[3]; | |
4494 | ||
4495 | printf_filtered ("Remote Threadlist test\n"); | |
4496 | if (!remote_get_threadlist (startflag, &nextthread, 3, &done, | |
4497 | &result_count, &threadlist[0])) | |
4498 | printf_filtered ("FAIL: threadlist test\n"); | |
4499 | else | |
4500 | { | |
4501 | threadref *scan = threadlist; | |
4502 | threadref *limit = scan + result_count; | |
4503 | ||
4504 | while (scan < limit) | |
4505 | output_threadid (" thread ", scan++); | |
4506 | } | |
4507 | } | |
4508 | ||
4509 | void | |
4510 | display_thread_info (info) | |
4511 | struct gdb_ext_thread_info *info; | |
4512 | { | |
4513 | output_threadid ("Threadid: ", &info->threadid); | |
4514 | printf_filtered ("Name: %s\n ", info->shortname); | |
4515 | printf_filtered ("State: %s\n", info->display); | |
4516 | printf_filtered ("other: %s\n\n", info->more_display); | |
4517 | } | |
4518 | ||
4519 | int | |
4520 | get_and_display_threadinfo (ref) | |
4521 | threadref *ref; | |
4522 | { | |
4523 | int result; | |
4524 | int set; | |
4525 | struct gdb_ext_thread_info threadinfo; | |
4526 | ||
4527 | set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME | |
4528 | | TAG_MOREDISPLAY | TAG_DISPLAY; | |
4529 | if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo))) | |
4530 | display_thread_info (&threadinfo); | |
4531 | return result; | |
4532 | } | |
4533 | ||
4534 | static void | |
4535 | threadinfo_test_cmd (cmd, tty) | |
4536 | char *cmd; | |
4537 | int tty; | |
4538 | { | |
4539 | int athread = SAMPLE_THREAD; | |
4540 | threadref thread; | |
4541 | int set; | |
4542 | ||
4543 | int_to_threadref (&thread, athread); | |
4544 | printf_filtered ("Remote Threadinfo test\n"); | |
4545 | if (!get_and_display_threadinfo (&thread)) | |
4546 | printf_filtered ("FAIL cannot get thread info\n"); | |
4547 | } | |
4548 | ||
4549 | static int | |
4550 | thread_display_step (ref, context) | |
4551 | threadref *ref; | |
4552 | void *context; | |
4553 | { | |
4554 | /* output_threadid(" threadstep ",ref); *//* simple test */ | |
4555 | return get_and_display_threadinfo (ref); | |
4556 | } | |
4557 | ||
4558 | static void | |
4559 | threadlist_update_test_cmd (cmd, tty) | |
4560 | char *cmd; | |
4561 | int tty; | |
4562 | { | |
4563 | printf_filtered ("Remote Threadlist update test\n"); | |
4564 | remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS); | |
4565 | } | |
4566 | ||
4567 | static void | |
4568 | init_remote_threadtests (void) | |
4569 | { | |
4570 | add_com ("tlist", class_obscure, threadlist_test_cmd, | |
4571 | "Fetch and print the remote list of thread identifiers, one pkt only"); | |
4572 | add_com ("tinfo", class_obscure, threadinfo_test_cmd, | |
4573 | "Fetch and display info about one thread"); | |
4574 | add_com ("tset", class_obscure, threadset_test_cmd, | |
4575 | "Test setting to a different thread"); | |
4576 | add_com ("tupd", class_obscure, threadlist_update_test_cmd, | |
4577 | "Iterate through updating all remote thread info"); | |
4578 | add_com ("talive", class_obscure, threadalive_test, | |
4579 | " Remote thread alive test "); | |
4580 | } | |
4581 | ||
4582 | #endif /* 0 */ | |
4583 | ||
4584 | static void | |
4585 | init_remote_ops () | |
4586 | { | |
c5aa993b | 4587 | remote_ops.to_shortname = "remote"; |
c906108c | 4588 | remote_ops.to_longname = "Remote serial target in gdb-specific protocol"; |
c5aa993b | 4589 | remote_ops.to_doc = |
c906108c | 4590 | "Use a remote computer via a serial line, using a gdb-specific protocol.\n\ |
c5aa993b JM |
4591 | Specify the serial device it is connected to (e.g. /dev/ttya)."; |
4592 | remote_ops.to_open = remote_open; | |
4593 | remote_ops.to_close = remote_close; | |
c906108c | 4594 | remote_ops.to_detach = remote_detach; |
c5aa993b | 4595 | remote_ops.to_resume = remote_resume; |
c906108c SS |
4596 | remote_ops.to_wait = remote_wait; |
4597 | remote_ops.to_fetch_registers = remote_fetch_registers; | |
4598 | remote_ops.to_store_registers = remote_store_registers; | |
4599 | remote_ops.to_prepare_to_store = remote_prepare_to_store; | |
c5aa993b JM |
4600 | remote_ops.to_xfer_memory = remote_xfer_memory; |
4601 | remote_ops.to_files_info = remote_files_info; | |
c906108c SS |
4602 | remote_ops.to_insert_breakpoint = remote_insert_breakpoint; |
4603 | remote_ops.to_remove_breakpoint = remote_remove_breakpoint; | |
c5aa993b JM |
4604 | remote_ops.to_kill = remote_kill; |
4605 | remote_ops.to_load = generic_load; | |
c906108c SS |
4606 | remote_ops.to_mourn_inferior = remote_mourn; |
4607 | remote_ops.to_thread_alive = remote_thread_alive; | |
0f71a2f6 | 4608 | remote_ops.to_find_new_threads = remote_threads_info; |
c906108c SS |
4609 | remote_ops.to_stop = remote_stop; |
4610 | remote_ops.to_query = remote_query; | |
96baa820 | 4611 | remote_ops.to_rcmd = remote_rcmd; |
c906108c | 4612 | remote_ops.to_stratum = process_stratum; |
c5aa993b JM |
4613 | remote_ops.to_has_all_memory = 1; |
4614 | remote_ops.to_has_memory = 1; | |
4615 | remote_ops.to_has_stack = 1; | |
4616 | remote_ops.to_has_registers = 1; | |
4617 | remote_ops.to_has_execution = 1; | |
4618 | remote_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */ | |
4619 | remote_ops.to_magic = OPS_MAGIC; | |
c906108c SS |
4620 | } |
4621 | ||
4622 | /* Set up the extended remote vector by making a copy of the standard | |
4623 | remote vector and adding to it. */ | |
4624 | ||
4625 | static void | |
4626 | init_extended_remote_ops () | |
4627 | { | |
4628 | extended_remote_ops = remote_ops; | |
4629 | ||
0f71a2f6 | 4630 | extended_remote_ops.to_shortname = "extended-remote"; |
c5aa993b | 4631 | extended_remote_ops.to_longname = |
c906108c | 4632 | "Extended remote serial target in gdb-specific protocol"; |
c5aa993b | 4633 | extended_remote_ops.to_doc = |
c906108c SS |
4634 | "Use a remote computer via a serial line, using a gdb-specific protocol.\n\ |
4635 | Specify the serial device it is connected to (e.g. /dev/ttya).", | |
c5aa993b | 4636 | extended_remote_ops.to_open = extended_remote_open; |
c906108c SS |
4637 | extended_remote_ops.to_create_inferior = extended_remote_create_inferior; |
4638 | extended_remote_ops.to_mourn_inferior = extended_remote_mourn; | |
0f71a2f6 JM |
4639 | } |
4640 | ||
4641 | /* | |
4642 | * Command: info remote-process | |
4643 | * | |
4644 | * This implements Cisco's version of the "info proc" command. | |
4645 | * | |
4646 | * This query allows the target stub to return an arbitrary string | |
4647 | * (or strings) giving arbitrary information about the target process. | |
4648 | * This is optional; the target stub isn't required to implement it. | |
4649 | * | |
4650 | * Syntax: qfProcessInfo request first string | |
4651 | * qsProcessInfo request subsequent string | |
4652 | * reply: 'O'<hex-encoded-string> | |
4653 | * 'l' last reply (empty) | |
4654 | */ | |
4655 | ||
4656 | static void | |
4657 | remote_info_process (args, from_tty) | |
4658 | char *args; | |
4659 | int from_tty; | |
4660 | { | |
085dd6e6 | 4661 | char *buf = alloca (PBUFSIZ); |
0f71a2f6 JM |
4662 | |
4663 | if (remote_desc == 0) | |
4664 | error ("Command can only be used when connected to the remote target."); | |
4665 | ||
4666 | putpkt ("qfProcessInfo"); | |
4667 | getpkt (buf, 0); | |
4668 | if (buf[0] == 0) | |
c5aa993b | 4669 | return; /* Silently: target does not support this feature. */ |
0f71a2f6 JM |
4670 | |
4671 | if (buf[0] == 'E') | |
4672 | error ("info proc: target error."); | |
4673 | ||
c5aa993b | 4674 | while (buf[0] == 'O') /* Capitol-O packet */ |
0f71a2f6 JM |
4675 | { |
4676 | remote_console_output (&buf[1]); | |
4677 | putpkt ("qsProcessInfo"); | |
4678 | getpkt (buf, 0); | |
4679 | } | |
4680 | } | |
4681 | ||
4682 | /* | |
4683 | * Target Cisco | |
4684 | */ | |
4685 | ||
4686 | static void | |
4687 | remote_cisco_open (name, from_tty) | |
4688 | char *name; | |
4689 | int from_tty; | |
4690 | { | |
4691 | if (name == 0) | |
4692 | error ( | |
c5aa993b | 4693 | "To open a remote debug connection, you need to specify what \n\ |
0f71a2f6 JM |
4694 | device is attached to the remote system (e.g. host:port)."); |
4695 | ||
4696 | target_preopen (from_tty); | |
4697 | ||
4698 | unpush_target (&remote_cisco_ops); | |
4699 | ||
4700 | remote_dcache = dcache_init (remote_read_bytes, remote_write_bytes); | |
4701 | ||
4702 | remote_desc = SERIAL_OPEN (name); | |
4703 | if (!remote_desc) | |
4704 | perror_with_name (name); | |
4705 | ||
4706 | /* | |
4707 | * If a baud rate was specified on the gdb command line it will | |
4708 | * be greater than the initial value of -1. If it is, use it otherwise | |
4709 | * default to 9600 | |
4710 | */ | |
4711 | ||
4712 | baud_rate = (baud_rate > 0) ? baud_rate : 9600; | |
c5aa993b | 4713 | if (SERIAL_SETBAUDRATE (remote_desc, baud_rate)) |
0f71a2f6 JM |
4714 | { |
4715 | SERIAL_CLOSE (remote_desc); | |
4716 | perror_with_name (name); | |
4717 | } | |
4718 | ||
4719 | SERIAL_RAW (remote_desc); | |
4720 | ||
4721 | /* If there is something sitting in the buffer we might take it as a | |
4722 | response to a command, which would be bad. */ | |
4723 | SERIAL_FLUSH_INPUT (remote_desc); | |
4724 | ||
4725 | if (from_tty) | |
4726 | { | |
4727 | puts_filtered ("Remote debugging using "); | |
4728 | puts_filtered (name); | |
4729 | puts_filtered ("\n"); | |
4730 | } | |
4731 | ||
4732 | remote_cisco_mode = 1; | |
4733 | ||
4734 | push_target (&remote_cisco_ops); /* Switch to using cisco target now */ | |
4735 | ||
5a2468f5 | 4736 | init_packet_config (&remote_protocol_P); |
96baa820 | 4737 | init_packet_config (&remote_protocol_Z); |
0f71a2f6 | 4738 | |
c5aa993b | 4739 | general_thread = -2; |
0f71a2f6 JM |
4740 | continue_thread = -2; |
4741 | ||
4742 | /* Force remote_write_bytes to check whether target supports | |
4743 | binary downloading. */ | |
96baa820 | 4744 | init_packet_config (&remote_protocol_binary_download); |
0f71a2f6 JM |
4745 | |
4746 | /* Without this, some commands which require an active target (such | |
4747 | as kill) won't work. This variable serves (at least) double duty | |
4748 | as both the pid of the target process (if it has such), and as a | |
4749 | flag indicating that a target is active. These functions should | |
4750 | be split out into seperate variables, especially since GDB will | |
4751 | someday have a notion of debugging several processes. */ | |
4752 | inferior_pid = MAGIC_NULL_PID; | |
4753 | ||
4754 | /* Start the remote connection; if error (0), discard this target. */ | |
4755 | ||
c5aa993b JM |
4756 | if (!catch_errors (remote_start_remote_dummy, (char *) 0, |
4757 | "Couldn't establish connection to remote target\n", | |
0f71a2f6 JM |
4758 | RETURN_MASK_ALL)) |
4759 | { | |
4760 | pop_target (); | |
4761 | return; | |
4762 | } | |
4763 | } | |
4764 | ||
4765 | static void | |
4766 | remote_cisco_close (quitting) | |
4767 | int quitting; | |
4768 | { | |
4769 | remote_cisco_mode = 0; | |
4770 | remote_close (quitting); | |
4771 | } | |
4772 | ||
c5aa993b JM |
4773 | static void |
4774 | remote_cisco_mourn | |
4775 | PARAMS ((void)) | |
0f71a2f6 JM |
4776 | { |
4777 | remote_mourn_1 (&remote_cisco_ops); | |
4778 | } | |
4779 | ||
c5aa993b JM |
4780 | enum |
4781 | { | |
4782 | READ_MORE, | |
4783 | FATAL_ERROR, | |
4784 | ENTER_DEBUG, | |
0f71a2f6 | 4785 | DISCONNECT_TELNET |
c5aa993b JM |
4786 | } |
4787 | minitelnet_return; | |
0f71a2f6 JM |
4788 | |
4789 | /* shared between readsocket() and readtty() */ | |
4790 | static char *tty_input; | |
4791 | ||
4792 | static int escape_count; | |
4793 | static int echo_check; | |
4794 | extern int quit_flag; | |
4795 | ||
4796 | static int | |
4797 | readsocket () | |
4798 | { | |
4799 | int data; | |
4800 | ||
4801 | /* Loop until the socket doesn't have any more data */ | |
4802 | ||
c5aa993b | 4803 | while ((data = readchar (0)) >= 0) |
0f71a2f6 JM |
4804 | { |
4805 | /* Check for the escape sequence */ | |
c5aa993b | 4806 | if (data == '|') |
0f71a2f6 JM |
4807 | { |
4808 | /* If this is the fourth escape, get out */ | |
c5aa993b | 4809 | if (++escape_count == 4) |
0f71a2f6 JM |
4810 | { |
4811 | return ENTER_DEBUG; | |
4812 | } | |
c5aa993b JM |
4813 | else |
4814 | { /* This is a '|', but not the fourth in a row. | |
4815 | Continue without echoing it. If it isn't actually | |
4816 | one of four in a row, it'll be echoed later. */ | |
0f71a2f6 JM |
4817 | continue; |
4818 | } | |
4819 | } | |
c5aa993b JM |
4820 | else |
4821 | /* Not a '|' */ | |
4822 | { | |
0f71a2f6 JM |
4823 | /* Ensure any pending '|'s are flushed. */ |
4824 | ||
c5aa993b JM |
4825 | for (; escape_count > 0; escape_count--) |
4826 | putchar ('|'); | |
0f71a2f6 | 4827 | } |
0f71a2f6 | 4828 | |
c5aa993b JM |
4829 | if (data == '\r') /* If this is a return character, */ |
4830 | continue; /* - just supress it. */ | |
4831 | ||
4832 | if (echo_check != -1) /* Check for echo of user input. */ | |
0f71a2f6 JM |
4833 | { |
4834 | if (tty_input[echo_check] == data) | |
4835 | { | |
c5aa993b JM |
4836 | echo_check++; /* Character matched user input: */ |
4837 | continue; /* Continue without echoing it. */ | |
0f71a2f6 | 4838 | } |
c5aa993b JM |
4839 | else if ((data == '\n') && (tty_input[echo_check] == '\r')) |
4840 | { /* End of the line (and of echo checking). */ | |
4841 | echo_check = -1; /* No more echo supression */ | |
4842 | continue; /* Continue without echoing. */ | |
0f71a2f6 JM |
4843 | } |
4844 | else | |
c5aa993b JM |
4845 | { /* Failed check for echo of user input. |
4846 | We now have some suppressed output to flush! */ | |
0f71a2f6 JM |
4847 | int j; |
4848 | ||
c5aa993b | 4849 | for (j = 0; j < echo_check; j++) |
0f71a2f6 JM |
4850 | putchar (tty_input[j]); |
4851 | echo_check = -1; | |
4852 | } | |
4853 | } | |
c5aa993b | 4854 | putchar (data); /* Default case: output the char. */ |
0f71a2f6 JM |
4855 | } |
4856 | ||
c5aa993b JM |
4857 | if (data == SERIAL_TIMEOUT) /* Timeout returned from readchar. */ |
4858 | return READ_MORE; /* Try to read some more */ | |
4859 | else | |
4860 | return FATAL_ERROR; /* Trouble, bail out */ | |
0f71a2f6 JM |
4861 | } |
4862 | ||
4863 | static int | |
4864 | readtty () | |
4865 | { | |
4866 | int status; | |
4867 | int tty_bytecount; | |
4868 | ||
4869 | /* First, read a buffer full from the terminal */ | |
4870 | tty_bytecount = read (fileno (stdin), tty_input, sizeof (tty_input) - 1); | |
c5aa993b | 4871 | if (tty_bytecount == -1) |
0f71a2f6 JM |
4872 | { |
4873 | perror ("readtty: read failed"); | |
4874 | return FATAL_ERROR; | |
4875 | } | |
4876 | ||
4877 | /* Remove a quoted newline. */ | |
4878 | if (tty_input[tty_bytecount - 1] == '\n' && | |
4879 | tty_input[tty_bytecount - 2] == '\\') /* line ending in backslash */ | |
4880 | { | |
c5aa993b JM |
4881 | tty_input[--tty_bytecount] = 0; /* remove newline */ |
4882 | tty_input[--tty_bytecount] = 0; /* remove backslash */ | |
0f71a2f6 JM |
4883 | } |
4884 | ||
4885 | /* Turn trailing newlines into returns */ | |
4886 | if (tty_input[tty_bytecount - 1] == '\n') | |
c5aa993b | 4887 | tty_input[tty_bytecount - 1] = '\r'; |
0f71a2f6 JM |
4888 | |
4889 | /* If the line consists of a ~, enter debugging mode. */ | |
4890 | if ((tty_input[0] == '~') && (tty_bytecount == 2)) | |
4891 | return ENTER_DEBUG; | |
4892 | ||
4893 | /* Make this a zero terminated string and write it out */ | |
4894 | tty_input[tty_bytecount] = 0; | |
c5aa993b | 4895 | if (SERIAL_WRITE (remote_desc, tty_input, tty_bytecount)) |
0f71a2f6 JM |
4896 | { |
4897 | perror_with_name ("readtty: write failed"); | |
4898 | return FATAL_ERROR; | |
4899 | } | |
4900 | ||
4901 | return READ_MORE; | |
4902 | } | |
4903 | ||
4904 | static int | |
4905 | minitelnet () | |
4906 | { | |
4907 | fd_set input; /* file descriptors for select */ | |
c5aa993b JM |
4908 | int tablesize; /* max number of FDs for select */ |
4909 | int status; | |
4910 | int quit_count = 0; | |
0f71a2f6 JM |
4911 | |
4912 | extern int escape_count; /* global shared by readsocket */ | |
4913 | extern int echo_check; /* ditto */ | |
4914 | ||
4915 | escape_count = 0; | |
c5aa993b | 4916 | echo_check = -1; |
0f71a2f6 JM |
4917 | |
4918 | tablesize = 8 * sizeof (input); | |
4919 | ||
c5aa993b | 4920 | for (;;) |
0f71a2f6 JM |
4921 | { |
4922 | /* Check for anything from our socket - doesn't block. Note that | |
c5aa993b JM |
4923 | this must be done *before* the select as there may be |
4924 | buffered I/O waiting to be processed. */ | |
0f71a2f6 | 4925 | |
c5aa993b | 4926 | if ((status = readsocket ()) == FATAL_ERROR) |
0f71a2f6 JM |
4927 | { |
4928 | error ("Debugging terminated by communications error"); | |
4929 | } | |
c5aa993b | 4930 | else if (status != READ_MORE) |
0f71a2f6 JM |
4931 | { |
4932 | return (status); | |
4933 | } | |
4934 | ||
c5aa993b | 4935 | fflush (stdout); /* Flush output before blocking */ |
0f71a2f6 JM |
4936 | |
4937 | /* Now block on more socket input or TTY input */ | |
c5aa993b | 4938 | |
0f71a2f6 | 4939 | FD_ZERO (&input); |
c5aa993b | 4940 | FD_SET (fileno (stdin), &input); |
0f71a2f6 JM |
4941 | FD_SET (remote_desc->fd, &input); |
4942 | ||
4943 | status = select (tablesize, &input, 0, 0, 0); | |
c5aa993b | 4944 | if ((status == -1) && (errno != EINTR)) |
0f71a2f6 JM |
4945 | { |
4946 | error ("Communications error on select %d", errno); | |
4947 | } | |
4948 | ||
4949 | /* Handle Control-C typed */ | |
4950 | ||
c5aa993b | 4951 | if (quit_flag) |
0f71a2f6 JM |
4952 | { |
4953 | if ((++quit_count) == 2) | |
4954 | { | |
4955 | if (query ("Interrupt GDB? ")) | |
4956 | { | |
4957 | printf_filtered ("Interrupted by user.\n"); | |
4958 | return_to_top_level (RETURN_QUIT); | |
4959 | } | |
4960 | quit_count = 0; | |
4961 | } | |
4962 | quit_flag = 0; | |
4963 | ||
4964 | if (remote_break) | |
4965 | SERIAL_SEND_BREAK (remote_desc); | |
4966 | else | |
4967 | SERIAL_WRITE (remote_desc, "\003", 1); | |
4968 | ||
4969 | continue; | |
4970 | } | |
4971 | ||
4972 | /* Handle console input */ | |
4973 | ||
c5aa993b | 4974 | if (FD_ISSET (fileno (stdin), &input)) |
0f71a2f6 JM |
4975 | { |
4976 | quit_count = 0; | |
4977 | echo_check = 0; | |
4978 | status = readtty (); | |
4979 | if (status == READ_MORE) | |
4980 | continue; | |
4981 | ||
4982 | return status; /* telnet session ended */ | |
4983 | } | |
4984 | } | |
4985 | } | |
4986 | ||
4987 | static int | |
4988 | remote_cisco_wait (pid, status) | |
4989 | int pid; | |
4990 | struct target_waitstatus *status; | |
4991 | { | |
c5aa993b | 4992 | if (minitelnet () != ENTER_DEBUG) |
0f71a2f6 JM |
4993 | { |
4994 | error ("Debugging session terminated by protocol error"); | |
4995 | } | |
4996 | putpkt ("?"); | |
4997 | return remote_wait (pid, status); | |
4998 | } | |
4999 | ||
5000 | static void | |
5001 | init_remote_cisco_ops () | |
5002 | { | |
5003 | remote_cisco_ops.to_shortname = "cisco"; | |
c5aa993b JM |
5004 | remote_cisco_ops.to_longname = "Remote serial target in cisco-specific protocol"; |
5005 | remote_cisco_ops.to_doc = | |
0f71a2f6 JM |
5006 | "Use a remote machine via TCP, using a cisco-specific protocol.\n\ |
5007 | Specify the serial device it is connected to (e.g. host:2020)."; | |
c5aa993b JM |
5008 | remote_cisco_ops.to_open = remote_cisco_open; |
5009 | remote_cisco_ops.to_close = remote_cisco_close; | |
5010 | remote_cisco_ops.to_detach = remote_detach; | |
5011 | remote_cisco_ops.to_resume = remote_resume; | |
5012 | remote_cisco_ops.to_wait = remote_cisco_wait; | |
5013 | remote_cisco_ops.to_fetch_registers = remote_fetch_registers; | |
5014 | remote_cisco_ops.to_store_registers = remote_store_registers; | |
5015 | remote_cisco_ops.to_prepare_to_store = remote_prepare_to_store; | |
5016 | remote_cisco_ops.to_xfer_memory = remote_xfer_memory; | |
5017 | remote_cisco_ops.to_files_info = remote_files_info; | |
0f71a2f6 JM |
5018 | remote_cisco_ops.to_insert_breakpoint = remote_insert_breakpoint; |
5019 | remote_cisco_ops.to_remove_breakpoint = remote_remove_breakpoint; | |
c5aa993b JM |
5020 | remote_cisco_ops.to_kill = remote_kill; |
5021 | remote_cisco_ops.to_load = generic_load; | |
5022 | remote_cisco_ops.to_mourn_inferior = remote_cisco_mourn; | |
5023 | remote_cisco_ops.to_thread_alive = remote_thread_alive; | |
5024 | remote_cisco_ops.to_find_new_threads = remote_threads_info; | |
5025 | remote_cisco_ops.to_stratum = process_stratum; | |
5026 | remote_cisco_ops.to_has_all_memory = 1; | |
5027 | remote_cisco_ops.to_has_memory = 1; | |
5028 | remote_cisco_ops.to_has_stack = 1; | |
5029 | remote_cisco_ops.to_has_registers = 1; | |
5030 | remote_cisco_ops.to_has_execution = 1; | |
5031 | remote_cisco_ops.to_magic = OPS_MAGIC; | |
0f71a2f6 JM |
5032 | } |
5033 | ||
43ff13b4 JM |
5034 | /* Target async and target extended-async. |
5035 | ||
5036 | This are temporary targets, until it is all tested. Eventually | |
5037 | async support will be incorporated int the usual 'remote' | |
5038 | target. */ | |
5039 | ||
5040 | static void | |
5041 | init_remote_async_ops () | |
5042 | { | |
5043 | remote_async_ops.to_shortname = "async"; | |
c5aa993b JM |
5044 | remote_async_ops.to_longname = "Remote serial target in async version of the gdb-specific protocol"; |
5045 | remote_async_ops.to_doc = | |
43ff13b4 JM |
5046 | "Use a remote computer via a serial line, using a gdb-specific protocol.\n\ |
5047 | Specify the serial device it is connected to (e.g. /dev/ttya)."; | |
c5aa993b JM |
5048 | remote_async_ops.to_open = remote_async_open; |
5049 | remote_async_ops.to_close = remote_close; | |
5050 | remote_async_ops.to_detach = remote_async_detach; | |
5051 | remote_async_ops.to_resume = remote_async_resume; | |
5052 | remote_async_ops.to_wait = remote_async_wait; | |
5053 | remote_async_ops.to_fetch_registers = remote_fetch_registers; | |
5054 | remote_async_ops.to_store_registers = remote_store_registers; | |
5055 | remote_async_ops.to_prepare_to_store = remote_prepare_to_store; | |
5056 | remote_async_ops.to_xfer_memory = remote_xfer_memory; | |
5057 | remote_async_ops.to_files_info = remote_files_info; | |
43ff13b4 JM |
5058 | remote_async_ops.to_insert_breakpoint = remote_insert_breakpoint; |
5059 | remote_async_ops.to_remove_breakpoint = remote_remove_breakpoint; | |
c5aa993b JM |
5060 | remote_async_ops.to_kill = remote_async_kill; |
5061 | remote_async_ops.to_load = generic_load; | |
5062 | remote_async_ops.to_mourn_inferior = remote_mourn; | |
5063 | remote_async_ops.to_thread_alive = remote_thread_alive; | |
5064 | remote_async_ops.to_find_new_threads = remote_threads_info; | |
43ff13b4 JM |
5065 | remote_async_ops.to_stop = remote_stop; |
5066 | remote_async_ops.to_query = remote_query; | |
96baa820 | 5067 | remote_async_ops.to_rcmd = remote_rcmd; |
c5aa993b JM |
5068 | remote_async_ops.to_stratum = process_stratum; |
5069 | remote_async_ops.to_has_all_memory = 1; | |
5070 | remote_async_ops.to_has_memory = 1; | |
5071 | remote_async_ops.to_has_stack = 1; | |
5072 | remote_async_ops.to_has_registers = 1; | |
5073 | remote_async_ops.to_has_execution = 1; | |
5074 | remote_async_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */ | |
5075 | remote_async_ops.to_has_async_exec = 1; | |
5076 | remote_async_ops.to_magic = OPS_MAGIC; | |
43ff13b4 JM |
5077 | } |
5078 | ||
5079 | /* Set up the async extended remote vector by making a copy of the standard | |
5080 | remote vector and adding to it. */ | |
5081 | ||
5082 | static void | |
5083 | init_extended_async_remote_ops () | |
5084 | { | |
5085 | extended_async_remote_ops = remote_async_ops; | |
5086 | ||
5087 | extended_async_remote_ops.to_shortname = "extended-async"; | |
c5aa993b | 5088 | extended_async_remote_ops.to_longname = |
43ff13b4 | 5089 | "Extended remote serial target in async gdb-specific protocol"; |
c5aa993b | 5090 | extended_async_remote_ops.to_doc = |
43ff13b4 JM |
5091 | "Use a remote computer via a serial line, using an async gdb-specific protocol.\n\ |
5092 | Specify the serial device it is connected to (e.g. /dev/ttya).", | |
c5aa993b | 5093 | extended_async_remote_ops.to_open = extended_remote_async_open; |
43ff13b4 JM |
5094 | extended_async_remote_ops.to_create_inferior = extended_remote_async_create_inferior; |
5095 | extended_async_remote_ops.to_mourn_inferior = extended_remote_mourn; | |
5096 | } | |
5097 | ||
5a2468f5 JM |
5098 | static void |
5099 | set_remote_cmd (args, from_tty) | |
5100 | char *args; | |
5101 | int from_tty; | |
5102 | { | |
5103 | ||
5104 | } | |
5105 | ||
5106 | ||
0f71a2f6 JM |
5107 | static void |
5108 | build_remote_gdbarch_data () | |
5109 | { | |
5110 | tty_input = xmalloc (PBUFSIZ); | |
5111 | } | |
5112 | ||
c906108c SS |
5113 | void |
5114 | _initialize_remote () | |
5115 | { | |
5a2468f5 JM |
5116 | static struct cmd_list_element *remote_set_cmdlist; |
5117 | static struct cmd_list_element *remote_show_cmdlist; | |
5118 | ||
0f71a2f6 JM |
5119 | /* architecture specific data */ |
5120 | build_remote_gdbarch_data (); | |
5121 | register_gdbarch_swap (&tty_input, sizeof (&tty_input), NULL); | |
5122 | register_gdbarch_swap (NULL, 0, build_remote_gdbarch_data); | |
5123 | ||
5124 | /* runtime constants - we retain the value of remote_write_size | |
5125 | across architecture swaps. */ | |
cce74817 JM |
5126 | remote_write_size = PBUFSIZ; |
5127 | ||
c906108c SS |
5128 | init_remote_ops (); |
5129 | add_target (&remote_ops); | |
5130 | ||
5131 | init_extended_remote_ops (); | |
5132 | add_target (&extended_remote_ops); | |
cce74817 | 5133 | |
43ff13b4 JM |
5134 | init_remote_async_ops (); |
5135 | add_target (&remote_async_ops); | |
5136 | ||
5137 | init_extended_async_remote_ops (); | |
5138 | add_target (&extended_async_remote_ops); | |
5139 | ||
0f71a2f6 JM |
5140 | init_remote_cisco_ops (); |
5141 | add_target (&remote_cisco_ops); | |
5142 | ||
c906108c SS |
5143 | #if 0 |
5144 | init_remote_threadtests (); | |
5145 | #endif | |
5146 | ||
5a2468f5 JM |
5147 | add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, "\ |
5148 | Remote protocol specific variables\n\ | |
5149 | Configure various remote-protocol specific variables such as\n\ | |
5150 | the packets being used", | |
5151 | &remote_set_cmdlist, "remote ", | |
5152 | 0/*allow-unknown*/, &setlist); | |
5153 | add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, "\ | |
5154 | Remote protocol specific variables\n\ | |
5155 | Configure various remote-protocol specific variables such as\n\ | |
5156 | the packets being used", | |
5157 | &remote_show_cmdlist, "remote ", | |
5158 | 0/*allow-unknown*/, &showlist); | |
5159 | ||
c5aa993b | 5160 | add_cmd ("compare-sections", class_obscure, compare_sections_command, |
c906108c | 5161 | "Compare section data on target to the exec file.\n\ |
c5aa993b | 5162 | Argument is a single section name (default: all loaded sections).", |
c906108c SS |
5163 | &cmdlist); |
5164 | ||
5165 | add_cmd ("packet", class_maintenance, packet_command, | |
5166 | "Send an arbitrary packet to a remote target.\n\ | |
5167 | maintenance packet TEXT\n\ | |
5168 | If GDB is talking to an inferior via the GDB serial protocol, then\n\ | |
5169 | this command sends the string TEXT to the inferior, and displays the\n\ | |
5170 | response packet. GDB supplies the initial `$' character, and the\n\ | |
5171 | terminating `#' character and checksum.", | |
5172 | &maintenancelist); | |
5173 | ||
c5aa993b | 5174 | add_show_from_set |
c906108c | 5175 | (add_set_cmd ("remotetimeout", no_class, |
c5aa993b | 5176 | var_integer, (char *) &remote_timeout, |
c906108c SS |
5177 | "Set timeout value for remote read.\n", |
5178 | &setlist), | |
5179 | &showlist); | |
5180 | ||
c5aa993b | 5181 | add_show_from_set |
c906108c | 5182 | (add_set_cmd ("remotebreak", no_class, |
adf40b2e | 5183 | var_boolean, (char *) &remote_break, |
c906108c SS |
5184 | "Set whether to send break if interrupted.\n", |
5185 | &setlist), | |
5186 | &showlist); | |
5187 | ||
c5aa993b | 5188 | add_show_from_set |
c906108c | 5189 | (add_set_cmd ("remotewritesize", no_class, |
c5aa993b JM |
5190 | var_integer, (char *) &remote_write_size, |
5191 | "Set the maximum number of bytes per memory write packet.\n", | |
c906108c SS |
5192 | &setlist), |
5193 | &showlist); | |
5194 | ||
5195 | remote_address_size = TARGET_PTR_BIT; | |
c5aa993b | 5196 | add_show_from_set |
c906108c | 5197 | (add_set_cmd ("remoteaddresssize", class_obscure, |
c5aa993b | 5198 | var_integer, (char *) &remote_address_size, |
c906108c SS |
5199 | "Set the maximum size of the address (in bits) \ |
5200 | in a memory packet.\n", | |
5201 | &setlist), | |
c5aa993b | 5202 | &showlist); |
c906108c | 5203 | |
96baa820 JM |
5204 | add_packet_config_cmd (&remote_protocol_binary_download, |
5205 | "X", "binary-download", | |
5206 | set_remote_protocol_binary_download_cmd, | |
5207 | show_remote_protocol_binary_download_cmd, | |
5208 | &remote_set_cmdlist, &remote_show_cmdlist); | |
5209 | #if 0 | |
5210 | /* XXXX - should ``set remotebinarydownload'' be retained for | |
5211 | compatibility. */ | |
c5aa993b | 5212 | add_show_from_set |
b83266a0 SS |
5213 | (add_set_cmd ("remotebinarydownload", no_class, |
5214 | var_boolean, (char *) &remote_binary_download, | |
5215 | "Set binary downloads.\n", &setlist), | |
5216 | &showlist); | |
96baa820 | 5217 | #endif |
0f71a2f6 JM |
5218 | |
5219 | add_info ("remote-process", remote_info_process, | |
5220 | "Query the remote system for process info."); | |
5221 | ||
5a2468f5 JM |
5222 | add_packet_config_cmd (&remote_protocol_P, "P", "set-register", |
5223 | set_remote_protocol_P_packet_cmd, | |
5224 | show_remote_protocol_P_packet_cmd, | |
5225 | &remote_set_cmdlist, &remote_show_cmdlist); | |
96baa820 JM |
5226 | |
5227 | add_packet_config_cmd (&remote_protocol_Z, "Z", "breakpoint", | |
5228 | set_remote_protocol_Z_packet_cmd, | |
5229 | show_remote_protocol_Z_packet_cmd, | |
5230 | &remote_set_cmdlist, &remote_show_cmdlist); | |
c906108c | 5231 | } |