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