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