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