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