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