20619857532db5875b795ad87f7bf931d4b07d2c
[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;
3448 char *p;
3449 int i;
3450
3451 sprintf (buf, "P%x=", regno);
3452 p = buf + strlen (buf);
3453 regp = register_buffer (regno);
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 = alloca (rs->remote_packet_size);
3469 int i;
3470 char *p;
3471 char *regs;
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 buf[0] = 'G';
3505
3506 /* Command describes registers byte by byte,
3507 each byte encoded as two hex characters. */
3508
3509 regs = register_buffer (-1);
3510 p = buf + 1;
3511 /* remote_prepare_to_store insures that register_bytes_found gets set. */
3512 bin2hex (regs, p, register_bytes_found);
3513 remote_send (buf, (rs->remote_packet_size));
3514 }
3515 \f
3516
3517 /* Return the number of hex digits in num. */
3518
3519 static int
3520 hexnumlen (ULONGEST num)
3521 {
3522 int i;
3523
3524 for (i = 0; num != 0; i++)
3525 num >>= 4;
3526
3527 return max (i, 1);
3528 }
3529
3530 /* Set BUF to the minimum number of hex digits representing NUM. */
3531
3532 static int
3533 hexnumstr (char *buf, ULONGEST num)
3534 {
3535 int len = hexnumlen (num);
3536 return hexnumnstr (buf, num, len);
3537 }
3538
3539
3540 /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
3541
3542 static int
3543 hexnumnstr (char *buf, ULONGEST num, int width)
3544 {
3545 int i;
3546
3547 buf[width] = '\0';
3548
3549 for (i = width - 1; i >= 0; i--)
3550 {
3551 buf[i] = "0123456789abcdef"[(num & 0xf)];
3552 num >>= 4;
3553 }
3554
3555 return width;
3556 }
3557
3558 /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
3559
3560 static CORE_ADDR
3561 remote_address_masked (CORE_ADDR addr)
3562 {
3563 if (remote_address_size > 0
3564 && remote_address_size < (sizeof (ULONGEST) * 8))
3565 {
3566 /* Only create a mask when that mask can safely be constructed
3567 in a ULONGEST variable. */
3568 ULONGEST mask = 1;
3569 mask = (mask << remote_address_size) - 1;
3570 addr &= mask;
3571 }
3572 return addr;
3573 }
3574
3575 /* Determine whether the remote target supports binary downloading.
3576 This is accomplished by sending a no-op memory write of zero length
3577 to the target at the specified address. It does not suffice to send
3578 the whole packet, since many stubs strip the eighth bit and subsequently
3579 compute a wrong checksum, which causes real havoc with remote_write_bytes.
3580
3581 NOTE: This can still lose if the serial line is not eight-bit
3582 clean. In cases like this, the user should clear "remote
3583 X-packet". */
3584
3585 static void
3586 check_binary_download (CORE_ADDR addr)
3587 {
3588 struct remote_state *rs = get_remote_state ();
3589 switch (remote_protocol_binary_download.support)
3590 {
3591 case PACKET_DISABLE:
3592 break;
3593 case PACKET_ENABLE:
3594 break;
3595 case PACKET_SUPPORT_UNKNOWN:
3596 {
3597 char *buf = alloca (rs->remote_packet_size);
3598 char *p;
3599
3600 p = buf;
3601 *p++ = 'X';
3602 p += hexnumstr (p, (ULONGEST) addr);
3603 *p++ = ',';
3604 p += hexnumstr (p, (ULONGEST) 0);
3605 *p++ = ':';
3606 *p = '\0';
3607
3608 putpkt_binary (buf, (int) (p - buf));
3609 getpkt (buf, (rs->remote_packet_size), 0);
3610
3611 if (buf[0] == '\0')
3612 {
3613 if (remote_debug)
3614 fprintf_unfiltered (gdb_stdlog,
3615 "binary downloading NOT suppported by target\n");
3616 remote_protocol_binary_download.support = PACKET_DISABLE;
3617 }
3618 else
3619 {
3620 if (remote_debug)
3621 fprintf_unfiltered (gdb_stdlog,
3622 "binary downloading suppported by target\n");
3623 remote_protocol_binary_download.support = PACKET_ENABLE;
3624 }
3625 break;
3626 }
3627 }
3628 }
3629
3630 /* Write memory data directly to the remote machine.
3631 This does not inform the data cache; the data cache uses this.
3632 MEMADDR is the address in the remote memory space.
3633 MYADDR is the address of the buffer in our space.
3634 LEN is the number of bytes.
3635
3636 Returns number of bytes transferred, or 0 (setting errno) for
3637 error. Only transfer a single packet. */
3638
3639 static int
3640 remote_write_bytes (CORE_ADDR memaddr, char *myaddr, int len)
3641 {
3642 unsigned char *buf;
3643 int max_buf_size; /* Max size of packet output buffer */
3644 unsigned char *p;
3645 unsigned char *plen;
3646 long sizeof_buf;
3647 int plenlen;
3648 int todo;
3649 int nr_bytes;
3650
3651 /* Verify that the target can support a binary download */
3652 check_binary_download (memaddr);
3653
3654 /* Determine the max packet size. */
3655 max_buf_size = get_memory_write_packet_size ();
3656 sizeof_buf = max_buf_size + 1; /* Space for trailing NUL */
3657 buf = alloca (sizeof_buf);
3658
3659 /* Subtract header overhead from max payload size - $M<memaddr>,<len>:#nn */
3660 max_buf_size -= 2 + hexnumlen (memaddr + len - 1) + 1 + hexnumlen (len) + 4;
3661
3662 /* construct "M"<memaddr>","<len>":" */
3663 /* sprintf (buf, "M%lx,%x:", (unsigned long) memaddr, todo); */
3664 p = buf;
3665
3666 /* Append [XM]. Compute a best guess of the number of bytes
3667 actually transfered. */
3668 switch (remote_protocol_binary_download.support)
3669 {
3670 case PACKET_ENABLE:
3671 *p++ = 'X';
3672 /* Best guess at number of bytes that will fit. */
3673 todo = min (len, max_buf_size);
3674 break;
3675 case PACKET_DISABLE:
3676 *p++ = 'M';
3677 /* num bytes that will fit */
3678 todo = min (len, max_buf_size / 2);
3679 break;
3680 case PACKET_SUPPORT_UNKNOWN:
3681 internal_error (__FILE__, __LINE__,
3682 "remote_write_bytes: bad internal state");
3683 default:
3684 internal_error (__FILE__, __LINE__, "bad switch");
3685 }
3686
3687 /* Append <memaddr> */
3688 memaddr = remote_address_masked (memaddr);
3689 p += hexnumstr (p, (ULONGEST) memaddr);
3690 *p++ = ',';
3691
3692 /* Append <len>. Retain the location/size of <len>. It may
3693 need to be adjusted once the packet body has been created. */
3694 plen = p;
3695 plenlen = hexnumstr (p, (ULONGEST) todo);
3696 p += plenlen;
3697 *p++ = ':';
3698 *p = '\0';
3699
3700 /* Append the packet body. */
3701 switch (remote_protocol_binary_download.support)
3702 {
3703 case PACKET_ENABLE:
3704 /* Binary mode. Send target system values byte by byte, in
3705 increasing byte addresses. Only escape certain critical
3706 characters. */
3707 for (nr_bytes = 0;
3708 (nr_bytes < todo) && (p - buf) < (max_buf_size - 2);
3709 nr_bytes++)
3710 {
3711 switch (myaddr[nr_bytes] & 0xff)
3712 {
3713 case '$':
3714 case '#':
3715 case 0x7d:
3716 /* These must be escaped */
3717 *p++ = 0x7d;
3718 *p++ = (myaddr[nr_bytes] & 0xff) ^ 0x20;
3719 break;
3720 default:
3721 *p++ = myaddr[nr_bytes] & 0xff;
3722 break;
3723 }
3724 }
3725 if (nr_bytes < todo)
3726 {
3727 /* Escape chars have filled up the buffer prematurely,
3728 and we have actually sent fewer bytes than planned.
3729 Fix-up the length field of the packet. Use the same
3730 number of characters as before. */
3731
3732 plen += hexnumnstr (plen, (ULONGEST) nr_bytes, plenlen);
3733 *plen = ':'; /* overwrite \0 from hexnumnstr() */
3734 }
3735 break;
3736 case PACKET_DISABLE:
3737 /* Normal mode: Send target system values byte by byte, in
3738 increasing byte addresses. Each byte is encoded as a two hex
3739 value. */
3740 nr_bytes = bin2hex (myaddr, p, todo);
3741 p += 2 * nr_bytes;
3742 break;
3743 case PACKET_SUPPORT_UNKNOWN:
3744 internal_error (__FILE__, __LINE__,
3745 "remote_write_bytes: bad internal state");
3746 default:
3747 internal_error (__FILE__, __LINE__, "bad switch");
3748 }
3749
3750 putpkt_binary (buf, (int) (p - buf));
3751 getpkt (buf, sizeof_buf, 0);
3752
3753 if (buf[0] == 'E')
3754 {
3755 /* There is no correspondance between what the remote protocol
3756 uses for errors and errno codes. We would like a cleaner way
3757 of representing errors (big enough to include errno codes,
3758 bfd_error codes, and others). But for now just return EIO. */
3759 errno = EIO;
3760 return 0;
3761 }
3762
3763 /* Return NR_BYTES, not TODO, in case escape chars caused us to send fewer
3764 bytes than we'd planned. */
3765 return nr_bytes;
3766 }
3767
3768 /* Read memory data directly from the remote machine.
3769 This does not use the data cache; the data cache uses this.
3770 MEMADDR is the address in the remote memory space.
3771 MYADDR is the address of the buffer in our space.
3772 LEN is the number of bytes.
3773
3774 Returns number of bytes transferred, or 0 for error. */
3775
3776 /* NOTE: cagney/1999-10-18: This function (and its siblings in other
3777 remote targets) shouldn't attempt to read the entire buffer.
3778 Instead it should read a single packet worth of data and then
3779 return the byte size of that packet to the caller. The caller (its
3780 caller and its callers caller ;-) already contains code for
3781 handling partial reads. */
3782
3783 static int
3784 remote_read_bytes (CORE_ADDR memaddr, char *myaddr, int len)
3785 {
3786 char *buf;
3787 int max_buf_size; /* Max size of packet output buffer */
3788 long sizeof_buf;
3789 int origlen;
3790
3791 /* Create a buffer big enough for this packet. */
3792 max_buf_size = get_memory_read_packet_size ();
3793 sizeof_buf = max_buf_size + 1; /* Space for trailing NUL */
3794 buf = alloca (sizeof_buf);
3795
3796 origlen = len;
3797 while (len > 0)
3798 {
3799 char *p;
3800 int todo;
3801 int i;
3802
3803 todo = min (len, max_buf_size / 2); /* num bytes that will fit */
3804
3805 /* construct "m"<memaddr>","<len>" */
3806 /* sprintf (buf, "m%lx,%x", (unsigned long) memaddr, todo); */
3807 memaddr = remote_address_masked (memaddr);
3808 p = buf;
3809 *p++ = 'm';
3810 p += hexnumstr (p, (ULONGEST) memaddr);
3811 *p++ = ',';
3812 p += hexnumstr (p, (ULONGEST) todo);
3813 *p = '\0';
3814
3815 putpkt (buf);
3816 getpkt (buf, sizeof_buf, 0);
3817
3818 if (buf[0] == 'E')
3819 {
3820 /* There is no correspondance between what the remote protocol uses
3821 for errors and errno codes. We would like a cleaner way of
3822 representing errors (big enough to include errno codes, bfd_error
3823 codes, and others). But for now just return EIO. */
3824 errno = EIO;
3825 return 0;
3826 }
3827
3828 /* Reply describes memory byte by byte,
3829 each byte encoded as two hex characters. */
3830
3831 p = buf;
3832 if ((i = hex2bin (p, myaddr, todo)) < todo)
3833 {
3834 /* Reply is short. This means that we were able to read
3835 only part of what we wanted to. */
3836 return i + (origlen - len);
3837 }
3838 myaddr += todo;
3839 memaddr += todo;
3840 len -= todo;
3841 }
3842 return origlen;
3843 }
3844 \f
3845 /* Read or write LEN bytes from inferior memory at MEMADDR,
3846 transferring to or from debugger address BUFFER. Write to inferior if
3847 SHOULD_WRITE is nonzero. Returns length of data written or read; 0
3848 for error. TARGET is unused. */
3849
3850 /* ARGSUSED */
3851 static int
3852 remote_xfer_memory (CORE_ADDR mem_addr, char *buffer, int mem_len,
3853 int should_write,
3854 struct mem_attrib *attrib ATTRIBUTE_UNUSED,
3855 struct target_ops *target)
3856 {
3857 CORE_ADDR targ_addr;
3858 int targ_len;
3859 int res;
3860
3861 REMOTE_TRANSLATE_XFER_ADDRESS (mem_addr, mem_len, &targ_addr, &targ_len);
3862 if (targ_len <= 0)
3863 return 0;
3864
3865 if (should_write)
3866 res = remote_write_bytes (targ_addr, buffer, targ_len);
3867 else
3868 res = remote_read_bytes (targ_addr, buffer, targ_len);
3869
3870 return res;
3871 }
3872
3873
3874 #if 0
3875 /* Enable after 4.12. */
3876
3877 void
3878 remote_search (int len, char *data, char *mask, CORE_ADDR startaddr,
3879 int increment, CORE_ADDR lorange, CORE_ADDR hirange,
3880 CORE_ADDR *addr_found, char *data_found)
3881 {
3882 if (increment == -4 && len == 4)
3883 {
3884 long mask_long, data_long;
3885 long data_found_long;
3886 CORE_ADDR addr_we_found;
3887 char *buf = alloca (rs->remote_packet_size);
3888 long returned_long[2];
3889 char *p;
3890
3891 mask_long = extract_unsigned_integer (mask, len);
3892 data_long = extract_unsigned_integer (data, len);
3893 sprintf (buf, "t%x:%x,%x", startaddr, data_long, mask_long);
3894 putpkt (buf);
3895 getpkt (buf, (rs->remote_packet_size), 0);
3896 if (buf[0] == '\0')
3897 {
3898 /* The stub doesn't support the 't' request. We might want to
3899 remember this fact, but on the other hand the stub could be
3900 switched on us. Maybe we should remember it only until
3901 the next "target remote". */
3902 generic_search (len, data, mask, startaddr, increment, lorange,
3903 hirange, addr_found, data_found);
3904 return;
3905 }
3906
3907 if (buf[0] == 'E')
3908 /* There is no correspondance between what the remote protocol uses
3909 for errors and errno codes. We would like a cleaner way of
3910 representing errors (big enough to include errno codes, bfd_error
3911 codes, and others). But for now just use EIO. */
3912 memory_error (EIO, startaddr);
3913 p = buf;
3914 addr_we_found = 0;
3915 while (*p != '\0' && *p != ',')
3916 addr_we_found = (addr_we_found << 4) + fromhex (*p++);
3917 if (*p == '\0')
3918 error ("Protocol error: short return for search");
3919
3920 data_found_long = 0;
3921 while (*p != '\0' && *p != ',')
3922 data_found_long = (data_found_long << 4) + fromhex (*p++);
3923 /* Ignore anything after this comma, for future extensions. */
3924
3925 if (addr_we_found < lorange || addr_we_found >= hirange)
3926 {
3927 *addr_found = 0;
3928 return;
3929 }
3930
3931 *addr_found = addr_we_found;
3932 *data_found = store_unsigned_integer (data_we_found, len);
3933 return;
3934 }
3935 generic_search (len, data, mask, startaddr, increment, lorange,
3936 hirange, addr_found, data_found);
3937 }
3938 #endif /* 0 */
3939 \f
3940 static void
3941 remote_files_info (struct target_ops *ignore)
3942 {
3943 puts_filtered ("Debugging a target over a serial line.\n");
3944 }
3945 \f
3946 /* Stuff for dealing with the packets which are part of this protocol.
3947 See comment at top of file for details. */
3948
3949 /* Read a single character from the remote end, masking it down to 7 bits. */
3950
3951 static int
3952 readchar (int timeout)
3953 {
3954 int ch;
3955
3956 ch = serial_readchar (remote_desc, timeout);
3957
3958 if (ch >= 0)
3959 return (ch & 0x7f);
3960
3961 switch ((enum serial_rc) ch)
3962 {
3963 case SERIAL_EOF:
3964 target_mourn_inferior ();
3965 error ("Remote connection closed");
3966 /* no return */
3967 case SERIAL_ERROR:
3968 perror_with_name ("Remote communication error");
3969 /* no return */
3970 case SERIAL_TIMEOUT:
3971 break;
3972 }
3973 return ch;
3974 }
3975
3976 /* Send the command in BUF to the remote machine, and read the reply
3977 into BUF. Report an error if we get an error reply. */
3978
3979 static void
3980 remote_send (char *buf,
3981 long sizeof_buf)
3982 {
3983 putpkt (buf);
3984 getpkt (buf, sizeof_buf, 0);
3985
3986 if (buf[0] == 'E')
3987 error ("Remote failure reply: %s", buf);
3988 }
3989
3990 /* Display a null-terminated packet on stdout, for debugging, using C
3991 string notation. */
3992
3993 static void
3994 print_packet (char *buf)
3995 {
3996 puts_filtered ("\"");
3997 fputstr_filtered (buf, '"', gdb_stdout);
3998 puts_filtered ("\"");
3999 }
4000
4001 int
4002 putpkt (char *buf)
4003 {
4004 return putpkt_binary (buf, strlen (buf));
4005 }
4006
4007 /* Send a packet to the remote machine, with error checking. The data
4008 of the packet is in BUF. The string in BUF can be at most (rs->remote_packet_size) - 5
4009 to account for the $, # and checksum, and for a possible /0 if we are
4010 debugging (remote_debug) and want to print the sent packet as a string */
4011
4012 static int
4013 putpkt_binary (char *buf, int cnt)
4014 {
4015 struct remote_state *rs = get_remote_state ();
4016 int i;
4017 unsigned char csum = 0;
4018 char *buf2 = alloca (cnt + 6);
4019 long sizeof_junkbuf = (rs->remote_packet_size);
4020 char *junkbuf = alloca (sizeof_junkbuf);
4021
4022 int ch;
4023 int tcount = 0;
4024 char *p;
4025
4026 /* Copy the packet into buffer BUF2, encapsulating it
4027 and giving it a checksum. */
4028
4029 p = buf2;
4030 *p++ = '$';
4031
4032 for (i = 0; i < cnt; i++)
4033 {
4034 csum += buf[i];
4035 *p++ = buf[i];
4036 }
4037 *p++ = '#';
4038 *p++ = tohex ((csum >> 4) & 0xf);
4039 *p++ = tohex (csum & 0xf);
4040
4041 /* Send it over and over until we get a positive ack. */
4042
4043 while (1)
4044 {
4045 int started_error_output = 0;
4046
4047 if (remote_debug)
4048 {
4049 *p = '\0';
4050 fprintf_unfiltered (gdb_stdlog, "Sending packet: ");
4051 fputstrn_unfiltered (buf2, p - buf2, 0, gdb_stdlog);
4052 fprintf_unfiltered (gdb_stdlog, "...");
4053 gdb_flush (gdb_stdlog);
4054 }
4055 if (serial_write (remote_desc, buf2, p - buf2))
4056 perror_with_name ("putpkt: write failed");
4057
4058 /* read until either a timeout occurs (-2) or '+' is read */
4059 while (1)
4060 {
4061 ch = readchar (remote_timeout);
4062
4063 if (remote_debug)
4064 {
4065 switch (ch)
4066 {
4067 case '+':
4068 case '-':
4069 case SERIAL_TIMEOUT:
4070 case '$':
4071 if (started_error_output)
4072 {
4073 putchar_unfiltered ('\n');
4074 started_error_output = 0;
4075 }
4076 }
4077 }
4078
4079 switch (ch)
4080 {
4081 case '+':
4082 if (remote_debug)
4083 fprintf_unfiltered (gdb_stdlog, "Ack\n");
4084 return 1;
4085 case '-':
4086 if (remote_debug)
4087 fprintf_unfiltered (gdb_stdlog, "Nak\n");
4088 case SERIAL_TIMEOUT:
4089 tcount++;
4090 if (tcount > 3)
4091 return 0;
4092 break; /* Retransmit buffer */
4093 case '$':
4094 {
4095 if (remote_debug)
4096 fprintf_unfiltered (gdb_stdlog, "Packet instead of Ack, ignoring it\n");
4097 /* It's probably an old response, and we're out of sync.
4098 Just gobble up the packet and ignore it. */
4099 read_frame (junkbuf, sizeof_junkbuf);
4100 continue; /* Now, go look for + */
4101 }
4102 default:
4103 if (remote_debug)
4104 {
4105 if (!started_error_output)
4106 {
4107 started_error_output = 1;
4108 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
4109 }
4110 fputc_unfiltered (ch & 0177, gdb_stdlog);
4111 }
4112 continue;
4113 }
4114 break; /* Here to retransmit */
4115 }
4116
4117 #if 0
4118 /* This is wrong. If doing a long backtrace, the user should be
4119 able to get out next time we call QUIT, without anything as
4120 violent as interrupt_query. If we want to provide a way out of
4121 here without getting to the next QUIT, it should be based on
4122 hitting ^C twice as in remote_wait. */
4123 if (quit_flag)
4124 {
4125 quit_flag = 0;
4126 interrupt_query ();
4127 }
4128 #endif
4129 }
4130 }
4131
4132 static int remote_cisco_mode;
4133
4134 /* Come here after finding the start of the frame. Collect the rest
4135 into BUF, verifying the checksum, length, and handling run-length
4136 compression. No more than sizeof_buf-1 characters are read so that
4137 the buffer can be NUL terminated.
4138
4139 Returns -1 on error, number of characters in buffer (ignoring the
4140 trailing NULL) on success. (could be extended to return one of the
4141 SERIAL status indications). */
4142
4143 static long
4144 read_frame (char *buf,
4145 long sizeof_buf)
4146 {
4147 unsigned char csum;
4148 long bc;
4149 int c;
4150
4151 csum = 0;
4152 bc = 0;
4153
4154 while (1)
4155 {
4156 /* ASSERT (bc < sizeof_buf - 1) - space for trailing NUL */
4157 c = readchar (remote_timeout);
4158 switch (c)
4159 {
4160 case SERIAL_TIMEOUT:
4161 if (remote_debug)
4162 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog);
4163 return -1;
4164 case '$':
4165 if (remote_debug)
4166 fputs_filtered ("Saw new packet start in middle of old one\n",
4167 gdb_stdlog);
4168 return -1; /* Start a new packet, count retries */
4169 case '#':
4170 {
4171 unsigned char pktcsum;
4172 int check_0 = 0;
4173 int check_1 = 0;
4174
4175 buf[bc] = '\0';
4176
4177 check_0 = readchar (remote_timeout);
4178 if (check_0 >= 0)
4179 check_1 = readchar (remote_timeout);
4180
4181 if (check_0 == SERIAL_TIMEOUT || check_1 == SERIAL_TIMEOUT)
4182 {
4183 if (remote_debug)
4184 fputs_filtered ("Timeout in checksum, retrying\n", gdb_stdlog);
4185 return -1;
4186 }
4187 else if (check_0 < 0 || check_1 < 0)
4188 {
4189 if (remote_debug)
4190 fputs_filtered ("Communication error in checksum\n", gdb_stdlog);
4191 return -1;
4192 }
4193
4194 pktcsum = (fromhex (check_0) << 4) | fromhex (check_1);
4195 if (csum == pktcsum)
4196 return bc;
4197
4198 if (remote_debug)
4199 {
4200 fprintf_filtered (gdb_stdlog,
4201 "Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
4202 pktcsum, csum);
4203 fputs_filtered (buf, gdb_stdlog);
4204 fputs_filtered ("\n", gdb_stdlog);
4205 }
4206 /* Number of characters in buffer ignoring trailing
4207 NUL. */
4208 return -1;
4209 }
4210 case '*': /* Run length encoding */
4211 {
4212 int repeat;
4213 csum += c;
4214
4215 if (remote_cisco_mode == 0)
4216 {
4217 c = readchar (remote_timeout);
4218 csum += c;
4219 repeat = c - ' ' + 3; /* Compute repeat count */
4220 }
4221 else
4222 {
4223 /* Cisco's run-length encoding variant uses two
4224 hex chars to represent the repeat count. */
4225
4226 c = readchar (remote_timeout);
4227 csum += c;
4228 repeat = fromhex (c) << 4;
4229 c = readchar (remote_timeout);
4230 csum += c;
4231 repeat += fromhex (c);
4232 }
4233
4234 /* The character before ``*'' is repeated. */
4235
4236 if (repeat > 0 && repeat <= 255
4237 && bc > 0
4238 && bc + repeat - 1 < sizeof_buf - 1)
4239 {
4240 memset (&buf[bc], buf[bc - 1], repeat);
4241 bc += repeat;
4242 continue;
4243 }
4244
4245 buf[bc] = '\0';
4246 printf_filtered ("Repeat count %d too large for buffer: ", repeat);
4247 puts_filtered (buf);
4248 puts_filtered ("\n");
4249 return -1;
4250 }
4251 default:
4252 if (bc < sizeof_buf - 1)
4253 {
4254 buf[bc++] = c;
4255 csum += c;
4256 continue;
4257 }
4258
4259 buf[bc] = '\0';
4260 puts_filtered ("Remote packet too long: ");
4261 puts_filtered (buf);
4262 puts_filtered ("\n");
4263
4264 return -1;
4265 }
4266 }
4267 }
4268
4269 /* Read a packet from the remote machine, with error checking, and
4270 store it in BUF. If FOREVER, wait forever rather than timing out;
4271 this is used (in synchronous mode) to wait for a target that is is
4272 executing user code to stop. */
4273 /* FIXME: ezannoni 2000-02-01 this wrapper is necessary so that we
4274 don't have to change all the calls to getpkt to deal with the
4275 return value, because at the moment I don't know what the right
4276 thing to do it for those. */
4277 void
4278 getpkt (char *buf,
4279 long sizeof_buf,
4280 int forever)
4281 {
4282 int timed_out;
4283
4284 timed_out = getpkt_sane (buf, sizeof_buf, forever);
4285 }
4286
4287
4288 /* Read a packet from the remote machine, with error checking, and
4289 store it in BUF. If FOREVER, wait forever rather than timing out;
4290 this is used (in synchronous mode) to wait for a target that is is
4291 executing user code to stop. If FOREVER == 0, this function is
4292 allowed to time out gracefully and return an indication of this to
4293 the caller. */
4294 static int
4295 getpkt_sane (char *buf,
4296 long sizeof_buf,
4297 int forever)
4298 {
4299 int c;
4300 int tries;
4301 int timeout;
4302 int val;
4303
4304 strcpy (buf, "timeout");
4305
4306 if (forever)
4307 {
4308 timeout = watchdog > 0 ? watchdog : -1;
4309 }
4310
4311 else
4312 timeout = remote_timeout;
4313
4314 #define MAX_TRIES 3
4315
4316 for (tries = 1; tries <= MAX_TRIES; tries++)
4317 {
4318 /* This can loop forever if the remote side sends us characters
4319 continuously, but if it pauses, we'll get a zero from readchar
4320 because of timeout. Then we'll count that as a retry. */
4321
4322 /* Note that we will only wait forever prior to the start of a packet.
4323 After that, we expect characters to arrive at a brisk pace. They
4324 should show up within remote_timeout intervals. */
4325
4326 do
4327 {
4328 c = readchar (timeout);
4329
4330 if (c == SERIAL_TIMEOUT)
4331 {
4332 if (forever) /* Watchdog went off? Kill the target. */
4333 {
4334 QUIT;
4335 target_mourn_inferior ();
4336 error ("Watchdog has expired. Target detached.\n");
4337 }
4338 if (remote_debug)
4339 fputs_filtered ("Timed out.\n", gdb_stdlog);
4340 goto retry;
4341 }
4342 }
4343 while (c != '$');
4344
4345 /* We've found the start of a packet, now collect the data. */
4346
4347 val = read_frame (buf, sizeof_buf);
4348
4349 if (val >= 0)
4350 {
4351 if (remote_debug)
4352 {
4353 fprintf_unfiltered (gdb_stdlog, "Packet received: ");
4354 fputstr_unfiltered (buf, 0, gdb_stdlog);
4355 fprintf_unfiltered (gdb_stdlog, "\n");
4356 }
4357 serial_write (remote_desc, "+", 1);
4358 return 0;
4359 }
4360
4361 /* Try the whole thing again. */
4362 retry:
4363 serial_write (remote_desc, "-", 1);
4364 }
4365
4366 /* We have tried hard enough, and just can't receive the packet. Give up. */
4367
4368 printf_unfiltered ("Ignoring packet error, continuing...\n");
4369 serial_write (remote_desc, "+", 1);
4370 return 1;
4371 }
4372 \f
4373 static void
4374 remote_kill (void)
4375 {
4376 /* For some mysterious reason, wait_for_inferior calls kill instead of
4377 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
4378 if (kill_kludge)
4379 {
4380 kill_kludge = 0;
4381 target_mourn_inferior ();
4382 return;
4383 }
4384
4385 /* Use catch_errors so the user can quit from gdb even when we aren't on
4386 speaking terms with the remote system. */
4387 catch_errors ((catch_errors_ftype *) putpkt, "k", "", RETURN_MASK_ERROR);
4388
4389 /* Don't wait for it to die. I'm not really sure it matters whether
4390 we do or not. For the existing stubs, kill is a noop. */
4391 target_mourn_inferior ();
4392 }
4393
4394 /* Async version of remote_kill. */
4395 static void
4396 remote_async_kill (void)
4397 {
4398 /* Unregister the file descriptor from the event loop. */
4399 if (target_is_async_p ())
4400 serial_async (remote_desc, NULL, 0);
4401
4402 /* For some mysterious reason, wait_for_inferior calls kill instead of
4403 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
4404 if (kill_kludge)
4405 {
4406 kill_kludge = 0;
4407 target_mourn_inferior ();
4408 return;
4409 }
4410
4411 /* Use catch_errors so the user can quit from gdb even when we aren't on
4412 speaking terms with the remote system. */
4413 catch_errors ((catch_errors_ftype *) putpkt, "k", "", RETURN_MASK_ERROR);
4414
4415 /* Don't wait for it to die. I'm not really sure it matters whether
4416 we do or not. For the existing stubs, kill is a noop. */
4417 target_mourn_inferior ();
4418 }
4419
4420 static void
4421 remote_mourn (void)
4422 {
4423 remote_mourn_1 (&remote_ops);
4424 }
4425
4426 static void
4427 remote_async_mourn (void)
4428 {
4429 remote_mourn_1 (&remote_async_ops);
4430 }
4431
4432 static void
4433 extended_remote_mourn (void)
4434 {
4435 /* We do _not_ want to mourn the target like this; this will
4436 remove the extended remote target from the target stack,
4437 and the next time the user says "run" it'll fail.
4438
4439 FIXME: What is the right thing to do here? */
4440 #if 0
4441 remote_mourn_1 (&extended_remote_ops);
4442 #endif
4443 }
4444
4445 /* Worker function for remote_mourn. */
4446 static void
4447 remote_mourn_1 (struct target_ops *target)
4448 {
4449 unpush_target (target);
4450 generic_mourn_inferior ();
4451 }
4452
4453 /* In the extended protocol we want to be able to do things like
4454 "run" and have them basically work as expected. So we need
4455 a special create_inferior function.
4456
4457 FIXME: One day add support for changing the exec file
4458 we're debugging, arguments and an environment. */
4459
4460 static void
4461 extended_remote_create_inferior (char *exec_file, char *args, char **env)
4462 {
4463 /* Rip out the breakpoints; we'll reinsert them after restarting
4464 the remote server. */
4465 remove_breakpoints ();
4466
4467 /* Now restart the remote server. */
4468 extended_remote_restart ();
4469
4470 /* Now put the breakpoints back in. This way we're safe if the
4471 restart function works via a unix fork on the remote side. */
4472 insert_breakpoints ();
4473
4474 /* Clean up from the last time we were running. */
4475 clear_proceed_status ();
4476
4477 /* Let the remote process run. */
4478 proceed (-1, TARGET_SIGNAL_0, 0);
4479 }
4480
4481 /* Async version of extended_remote_create_inferior. */
4482 static void
4483 extended_remote_async_create_inferior (char *exec_file, char *args, char **env)
4484 {
4485 /* Rip out the breakpoints; we'll reinsert them after restarting
4486 the remote server. */
4487 remove_breakpoints ();
4488
4489 /* If running asynchronously, register the target file descriptor
4490 with the event loop. */
4491 if (event_loop_p && target_can_async_p ())
4492 target_async (inferior_event_handler, 0);
4493
4494 /* Now restart the remote server. */
4495 extended_remote_restart ();
4496
4497 /* Now put the breakpoints back in. This way we're safe if the
4498 restart function works via a unix fork on the remote side. */
4499 insert_breakpoints ();
4500
4501 /* Clean up from the last time we were running. */
4502 clear_proceed_status ();
4503
4504 /* Let the remote process run. */
4505 proceed (-1, TARGET_SIGNAL_0, 0);
4506 }
4507 \f
4508
4509 /* On some machines, e.g. 68k, we may use a different breakpoint instruction
4510 than other targets; in those use REMOTE_BREAKPOINT instead of just
4511 BREAKPOINT. Also, bi-endian targets may define LITTLE_REMOTE_BREAKPOINT
4512 and BIG_REMOTE_BREAKPOINT. If none of these are defined, we just call
4513 the standard routines that are in mem-break.c. */
4514
4515 /* FIXME, these ought to be done in a more dynamic fashion. For instance,
4516 the choice of breakpoint instruction affects target program design and
4517 vice versa, and by making it user-tweakable, the special code here
4518 goes away and we need fewer special GDB configurations. */
4519
4520 #if defined (LITTLE_REMOTE_BREAKPOINT) && defined (BIG_REMOTE_BREAKPOINT) && !defined(REMOTE_BREAKPOINT)
4521 #define REMOTE_BREAKPOINT
4522 #endif
4523
4524 #ifdef REMOTE_BREAKPOINT
4525
4526 /* If the target isn't bi-endian, just pretend it is. */
4527 #if !defined (LITTLE_REMOTE_BREAKPOINT) && !defined (BIG_REMOTE_BREAKPOINT)
4528 #define LITTLE_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
4529 #define BIG_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
4530 #endif
4531
4532 static unsigned char big_break_insn[] = BIG_REMOTE_BREAKPOINT;
4533 static unsigned char little_break_insn[] = LITTLE_REMOTE_BREAKPOINT;
4534
4535 #endif /* REMOTE_BREAKPOINT */
4536
4537 /* Insert a breakpoint on targets that don't have any better breakpoint
4538 support. We read the contents of the target location and stash it,
4539 then overwrite it with a breakpoint instruction. ADDR is the target
4540 location in the target machine. CONTENTS_CACHE is a pointer to
4541 memory allocated for saving the target contents. It is guaranteed
4542 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
4543 is accomplished via BREAKPOINT_MAX). */
4544
4545 static int
4546 remote_insert_breakpoint (CORE_ADDR addr, char *contents_cache)
4547 {
4548 struct remote_state *rs = get_remote_state ();
4549 #ifdef REMOTE_BREAKPOINT
4550 int val;
4551 #endif
4552 int bp_size;
4553
4554 /* Try the "Z" s/w breakpoint packet if it is not already disabled.
4555 If it succeeds, then set the support to PACKET_ENABLE. If it
4556 fails, and the user has explicitly requested the Z support then
4557 report an error, otherwise, mark it disabled and go on. */
4558
4559 if (remote_protocol_Z[Z_PACKET_SOFTWARE_BP].support != PACKET_DISABLE)
4560 {
4561 char *buf = alloca (rs->remote_packet_size);
4562 char *p = buf;
4563
4564 addr = remote_address_masked (addr);
4565 *(p++) = 'Z';
4566 *(p++) = '0';
4567 *(p++) = ',';
4568 p += hexnumstr (p, (ULONGEST) addr);
4569 BREAKPOINT_FROM_PC (&addr, &bp_size);
4570 sprintf (p, ",%d", bp_size);
4571
4572 putpkt (buf);
4573 getpkt (buf, (rs->remote_packet_size), 0);
4574
4575 switch (packet_ok (buf, &remote_protocol_Z[Z_PACKET_SOFTWARE_BP]))
4576 {
4577 case PACKET_ERROR:
4578 return -1;
4579 case PACKET_OK:
4580 return 0;
4581 case PACKET_UNKNOWN:
4582 break;
4583 }
4584 }
4585
4586 #ifdef REMOTE_BREAKPOINT
4587 val = target_read_memory (addr, contents_cache, sizeof big_break_insn);
4588
4589 if (val == 0)
4590 {
4591 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
4592 val = target_write_memory (addr, (char *) big_break_insn,
4593 sizeof big_break_insn);
4594 else
4595 val = target_write_memory (addr, (char *) little_break_insn,
4596 sizeof little_break_insn);
4597 }
4598
4599 return val;
4600 #else
4601 return memory_insert_breakpoint (addr, contents_cache);
4602 #endif /* REMOTE_BREAKPOINT */
4603 }
4604
4605 static int
4606 remote_remove_breakpoint (CORE_ADDR addr, char *contents_cache)
4607 {
4608 struct remote_state *rs = get_remote_state ();
4609 int bp_size;
4610
4611 if (remote_protocol_Z[Z_PACKET_SOFTWARE_BP].support != PACKET_DISABLE)
4612 {
4613 char *buf = alloca (rs->remote_packet_size);
4614 char *p = buf;
4615
4616 *(p++) = 'z';
4617 *(p++) = '0';
4618 *(p++) = ',';
4619
4620 addr = remote_address_masked (addr);
4621 p += hexnumstr (p, (ULONGEST) addr);
4622 BREAKPOINT_FROM_PC (&addr, &bp_size);
4623 sprintf (p, ",%d", bp_size);
4624
4625 putpkt (buf);
4626 getpkt (buf, (rs->remote_packet_size), 0);
4627
4628 return (buf[0] == 'E');
4629 }
4630
4631 #ifdef REMOTE_BREAKPOINT
4632 return target_write_memory (addr, contents_cache, sizeof big_break_insn);
4633 #else
4634 return memory_remove_breakpoint (addr, contents_cache);
4635 #endif /* REMOTE_BREAKPOINT */
4636 }
4637
4638 static int
4639 watchpoint_to_Z_packet (int type)
4640 {
4641 switch (type)
4642 {
4643 case hw_write:
4644 return 2;
4645 break;
4646 case hw_read:
4647 return 3;
4648 break;
4649 case hw_access:
4650 return 4;
4651 break;
4652 default:
4653 internal_error (__FILE__, __LINE__,
4654 "hw_bp_to_z: bad watchpoint type %d", type);
4655 }
4656 }
4657
4658 /* FIXME: This function should be static and a member of the remote
4659 target vector. */
4660
4661 int
4662 remote_insert_watchpoint (CORE_ADDR addr, int len, int type)
4663 {
4664 struct remote_state *rs = get_remote_state ();
4665 char *buf = alloca (rs->remote_packet_size);
4666 char *p;
4667 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
4668
4669 if (remote_protocol_Z[packet].support == PACKET_DISABLE)
4670 error ("Can't set hardware watchpoints without the '%s' (%s) packet\n",
4671 remote_protocol_Z[packet].name,
4672 remote_protocol_Z[packet].title);
4673
4674 sprintf (buf, "Z%x,", packet);
4675 p = strchr (buf, '\0');
4676 addr = remote_address_masked (addr);
4677 p += hexnumstr (p, (ULONGEST) addr);
4678 sprintf (p, ",%x", len);
4679
4680 putpkt (buf);
4681 getpkt (buf, (rs->remote_packet_size), 0);
4682
4683 switch (packet_ok (buf, &remote_protocol_Z[packet]))
4684 {
4685 case PACKET_ERROR:
4686 case PACKET_UNKNOWN:
4687 return -1;
4688 case PACKET_OK:
4689 return 0;
4690 }
4691 internal_error (__FILE__, __LINE__,
4692 "remote_insert_watchpoint: reached end of function");
4693 }
4694
4695 /* FIXME: This function should be static and a member of the remote
4696 target vector. */
4697
4698 int
4699 remote_remove_watchpoint (CORE_ADDR addr, int len, int type)
4700 {
4701 struct remote_state *rs = get_remote_state ();
4702 char *buf = alloca (rs->remote_packet_size);
4703 char *p;
4704 enum Z_packet_type packet = watchpoint_to_Z_packet (type);
4705
4706 if (remote_protocol_Z[packet].support == PACKET_DISABLE)
4707 error ("Can't clear hardware watchpoints without the '%s' (%s) packet\n",
4708 remote_protocol_Z[packet].name,
4709 remote_protocol_Z[packet].title);
4710
4711 sprintf (buf, "z%x,", packet);
4712 p = strchr (buf, '\0');
4713 addr = remote_address_masked (addr);
4714 p += hexnumstr (p, (ULONGEST) addr);
4715 sprintf (p, ",%x", len);
4716 putpkt (buf);
4717 getpkt (buf, (rs->remote_packet_size), 0);
4718
4719 switch (packet_ok (buf, &remote_protocol_Z[packet]))
4720 {
4721 case PACKET_ERROR:
4722 case PACKET_UNKNOWN:
4723 return -1;
4724 case PACKET_OK:
4725 return 0;
4726 }
4727 internal_error (__FILE__, __LINE__,
4728 "remote_remove_watchpoint: reached end of function");
4729 }
4730
4731 /* FIXME: This function should be static and a member of the remote
4732 target vector. */
4733
4734 int
4735 remote_insert_hw_breakpoint (CORE_ADDR addr, int len)
4736 {
4737 struct remote_state *rs = get_remote_state ();
4738 char *buf = alloca (rs->remote_packet_size);
4739 char *p = buf;
4740
4741 if (remote_protocol_Z[Z_PACKET_HARDWARE_BP].support == PACKET_DISABLE)
4742 error ("Can't set hardware breakpoint without the '%s' (%s) packet\n",
4743 remote_protocol_Z[Z_PACKET_HARDWARE_BP].name,
4744 remote_protocol_Z[Z_PACKET_HARDWARE_BP].title);
4745
4746 *(p++) = 'Z';
4747 *(p++) = '1';
4748 *(p++) = ',';
4749
4750 addr = remote_address_masked (addr);
4751 p += hexnumstr (p, (ULONGEST) addr);
4752 sprintf (p, ",%x", len);
4753
4754 putpkt (buf);
4755 getpkt (buf, (rs->remote_packet_size), 0);
4756
4757 switch (packet_ok (buf, &remote_protocol_Z[Z_PACKET_HARDWARE_BP]))
4758 {
4759 case PACKET_ERROR:
4760 case PACKET_UNKNOWN:
4761 return -1;
4762 case PACKET_OK:
4763 return 0;
4764 }
4765 internal_error (__FILE__, __LINE__,
4766 "remote_remove_watchpoint: reached end of function");
4767 }
4768
4769 /* FIXME: This function should be static and a member of the remote
4770 target vector. */
4771
4772 int
4773 remote_remove_hw_breakpoint (CORE_ADDR addr, int len)
4774 {
4775 struct remote_state *rs = get_remote_state ();
4776 char *buf = alloca (rs->remote_packet_size);
4777 char *p = buf;
4778
4779 if (remote_protocol_Z[Z_PACKET_HARDWARE_BP].support == PACKET_DISABLE)
4780 error ("Can't clear hardware breakpoint without the '%s' (%s) packet\n",
4781 remote_protocol_Z[Z_PACKET_HARDWARE_BP].name,
4782 remote_protocol_Z[Z_PACKET_HARDWARE_BP].title);
4783
4784 *(p++) = 'z';
4785 *(p++) = '1';
4786 *(p++) = ',';
4787
4788 addr = remote_address_masked (addr);
4789 p += hexnumstr (p, (ULONGEST) addr);
4790 sprintf (p, ",%x", len);
4791
4792 putpkt(buf);
4793 getpkt (buf, (rs->remote_packet_size), 0);
4794
4795 switch (packet_ok (buf, &remote_protocol_Z[Z_PACKET_HARDWARE_BP]))
4796 {
4797 case PACKET_ERROR:
4798 case PACKET_UNKNOWN:
4799 return -1;
4800 case PACKET_OK:
4801 return 0;
4802 }
4803 internal_error (__FILE__, __LINE__,
4804 "remote_remove_watchpoint: reached end of function");
4805 }
4806
4807 /* Some targets are only capable of doing downloads, and afterwards
4808 they switch to the remote serial protocol. This function provides
4809 a clean way to get from the download target to the remote target.
4810 It's basically just a wrapper so that we don't have to expose any
4811 of the internal workings of remote.c.
4812
4813 Prior to calling this routine, you should shutdown the current
4814 target code, else you will get the "A program is being debugged
4815 already..." message. Usually a call to pop_target() suffices. */
4816
4817 void
4818 push_remote_target (char *name, int from_tty)
4819 {
4820 printf_filtered ("Switching to remote protocol\n");
4821 remote_open (name, from_tty);
4822 }
4823
4824 /* Other targets want to use the entire remote serial module but with
4825 certain remote_ops overridden. */
4826
4827 void
4828 open_remote_target (char *name, int from_tty, struct target_ops *target,
4829 int extended_p)
4830 {
4831 printf_filtered ("Selecting the %sremote protocol\n",
4832 (extended_p ? "extended-" : ""));
4833 remote_open_1 (name, from_tty, target, extended_p);
4834 }
4835
4836 /* Table used by the crc32 function to calcuate the checksum. */
4837
4838 static unsigned long crc32_table[256] =
4839 {0, 0};
4840
4841 static unsigned long
4842 crc32 (unsigned char *buf, int len, unsigned int crc)
4843 {
4844 if (!crc32_table[1])
4845 {
4846 /* Initialize the CRC table and the decoding table. */
4847 int i, j;
4848 unsigned int c;
4849
4850 for (i = 0; i < 256; i++)
4851 {
4852 for (c = i << 24, j = 8; j > 0; --j)
4853 c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
4854 crc32_table[i] = c;
4855 }
4856 }
4857
4858 while (len--)
4859 {
4860 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *buf) & 255];
4861 buf++;
4862 }
4863 return crc;
4864 }
4865
4866 /* compare-sections command
4867
4868 With no arguments, compares each loadable section in the exec bfd
4869 with the same memory range on the target, and reports mismatches.
4870 Useful for verifying the image on the target against the exec file.
4871 Depends on the target understanding the new "qCRC:" request. */
4872
4873 /* FIXME: cagney/1999-10-26: This command should be broken down into a
4874 target method (target verify memory) and generic version of the
4875 actual command. This will allow other high-level code (especially
4876 generic_load()) to make use of this target functionality. */
4877
4878 static void
4879 compare_sections_command (char *args, int from_tty)
4880 {
4881 struct remote_state *rs = get_remote_state ();
4882 asection *s;
4883 unsigned long host_crc, target_crc;
4884 extern bfd *exec_bfd;
4885 struct cleanup *old_chain;
4886 char *tmp;
4887 char *sectdata;
4888 const char *sectname;
4889 char *buf = alloca (rs->remote_packet_size);
4890 bfd_size_type size;
4891 bfd_vma lma;
4892 int matched = 0;
4893 int mismatched = 0;
4894
4895 if (!exec_bfd)
4896 error ("command cannot be used without an exec file");
4897 if (!current_target.to_shortname ||
4898 strcmp (current_target.to_shortname, "remote") != 0)
4899 error ("command can only be used with remote target");
4900
4901 for (s = exec_bfd->sections; s; s = s->next)
4902 {
4903 if (!(s->flags & SEC_LOAD))
4904 continue; /* skip non-loadable section */
4905
4906 size = bfd_get_section_size_before_reloc (s);
4907 if (size == 0)
4908 continue; /* skip zero-length section */
4909
4910 sectname = bfd_get_section_name (exec_bfd, s);
4911 if (args && strcmp (args, sectname) != 0)
4912 continue; /* not the section selected by user */
4913
4914 matched = 1; /* do this section */
4915 lma = s->lma;
4916 /* FIXME: assumes lma can fit into long */
4917 sprintf (buf, "qCRC:%lx,%lx", (long) lma, (long) size);
4918 putpkt (buf);
4919
4920 /* be clever; compute the host_crc before waiting for target reply */
4921 sectdata = xmalloc (size);
4922 old_chain = make_cleanup (xfree, sectdata);
4923 bfd_get_section_contents (exec_bfd, s, sectdata, 0, size);
4924 host_crc = crc32 ((unsigned char *) sectdata, size, 0xffffffff);
4925
4926 getpkt (buf, (rs->remote_packet_size), 0);
4927 if (buf[0] == 'E')
4928 error ("target memory fault, section %s, range 0x%08x -- 0x%08x",
4929 sectname, lma, lma + size);
4930 if (buf[0] != 'C')
4931 error ("remote target does not support this operation");
4932
4933 for (target_crc = 0, tmp = &buf[1]; *tmp; tmp++)
4934 target_crc = target_crc * 16 + fromhex (*tmp);
4935
4936 printf_filtered ("Section %s, range 0x%s -- 0x%s: ",
4937 sectname, paddr (lma), paddr (lma + size));
4938 if (host_crc == target_crc)
4939 printf_filtered ("matched.\n");
4940 else
4941 {
4942 printf_filtered ("MIS-MATCHED!\n");
4943 mismatched++;
4944 }
4945
4946 do_cleanups (old_chain);
4947 }
4948 if (mismatched > 0)
4949 warning ("One or more sections of the remote executable does not match\n\
4950 the loaded file\n");
4951 if (args && !matched)
4952 printf_filtered ("No loaded section named '%s'.\n", args);
4953 }
4954
4955 static int
4956 remote_query (int query_type, char *buf, char *outbuf, int *bufsiz)
4957 {
4958 struct remote_state *rs = get_remote_state ();
4959 int i;
4960 char *buf2 = alloca (rs->remote_packet_size);
4961 char *p2 = &buf2[0];
4962
4963 if (!bufsiz)
4964 error ("null pointer to remote bufer size specified");
4965
4966 /* minimum outbuf size is (rs->remote_packet_size) - if bufsiz is not large enough let
4967 the caller know and return what the minimum size is */
4968 /* Note: a zero bufsiz can be used to query the minimum buffer size */
4969 if (*bufsiz < (rs->remote_packet_size))
4970 {
4971 *bufsiz = (rs->remote_packet_size);
4972 return -1;
4973 }
4974
4975 /* except for querying the minimum buffer size, target must be open */
4976 if (!remote_desc)
4977 error ("remote query is only available after target open");
4978
4979 /* we only take uppercase letters as query types, at least for now */
4980 if ((query_type < 'A') || (query_type > 'Z'))
4981 error ("invalid remote query type");
4982
4983 if (!buf)
4984 error ("null remote query specified");
4985
4986 if (!outbuf)
4987 error ("remote query requires a buffer to receive data");
4988
4989 outbuf[0] = '\0';
4990
4991 *p2++ = 'q';
4992 *p2++ = query_type;
4993
4994 /* we used one buffer char for the remote protocol q command and another
4995 for the query type. As the remote protocol encapsulation uses 4 chars
4996 plus one extra in case we are debugging (remote_debug),
4997 we have PBUFZIZ - 7 left to pack the query string */
4998 i = 0;
4999 while (buf[i] && (i < ((rs->remote_packet_size) - 8)))
5000 {
5001 /* bad caller may have sent forbidden characters */
5002 if ((!isprint (buf[i])) || (buf[i] == '$') || (buf[i] == '#'))
5003 error ("illegal characters in query string");
5004
5005 *p2++ = buf[i];
5006 i++;
5007 }
5008 *p2 = buf[i];
5009
5010 if (buf[i])
5011 error ("query larger than available buffer");
5012
5013 i = putpkt (buf2);
5014 if (i < 0)
5015 return i;
5016
5017 getpkt (outbuf, *bufsiz, 0);
5018
5019 return 0;
5020 }
5021
5022 static void
5023 remote_rcmd (char *command,
5024 struct ui_file *outbuf)
5025 {
5026 struct remote_state *rs = get_remote_state ();
5027 int i;
5028 char *buf = alloca (rs->remote_packet_size);
5029 char *p = buf;
5030
5031 if (!remote_desc)
5032 error ("remote rcmd is only available after target open");
5033
5034 /* Send a NULL command across as an empty command */
5035 if (command == NULL)
5036 command = "";
5037
5038 /* The query prefix */
5039 strcpy (buf, "qRcmd,");
5040 p = strchr (buf, '\0');
5041
5042 if ((strlen (buf) + strlen (command) * 2 + 8/*misc*/) > (rs->remote_packet_size))
5043 error ("\"monitor\" command ``%s'' is too long\n", command);
5044
5045 /* Encode the actual command */
5046 bin2hex (command, p, 0);
5047
5048 if (putpkt (buf) < 0)
5049 error ("Communication problem with target\n");
5050
5051 /* get/display the response */
5052 while (1)
5053 {
5054 /* XXX - see also tracepoint.c:remote_get_noisy_reply() */
5055 buf[0] = '\0';
5056 getpkt (buf, (rs->remote_packet_size), 0);
5057 if (buf[0] == '\0')
5058 error ("Target does not support this command\n");
5059 if (buf[0] == 'O' && buf[1] != 'K')
5060 {
5061 remote_console_output (buf + 1); /* 'O' message from stub */
5062 continue;
5063 }
5064 if (strcmp (buf, "OK") == 0)
5065 break;
5066 if (strlen (buf) == 3 && buf[0] == 'E'
5067 && isdigit (buf[1]) && isdigit (buf[2]))
5068 {
5069 error ("Protocol error with Rcmd");
5070 }
5071 for (p = buf; p[0] != '\0' && p[1] != '\0'; p += 2)
5072 {
5073 char c = (fromhex (p[0]) << 4) + fromhex (p[1]);
5074 fputc_unfiltered (c, outbuf);
5075 }
5076 break;
5077 }
5078 }
5079
5080 static void
5081 packet_command (char *args, int from_tty)
5082 {
5083 struct remote_state *rs = get_remote_state ();
5084 char *buf = alloca (rs->remote_packet_size);
5085
5086 if (!remote_desc)
5087 error ("command can only be used with remote target");
5088
5089 if (!args)
5090 error ("remote-packet command requires packet text as argument");
5091
5092 puts_filtered ("sending: ");
5093 print_packet (args);
5094 puts_filtered ("\n");
5095 putpkt (args);
5096
5097 getpkt (buf, (rs->remote_packet_size), 0);
5098 puts_filtered ("received: ");
5099 print_packet (buf);
5100 puts_filtered ("\n");
5101 }
5102
5103 #if 0
5104 /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------------- */
5105
5106 static void display_thread_info (struct gdb_ext_thread_info *info);
5107
5108 static void threadset_test_cmd (char *cmd, int tty);
5109
5110 static void threadalive_test (char *cmd, int tty);
5111
5112 static void threadlist_test_cmd (char *cmd, int tty);
5113
5114 int get_and_display_threadinfo (threadref * ref);
5115
5116 static void threadinfo_test_cmd (char *cmd, int tty);
5117
5118 static int thread_display_step (threadref * ref, void *context);
5119
5120 static void threadlist_update_test_cmd (char *cmd, int tty);
5121
5122 static void init_remote_threadtests (void);
5123
5124 #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid */
5125
5126 static void
5127 threadset_test_cmd (char *cmd, int tty)
5128 {
5129 int sample_thread = SAMPLE_THREAD;
5130
5131 printf_filtered ("Remote threadset test\n");
5132 set_thread (sample_thread, 1);
5133 }
5134
5135
5136 static void
5137 threadalive_test (char *cmd, int tty)
5138 {
5139 int sample_thread = SAMPLE_THREAD;
5140
5141 if (remote_thread_alive (pid_to_ptid (sample_thread)))
5142 printf_filtered ("PASS: Thread alive test\n");
5143 else
5144 printf_filtered ("FAIL: Thread alive test\n");
5145 }
5146
5147 void output_threadid (char *title, threadref * ref);
5148
5149 void
5150 output_threadid (char *title, threadref *ref)
5151 {
5152 char hexid[20];
5153
5154 pack_threadid (&hexid[0], ref); /* Convert threead id into hex */
5155 hexid[16] = 0;
5156 printf_filtered ("%s %s\n", title, (&hexid[0]));
5157 }
5158
5159 static void
5160 threadlist_test_cmd (char *cmd, int tty)
5161 {
5162 int startflag = 1;
5163 threadref nextthread;
5164 int done, result_count;
5165 threadref threadlist[3];
5166
5167 printf_filtered ("Remote Threadlist test\n");
5168 if (!remote_get_threadlist (startflag, &nextthread, 3, &done,
5169 &result_count, &threadlist[0]))
5170 printf_filtered ("FAIL: threadlist test\n");
5171 else
5172 {
5173 threadref *scan = threadlist;
5174 threadref *limit = scan + result_count;
5175
5176 while (scan < limit)
5177 output_threadid (" thread ", scan++);
5178 }
5179 }
5180
5181 void
5182 display_thread_info (struct gdb_ext_thread_info *info)
5183 {
5184 output_threadid ("Threadid: ", &info->threadid);
5185 printf_filtered ("Name: %s\n ", info->shortname);
5186 printf_filtered ("State: %s\n", info->display);
5187 printf_filtered ("other: %s\n\n", info->more_display);
5188 }
5189
5190 int
5191 get_and_display_threadinfo (threadref *ref)
5192 {
5193 int result;
5194 int set;
5195 struct gdb_ext_thread_info threadinfo;
5196
5197 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
5198 | TAG_MOREDISPLAY | TAG_DISPLAY;
5199 if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo)))
5200 display_thread_info (&threadinfo);
5201 return result;
5202 }
5203
5204 static void
5205 threadinfo_test_cmd (char *cmd, int tty)
5206 {
5207 int athread = SAMPLE_THREAD;
5208 threadref thread;
5209 int set;
5210
5211 int_to_threadref (&thread, athread);
5212 printf_filtered ("Remote Threadinfo test\n");
5213 if (!get_and_display_threadinfo (&thread))
5214 printf_filtered ("FAIL cannot get thread info\n");
5215 }
5216
5217 static int
5218 thread_display_step (threadref *ref, void *context)
5219 {
5220 /* output_threadid(" threadstep ",ref); *//* simple test */
5221 return get_and_display_threadinfo (ref);
5222 }
5223
5224 static void
5225 threadlist_update_test_cmd (char *cmd, int tty)
5226 {
5227 printf_filtered ("Remote Threadlist update test\n");
5228 remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS);
5229 }
5230
5231 static void
5232 init_remote_threadtests (void)
5233 {
5234 add_com ("tlist", class_obscure, threadlist_test_cmd,
5235 "Fetch and print the remote list of thread identifiers, one pkt only");
5236 add_com ("tinfo", class_obscure, threadinfo_test_cmd,
5237 "Fetch and display info about one thread");
5238 add_com ("tset", class_obscure, threadset_test_cmd,
5239 "Test setting to a different thread");
5240 add_com ("tupd", class_obscure, threadlist_update_test_cmd,
5241 "Iterate through updating all remote thread info");
5242 add_com ("talive", class_obscure, threadalive_test,
5243 " Remote thread alive test ");
5244 }
5245
5246 #endif /* 0 */
5247
5248 /* Convert a thread ID to a string. Returns the string in a static
5249 buffer. */
5250
5251 static char *
5252 remote_pid_to_str (ptid_t ptid)
5253 {
5254 static char buf[30];
5255
5256 sprintf (buf, "Thread %d", PIDGET (ptid));
5257 return buf;
5258 }
5259
5260 static void
5261 init_remote_ops (void)
5262 {
5263 remote_ops.to_shortname = "remote";
5264 remote_ops.to_longname = "Remote serial target in gdb-specific protocol";
5265 remote_ops.to_doc =
5266 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5267 Specify the serial device it is connected to\n\
5268 (e.g. /dev/ttyS0, /dev/ttya, COM1, etc.).";
5269 remote_ops.to_open = remote_open;
5270 remote_ops.to_close = remote_close;
5271 remote_ops.to_detach = remote_detach;
5272 remote_ops.to_resume = remote_resume;
5273 remote_ops.to_wait = remote_wait;
5274 remote_ops.to_fetch_registers = remote_fetch_registers;
5275 remote_ops.to_store_registers = remote_store_registers;
5276 remote_ops.to_prepare_to_store = remote_prepare_to_store;
5277 remote_ops.to_xfer_memory = remote_xfer_memory;
5278 remote_ops.to_files_info = remote_files_info;
5279 remote_ops.to_insert_breakpoint = remote_insert_breakpoint;
5280 remote_ops.to_remove_breakpoint = remote_remove_breakpoint;
5281 remote_ops.to_kill = remote_kill;
5282 remote_ops.to_load = generic_load;
5283 remote_ops.to_mourn_inferior = remote_mourn;
5284 remote_ops.to_thread_alive = remote_thread_alive;
5285 remote_ops.to_find_new_threads = remote_threads_info;
5286 remote_ops.to_pid_to_str = remote_pid_to_str;
5287 remote_ops.to_extra_thread_info = remote_threads_extra_info;
5288 remote_ops.to_stop = remote_stop;
5289 remote_ops.to_query = remote_query;
5290 remote_ops.to_rcmd = remote_rcmd;
5291 remote_ops.to_stratum = process_stratum;
5292 remote_ops.to_has_all_memory = 1;
5293 remote_ops.to_has_memory = 1;
5294 remote_ops.to_has_stack = 1;
5295 remote_ops.to_has_registers = 1;
5296 remote_ops.to_has_execution = 1;
5297 remote_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */
5298 remote_ops.to_magic = OPS_MAGIC;
5299 }
5300
5301 /* Set up the extended remote vector by making a copy of the standard
5302 remote vector and adding to it. */
5303
5304 static void
5305 init_extended_remote_ops (void)
5306 {
5307 extended_remote_ops = remote_ops;
5308
5309 extended_remote_ops.to_shortname = "extended-remote";
5310 extended_remote_ops.to_longname =
5311 "Extended remote serial target in gdb-specific protocol";
5312 extended_remote_ops.to_doc =
5313 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5314 Specify the serial device it is connected to (e.g. /dev/ttya).",
5315 extended_remote_ops.to_open = extended_remote_open;
5316 extended_remote_ops.to_create_inferior = extended_remote_create_inferior;
5317 extended_remote_ops.to_mourn_inferior = extended_remote_mourn;
5318 }
5319
5320 /*
5321 * Command: info remote-process
5322 *
5323 * This implements Cisco's version of the "info proc" command.
5324 *
5325 * This query allows the target stub to return an arbitrary string
5326 * (or strings) giving arbitrary information about the target process.
5327 * This is optional; the target stub isn't required to implement it.
5328 *
5329 * Syntax: qfProcessInfo request first string
5330 * qsProcessInfo request subsequent string
5331 * reply: 'O'<hex-encoded-string>
5332 * 'l' last reply (empty)
5333 */
5334
5335 static void
5336 remote_info_process (char *args, int from_tty)
5337 {
5338 struct remote_state *rs = get_remote_state ();
5339 char *buf = alloca (rs->remote_packet_size);
5340
5341 if (remote_desc == 0)
5342 error ("Command can only be used when connected to the remote target.");
5343
5344 putpkt ("qfProcessInfo");
5345 getpkt (buf, (rs->remote_packet_size), 0);
5346 if (buf[0] == 0)
5347 return; /* Silently: target does not support this feature. */
5348
5349 if (buf[0] == 'E')
5350 error ("info proc: target error.");
5351
5352 while (buf[0] == 'O') /* Capitol-O packet */
5353 {
5354 remote_console_output (&buf[1]);
5355 putpkt ("qsProcessInfo");
5356 getpkt (buf, (rs->remote_packet_size), 0);
5357 }
5358 }
5359
5360 /*
5361 * Target Cisco
5362 */
5363
5364 static void
5365 remote_cisco_open (char *name, int from_tty)
5366 {
5367 if (name == 0)
5368 error (
5369 "To open a remote debug connection, you need to specify what \n\
5370 device is attached to the remote system (e.g. host:port).");
5371
5372 /* See FIXME above */
5373 wait_forever_enabled_p = 1;
5374
5375 target_preopen (from_tty);
5376
5377 unpush_target (&remote_cisco_ops);
5378
5379 remote_desc = serial_open (name);
5380 if (!remote_desc)
5381 perror_with_name (name);
5382
5383 /*
5384 * If a baud rate was specified on the gdb command line it will
5385 * be greater than the initial value of -1. If it is, use it otherwise
5386 * default to 9600
5387 */
5388
5389 baud_rate = (baud_rate > 0) ? baud_rate : 9600;
5390 if (serial_setbaudrate (remote_desc, baud_rate))
5391 {
5392 serial_close (remote_desc);
5393 perror_with_name (name);
5394 }
5395
5396 serial_raw (remote_desc);
5397
5398 /* If there is something sitting in the buffer we might take it as a
5399 response to a command, which would be bad. */
5400 serial_flush_input (remote_desc);
5401
5402 if (from_tty)
5403 {
5404 puts_filtered ("Remote debugging using ");
5405 puts_filtered (name);
5406 puts_filtered ("\n");
5407 }
5408
5409 remote_cisco_mode = 1;
5410
5411 push_target (&remote_cisco_ops); /* Switch to using cisco target now */
5412
5413 init_all_packet_configs ();
5414
5415 general_thread = -2;
5416 continue_thread = -2;
5417
5418 /* Probe for ability to use "ThreadInfo" query, as required. */
5419 use_threadinfo_query = 1;
5420 use_threadextra_query = 1;
5421
5422 /* Without this, some commands which require an active target (such
5423 as kill) won't work. This variable serves (at least) double duty
5424 as both the pid of the target process (if it has such), and as a
5425 flag indicating that a target is active. These functions should
5426 be split out into seperate variables, especially since GDB will
5427 someday have a notion of debugging several processes. */
5428 inferior_ptid = pid_to_ptid (MAGIC_NULL_PID);
5429
5430 /* Start the remote connection; if error (0), discard this target. */
5431
5432 if (!catch_errors (remote_start_remote_dummy, (char *) 0,
5433 "Couldn't establish connection to remote target\n",
5434 RETURN_MASK_ALL))
5435 {
5436 pop_target ();
5437 return;
5438 }
5439 }
5440
5441 static void
5442 remote_cisco_close (int quitting)
5443 {
5444 remote_cisco_mode = 0;
5445 remote_close (quitting);
5446 }
5447
5448 static void
5449 remote_cisco_mourn (void)
5450 {
5451 remote_mourn_1 (&remote_cisco_ops);
5452 }
5453
5454 enum
5455 {
5456 READ_MORE,
5457 FATAL_ERROR,
5458 ENTER_DEBUG,
5459 DISCONNECT_TELNET
5460 }
5461 minitelnet_return;
5462
5463 /* Shared between readsocket() and readtty(). The size is arbitrary,
5464 however all targets are known to support a 400 character packet. */
5465 static char tty_input[400];
5466
5467 static int escape_count;
5468 static int echo_check;
5469 extern int quit_flag;
5470
5471 static int
5472 readsocket (void)
5473 {
5474 int data;
5475
5476 /* Loop until the socket doesn't have any more data */
5477
5478 while ((data = readchar (0)) >= 0)
5479 {
5480 /* Check for the escape sequence */
5481 if (data == '|')
5482 {
5483 /* If this is the fourth escape, get out */
5484 if (++escape_count == 4)
5485 {
5486 return ENTER_DEBUG;
5487 }
5488 else
5489 { /* This is a '|', but not the fourth in a row.
5490 Continue without echoing it. If it isn't actually
5491 one of four in a row, it'll be echoed later. */
5492 continue;
5493 }
5494 }
5495 else
5496 /* Not a '|' */
5497 {
5498 /* Ensure any pending '|'s are flushed. */
5499
5500 for (; escape_count > 0; escape_count--)
5501 putchar ('|');
5502 }
5503
5504 if (data == '\r') /* If this is a return character, */
5505 continue; /* - just supress it. */
5506
5507 if (echo_check != -1) /* Check for echo of user input. */
5508 {
5509 if (tty_input[echo_check] == data)
5510 {
5511 gdb_assert (echo_check <= sizeof (tty_input));
5512 echo_check++; /* Character matched user input: */
5513 continue; /* Continue without echoing it. */
5514 }
5515 else if ((data == '\n') && (tty_input[echo_check] == '\r'))
5516 { /* End of the line (and of echo checking). */
5517 echo_check = -1; /* No more echo supression */
5518 continue; /* Continue without echoing. */
5519 }
5520 else
5521 { /* Failed check for echo of user input.
5522 We now have some suppressed output to flush! */
5523 int j;
5524
5525 for (j = 0; j < echo_check; j++)
5526 putchar (tty_input[j]);
5527 echo_check = -1;
5528 }
5529 }
5530 putchar (data); /* Default case: output the char. */
5531 }
5532
5533 if (data == SERIAL_TIMEOUT) /* Timeout returned from readchar. */
5534 return READ_MORE; /* Try to read some more */
5535 else
5536 return FATAL_ERROR; /* Trouble, bail out */
5537 }
5538
5539 static int
5540 readtty (void)
5541 {
5542 int tty_bytecount;
5543
5544 /* First, read a buffer full from the terminal */
5545 tty_bytecount = read (fileno (stdin), tty_input, sizeof (tty_input) - 1);
5546 if (tty_bytecount == -1)
5547 {
5548 perror ("readtty: read failed");
5549 return FATAL_ERROR;
5550 }
5551
5552 /* Remove a quoted newline. */
5553 if (tty_input[tty_bytecount - 1] == '\n' &&
5554 tty_input[tty_bytecount - 2] == '\\') /* line ending in backslash */
5555 {
5556 tty_input[--tty_bytecount] = 0; /* remove newline */
5557 tty_input[--tty_bytecount] = 0; /* remove backslash */
5558 }
5559
5560 /* Turn trailing newlines into returns */
5561 if (tty_input[tty_bytecount - 1] == '\n')
5562 tty_input[tty_bytecount - 1] = '\r';
5563
5564 /* If the line consists of a ~, enter debugging mode. */
5565 if ((tty_input[0] == '~') && (tty_bytecount == 2))
5566 return ENTER_DEBUG;
5567
5568 /* Make this a zero terminated string and write it out */
5569 tty_input[tty_bytecount] = 0;
5570 if (serial_write (remote_desc, tty_input, tty_bytecount))
5571 {
5572 perror_with_name ("readtty: write failed");
5573 return FATAL_ERROR;
5574 }
5575
5576 return READ_MORE;
5577 }
5578
5579 static int
5580 minitelnet (void)
5581 {
5582 fd_set input; /* file descriptors for select */
5583 int tablesize; /* max number of FDs for select */
5584 int status;
5585 int quit_count = 0;
5586
5587 extern int escape_count; /* global shared by readsocket */
5588 extern int echo_check; /* ditto */
5589
5590 escape_count = 0;
5591 echo_check = -1;
5592
5593 tablesize = 8 * sizeof (input);
5594
5595 for (;;)
5596 {
5597 /* Check for anything from our socket - doesn't block. Note that
5598 this must be done *before* the select as there may be
5599 buffered I/O waiting to be processed. */
5600
5601 if ((status = readsocket ()) == FATAL_ERROR)
5602 {
5603 error ("Debugging terminated by communications error");
5604 }
5605 else if (status != READ_MORE)
5606 {
5607 return (status);
5608 }
5609
5610 fflush (stdout); /* Flush output before blocking */
5611
5612 /* Now block on more socket input or TTY input */
5613
5614 FD_ZERO (&input);
5615 FD_SET (fileno (stdin), &input);
5616 FD_SET (deprecated_serial_fd (remote_desc), &input);
5617
5618 status = select (tablesize, &input, 0, 0, 0);
5619 if ((status == -1) && (errno != EINTR))
5620 {
5621 error ("Communications error on select %d", errno);
5622 }
5623
5624 /* Handle Control-C typed */
5625
5626 if (quit_flag)
5627 {
5628 if ((++quit_count) == 2)
5629 {
5630 if (query ("Interrupt GDB? "))
5631 {
5632 printf_filtered ("Interrupted by user.\n");
5633 return_to_top_level (RETURN_QUIT);
5634 }
5635 quit_count = 0;
5636 }
5637 quit_flag = 0;
5638
5639 if (remote_break)
5640 serial_send_break (remote_desc);
5641 else
5642 serial_write (remote_desc, "\003", 1);
5643
5644 continue;
5645 }
5646
5647 /* Handle console input */
5648
5649 if (FD_ISSET (fileno (stdin), &input))
5650 {
5651 quit_count = 0;
5652 echo_check = 0;
5653 status = readtty ();
5654 if (status == READ_MORE)
5655 continue;
5656
5657 return status; /* telnet session ended */
5658 }
5659 }
5660 }
5661
5662 static ptid_t
5663 remote_cisco_wait (ptid_t ptid, struct target_waitstatus *status)
5664 {
5665 if (minitelnet () != ENTER_DEBUG)
5666 {
5667 error ("Debugging session terminated by protocol error");
5668 }
5669 putpkt ("?");
5670 return remote_wait (ptid, status);
5671 }
5672
5673 static void
5674 init_remote_cisco_ops (void)
5675 {
5676 remote_cisco_ops.to_shortname = "cisco";
5677 remote_cisco_ops.to_longname = "Remote serial target in cisco-specific protocol";
5678 remote_cisco_ops.to_doc =
5679 "Use a remote machine via TCP, using a cisco-specific protocol.\n\
5680 Specify the serial device it is connected to (e.g. host:2020).";
5681 remote_cisco_ops.to_open = remote_cisco_open;
5682 remote_cisco_ops.to_close = remote_cisco_close;
5683 remote_cisco_ops.to_detach = remote_detach;
5684 remote_cisco_ops.to_resume = remote_resume;
5685 remote_cisco_ops.to_wait = remote_cisco_wait;
5686 remote_cisco_ops.to_fetch_registers = remote_fetch_registers;
5687 remote_cisco_ops.to_store_registers = remote_store_registers;
5688 remote_cisco_ops.to_prepare_to_store = remote_prepare_to_store;
5689 remote_cisco_ops.to_xfer_memory = remote_xfer_memory;
5690 remote_cisco_ops.to_files_info = remote_files_info;
5691 remote_cisco_ops.to_insert_breakpoint = remote_insert_breakpoint;
5692 remote_cisco_ops.to_remove_breakpoint = remote_remove_breakpoint;
5693 remote_cisco_ops.to_kill = remote_kill;
5694 remote_cisco_ops.to_load = generic_load;
5695 remote_cisco_ops.to_mourn_inferior = remote_cisco_mourn;
5696 remote_cisco_ops.to_thread_alive = remote_thread_alive;
5697 remote_cisco_ops.to_find_new_threads = remote_threads_info;
5698 remote_cisco_ops.to_pid_to_str = remote_pid_to_str;
5699 remote_cisco_ops.to_extra_thread_info = remote_threads_extra_info;
5700 remote_cisco_ops.to_stratum = process_stratum;
5701 remote_cisco_ops.to_has_all_memory = 1;
5702 remote_cisco_ops.to_has_memory = 1;
5703 remote_cisco_ops.to_has_stack = 1;
5704 remote_cisco_ops.to_has_registers = 1;
5705 remote_cisco_ops.to_has_execution = 1;
5706 remote_cisco_ops.to_magic = OPS_MAGIC;
5707 }
5708
5709 static int
5710 remote_can_async_p (void)
5711 {
5712 /* We're async whenever the serial device is. */
5713 return (current_target.to_async_mask_value) && serial_can_async_p (remote_desc);
5714 }
5715
5716 static int
5717 remote_is_async_p (void)
5718 {
5719 /* We're async whenever the serial device is. */
5720 return (current_target.to_async_mask_value) && serial_is_async_p (remote_desc);
5721 }
5722
5723 /* Pass the SERIAL event on and up to the client. One day this code
5724 will be able to delay notifying the client of an event until the
5725 point where an entire packet has been received. */
5726
5727 static void (*async_client_callback) (enum inferior_event_type event_type, void *context);
5728 static void *async_client_context;
5729 static serial_event_ftype remote_async_serial_handler;
5730
5731 static void
5732 remote_async_serial_handler (struct serial *scb, void *context)
5733 {
5734 /* Don't propogate error information up to the client. Instead let
5735 the client find out about the error by querying the target. */
5736 async_client_callback (INF_REG_EVENT, async_client_context);
5737 }
5738
5739 static void
5740 remote_async (void (*callback) (enum inferior_event_type event_type, void *context), void *context)
5741 {
5742 if (current_target.to_async_mask_value == 0)
5743 internal_error (__FILE__, __LINE__,
5744 "Calling remote_async when async is masked");
5745
5746 if (callback != NULL)
5747 {
5748 serial_async (remote_desc, remote_async_serial_handler, NULL);
5749 async_client_callback = callback;
5750 async_client_context = context;
5751 }
5752 else
5753 serial_async (remote_desc, NULL, NULL);
5754 }
5755
5756 /* Target async and target extended-async.
5757
5758 This are temporary targets, until it is all tested. Eventually
5759 async support will be incorporated int the usual 'remote'
5760 target. */
5761
5762 static void
5763 init_remote_async_ops (void)
5764 {
5765 remote_async_ops.to_shortname = "async";
5766 remote_async_ops.to_longname = "Remote serial target in async version of the gdb-specific protocol";
5767 remote_async_ops.to_doc =
5768 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5769 Specify the serial device it is connected to (e.g. /dev/ttya).";
5770 remote_async_ops.to_open = remote_async_open;
5771 remote_async_ops.to_close = remote_close;
5772 remote_async_ops.to_detach = remote_async_detach;
5773 remote_async_ops.to_resume = remote_async_resume;
5774 remote_async_ops.to_wait = remote_async_wait;
5775 remote_async_ops.to_fetch_registers = remote_fetch_registers;
5776 remote_async_ops.to_store_registers = remote_store_registers;
5777 remote_async_ops.to_prepare_to_store = remote_prepare_to_store;
5778 remote_async_ops.to_xfer_memory = remote_xfer_memory;
5779 remote_async_ops.to_files_info = remote_files_info;
5780 remote_async_ops.to_insert_breakpoint = remote_insert_breakpoint;
5781 remote_async_ops.to_remove_breakpoint = remote_remove_breakpoint;
5782 remote_async_ops.to_terminal_inferior = remote_async_terminal_inferior;
5783 remote_async_ops.to_terminal_ours = remote_async_terminal_ours;
5784 remote_async_ops.to_kill = remote_async_kill;
5785 remote_async_ops.to_load = generic_load;
5786 remote_async_ops.to_mourn_inferior = remote_async_mourn;
5787 remote_async_ops.to_thread_alive = remote_thread_alive;
5788 remote_async_ops.to_find_new_threads = remote_threads_info;
5789 remote_async_ops.to_pid_to_str = remote_pid_to_str;
5790 remote_async_ops.to_extra_thread_info = remote_threads_extra_info;
5791 remote_async_ops.to_stop = remote_stop;
5792 remote_async_ops.to_query = remote_query;
5793 remote_async_ops.to_rcmd = remote_rcmd;
5794 remote_async_ops.to_stratum = process_stratum;
5795 remote_async_ops.to_has_all_memory = 1;
5796 remote_async_ops.to_has_memory = 1;
5797 remote_async_ops.to_has_stack = 1;
5798 remote_async_ops.to_has_registers = 1;
5799 remote_async_ops.to_has_execution = 1;
5800 remote_async_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */
5801 remote_async_ops.to_can_async_p = remote_can_async_p;
5802 remote_async_ops.to_is_async_p = remote_is_async_p;
5803 remote_async_ops.to_async = remote_async;
5804 remote_async_ops.to_async_mask_value = 1;
5805 remote_async_ops.to_magic = OPS_MAGIC;
5806 }
5807
5808 /* Set up the async extended remote vector by making a copy of the standard
5809 remote vector and adding to it. */
5810
5811 static void
5812 init_extended_async_remote_ops (void)
5813 {
5814 extended_async_remote_ops = remote_async_ops;
5815
5816 extended_async_remote_ops.to_shortname = "extended-async";
5817 extended_async_remote_ops.to_longname =
5818 "Extended remote serial target in async gdb-specific protocol";
5819 extended_async_remote_ops.to_doc =
5820 "Use a remote computer via a serial line, using an async gdb-specific protocol.\n\
5821 Specify the serial device it is connected to (e.g. /dev/ttya).",
5822 extended_async_remote_ops.to_open = extended_remote_async_open;
5823 extended_async_remote_ops.to_create_inferior = extended_remote_async_create_inferior;
5824 extended_async_remote_ops.to_mourn_inferior = extended_remote_mourn;
5825 }
5826
5827 static void
5828 set_remote_cmd (char *args, int from_tty)
5829 {
5830 }
5831
5832 static void
5833 show_remote_cmd (char *args, int from_tty)
5834 {
5835
5836 show_remote_protocol_Z_packet_cmd (args, from_tty);
5837 show_remote_protocol_e_packet_cmd (args, from_tty);
5838 show_remote_protocol_E_packet_cmd (args, from_tty);
5839 show_remote_protocol_P_packet_cmd (args, from_tty);
5840 show_remote_protocol_qSymbol_packet_cmd (args, from_tty);
5841 show_remote_protocol_binary_download_cmd (args, from_tty);
5842 }
5843
5844 static void
5845 build_remote_gdbarch_data (void)
5846 {
5847 remote_address_size = TARGET_ADDR_BIT;
5848 }
5849
5850 /* Saved pointer to previous owner of the new_objfile event. */
5851 static void (*remote_new_objfile_chain) (struct objfile *);
5852
5853 /* Function to be called whenever a new objfile (shlib) is detected. */
5854 static void
5855 remote_new_objfile (struct objfile *objfile)
5856 {
5857 if (remote_desc != 0) /* Have a remote connection */
5858 {
5859 remote_check_symbols (objfile);
5860 }
5861 /* Call predecessor on chain, if any. */
5862 if (remote_new_objfile_chain != 0 &&
5863 remote_desc == 0)
5864 remote_new_objfile_chain (objfile);
5865 }
5866
5867 void
5868 _initialize_remote (void)
5869 {
5870 static struct cmd_list_element *remote_set_cmdlist;
5871 static struct cmd_list_element *remote_show_cmdlist;
5872 struct cmd_list_element *tmpcmd;
5873
5874 /* architecture specific data */
5875 remote_gdbarch_data_handle = register_gdbarch_data (init_remote_state,
5876 free_remote_state);
5877
5878 /* Old tacky stuff. NOTE: This comes after the remote protocol so
5879 that the remote protocol has been initialized. */
5880 register_gdbarch_swap (&tty_input, sizeof (&tty_input), NULL);
5881 register_gdbarch_swap (&remote_address_size,
5882 sizeof (&remote_address_size), NULL);
5883 register_gdbarch_swap (NULL, 0, build_remote_gdbarch_data);
5884
5885 init_remote_ops ();
5886 add_target (&remote_ops);
5887
5888 init_extended_remote_ops ();
5889 add_target (&extended_remote_ops);
5890
5891 init_remote_async_ops ();
5892 add_target (&remote_async_ops);
5893
5894 init_extended_async_remote_ops ();
5895 add_target (&extended_async_remote_ops);
5896
5897 init_remote_cisco_ops ();
5898 add_target (&remote_cisco_ops);
5899
5900 /* Hook into new objfile notification. */
5901 remote_new_objfile_chain = target_new_objfile_hook;
5902 target_new_objfile_hook = remote_new_objfile;
5903
5904 #if 0
5905 init_remote_threadtests ();
5906 #endif
5907
5908 /* set/show remote ... */
5909
5910 add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, "\
5911 Remote protocol specific variables\n\
5912 Configure various remote-protocol specific variables such as\n\
5913 the packets being used",
5914 &remote_set_cmdlist, "set remote ",
5915 0/*allow-unknown*/, &setlist);
5916 add_prefix_cmd ("remote", class_maintenance, show_remote_cmd, "\
5917 Remote protocol specific variables\n\
5918 Configure various remote-protocol specific variables such as\n\
5919 the packets being used",
5920 &remote_show_cmdlist, "show remote ",
5921 0/*allow-unknown*/, &showlist);
5922
5923 add_cmd ("compare-sections", class_obscure, compare_sections_command,
5924 "Compare section data on target to the exec file.\n\
5925 Argument is a single section name (default: all loaded sections).",
5926 &cmdlist);
5927
5928 add_cmd ("packet", class_maintenance, packet_command,
5929 "Send an arbitrary packet to a remote target.\n\
5930 maintenance packet TEXT\n\
5931 If GDB is talking to an inferior via the GDB serial protocol, then\n\
5932 this command sends the string TEXT to the inferior, and displays the\n\
5933 response packet. GDB supplies the initial `$' character, and the\n\
5934 terminating `#' character and checksum.",
5935 &maintenancelist);
5936
5937 add_show_from_set
5938 (add_set_boolean_cmd ("remotebreak", no_class, &remote_break,
5939 "Set whether to send break if interrupted.\n",
5940 &setlist),
5941 &showlist);
5942
5943 /* Install commands for configuring memory read/write packets. */
5944
5945 add_cmd ("remotewritesize", no_class, set_memory_write_packet_size,
5946 "Set the maximum number of bytes per memory write packet (deprecated).\n",
5947 &setlist);
5948 add_cmd ("remotewritesize", no_class, show_memory_write_packet_size,
5949 "Show the maximum number of bytes per memory write packet (deprecated).\n",
5950 &showlist);
5951 add_cmd ("memory-write-packet-size", no_class,
5952 set_memory_write_packet_size,
5953 "Set the maximum number of bytes per memory-write packet.\n"
5954 "Specify the number of bytes in a packet or 0 (zero) for the\n"
5955 "default packet size. The actual limit is further reduced\n"
5956 "dependent on the target. Specify ``fixed'' to disable the\n"
5957 "further restriction and ``limit'' to enable that restriction\n",
5958 &remote_set_cmdlist);
5959 add_cmd ("memory-read-packet-size", no_class,
5960 set_memory_read_packet_size,
5961 "Set the maximum number of bytes per memory-read packet.\n"
5962 "Specify the number of bytes in a packet or 0 (zero) for the\n"
5963 "default packet size. The actual limit is further reduced\n"
5964 "dependent on the target. Specify ``fixed'' to disable the\n"
5965 "further restriction and ``limit'' to enable that restriction\n",
5966 &remote_set_cmdlist);
5967 add_cmd ("memory-write-packet-size", no_class,
5968 show_memory_write_packet_size,
5969 "Show the maximum number of bytes per memory-write packet.\n",
5970 &remote_show_cmdlist);
5971 add_cmd ("memory-read-packet-size", no_class,
5972 show_memory_read_packet_size,
5973 "Show the maximum number of bytes per memory-read packet.\n",
5974 &remote_show_cmdlist);
5975
5976 add_show_from_set
5977 (add_set_cmd ("remoteaddresssize", class_obscure,
5978 var_integer, (char *) &remote_address_size,
5979 "Set the maximum size of the address (in bits) \
5980 in a memory packet.\n",
5981 &setlist),
5982 &showlist);
5983
5984 add_packet_config_cmd (&remote_protocol_binary_download,
5985 "X", "binary-download",
5986 set_remote_protocol_binary_download_cmd,
5987 show_remote_protocol_binary_download_cmd,
5988 &remote_set_cmdlist, &remote_show_cmdlist,
5989 1);
5990 #if 0
5991 /* XXXX - should ``set remotebinarydownload'' be retained for
5992 compatibility. */
5993 add_show_from_set
5994 (add_set_cmd ("remotebinarydownload", no_class,
5995 var_boolean, (char *) &remote_binary_download,
5996 "Set binary downloads.\n", &setlist),
5997 &showlist);
5998 #endif
5999
6000 add_info ("remote-process", remote_info_process,
6001 "Query the remote system for process info.");
6002
6003 add_packet_config_cmd (&remote_protocol_qSymbol,
6004 "qSymbol", "symbol-lookup",
6005 set_remote_protocol_qSymbol_packet_cmd,
6006 show_remote_protocol_qSymbol_packet_cmd,
6007 &remote_set_cmdlist, &remote_show_cmdlist,
6008 0);
6009
6010 add_packet_config_cmd (&remote_protocol_e,
6011 "e", "step-over-range",
6012 set_remote_protocol_e_packet_cmd,
6013 show_remote_protocol_e_packet_cmd,
6014 &remote_set_cmdlist, &remote_show_cmdlist,
6015 0);
6016
6017 add_packet_config_cmd (&remote_protocol_E,
6018 "E", "step-over-range-w-signal",
6019 set_remote_protocol_E_packet_cmd,
6020 show_remote_protocol_E_packet_cmd,
6021 &remote_set_cmdlist, &remote_show_cmdlist,
6022 0);
6023
6024 add_packet_config_cmd (&remote_protocol_P,
6025 "P", "set-register",
6026 set_remote_protocol_P_packet_cmd,
6027 show_remote_protocol_P_packet_cmd,
6028 &remote_set_cmdlist, &remote_show_cmdlist,
6029 1);
6030
6031 add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_SOFTWARE_BP],
6032 "Z0", "software-breakpoint",
6033 set_remote_protocol_Z_software_bp_packet_cmd,
6034 show_remote_protocol_Z_software_bp_packet_cmd,
6035 &remote_set_cmdlist, &remote_show_cmdlist,
6036 0);
6037
6038 add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_HARDWARE_BP],
6039 "Z1", "hardware-breakpoint",
6040 set_remote_protocol_Z_hardware_bp_packet_cmd,
6041 show_remote_protocol_Z_hardware_bp_packet_cmd,
6042 &remote_set_cmdlist, &remote_show_cmdlist,
6043 0);
6044
6045 add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_WRITE_WP],
6046 "Z2", "write-watchpoint",
6047 set_remote_protocol_Z_write_wp_packet_cmd,
6048 show_remote_protocol_Z_write_wp_packet_cmd,
6049 &remote_set_cmdlist, &remote_show_cmdlist,
6050 0);
6051
6052 add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_READ_WP],
6053 "Z3", "read-watchpoint",
6054 set_remote_protocol_Z_read_wp_packet_cmd,
6055 show_remote_protocol_Z_read_wp_packet_cmd,
6056 &remote_set_cmdlist, &remote_show_cmdlist,
6057 0);
6058
6059 add_packet_config_cmd (&remote_protocol_Z[Z_PACKET_ACCESS_WP],
6060 "Z4", "access-watchpoint",
6061 set_remote_protocol_Z_access_wp_packet_cmd,
6062 show_remote_protocol_Z_access_wp_packet_cmd,
6063 &remote_set_cmdlist, &remote_show_cmdlist,
6064 0);
6065
6066 /* Keep the old ``set remote Z-packet ...'' working. */
6067 tmpcmd = add_set_auto_boolean_cmd ("Z-packet", class_obscure,
6068 &remote_Z_packet_detect,
6069 "\
6070 Set use of remote protocol `Z' packets", &remote_set_cmdlist);
6071 tmpcmd->function.sfunc = set_remote_protocol_Z_packet_cmd;
6072 add_cmd ("Z-packet", class_obscure, show_remote_protocol_Z_packet_cmd,
6073 "Show use of remote protocol `Z' packets ",
6074 &remote_show_cmdlist);
6075 }
This page took 0.233522 seconds and 4 git commands to generate.