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