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