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