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