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