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