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