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