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