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