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