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