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