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