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