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.
5 This file is part of GDB.
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.
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.
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. */
22 /* See the GDB User Guide for details of the GDB remote protocol. */
25 #include "gdb_string.h"
34 /*#include "terminal.h" */
37 #include "gdb-stabs.h"
38 #include "gdbthread.h"
46 #include <sys/types.h>
49 #include "event-loop.h"
50 #include "event-top.h"
55 /* Prototypes for local functions */
56 static void initialize_sigint_signal_handler
PARAMS ((void));
57 static void handle_remote_sigint
PARAMS ((int));
58 static void handle_remote_sigint_twice
PARAMS ((int));
59 static void async_remote_interrupt
PARAMS ((gdb_client_data
));
60 static void async_remote_interrupt_twice
PARAMS ((gdb_client_data
));
62 static void set_extended_protocol
PARAMS ((struct continuation_arg
*));
64 static void build_remote_gdbarch_data
PARAMS ((void));
66 static int remote_write_bytes
PARAMS ((CORE_ADDR memaddr
,
67 char *myaddr
, int len
));
69 static int remote_read_bytes
PARAMS ((CORE_ADDR memaddr
,
70 char *myaddr
, int len
));
72 static void remote_files_info
PARAMS ((struct target_ops
* ignore
));
74 static int remote_xfer_memory
PARAMS ((CORE_ADDR memaddr
, char *myaddr
,
75 int len
, int should_write
,
76 struct target_ops
* target
));
78 static void remote_prepare_to_store
PARAMS ((void));
80 static void remote_fetch_registers
PARAMS ((int regno
));
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
));
87 static int remote_start_remote
PARAMS ((PTR
));
89 static void remote_open
PARAMS ((char *name
, int from_tty
));
90 static void remote_async_open
PARAMS ((char *name
, int from_tty
));
92 static void extended_remote_open
PARAMS ((char *name
, int from_tty
));
93 static void extended_remote_async_open
PARAMS ((char *name
, int from_tty
));
95 static void remote_open_1
PARAMS ((char *, int, struct target_ops
*,
97 static void remote_async_open_1
PARAMS ((char *, int, struct target_ops
*,
100 static void remote_close
PARAMS ((int quitting
));
102 static void remote_store_registers
PARAMS ((int regno
));
104 static void remote_mourn
PARAMS ((void));
105 static void remote_async_mourn
PARAMS ((void));
107 static void extended_remote_restart
PARAMS ((void));
109 static void extended_remote_mourn
PARAMS ((void));
111 static void extended_remote_create_inferior
PARAMS ((char *, char *, char **));
112 static void extended_remote_async_create_inferior
PARAMS ((char *, char *, char **));
114 static void remote_mourn_1
PARAMS ((struct target_ops
*));
116 static void remote_send
PARAMS ((char *buf
));
118 static int readchar
PARAMS ((int timeout
));
120 static int remote_wait
PARAMS ((int pid
, struct target_waitstatus
* status
));
121 static int remote_async_wait
PARAMS ((int pid
, struct target_waitstatus
* status
));
123 static void remote_kill
PARAMS ((void));
124 static void remote_async_kill
PARAMS ((void));
126 static int tohex
PARAMS ((int nib
));
128 static void remote_detach
PARAMS ((char *args
, int from_tty
));
129 static void remote_async_detach
PARAMS ((char *args
, int from_tty
));
131 static void remote_interrupt
PARAMS ((int signo
));
133 static void remote_interrupt_twice
PARAMS ((int signo
));
135 static void interrupt_query
PARAMS ((void));
137 static void set_thread
PARAMS ((int, int));
139 static int remote_thread_alive
PARAMS ((int));
141 static void get_offsets
PARAMS ((void));
143 static int read_frame
PARAMS ((char *));
145 static int remote_insert_breakpoint
PARAMS ((CORE_ADDR
, char *));
147 static int remote_remove_breakpoint
PARAMS ((CORE_ADDR
, char *));
149 static int hexnumlen
PARAMS ((ULONGEST num
));
151 static void init_remote_ops
PARAMS ((void));
153 static void init_extended_remote_ops
PARAMS ((void));
155 static void init_remote_cisco_ops
PARAMS ((void));
157 static struct target_ops remote_cisco_ops
;
159 static void remote_stop
PARAMS ((void));
161 static int ishex
PARAMS ((int ch
, int *val
));
163 static int stubhex
PARAMS ((int ch
));
165 static int remote_query
PARAMS ((int /*char */ , char *, char *, int *));
167 static int hexnumstr
PARAMS ((char *, ULONGEST
));
169 static CORE_ADDR remote_address_masked
PARAMS ((CORE_ADDR
));
171 static void print_packet
PARAMS ((char *));
173 static unsigned long crc32
PARAMS ((unsigned char *, int, unsigned int));
175 static void compare_sections_command
PARAMS ((char *, int));
177 static void packet_command
PARAMS ((char *, int));
179 static int stub_unpack_int
PARAMS ((char *buff
, int fieldlength
));
181 static int remote_current_thread
PARAMS ((int oldpid
));
183 static void remote_find_new_threads
PARAMS ((void));
185 static void record_currthread
PARAMS ((int currthread
));
187 /* exported functions */
189 extern int fromhex
PARAMS ((int a
));
191 static int putpkt_binary
PARAMS ((char *buf
, int cnt
));
193 static void check_binary_download
PARAMS ((CORE_ADDR addr
));
195 struct packet_config
;
197 static void show_packet_config_cmd
PARAMS ((struct packet_config
* config
));
199 static void set_packet_config_cmd
PARAMS ((struct packet_config
* config
,
200 struct cmd_list_element
* c
));
202 static void add_packet_config_cmd
PARAMS ((struct packet_config
* config
,
205 void (*set_func
) (char *args
, int from_tty
, struct cmd_list_element
* c
),
206 void (*show_func
) (char *name
, int from_tty
),
207 struct cmd_list_element
**setlist
,
208 struct cmd_list_element
**showlist
));
210 static void init_packet_config
PARAMS ((struct packet_config
* config
));
212 static void set_remote_protocol_P_packet_cmd
PARAMS ((char *args
,
214 struct cmd_list_element
* c
));
216 static void show_remote_protocol_P_packet_cmd
PARAMS ((char *args
,
219 static void set_remote_protocol_Z_packet_cmd
PARAMS ((char *args
,
221 struct cmd_list_element
* c
));
223 static void show_remote_protocol_Z_packet_cmd
PARAMS ((char *args
,
229 /* Define the target subroutine names */
231 void open_remote_target
PARAMS ((char *, int, struct target_ops
*, int));
233 void _initialize_remote
PARAMS ((void));
237 static struct target_ops remote_ops
;
239 static struct target_ops extended_remote_ops
;
241 /* Temporary target ops. Just like the remote_ops and
242 extended_remote_ops, but with asynchronous support. */
243 static struct target_ops remote_async_ops
;
245 static struct target_ops extended_async_remote_ops
;
247 /* This was 5 seconds, which is a long time to sit and wait.
248 Unless this is going though some terminal server or multiplexer or
249 other form of hairy serial connection, I would think 2 seconds would
252 /* Changed to allow option to set timeout value.
253 was static int remote_timeout = 2; */
254 extern int remote_timeout
;
256 /* This variable chooses whether to send a ^C or a break when the user
257 requests program interruption. Although ^C is usually what remote
258 systems expect, and that is the default here, sometimes a break is
259 preferable instead. */
261 static int remote_break
;
263 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
264 remote_open knows that we don't have a file open when the program
266 static serial_t remote_desc
= NULL
;
268 /* This is set by the target (thru the 'S' message)
269 to denote that the target is in kernel mode. */
270 static int cisco_kernel_mode
= 0;
272 /* Maximum number of bytes to read/write at once. The value here
273 is chosen to fill up a packet (the headers account for the 32). */
274 #define MAXBUFBYTES(N) (((N)-32)/2)
276 /* Having this larger than 400 causes us to be incompatible with m68k-stub.c
277 and i386-stub.c. Normally, no one would notice because it only matters
278 for writing large chunks of memory (e.g. in downloads). Also, this needs
279 to be more than 400 if required to hold the registers (see below, where
280 we round it up based on REGISTER_BYTES). */
281 /* Round up PBUFSIZ to hold all the registers, at least. */
282 #define PBUFSIZ ((REGISTER_BYTES > MAXBUFBYTES (400)) \
283 ? (REGISTER_BYTES * 2 + 32) \
287 /* This variable sets the number of bytes to be written to the target
288 in a single packet. Normally PBUFSIZ is satisfactory, but some
289 targets need smaller values (perhaps because the receiving end
292 static int remote_write_size
;
294 /* This variable sets the number of bits in an address that are to be
295 sent in a memory ("M" or "m") packet. Normally, after stripping
296 leading zeros, the entire address would be sent. This variable
297 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
298 initial implementation of remote.c restricted the address sent in
299 memory packets to ``host::sizeof long'' bytes - (typically 32
300 bits). Consequently, for 64 bit targets, the upper 32 bits of an
301 address was never sent. Since fixing this bug may cause a break in
302 some remote targets this variable is principly provided to
303 facilitate backward compatibility. */
305 static int remote_address_size
;
307 /* This is the size (in chars) of the first response to the `g' command. This
308 is used to limit the size of the memory read and write commands to prevent
309 stub buffers from overflowing. The size does not include headers and
310 trailers, it is only the payload size. */
312 static int remote_register_buf_size
= 0;
314 /* Generic configuration support for packets the stub optionally
315 supports. Allows the user to specify the use of the packet as well
316 as allowing GDB to auto-detect support in the remote stub. */
320 PACKET_SUPPORT_UNKNOWN
= 0,
327 PACKET_AUTO_DETECT
= 0,
336 enum packet_detect detect
;
337 enum packet_support support
;
340 static char packet_support_auto
[] = "auto";
341 static char packet_enable
[] = "enable";
342 static char packet_disable
[] = "disable";
343 static char *packet_support_enums
[] =
352 set_packet_config_cmd (config
, c
)
353 struct packet_config
*config
;
354 struct cmd_list_element
*c
;
356 if (config
->state
== packet_enable
)
358 config
->detect
= PACKET_MANUAL_DETECT
;
359 config
->support
= PACKET_ENABLE
;
361 else if (config
->state
== packet_disable
)
363 config
->detect
= PACKET_MANUAL_DETECT
;
364 config
->support
= PACKET_DISABLE
;
366 else if (config
->state
== packet_support_auto
)
368 config
->detect
= PACKET_AUTO_DETECT
;
369 config
->support
= PACKET_SUPPORT_UNKNOWN
;
372 internal_error ("Bad enum value");
376 show_packet_config_cmd (config
)
377 struct packet_config
*config
;
379 char *support
= "internal-error";
380 switch (config
->support
)
386 support
= "disabled";
388 case PACKET_SUPPORT_UNKNOWN
:
392 switch (config
->detect
)
394 case PACKET_AUTO_DETECT
:
395 printf_filtered ("Support for remote protocol `%s' (%s) packet is auto-detected, currently %s.\n",
396 config
->name
, config
->title
, support
);
398 case PACKET_MANUAL_DETECT
:
399 printf_filtered ("Support for remote protocol `%s' (%s) is currently %s.\n",
400 config
->name
, config
->title
, support
);
405 add_packet_config_cmd (config
, name
, title
, set_func
, show_func
,
407 struct packet_config
*config
;
410 void (*set_func
) PARAMS ((char *args
, int from_tty
,
411 struct cmd_list_element
* c
));
412 void (*show_func
) PARAMS ((char *name
, int from_tty
));
413 struct cmd_list_element
**setlist
;
414 struct cmd_list_element
**showlist
;
416 struct cmd_list_element
*c
;
421 config
->title
= title
;
422 asprintf (&set_doc
, "Set use of remote protocol `%s' (%s) packet",
424 asprintf (&show_doc
, "Show current use of remote protocol `%s' (%s) packet",
426 asprintf (&full_name
, "%s-packet", name
);
427 c
= add_set_enum_cmd (full_name
,
428 class_obscure
, packet_support_enums
,
429 (char *) &config
->state
,
431 c
->function
.sfunc
= set_func
;
432 add_cmd (full_name
, class_obscure
, show_func
, show_doc
, showlist
);
436 init_packet_config (config
)
437 struct packet_config
*config
;
439 switch (config
->detect
)
441 case PACKET_AUTO_DETECT
:
442 config
->support
= PACKET_SUPPORT_UNKNOWN
;
444 case PACKET_MANUAL_DETECT
:
445 /* let the user beware */
450 /* Should we try the 'P' (set register) request? */
452 static struct packet_config remote_protocol_P
;
455 set_remote_protocol_P_packet_cmd (args
, from_tty
, c
)
458 struct cmd_list_element
*c
;
460 set_packet_config_cmd (&remote_protocol_P
, c
);
464 show_remote_protocol_P_packet_cmd (args
, from_tty
)
468 show_packet_config_cmd (&remote_protocol_P
);
471 /* Should we try the 'Z' (set breakpoint) request? */
473 static struct packet_config remote_protocol_Z
;
476 set_remote_protocol_Z_packet_cmd (args
, from_tty
, c
)
479 struct cmd_list_element
*c
;
481 set_packet_config_cmd (&remote_protocol_Z
, c
);
485 show_remote_protocol_Z_packet_cmd (args
, from_tty
)
489 show_packet_config_cmd (&remote_protocol_Z
);
492 /* Should we try the 'X' (remote binary download) packet?
494 This variable (available to the user via "set remote X-packet")
495 dictates whether downloads are sent in binary (via the 'X' packet).
496 We assume that the stub can, and attempt to do it. This will be
497 cleared if the stub does not understand it. This switch is still
498 needed, though in cases when the packet is supported in the stub,
499 but the connection does not allow it (i.e., 7-bit serial connection
502 static struct packet_config remote_protocol_binary_download
;
505 set_remote_protocol_binary_download_cmd (char *args
,
507 struct cmd_list_element
*c
)
509 set_packet_config_cmd (&remote_protocol_binary_download
, c
);
513 show_remote_protocol_binary_download_cmd (char *args
,
516 show_packet_config_cmd (&remote_protocol_binary_download
);
520 /* Tokens for use by the asynchronous signal handlers for SIGINT */
521 PTR sigint_remote_twice_token
;
522 PTR sigint_remote_token
;
524 /* These are pointers to hook functions that may be set in order to
525 modify resume/wait behavior for a particular architecture. */
527 void (*target_resume_hook
) PARAMS ((void));
528 void (*target_wait_loop_hook
) PARAMS ((void));
532 /* These are the threads which we last sent to the remote system.
533 -1 for all or -2 for not sent yet. */
534 static int general_thread
;
535 static int continue_thread
;
537 /* Call this function as a result of
538 1) A halt indication (T packet) containing a thread id
539 2) A direct query of currthread
540 3) Successful execution of set thread
544 record_currthread (currthread
)
547 general_thread
= currthread
;
549 /* If this is a new thread, add it to GDB's thread list.
550 If we leave it up to WFI to do this, bad things will happen. */
551 if (!in_thread_list (currthread
))
553 add_thread (currthread
);
554 printf_filtered ("[New %s]\n", target_pid_to_str (currthread
));
558 #define MAGIC_NULL_PID 42000
565 char *buf
= alloca (PBUFSIZ
);
566 int state
= gen
? general_thread
: continue_thread
;
572 buf
[1] = gen
? 'g' : 'c';
573 if (th
== MAGIC_NULL_PID
)
579 sprintf (&buf
[2], "-%x", -th
);
581 sprintf (&buf
[2], "%x", th
);
587 continue_thread
= th
;
590 /* Return nonzero if the thread TH is still alive on the remote system. */
593 remote_thread_alive (tid
)
599 sprintf (buf
, "T-%08x", -tid
);
601 sprintf (buf
, "T%08x", tid
);
604 return (buf
[0] == 'O' && buf
[1] == 'K');
607 /* About these extended threadlist and threadinfo packets. They are
608 variable length packets but, the fields within them are often fixed
609 length. They are redundent enough to send over UDP as is the
610 remote protocol in general. There is a matching unit test module
613 #define OPAQUETHREADBYTES 8
615 /* a 64 bit opaque identifier */
616 typedef unsigned char threadref
[OPAQUETHREADBYTES
];
618 /* WARNING: This threadref data structure comes from the remote O.S., libstub
619 protocol encoding, and remote.c. it is not particularly changable */
621 /* Right now, the internal structure is int. We want it to be bigger.
625 typedef int gdb_threadref
; /* internal GDB thread reference */
627 /* gdb_ext_thread_info is an internal GDB data structure which is
628 equivalint to the reply of the remote threadinfo packet */
630 struct gdb_ext_thread_info
632 threadref threadid
; /* External form of thread reference */
633 int active
; /* Has state interesting to GDB? , regs, stack */
634 char display
[256]; /* Brief state display, name, blocked/syspended */
635 char shortname
[32]; /* To be used to name threads */
636 char more_display
[256]; /* Long info, statistics, queue depth, whatever */
639 /* The volume of remote transfers can be limited by submitting
640 a mask containing bits specifying the desired information.
641 Use a union of these values as the 'selection' parameter to
642 get_thread_info. FIXME: Make these TAG names more thread specific.
645 #define TAG_THREADID 1
647 #define TAG_DISPLAY 4
648 #define TAG_THREADNAME 8
649 #define TAG_MOREDISPLAY 16
651 #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES*2)
653 char *unpack_varlen_hex
PARAMS ((char *buff
, int *result
));
655 static char *unpack_nibble
PARAMS ((char *buf
, int *val
));
657 static char *pack_nibble
PARAMS ((char *buf
, int nibble
));
659 static char *pack_hex_byte
PARAMS ((char *pkt
, int /*unsigned char */ byte
));
661 static char *unpack_byte
PARAMS ((char *buf
, int *value
));
663 static char *pack_int
PARAMS ((char *buf
, int value
));
665 static char *unpack_int
PARAMS ((char *buf
, int *value
));
667 static char *unpack_string
PARAMS ((char *src
, char *dest
, int length
));
669 static char *pack_threadid
PARAMS ((char *pkt
, threadref
* id
));
671 static char *unpack_threadid
PARAMS ((char *inbuf
, threadref
* id
));
673 void int_to_threadref
PARAMS ((threadref
* id
, int value
));
675 static int threadref_to_int
PARAMS ((threadref
* ref
));
677 static void copy_threadref
PARAMS ((threadref
* dest
, threadref
* src
));
679 static int threadmatch
PARAMS ((threadref
* dest
, threadref
* src
));
681 static char *pack_threadinfo_request
PARAMS ((char *pkt
, int mode
,
684 static int remote_unpack_thread_info_response
PARAMS ((char *pkt
,
685 threadref
* expectedref
,
686 struct gdb_ext_thread_info
* info
));
689 static int remote_get_threadinfo
PARAMS ((threadref
* threadid
,
690 int fieldset
, /*TAG mask */
691 struct gdb_ext_thread_info
* info
));
693 static int adapt_remote_get_threadinfo
PARAMS ((gdb_threadref
* ref
,
695 struct gdb_ext_thread_info
* info
));
697 static char *pack_threadlist_request
PARAMS ((char *pkt
, int startflag
,
699 threadref
* nextthread
));
701 static int parse_threadlist_response
PARAMS ((char *pkt
,
703 threadref
* original_echo
,
704 threadref
* resultlist
,
707 static int remote_get_threadlist
PARAMS ((int startflag
,
708 threadref
* nextthread
,
712 threadref
* threadlist
));
714 typedef int (*rmt_thread_action
) (threadref
* ref
, void *context
);
716 static int remote_threadlist_iterator
PARAMS ((rmt_thread_action stepfunction
,
717 void *context
, int looplimit
));
719 static int remote_newthread_step
PARAMS ((threadref
* ref
, void *context
));
721 /* encode 64 bits in 16 chars of hex */
723 static const char hexchars
[] = "0123456789abcdef";
730 if ((ch
>= 'a') && (ch
<= 'f'))
732 *val
= ch
- 'a' + 10;
735 if ((ch
>= 'A') && (ch
<= 'F'))
737 *val
= ch
- 'A' + 10;
740 if ((ch
>= '0') && (ch
<= '9'))
752 if (ch
>= 'a' && ch
<= 'f')
753 return ch
- 'a' + 10;
754 if (ch
>= '0' && ch
<= '9')
756 if (ch
>= 'A' && ch
<= 'F')
757 return ch
- 'A' + 10;
762 stub_unpack_int (buff
, fieldlength
)
771 nibble
= stubhex (*buff
++);
775 retval
= retval
<< 4;
781 unpack_varlen_hex (buff
, result
)
782 char *buff
; /* packet to parse */
788 while (ishex (*buff
, &nibble
))
791 retval
= retval
<< 4;
792 retval
|= nibble
& 0x0f;
799 unpack_nibble (buf
, val
)
808 pack_nibble (buf
, nibble
)
812 *buf
++ = hexchars
[(nibble
& 0x0f)];
817 pack_hex_byte (pkt
, byte
)
821 *pkt
++ = hexchars
[(byte
>> 4) & 0xf];
822 *pkt
++ = hexchars
[(byte
& 0xf)];
827 unpack_byte (buf
, value
)
831 *value
= stub_unpack_int (buf
, 2);
836 pack_int (buf
, value
)
840 buf
= pack_hex_byte (buf
, (value
>> 24) & 0xff);
841 buf
= pack_hex_byte (buf
, (value
>> 16) & 0xff);
842 buf
= pack_hex_byte (buf
, (value
>> 8) & 0x0ff);
843 buf
= pack_hex_byte (buf
, (value
& 0xff));
848 unpack_int (buf
, value
)
852 *value
= stub_unpack_int (buf
, 8);
856 #if 0 /* currently unused, uncomment when needed */
857 static char *pack_string
PARAMS ((char *pkt
, char *string
));
860 pack_string (pkt
, string
)
867 len
= strlen (string
);
869 len
= 200; /* Bigger than most GDB packets, junk??? */
870 pkt
= pack_hex_byte (pkt
, len
);
874 if ((ch
== '\0') || (ch
== '#'))
875 ch
= '*'; /* Protect encapsulation */
880 #endif /* 0 (unused) */
883 unpack_string (src
, dest
, length
)
895 pack_threadid (pkt
, id
)
900 unsigned char *altid
;
902 altid
= (unsigned char *) id
;
903 limit
= pkt
+ BUF_THREAD_ID_SIZE
;
905 pkt
= pack_hex_byte (pkt
, *altid
++);
911 unpack_threadid (inbuf
, id
)
916 char *limit
= inbuf
+ BUF_THREAD_ID_SIZE
;
919 altref
= (char *) id
;
921 while (inbuf
< limit
)
923 x
= stubhex (*inbuf
++);
924 y
= stubhex (*inbuf
++);
925 *altref
++ = (x
<< 4) | y
;
930 /* Externally, threadrefs are 64 bits but internally, they are still
931 ints. This is due to a mismatch of specifications. We would like
932 to use 64bit thread references internally. This is an adapter
936 int_to_threadref (id
, value
)
942 scan
= (unsigned char *) id
;
948 *scan
++ = (value
>> 24) & 0xff;
949 *scan
++ = (value
>> 16) & 0xff;
950 *scan
++ = (value
>> 8) & 0xff;
951 *scan
++ = (value
& 0xff);
955 threadref_to_int (ref
)
965 value
= (value
<< 8) | ((*scan
++) & 0xff);
970 copy_threadref (dest
, src
)
975 unsigned char *csrc
, *cdest
;
977 csrc
= (unsigned char *) src
;
978 cdest
= (unsigned char *) dest
;
985 threadmatch (dest
, src
)
989 /* things are broken right now, so just assume we got a match */
991 unsigned char *srcp
, *destp
;
994 destp
= (char *) dest
;
998 result
&= (*srcp
++ == *destp
++) ? 1 : 0;
1005 threadid:1, # always request threadid
1012 /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
1015 pack_threadinfo_request (pkt
, mode
, id
)
1020 *pkt
++ = 'q'; /* Info Query */
1021 *pkt
++ = 'P'; /* process or thread info */
1022 pkt
= pack_int (pkt
, mode
); /* mode */
1023 pkt
= pack_threadid (pkt
, id
); /* threadid */
1024 *pkt
= '\0'; /* terminate */
1028 /* These values tag the fields in a thread info response packet */
1029 /* Tagging the fields allows us to request specific fields and to
1030 add more fields as time goes by */
1032 #define TAG_THREADID 1 /* Echo the thread identifier */
1033 #define TAG_EXISTS 2 /* Is this process defined enough to
1034 fetch registers and its stack */
1035 #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
1036 #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is */
1037 #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
1041 remote_unpack_thread_info_response (pkt
, expectedref
, info
)
1043 threadref
*expectedref
;
1044 struct gdb_ext_thread_info
*info
;
1049 char *limit
= pkt
+ PBUFSIZ
; /* plausable parsing limit */
1052 /* info->threadid = 0; FIXME: implement zero_threadref */
1054 info
->display
[0] = '\0';
1055 info
->shortname
[0] = '\0';
1056 info
->more_display
[0] = '\0';
1058 /* Assume the characters indicating the packet type have been stripped */
1059 pkt
= unpack_int (pkt
, &mask
); /* arg mask */
1060 pkt
= unpack_threadid (pkt
, &ref
);
1063 warning ("Incomplete response to threadinfo request\n");
1064 if (!threadmatch (&ref
, expectedref
))
1065 { /* This is an answer to a different request */
1066 warning ("ERROR RMT Thread info mismatch\n");
1069 copy_threadref (&info
->threadid
, &ref
);
1071 /* Loop on tagged fields , try to bail if somthing goes wrong */
1073 while ((pkt
< limit
) && mask
&& *pkt
) /* packets are terminated with nulls */
1075 pkt
= unpack_int (pkt
, &tag
); /* tag */
1076 pkt
= unpack_byte (pkt
, &length
); /* length */
1077 if (!(tag
& mask
)) /* tags out of synch with mask */
1079 warning ("ERROR RMT: threadinfo tag mismatch\n");
1083 if (tag
== TAG_THREADID
)
1087 warning ("ERROR RMT: length of threadid is not 16\n");
1091 pkt
= unpack_threadid (pkt
, &ref
);
1092 mask
= mask
& ~TAG_THREADID
;
1095 if (tag
== TAG_EXISTS
)
1097 info
->active
= stub_unpack_int (pkt
, length
);
1099 mask
= mask
& ~(TAG_EXISTS
);
1102 warning ("ERROR RMT: 'exists' length too long\n");
1108 if (tag
== TAG_THREADNAME
)
1110 pkt
= unpack_string (pkt
, &info
->shortname
[0], length
);
1111 mask
= mask
& ~TAG_THREADNAME
;
1114 if (tag
== TAG_DISPLAY
)
1116 pkt
= unpack_string (pkt
, &info
->display
[0], length
);
1117 mask
= mask
& ~TAG_DISPLAY
;
1120 if (tag
== TAG_MOREDISPLAY
)
1122 pkt
= unpack_string (pkt
, &info
->more_display
[0], length
);
1123 mask
= mask
& ~TAG_MOREDISPLAY
;
1126 warning ("ERROR RMT: unknown thread info tag\n");
1127 break; /* Not a tag we know about */
1133 remote_get_threadinfo (threadid
, fieldset
, info
)
1134 threadref
*threadid
;
1135 int fieldset
; /* TAG mask */
1136 struct gdb_ext_thread_info
*info
;
1139 char *threadinfo_pkt
= alloca (PBUFSIZ
);
1141 pack_threadinfo_request (threadinfo_pkt
, fieldset
, threadid
);
1142 putpkt (threadinfo_pkt
);
1143 getpkt (threadinfo_pkt
, 0);
1144 result
= remote_unpack_thread_info_response (threadinfo_pkt
+ 2, threadid
,
1149 /* Unfortunately, 61 bit thread-ids are bigger than the internal
1150 representation of a threadid. */
1153 adapt_remote_get_threadinfo (ref
, selection
, info
)
1156 struct gdb_ext_thread_info
*info
;
1160 int_to_threadref (&lclref
, *ref
);
1161 return remote_get_threadinfo (&lclref
, selection
, info
);
1164 /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
1167 pack_threadlist_request (pkt
, startflag
, threadcount
, nextthread
)
1171 threadref
*nextthread
;
1173 *pkt
++ = 'q'; /* info query packet */
1174 *pkt
++ = 'L'; /* Process LIST or threadLIST request */
1175 pkt
= pack_nibble (pkt
, startflag
); /* initflag 1 bytes */
1176 pkt
= pack_hex_byte (pkt
, threadcount
); /* threadcount 2 bytes */
1177 pkt
= pack_threadid (pkt
, nextthread
); /* 64 bit thread identifier */
1182 /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
1185 parse_threadlist_response (pkt
, result_limit
, original_echo
, resultlist
,
1189 threadref
*original_echo
;
1190 threadref
*resultlist
;
1194 int count
, resultcount
, done
;
1197 /* Assume the 'q' and 'M chars have been stripped. */
1198 limit
= pkt
+ (PBUFSIZ
- BUF_THREAD_ID_SIZE
); /* done parse past here */
1199 pkt
= unpack_byte (pkt
, &count
); /* count field */
1200 pkt
= unpack_nibble (pkt
, &done
);
1201 /* The first threadid is the argument threadid. */
1202 pkt
= unpack_threadid (pkt
, original_echo
); /* should match query packet */
1203 while ((count
-- > 0) && (pkt
< limit
))
1205 pkt
= unpack_threadid (pkt
, resultlist
++);
1206 if (resultcount
++ >= result_limit
)
1215 remote_get_threadlist (startflag
, nextthread
, result_limit
,
1216 done
, result_count
, threadlist
)
1218 threadref
*nextthread
;
1222 threadref
*threadlist
;
1225 static threadref echo_nextthread
;
1226 char *threadlist_packet
= alloca (PBUFSIZ
);
1227 char *t_response
= alloca (PBUFSIZ
);
1230 /* Trancate result limit to be smaller than the packet size */
1231 if ((((result_limit
+ 1) * BUF_THREAD_ID_SIZE
) + 10) >= PBUFSIZ
)
1232 result_limit
= (PBUFSIZ
/ BUF_THREAD_ID_SIZE
) - 2;
1234 pack_threadlist_request (threadlist_packet
,
1235 startflag
, result_limit
, nextthread
);
1236 putpkt (threadlist_packet
);
1237 getpkt (t_response
, 0);
1240 parse_threadlist_response (t_response
+ 2, result_limit
, &echo_nextthread
,
1243 if (!threadmatch (&echo_nextthread
, nextthread
))
1245 /* FIXME: This is a good reason to drop the packet */
1246 /* Possably, there is a duplicate response */
1248 retransmit immediatly - race conditions
1249 retransmit after timeout - yes
1251 wait for packet, then exit
1253 warning ("HMM: threadlist did not echo arg thread, dropping it\n");
1254 return 0; /* I choose simply exiting */
1256 if (*result_count
<= 0)
1260 warning ("RMT ERROR : failed to get remote thread list\n");
1263 return result
; /* break; */
1265 if (*result_count
> result_limit
)
1268 warning ("RMT ERROR: threadlist response longer than requested\n");
1274 /* This is the interface between remote and threads, remotes upper interface */
1276 /* remote_find_new_threads retrieves the thread list and for each
1277 thread in the list, looks up the thread in GDB's internal list,
1278 ading the thread if it does not already exist. This involves
1279 getting partial thread lists from the remote target so, polling the
1280 quit_flag is required. */
1283 /* About this many threadisds fit in a packet. */
1285 #define MAXTHREADLISTRESULTS 32
1288 remote_threadlist_iterator (stepfunction
, context
, looplimit
)
1289 rmt_thread_action stepfunction
;
1293 int done
, i
, result_count
;
1297 static threadref nextthread
;
1298 static threadref resultthreadlist
[MAXTHREADLISTRESULTS
];
1303 if (loopcount
++ > looplimit
)
1306 warning ("Remote fetch threadlist -infinite loop-\n");
1309 if (!remote_get_threadlist (startflag
, &nextthread
, MAXTHREADLISTRESULTS
,
1310 &done
, &result_count
, resultthreadlist
))
1315 /* clear for later iterations */
1317 /* Setup to resume next batch of thread references, set nextthread. */
1318 if (result_count
>= 1)
1319 copy_threadref (&nextthread
, &resultthreadlist
[result_count
- 1]);
1321 while (result_count
--)
1322 if (!(result
= (*stepfunction
) (&resultthreadlist
[i
++], context
)))
1329 remote_newthread_step (ref
, context
)
1335 pid
= threadref_to_int (ref
);
1336 if (!in_thread_list (pid
))
1338 return 1; /* continue iterator */
1341 #define CRAZY_MAX_THREADS 1000
1344 remote_current_thread (oldpid
)
1347 char *buf
= alloca (PBUFSIZ
);
1351 if (buf
[0] == 'Q' && buf
[1] == 'C')
1352 return strtol (&buf
[2], NULL
, 16);
1357 /* Find new threads for info threads command. */
1360 remote_find_new_threads ()
1362 remote_threadlist_iterator (remote_newthread_step
, 0,
1364 if (inferior_pid
== MAGIC_NULL_PID
) /* ack ack ack */
1365 inferior_pid
= remote_current_thread (inferior_pid
);
1369 remote_threads_info (void)
1371 char *buf
= alloca (PBUFSIZ
);
1375 if (remote_desc
== 0) /* paranoia */
1376 error ("Command can only be used when connected to the remote target.");
1378 putpkt ("qfThreadInfo");
1379 getpkt (bufp
= buf
, 0);
1380 if (bufp
[0] == '\0') /* q packet not recognized! */
1381 { /* try old jmetzler method */
1382 remote_find_new_threads ();
1385 else /* try new 'q' method */
1386 while (*bufp
++ == 'm') /* reply contains one or more TID */
1390 tid
= strtol (bufp
, &bufp
, 16);
1391 if (tid
!= 0 && !in_thread_list (tid
))
1394 while (*bufp
++ == ','); /* comma-separated list */
1395 putpkt ("qsThreadInfo");
1396 getpkt (bufp
= buf
, 0);
1401 /* Restart the remote side; this is an extended protocol operation. */
1404 extended_remote_restart ()
1406 char *buf
= alloca (PBUFSIZ
);
1408 /* Send the restart command; for reasons I don't understand the
1409 remote side really expects a number after the "R". */
1411 sprintf (&buf
[1], "%x", 0);
1414 /* Now query for status so this looks just like we restarted
1415 gdbserver from scratch. */
1420 /* Clean up connection to a remote debugger. */
1424 remote_close (quitting
)
1428 SERIAL_CLOSE (remote_desc
);
1432 /* Query the remote side for the text, data and bss offsets. */
1437 char *buf
= alloca (PBUFSIZ
);
1440 CORE_ADDR text_addr
, data_addr
, bss_addr
;
1441 struct section_offsets
*offs
;
1443 putpkt ("qOffsets");
1447 if (buf
[0] == '\000')
1448 return; /* Return silently. Stub doesn't support
1452 warning ("Remote failure reply: %s", buf
);
1456 /* Pick up each field in turn. This used to be done with scanf, but
1457 scanf will make trouble if CORE_ADDR size doesn't match
1458 conversion directives correctly. The following code will work
1459 with any size of CORE_ADDR. */
1460 text_addr
= data_addr
= bss_addr
= 0;
1464 if (strncmp (ptr
, "Text=", 5) == 0)
1467 /* Don't use strtol, could lose on big values. */
1468 while (*ptr
&& *ptr
!= ';')
1469 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
1474 if (!lose
&& strncmp (ptr
, ";Data=", 6) == 0)
1477 while (*ptr
&& *ptr
!= ';')
1478 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
1483 if (!lose
&& strncmp (ptr
, ";Bss=", 5) == 0)
1486 while (*ptr
&& *ptr
!= ';')
1487 bss_addr
= (bss_addr
<< 4) + fromhex (*ptr
++);
1493 error ("Malformed response to offset query, %s", buf
);
1495 if (symfile_objfile
== NULL
)
1498 offs
= (struct section_offsets
*) alloca (SIZEOF_SECTION_OFFSETS
);
1499 memcpy (offs
, symfile_objfile
->section_offsets
, SIZEOF_SECTION_OFFSETS
);
1501 ANOFFSET (offs
, SECT_OFF_TEXT
) = text_addr
;
1503 /* This is a temporary kludge to force data and bss to use the same offsets
1504 because that's what nlmconv does now. The real solution requires changes
1505 to the stub and remote.c that I don't have time to do right now. */
1507 ANOFFSET (offs
, SECT_OFF_DATA
) = data_addr
;
1508 ANOFFSET (offs
, SECT_OFF_BSS
) = data_addr
;
1510 objfile_relocate (symfile_objfile
, offs
);
1514 * Cisco version of section offsets:
1516 * Instead of having GDB query the target for the section offsets,
1517 * Cisco lets the target volunteer the information! It's also in
1518 * a different format, so here are the functions that will decode
1519 * a section offset packet from a Cisco target.
1523 * Function: remote_cisco_section_offsets
1525 * Returns: zero for success, non-zero for failure
1529 remote_cisco_section_offsets (text_addr
, data_addr
, bss_addr
,
1530 text_offs
, data_offs
, bss_offs
)
1534 bfd_signed_vma
*text_offs
;
1535 bfd_signed_vma
*data_offs
;
1536 bfd_signed_vma
*bss_offs
;
1538 bfd_vma text_base
, data_base
, bss_base
;
1539 struct minimal_symbol
*start
;
1545 if (symfile_objfile
== NULL
)
1546 return -1; /* no can do nothin' */
1548 start
= lookup_minimal_symbol ("_start", NULL
, NULL
);
1550 return -1; /* Can't find "_start" symbol */
1552 data_base
= bss_base
= 0;
1553 text_base
= SYMBOL_VALUE_ADDRESS (start
);
1555 abfd
= symfile_objfile
->obfd
;
1556 for (sect
= abfd
->sections
;
1560 p
= (unsigned char *) bfd_get_section_name (abfd
, sect
);
1562 if (strcmp (p
+ len
- 4, "data") == 0) /* ends in "data" */
1563 if (data_base
== 0 ||
1564 data_base
> bfd_get_section_vma (abfd
, sect
))
1565 data_base
= bfd_get_section_vma (abfd
, sect
);
1566 if (strcmp (p
+ len
- 3, "bss") == 0) /* ends in "bss" */
1567 if (bss_base
== 0 ||
1568 bss_base
> bfd_get_section_vma (abfd
, sect
))
1569 bss_base
= bfd_get_section_vma (abfd
, sect
);
1571 *text_offs
= text_addr
- text_base
;
1572 *data_offs
= data_addr
- data_base
;
1573 *bss_offs
= bss_addr
- bss_base
;
1578 sprintf (tmp
, "VMA: text = 0x");
1579 sprintf_vma (tmp
+ strlen (tmp
), text_addr
);
1580 sprintf (tmp
+ strlen (tmp
), " data = 0x");
1581 sprintf_vma (tmp
+ strlen (tmp
), data_addr
);
1582 sprintf (tmp
+ strlen (tmp
), " bss = 0x");
1583 sprintf_vma (tmp
+ strlen (tmp
), bss_addr
);
1584 fprintf_filtered (gdb_stdlog
, tmp
);
1585 fprintf_filtered (gdb_stdlog
,
1586 "Reloc offset: text = 0x%s data = 0x%s bss = 0x%s\n",
1587 paddr_nz (*text_offs
),
1588 paddr_nz (*data_offs
),
1589 paddr_nz (*bss_offs
));
1596 * Function: remote_cisco_objfile_relocate
1598 * Relocate the symbol file for a remote target.
1602 remote_cisco_objfile_relocate (text_off
, data_off
, bss_off
)
1603 bfd_signed_vma text_off
;
1604 bfd_signed_vma data_off
;
1605 bfd_signed_vma bss_off
;
1607 struct section_offsets
*offs
;
1609 if (text_off
!= 0 || data_off
!= 0 || bss_off
!= 0)
1611 /* FIXME: This code assumes gdb-stabs.h is being used; it's
1612 broken for xcoff, dwarf, sdb-coff, etc. But there is no
1613 simple canonical representation for this stuff. */
1615 offs
= (struct section_offsets
*) alloca (SIZEOF_SECTION_OFFSETS
);
1616 memcpy (offs
, symfile_objfile
->section_offsets
, SIZEOF_SECTION_OFFSETS
);
1618 ANOFFSET (offs
, SECT_OFF_TEXT
) = text_off
;
1619 ANOFFSET (offs
, SECT_OFF_DATA
) = data_off
;
1620 ANOFFSET (offs
, SECT_OFF_BSS
) = bss_off
;
1622 /* First call the standard objfile_relocate. */
1623 objfile_relocate (symfile_objfile
, offs
);
1625 /* Now we need to fix up the section entries already attached to
1626 the exec target. These entries will control memory transfers
1627 from the exec file. */
1629 exec_set_section_offsets (text_off
, data_off
, bss_off
);
1633 /* Stub for catch_errors. */
1636 remote_start_remote_dummy (dummy
)
1639 start_remote (); /* Initialize gdb process mechanisms */
1644 remote_start_remote (dummy
)
1647 immediate_quit
= 1; /* Allow user to interrupt it */
1649 /* Ack any packet which the remote side has already sent. */
1650 SERIAL_WRITE (remote_desc
, "+", 1);
1652 /* Let the stub know that we want it to return the thread. */
1655 inferior_pid
= remote_current_thread (inferior_pid
);
1657 get_offsets (); /* Get text, data & bss offsets */
1659 putpkt ("?"); /* initiate a query from remote machine */
1662 return remote_start_remote_dummy (dummy
);
1665 /* Open a connection to a remote debugger.
1666 NAME is the filename used for communication. */
1669 remote_open (name
, from_tty
)
1673 remote_open_1 (name
, from_tty
, &remote_ops
, 0);
1676 /* Just like remote_open, but with asynchronous support. */
1678 remote_async_open (name
, from_tty
)
1682 remote_async_open_1 (name
, from_tty
, &remote_async_ops
, 0);
1685 /* Open a connection to a remote debugger using the extended
1686 remote gdb protocol. NAME is the filename used for communication. */
1689 extended_remote_open (name
, from_tty
)
1693 remote_open_1 (name
, from_tty
, &extended_remote_ops
, 1 /*extended_p */ );
1696 /* Just like extended_remote_open, but with asynchronous support. */
1698 extended_remote_async_open (name
, from_tty
)
1702 remote_async_open_1 (name
, from_tty
, &extended_async_remote_ops
, 1 /*extended_p */ );
1705 /* Generic code for opening a connection to a remote target. */
1707 static DCACHE
*remote_dcache
;
1710 remote_open_1 (name
, from_tty
, target
, extended_p
)
1713 struct target_ops
*target
;
1717 error ("To open a remote debug connection, you need to specify what\n\
1718 serial device is attached to the remote system (e.g. /dev/ttya).");
1720 target_preopen (from_tty
);
1722 unpush_target (target
);
1724 remote_dcache
= dcache_init (remote_read_bytes
, remote_write_bytes
);
1726 remote_desc
= SERIAL_OPEN (name
);
1728 perror_with_name (name
);
1730 if (baud_rate
!= -1)
1732 if (SERIAL_SETBAUDRATE (remote_desc
, baud_rate
))
1734 SERIAL_CLOSE (remote_desc
);
1735 perror_with_name (name
);
1739 SERIAL_RAW (remote_desc
);
1741 /* If there is something sitting in the buffer we might take it as a
1742 response to a command, which would be bad. */
1743 SERIAL_FLUSH_INPUT (remote_desc
);
1747 puts_filtered ("Remote debugging using ");
1748 puts_filtered (name
);
1749 puts_filtered ("\n");
1751 push_target (target
); /* Switch to using remote target now */
1753 init_packet_config (&remote_protocol_P
);
1754 init_packet_config (&remote_protocol_Z
);
1756 general_thread
= -2;
1757 continue_thread
= -2;
1759 /* Force remote_write_bytes to check whether target supports
1760 binary downloading. */
1761 init_packet_config (&remote_protocol_binary_download
);
1763 /* Without this, some commands which require an active target (such
1764 as kill) won't work. This variable serves (at least) double duty
1765 as both the pid of the target process (if it has such), and as a
1766 flag indicating that a target is active. These functions should
1767 be split out into seperate variables, especially since GDB will
1768 someday have a notion of debugging several processes. */
1770 inferior_pid
= MAGIC_NULL_PID
;
1771 /* Start the remote connection; if error (0), discard this target.
1772 In particular, if the user quits, be sure to discard it
1773 (we'd be in an inconsistent state otherwise). */
1774 if (!catch_errors (remote_start_remote
, NULL
,
1775 "Couldn't establish connection to remote target\n",
1784 /* tell the remote that we're using the extended protocol. */
1785 char *buf
= alloca (PBUFSIZ
);
1791 /* Just like remote_open but with asynchronous support. */
1793 remote_async_open_1 (name
, from_tty
, target
, extended_p
)
1796 struct target_ops
*target
;
1800 error ("To open a remote debug connection, you need to specify what\n\
1801 serial device is attached to the remote system (e.g. /dev/ttya).");
1803 target_preopen (from_tty
);
1805 unpush_target (target
);
1807 remote_dcache
= dcache_init (remote_read_bytes
, remote_write_bytes
);
1809 remote_desc
= SERIAL_OPEN (name
);
1811 perror_with_name (name
);
1813 if (baud_rate
!= -1)
1815 if (SERIAL_SETBAUDRATE (remote_desc
, baud_rate
))
1817 SERIAL_CLOSE (remote_desc
);
1818 perror_with_name (name
);
1822 SERIAL_RAW (remote_desc
);
1824 /* If there is something sitting in the buffer we might take it as a
1825 response to a command, which would be bad. */
1826 SERIAL_FLUSH_INPUT (remote_desc
);
1830 puts_filtered ("Remote debugging using ");
1831 puts_filtered (name
);
1832 puts_filtered ("\n");
1835 /* If running in asynchronous mode, register the target with the
1836 event loop. Set things up so that when there is an event on the
1837 file descriptor, the event loop will call fetch_inferior_event,
1838 which will do the proper analysis to determine what happened. */
1839 if (async_p
&& SERIAL_CAN_ASYNC_P (remote_desc
))
1840 SERIAL_ASYNC (remote_desc
, inferior_event_handler
, 0);
1841 if (remote_debug
&& SERIAL_IS_ASYNC_P (remote_desc
))
1842 fputs_unfiltered ("Async mode.\n", gdb_stdlog
);
1844 push_target (target
); /* Switch to using remote target now */
1846 init_packet_config (&remote_protocol_P
);
1847 init_packet_config (&remote_protocol_Z
);
1849 general_thread
= -2;
1850 continue_thread
= -2;
1852 /* Force remote_write_bytes to check whether target supports
1853 binary downloading. */
1854 init_packet_config (&remote_protocol_binary_download
);
1856 /* If running asynchronously, set things up for telling the target
1857 to use the extended protocol. This will happen only after the
1858 target has been connected to, in fetch_inferior_event. */
1859 if (extended_p
&& SERIAL_IS_ASYNC_P (remote_desc
))
1860 add_continuation (set_extended_protocol
, NULL
);
1862 /* Without this, some commands which require an active target (such
1863 as kill) won't work. This variable serves (at least) double duty
1864 as both the pid of the target process (if it has such), and as a
1865 flag indicating that a target is active. These functions should
1866 be split out into seperate variables, especially since GDB will
1867 someday have a notion of debugging several processes. */
1869 inferior_pid
= MAGIC_NULL_PID
;
1870 /* Start the remote connection; if error (0), discard this target.
1871 In particular, if the user quits, be sure to discard it
1872 (we'd be in an inconsistent state otherwise). */
1873 if (!catch_errors (remote_start_remote
, NULL
,
1874 "Couldn't establish connection to remote target\n",
1877 /* Unregister the file descriptor from the event loop. */
1878 if (SERIAL_IS_ASYNC_P (remote_desc
))
1879 SERIAL_ASYNC (remote_desc
, NULL
, 0);
1884 if (!SERIAL_IS_ASYNC_P (remote_desc
))
1888 /* tell the remote that we're using the extended protocol. */
1889 char *buf
= alloca (PBUFSIZ
);
1896 /* This will be called by fetch_inferior_event, via the
1897 cmd_continuation pointer, only after the target has stopped. */
1899 set_extended_protocol (arg
)
1900 struct continuation_arg
*arg
;
1902 /* tell the remote that we're using the extended protocol. */
1903 char *buf
= alloca (PBUFSIZ
);
1908 /* This takes a program previously attached to and detaches it. After
1909 this is done, GDB can be used to debug some other program. We
1910 better not have left any breakpoints in the target program or it'll
1911 die when it hits one. */
1914 remote_detach (args
, from_tty
)
1918 char *buf
= alloca (PBUFSIZ
);
1921 error ("Argument given to \"detach\" when remotely debugging.");
1923 /* Tell the remote target to detach. */
1929 puts_filtered ("Ending remote debugging.\n");
1933 /* Same as remote_detach, but with async support. */
1935 remote_async_detach (args
, from_tty
)
1939 char *buf
= alloca (PBUFSIZ
);
1942 error ("Argument given to \"detach\" when remotely debugging.");
1944 /* Tell the remote target to detach. */
1948 /* Unregister the file descriptor from the event loop. */
1949 if (SERIAL_IS_ASYNC_P (remote_desc
))
1950 SERIAL_ASYNC (remote_desc
, NULL
, 0);
1954 puts_filtered ("Ending remote debugging.\n");
1957 /* Convert hex digit A to a number. */
1963 if (a
>= '0' && a
<= '9')
1965 else if (a
>= 'a' && a
<= 'f')
1966 return a
- 'a' + 10;
1967 else if (a
>= 'A' && a
<= 'F')
1968 return a
- 'A' + 10;
1970 error ("Reply contains invalid hex digit %d", a
);
1973 /* Convert number NIB to a hex digit. */
1982 return 'a' + nib
- 10;
1985 /* Tell the remote machine to resume. */
1987 static enum target_signal last_sent_signal
= TARGET_SIGNAL_0
;
1989 static int last_sent_step
;
1992 remote_resume (pid
, step
, siggnal
)
1994 enum target_signal siggnal
;
1996 char *buf
= alloca (PBUFSIZ
);
1999 set_thread (0, 0); /* run any thread */
2001 set_thread (pid
, 0); /* run this thread */
2003 dcache_flush (remote_dcache
);
2005 last_sent_signal
= siggnal
;
2006 last_sent_step
= step
;
2008 /* A hook for when we need to do something at the last moment before
2010 if (target_resume_hook
)
2011 (*target_resume_hook
) ();
2013 if (siggnal
!= TARGET_SIGNAL_0
)
2015 buf
[0] = step
? 'S' : 'C';
2016 buf
[1] = tohex (((int) siggnal
>> 4) & 0xf);
2017 buf
[2] = tohex ((int) siggnal
& 0xf);
2021 strcpy (buf
, step
? "s" : "c");
2026 /* Same as remote_resume, but with async support. */
2028 remote_async_resume (pid
, step
, siggnal
)
2030 enum target_signal siggnal
;
2032 char *buf
= alloca (PBUFSIZ
);
2035 set_thread (0, 0); /* run any thread */
2037 set_thread (pid
, 0); /* run this thread */
2039 dcache_flush (remote_dcache
);
2041 last_sent_signal
= siggnal
;
2042 last_sent_step
= step
;
2044 /* A hook for when we need to do something at the last moment before
2046 if (target_resume_hook
)
2047 (*target_resume_hook
) ();
2049 /* Set things up before execution starts for async commands. */
2050 /* This function can be entered more than once for the same execution
2051 command, because it is also called by handle_inferior_event. So
2052 we make sure that we don't do the initialization for sync
2053 execution more than once. */
2054 if (SERIAL_IS_ASYNC_P (remote_desc
) && !target_executing
)
2056 target_executing
= 1;
2058 /* If the command must look synchronous, fake it, by making gdb
2059 display an empty prompt after the command has completed. Also
2063 push_prompt ("", "", "");
2064 delete_file_handler (input_fd
);
2065 initialize_sigint_signal_handler ();
2069 if (siggnal
!= TARGET_SIGNAL_0
)
2071 buf
[0] = step
? 'S' : 'C';
2072 buf
[1] = tohex (((int) siggnal
>> 4) & 0xf);
2073 buf
[2] = tohex ((int) siggnal
& 0xf);
2077 strcpy (buf
, step
? "s" : "c");
2083 /* Set up the signal handler for SIGINT, while the target is
2084 executing, ovewriting the 'regular' SIGINT signal handler. */
2086 initialize_sigint_signal_handler ()
2088 sigint_remote_token
=
2089 create_async_signal_handler (async_remote_interrupt
, NULL
);
2090 signal (SIGINT
, handle_remote_sigint
);
2093 /* Signal handler for SIGINT, while the target is executing. */
2095 handle_remote_sigint (sig
)
2098 signal (sig
, handle_remote_sigint_twice
);
2099 sigint_remote_twice_token
=
2100 create_async_signal_handler (async_remote_interrupt_twice
, NULL
);
2101 mark_async_signal_handler_wrapper (sigint_remote_token
);
2104 /* Signal handler for SIGINT, installed after SIGINT has already been
2105 sent once. It will take effect the second time that the user sends
2108 handle_remote_sigint_twice (sig
)
2111 signal (sig
, handle_sigint
);
2112 sigint_remote_twice_token
=
2113 create_async_signal_handler (async_remote_interrupt
, NULL
);
2114 mark_async_signal_handler_wrapper (sigint_remote_twice_token
);
2117 /* Perform the real interruption of hte target execution, in response
2120 async_remote_interrupt (arg
)
2121 gdb_client_data arg
;
2124 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt called\n");
2129 /* Perform interrupt, if the first attempt did not succeed. Just give
2130 up on the target alltogether. */
2132 async_remote_interrupt_twice (arg
)
2133 gdb_client_data arg
;
2136 signal (SIGINT
, handle_remote_sigint
);
2139 /* Reinstall the usual SIGINT handlers, after the target has
2142 cleanup_sigint_signal_handler ()
2144 signal (SIGINT
, handle_sigint
);
2145 if (sigint_remote_twice_token
)
2146 delete_async_signal_handler ((struct async_signal_handler
**) & sigint_remote_twice_token
);
2147 if (sigint_remote_token
)
2148 delete_async_signal_handler ((struct async_signal_handler
**) & sigint_remote_token
);
2151 /* Send ^C to target to halt it. Target will respond, and send us a
2153 static void (*ofunc
) PARAMS ((int));
2155 /* The command line interface's stop routine. This function is installed
2156 as a signal handler for SIGINT. The first time a user requests a
2157 stop, we call remote_stop to send a break or ^C. If there is no
2158 response from the target (it didn't stop when the user requested it),
2159 we ask the user if he'd like to detach from the target. */
2161 remote_interrupt (signo
)
2164 /* If this doesn't work, try more severe steps. */
2165 signal (signo
, remote_interrupt_twice
);
2168 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt called\n");
2173 /* The user typed ^C twice. */
2176 remote_interrupt_twice (signo
)
2179 signal (signo
, ofunc
);
2181 signal (signo
, remote_interrupt
);
2184 /* This is the generic stop called via the target vector. When a target
2185 interrupt is requested, either by the command line or the GUI, we
2186 will eventually end up here. */
2190 /* Send a break or a ^C, depending on user preference. */
2192 fprintf_unfiltered (gdb_stdlog
, "remote_stop called\n");
2195 SERIAL_SEND_BREAK (remote_desc
);
2197 SERIAL_WRITE (remote_desc
, "\003", 1);
2200 /* Ask the user what to do when an interrupt is received. */
2205 target_terminal_ours ();
2207 if (query ("Interrupted while waiting for the program.\n\
2208 Give up (and stop debugging it)? "))
2210 target_mourn_inferior ();
2211 return_to_top_level (RETURN_QUIT
);
2214 target_terminal_inferior ();
2217 /* If nonzero, ignore the next kill. */
2222 remote_console_output (msg
)
2227 for (p
= msg
; p
[0] && p
[1]; p
+= 2)
2230 char c
= fromhex (p
[0]) * 16 + fromhex (p
[1]);
2233 fputs_unfiltered (tb
, gdb_stdtarg
);
2237 /* Wait until the remote machine stops, then return,
2238 storing status in STATUS just as `wait' would.
2239 Returns "pid", which in the case of a multi-threaded
2240 remote OS, is the thread-id. */
2243 remote_wait (pid
, status
)
2245 struct target_waitstatus
*status
;
2247 unsigned char *buf
= alloca (PBUFSIZ
);
2248 int thread_num
= -1;
2250 status
->kind
= TARGET_WAITKIND_EXITED
;
2251 status
->value
.integer
= 0;
2257 ofunc
= signal (SIGINT
, remote_interrupt
);
2258 getpkt ((char *) buf
, 1);
2259 signal (SIGINT
, ofunc
);
2261 /* This is a hook for when we need to do something (perhaps the
2262 collection of trace data) every time the target stops. */
2263 if (target_wait_loop_hook
)
2264 (*target_wait_loop_hook
) ();
2268 case 'E': /* Error of some sort */
2269 warning ("Remote failure reply: %s", buf
);
2271 case 'T': /* Status with PC, SP, FP, ... */
2275 char regs
[MAX_REGISTER_RAW_SIZE
];
2277 /* Expedited reply, containing Signal, {regno, reg} repeat */
2278 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
2280 n... = register number
2281 r... = register contents
2283 p
= &buf
[3]; /* after Txx */
2290 /* Read the register number */
2291 regno
= strtol ((const char *) p
, &p_temp
, 16);
2292 p1
= (unsigned char *) p_temp
;
2294 if (p1
== p
) /* No register number present here */
2296 p1
= (unsigned char *) strchr ((const char *) p
, ':');
2298 warning ("Malformed packet(a) (missing colon): %s\n\
2301 if (strncmp ((const char *) p
, "thread", p1
- p
) == 0)
2303 p_temp
= unpack_varlen_hex (++p1
, &thread_num
);
2304 record_currthread (thread_num
);
2305 p
= (unsigned char *) p_temp
;
2313 warning ("Malformed packet(b) (missing colon): %s\n\
2317 if (regno
>= NUM_REGS
)
2318 warning ("Remote sent bad register number %ld: %s\n\
2322 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
++)
2324 if (p
[0] == 0 || p
[1] == 0)
2325 warning ("Remote reply is too short: %s", buf
);
2326 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
2329 supply_register (regno
, regs
);
2334 warning ("Remote register badly formatted: %s", buf
);
2335 warning (" here: %s", p
);
2340 case 'S': /* Old style status, just signal only */
2341 status
->kind
= TARGET_WAITKIND_STOPPED
;
2342 status
->value
.sig
= (enum target_signal
)
2343 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
2347 /* Export Cisco kernel mode as a convenience variable
2348 (so that it can be used in the GDB prompt if desired). */
2350 if (cisco_kernel_mode
== 1)
2351 set_internalvar (lookup_internalvar ("cisco_kernel_mode"),
2352 value_from_string ("PDEBUG-"));
2353 cisco_kernel_mode
= 0;
2354 thread_num
= strtol ((const char *) &buf
[4], NULL
, 16);
2355 record_currthread (thread_num
);
2357 else if (buf
[3] == 'k')
2359 /* Export Cisco kernel mode as a convenience variable
2360 (so that it can be used in the GDB prompt if desired). */
2362 if (cisco_kernel_mode
== 1)
2363 set_internalvar (lookup_internalvar ("cisco_kernel_mode"),
2364 value_from_string ("KDEBUG-"));
2365 cisco_kernel_mode
= 1;
2368 case 'N': /* Cisco special: status and offsets */
2370 bfd_vma text_addr
, data_addr
, bss_addr
;
2371 bfd_signed_vma text_off
, data_off
, bss_off
;
2374 status
->kind
= TARGET_WAITKIND_STOPPED
;
2375 status
->value
.sig
= (enum target_signal
)
2376 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
2378 if (symfile_objfile
== NULL
)
2380 warning ("Relocation packet received with no symbol file. \
2385 /* Relocate object file. Buffer format is NAATT;DD;BB
2386 * where AA is the signal number, TT is the new text
2387 * address, DD * is the new data address, and BB is the
2388 * new bss address. */
2391 text_addr
= strtoul (p
, (char **) &p1
, 16);
2392 if (p1
== p
|| *p1
!= ';')
2393 warning ("Malformed relocation packet: Packet '%s'", buf
);
2395 data_addr
= strtoul (p
, (char **) &p1
, 16);
2396 if (p1
== p
|| *p1
!= ';')
2397 warning ("Malformed relocation packet: Packet '%s'", buf
);
2399 bss_addr
= strtoul (p
, (char **) &p1
, 16);
2401 warning ("Malformed relocation packet: Packet '%s'", buf
);
2403 if (remote_cisco_section_offsets (text_addr
, data_addr
, bss_addr
,
2404 &text_off
, &data_off
, &bss_off
)
2406 if (text_off
!= 0 || data_off
!= 0 || bss_off
!= 0)
2407 remote_cisco_objfile_relocate (text_off
, data_off
, bss_off
);
2411 case 'W': /* Target exited */
2413 /* The remote process exited. */
2414 status
->kind
= TARGET_WAITKIND_EXITED
;
2415 status
->value
.integer
= (fromhex (buf
[1]) << 4) + fromhex (buf
[2]);
2419 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
2420 status
->value
.sig
= (enum target_signal
)
2421 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
2425 case 'O': /* Console output */
2426 remote_console_output (buf
+ 1);
2429 if (last_sent_signal
!= TARGET_SIGNAL_0
)
2431 /* Zero length reply means that we tried 'S' or 'C' and
2432 the remote system doesn't support it. */
2433 target_terminal_ours_for_output ();
2435 ("Can't send signals to this remote system. %s not sent.\n",
2436 target_signal_to_name (last_sent_signal
));
2437 last_sent_signal
= TARGET_SIGNAL_0
;
2438 target_terminal_inferior ();
2440 strcpy ((char *) buf
, last_sent_step
? "s" : "c");
2441 putpkt ((char *) buf
);
2444 /* else fallthrough */
2446 warning ("Invalid remote reply: %s", buf
);
2451 if (thread_num
!= -1)
2455 return inferior_pid
;
2458 /* Async version of remote_wait. */
2460 remote_async_wait (pid
, status
)
2462 struct target_waitstatus
*status
;
2464 unsigned char *buf
= alloca (PBUFSIZ
);
2465 int thread_num
= -1;
2467 status
->kind
= TARGET_WAITKIND_EXITED
;
2468 status
->value
.integer
= 0;
2474 if (!SERIAL_IS_ASYNC_P (remote_desc
))
2475 ofunc
= signal (SIGINT
, remote_interrupt
);
2476 getpkt ((char *) buf
, 1);
2477 if (!SERIAL_IS_ASYNC_P (remote_desc
))
2478 signal (SIGINT
, ofunc
);
2480 /* This is a hook for when we need to do something (perhaps the
2481 collection of trace data) every time the target stops. */
2482 if (target_wait_loop_hook
)
2483 (*target_wait_loop_hook
) ();
2487 case 'E': /* Error of some sort */
2488 warning ("Remote failure reply: %s", buf
);
2490 case 'T': /* Status with PC, SP, FP, ... */
2494 char regs
[MAX_REGISTER_RAW_SIZE
];
2496 /* Expedited reply, containing Signal, {regno, reg} repeat */
2497 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
2499 n... = register number
2500 r... = register contents
2502 p
= &buf
[3]; /* after Txx */
2509 /* Read the register number */
2510 regno
= strtol ((const char *) p
, &p_temp
, 16);
2511 p1
= (unsigned char *) p_temp
;
2513 if (p1
== p
) /* No register number present here */
2515 p1
= (unsigned char *) strchr ((const char *) p
, ':');
2517 warning ("Malformed packet(a) (missing colon): %s\n\
2520 if (strncmp ((const char *) p
, "thread", p1
- p
) == 0)
2522 p_temp
= unpack_varlen_hex (++p1
, &thread_num
);
2523 record_currthread (thread_num
);
2524 p
= (unsigned char *) p_temp
;
2532 warning ("Malformed packet(b) (missing colon): %s\n\
2536 if (regno
>= NUM_REGS
)
2537 warning ("Remote sent bad register number %ld: %s\n\
2541 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
++)
2543 if (p
[0] == 0 || p
[1] == 0)
2544 warning ("Remote reply is too short: %s", buf
);
2545 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
2548 supply_register (regno
, regs
);
2553 warning ("Remote register badly formatted: %s", buf
);
2554 warning (" here: %s", p
);
2559 case 'S': /* Old style status, just signal only */
2560 status
->kind
= TARGET_WAITKIND_STOPPED
;
2561 status
->value
.sig
= (enum target_signal
)
2562 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
2566 /* Export Cisco kernel mode as a convenience variable
2567 (so that it can be used in the GDB prompt if desired). */
2569 if (cisco_kernel_mode
== 1)
2570 set_internalvar (lookup_internalvar ("cisco_kernel_mode"),
2571 value_from_string ("PDEBUG-"));
2572 cisco_kernel_mode
= 0;
2573 thread_num
= strtol ((const char *) &buf
[4], NULL
, 16);
2574 record_currthread (thread_num
);
2576 else if (buf
[3] == 'k')
2578 /* Export Cisco kernel mode as a convenience variable
2579 (so that it can be used in the GDB prompt if desired). */
2581 if (cisco_kernel_mode
== 1)
2582 set_internalvar (lookup_internalvar ("cisco_kernel_mode"),
2583 value_from_string ("KDEBUG-"));
2584 cisco_kernel_mode
= 1;
2587 case 'N': /* Cisco special: status and offsets */
2589 bfd_vma text_addr
, data_addr
, bss_addr
;
2590 bfd_signed_vma text_off
, data_off
, bss_off
;
2593 status
->kind
= TARGET_WAITKIND_STOPPED
;
2594 status
->value
.sig
= (enum target_signal
)
2595 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
2597 if (symfile_objfile
== NULL
)
2599 warning ("Relocation packet recieved with no symbol file. \
2604 /* Relocate object file. Buffer format is NAATT;DD;BB
2605 * where AA is the signal number, TT is the new text
2606 * address, DD * is the new data address, and BB is the
2607 * new bss address. */
2610 text_addr
= strtoul (p
, (char **) &p1
, 16);
2611 if (p1
== p
|| *p1
!= ';')
2612 warning ("Malformed relocation packet: Packet '%s'", buf
);
2614 data_addr
= strtoul (p
, (char **) &p1
, 16);
2615 if (p1
== p
|| *p1
!= ';')
2616 warning ("Malformed relocation packet: Packet '%s'", buf
);
2618 bss_addr
= strtoul (p
, (char **) &p1
, 16);
2620 warning ("Malformed relocation packet: Packet '%s'", buf
);
2622 if (remote_cisco_section_offsets (text_addr
, data_addr
, bss_addr
,
2623 &text_off
, &data_off
, &bss_off
)
2625 if (text_off
!= 0 || data_off
!= 0 || bss_off
!= 0)
2626 remote_cisco_objfile_relocate (text_off
, data_off
, bss_off
);
2630 case 'W': /* Target exited */
2632 /* The remote process exited. */
2633 status
->kind
= TARGET_WAITKIND_EXITED
;
2634 status
->value
.integer
= (fromhex (buf
[1]) << 4) + fromhex (buf
[2]);
2638 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
2639 status
->value
.sig
= (enum target_signal
)
2640 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
2644 case 'O': /* Console output */
2645 remote_console_output (buf
+ 1);
2648 if (last_sent_signal
!= TARGET_SIGNAL_0
)
2650 /* Zero length reply means that we tried 'S' or 'C' and
2651 the remote system doesn't support it. */
2652 target_terminal_ours_for_output ();
2654 ("Can't send signals to this remote system. %s not sent.\n",
2655 target_signal_to_name (last_sent_signal
));
2656 last_sent_signal
= TARGET_SIGNAL_0
;
2657 target_terminal_inferior ();
2659 strcpy ((char *) buf
, last_sent_step
? "s" : "c");
2660 putpkt ((char *) buf
);
2663 /* else fallthrough */
2665 warning ("Invalid remote reply: %s", buf
);
2670 if (thread_num
!= -1)
2674 return inferior_pid
;
2677 /* Number of bytes of registers this stub implements. */
2679 static int register_bytes_found
;
2681 /* Read the remote registers into the block REGS. */
2682 /* Currently we just read all the registers, so we don't use regno. */
2686 remote_fetch_registers (regno
)
2689 char *buf
= alloca (PBUFSIZ
);
2692 char regs
[REGISTER_BYTES
];
2694 set_thread (inferior_pid
, 1);
2699 if (remote_register_buf_size
== 0)
2700 remote_register_buf_size
= strlen (buf
);
2702 /* Unimplemented registers read as all bits zero. */
2703 memset (regs
, 0, REGISTER_BYTES
);
2705 /* We can get out of synch in various cases. If the first character
2706 in the buffer is not a hex character, assume that has happened
2707 and try to fetch another packet to read. */
2708 while ((buf
[0] < '0' || buf
[0] > '9')
2709 && (buf
[0] < 'a' || buf
[0] > 'f')
2710 && buf
[0] != 'x') /* New: unavailable register value */
2713 fprintf_unfiltered (gdb_stdlog
,
2714 "Bad register packet; fetching a new packet\n");
2718 /* Reply describes registers byte by byte, each byte encoded as two
2719 hex characters. Suck them all up, then supply them to the
2720 register cacheing/storage mechanism. */
2723 for (i
= 0; i
< REGISTER_BYTES
; i
++)
2729 warning ("Remote reply is of odd length: %s", buf
);
2730 /* Don't change register_bytes_found in this case, and don't
2731 print a second warning. */
2734 if (p
[0] == 'x' && p
[1] == 'x')
2735 regs
[i
] = 0; /* 'x' */
2737 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
2741 if (i
!= register_bytes_found
)
2743 register_bytes_found
= i
;
2744 #ifdef REGISTER_BYTES_OK
2745 if (!REGISTER_BYTES_OK (i
))
2746 warning ("Remote reply is too short: %s", buf
);
2751 for (i
= 0; i
< NUM_REGS
; i
++)
2753 supply_register (i
, ®s
[REGISTER_BYTE (i
)]);
2754 if (buf
[REGISTER_BYTE (i
) * 2] == 'x')
2755 register_valid
[i
] = -1; /* register value not available */
2759 /* Prepare to store registers. Since we may send them all (using a
2760 'G' request), we have to read out the ones we don't want to change
2764 remote_prepare_to_store ()
2766 /* Make sure the entire registers array is valid. */
2767 switch (remote_protocol_P
.support
)
2769 case PACKET_DISABLE
:
2770 case PACKET_SUPPORT_UNKNOWN
:
2771 read_register_bytes (0, (char *) NULL
, REGISTER_BYTES
);
2778 /* Helper: Attempt to store REGNO using the P packet. Return fail IFF
2779 packet was not recognized. */
2782 store_register_using_P (regno
)
2785 /* Try storing a single register. */
2786 char *buf
= alloca (PBUFSIZ
);
2791 sprintf (buf
, "P%x=", regno
);
2792 p
= buf
+ strlen (buf
);
2793 regp
= ®isters
[REGISTER_BYTE (regno
)];
2794 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); ++i
)
2796 *p
++ = tohex ((regp
[i
] >> 4) & 0xf);
2797 *p
++ = tohex (regp
[i
] & 0xf);
2802 return buf
[0] != '\0';
2806 /* Store register REGNO, or all registers if REGNO == -1, from the contents
2807 of REGISTERS. FIXME: ignores errors. */
2810 remote_store_registers (regno
)
2813 char *buf
= alloca (PBUFSIZ
);
2817 set_thread (inferior_pid
, 1);
2821 switch (remote_protocol_P
.support
)
2823 case PACKET_DISABLE
:
2826 if (store_register_using_P (regno
))
2829 error ("Protocol error: P packet not recognized by stub");
2830 case PACKET_SUPPORT_UNKNOWN
:
2831 if (store_register_using_P (regno
))
2833 /* The stub recognized the 'P' packet. Remember this. */
2834 remote_protocol_P
.support
= PACKET_ENABLE
;
2839 /* The stub does not support the 'P' packet. Use 'G'
2840 instead, and don't try using 'P' in the future (it
2841 will just waste our time). */
2842 remote_protocol_P
.support
= PACKET_DISABLE
;
2850 /* Command describes registers byte by byte,
2851 each byte encoded as two hex characters. */
2854 /* remote_prepare_to_store insures that register_bytes_found gets set. */
2855 for (i
= 0; i
< register_bytes_found
; i
++)
2857 *p
++ = tohex ((registers
[i
] >> 4) & 0xf);
2858 *p
++ = tohex (registers
[i
] & 0xf);
2865 /* Use of the data cache *used* to be disabled because it loses for looking
2866 at and changing hardware I/O ports and the like. Accepting `volatile'
2867 would perhaps be one way to fix it. Another idea would be to use the
2868 executable file for the text segment (for all SEC_CODE sections?
2869 For all SEC_READONLY sections?). This has problems if you want to
2870 actually see what the memory contains (e.g. self-modifying code,
2871 clobbered memory, user downloaded the wrong thing).
2873 Because it speeds so much up, it's now enabled, if you're playing
2874 with registers you turn it of (set remotecache 0). */
2876 /* Read a word from remote address ADDR and return it.
2877 This goes through the data cache. */
2881 remote_fetch_word (addr
)
2884 return dcache_fetch (remote_dcache
, addr
);
2887 /* Write a word WORD into remote address ADDR.
2888 This goes through the data cache. */
2891 remote_store_word (addr
, word
)
2895 dcache_poke (remote_dcache
, addr
, word
);
2897 #endif /* 0 (unused?) */
2901 /* Return the number of hex digits in num. */
2909 for (i
= 0; num
!= 0; i
++)
2915 /* Set BUF to the hex digits representing NUM. */
2918 hexnumstr (buf
, num
)
2923 int len
= hexnumlen (num
);
2927 for (i
= len
- 1; i
>= 0; i
--)
2929 buf
[i
] = "0123456789abcdef"[(num
& 0xf)];
2936 /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
2939 remote_address_masked (addr
)
2942 if (remote_address_size
> 0
2943 && remote_address_size
< (sizeof (ULONGEST
) * 8))
2945 /* Only create a mask when that mask can safely be constructed
2946 in a ULONGEST variable. */
2948 mask
= (mask
<< remote_address_size
) - 1;
2954 /* Determine whether the remote target supports binary downloading.
2955 This is accomplished by sending a no-op memory write of zero length
2956 to the target at the specified address. It does not suffice to send
2957 the whole packet, since many stubs strip the eighth bit and subsequently
2958 compute a wrong checksum, which causes real havoc with remote_write_bytes.
2960 NOTE: This can still lose if the serial line is not eight-bit
2961 clean. In cases like this, the user should clear "remote
2965 check_binary_download (addr
)
2968 switch (remote_protocol_binary_download
.support
)
2970 case PACKET_DISABLE
:
2974 case PACKET_SUPPORT_UNKNOWN
:
2976 char *buf
= alloca (PBUFSIZ
);
2981 p
+= hexnumstr (p
, (ULONGEST
) addr
);
2983 p
+= hexnumstr (p
, (ULONGEST
) 0);
2987 putpkt_binary (buf
, (int) (p
- buf
));
2993 fprintf_unfiltered (gdb_stdlog
,
2994 "binary downloading NOT suppported by target\n");
2995 remote_protocol_binary_download
.support
= PACKET_DISABLE
;
3000 fprintf_unfiltered (gdb_stdlog
,
3001 "binary downloading suppported by target\n");
3002 remote_protocol_binary_download
.support
= PACKET_ENABLE
;
3009 /* Write memory data directly to the remote machine.
3010 This does not inform the data cache; the data cache uses this.
3011 MEMADDR is the address in the remote memory space.
3012 MYADDR is the address of the buffer in our space.
3013 LEN is the number of bytes.
3015 Returns number of bytes transferred, or 0 for error. */
3018 remote_write_bytes (memaddr
, myaddr
, len
)
3023 unsigned char *buf
= alloca (PBUFSIZ
);
3024 int max_buf_size
; /* Max size of packet output buffer */
3027 /* Verify that the target can support a binary download */
3028 check_binary_download (memaddr
);
3030 /* Chop the transfer down if necessary */
3032 max_buf_size
= min (remote_write_size
, PBUFSIZ
);
3033 if (remote_register_buf_size
!= 0)
3034 max_buf_size
= min (max_buf_size
, remote_register_buf_size
);
3036 /* Subtract header overhead from max payload size - $M<memaddr>,<len>:#nn */
3037 max_buf_size
-= 2 + hexnumlen (memaddr
+ len
- 1) + 1 + hexnumlen (len
) + 4;
3042 unsigned char *p
, *plen
;
3046 /* construct "M"<memaddr>","<len>":" */
3047 /* sprintf (buf, "M%lx,%x:", (unsigned long) memaddr, todo); */
3048 memaddr
= remote_address_masked (memaddr
);
3050 switch (remote_protocol_binary_download
.support
)
3054 todo
= min (len
, max_buf_size
);
3056 case PACKET_DISABLE
:
3058 todo
= min (len
, max_buf_size
/ 2); /* num bytes that will fit */
3060 case PACKET_SUPPORT_UNKNOWN
:
3061 internal_error ("remote_write_bytes: bad switch");
3064 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
3067 plen
= p
; /* remember where len field goes */
3068 p
+= hexnumstr (p
, (ULONGEST
) todo
);
3072 /* We send target system values byte by byte, in increasing byte
3073 addresses, each byte encoded as two hex characters (or one
3074 binary character). */
3075 switch (remote_protocol_binary_download
.support
)
3081 (i
< todo
) && (i
+ escaped
) < (max_buf_size
- 2);
3084 switch (myaddr
[i
] & 0xff)
3089 /* These must be escaped */
3092 *p
++ = (myaddr
[i
] & 0xff) ^ 0x20;
3095 *p
++ = myaddr
[i
] & 0xff;
3102 /* Escape chars have filled up the buffer prematurely,
3103 and we have actually sent fewer bytes than planned.
3104 Fix-up the length field of the packet. */
3106 /* FIXME: will fail if new len is a shorter string than
3109 plen
+= hexnumstr (plen
, (ULONGEST
) i
);
3114 case PACKET_DISABLE
:
3116 for (i
= 0; i
< todo
; i
++)
3118 *p
++ = tohex ((myaddr
[i
] >> 4) & 0xf);
3119 *p
++ = tohex (myaddr
[i
] & 0xf);
3124 case PACKET_SUPPORT_UNKNOWN
:
3125 internal_error ("remote_write_bytes: bad switch");
3128 putpkt_binary (buf
, (int) (p
- buf
));
3133 /* There is no correspondance between what the remote protocol uses
3134 for errors and errno codes. We would like a cleaner way of
3135 representing errors (big enough to include errno codes, bfd_error
3136 codes, and others). But for now just return EIO. */
3141 /* Increment by i, not by todo, in case escape chars
3142 caused us to send fewer bytes than we'd planned. */
3150 /* Read memory data directly from the remote machine.
3151 This does not use the data cache; the data cache uses this.
3152 MEMADDR is the address in the remote memory space.
3153 MYADDR is the address of the buffer in our space.
3154 LEN is the number of bytes.
3156 Returns number of bytes transferred, or 0 for error. */
3159 remote_read_bytes (memaddr
, myaddr
, len
)
3164 char *buf
= alloca (PBUFSIZ
);
3165 int max_buf_size
; /* Max size of packet output buffer */
3168 /* Chop the transfer down if necessary */
3170 max_buf_size
= min (remote_write_size
, PBUFSIZ
);
3171 if (remote_register_buf_size
!= 0)
3172 max_buf_size
= min (max_buf_size
, remote_register_buf_size
);
3181 todo
= min (len
, max_buf_size
/ 2); /* num bytes that will fit */
3183 /* construct "m"<memaddr>","<len>" */
3184 /* sprintf (buf, "m%lx,%x", (unsigned long) memaddr, todo); */
3185 memaddr
= remote_address_masked (memaddr
);
3188 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
3190 p
+= hexnumstr (p
, (ULONGEST
) todo
);
3198 /* There is no correspondance between what the remote protocol uses
3199 for errors and errno codes. We would like a cleaner way of
3200 representing errors (big enough to include errno codes, bfd_error
3201 codes, and others). But for now just return EIO. */
3206 /* Reply describes memory byte by byte,
3207 each byte encoded as two hex characters. */
3210 for (i
= 0; i
< todo
; i
++)
3212 if (p
[0] == 0 || p
[1] == 0)
3213 /* Reply is short. This means that we were able to read
3214 only part of what we wanted to. */
3215 return i
+ (origlen
- len
);
3216 myaddr
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
3226 /* Read or write LEN bytes from inferior memory at MEMADDR,
3227 transferring to or from debugger address BUFFER. Write to inferior if
3228 SHOULD_WRITE is nonzero. Returns length of data written or read; 0
3231 #ifndef REMOTE_TRANSLATE_XFER_ADDRESS
3232 #define REMOTE_TRANSLATE_XFER_ADDRESS(MEM_ADDR, MEM_LEN, TARG_ADDR, TARG_LEN) \
3233 (*(TARG_ADDR) = (MEM_ADDR), *(TARG_LEN) = (MEM_LEN))
3238 remote_xfer_memory (mem_addr
, buffer
, mem_len
, should_write
, target
)
3243 struct target_ops
*target
; /* ignored */
3245 CORE_ADDR targ_addr
;
3247 REMOTE_TRANSLATE_XFER_ADDRESS (mem_addr
, mem_len
, &targ_addr
, &targ_len
);
3251 return dcache_xfer_memory (remote_dcache
, targ_addr
, buffer
,
3252 targ_len
, should_write
);
3257 /* Enable after 4.12. */
3260 remote_search (len
, data
, mask
, startaddr
, increment
, lorange
, hirange
3261 addr_found
, data_found
)
3265 CORE_ADDR startaddr
;
3269 CORE_ADDR
*addr_found
;
3272 if (increment
== -4 && len
== 4)
3274 long mask_long
, data_long
;
3275 long data_found_long
;
3276 CORE_ADDR addr_we_found
;
3277 char *buf
= alloca (PBUFSIZ
);
3278 long returned_long
[2];
3281 mask_long
= extract_unsigned_integer (mask
, len
);
3282 data_long
= extract_unsigned_integer (data
, len
);
3283 sprintf (buf
, "t%x:%x,%x", startaddr
, data_long
, mask_long
);
3288 /* The stub doesn't support the 't' request. We might want to
3289 remember this fact, but on the other hand the stub could be
3290 switched on us. Maybe we should remember it only until
3291 the next "target remote". */
3292 generic_search (len
, data
, mask
, startaddr
, increment
, lorange
,
3293 hirange
, addr_found
, data_found
);
3298 /* There is no correspondance between what the remote protocol uses
3299 for errors and errno codes. We would like a cleaner way of
3300 representing errors (big enough to include errno codes, bfd_error
3301 codes, and others). But for now just use EIO. */
3302 memory_error (EIO
, startaddr
);
3305 while (*p
!= '\0' && *p
!= ',')
3306 addr_we_found
= (addr_we_found
<< 4) + fromhex (*p
++);
3308 error ("Protocol error: short return for search");
3310 data_found_long
= 0;
3311 while (*p
!= '\0' && *p
!= ',')
3312 data_found_long
= (data_found_long
<< 4) + fromhex (*p
++);
3313 /* Ignore anything after this comma, for future extensions. */
3315 if (addr_we_found
< lorange
|| addr_we_found
>= hirange
)
3321 *addr_found
= addr_we_found
;
3322 *data_found
= store_unsigned_integer (data_we_found
, len
);
3325 generic_search (len
, data
, mask
, startaddr
, increment
, lorange
,
3326 hirange
, addr_found
, data_found
);
3331 remote_files_info (ignore
)
3332 struct target_ops
*ignore
;
3334 puts_filtered ("Debugging a target over a serial line.\n");
3337 /* Stuff for dealing with the packets which are part of this protocol.
3338 See comment at top of file for details. */
3340 /* Read a single character from the remote end, masking it down to 7 bits. */
3348 ch
= SERIAL_READCHAR (remote_desc
, timeout
);
3353 error ("Remote connection closed");
3355 perror_with_name ("Remote communication error");
3356 case SERIAL_TIMEOUT
:
3363 /* Send the command in BUF to the remote machine, and read the reply
3364 into BUF. Report an error if we get an error reply. */
3374 error ("Remote failure reply: %s", buf
);
3377 /* Display a null-terminated packet on stdout, for debugging, using C
3384 puts_filtered ("\"");
3385 fputstr_filtered (buf
, '"', gdb_stdout
);
3386 puts_filtered ("\"");
3393 return putpkt_binary (buf
, strlen (buf
));
3396 /* Send a packet to the remote machine, with error checking. The data
3397 of the packet is in BUF. The string in BUF can be at most PBUFSIZ - 5
3398 to account for the $, # and checksum, and for a possible /0 if we are
3399 debugging (remote_debug) and want to print the sent packet as a string */
3402 putpkt_binary (buf
, cnt
)
3407 unsigned char csum
= 0;
3408 char *buf2
= alloca (PBUFSIZ
);
3409 char *junkbuf
= alloca (PBUFSIZ
);
3415 /* Copy the packet into buffer BUF2, encapsulating it
3416 and giving it a checksum. */
3418 if (cnt
> BUFSIZ
- 5) /* Prosanity check */
3424 for (i
= 0; i
< cnt
; i
++)
3430 *p
++ = tohex ((csum
>> 4) & 0xf);
3431 *p
++ = tohex (csum
& 0xf);
3433 /* Send it over and over until we get a positive ack. */
3437 int started_error_output
= 0;
3442 fprintf_unfiltered (gdb_stdlog
, "Sending packet: ");
3443 fputstrn_unfiltered (buf2
, p
- buf2
, 0, gdb_stdlog
);
3444 fprintf_unfiltered (gdb_stdlog
, "...");
3445 gdb_flush (gdb_stdlog
);
3447 if (SERIAL_WRITE (remote_desc
, buf2
, p
- buf2
))
3448 perror_with_name ("putpkt: write failed");
3450 /* read until either a timeout occurs (-2) or '+' is read */
3453 ch
= readchar (remote_timeout
);
3460 case SERIAL_TIMEOUT
:
3462 if (started_error_output
)
3464 putchar_unfiltered ('\n');
3465 started_error_output
= 0;
3474 fprintf_unfiltered (gdb_stdlog
, "Ack\n");
3476 case SERIAL_TIMEOUT
:
3480 break; /* Retransmit buffer */
3483 /* It's probably an old response, and we're out of sync.
3484 Just gobble up the packet and ignore it. */
3485 getpkt (junkbuf
, 0);
3486 continue; /* Now, go look for + */
3491 if (!started_error_output
)
3493 started_error_output
= 1;
3494 fprintf_unfiltered (gdb_stdlog
, "putpkt: Junk: ");
3496 fputc_unfiltered (ch
& 0177, gdb_stdlog
);
3500 break; /* Here to retransmit */
3504 /* This is wrong. If doing a long backtrace, the user should be
3505 able to get out next time we call QUIT, without anything as
3506 violent as interrupt_query. If we want to provide a way out of
3507 here without getting to the next QUIT, it should be based on
3508 hitting ^C twice as in remote_wait. */
3518 static int remote_cisco_mode
;
3520 /* Come here after finding the start of the frame. Collect the rest
3521 into BUF, verifying the checksum, length, and handling run-length
3522 compression. Returns 0 on any error, 1 on success. */
3537 c
= readchar (remote_timeout
);
3541 case SERIAL_TIMEOUT
:
3543 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog
);
3547 fputs_filtered ("Saw new packet start in middle of old one\n",
3549 return 0; /* Start a new packet, count retries */
3552 unsigned char pktcsum
;
3556 pktcsum
= fromhex (readchar (remote_timeout
)) << 4;
3557 pktcsum
|= fromhex (readchar (remote_timeout
));
3559 if (csum
== pktcsum
)
3564 fprintf_filtered (gdb_stdlog
,
3565 "Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
3567 fputs_filtered (buf
, gdb_stdlog
);
3568 fputs_filtered ("\n", gdb_stdlog
);
3572 case '*': /* Run length encoding */
3577 if (remote_cisco_mode
== 0)
3579 c
= readchar (remote_timeout
);
3581 repeat
= c
- ' ' + 3; /* Compute repeat count */
3585 /* Cisco's run-length encoding variant uses two
3586 hex chars to represent the repeat count. */
3588 c
= readchar (remote_timeout
);
3590 repeat
= fromhex (c
) << 4;
3591 c
= readchar (remote_timeout
);
3593 repeat
+= fromhex (c
);
3596 if (repeat
> 0 && repeat
<= 255
3597 && bp
+ repeat
- 1 < buf
+ PBUFSIZ
- 1)
3599 memset (bp
, *(bp
- 1), repeat
);
3605 printf_filtered ("Repeat count %d too large for buffer: ", repeat
);
3606 puts_filtered (buf
);
3607 puts_filtered ("\n");
3611 if (bp
< buf
+ PBUFSIZ
- 1)
3619 puts_filtered ("Remote packet too long: ");
3620 puts_filtered (buf
);
3621 puts_filtered ("\n");
3628 /* Read a packet from the remote machine, with error checking, and
3629 store it in BUF. BUF is expected to be of size PBUFSIZ. If
3630 FOREVER, wait forever rather than timing out; this is used while
3631 the target is executing user code. */
3634 getpkt (buf
, forever
)
3643 strcpy (buf
, "timeout");
3647 timeout
= watchdog
> 0 ? watchdog
: -1;
3651 timeout
= remote_timeout
;
3655 for (tries
= 1; tries
<= MAX_TRIES
; tries
++)
3657 /* This can loop forever if the remote side sends us characters
3658 continuously, but if it pauses, we'll get a zero from readchar
3659 because of timeout. Then we'll count that as a retry. */
3661 /* Note that we will only wait forever prior to the start of a packet.
3662 After that, we expect characters to arrive at a brisk pace. They
3663 should show up within remote_timeout intervals. */
3667 c
= readchar (timeout
);
3669 if (c
== SERIAL_TIMEOUT
)
3671 if (forever
) /* Watchdog went off. Kill the target. */
3673 target_mourn_inferior ();
3674 error ("Watchdog has expired. Target detached.\n");
3677 fputs_filtered ("Timed out.\n", gdb_stdlog
);
3683 /* We've found the start of a packet, now collect the data. */
3685 val
= read_frame (buf
);
3691 fprintf_unfiltered (gdb_stdlog
, "Packet received: ");
3692 fputstr_unfiltered (buf
, 0, gdb_stdlog
);
3693 fprintf_unfiltered (gdb_stdlog
, "\n");
3695 SERIAL_WRITE (remote_desc
, "+", 1);
3699 /* Try the whole thing again. */
3701 SERIAL_WRITE (remote_desc
, "-", 1);
3704 /* We have tried hard enough, and just can't receive the packet. Give up. */
3706 printf_unfiltered ("Ignoring packet error, continuing...\n");
3707 SERIAL_WRITE (remote_desc
, "+", 1);
3713 /* For some mysterious reason, wait_for_inferior calls kill instead of
3714 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
3718 target_mourn_inferior ();
3722 /* Use catch_errors so the user can quit from gdb even when we aren't on
3723 speaking terms with the remote system. */
3724 catch_errors ((catch_errors_ftype
*) putpkt
, "k", "", RETURN_MASK_ERROR
);
3726 /* Don't wait for it to die. I'm not really sure it matters whether
3727 we do or not. For the existing stubs, kill is a noop. */
3728 target_mourn_inferior ();
3731 /* Async version of remote_kill. */
3733 remote_async_kill ()
3735 /* Unregister the file descriptor from the event loop. */
3736 if (SERIAL_IS_ASYNC_P (remote_desc
))
3737 SERIAL_ASYNC (remote_desc
, NULL
, 0);
3739 /* For some mysterious reason, wait_for_inferior calls kill instead of
3740 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
3744 target_mourn_inferior ();
3748 /* Use catch_errors so the user can quit from gdb even when we aren't on
3749 speaking terms with the remote system. */
3750 catch_errors ((catch_errors_ftype
*) putpkt
, "k", "", RETURN_MASK_ERROR
);
3752 /* Don't wait for it to die. I'm not really sure it matters whether
3753 we do or not. For the existing stubs, kill is a noop. */
3754 target_mourn_inferior ();
3760 remote_mourn_1 (&remote_ops
);
3764 remote_async_mourn ()
3766 remote_mourn_1 (&remote_async_ops
);
3770 extended_remote_mourn ()
3772 /* We do _not_ want to mourn the target like this; this will
3773 remove the extended remote target from the target stack,
3774 and the next time the user says "run" it'll fail.
3776 FIXME: What is the right thing to do here? */
3778 remote_mourn_1 (&extended_remote_ops
);
3782 /* Worker function for remote_mourn. */
3784 remote_mourn_1 (target
)
3785 struct target_ops
*target
;
3787 unpush_target (target
);
3788 generic_mourn_inferior ();
3791 /* In the extended protocol we want to be able to do things like
3792 "run" and have them basically work as expected. So we need
3793 a special create_inferior function.
3795 FIXME: One day add support for changing the exec file
3796 we're debugging, arguments and an environment. */
3799 extended_remote_create_inferior (exec_file
, args
, env
)
3804 /* Rip out the breakpoints; we'll reinsert them after restarting
3805 the remote server. */
3806 remove_breakpoints ();
3808 /* Now restart the remote server. */
3809 extended_remote_restart ();
3811 /* Now put the breakpoints back in. This way we're safe if the
3812 restart function works via a unix fork on the remote side. */
3813 insert_breakpoints ();
3815 /* Clean up from the last time we were running. */
3816 clear_proceed_status ();
3818 /* Let the remote process run. */
3819 proceed (-1, TARGET_SIGNAL_0
, 0);
3822 /* Async version of extended_remote_create_inferior. */
3824 extended_remote_async_create_inferior (exec_file
, args
, env
)
3829 /* Rip out the breakpoints; we'll reinsert them after restarting
3830 the remote server. */
3831 remove_breakpoints ();
3833 /* If running asynchronously, register the target file descriptor
3834 with the event loop. */
3835 if (async_p
&& SERIAL_CAN_ASYNC_P (remote_desc
))
3836 SERIAL_ASYNC (remote_desc
, inferior_event_handler
, 0);
3838 /* Now restart the remote server. */
3839 extended_remote_restart ();
3841 /* Now put the breakpoints back in. This way we're safe if the
3842 restart function works via a unix fork on the remote side. */
3843 insert_breakpoints ();
3845 /* Clean up from the last time we were running. */
3846 clear_proceed_status ();
3848 /* Let the remote process run. */
3849 proceed (-1, TARGET_SIGNAL_0
, 0);
3853 /* On some machines, e.g. 68k, we may use a different breakpoint instruction
3854 than other targets; in those use REMOTE_BREAKPOINT instead of just
3855 BREAKPOINT. Also, bi-endian targets may define LITTLE_REMOTE_BREAKPOINT
3856 and BIG_REMOTE_BREAKPOINT. If none of these are defined, we just call
3857 the standard routines that are in mem-break.c. */
3859 /* FIXME, these ought to be done in a more dynamic fashion. For instance,
3860 the choice of breakpoint instruction affects target program design and
3861 vice versa, and by making it user-tweakable, the special code here
3862 goes away and we need fewer special GDB configurations. */
3864 #if defined (LITTLE_REMOTE_BREAKPOINT) && defined (BIG_REMOTE_BREAKPOINT) && !defined(REMOTE_BREAKPOINT)
3865 #define REMOTE_BREAKPOINT
3868 #ifdef REMOTE_BREAKPOINT
3870 /* If the target isn't bi-endian, just pretend it is. */
3871 #if !defined (LITTLE_REMOTE_BREAKPOINT) && !defined (BIG_REMOTE_BREAKPOINT)
3872 #define LITTLE_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
3873 #define BIG_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
3876 static unsigned char big_break_insn
[] = BIG_REMOTE_BREAKPOINT
;
3877 static unsigned char little_break_insn
[] = LITTLE_REMOTE_BREAKPOINT
;
3879 #endif /* REMOTE_BREAKPOINT */
3881 /* Insert a breakpoint on targets that don't have any better breakpoint
3882 support. We read the contents of the target location and stash it,
3883 then overwrite it with a breakpoint instruction. ADDR is the target
3884 location in the target machine. CONTENTS_CACHE is a pointer to
3885 memory allocated for saving the target contents. It is guaranteed
3886 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
3887 is accomplished via BREAKPOINT_MAX). */
3890 remote_insert_breakpoint (addr
, contents_cache
)
3892 char *contents_cache
;
3894 #ifdef REMOTE_BREAKPOINT
3899 /* Try the "Z" packet if it is not already disabled.
3900 If it succeeds, then set the support to PACKET_ENABLE.
3901 If it fails, and the user has explicitly requested the Z support
3902 then report an error, otherwise, mark it disabled and go on. */
3904 if ((remote_protocol_Z
.support
== PACKET_ENABLE
)
3905 || (remote_protocol_Z
.support
== PACKET_SUPPORT_UNKNOWN
))
3907 char buf
[PBUFSIZ
], *p
= buf
;
3909 addr
= remote_address_masked (addr
);
3913 p
+= hexnumstr (p
, (ULONGEST
) addr
);
3914 BREAKPOINT_FROM_PC (&addr
, &bp_size
);
3915 sprintf (p
, ",%d", bp_size
);
3922 remote_protocol_Z
.support
= PACKET_ENABLE
;
3923 return (buf
[0] == 'E');
3926 /* The stub does not support the 'Z' request. If the user has
3927 explicitly requested the Z support, or if the stub previously
3928 said it supported the packet, this is an error,
3929 otherwise, mark it disabled. */
3931 else if (remote_protocol_Z
.support
== PACKET_ENABLE
)
3933 error ("Protocol error: Z packet not recognized by stub");
3937 remote_protocol_Z
.support
= PACKET_DISABLE
;
3941 #ifdef REMOTE_BREAKPOINT
3942 val
= target_read_memory (addr
, contents_cache
, sizeof big_break_insn
);
3946 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
3947 val
= target_write_memory (addr
, (char *) big_break_insn
,
3948 sizeof big_break_insn
);
3950 val
= target_write_memory (addr
, (char *) little_break_insn
,
3951 sizeof little_break_insn
);
3956 return memory_insert_breakpoint (addr
, contents_cache
);
3957 #endif /* REMOTE_BREAKPOINT */
3961 remote_remove_breakpoint (addr
, contents_cache
)
3963 char *contents_cache
;
3967 if ((remote_protocol_Z
.support
== PACKET_ENABLE
)
3968 || (remote_protocol_Z
.support
== PACKET_SUPPORT_UNKNOWN
))
3970 char buf
[PBUFSIZ
], *p
= buf
;
3976 addr
= remote_address_masked (addr
);
3977 p
+= hexnumstr (p
, (ULONGEST
) addr
);
3978 BREAKPOINT_FROM_PC (&addr
, &bp_size
);
3979 sprintf (p
, ",%d", bp_size
);
3984 return (buf
[0] == 'E');
3987 #ifdef REMOTE_BREAKPOINT
3988 return target_write_memory (addr
, contents_cache
, sizeof big_break_insn
);
3990 return memory_remove_breakpoint (addr
, contents_cache
);
3991 #endif /* REMOTE_BREAKPOINT */
3994 #ifdef TARGET_HAS_HARDWARE_WATCHPOINTS
3996 remote_insert_watchpoint (addr
, len
, type
)
4001 char buf
[PBUFSIZ
], *p
;
4003 if (remote_protocol_Z
.support
== PACKET_DISABLE
)
4004 error ("Can't set hardware watchpoints without the 'Z' packet\n");
4006 sprintf (buf
, "Z%x,", type
+ 2 );
4007 p
= strchr (buf
, '\0');
4008 addr
= remote_address_masked (addr
);
4009 p
+= hexnumstr (p
, (ULONGEST
) addr
);
4010 sprintf (p
, ",%x", len
);
4015 if (buf
[0] == '\0' || buf
[0] == 'E')
4022 remote_remove_watchpoint (addr
, len
, type
)
4027 char buf
[PBUFSIZ
], *p
;
4029 sprintf (buf
, "z%x,", type
+ 2 );
4030 p
= strchr (buf
, '\0');
4031 addr
= remote_address_masked (addr
);
4032 p
+= hexnumstr (p
, (ULONGEST
) addr
);
4033 sprintf (p
, ",%x", len
);
4037 if (buf
[0] == '\0' || buf
[0] == 'E')
4044 remote_insert_hw_breakpoint (addr
, len
)
4048 char buf
[PBUFSIZ
], *p
= buf
;
4050 if (remote_protocol_Z
.support
== PACKET_DISABLE
)
4051 error ("Can't set hardware breakpoints without the 'Z' packet\n");
4057 addr
= remote_address_masked (addr
);
4058 p
+= hexnumstr (p
, (ULONGEST
) addr
);
4064 if (buf
[0] == '\0' || buf
[0] == 'E')
4071 remote_remove_hw_breakpoint (addr
, len
)
4075 char buf
[PBUFSIZ
], *p
= buf
;
4081 addr
= remote_address_masked (addr
);
4082 p
+= hexnumstr (p
, (ULONGEST
) addr
);
4088 if (buf
[0] == '\0' || buf
[0] == 'E')
4095 /* Some targets are only capable of doing downloads, and afterwards
4096 they switch to the remote serial protocol. This function provides
4097 a clean way to get from the download target to the remote target.
4098 It's basically just a wrapper so that we don't have to expose any
4099 of the internal workings of remote.c.
4101 Prior to calling this routine, you should shutdown the current
4102 target code, else you will get the "A program is being debugged
4103 already..." message. Usually a call to pop_target() suffices. */
4106 push_remote_target (name
, from_tty
)
4110 printf_filtered ("Switching to remote protocol\n");
4111 remote_open (name
, from_tty
);
4114 /* Other targets want to use the entire remote serial module but with
4115 certain remote_ops overridden. */
4118 open_remote_target (name
, from_tty
, target
, extended_p
)
4121 struct target_ops
*target
;
4124 printf_filtered ("Selecting the %sremote protocol\n",
4125 (extended_p
? "extended-" : ""));
4126 remote_open_1 (name
, from_tty
, target
, extended_p
);
4129 /* Table used by the crc32 function to calcuate the checksum. */
4131 static unsigned long crc32_table
[256] =
4134 static unsigned long
4135 crc32 (buf
, len
, crc
)
4140 if (!crc32_table
[1])
4142 /* Initialize the CRC table and the decoding table. */
4146 for (i
= 0; i
< 256; i
++)
4148 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
4149 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
4156 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ *buf
) & 255];
4162 /* compare-sections command
4164 With no arguments, compares each loadable section in the exec bfd
4165 with the same memory range on the target, and reports mismatches.
4166 Useful for verifying the image on the target against the exec file.
4167 Depends on the target understanding the new "qCRC:" request. */
4170 compare_sections_command (args
, from_tty
)
4175 unsigned long host_crc
, target_crc
;
4176 extern bfd
*exec_bfd
;
4177 struct cleanup
*old_chain
;
4181 char *buf
= alloca (PBUFSIZ
);
4188 error ("command cannot be used without an exec file");
4189 if (!current_target
.to_shortname
||
4190 strcmp (current_target
.to_shortname
, "remote") != 0)
4191 error ("command can only be used with remote target");
4193 for (s
= exec_bfd
->sections
; s
; s
= s
->next
)
4195 if (!(s
->flags
& SEC_LOAD
))
4196 continue; /* skip non-loadable section */
4198 size
= bfd_get_section_size_before_reloc (s
);
4200 continue; /* skip zero-length section */
4202 sectname
= (char *) bfd_get_section_name (exec_bfd
, s
);
4203 if (args
&& strcmp (args
, sectname
) != 0)
4204 continue; /* not the section selected by user */
4206 matched
= 1; /* do this section */
4208 /* FIXME: assumes lma can fit into long */
4209 sprintf (buf
, "qCRC:%lx,%lx", (long) lma
, (long) size
);
4212 /* be clever; compute the host_crc before waiting for target reply */
4213 sectdata
= xmalloc (size
);
4214 old_chain
= make_cleanup (free
, sectdata
);
4215 bfd_get_section_contents (exec_bfd
, s
, sectdata
, 0, size
);
4216 host_crc
= crc32 ((unsigned char *) sectdata
, size
, 0xffffffff);
4220 error ("target memory fault, section %s, range 0x%08x -- 0x%08x",
4221 sectname
, lma
, lma
+ size
);
4223 error ("remote target does not support this operation");
4225 for (target_crc
= 0, tmp
= &buf
[1]; *tmp
; tmp
++)
4226 target_crc
= target_crc
* 16 + fromhex (*tmp
);
4228 printf_filtered ("Section %s, range 0x%s -- 0x%s: ",
4229 sectname
, paddr (lma
), paddr (lma
+ size
));
4230 if (host_crc
== target_crc
)
4231 printf_filtered ("matched.\n");
4234 printf_filtered ("MIS-MATCHED!\n");
4238 do_cleanups (old_chain
);
4241 warning ("One or more sections of the remote executable does not match\n\
4242 the loaded file\n");
4243 if (args
&& !matched
)
4244 printf_filtered ("No loaded section named '%s'.\n", args
);
4248 remote_query (query_type
, buf
, outbuf
, bufsiz
)
4255 char *buf2
= alloca (PBUFSIZ
);
4256 char *p2
= &buf2
[0];
4259 error ("null pointer to remote bufer size specified");
4261 /* minimum outbuf size is PBUFSIZ - if bufsiz is not large enough let
4262 the caller know and return what the minimum size is */
4263 /* Note: a zero bufsiz can be used to query the minimum buffer size */
4264 if (*bufsiz
< PBUFSIZ
)
4270 /* except for querying the minimum buffer size, target must be open */
4272 error ("remote query is only available after target open");
4274 /* we only take uppercase letters as query types, at least for now */
4275 if ((query_type
< 'A') || (query_type
> 'Z'))
4276 error ("invalid remote query type");
4279 error ("null remote query specified");
4282 error ("remote query requires a buffer to receive data");
4289 /* we used one buffer char for the remote protocol q command and another
4290 for the query type. As the remote protocol encapsulation uses 4 chars
4291 plus one extra in case we are debugging (remote_debug),
4292 we have PBUFZIZ - 7 left to pack the query string */
4294 while (buf
[i
] && (i
< (PBUFSIZ
- 8)))
4296 /* bad caller may have sent forbidden characters */
4297 if ((!isprint (buf
[i
])) || (buf
[i
] == '$') || (buf
[i
] == '#'))
4298 error ("illegal characters in query string");
4306 error ("query larger than available buffer");
4318 remote_rcmd (char *command
,
4319 struct gdb_file
*outbuf
)
4322 char *buf
= alloca (PBUFSIZ
);
4326 error ("remote rcmd is only available after target open");
4328 /* Send a NULL command across as an empty command */
4329 if (command
== NULL
)
4332 /* The query prefix */
4333 strcpy (buf
, "qRcmd,");
4334 p
= strchr (buf
, '\0');
4336 if ((strlen (buf
) + strlen (command
) * 2 + 8/*misc*/) > PBUFSIZ
)
4337 error ("\"monitor\" command ``%s'' is too long\n", command
);
4339 /* Encode the actual command */
4340 for (i
= 0; command
[i
]; i
++)
4342 *p
++ = tohex ((command
[i
] >> 4) & 0xf);
4343 *p
++ = tohex (command
[i
] & 0xf);
4347 if (putpkt (buf
) < 0)
4348 error ("Communication problem with target\n");
4350 /* get/display the response */
4353 /* XXX - see also tracepoint.c:remote_get_noisy_reply() */
4357 error ("Target does not support this command\n");
4358 if (buf
[0] == 'O' && buf
[1] != 'K')
4360 remote_console_output (buf
+ 1); /* 'O' message from stub */
4363 if (strcmp (buf
, "OK") == 0)
4365 if (strlen (buf
) == 3 && buf
[0] == 'E'
4366 && isdigit (buf
[1]) && isdigit (buf
[2]))
4368 error ("Protocol error with Rcmd");
4370 for (p
= buf
; p
[0] != '\0' && p
[1] != '\0'; p
+= 2)
4372 char c
= (fromhex (p
[0]) << 4) + fromhex (p
[1]);
4373 fputc_unfiltered (c
, outbuf
);
4380 packet_command (args
, from_tty
)
4384 char *buf
= alloca (PBUFSIZ
);
4387 error ("command can only be used with remote target");
4390 error ("remote-packet command requires packet text as argument");
4392 puts_filtered ("sending: ");
4393 print_packet (args
);
4394 puts_filtered ("\n");
4398 puts_filtered ("received: ");
4400 puts_filtered ("\n");
4404 /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------------- */
4406 static void display_thread_info
PARAMS ((struct gdb_ext_thread_info
* info
));
4408 static void threadset_test_cmd
PARAMS ((char *cmd
, int tty
));
4410 static void threadalive_test
PARAMS ((char *cmd
, int tty
));
4412 static void threadlist_test_cmd
PARAMS ((char *cmd
, int tty
));
4414 int get_and_display_threadinfo
PARAMS ((threadref
* ref
));
4416 static void threadinfo_test_cmd
PARAMS ((char *cmd
, int tty
));
4418 static int thread_display_step
PARAMS ((threadref
* ref
, void *context
));
4420 static void threadlist_update_test_cmd
PARAMS ((char *cmd
, int tty
));
4422 static void init_remote_threadtests
PARAMS ((void));
4424 #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid */
4427 threadset_test_cmd (cmd
, tty
)
4431 int sample_thread
= SAMPLE_THREAD
;
4433 printf_filtered ("Remote threadset test\n");
4434 set_thread (sample_thread
, 1);
4439 threadalive_test (cmd
, tty
)
4443 int sample_thread
= SAMPLE_THREAD
;
4445 if (remote_thread_alive (sample_thread
))
4446 printf_filtered ("PASS: Thread alive test\n");
4448 printf_filtered ("FAIL: Thread alive test\n");
4451 void output_threadid
PARAMS ((char *title
, threadref
* ref
));
4454 output_threadid (title
, ref
)
4460 pack_threadid (&hexid
[0], ref
); /* Convert threead id into hex */
4462 printf_filtered ("%s %s\n", title
, (&hexid
[0]));
4466 threadlist_test_cmd (cmd
, tty
)
4471 threadref nextthread
;
4472 int done
, result_count
;
4473 threadref threadlist
[3];
4475 printf_filtered ("Remote Threadlist test\n");
4476 if (!remote_get_threadlist (startflag
, &nextthread
, 3, &done
,
4477 &result_count
, &threadlist
[0]))
4478 printf_filtered ("FAIL: threadlist test\n");
4481 threadref
*scan
= threadlist
;
4482 threadref
*limit
= scan
+ result_count
;
4484 while (scan
< limit
)
4485 output_threadid (" thread ", scan
++);
4490 display_thread_info (info
)
4491 struct gdb_ext_thread_info
*info
;
4493 output_threadid ("Threadid: ", &info
->threadid
);
4494 printf_filtered ("Name: %s\n ", info
->shortname
);
4495 printf_filtered ("State: %s\n", info
->display
);
4496 printf_filtered ("other: %s\n\n", info
->more_display
);
4500 get_and_display_threadinfo (ref
)
4505 struct gdb_ext_thread_info threadinfo
;
4507 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
4508 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
4509 if (0 != (result
= remote_get_threadinfo (ref
, set
, &threadinfo
)))
4510 display_thread_info (&threadinfo
);
4515 threadinfo_test_cmd (cmd
, tty
)
4519 int athread
= SAMPLE_THREAD
;
4523 int_to_threadref (&thread
, athread
);
4524 printf_filtered ("Remote Threadinfo test\n");
4525 if (!get_and_display_threadinfo (&thread
))
4526 printf_filtered ("FAIL cannot get thread info\n");
4530 thread_display_step (ref
, context
)
4534 /* output_threadid(" threadstep ",ref); *//* simple test */
4535 return get_and_display_threadinfo (ref
);
4539 threadlist_update_test_cmd (cmd
, tty
)
4543 printf_filtered ("Remote Threadlist update test\n");
4544 remote_threadlist_iterator (thread_display_step
, 0, CRAZY_MAX_THREADS
);
4548 init_remote_threadtests (void)
4550 add_com ("tlist", class_obscure
, threadlist_test_cmd
,
4551 "Fetch and print the remote list of thread identifiers, one pkt only");
4552 add_com ("tinfo", class_obscure
, threadinfo_test_cmd
,
4553 "Fetch and display info about one thread");
4554 add_com ("tset", class_obscure
, threadset_test_cmd
,
4555 "Test setting to a different thread");
4556 add_com ("tupd", class_obscure
, threadlist_update_test_cmd
,
4557 "Iterate through updating all remote thread info");
4558 add_com ("talive", class_obscure
, threadalive_test
,
4559 " Remote thread alive test ");
4567 remote_ops
.to_shortname
= "remote";
4568 remote_ops
.to_longname
= "Remote serial target in gdb-specific protocol";
4570 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
4571 Specify the serial device it is connected to (e.g. /dev/ttya).";
4572 remote_ops
.to_open
= remote_open
;
4573 remote_ops
.to_close
= remote_close
;
4574 remote_ops
.to_detach
= remote_detach
;
4575 remote_ops
.to_resume
= remote_resume
;
4576 remote_ops
.to_wait
= remote_wait
;
4577 remote_ops
.to_fetch_registers
= remote_fetch_registers
;
4578 remote_ops
.to_store_registers
= remote_store_registers
;
4579 remote_ops
.to_prepare_to_store
= remote_prepare_to_store
;
4580 remote_ops
.to_xfer_memory
= remote_xfer_memory
;
4581 remote_ops
.to_files_info
= remote_files_info
;
4582 remote_ops
.to_insert_breakpoint
= remote_insert_breakpoint
;
4583 remote_ops
.to_remove_breakpoint
= remote_remove_breakpoint
;
4584 remote_ops
.to_kill
= remote_kill
;
4585 remote_ops
.to_load
= generic_load
;
4586 remote_ops
.to_mourn_inferior
= remote_mourn
;
4587 remote_ops
.to_thread_alive
= remote_thread_alive
;
4588 remote_ops
.to_find_new_threads
= remote_threads_info
;
4589 remote_ops
.to_stop
= remote_stop
;
4590 remote_ops
.to_query
= remote_query
;
4591 remote_ops
.to_rcmd
= remote_rcmd
;
4592 remote_ops
.to_stratum
= process_stratum
;
4593 remote_ops
.to_has_all_memory
= 1;
4594 remote_ops
.to_has_memory
= 1;
4595 remote_ops
.to_has_stack
= 1;
4596 remote_ops
.to_has_registers
= 1;
4597 remote_ops
.to_has_execution
= 1;
4598 remote_ops
.to_has_thread_control
= tc_schedlock
; /* can lock scheduler */
4599 remote_ops
.to_magic
= OPS_MAGIC
;
4602 /* Set up the extended remote vector by making a copy of the standard
4603 remote vector and adding to it. */
4606 init_extended_remote_ops ()
4608 extended_remote_ops
= remote_ops
;
4610 extended_remote_ops
.to_shortname
= "extended-remote";
4611 extended_remote_ops
.to_longname
=
4612 "Extended remote serial target in gdb-specific protocol";
4613 extended_remote_ops
.to_doc
=
4614 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
4615 Specify the serial device it is connected to (e.g. /dev/ttya).",
4616 extended_remote_ops
.to_open
= extended_remote_open
;
4617 extended_remote_ops
.to_create_inferior
= extended_remote_create_inferior
;
4618 extended_remote_ops
.to_mourn_inferior
= extended_remote_mourn
;
4622 * Command: info remote-process
4624 * This implements Cisco's version of the "info proc" command.
4626 * This query allows the target stub to return an arbitrary string
4627 * (or strings) giving arbitrary information about the target process.
4628 * This is optional; the target stub isn't required to implement it.
4630 * Syntax: qfProcessInfo request first string
4631 * qsProcessInfo request subsequent string
4632 * reply: 'O'<hex-encoded-string>
4633 * 'l' last reply (empty)
4637 remote_info_process (args
, from_tty
)
4641 char *buf
= alloca (PBUFSIZ
);
4643 if (remote_desc
== 0)
4644 error ("Command can only be used when connected to the remote target.");
4646 putpkt ("qfProcessInfo");
4649 return; /* Silently: target does not support this feature. */
4652 error ("info proc: target error.");
4654 while (buf
[0] == 'O') /* Capitol-O packet */
4656 remote_console_output (&buf
[1]);
4657 putpkt ("qsProcessInfo");
4667 remote_cisco_open (name
, from_tty
)
4673 "To open a remote debug connection, you need to specify what \n\
4674 device is attached to the remote system (e.g. host:port).");
4676 target_preopen (from_tty
);
4678 unpush_target (&remote_cisco_ops
);
4680 remote_dcache
= dcache_init (remote_read_bytes
, remote_write_bytes
);
4682 remote_desc
= SERIAL_OPEN (name
);
4684 perror_with_name (name
);
4687 * If a baud rate was specified on the gdb command line it will
4688 * be greater than the initial value of -1. If it is, use it otherwise
4692 baud_rate
= (baud_rate
> 0) ? baud_rate
: 9600;
4693 if (SERIAL_SETBAUDRATE (remote_desc
, baud_rate
))
4695 SERIAL_CLOSE (remote_desc
);
4696 perror_with_name (name
);
4699 SERIAL_RAW (remote_desc
);
4701 /* If there is something sitting in the buffer we might take it as a
4702 response to a command, which would be bad. */
4703 SERIAL_FLUSH_INPUT (remote_desc
);
4707 puts_filtered ("Remote debugging using ");
4708 puts_filtered (name
);
4709 puts_filtered ("\n");
4712 remote_cisco_mode
= 1;
4714 push_target (&remote_cisco_ops
); /* Switch to using cisco target now */
4716 init_packet_config (&remote_protocol_P
);
4717 init_packet_config (&remote_protocol_Z
);
4719 general_thread
= -2;
4720 continue_thread
= -2;
4722 /* Force remote_write_bytes to check whether target supports
4723 binary downloading. */
4724 init_packet_config (&remote_protocol_binary_download
);
4726 /* Without this, some commands which require an active target (such
4727 as kill) won't work. This variable serves (at least) double duty
4728 as both the pid of the target process (if it has such), and as a
4729 flag indicating that a target is active. These functions should
4730 be split out into seperate variables, especially since GDB will
4731 someday have a notion of debugging several processes. */
4732 inferior_pid
= MAGIC_NULL_PID
;
4734 /* Start the remote connection; if error (0), discard this target. */
4736 if (!catch_errors (remote_start_remote_dummy
, (char *) 0,
4737 "Couldn't establish connection to remote target\n",
4746 remote_cisco_close (quitting
)
4749 remote_cisco_mode
= 0;
4750 remote_close (quitting
);
4757 remote_mourn_1 (&remote_cisco_ops
);
4769 /* shared between readsocket() and readtty() */
4770 static char *tty_input
;
4772 static int escape_count
;
4773 static int echo_check
;
4774 extern int quit_flag
;
4781 /* Loop until the socket doesn't have any more data */
4783 while ((data
= readchar (0)) >= 0)
4785 /* Check for the escape sequence */
4788 /* If this is the fourth escape, get out */
4789 if (++escape_count
== 4)
4794 { /* This is a '|', but not the fourth in a row.
4795 Continue without echoing it. If it isn't actually
4796 one of four in a row, it'll be echoed later. */
4803 /* Ensure any pending '|'s are flushed. */
4805 for (; escape_count
> 0; escape_count
--)
4809 if (data
== '\r') /* If this is a return character, */
4810 continue; /* - just supress it. */
4812 if (echo_check
!= -1) /* Check for echo of user input. */
4814 if (tty_input
[echo_check
] == data
)
4816 echo_check
++; /* Character matched user input: */
4817 continue; /* Continue without echoing it. */
4819 else if ((data
== '\n') && (tty_input
[echo_check
] == '\r'))
4820 { /* End of the line (and of echo checking). */
4821 echo_check
= -1; /* No more echo supression */
4822 continue; /* Continue without echoing. */
4825 { /* Failed check for echo of user input.
4826 We now have some suppressed output to flush! */
4829 for (j
= 0; j
< echo_check
; j
++)
4830 putchar (tty_input
[j
]);
4834 putchar (data
); /* Default case: output the char. */
4837 if (data
== SERIAL_TIMEOUT
) /* Timeout returned from readchar. */
4838 return READ_MORE
; /* Try to read some more */
4840 return FATAL_ERROR
; /* Trouble, bail out */
4848 /* First, read a buffer full from the terminal */
4849 tty_bytecount
= read (fileno (stdin
), tty_input
, sizeof (tty_input
) - 1);
4850 if (tty_bytecount
== -1)
4852 perror ("readtty: read failed");
4856 /* Remove a quoted newline. */
4857 if (tty_input
[tty_bytecount
- 1] == '\n' &&
4858 tty_input
[tty_bytecount
- 2] == '\\') /* line ending in backslash */
4860 tty_input
[--tty_bytecount
] = 0; /* remove newline */
4861 tty_input
[--tty_bytecount
] = 0; /* remove backslash */
4864 /* Turn trailing newlines into returns */
4865 if (tty_input
[tty_bytecount
- 1] == '\n')
4866 tty_input
[tty_bytecount
- 1] = '\r';
4868 /* If the line consists of a ~, enter debugging mode. */
4869 if ((tty_input
[0] == '~') && (tty_bytecount
== 2))
4872 /* Make this a zero terminated string and write it out */
4873 tty_input
[tty_bytecount
] = 0;
4874 if (SERIAL_WRITE (remote_desc
, tty_input
, tty_bytecount
))
4876 perror_with_name ("readtty: write failed");
4886 fd_set input
; /* file descriptors for select */
4887 int tablesize
; /* max number of FDs for select */
4891 extern int escape_count
; /* global shared by readsocket */
4892 extern int echo_check
; /* ditto */
4897 tablesize
= 8 * sizeof (input
);
4901 /* Check for anything from our socket - doesn't block. Note that
4902 this must be done *before* the select as there may be
4903 buffered I/O waiting to be processed. */
4905 if ((status
= readsocket ()) == FATAL_ERROR
)
4907 error ("Debugging terminated by communications error");
4909 else if (status
!= READ_MORE
)
4914 fflush (stdout
); /* Flush output before blocking */
4916 /* Now block on more socket input or TTY input */
4919 FD_SET (fileno (stdin
), &input
);
4920 FD_SET (DEPRECATED_SERIAL_FD (remote_desc
), &input
);
4922 status
= select (tablesize
, &input
, 0, 0, 0);
4923 if ((status
== -1) && (errno
!= EINTR
))
4925 error ("Communications error on select %d", errno
);
4928 /* Handle Control-C typed */
4932 if ((++quit_count
) == 2)
4934 if (query ("Interrupt GDB? "))
4936 printf_filtered ("Interrupted by user.\n");
4937 return_to_top_level (RETURN_QUIT
);
4944 SERIAL_SEND_BREAK (remote_desc
);
4946 SERIAL_WRITE (remote_desc
, "\003", 1);
4951 /* Handle console input */
4953 if (FD_ISSET (fileno (stdin
), &input
))
4957 status
= readtty ();
4958 if (status
== READ_MORE
)
4961 return status
; /* telnet session ended */
4967 remote_cisco_wait (pid
, status
)
4969 struct target_waitstatus
*status
;
4971 if (minitelnet () != ENTER_DEBUG
)
4973 error ("Debugging session terminated by protocol error");
4976 return remote_wait (pid
, status
);
4980 init_remote_cisco_ops ()
4982 remote_cisco_ops
.to_shortname
= "cisco";
4983 remote_cisco_ops
.to_longname
= "Remote serial target in cisco-specific protocol";
4984 remote_cisco_ops
.to_doc
=
4985 "Use a remote machine via TCP, using a cisco-specific protocol.\n\
4986 Specify the serial device it is connected to (e.g. host:2020).";
4987 remote_cisco_ops
.to_open
= remote_cisco_open
;
4988 remote_cisco_ops
.to_close
= remote_cisco_close
;
4989 remote_cisco_ops
.to_detach
= remote_detach
;
4990 remote_cisco_ops
.to_resume
= remote_resume
;
4991 remote_cisco_ops
.to_wait
= remote_cisco_wait
;
4992 remote_cisco_ops
.to_fetch_registers
= remote_fetch_registers
;
4993 remote_cisco_ops
.to_store_registers
= remote_store_registers
;
4994 remote_cisco_ops
.to_prepare_to_store
= remote_prepare_to_store
;
4995 remote_cisco_ops
.to_xfer_memory
= remote_xfer_memory
;
4996 remote_cisco_ops
.to_files_info
= remote_files_info
;
4997 remote_cisco_ops
.to_insert_breakpoint
= remote_insert_breakpoint
;
4998 remote_cisco_ops
.to_remove_breakpoint
= remote_remove_breakpoint
;
4999 remote_cisco_ops
.to_kill
= remote_kill
;
5000 remote_cisco_ops
.to_load
= generic_load
;
5001 remote_cisco_ops
.to_mourn_inferior
= remote_cisco_mourn
;
5002 remote_cisco_ops
.to_thread_alive
= remote_thread_alive
;
5003 remote_cisco_ops
.to_find_new_threads
= remote_threads_info
;
5004 remote_cisco_ops
.to_stratum
= process_stratum
;
5005 remote_cisco_ops
.to_has_all_memory
= 1;
5006 remote_cisco_ops
.to_has_memory
= 1;
5007 remote_cisco_ops
.to_has_stack
= 1;
5008 remote_cisco_ops
.to_has_registers
= 1;
5009 remote_cisco_ops
.to_has_execution
= 1;
5010 remote_cisco_ops
.to_magic
= OPS_MAGIC
;
5013 /* Target async and target extended-async.
5015 This are temporary targets, until it is all tested. Eventually
5016 async support will be incorporated int the usual 'remote'
5020 init_remote_async_ops ()
5022 remote_async_ops
.to_shortname
= "async";
5023 remote_async_ops
.to_longname
= "Remote serial target in async version of the gdb-specific protocol";
5024 remote_async_ops
.to_doc
=
5025 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5026 Specify the serial device it is connected to (e.g. /dev/ttya).";
5027 remote_async_ops
.to_open
= remote_async_open
;
5028 remote_async_ops
.to_close
= remote_close
;
5029 remote_async_ops
.to_detach
= remote_async_detach
;
5030 remote_async_ops
.to_resume
= remote_async_resume
;
5031 remote_async_ops
.to_wait
= remote_async_wait
;
5032 remote_async_ops
.to_fetch_registers
= remote_fetch_registers
;
5033 remote_async_ops
.to_store_registers
= remote_store_registers
;
5034 remote_async_ops
.to_prepare_to_store
= remote_prepare_to_store
;
5035 remote_async_ops
.to_xfer_memory
= remote_xfer_memory
;
5036 remote_async_ops
.to_files_info
= remote_files_info
;
5037 remote_async_ops
.to_insert_breakpoint
= remote_insert_breakpoint
;
5038 remote_async_ops
.to_remove_breakpoint
= remote_remove_breakpoint
;
5039 remote_async_ops
.to_kill
= remote_async_kill
;
5040 remote_async_ops
.to_load
= generic_load
;
5041 remote_async_ops
.to_mourn_inferior
= remote_async_mourn
;
5042 remote_async_ops
.to_thread_alive
= remote_thread_alive
;
5043 remote_async_ops
.to_find_new_threads
= remote_threads_info
;
5044 remote_async_ops
.to_stop
= remote_stop
;
5045 remote_async_ops
.to_query
= remote_query
;
5046 remote_async_ops
.to_rcmd
= remote_rcmd
;
5047 remote_async_ops
.to_stratum
= process_stratum
;
5048 remote_async_ops
.to_has_all_memory
= 1;
5049 remote_async_ops
.to_has_memory
= 1;
5050 remote_async_ops
.to_has_stack
= 1;
5051 remote_async_ops
.to_has_registers
= 1;
5052 remote_async_ops
.to_has_execution
= 1;
5053 remote_async_ops
.to_has_thread_control
= tc_schedlock
; /* can lock scheduler */
5054 remote_async_ops
.to_has_async_exec
= 1;
5055 remote_async_ops
.to_magic
= OPS_MAGIC
;
5058 /* Set up the async extended remote vector by making a copy of the standard
5059 remote vector and adding to it. */
5062 init_extended_async_remote_ops ()
5064 extended_async_remote_ops
= remote_async_ops
;
5066 extended_async_remote_ops
.to_shortname
= "extended-async";
5067 extended_async_remote_ops
.to_longname
=
5068 "Extended remote serial target in async gdb-specific protocol";
5069 extended_async_remote_ops
.to_doc
=
5070 "Use a remote computer via a serial line, using an async gdb-specific protocol.\n\
5071 Specify the serial device it is connected to (e.g. /dev/ttya).",
5072 extended_async_remote_ops
.to_open
= extended_remote_async_open
;
5073 extended_async_remote_ops
.to_create_inferior
= extended_remote_async_create_inferior
;
5074 extended_async_remote_ops
.to_mourn_inferior
= extended_remote_mourn
;
5078 set_remote_cmd (args
, from_tty
)
5087 build_remote_gdbarch_data ()
5089 tty_input
= xmalloc (PBUFSIZ
);
5093 _initialize_remote ()
5095 static struct cmd_list_element
*remote_set_cmdlist
;
5096 static struct cmd_list_element
*remote_show_cmdlist
;
5098 /* architecture specific data */
5099 build_remote_gdbarch_data ();
5100 register_gdbarch_swap (&tty_input
, sizeof (&tty_input
), NULL
);
5101 register_gdbarch_swap (NULL
, 0, build_remote_gdbarch_data
);
5103 /* runtime constants - we retain the value of remote_write_size
5104 across architecture swaps. */
5105 remote_write_size
= PBUFSIZ
;
5108 add_target (&remote_ops
);
5110 init_extended_remote_ops ();
5111 add_target (&extended_remote_ops
);
5113 init_remote_async_ops ();
5114 add_target (&remote_async_ops
);
5116 init_extended_async_remote_ops ();
5117 add_target (&extended_async_remote_ops
);
5119 init_remote_cisco_ops ();
5120 add_target (&remote_cisco_ops
);
5123 init_remote_threadtests ();
5126 add_prefix_cmd ("remote", class_maintenance
, set_remote_cmd
, "\
5127 Remote protocol specific variables\n\
5128 Configure various remote-protocol specific variables such as\n\
5129 the packets being used",
5130 &remote_set_cmdlist
, "set remote ",
5131 0/*allow-unknown*/, &setlist
);
5132 add_prefix_cmd ("remote", class_maintenance
, set_remote_cmd
, "\
5133 Remote protocol specific variables\n\
5134 Configure various remote-protocol specific variables such as\n\
5135 the packets being used",
5136 &remote_show_cmdlist
, "show remote ",
5137 0/*allow-unknown*/, &showlist
);
5139 add_cmd ("compare-sections", class_obscure
, compare_sections_command
,
5140 "Compare section data on target to the exec file.\n\
5141 Argument is a single section name (default: all loaded sections).",
5144 add_cmd ("packet", class_maintenance
, packet_command
,
5145 "Send an arbitrary packet to a remote target.\n\
5146 maintenance packet TEXT\n\
5147 If GDB is talking to an inferior via the GDB serial protocol, then\n\
5148 this command sends the string TEXT to the inferior, and displays the\n\
5149 response packet. GDB supplies the initial `$' character, and the\n\
5150 terminating `#' character and checksum.",
5154 (add_set_cmd ("remotetimeout", no_class
,
5155 var_integer
, (char *) &remote_timeout
,
5156 "Set timeout value for remote read.\n",
5161 (add_set_cmd ("remotebreak", no_class
,
5162 var_boolean
, (char *) &remote_break
,
5163 "Set whether to send break if interrupted.\n",
5168 (add_set_cmd ("remotewritesize", no_class
,
5169 var_integer
, (char *) &remote_write_size
,
5170 "Set the maximum number of bytes per memory write packet.\n",
5174 remote_address_size
= TARGET_PTR_BIT
;
5176 (add_set_cmd ("remoteaddresssize", class_obscure
,
5177 var_integer
, (char *) &remote_address_size
,
5178 "Set the maximum size of the address (in bits) \
5179 in a memory packet.\n",
5183 add_packet_config_cmd (&remote_protocol_binary_download
,
5184 "X", "binary-download",
5185 set_remote_protocol_binary_download_cmd
,
5186 show_remote_protocol_binary_download_cmd
,
5187 &remote_set_cmdlist
, &remote_show_cmdlist
);
5189 /* XXXX - should ``set remotebinarydownload'' be retained for
5192 (add_set_cmd ("remotebinarydownload", no_class
,
5193 var_boolean
, (char *) &remote_binary_download
,
5194 "Set binary downloads.\n", &setlist
),
5198 add_info ("remote-process", remote_info_process
,
5199 "Query the remote system for process info.");
5201 add_packet_config_cmd (&remote_protocol_P
, "P", "set-register",
5202 set_remote_protocol_P_packet_cmd
,
5203 show_remote_protocol_P_packet_cmd
,
5204 &remote_set_cmdlist
, &remote_show_cmdlist
);
5206 add_packet_config_cmd (&remote_protocol_Z
, "Z", "breakpoint",
5207 set_remote_protocol_Z_packet_cmd
,
5208 show_remote_protocol_Z_packet_cmd
,
5209 &remote_set_cmdlist
, &remote_show_cmdlist
);