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