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