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