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