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"
45 #include <sys/types.h>
48 #include "event-loop.h"
53 /* Prototypes for local functions */
54 static void initialize_sigint_signal_handler
PARAMS ((void));
55 static void handle_remote_sigint
PARAMS ((int));
56 static void handle_remote_sigint_twice
PARAMS ((int));
57 static void async_remote_interrupt
PARAMS ((gdb_client_data
));
58 static void async_remote_interrupt_twice
PARAMS ((gdb_client_data
));
60 static void set_extended_protocol
PARAMS ((struct continuation_arg
*));
62 static void build_remote_gdbarch_data
PARAMS ((void));
64 static int remote_write_bytes
PARAMS ((CORE_ADDR memaddr
,
65 char *myaddr
, int len
));
67 static int remote_read_bytes
PARAMS ((CORE_ADDR memaddr
,
68 char *myaddr
, int len
));
70 static void remote_files_info
PARAMS ((struct target_ops
* ignore
));
72 static int remote_xfer_memory
PARAMS ((CORE_ADDR memaddr
, char *myaddr
,
73 int len
, int should_write
,
74 struct target_ops
* target
));
76 static void remote_prepare_to_store
PARAMS ((void));
78 static void remote_fetch_registers
PARAMS ((int regno
));
80 static void remote_resume
PARAMS ((int pid
, int step
,
81 enum target_signal siggnal
));
82 static void remote_async_resume
PARAMS ((int pid
, int step
,
83 enum target_signal siggnal
));
85 static int remote_start_remote
PARAMS ((PTR
));
87 static void remote_open
PARAMS ((char *name
, int from_tty
));
88 static void remote_async_open
PARAMS ((char *name
, int from_tty
));
90 static void extended_remote_open
PARAMS ((char *name
, int from_tty
));
91 static void extended_remote_async_open
PARAMS ((char *name
, int from_tty
));
93 static void remote_open_1
PARAMS ((char *, int, struct target_ops
*,
95 static void remote_async_open_1
PARAMS ((char *, int, struct target_ops
*,
98 static void remote_close
PARAMS ((int quitting
));
100 static void remote_store_registers
PARAMS ((int regno
));
102 static void remote_mourn
PARAMS ((void));
103 static void remote_async_mourn
PARAMS ((void));
105 static void extended_remote_restart
PARAMS ((void));
107 static void extended_remote_mourn
PARAMS ((void));
109 static void extended_remote_create_inferior
PARAMS ((char *, char *, char **));
110 static void extended_remote_async_create_inferior
PARAMS ((char *, char *, char **));
112 static void remote_mourn_1
PARAMS ((struct target_ops
*));
114 static void remote_send
PARAMS ((char *buf
));
116 static int readchar
PARAMS ((int timeout
));
118 static int remote_wait
PARAMS ((int pid
, struct target_waitstatus
* status
));
119 static int remote_async_wait
PARAMS ((int pid
, struct target_waitstatus
* status
));
121 static void remote_kill
PARAMS ((void));
122 static void remote_async_kill
PARAMS ((void));
124 static int tohex
PARAMS ((int nib
));
126 static void remote_detach
PARAMS ((char *args
, int from_tty
));
127 static void remote_async_detach
PARAMS ((char *args
, int from_tty
));
129 static void remote_interrupt
PARAMS ((int signo
));
131 static void remote_interrupt_twice
PARAMS ((int signo
));
133 static void interrupt_query
PARAMS ((void));
135 static void set_thread
PARAMS ((int, int));
137 static int remote_thread_alive
PARAMS ((int));
139 static void get_offsets
PARAMS ((void));
141 static int read_frame
PARAMS ((char *));
143 static int remote_insert_breakpoint
PARAMS ((CORE_ADDR
, char *));
145 static int remote_remove_breakpoint
PARAMS ((CORE_ADDR
, char *));
147 static int hexnumlen
PARAMS ((ULONGEST num
));
149 static void init_remote_ops
PARAMS ((void));
151 static void init_extended_remote_ops
PARAMS ((void));
153 static void init_remote_cisco_ops
PARAMS ((void));
155 static struct target_ops remote_cisco_ops
;
157 static void remote_stop
PARAMS ((void));
159 static int ishex
PARAMS ((int ch
, int *val
));
161 static int stubhex
PARAMS ((int ch
));
163 static int remote_query
PARAMS ((int /*char */ , char *, char *, int *));
165 static int hexnumstr
PARAMS ((char *, ULONGEST
));
167 static CORE_ADDR remote_address_masked
PARAMS ((CORE_ADDR
));
169 static void print_packet
PARAMS ((char *));
171 static unsigned long crc32
PARAMS ((unsigned char *, int, unsigned int));
173 static void compare_sections_command
PARAMS ((char *, int));
175 static void packet_command
PARAMS ((char *, int));
177 static int stub_unpack_int
PARAMS ((char *buff
, int fieldlength
));
179 static int remote_current_thread
PARAMS ((int oldpid
));
181 static void remote_find_new_threads
PARAMS ((void));
183 static void record_currthread
PARAMS ((int currthread
));
185 /* exported functions */
187 extern int fromhex
PARAMS ((int a
));
189 extern void getpkt
PARAMS ((char *buf
, int forever
));
191 extern int putpkt
PARAMS ((char *buf
));
193 static int putpkt_binary
PARAMS ((char *buf
, int cnt
));
195 void remote_console_output
PARAMS ((char *));
197 static void check_binary_download
PARAMS ((CORE_ADDR addr
));
200 struct packet_config
;
203 static void show_packet_config_cmd
PARAMS ((struct packet_config
* config
));
205 static void set_packet_config_cmd
PARAMS ((struct packet_config
* config
,
206 struct cmd_list_element
* c
));
208 static void add_packet_config_cmd
PARAMS ((struct packet_config
* config
,
211 void (*set_func
) (char *args
, int from_tty
, struct cmd_list_element
* c
),
212 void (*show_func
) (char *name
, int from_tty
),
213 struct cmd_list_element
**setlist
,
214 struct cmd_list_element
**showlist
));
216 static void init_packet_config
PARAMS ((struct packet_config
* config
));
218 static void set_remote_protocol_P_packet_cmd
PARAMS ((char *args
,
220 struct cmd_list_element
* c
));
222 static void show_remote_protocol_P_packet_cmd
PARAMS ((char *args
,
225 static void set_remote_protocol_Z_packet_cmd
PARAMS ((char *args
,
227 struct cmd_list_element
* c
));
229 static void show_remote_protocol_Z_packet_cmd
PARAMS ((char *args
,
235 /* Define the target subroutine names */
237 void open_remote_target
PARAMS ((char *, int, struct target_ops
*, int));
239 void _initialize_remote
PARAMS ((void));
243 static struct target_ops remote_ops
;
245 static struct target_ops extended_remote_ops
;
247 /* Temporary target ops. Just like the remote_ops and
248 extended_remote_ops, but with asynchronous support. */
249 static struct target_ops remote_async_ops
;
251 static struct target_ops extended_async_remote_ops
;
253 /* This was 5 seconds, which is a long time to sit and wait.
254 Unless this is going though some terminal server or multiplexer or
255 other form of hairy serial connection, I would think 2 seconds would
258 /* Changed to allow option to set timeout value.
259 was static int remote_timeout = 2; */
260 extern int remote_timeout
;
262 /* This variable chooses whether to send a ^C or a break when the user
263 requests program interruption. Although ^C is usually what remote
264 systems expect, and that is the default here, sometimes a break is
265 preferable instead. */
267 static int remote_break
;
269 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
270 remote_open knows that we don't have a file open when the program
272 static serial_t remote_desc
= NULL
;
274 /* This is set by the target (thru the 'S' message)
275 to denote that the target is in kernel mode. */
276 static int cisco_kernel_mode
= 0;
278 /* Maximum number of bytes to read/write at once. The value here
279 is chosen to fill up a packet (the headers account for the 32). */
280 #define MAXBUFBYTES(N) (((N)-32)/2)
282 /* Having this larger than 400 causes us to be incompatible with m68k-stub.c
283 and i386-stub.c. Normally, no one would notice because it only matters
284 for writing large chunks of memory (e.g. in downloads). Also, this needs
285 to be more than 400 if required to hold the registers (see below, where
286 we round it up based on REGISTER_BYTES). */
287 /* Round up PBUFSIZ to hold all the registers, at least. */
288 #define PBUFSIZ ((REGISTER_BYTES > MAXBUFBYTES (400)) \
289 ? (REGISTER_BYTES * 2 + 32) \
293 /* This variable sets the number of bytes to be written to the target
294 in a single packet. Normally PBUFSIZ is satisfactory, but some
295 targets need smaller values (perhaps because the receiving end
298 static int remote_write_size
;
300 /* This variable sets the number of bits in an address that are to be
301 sent in a memory ("M" or "m") packet. Normally, after stripping
302 leading zeros, the entire address would be sent. This variable
303 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
304 initial implementation of remote.c restricted the address sent in
305 memory packets to ``host::sizeof long'' bytes - (typically 32
306 bits). Consequently, for 64 bit targets, the upper 32 bits of an
307 address was never sent. Since fixing this bug may cause a break in
308 some remote targets this variable is principly provided to
309 facilitate backward compatibility. */
311 static int remote_address_size
;
313 /* This is the size (in chars) of the first response to the `g' command. This
314 is used to limit the size of the memory read and write commands to prevent
315 stub buffers from overflowing. The size does not include headers and
316 trailers, it is only the payload size. */
318 static int remote_register_buf_size
= 0;
320 /* Generic configuration support for packets the stub optionally
321 supports. Allows the user to specify the use of the packet as well
322 as allowing GDB to auto-detect support in the remote stub. */
326 PACKET_SUPPORT_UNKNOWN
= 0,
333 PACKET_AUTO_DETECT
= 0,
342 enum packet_detect detect
;
343 enum packet_support support
;
346 static char packet_support_auto
[] = "auto";
347 static char packet_enable
[] = "enable";
348 static char packet_disable
[] = "disable";
349 static char *packet_support_enums
[] =
358 set_packet_config_cmd (config
, c
)
359 struct packet_config
*config
;
360 struct cmd_list_element
*c
;
362 if (config
->state
== packet_enable
)
364 config
->detect
= PACKET_MANUAL_DETECT
;
365 config
->support
= PACKET_ENABLE
;
367 else if (config
->state
== packet_disable
)
369 config
->detect
= PACKET_MANUAL_DETECT
;
370 config
->support
= PACKET_DISABLE
;
372 else if (config
->state
== packet_support_auto
)
374 config
->detect
= PACKET_AUTO_DETECT
;
375 config
->support
= PACKET_SUPPORT_UNKNOWN
;
378 internal_error ("Bad enum value");
382 show_packet_config_cmd (config
)
383 struct packet_config
*config
;
385 char *support
= "internal-error";
386 switch (config
->support
)
392 support
= "disabled";
394 case PACKET_SUPPORT_UNKNOWN
:
398 switch (config
->detect
)
400 case PACKET_AUTO_DETECT
:
401 printf_filtered ("Support for remote protocol `%s' (%s) packet is auto-detected, currently %s.\n",
402 config
->name
, config
->title
, support
);
404 case PACKET_MANUAL_DETECT
:
405 printf_filtered ("Support for remote protocol `%s' (%s) is currently %s.\n",
406 config
->name
, config
->title
, support
);
411 add_packet_config_cmd (config
, name
, title
, set_func
, show_func
,
413 struct packet_config
*config
;
416 void (*set_func
) PARAMS ((char *args
, int from_tty
,
417 struct cmd_list_element
* c
));
418 void (*show_func
) PARAMS ((char *name
, int from_tty
));
419 struct cmd_list_element
**setlist
;
420 struct cmd_list_element
**showlist
;
422 struct cmd_list_element
*c
;
427 config
->title
= title
;
428 asprintf (&set_doc
, "Set use of remote protocol `%s' (%s) packet",
430 asprintf (&show_doc
, "Show current use of remote protocol `%s' (%s) packet",
432 asprintf (&full_name
, "%s-packet", name
);
433 c
= add_set_enum_cmd (full_name
,
434 class_obscure
, packet_support_enums
,
435 (char *) &config
->state
,
437 c
->function
.sfunc
= set_func
;
438 add_cmd (full_name
, class_obscure
, show_func
, show_doc
, showlist
);
442 init_packet_config (config
)
443 struct packet_config
*config
;
445 switch (config
->detect
)
447 case PACKET_AUTO_DETECT
:
448 config
->support
= PACKET_SUPPORT_UNKNOWN
;
450 case PACKET_MANUAL_DETECT
:
451 /* let the user beware */
456 /* Should we try the 'P' (set register) request? */
458 static struct packet_config remote_protocol_P
;
461 set_remote_protocol_P_packet_cmd (args
, from_tty
, c
)
464 struct cmd_list_element
*c
;
466 set_packet_config_cmd (&remote_protocol_P
, c
);
470 show_remote_protocol_P_packet_cmd (args
, from_tty
)
474 show_packet_config_cmd (&remote_protocol_P
);
477 /* Should we try the 'Z' (set breakpoint) request? */
479 static struct packet_config remote_protocol_Z
;
482 set_remote_protocol_Z_packet_cmd (args
, from_tty
, c
)
485 struct cmd_list_element
*c
;
487 set_packet_config_cmd (&remote_protocol_Z
, c
);
491 show_remote_protocol_Z_packet_cmd (args
, from_tty
)
495 show_packet_config_cmd (&remote_protocol_Z
);
498 /* Should we try the 'X' (remote binary download) packet?
500 This variable (available to the user via "set remote X-packet")
501 dictates whether downloads are sent in binary (via the 'X' packet).
502 We assume that the stub can, and attempt to do it. This will be
503 cleared if the stub does not understand it. This switch is still
504 needed, though in cases when the packet is supported in the stub,
505 but the connection does not allow it (i.e., 7-bit serial connection
508 static struct packet_config remote_protocol_binary_download
;
511 set_remote_protocol_binary_download_cmd (char *args
,
513 struct cmd_list_element
*c
)
515 set_packet_config_cmd (&remote_protocol_binary_download
, c
);
519 show_remote_protocol_binary_download_cmd (char *args
,
522 show_packet_config_cmd (&remote_protocol_binary_download
);
526 /* Tokens for use by the asynchronous signal handlers for SIGINT */
527 PTR sigint_remote_twice_token
;
528 PTR sigint_remote_token
;
530 /* These are pointers to hook functions that may be set in order to
531 modify resume/wait behavior for a particular architecture. */
533 void (*target_resume_hook
) PARAMS ((void));
534 void (*target_wait_loop_hook
) PARAMS ((void));
538 /* These are the threads which we last sent to the remote system.
539 -1 for all or -2 for not sent yet. */
540 static int general_thread
;
541 static int continue_thread
;
543 /* Call this function as a result of
544 1) A halt indication (T packet) containing a thread id
545 2) A direct query of currthread
546 3) Successful execution of set thread
550 record_currthread (currthread
)
553 general_thread
= currthread
;
555 /* If this is a new thread, add it to GDB's thread list.
556 If we leave it up to WFI to do this, bad things will happen. */
557 if (!in_thread_list (currthread
))
559 add_thread (currthread
);
560 printf_filtered ("[New %s]\n", target_pid_to_str (currthread
));
564 #define MAGIC_NULL_PID 42000
571 char *buf
= alloca (PBUFSIZ
);
572 int state
= gen
? general_thread
: continue_thread
;
578 buf
[1] = gen
? 'g' : 'c';
579 if (th
== MAGIC_NULL_PID
)
585 sprintf (&buf
[2], "-%x", -th
);
587 sprintf (&buf
[2], "%x", th
);
593 continue_thread
= th
;
596 /* Return nonzero if the thread TH is still alive on the remote system. */
599 remote_thread_alive (tid
)
605 sprintf (buf
, "T-%08x", -tid
);
607 sprintf (buf
, "T%08x", tid
);
610 return (buf
[0] == 'O' && buf
[1] == 'K');
613 /* About these extended threadlist and threadinfo packets. They are
614 variable length packets but, the fields within them are often fixed
615 length. They are redundent enough to send over UDP as is the
616 remote protocol in general. There is a matching unit test module
619 #define OPAQUETHREADBYTES 8
621 /* a 64 bit opaque identifier */
622 typedef unsigned char threadref
[OPAQUETHREADBYTES
];
624 /* WARNING: This threadref data structure comes from the remote O.S., libstub
625 protocol encoding, and remote.c. it is not particularly changable */
627 /* Right now, the internal structure is int. We want it to be bigger.
631 typedef int gdb_threadref
; /* internal GDB thread reference */
633 /* gdb_ext_thread_info is an internal GDB data structure which is
634 equivalint to the reply of the remote threadinfo packet */
636 struct gdb_ext_thread_info
638 threadref threadid
; /* External form of thread reference */
639 int active
; /* Has state interesting to GDB? , regs, stack */
640 char display
[256]; /* Brief state display, name, blocked/syspended */
641 char shortname
[32]; /* To be used to name threads */
642 char more_display
[256]; /* Long info, statistics, queue depth, whatever */
645 /* The volume of remote transfers can be limited by submitting
646 a mask containing bits specifying the desired information.
647 Use a union of these values as the 'selection' parameter to
648 get_thread_info. FIXME: Make these TAG names more thread specific.
651 #define TAG_THREADID 1
653 #define TAG_DISPLAY 4
654 #define TAG_THREADNAME 8
655 #define TAG_MOREDISPLAY 16
657 #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES*2)
659 char *unpack_varlen_hex
PARAMS ((char *buff
, int *result
));
661 static char *unpack_nibble
PARAMS ((char *buf
, int *val
));
663 static char *pack_nibble
PARAMS ((char *buf
, int nibble
));
665 static char *pack_hex_byte
PARAMS ((char *pkt
, int /*unsigned char */ byte
));
667 static char *unpack_byte
PARAMS ((char *buf
, int *value
));
669 static char *pack_int
PARAMS ((char *buf
, int value
));
671 static char *unpack_int
PARAMS ((char *buf
, int *value
));
673 static char *unpack_string
PARAMS ((char *src
, char *dest
, int length
));
675 static char *pack_threadid
PARAMS ((char *pkt
, threadref
* id
));
677 static char *unpack_threadid
PARAMS ((char *inbuf
, threadref
* id
));
679 void int_to_threadref
PARAMS ((threadref
* id
, int value
));
681 static int threadref_to_int
PARAMS ((threadref
* ref
));
683 static void copy_threadref
PARAMS ((threadref
* dest
, threadref
* src
));
685 static int threadmatch
PARAMS ((threadref
* dest
, threadref
* src
));
687 static char *pack_threadinfo_request
PARAMS ((char *pkt
, int mode
,
690 static int remote_unpack_thread_info_response
PARAMS ((char *pkt
,
691 threadref
* expectedref
,
692 struct gdb_ext_thread_info
* info
));
695 static int remote_get_threadinfo
PARAMS ((threadref
* threadid
,
696 int fieldset
, /*TAG mask */
697 struct gdb_ext_thread_info
* info
));
699 static int adapt_remote_get_threadinfo
PARAMS ((gdb_threadref
* ref
,
701 struct gdb_ext_thread_info
* info
));
703 static char *pack_threadlist_request
PARAMS ((char *pkt
, int startflag
,
705 threadref
* nextthread
));
707 static int parse_threadlist_response
PARAMS ((char *pkt
,
709 threadref
* original_echo
,
710 threadref
* resultlist
,
713 static int remote_get_threadlist
PARAMS ((int startflag
,
714 threadref
* nextthread
,
718 threadref
* threadlist
));
720 typedef int (*rmt_thread_action
) (threadref
* ref
, void *context
);
722 static int remote_threadlist_iterator
PARAMS ((rmt_thread_action stepfunction
,
723 void *context
, int looplimit
));
725 static int remote_newthread_step
PARAMS ((threadref
* ref
, void *context
));
727 /* encode 64 bits in 16 chars of hex */
729 static const char hexchars
[] = "0123456789abcdef";
736 if ((ch
>= 'a') && (ch
<= 'f'))
738 *val
= ch
- 'a' + 10;
741 if ((ch
>= 'A') && (ch
<= 'F'))
743 *val
= ch
- 'A' + 10;
746 if ((ch
>= '0') && (ch
<= '9'))
758 if (ch
>= 'a' && ch
<= 'f')
759 return ch
- 'a' + 10;
760 if (ch
>= '0' && ch
<= '9')
762 if (ch
>= 'A' && ch
<= 'F')
763 return ch
- 'A' + 10;
768 stub_unpack_int (buff
, fieldlength
)
777 nibble
= stubhex (*buff
++);
781 retval
= retval
<< 4;
787 unpack_varlen_hex (buff
, result
)
788 char *buff
; /* packet to parse */
794 while (ishex (*buff
, &nibble
))
797 retval
= retval
<< 4;
798 retval
|= nibble
& 0x0f;
805 unpack_nibble (buf
, val
)
814 pack_nibble (buf
, nibble
)
818 *buf
++ = hexchars
[(nibble
& 0x0f)];
823 pack_hex_byte (pkt
, byte
)
827 *pkt
++ = hexchars
[(byte
>> 4) & 0xf];
828 *pkt
++ = hexchars
[(byte
& 0xf)];
833 unpack_byte (buf
, value
)
837 *value
= stub_unpack_int (buf
, 2);
842 pack_int (buf
, value
)
846 buf
= pack_hex_byte (buf
, (value
>> 24) & 0xff);
847 buf
= pack_hex_byte (buf
, (value
>> 16) & 0xff);
848 buf
= pack_hex_byte (buf
, (value
>> 8) & 0x0ff);
849 buf
= pack_hex_byte (buf
, (value
& 0xff));
854 unpack_int (buf
, value
)
858 *value
= stub_unpack_int (buf
, 8);
862 #if 0 /* currently unused, uncomment when needed */
863 static char *pack_string
PARAMS ((char *pkt
, char *string
));
866 pack_string (pkt
, string
)
873 len
= strlen (string
);
875 len
= 200; /* Bigger than most GDB packets, junk??? */
876 pkt
= pack_hex_byte (pkt
, len
);
880 if ((ch
== '\0') || (ch
== '#'))
881 ch
= '*'; /* Protect encapsulation */
886 #endif /* 0 (unused) */
889 unpack_string (src
, dest
, length
)
901 pack_threadid (pkt
, id
)
906 unsigned char *altid
;
908 altid
= (unsigned char *) id
;
909 limit
= pkt
+ BUF_THREAD_ID_SIZE
;
911 pkt
= pack_hex_byte (pkt
, *altid
++);
917 unpack_threadid (inbuf
, id
)
922 char *limit
= inbuf
+ BUF_THREAD_ID_SIZE
;
925 altref
= (char *) id
;
927 while (inbuf
< limit
)
929 x
= stubhex (*inbuf
++);
930 y
= stubhex (*inbuf
++);
931 *altref
++ = (x
<< 4) | y
;
936 /* Externally, threadrefs are 64 bits but internally, they are still
937 ints. This is due to a mismatch of specifications. We would like
938 to use 64bit thread references internally. This is an adapter
942 int_to_threadref (id
, value
)
948 scan
= (unsigned char *) id
;
954 *scan
++ = (value
>> 24) & 0xff;
955 *scan
++ = (value
>> 16) & 0xff;
956 *scan
++ = (value
>> 8) & 0xff;
957 *scan
++ = (value
& 0xff);
961 threadref_to_int (ref
)
971 value
= (value
<< 8) | ((*scan
++) & 0xff);
976 copy_threadref (dest
, src
)
981 unsigned char *csrc
, *cdest
;
983 csrc
= (unsigned char *) src
;
984 cdest
= (unsigned char *) dest
;
991 threadmatch (dest
, src
)
995 /* things are broken right now, so just assume we got a match */
997 unsigned char *srcp
, *destp
;
1000 destp
= (char *) dest
;
1004 result
&= (*srcp
++ == *destp
++) ? 1 : 0;
1011 threadid:1, # always request threadid
1018 /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
1021 pack_threadinfo_request (pkt
, mode
, id
)
1026 *pkt
++ = 'q'; /* Info Query */
1027 *pkt
++ = 'P'; /* process or thread info */
1028 pkt
= pack_int (pkt
, mode
); /* mode */
1029 pkt
= pack_threadid (pkt
, id
); /* threadid */
1030 *pkt
= '\0'; /* terminate */
1034 /* These values tag the fields in a thread info response packet */
1035 /* Tagging the fields allows us to request specific fields and to
1036 add more fields as time goes by */
1038 #define TAG_THREADID 1 /* Echo the thread identifier */
1039 #define TAG_EXISTS 2 /* Is this process defined enough to
1040 fetch registers and its stack */
1041 #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
1042 #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is */
1043 #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
1047 remote_unpack_thread_info_response (pkt
, expectedref
, info
)
1049 threadref
*expectedref
;
1050 struct gdb_ext_thread_info
*info
;
1055 char *limit
= pkt
+ PBUFSIZ
; /* plausable parsing limit */
1058 /* info->threadid = 0; FIXME: implement zero_threadref */
1060 info
->display
[0] = '\0';
1061 info
->shortname
[0] = '\0';
1062 info
->more_display
[0] = '\0';
1064 /* Assume the characters indicating the packet type have been stripped */
1065 pkt
= unpack_int (pkt
, &mask
); /* arg mask */
1066 pkt
= unpack_threadid (pkt
, &ref
);
1069 warning ("Incomplete response to threadinfo request\n");
1070 if (!threadmatch (&ref
, expectedref
))
1071 { /* This is an answer to a different request */
1072 warning ("ERROR RMT Thread info mismatch\n");
1075 copy_threadref (&info
->threadid
, &ref
);
1077 /* Loop on tagged fields , try to bail if somthing goes wrong */
1079 while ((pkt
< limit
) && mask
&& *pkt
) /* packets are terminated with nulls */
1081 pkt
= unpack_int (pkt
, &tag
); /* tag */
1082 pkt
= unpack_byte (pkt
, &length
); /* length */
1083 if (!(tag
& mask
)) /* tags out of synch with mask */
1085 warning ("ERROR RMT: threadinfo tag mismatch\n");
1089 if (tag
== TAG_THREADID
)
1093 warning ("ERROR RMT: length of threadid is not 16\n");
1097 pkt
= unpack_threadid (pkt
, &ref
);
1098 mask
= mask
& ~TAG_THREADID
;
1101 if (tag
== TAG_EXISTS
)
1103 info
->active
= stub_unpack_int (pkt
, length
);
1105 mask
= mask
& ~(TAG_EXISTS
);
1108 warning ("ERROR RMT: 'exists' length too long\n");
1114 if (tag
== TAG_THREADNAME
)
1116 pkt
= unpack_string (pkt
, &info
->shortname
[0], length
);
1117 mask
= mask
& ~TAG_THREADNAME
;
1120 if (tag
== TAG_DISPLAY
)
1122 pkt
= unpack_string (pkt
, &info
->display
[0], length
);
1123 mask
= mask
& ~TAG_DISPLAY
;
1126 if (tag
== TAG_MOREDISPLAY
)
1128 pkt
= unpack_string (pkt
, &info
->more_display
[0], length
);
1129 mask
= mask
& ~TAG_MOREDISPLAY
;
1132 warning ("ERROR RMT: unknown thread info tag\n");
1133 break; /* Not a tag we know about */
1139 remote_get_threadinfo (threadid
, fieldset
, info
)
1140 threadref
*threadid
;
1141 int fieldset
; /* TAG mask */
1142 struct gdb_ext_thread_info
*info
;
1145 char *threadinfo_pkt
= alloca (PBUFSIZ
);
1147 pack_threadinfo_request (threadinfo_pkt
, fieldset
, threadid
);
1148 putpkt (threadinfo_pkt
);
1149 getpkt (threadinfo_pkt
, 0);
1150 result
= remote_unpack_thread_info_response (threadinfo_pkt
+ 2, threadid
,
1155 /* Unfortunately, 61 bit thread-ids are bigger than the internal
1156 representation of a threadid. */
1159 adapt_remote_get_threadinfo (ref
, selection
, info
)
1162 struct gdb_ext_thread_info
*info
;
1166 int_to_threadref (&lclref
, *ref
);
1167 return remote_get_threadinfo (&lclref
, selection
, info
);
1170 /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
1173 pack_threadlist_request (pkt
, startflag
, threadcount
, nextthread
)
1177 threadref
*nextthread
;
1179 *pkt
++ = 'q'; /* info query packet */
1180 *pkt
++ = 'L'; /* Process LIST or threadLIST request */
1181 pkt
= pack_nibble (pkt
, startflag
); /* initflag 1 bytes */
1182 pkt
= pack_hex_byte (pkt
, threadcount
); /* threadcount 2 bytes */
1183 pkt
= pack_threadid (pkt
, nextthread
); /* 64 bit thread identifier */
1188 /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
1191 parse_threadlist_response (pkt
, result_limit
, original_echo
, resultlist
,
1195 threadref
*original_echo
;
1196 threadref
*resultlist
;
1200 int count
, resultcount
, done
;
1203 /* Assume the 'q' and 'M chars have been stripped. */
1204 limit
= pkt
+ (PBUFSIZ
- BUF_THREAD_ID_SIZE
); /* done parse past here */
1205 pkt
= unpack_byte (pkt
, &count
); /* count field */
1206 pkt
= unpack_nibble (pkt
, &done
);
1207 /* The first threadid is the argument threadid. */
1208 pkt
= unpack_threadid (pkt
, original_echo
); /* should match query packet */
1209 while ((count
-- > 0) && (pkt
< limit
))
1211 pkt
= unpack_threadid (pkt
, resultlist
++);
1212 if (resultcount
++ >= result_limit
)
1221 remote_get_threadlist (startflag
, nextthread
, result_limit
,
1222 done
, result_count
, threadlist
)
1224 threadref
*nextthread
;
1228 threadref
*threadlist
;
1231 static threadref echo_nextthread
;
1232 char *threadlist_packet
= alloca (PBUFSIZ
);
1233 char *t_response
= alloca (PBUFSIZ
);
1236 /* Trancate result limit to be smaller than the packet size */
1237 if ((((result_limit
+ 1) * BUF_THREAD_ID_SIZE
) + 10) >= PBUFSIZ
)
1238 result_limit
= (PBUFSIZ
/ BUF_THREAD_ID_SIZE
) - 2;
1240 pack_threadlist_request (threadlist_packet
,
1241 startflag
, result_limit
, nextthread
);
1242 putpkt (threadlist_packet
);
1243 getpkt (t_response
, 0);
1246 parse_threadlist_response (t_response
+ 2, result_limit
, &echo_nextthread
,
1249 if (!threadmatch (&echo_nextthread
, nextthread
))
1251 /* FIXME: This is a good reason to drop the packet */
1252 /* Possably, there is a duplicate response */
1254 retransmit immediatly - race conditions
1255 retransmit after timeout - yes
1257 wait for packet, then exit
1259 warning ("HMM: threadlist did not echo arg thread, dropping it\n");
1260 return 0; /* I choose simply exiting */
1262 if (*result_count
<= 0)
1266 warning ("RMT ERROR : failed to get remote thread list\n");
1269 return result
; /* break; */
1271 if (*result_count
> result_limit
)
1274 warning ("RMT ERROR: threadlist response longer than requested\n");
1280 /* This is the interface between remote and threads, remotes upper interface */
1282 /* remote_find_new_threads retrieves the thread list and for each
1283 thread in the list, looks up the thread in GDB's internal list,
1284 ading the thread if it does not already exist. This involves
1285 getting partial thread lists from the remote target so, polling the
1286 quit_flag is required. */
1289 /* About this many threadisds fit in a packet. */
1291 #define MAXTHREADLISTRESULTS 32
1294 remote_threadlist_iterator (stepfunction
, context
, looplimit
)
1295 rmt_thread_action stepfunction
;
1299 int done
, i
, result_count
;
1303 static threadref nextthread
;
1304 static threadref resultthreadlist
[MAXTHREADLISTRESULTS
];
1309 if (loopcount
++ > looplimit
)
1312 warning ("Remote fetch threadlist -infinite loop-\n");
1315 if (!remote_get_threadlist (startflag
, &nextthread
, MAXTHREADLISTRESULTS
,
1316 &done
, &result_count
, resultthreadlist
))
1321 /* clear for later iterations */
1323 /* Setup to resume next batch of thread references, set nextthread. */
1324 if (result_count
>= 1)
1325 copy_threadref (&nextthread
, &resultthreadlist
[result_count
- 1]);
1327 while (result_count
--)
1328 if (!(result
= (*stepfunction
) (&resultthreadlist
[i
++], context
)))
1335 remote_newthread_step (ref
, context
)
1341 pid
= threadref_to_int (ref
);
1342 if (!in_thread_list (pid
))
1344 return 1; /* continue iterator */
1347 #define CRAZY_MAX_THREADS 1000
1350 remote_current_thread (oldpid
)
1353 char *buf
= alloca (PBUFSIZ
);
1357 if (buf
[0] == 'Q' && buf
[1] == 'C')
1358 return strtol (&buf
[2], NULL
, 16);
1363 /* Find new threads for info threads command. */
1366 remote_find_new_threads ()
1368 remote_threadlist_iterator (remote_newthread_step
, 0,
1370 if (inferior_pid
== MAGIC_NULL_PID
) /* ack ack ack */
1371 inferior_pid
= remote_current_thread (inferior_pid
);
1375 remote_threads_info (void)
1377 char *buf
= alloca (PBUFSIZ
);
1381 if (remote_desc
== 0) /* paranoia */
1382 error ("Command can only be used when connected to the remote target.");
1384 putpkt ("qfThreadInfo");
1385 getpkt (bufp
= buf
, 0);
1386 if (bufp
[0] == '\0') /* q packet not recognized! */
1387 { /* try old jmetzler method */
1388 remote_find_new_threads ();
1391 else /* try new 'q' method */
1392 while (*bufp
++ == 'm') /* reply contains one or more TID */
1396 tid
= strtol (bufp
, &bufp
, 16);
1397 if (tid
!= 0 && !in_thread_list (tid
))
1400 while (*bufp
++ == ','); /* comma-separated list */
1401 putpkt ("qsThreadInfo");
1402 getpkt (bufp
= buf
, 0);
1407 /* Restart the remote side; this is an extended protocol operation. */
1410 extended_remote_restart ()
1412 char *buf
= alloca (PBUFSIZ
);
1414 /* Send the restart command; for reasons I don't understand the
1415 remote side really expects a number after the "R". */
1417 sprintf (&buf
[1], "%x", 0);
1420 /* Now query for status so this looks just like we restarted
1421 gdbserver from scratch. */
1426 /* Clean up connection to a remote debugger. */
1430 remote_close (quitting
)
1434 SERIAL_CLOSE (remote_desc
);
1438 /* Query the remote side for the text, data and bss offsets. */
1443 char *buf
= alloca (PBUFSIZ
);
1446 CORE_ADDR text_addr
, data_addr
, bss_addr
;
1447 struct section_offsets
*offs
;
1449 putpkt ("qOffsets");
1453 if (buf
[0] == '\000')
1454 return; /* Return silently. Stub doesn't support
1458 warning ("Remote failure reply: %s", buf
);
1462 /* Pick up each field in turn. This used to be done with scanf, but
1463 scanf will make trouble if CORE_ADDR size doesn't match
1464 conversion directives correctly. The following code will work
1465 with any size of CORE_ADDR. */
1466 text_addr
= data_addr
= bss_addr
= 0;
1470 if (strncmp (ptr
, "Text=", 5) == 0)
1473 /* Don't use strtol, could lose on big values. */
1474 while (*ptr
&& *ptr
!= ';')
1475 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
1480 if (!lose
&& strncmp (ptr
, ";Data=", 6) == 0)
1483 while (*ptr
&& *ptr
!= ';')
1484 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
1489 if (!lose
&& strncmp (ptr
, ";Bss=", 5) == 0)
1492 while (*ptr
&& *ptr
!= ';')
1493 bss_addr
= (bss_addr
<< 4) + fromhex (*ptr
++);
1499 error ("Malformed response to offset query, %s", buf
);
1501 if (symfile_objfile
== NULL
)
1504 offs
= alloca (sizeof (struct section_offsets
)
1505 + symfile_objfile
->num_sections
1506 * sizeof (offs
->offsets
));
1507 memcpy (offs
, symfile_objfile
->section_offsets
,
1508 sizeof (struct section_offsets
)
1509 + symfile_objfile
->num_sections
1510 * sizeof (offs
->offsets
));
1512 ANOFFSET (offs
, SECT_OFF_TEXT
) = text_addr
;
1514 /* This is a temporary kludge to force data and bss to use the same offsets
1515 because that's what nlmconv does now. The real solution requires changes
1516 to the stub and remote.c that I don't have time to do right now. */
1518 ANOFFSET (offs
, SECT_OFF_DATA
) = data_addr
;
1519 ANOFFSET (offs
, SECT_OFF_BSS
) = data_addr
;
1521 objfile_relocate (symfile_objfile
, offs
);
1525 * Cisco version of section offsets:
1527 * Instead of having GDB query the target for the section offsets,
1528 * Cisco lets the target volunteer the information! It's also in
1529 * a different format, so here are the functions that will decode
1530 * a section offset packet from a Cisco target.
1534 * Function: remote_cisco_section_offsets
1536 * Returns: zero for success, non-zero for failure
1540 remote_cisco_section_offsets (text_addr
, data_addr
, bss_addr
,
1541 text_offs
, data_offs
, bss_offs
)
1545 bfd_signed_vma
*text_offs
;
1546 bfd_signed_vma
*data_offs
;
1547 bfd_signed_vma
*bss_offs
;
1549 bfd_vma text_base
, data_base
, bss_base
;
1550 struct minimal_symbol
*start
;
1556 if (symfile_objfile
== NULL
)
1557 return -1; /* no can do nothin' */
1559 start
= lookup_minimal_symbol ("_start", NULL
, NULL
);
1561 return -1; /* Can't find "_start" symbol */
1563 data_base
= bss_base
= 0;
1564 text_base
= SYMBOL_VALUE_ADDRESS (start
);
1566 abfd
= symfile_objfile
->obfd
;
1567 for (sect
= abfd
->sections
;
1571 p
= (unsigned char *) bfd_get_section_name (abfd
, sect
);
1573 if (strcmp (p
+ len
- 4, "data") == 0) /* ends in "data" */
1574 if (data_base
== 0 ||
1575 data_base
> bfd_get_section_vma (abfd
, sect
))
1576 data_base
= bfd_get_section_vma (abfd
, sect
);
1577 if (strcmp (p
+ len
- 3, "bss") == 0) /* ends in "bss" */
1578 if (bss_base
== 0 ||
1579 bss_base
> bfd_get_section_vma (abfd
, sect
))
1580 bss_base
= bfd_get_section_vma (abfd
, sect
);
1582 *text_offs
= text_addr
- text_base
;
1583 *data_offs
= data_addr
- data_base
;
1584 *bss_offs
= bss_addr
- bss_base
;
1589 sprintf (tmp
, "VMA: text = 0x");
1590 sprintf_vma (tmp
+ strlen (tmp
), text_addr
);
1591 sprintf (tmp
+ strlen (tmp
), " data = 0x");
1592 sprintf_vma (tmp
+ strlen (tmp
), data_addr
);
1593 sprintf (tmp
+ strlen (tmp
), " bss = 0x");
1594 sprintf_vma (tmp
+ strlen (tmp
), bss_addr
);
1595 fprintf_filtered (gdb_stdlog
, tmp
);
1596 fprintf_filtered (gdb_stdlog
,
1597 "Reloc offset: text = 0x%x data = 0x%x bss = 0x%x\n",
1598 (long) *text_offs
, (long) *data_offs
, (long) *bss_offs
);
1605 * Function: remote_cisco_objfile_relocate
1607 * Relocate the symbol file for a remote target.
1611 remote_cisco_objfile_relocate (text_off
, data_off
, bss_off
)
1612 bfd_signed_vma text_off
;
1613 bfd_signed_vma data_off
;
1614 bfd_signed_vma bss_off
;
1616 struct section_offsets
*offs
;
1618 if (text_off
!= 0 || data_off
!= 0 || bss_off
!= 0)
1620 /* FIXME: This code assumes gdb-stabs.h is being used; it's
1621 broken for xcoff, dwarf, sdb-coff, etc. But there is no
1622 simple canonical representation for this stuff. */
1624 offs
= ((struct section_offsets
*)
1625 alloca (sizeof (struct section_offsets
)
1626 + (symfile_objfile
->num_sections
1627 * sizeof (offs
->offsets
))));
1629 memcpy (offs
, symfile_objfile
->section_offsets
,
1630 (sizeof (struct section_offsets
)
1631 + (symfile_objfile
->num_sections
1632 * sizeof (offs
->offsets
))));
1634 ANOFFSET (offs
, SECT_OFF_TEXT
) = text_off
;
1635 ANOFFSET (offs
, SECT_OFF_DATA
) = data_off
;
1636 ANOFFSET (offs
, SECT_OFF_BSS
) = bss_off
;
1638 /* First call the standard objfile_relocate. */
1639 objfile_relocate (symfile_objfile
, offs
);
1641 /* Now we need to fix up the section entries already attached to
1642 the exec target. These entries will control memory transfers
1643 from the exec file. */
1645 exec_set_section_offsets (text_off
, data_off
, bss_off
);
1649 /* Stub for catch_errors. */
1652 remote_start_remote_dummy (dummy
)
1655 start_remote (); /* Initialize gdb process mechanisms */
1660 remote_start_remote (dummy
)
1663 immediate_quit
= 1; /* Allow user to interrupt it */
1665 /* Ack any packet which the remote side has already sent. */
1666 SERIAL_WRITE (remote_desc
, "+", 1);
1668 /* Let the stub know that we want it to return the thread. */
1671 inferior_pid
= remote_current_thread (inferior_pid
);
1673 get_offsets (); /* Get text, data & bss offsets */
1675 putpkt ("?"); /* initiate a query from remote machine */
1678 return remote_start_remote_dummy (dummy
);
1681 /* Open a connection to a remote debugger.
1682 NAME is the filename used for communication. */
1685 remote_open (name
, from_tty
)
1689 remote_open_1 (name
, from_tty
, &remote_ops
, 0);
1692 /* Just like remote_open, but with asynchronous support. */
1694 remote_async_open (name
, from_tty
)
1698 remote_async_open_1 (name
, from_tty
, &remote_async_ops
, 0);
1701 /* Open a connection to a remote debugger using the extended
1702 remote gdb protocol. NAME is the filename used for communication. */
1705 extended_remote_open (name
, from_tty
)
1709 remote_open_1 (name
, from_tty
, &extended_remote_ops
, 1 /*extended_p */ );
1712 /* Just like extended_remote_open, but with asynchronous support. */
1714 extended_remote_async_open (name
, from_tty
)
1718 remote_async_open_1 (name
, from_tty
, &extended_async_remote_ops
, 1 /*extended_p */ );
1721 /* Generic code for opening a connection to a remote target. */
1723 static DCACHE
*remote_dcache
;
1726 remote_open_1 (name
, from_tty
, target
, extended_p
)
1729 struct target_ops
*target
;
1733 error ("To open a remote debug connection, you need to specify what\n\
1734 serial device is attached to the remote system (e.g. /dev/ttya).");
1736 target_preopen (from_tty
);
1738 unpush_target (target
);
1740 remote_dcache
= dcache_init (remote_read_bytes
, remote_write_bytes
);
1742 remote_desc
= SERIAL_OPEN (name
);
1744 perror_with_name (name
);
1746 if (baud_rate
!= -1)
1748 if (SERIAL_SETBAUDRATE (remote_desc
, baud_rate
))
1750 SERIAL_CLOSE (remote_desc
);
1751 perror_with_name (name
);
1755 SERIAL_RAW (remote_desc
);
1757 /* If there is something sitting in the buffer we might take it as a
1758 response to a command, which would be bad. */
1759 SERIAL_FLUSH_INPUT (remote_desc
);
1763 puts_filtered ("Remote debugging using ");
1764 puts_filtered (name
);
1765 puts_filtered ("\n");
1767 push_target (target
); /* Switch to using remote target now */
1769 init_packet_config (&remote_protocol_P
);
1770 init_packet_config (&remote_protocol_Z
);
1772 general_thread
= -2;
1773 continue_thread
= -2;
1775 /* Force remote_write_bytes to check whether target supports
1776 binary downloading. */
1777 init_packet_config (&remote_protocol_binary_download
);
1779 /* Without this, some commands which require an active target (such
1780 as kill) won't work. This variable serves (at least) double duty
1781 as both the pid of the target process (if it has such), and as a
1782 flag indicating that a target is active. These functions should
1783 be split out into seperate variables, especially since GDB will
1784 someday have a notion of debugging several processes. */
1786 inferior_pid
= MAGIC_NULL_PID
;
1787 /* Start the remote connection; if error (0), discard this target.
1788 In particular, if the user quits, be sure to discard it
1789 (we'd be in an inconsistent state otherwise). */
1790 if (!catch_errors (remote_start_remote
, NULL
,
1791 "Couldn't establish connection to remote target\n",
1800 /* tell the remote that we're using the extended protocol. */
1801 char *buf
= alloca (PBUFSIZ
);
1807 /* Just like remote_open but with asynchronous support. */
1809 remote_async_open_1 (name
, from_tty
, target
, extended_p
)
1812 struct target_ops
*target
;
1816 error ("To open a remote debug connection, you need to specify what\n\
1817 serial device is attached to the remote system (e.g. /dev/ttya).");
1819 target_preopen (from_tty
);
1821 unpush_target (target
);
1823 remote_dcache
= dcache_init (remote_read_bytes
, remote_write_bytes
);
1825 remote_desc
= SERIAL_OPEN (name
);
1827 perror_with_name (name
);
1829 if (baud_rate
!= -1)
1831 if (SERIAL_SETBAUDRATE (remote_desc
, baud_rate
))
1833 SERIAL_CLOSE (remote_desc
);
1834 perror_with_name (name
);
1838 SERIAL_RAW (remote_desc
);
1840 /* If there is something sitting in the buffer we might take it as a
1841 response to a command, which would be bad. */
1842 SERIAL_FLUSH_INPUT (remote_desc
);
1846 puts_filtered ("Remote debugging using ");
1847 puts_filtered (name
);
1848 puts_filtered ("\n");
1851 /* If running in asynchronous mode, register the target with the
1852 event loop. Set things up so that when there is an event on the
1853 file descriptor, the event loop will call fetch_inferior_event,
1854 which will do the proper analysis to determine what happened. */
1856 add_file_handler (remote_desc
->fd
, fetch_inferior_event
, 0);
1858 push_target (target
); /* Switch to using remote target now */
1860 init_packet_config (&remote_protocol_P
);
1861 init_packet_config (&remote_protocol_Z
);
1863 general_thread
= -2;
1864 continue_thread
= -2;
1866 /* Force remote_write_bytes to check whether target supports
1867 binary downloading. */
1868 init_packet_config (&remote_protocol_binary_download
);
1870 /* If running asynchronously, set things up for telling the target
1871 to use the extended protocol. This will happen only after the
1872 target has been connected to, in fetch_inferior_event. */
1873 if (extended_p
&& async_p
)
1874 add_continuation (set_extended_protocol
, NULL
);
1876 /* Without this, some commands which require an active target (such
1877 as kill) won't work. This variable serves (at least) double duty
1878 as both the pid of the target process (if it has such), and as a
1879 flag indicating that a target is active. These functions should
1880 be split out into seperate variables, especially since GDB will
1881 someday have a notion of debugging several processes. */
1883 inferior_pid
= MAGIC_NULL_PID
;
1884 /* Start the remote connection; if error (0), discard this target.
1885 In particular, if the user quits, be sure to discard it
1886 (we'd be in an inconsistent state otherwise). */
1887 if (!catch_errors (remote_start_remote
, NULL
,
1888 "Couldn't establish connection to remote target\n",
1891 /* Unregister the file descriptor from the event loop. */
1893 delete_file_handler (remote_desc
->fd
);
1902 /* tell the remote that we're using the extended protocol. */
1903 char *buf
= alloca (PBUFSIZ
);
1910 /* This will be called by fetch_inferior_event, via the
1911 cmd_continuation pointer, only after the target has stopped. */
1913 set_extended_protocol (arg
)
1914 struct continuation_arg
*arg
;
1916 /* tell the remote that we're using the extended protocol. */
1917 char *buf
= alloca (PBUFSIZ
);
1922 /* This takes a program previously attached to and detaches it. After
1923 this is done, GDB can be used to debug some other program. We
1924 better not have left any breakpoints in the target program or it'll
1925 die when it hits one. */
1928 remote_detach (args
, from_tty
)
1932 char *buf
= alloca (PBUFSIZ
);
1935 error ("Argument given to \"detach\" when remotely debugging.");
1937 /* Tell the remote target to detach. */
1943 puts_filtered ("Ending remote debugging.\n");
1947 /* Same as remote_detach, but with async support. */
1949 remote_async_detach (args
, from_tty
)
1953 char *buf
= alloca (PBUFSIZ
);
1956 error ("Argument given to \"detach\" when remotely debugging.");
1958 /* Tell the remote target to detach. */
1962 /* Unregister the file descriptor from the event loop. */
1964 delete_file_handler (remote_desc
->fd
);
1968 puts_filtered ("Ending remote debugging.\n");
1971 /* Convert hex digit A to a number. */
1977 if (a
>= '0' && a
<= '9')
1979 else if (a
>= 'a' && a
<= 'f')
1980 return a
- 'a' + 10;
1981 else if (a
>= 'A' && a
<= 'F')
1982 return a
- 'A' + 10;
1984 error ("Reply contains invalid hex digit %d", a
);
1987 /* Convert number NIB to a hex digit. */
1996 return 'a' + nib
- 10;
1999 /* Tell the remote machine to resume. */
2001 static enum target_signal last_sent_signal
= TARGET_SIGNAL_0
;
2003 static int last_sent_step
;
2006 remote_resume (pid
, step
, siggnal
)
2008 enum target_signal siggnal
;
2010 char *buf
= alloca (PBUFSIZ
);
2013 set_thread (0, 0); /* run any thread */
2015 set_thread (pid
, 0); /* run this thread */
2017 dcache_flush (remote_dcache
);
2019 last_sent_signal
= siggnal
;
2020 last_sent_step
= step
;
2022 /* A hook for when we need to do something at the last moment before
2024 if (target_resume_hook
)
2025 (*target_resume_hook
) ();
2027 if (siggnal
!= TARGET_SIGNAL_0
)
2029 buf
[0] = step
? 'S' : 'C';
2030 buf
[1] = tohex (((int) siggnal
>> 4) & 0xf);
2031 buf
[2] = tohex ((int) siggnal
& 0xf);
2035 strcpy (buf
, step
? "s" : "c");
2040 /* Same as remote_resume, but with async support. */
2042 remote_async_resume (pid
, step
, siggnal
)
2044 enum target_signal siggnal
;
2046 char *buf
= alloca (PBUFSIZ
);
2049 set_thread (0, 0); /* run any thread */
2051 set_thread (pid
, 0); /* run this thread */
2053 dcache_flush (remote_dcache
);
2055 last_sent_signal
= siggnal
;
2056 last_sent_step
= step
;
2058 /* A hook for when we need to do something at the last moment before
2060 if (target_resume_hook
)
2061 (*target_resume_hook
) ();
2063 /* Set things up before execution starts for async commands. */
2064 /* This function can be entered more than once for the same execution
2065 command, because it is also called by handle_inferior_event. So
2066 we make sure that we don't do the initialization for sync
2067 execution more than once. */
2068 if (async_p
&& !target_executing
)
2070 target_executing
= 1;
2072 /* If the command must look synchronous, fake it, by making gdb
2073 display an empty prompt after the command has completed. Also
2077 push_prompt ("", "", "");
2078 delete_file_handler (input_fd
);
2079 initialize_sigint_signal_handler ();
2083 if (siggnal
!= TARGET_SIGNAL_0
)
2085 buf
[0] = step
? 'S' : 'C';
2086 buf
[1] = tohex (((int) siggnal
>> 4) & 0xf);
2087 buf
[2] = tohex ((int) siggnal
& 0xf);
2091 strcpy (buf
, step
? "s" : "c");
2097 /* Set up the signal handler for SIGINT, while the target is
2098 executing, ovewriting the 'regular' SIGINT signal handler. */
2100 initialize_sigint_signal_handler ()
2102 sigint_remote_token
=
2103 create_async_signal_handler (async_remote_interrupt
, NULL
);
2104 signal (SIGINT
, handle_remote_sigint
);
2107 /* Signal handler for SIGINT, while the target is executing. */
2109 handle_remote_sigint (sig
)
2112 signal (sig
, handle_remote_sigint_twice
);
2113 sigint_remote_twice_token
=
2114 create_async_signal_handler (async_remote_interrupt_twice
, NULL
);
2115 mark_async_signal_handler_wrapper (sigint_remote_token
);
2118 /* Signal handler for SIGINT, installed after SIGINT has already been
2119 sent once. It will take effect the second time that the user sends
2122 handle_remote_sigint_twice (sig
)
2125 signal (sig
, handle_sigint
);
2126 sigint_remote_twice_token
=
2127 create_async_signal_handler (async_remote_interrupt
, NULL
);
2128 mark_async_signal_handler_wrapper (sigint_remote_twice_token
);
2131 /* Perform the real interruption of hte target execution, in response
2134 async_remote_interrupt (arg
)
2135 gdb_client_data arg
;
2138 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt called\n");
2143 /* Perform interrupt, if the first attempt did not succeed. Just give
2144 up on the target alltogether. */
2146 async_remote_interrupt_twice (arg
)
2147 gdb_client_data arg
;
2150 signal (SIGINT
, handle_remote_sigint
);
2153 /* Reinstall the usual SIGINT handlers, after the target has
2156 cleanup_sigint_signal_handler ()
2158 signal (SIGINT
, handle_sigint
);
2159 if (sigint_remote_twice_token
)
2160 delete_async_signal_handler ((async_signal_handler
**) & sigint_remote_twice_token
);
2161 if (sigint_remote_token
)
2162 delete_async_signal_handler ((async_signal_handler
**) & sigint_remote_token
);
2165 /* Send ^C to target to halt it. Target will respond, and send us a
2167 static void (*ofunc
) PARAMS ((int));
2169 /* The command line interface's stop routine. This function is installed
2170 as a signal handler for SIGINT. The first time a user requests a
2171 stop, we call remote_stop to send a break or ^C. If there is no
2172 response from the target (it didn't stop when the user requested it),
2173 we ask the user if he'd like to detach from the target. */
2175 remote_interrupt (signo
)
2178 /* If this doesn't work, try more severe steps. */
2179 signal (signo
, remote_interrupt_twice
);
2182 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt called\n");
2187 /* The user typed ^C twice. */
2190 remote_interrupt_twice (signo
)
2193 signal (signo
, ofunc
);
2195 signal (signo
, remote_interrupt
);
2198 /* This is the generic stop called via the target vector. When a target
2199 interrupt is requested, either by the command line or the GUI, we
2200 will eventually end up here. */
2204 /* Send a break or a ^C, depending on user preference. */
2206 fprintf_unfiltered (gdb_stdlog
, "remote_stop called\n");
2209 SERIAL_SEND_BREAK (remote_desc
);
2211 SERIAL_WRITE (remote_desc
, "\003", 1);
2214 /* Ask the user what to do when an interrupt is received. */
2219 target_terminal_ours ();
2221 if (query ("Interrupted while waiting for the program.\n\
2222 Give up (and stop debugging it)? "))
2224 target_mourn_inferior ();
2225 return_to_top_level (RETURN_QUIT
);
2228 target_terminal_inferior ();
2231 /* If nonzero, ignore the next kill. */
2236 remote_console_output (msg
)
2241 for (p
= msg
; p
[0] && p
[1]; p
+= 2)
2244 char c
= fromhex (p
[0]) * 16 + fromhex (p
[1]);
2247 fputs_unfiltered (tb
, gdb_stdtarg
);
2251 /* Wait until the remote machine stops, then return,
2252 storing status in STATUS just as `wait' would.
2253 Returns "pid", which in the case of a multi-threaded
2254 remote OS, is the thread-id. */
2257 remote_wait (pid
, status
)
2259 struct target_waitstatus
*status
;
2261 unsigned char *buf
= alloca (PBUFSIZ
);
2262 int thread_num
= -1;
2264 status
->kind
= TARGET_WAITKIND_EXITED
;
2265 status
->value
.integer
= 0;
2271 ofunc
= signal (SIGINT
, remote_interrupt
);
2272 getpkt ((char *) buf
, 1);
2273 signal (SIGINT
, ofunc
);
2275 /* This is a hook for when we need to do something (perhaps the
2276 collection of trace data) every time the target stops. */
2277 if (target_wait_loop_hook
)
2278 (*target_wait_loop_hook
) ();
2282 case 'E': /* Error of some sort */
2283 warning ("Remote failure reply: %s", buf
);
2285 case 'T': /* Status with PC, SP, FP, ... */
2289 char regs
[MAX_REGISTER_RAW_SIZE
];
2291 /* Expedited reply, containing Signal, {regno, reg} repeat */
2292 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
2294 n... = register number
2295 r... = register contents
2297 p
= &buf
[3]; /* after Txx */
2304 /* Read the register number */
2305 regno
= strtol ((const char *) p
, &p_temp
, 16);
2306 p1
= (unsigned char *) p_temp
;
2308 if (p1
== p
) /* No register number present here */
2310 p1
= (unsigned char *) strchr ((const char *) p
, ':');
2312 warning ("Malformed packet(a) (missing colon): %s\n\
2315 if (strncmp ((const char *) p
, "thread", p1
- p
) == 0)
2317 p_temp
= unpack_varlen_hex (++p1
, &thread_num
);
2318 record_currthread (thread_num
);
2319 p
= (unsigned char *) p_temp
;
2327 warning ("Malformed packet(b) (missing colon): %s\n\
2331 if (regno
>= NUM_REGS
)
2332 warning ("Remote sent bad register number %ld: %s\n\
2336 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
++)
2338 if (p
[0] == 0 || p
[1] == 0)
2339 warning ("Remote reply is too short: %s", buf
);
2340 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
2343 supply_register (regno
, regs
);
2348 warning ("Remote register badly formatted: %s", buf
);
2349 warning (" here: %s", p
);
2354 case 'S': /* Old style status, just signal only */
2355 status
->kind
= TARGET_WAITKIND_STOPPED
;
2356 status
->value
.sig
= (enum target_signal
)
2357 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
2361 /* Export Cisco kernel mode as a convenience variable
2362 (so that it can be used in the GDB prompt if desired). */
2364 if (cisco_kernel_mode
== 1)
2365 set_internalvar (lookup_internalvar ("cisco_kernel_mode"),
2366 value_from_string ("PDEBUG-"));
2367 cisco_kernel_mode
= 0;
2368 thread_num
= strtol ((const char *) &buf
[4], NULL
, 16);
2369 record_currthread (thread_num
);
2371 else if (buf
[3] == 'k')
2373 /* Export Cisco kernel mode as a convenience variable
2374 (so that it can be used in the GDB prompt if desired). */
2376 if (cisco_kernel_mode
== 1)
2377 set_internalvar (lookup_internalvar ("cisco_kernel_mode"),
2378 value_from_string ("KDEBUG-"));
2379 cisco_kernel_mode
= 1;
2382 case 'N': /* Cisco special: status and offsets */
2384 bfd_vma text_addr
, data_addr
, bss_addr
;
2385 bfd_signed_vma text_off
, data_off
, bss_off
;
2388 status
->kind
= TARGET_WAITKIND_STOPPED
;
2389 status
->value
.sig
= (enum target_signal
)
2390 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
2392 if (symfile_objfile
== NULL
)
2394 warning ("Relocation packet recieved with no symbol file. \
2399 /* Relocate object file. Buffer format is NAATT;DD;BB
2400 * where AA is the signal number, TT is the new text
2401 * address, DD * is the new data address, and BB is the
2402 * new bss address. */
2405 text_addr
= strtoul (p
, (char **) &p1
, 16);
2406 if (p1
== p
|| *p1
!= ';')
2407 warning ("Malformed relocation packet: Packet '%s'", buf
);
2409 data_addr
= strtoul (p
, (char **) &p1
, 16);
2410 if (p1
== p
|| *p1
!= ';')
2411 warning ("Malformed relocation packet: Packet '%s'", buf
);
2413 bss_addr
= strtoul (p
, (char **) &p1
, 16);
2415 warning ("Malformed relocation packet: Packet '%s'", buf
);
2417 if (remote_cisco_section_offsets (text_addr
, data_addr
, bss_addr
,
2418 &text_off
, &data_off
, &bss_off
)
2420 if (text_off
!= 0 || data_off
!= 0 || bss_off
!= 0)
2421 remote_cisco_objfile_relocate (text_off
, data_off
, bss_off
);
2425 case 'W': /* Target exited */
2427 /* The remote process exited. */
2428 status
->kind
= TARGET_WAITKIND_EXITED
;
2429 status
->value
.integer
= (fromhex (buf
[1]) << 4) + fromhex (buf
[2]);
2433 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
2434 status
->value
.sig
= (enum target_signal
)
2435 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
2439 case 'O': /* Console output */
2440 remote_console_output (buf
+ 1);
2443 if (last_sent_signal
!= TARGET_SIGNAL_0
)
2445 /* Zero length reply means that we tried 'S' or 'C' and
2446 the remote system doesn't support it. */
2447 target_terminal_ours_for_output ();
2449 ("Can't send signals to this remote system. %s not sent.\n",
2450 target_signal_to_name (last_sent_signal
));
2451 last_sent_signal
= TARGET_SIGNAL_0
;
2452 target_terminal_inferior ();
2454 strcpy ((char *) buf
, last_sent_step
? "s" : "c");
2455 putpkt ((char *) buf
);
2458 /* else fallthrough */
2460 warning ("Invalid remote reply: %s", buf
);
2465 if (thread_num
!= -1)
2469 return inferior_pid
;
2472 /* Async version of remote_wait. */
2474 remote_async_wait (pid
, status
)
2476 struct target_waitstatus
*status
;
2478 unsigned char *buf
= alloca (PBUFSIZ
);
2479 int thread_num
= -1;
2481 status
->kind
= TARGET_WAITKIND_EXITED
;
2482 status
->value
.integer
= 0;
2489 ofunc
= signal (SIGINT
, remote_interrupt
);
2490 getpkt ((char *) buf
, 1);
2492 signal (SIGINT
, ofunc
);
2494 /* This is a hook for when we need to do something (perhaps the
2495 collection of trace data) every time the target stops. */
2496 if (target_wait_loop_hook
)
2497 (*target_wait_loop_hook
) ();
2501 case 'E': /* Error of some sort */
2502 warning ("Remote failure reply: %s", buf
);
2504 case 'T': /* Status with PC, SP, FP, ... */
2508 char regs
[MAX_REGISTER_RAW_SIZE
];
2510 /* Expedited reply, containing Signal, {regno, reg} repeat */
2511 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
2513 n... = register number
2514 r... = register contents
2516 p
= &buf
[3]; /* after Txx */
2523 /* Read the register number */
2524 regno
= strtol ((const char *) p
, &p_temp
, 16);
2525 p1
= (unsigned char *) p_temp
;
2527 if (p1
== p
) /* No register number present here */
2529 p1
= (unsigned char *) strchr ((const char *) p
, ':');
2531 warning ("Malformed packet(a) (missing colon): %s\n\
2534 if (strncmp ((const char *) p
, "thread", p1
- p
) == 0)
2536 p_temp
= unpack_varlen_hex (++p1
, &thread_num
);
2537 record_currthread (thread_num
);
2538 p
= (unsigned char *) p_temp
;
2546 warning ("Malformed packet(b) (missing colon): %s\n\
2550 if (regno
>= NUM_REGS
)
2551 warning ("Remote sent bad register number %ld: %s\n\
2555 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
++)
2557 if (p
[0] == 0 || p
[1] == 0)
2558 warning ("Remote reply is too short: %s", buf
);
2559 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
2562 supply_register (regno
, regs
);
2567 warning ("Remote register badly formatted: %s", buf
);
2568 warning (" here: %s", p
);
2573 case 'S': /* Old style status, just signal only */
2574 status
->kind
= TARGET_WAITKIND_STOPPED
;
2575 status
->value
.sig
= (enum target_signal
)
2576 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
2580 /* Export Cisco kernel mode as a convenience variable
2581 (so that it can be used in the GDB prompt if desired). */
2583 if (cisco_kernel_mode
== 1)
2584 set_internalvar (lookup_internalvar ("cisco_kernel_mode"),
2585 value_from_string ("PDEBUG-"));
2586 cisco_kernel_mode
= 0;
2587 thread_num
= strtol ((const char *) &buf
[4], NULL
, 16);
2588 record_currthread (thread_num
);
2590 else if (buf
[3] == 'k')
2592 /* Export Cisco kernel mode as a convenience variable
2593 (so that it can be used in the GDB prompt if desired). */
2595 if (cisco_kernel_mode
== 1)
2596 set_internalvar (lookup_internalvar ("cisco_kernel_mode"),
2597 value_from_string ("KDEBUG-"));
2598 cisco_kernel_mode
= 1;
2601 case 'N': /* Cisco special: status and offsets */
2603 bfd_vma text_addr
, data_addr
, bss_addr
;
2604 bfd_signed_vma text_off
, data_off
, bss_off
;
2607 status
->kind
= TARGET_WAITKIND_STOPPED
;
2608 status
->value
.sig
= (enum target_signal
)
2609 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
2611 if (symfile_objfile
== NULL
)
2613 warning ("Relocation packet recieved with no symbol file. \
2618 /* Relocate object file. Buffer format is NAATT;DD;BB
2619 * where AA is the signal number, TT is the new text
2620 * address, DD * is the new data address, and BB is the
2621 * new bss address. */
2624 text_addr
= strtoul (p
, (char **) &p1
, 16);
2625 if (p1
== p
|| *p1
!= ';')
2626 warning ("Malformed relocation packet: Packet '%s'", buf
);
2628 data_addr
= strtoul (p
, (char **) &p1
, 16);
2629 if (p1
== p
|| *p1
!= ';')
2630 warning ("Malformed relocation packet: Packet '%s'", buf
);
2632 bss_addr
= strtoul (p
, (char **) &p1
, 16);
2634 warning ("Malformed relocation packet: Packet '%s'", buf
);
2636 if (remote_cisco_section_offsets (text_addr
, data_addr
, bss_addr
,
2637 &text_off
, &data_off
, &bss_off
)
2639 if (text_off
!= 0 || data_off
!= 0 || bss_off
!= 0)
2640 remote_cisco_objfile_relocate (text_off
, data_off
, bss_off
);
2644 case 'W': /* Target exited */
2646 /* The remote process exited. */
2647 status
->kind
= TARGET_WAITKIND_EXITED
;
2648 status
->value
.integer
= (fromhex (buf
[1]) << 4) + fromhex (buf
[2]);
2652 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
2653 status
->value
.sig
= (enum target_signal
)
2654 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
2658 case 'O': /* Console output */
2659 remote_console_output (buf
+ 1);
2662 if (last_sent_signal
!= TARGET_SIGNAL_0
)
2664 /* Zero length reply means that we tried 'S' or 'C' and
2665 the remote system doesn't support it. */
2666 target_terminal_ours_for_output ();
2668 ("Can't send signals to this remote system. %s not sent.\n",
2669 target_signal_to_name (last_sent_signal
));
2670 last_sent_signal
= TARGET_SIGNAL_0
;
2671 target_terminal_inferior ();
2673 strcpy ((char *) buf
, last_sent_step
? "s" : "c");
2674 putpkt ((char *) buf
);
2677 /* else fallthrough */
2679 warning ("Invalid remote reply: %s", buf
);
2684 if (thread_num
!= -1)
2688 return inferior_pid
;
2691 /* Number of bytes of registers this stub implements. */
2693 static int register_bytes_found
;
2695 /* Read the remote registers into the block REGS. */
2696 /* Currently we just read all the registers, so we don't use regno. */
2700 remote_fetch_registers (regno
)
2703 char *buf
= alloca (PBUFSIZ
);
2706 char regs
[REGISTER_BYTES
];
2708 set_thread (inferior_pid
, 1);
2713 if (remote_register_buf_size
== 0)
2714 remote_register_buf_size
= strlen (buf
);
2716 /* Unimplemented registers read as all bits zero. */
2717 memset (regs
, 0, REGISTER_BYTES
);
2719 /* We can get out of synch in various cases. If the first character
2720 in the buffer is not a hex character, assume that has happened
2721 and try to fetch another packet to read. */
2722 while ((buf
[0] < '0' || buf
[0] > '9')
2723 && (buf
[0] < 'a' || buf
[0] > 'f')
2724 && buf
[0] != 'x') /* New: unavailable register value */
2727 fprintf_unfiltered (gdb_stdlog
,
2728 "Bad register packet; fetching a new packet\n");
2732 /* Reply describes registers byte by byte, each byte encoded as two
2733 hex characters. Suck them all up, then supply them to the
2734 register cacheing/storage mechanism. */
2737 for (i
= 0; i
< REGISTER_BYTES
; i
++)
2743 warning ("Remote reply is of odd length: %s", buf
);
2744 /* Don't change register_bytes_found in this case, and don't
2745 print a second warning. */
2748 if (p
[0] == 'x' && p
[1] == 'x')
2749 regs
[i
] = 0; /* 'x' */
2751 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
2755 if (i
!= register_bytes_found
)
2757 register_bytes_found
= i
;
2758 #ifdef REGISTER_BYTES_OK
2759 if (!REGISTER_BYTES_OK (i
))
2760 warning ("Remote reply is too short: %s", buf
);
2765 for (i
= 0; i
< NUM_REGS
; i
++)
2767 supply_register (i
, ®s
[REGISTER_BYTE (i
)]);
2768 if (buf
[REGISTER_BYTE (i
) * 2] == 'x')
2769 register_valid
[i
] = -1; /* register value not available */
2773 /* Prepare to store registers. Since we may send them all (using a
2774 'G' request), we have to read out the ones we don't want to change
2778 remote_prepare_to_store ()
2780 /* Make sure the entire registers array is valid. */
2781 switch (remote_protocol_P
.support
)
2783 case PACKET_DISABLE
:
2784 case PACKET_SUPPORT_UNKNOWN
:
2785 read_register_bytes (0, (char *) NULL
, REGISTER_BYTES
);
2792 /* Helper: Attempt to store REGNO using the P packet. Return fail IFF
2793 packet was not recognized. */
2796 store_register_using_P (regno
)
2799 /* Try storing a single register. */
2800 char *buf
= alloca (PBUFSIZ
);
2805 sprintf (buf
, "P%x=", regno
);
2806 p
= buf
+ strlen (buf
);
2807 regp
= ®isters
[REGISTER_BYTE (regno
)];
2808 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); ++i
)
2810 *p
++ = tohex ((regp
[i
] >> 4) & 0xf);
2811 *p
++ = tohex (regp
[i
] & 0xf);
2816 return buf
[0] != '\0';
2820 /* Store register REGNO, or all registers if REGNO == -1, from the contents
2821 of REGISTERS. FIXME: ignores errors. */
2824 remote_store_registers (regno
)
2827 char *buf
= alloca (PBUFSIZ
);
2831 set_thread (inferior_pid
, 1);
2835 switch (remote_protocol_P
.support
)
2837 case PACKET_DISABLE
:
2840 if (store_register_using_P (regno
))
2843 error ("Protocol error: P packet not recognized by stub");
2844 case PACKET_SUPPORT_UNKNOWN
:
2845 if (store_register_using_P (regno
))
2847 /* The stub recognized the 'P' packet. Remember this. */
2848 remote_protocol_P
.support
= PACKET_ENABLE
;
2853 /* The stub does not support the 'P' packet. Use 'G'
2854 instead, and don't try using 'P' in the future (it
2855 will just waste our time). */
2856 remote_protocol_P
.support
= PACKET_DISABLE
;
2864 /* Command describes registers byte by byte,
2865 each byte encoded as two hex characters. */
2868 /* remote_prepare_to_store insures that register_bytes_found gets set. */
2869 for (i
= 0; i
< register_bytes_found
; i
++)
2871 *p
++ = tohex ((registers
[i
] >> 4) & 0xf);
2872 *p
++ = tohex (registers
[i
] & 0xf);
2879 /* Use of the data cache *used* to be disabled because it loses for looking
2880 at and changing hardware I/O ports and the like. Accepting `volatile'
2881 would perhaps be one way to fix it. Another idea would be to use the
2882 executable file for the text segment (for all SEC_CODE sections?
2883 For all SEC_READONLY sections?). This has problems if you want to
2884 actually see what the memory contains (e.g. self-modifying code,
2885 clobbered memory, user downloaded the wrong thing).
2887 Because it speeds so much up, it's now enabled, if you're playing
2888 with registers you turn it of (set remotecache 0). */
2890 /* Read a word from remote address ADDR and return it.
2891 This goes through the data cache. */
2895 remote_fetch_word (addr
)
2898 return dcache_fetch (remote_dcache
, addr
);
2901 /* Write a word WORD into remote address ADDR.
2902 This goes through the data cache. */
2905 remote_store_word (addr
, word
)
2909 dcache_poke (remote_dcache
, addr
, word
);
2911 #endif /* 0 (unused?) */
2915 /* Return the number of hex digits in num. */
2923 for (i
= 0; num
!= 0; i
++)
2929 /* Set BUF to the hex digits representing NUM. */
2932 hexnumstr (buf
, num
)
2937 int len
= hexnumlen (num
);
2941 for (i
= len
- 1; i
>= 0; i
--)
2943 buf
[i
] = "0123456789abcdef"[(num
& 0xf)];
2950 /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
2953 remote_address_masked (addr
)
2956 if (remote_address_size
> 0
2957 && remote_address_size
< (sizeof (ULONGEST
) * 8))
2959 /* Only create a mask when that mask can safely be constructed
2960 in a ULONGEST variable. */
2962 mask
= (mask
<< remote_address_size
) - 1;
2968 /* Determine whether the remote target supports binary downloading.
2969 This is accomplished by sending a no-op memory write of zero length
2970 to the target at the specified address. It does not suffice to send
2971 the whole packet, since many stubs strip the eighth bit and subsequently
2972 compute a wrong checksum, which causes real havoc with remote_write_bytes.
2974 NOTE: This can still lose if the serial line is not eight-bit
2975 clean. In cases like this, the user should clear "remote
2979 check_binary_download (addr
)
2982 switch (remote_protocol_binary_download
.support
)
2984 case PACKET_DISABLE
:
2988 case PACKET_SUPPORT_UNKNOWN
:
2990 char *buf
= alloca (PBUFSIZ
);
2995 p
+= hexnumstr (p
, (ULONGEST
) addr
);
2997 p
+= hexnumstr (p
, (ULONGEST
) 0);
3001 putpkt_binary (buf
, (int) (p
- buf
));
3007 fprintf_unfiltered (gdb_stdlog
,
3008 "binary downloading NOT suppported by target\n");
3009 remote_protocol_binary_download
.support
= PACKET_DISABLE
;
3014 fprintf_unfiltered (gdb_stdlog
,
3015 "binary downloading suppported by target\n");
3016 remote_protocol_binary_download
.support
= PACKET_ENABLE
;
3023 /* Write memory data directly to the remote machine.
3024 This does not inform the data cache; the data cache uses this.
3025 MEMADDR is the address in the remote memory space.
3026 MYADDR is the address of the buffer in our space.
3027 LEN is the number of bytes.
3029 Returns number of bytes transferred, or 0 for error. */
3032 remote_write_bytes (memaddr
, myaddr
, len
)
3037 unsigned char *buf
= alloca (PBUFSIZ
);
3038 int max_buf_size
; /* Max size of packet output buffer */
3041 /* Verify that the target can support a binary download */
3042 check_binary_download (memaddr
);
3044 /* Chop the transfer down if necessary */
3046 max_buf_size
= min (remote_write_size
, PBUFSIZ
);
3047 if (remote_register_buf_size
!= 0)
3048 max_buf_size
= min (max_buf_size
, remote_register_buf_size
);
3050 /* Subtract header overhead from max payload size - $M<memaddr>,<len>:#nn */
3051 max_buf_size
-= 2 + hexnumlen (memaddr
+ len
- 1) + 1 + hexnumlen (len
) + 4;
3056 unsigned char *p
, *plen
;
3060 /* construct "M"<memaddr>","<len>":" */
3061 /* sprintf (buf, "M%lx,%x:", (unsigned long) memaddr, todo); */
3062 memaddr
= remote_address_masked (memaddr
);
3064 switch (remote_protocol_binary_download
.support
)
3068 todo
= min (len
, max_buf_size
);
3070 case PACKET_DISABLE
:
3072 todo
= min (len
, max_buf_size
/ 2); /* num bytes that will fit */
3074 case PACKET_SUPPORT_UNKNOWN
:
3075 internal_error ("remote_write_bytes: bad switch");
3078 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
3081 plen
= p
; /* remember where len field goes */
3082 p
+= hexnumstr (p
, (ULONGEST
) todo
);
3086 /* We send target system values byte by byte, in increasing byte
3087 addresses, each byte encoded as two hex characters (or one
3088 binary character). */
3089 switch (remote_protocol_binary_download
.support
)
3095 (i
< todo
) && (i
+ escaped
) < (max_buf_size
- 2);
3098 switch (myaddr
[i
] & 0xff)
3103 /* These must be escaped */
3106 *p
++ = (myaddr
[i
] & 0xff) ^ 0x20;
3109 *p
++ = myaddr
[i
] & 0xff;
3116 /* Escape chars have filled up the buffer prematurely,
3117 and we have actually sent fewer bytes than planned.
3118 Fix-up the length field of the packet. */
3120 /* FIXME: will fail if new len is a shorter string than
3123 plen
+= hexnumstr (plen
, (ULONGEST
) i
);
3128 case PACKET_DISABLE
:
3130 for (i
= 0; i
< todo
; i
++)
3132 *p
++ = tohex ((myaddr
[i
] >> 4) & 0xf);
3133 *p
++ = tohex (myaddr
[i
] & 0xf);
3138 case PACKET_SUPPORT_UNKNOWN
:
3139 internal_error ("remote_write_bytes: bad switch");
3142 putpkt_binary (buf
, (int) (p
- buf
));
3147 /* There is no correspondance between what the remote protocol uses
3148 for errors and errno codes. We would like a cleaner way of
3149 representing errors (big enough to include errno codes, bfd_error
3150 codes, and others). But for now just return EIO. */
3155 /* Increment by i, not by todo, in case escape chars
3156 caused us to send fewer bytes than we'd planned. */
3164 /* Read memory data directly from the remote machine.
3165 This does not use the data cache; the data cache uses this.
3166 MEMADDR is the address in the remote memory space.
3167 MYADDR is the address of the buffer in our space.
3168 LEN is the number of bytes.
3170 Returns number of bytes transferred, or 0 for error. */
3173 remote_read_bytes (memaddr
, myaddr
, len
)
3178 char *buf
= alloca (PBUFSIZ
);
3179 int max_buf_size
; /* Max size of packet output buffer */
3182 /* Chop the transfer down if necessary */
3184 max_buf_size
= min (remote_write_size
, PBUFSIZ
);
3185 if (remote_register_buf_size
!= 0)
3186 max_buf_size
= min (max_buf_size
, remote_register_buf_size
);
3195 todo
= min (len
, max_buf_size
/ 2); /* num bytes that will fit */
3197 /* construct "m"<memaddr>","<len>" */
3198 /* sprintf (buf, "m%lx,%x", (unsigned long) memaddr, todo); */
3199 memaddr
= remote_address_masked (memaddr
);
3202 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
3204 p
+= hexnumstr (p
, (ULONGEST
) todo
);
3212 /* There is no correspondance between what the remote protocol uses
3213 for errors and errno codes. We would like a cleaner way of
3214 representing errors (big enough to include errno codes, bfd_error
3215 codes, and others). But for now just return EIO. */
3220 /* Reply describes memory byte by byte,
3221 each byte encoded as two hex characters. */
3224 for (i
= 0; i
< todo
; i
++)
3226 if (p
[0] == 0 || p
[1] == 0)
3227 /* Reply is short. This means that we were able to read
3228 only part of what we wanted to. */
3229 return i
+ (origlen
- len
);
3230 myaddr
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
3240 /* Read or write LEN bytes from inferior memory at MEMADDR,
3241 transferring to or from debugger address BUFFER. Write to inferior if
3242 SHOULD_WRITE is nonzero. Returns length of data written or read; 0
3245 #ifndef REMOTE_TRANSLATE_XFER_ADDRESS
3246 #define REMOTE_TRANSLATE_XFER_ADDRESS(MEM_ADDR, MEM_LEN, TARG_ADDR, TARG_LEN) \
3247 (*(TARG_ADDR) = (MEM_ADDR), *(TARG_LEN) = (MEM_LEN))
3252 remote_xfer_memory (mem_addr
, buffer
, mem_len
, should_write
, target
)
3257 struct target_ops
*target
; /* ignored */
3259 CORE_ADDR targ_addr
;
3261 REMOTE_TRANSLATE_XFER_ADDRESS (mem_addr
, mem_len
, &targ_addr
, &targ_len
);
3265 return dcache_xfer_memory (remote_dcache
, targ_addr
, buffer
,
3266 targ_len
, should_write
);
3271 /* Enable after 4.12. */
3274 remote_search (len
, data
, mask
, startaddr
, increment
, lorange
, hirange
3275 addr_found
, data_found
)
3279 CORE_ADDR startaddr
;
3283 CORE_ADDR
*addr_found
;
3286 if (increment
== -4 && len
== 4)
3288 long mask_long
, data_long
;
3289 long data_found_long
;
3290 CORE_ADDR addr_we_found
;
3291 char *buf
= alloca (PBUFSIZ
);
3292 long returned_long
[2];
3295 mask_long
= extract_unsigned_integer (mask
, len
);
3296 data_long
= extract_unsigned_integer (data
, len
);
3297 sprintf (buf
, "t%x:%x,%x", startaddr
, data_long
, mask_long
);
3302 /* The stub doesn't support the 't' request. We might want to
3303 remember this fact, but on the other hand the stub could be
3304 switched on us. Maybe we should remember it only until
3305 the next "target remote". */
3306 generic_search (len
, data
, mask
, startaddr
, increment
, lorange
,
3307 hirange
, addr_found
, data_found
);
3312 /* There is no correspondance between what the remote protocol uses
3313 for errors and errno codes. We would like a cleaner way of
3314 representing errors (big enough to include errno codes, bfd_error
3315 codes, and others). But for now just use EIO. */
3316 memory_error (EIO
, startaddr
);
3319 while (*p
!= '\0' && *p
!= ',')
3320 addr_we_found
= (addr_we_found
<< 4) + fromhex (*p
++);
3322 error ("Protocol error: short return for search");
3324 data_found_long
= 0;
3325 while (*p
!= '\0' && *p
!= ',')
3326 data_found_long
= (data_found_long
<< 4) + fromhex (*p
++);
3327 /* Ignore anything after this comma, for future extensions. */
3329 if (addr_we_found
< lorange
|| addr_we_found
>= hirange
)
3335 *addr_found
= addr_we_found
;
3336 *data_found
= store_unsigned_integer (data_we_found
, len
);
3339 generic_search (len
, data
, mask
, startaddr
, increment
, lorange
,
3340 hirange
, addr_found
, data_found
);
3345 remote_files_info (ignore
)
3346 struct target_ops
*ignore
;
3348 puts_filtered ("Debugging a target over a serial line.\n");
3351 /* Stuff for dealing with the packets which are part of this protocol.
3352 See comment at top of file for details. */
3354 /* Read a single character from the remote end, masking it down to 7 bits. */
3362 ch
= SERIAL_READCHAR (remote_desc
, timeout
);
3367 error ("Remote connection closed");
3369 perror_with_name ("Remote communication error");
3370 case SERIAL_TIMEOUT
:
3377 /* Send the command in BUF to the remote machine, and read the reply
3378 into BUF. Report an error if we get an error reply. */
3388 error ("Remote failure reply: %s", buf
);
3391 /* Display a null-terminated packet on stdout, for debugging, using C
3398 puts_filtered ("\"");
3399 fputstr_filtered (buf
, '"', gdb_stdout
);
3400 puts_filtered ("\"");
3407 return putpkt_binary (buf
, strlen (buf
));
3410 /* Send a packet to the remote machine, with error checking. The data
3411 of the packet is in BUF. The string in BUF can be at most PBUFSIZ - 5
3412 to account for the $, # and checksum, and for a possible /0 if we are
3413 debugging (remote_debug) and want to print the sent packet as a string */
3416 putpkt_binary (buf
, cnt
)
3421 unsigned char csum
= 0;
3422 char *buf2
= alloca (PBUFSIZ
);
3423 char *junkbuf
= alloca (PBUFSIZ
);
3429 /* Copy the packet into buffer BUF2, encapsulating it
3430 and giving it a checksum. */
3432 if (cnt
> BUFSIZ
- 5) /* Prosanity check */
3438 for (i
= 0; i
< cnt
; i
++)
3444 *p
++ = tohex ((csum
>> 4) & 0xf);
3445 *p
++ = tohex (csum
& 0xf);
3447 /* Send it over and over until we get a positive ack. */
3451 int started_error_output
= 0;
3456 fprintf_unfiltered (gdb_stdlog
, "Sending packet: ");
3457 fputstrn_unfiltered (buf2
, p
- buf2
, 0, gdb_stdlog
);
3458 fprintf_unfiltered (gdb_stdlog
, "...", buf2
);
3459 gdb_flush (gdb_stdlog
);
3461 if (SERIAL_WRITE (remote_desc
, buf2
, p
- buf2
))
3462 perror_with_name ("putpkt: write failed");
3464 /* read until either a timeout occurs (-2) or '+' is read */
3467 ch
= readchar (remote_timeout
);
3474 case SERIAL_TIMEOUT
:
3476 if (started_error_output
)
3478 putchar_unfiltered ('\n');
3479 started_error_output
= 0;
3488 fprintf_unfiltered (gdb_stdlog
, "Ack\n");
3490 case SERIAL_TIMEOUT
:
3494 break; /* Retransmit buffer */
3497 /* It's probably an old response, and we're out of sync.
3498 Just gobble up the packet and ignore it. */
3499 getpkt (junkbuf
, 0);
3500 continue; /* Now, go look for + */
3505 if (!started_error_output
)
3507 started_error_output
= 1;
3508 fprintf_unfiltered (gdb_stdlog
, "putpkt: Junk: ");
3510 fputc_unfiltered (ch
& 0177, gdb_stdlog
);
3514 break; /* Here to retransmit */
3518 /* This is wrong. If doing a long backtrace, the user should be
3519 able to get out next time we call QUIT, without anything as
3520 violent as interrupt_query. If we want to provide a way out of
3521 here without getting to the next QUIT, it should be based on
3522 hitting ^C twice as in remote_wait. */
3532 static int remote_cisco_mode
;
3535 remote_cisco_expand (src
, dest
)
3546 repeat
= (fromhex (src
[1]) << 4) + fromhex (src
[2]);
3547 for (i
= 0; i
< repeat
; i
++)
3549 *dest
++ = *(src
- 1);
3561 /* Come here after finding the start of the frame. Collect the rest
3562 into BUF, verifying the checksum, length, and handling run-length
3563 compression. Returns 0 on any error, 1 on success. */
3578 c
= readchar (remote_timeout
);
3582 case SERIAL_TIMEOUT
:
3584 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog
);
3588 fputs_filtered ("Saw new packet start in middle of old one\n",
3590 return 0; /* Start a new packet, count retries */
3593 unsigned char pktcsum
;
3597 pktcsum
= fromhex (readchar (remote_timeout
)) << 4;
3598 pktcsum
|= fromhex (readchar (remote_timeout
));
3600 if (csum
== pktcsum
)
3602 if (remote_cisco_mode
) /* variant run-length-encoding */
3604 char *tmp_buf
= alloca (PBUFSIZ
);
3606 remote_cisco_expand (buf
, tmp_buf
);
3607 strcpy (buf
, tmp_buf
);
3614 fprintf_filtered (gdb_stdlog
,
3615 "Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
3617 fputs_filtered (buf
, gdb_stdlog
);
3618 fputs_filtered ("\n", gdb_stdlog
);
3622 case '*': /* Run length encoding */
3623 if (remote_cisco_mode
== 0) /* variant run-length-encoding */
3626 c
= readchar (remote_timeout
);
3628 c
= c
- ' ' + 3; /* Compute repeat count */
3630 if (c
> 0 && c
< 255 && bp
+ c
- 1 < buf
+ PBUFSIZ
- 1)
3632 memset (bp
, *(bp
- 1), c
);
3638 printf_filtered ("Repeat count %d too large for buffer: ", c
);
3639 puts_filtered (buf
);
3640 puts_filtered ("\n");
3643 /* else fall thru to treat like default */
3645 if (bp
< buf
+ PBUFSIZ
- 1)
3653 puts_filtered ("Remote packet too long: ");
3654 puts_filtered (buf
);
3655 puts_filtered ("\n");
3662 /* Read a packet from the remote machine, with error checking, and
3663 store it in BUF. BUF is expected to be of size PBUFSIZ. If
3664 FOREVER, wait forever rather than timing out; this is used while
3665 the target is executing user code. */
3668 getpkt (buf
, forever
)
3677 strcpy (buf
, "timeout");
3681 timeout
= watchdog
> 0 ? watchdog
: -1;
3685 timeout
= remote_timeout
;
3689 for (tries
= 1; tries
<= MAX_TRIES
; tries
++)
3691 /* This can loop forever if the remote side sends us characters
3692 continuously, but if it pauses, we'll get a zero from readchar
3693 because of timeout. Then we'll count that as a retry. */
3695 /* Note that we will only wait forever prior to the start of a packet.
3696 After that, we expect characters to arrive at a brisk pace. They
3697 should show up within remote_timeout intervals. */
3701 c
= readchar (timeout
);
3703 if (c
== SERIAL_TIMEOUT
)
3705 if (forever
) /* Watchdog went off. Kill the target. */
3707 target_mourn_inferior ();
3708 error ("Watchdog has expired. Target detached.\n");
3711 fputs_filtered ("Timed out.\n", gdb_stdlog
);
3717 /* We've found the start of a packet, now collect the data. */
3719 val
= read_frame (buf
);
3725 fprintf_unfiltered (gdb_stdlog
, "Packet received: ");
3726 fputstr_unfiltered (buf
, 0, gdb_stdlog
);
3727 fprintf_unfiltered (gdb_stdlog
, "\n");
3729 SERIAL_WRITE (remote_desc
, "+", 1);
3733 /* Try the whole thing again. */
3735 SERIAL_WRITE (remote_desc
, "-", 1);
3738 /* We have tried hard enough, and just can't receive the packet. Give up. */
3740 printf_unfiltered ("Ignoring packet error, continuing...\n");
3741 SERIAL_WRITE (remote_desc
, "+", 1);
3747 /* For some mysterious reason, wait_for_inferior calls kill instead of
3748 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
3752 target_mourn_inferior ();
3756 /* Use catch_errors so the user can quit from gdb even when we aren't on
3757 speaking terms with the remote system. */
3758 catch_errors ((catch_errors_ftype
*) putpkt
, "k", "", RETURN_MASK_ERROR
);
3760 /* Don't wait for it to die. I'm not really sure it matters whether
3761 we do or not. For the existing stubs, kill is a noop. */
3762 target_mourn_inferior ();
3765 /* Async version of remote_kill. */
3767 remote_async_kill ()
3769 /* Unregister the file descriptor from the event loop. */
3771 delete_file_handler (remote_desc
->fd
);
3773 /* For some mysterious reason, wait_for_inferior calls kill instead of
3774 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
3778 target_mourn_inferior ();
3782 /* Use catch_errors so the user can quit from gdb even when we aren't on
3783 speaking terms with the remote system. */
3784 catch_errors ((catch_errors_ftype
*) putpkt
, "k", "", RETURN_MASK_ERROR
);
3786 /* Don't wait for it to die. I'm not really sure it matters whether
3787 we do or not. For the existing stubs, kill is a noop. */
3788 target_mourn_inferior ();
3794 remote_mourn_1 (&remote_ops
);
3798 remote_async_mourn ()
3800 remote_mourn_1 (&remote_async_ops
);
3804 extended_remote_mourn ()
3806 /* We do _not_ want to mourn the target like this; this will
3807 remove the extended remote target from the target stack,
3808 and the next time the user says "run" it'll fail.
3810 FIXME: What is the right thing to do here? */
3812 remote_mourn_1 (&extended_remote_ops
);
3816 /* Worker function for remote_mourn. */
3818 remote_mourn_1 (target
)
3819 struct target_ops
*target
;
3821 unpush_target (target
);
3822 generic_mourn_inferior ();
3825 /* In the extended protocol we want to be able to do things like
3826 "run" and have them basically work as expected. So we need
3827 a special create_inferior function.
3829 FIXME: One day add support for changing the exec file
3830 we're debugging, arguments and an environment. */
3833 extended_remote_create_inferior (exec_file
, args
, env
)
3838 /* Rip out the breakpoints; we'll reinsert them after restarting
3839 the remote server. */
3840 remove_breakpoints ();
3842 /* Now restart the remote server. */
3843 extended_remote_restart ();
3845 /* Now put the breakpoints back in. This way we're safe if the
3846 restart function works via a unix fork on the remote side. */
3847 insert_breakpoints ();
3849 /* Clean up from the last time we were running. */
3850 clear_proceed_status ();
3852 /* Let the remote process run. */
3853 proceed (-1, TARGET_SIGNAL_0
, 0);
3856 /* Async version of extended_remote_create_inferior. */
3858 extended_remote_async_create_inferior (exec_file
, args
, env
)
3863 /* Rip out the breakpoints; we'll reinsert them after restarting
3864 the remote server. */
3865 remove_breakpoints ();
3867 /* If running asynchronously, register the target file descriptor
3868 with the event loop. */
3870 add_file_handler (remote_desc
->fd
, fetch_inferior_event
, 0);
3872 /* Now restart the remote server. */
3873 extended_remote_restart ();
3875 /* Now put the breakpoints back in. This way we're safe if the
3876 restart function works via a unix fork on the remote side. */
3877 insert_breakpoints ();
3879 /* Clean up from the last time we were running. */
3880 clear_proceed_status ();
3882 /* Let the remote process run. */
3883 proceed (-1, TARGET_SIGNAL_0
, 0);
3887 /* On some machines, e.g. 68k, we may use a different breakpoint instruction
3888 than other targets; in those use REMOTE_BREAKPOINT instead of just
3889 BREAKPOINT. Also, bi-endian targets may define LITTLE_REMOTE_BREAKPOINT
3890 and BIG_REMOTE_BREAKPOINT. If none of these are defined, we just call
3891 the standard routines that are in mem-break.c. */
3893 /* FIXME, these ought to be done in a more dynamic fashion. For instance,
3894 the choice of breakpoint instruction affects target program design and
3895 vice versa, and by making it user-tweakable, the special code here
3896 goes away and we need fewer special GDB configurations. */
3898 #if defined (LITTLE_REMOTE_BREAKPOINT) && defined (BIG_REMOTE_BREAKPOINT) && !defined(REMOTE_BREAKPOINT)
3899 #define REMOTE_BREAKPOINT
3902 #ifdef REMOTE_BREAKPOINT
3904 /* If the target isn't bi-endian, just pretend it is. */
3905 #if !defined (LITTLE_REMOTE_BREAKPOINT) && !defined (BIG_REMOTE_BREAKPOINT)
3906 #define LITTLE_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
3907 #define BIG_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
3910 static unsigned char big_break_insn
[] = BIG_REMOTE_BREAKPOINT
;
3911 static unsigned char little_break_insn
[] = LITTLE_REMOTE_BREAKPOINT
;
3913 #endif /* REMOTE_BREAKPOINT */
3915 /* Insert a breakpoint on targets that don't have any better breakpoint
3916 support. We read the contents of the target location and stash it,
3917 then overwrite it with a breakpoint instruction. ADDR is the target
3918 location in the target machine. CONTENTS_CACHE is a pointer to
3919 memory allocated for saving the target contents. It is guaranteed
3920 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
3921 is accomplished via BREAKPOINT_MAX). */
3924 remote_insert_breakpoint (addr
, contents_cache
)
3926 char *contents_cache
;
3928 #ifdef REMOTE_BREAKPOINT
3933 /* Try the "Z" packet if it is not already disabled.
3934 If it succeeds, then set the support to PACKET_ENABLE.
3935 If it fails, and the user has explicitly requested the Z support
3936 then report an error, otherwise, mark it disabled and go on. */
3938 if ((remote_protocol_Z
.support
== PACKET_ENABLE
)
3939 || (remote_protocol_Z
.support
== PACKET_SUPPORT_UNKNOWN
))
3941 char buf
[PBUFSIZ
], *p
= buf
;
3943 addr
= remote_address_masked (addr
);
3947 p
+= hexnumstr (p
, (ULONGEST
) addr
);
3948 BREAKPOINT_FROM_PC (&addr
, &bp_size
);
3949 sprintf (p
, ",%d", bp_size
);
3956 remote_protocol_Z
.support
= PACKET_ENABLE
;
3957 return (buf
[0] == 'E');
3960 /* The stub does not support the 'Z' request. If the user has
3961 explicitly requested the Z support, or if the stub previously
3962 said it supported the packet, this is an error,
3963 otherwise, mark it disabled. */
3965 else if (remote_protocol_Z
.support
== PACKET_ENABLE
)
3967 error ("Protocol error: Z packet not recognized by stub");
3971 remote_protocol_Z
.support
= PACKET_DISABLE
;
3975 #ifdef REMOTE_BREAKPOINT
3976 val
= target_read_memory (addr
, contents_cache
, sizeof big_break_insn
);
3980 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
3981 val
= target_write_memory (addr
, (char *) big_break_insn
,
3982 sizeof big_break_insn
);
3984 val
= target_write_memory (addr
, (char *) little_break_insn
,
3985 sizeof little_break_insn
);
3990 return memory_insert_breakpoint (addr
, contents_cache
);
3991 #endif /* REMOTE_BREAKPOINT */
3995 remote_remove_breakpoint (addr
, contents_cache
)
3997 char *contents_cache
;
4002 if ((remote_protocol_Z
.support
== PACKET_ENABLE
)
4003 || (remote_protocol_Z
.support
== PACKET_SUPPORT_UNKNOWN
))
4005 char buf
[PBUFSIZ
], *p
= buf
;
4011 addr
= remote_address_masked (addr
);
4012 p
+= hexnumstr (p
, (ULONGEST
) addr
);
4013 BREAKPOINT_FROM_PC (&addr
, &bp_size
);
4014 sprintf (p
, ",%d", bp_size
);
4019 return (buf
[0] == 'E');
4022 #ifdef REMOTE_BREAKPOINT
4023 return target_write_memory (addr
, contents_cache
, sizeof big_break_insn
);
4025 return memory_remove_breakpoint (addr
, contents_cache
);
4026 #endif /* REMOTE_BREAKPOINT */
4029 #ifdef TARGET_HAS_HARDWARE_WATCHPOINTS
4031 remote_insert_watchpoint (addr
, len
, type
)
4036 char buf
[PBUFSIZ
], *p
;
4038 if (remote_protocol_Z
.support
== PACKET_DISABLE
)
4039 error ("Can't set hardware watchpoints without the 'Z' packet\n");
4041 sprintf (buf
, "Z%x,", type
+ 2 );
4042 p
= strchr (buf
, '\0');
4043 addr
= remote_address_masked (addr
);
4044 p
+= hexnumstr (p
, (ULONGEST
) addr
);
4045 sprintf (p
, ",%lx", len
);
4050 if (buf
[0] == '\0' || buf
[0] == 'E')
4057 remote_remove_watchpoint (addr
, len
, type
)
4062 char buf
[PBUFSIZ
], *p
;
4064 sprintf (buf
, "z%x,", type
+ 2 );
4065 p
= strchr (buf
, '\0');
4066 addr
= remote_address_masked (addr
);
4067 p
+= hexnumstr (p
, (ULONGEST
) addr
);
4068 sprintf (p
, ",%lx", len
);
4072 if (buf
[0] == '\0' || buf
[0] == 'E')
4079 remote_insert_hw_breakpoint (addr
, len
)
4083 char buf
[PBUFSIZ
], *p
= buf
;
4085 if (remote_protocol_Z
.support
== PACKET_DISABLE
)
4086 error ("Can't set hardware breakpoints without the 'Z' packet\n");
4092 addr
= remote_address_masked (addr
);
4093 p
+= hexnumstr (p
, (ULONGEST
) addr
);
4099 if (buf
[0] == '\0' || buf
[0] == 'E')
4106 remote_remove_hw_breakpoint (addr
, len
)
4110 char buf
[PBUFSIZ
], *p
= buf
;
4116 addr
= remote_address_masked (addr
);
4117 p
+= hexnumstr (p
, (ULONGEST
) addr
);
4123 if (buf
[0] == '\0' || buf
[0] == 'E')
4130 /* Some targets are only capable of doing downloads, and afterwards
4131 they switch to the remote serial protocol. This function provides
4132 a clean way to get from the download target to the remote target.
4133 It's basically just a wrapper so that we don't have to expose any
4134 of the internal workings of remote.c.
4136 Prior to calling this routine, you should shutdown the current
4137 target code, else you will get the "A program is being debugged
4138 already..." message. Usually a call to pop_target() suffices. */
4141 push_remote_target (name
, from_tty
)
4145 printf_filtered ("Switching to remote protocol\n");
4146 remote_open (name
, from_tty
);
4149 /* Other targets want to use the entire remote serial module but with
4150 certain remote_ops overridden. */
4153 open_remote_target (name
, from_tty
, target
, extended_p
)
4156 struct target_ops
*target
;
4159 printf_filtered ("Selecting the %sremote protocol\n",
4160 (extended_p
? "extended-" : ""));
4161 remote_open_1 (name
, from_tty
, target
, extended_p
);
4164 /* Table used by the crc32 function to calcuate the checksum. */
4166 static unsigned long crc32_table
[256] =
4169 static unsigned long
4170 crc32 (buf
, len
, crc
)
4175 if (!crc32_table
[1])
4177 /* Initialize the CRC table and the decoding table. */
4181 for (i
= 0; i
< 256; i
++)
4183 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
4184 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
4191 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ *buf
) & 255];
4197 /* compare-sections command
4199 With no arguments, compares each loadable section in the exec bfd
4200 with the same memory range on the target, and reports mismatches.
4201 Useful for verifying the image on the target against the exec file.
4202 Depends on the target understanding the new "qCRC:" request. */
4205 compare_sections_command (args
, from_tty
)
4210 unsigned long host_crc
, target_crc
;
4211 extern bfd
*exec_bfd
;
4212 struct cleanup
*old_chain
;
4216 char *buf
= alloca (PBUFSIZ
);
4223 error ("command cannot be used without an exec file");
4224 if (!current_target
.to_shortname
||
4225 strcmp (current_target
.to_shortname
, "remote") != 0)
4226 error ("command can only be used with remote target");
4228 for (s
= exec_bfd
->sections
; s
; s
= s
->next
)
4230 if (!(s
->flags
& SEC_LOAD
))
4231 continue; /* skip non-loadable section */
4233 size
= bfd_get_section_size_before_reloc (s
);
4235 continue; /* skip zero-length section */
4237 sectname
= (char *) bfd_get_section_name (exec_bfd
, s
);
4238 if (args
&& strcmp (args
, sectname
) != 0)
4239 continue; /* not the section selected by user */
4241 matched
= 1; /* do this section */
4243 /* FIXME: assumes lma can fit into long */
4244 sprintf (buf
, "qCRC:%lx,%lx", (long) lma
, (long) size
);
4247 /* be clever; compute the host_crc before waiting for target reply */
4248 sectdata
= xmalloc (size
);
4249 old_chain
= make_cleanup (free
, sectdata
);
4250 bfd_get_section_contents (exec_bfd
, s
, sectdata
, 0, size
);
4251 host_crc
= crc32 ((unsigned char *) sectdata
, size
, 0xffffffff);
4255 error ("target memory fault, section %s, range 0x%08x -- 0x%08x",
4256 sectname
, lma
, lma
+ size
);
4258 error ("remote target does not support this operation");
4260 for (target_crc
= 0, tmp
= &buf
[1]; *tmp
; tmp
++)
4261 target_crc
= target_crc
* 16 + fromhex (*tmp
);
4263 printf_filtered ("Section %s, range 0x%08x -- 0x%08x: ",
4264 sectname
, lma
, lma
+ size
);
4265 if (host_crc
== target_crc
)
4266 printf_filtered ("matched.\n");
4269 printf_filtered ("MIS-MATCHED!\n");
4273 do_cleanups (old_chain
);
4276 warning ("One or more sections of the remote executable does not match\n\
4277 the loaded file\n");
4278 if (args
&& !matched
)
4279 printf_filtered ("No loaded section named '%s'.\n", args
);
4283 remote_query (query_type
, buf
, outbuf
, bufsiz
)
4290 char *buf2
= alloca (PBUFSIZ
);
4291 char *p2
= &buf2
[0];
4295 error ("null pointer to remote bufer size specified");
4297 /* minimum outbuf size is PBUFSIZ - if bufsiz is not large enough let
4298 the caller know and return what the minimum size is */
4299 /* Note: a zero bufsiz can be used to query the minimum buffer size */
4300 if (*bufsiz
< PBUFSIZ
)
4306 /* except for querying the minimum buffer size, target must be open */
4308 error ("remote query is only available after target open");
4310 /* we only take uppercase letters as query types, at least for now */
4311 if ((query_type
< 'A') || (query_type
> 'Z'))
4312 error ("invalid remote query type");
4315 error ("null remote query specified");
4318 error ("remote query requires a buffer to receive data");
4325 /* we used one buffer char for the remote protocol q command and another
4326 for the query type. As the remote protocol encapsulation uses 4 chars
4327 plus one extra in case we are debugging (remote_debug),
4328 we have PBUFZIZ - 7 left to pack the query string */
4330 while (buf
[i
] && (i
< (PBUFSIZ
- 8)))
4332 /* bad caller may have sent forbidden characters */
4333 if ((!isprint (buf
[i
])) || (buf
[i
] == '$') || (buf
[i
] == '#'))
4334 error ("illegal characters in query string");
4342 error ("query larger than available buffer");
4354 remote_rcmd (char *command
,
4355 struct gdb_file
*outbuf
)
4358 char *buf
= alloca (PBUFSIZ
);
4362 error ("remote rcmd is only available after target open");
4364 /* Send a NULL command across as an empty command */
4365 if (command
== NULL
)
4368 /* The query prefix */
4369 strcpy (buf
, "qRcmd,");
4370 p
= strchr (buf
, '\0');
4372 if ((strlen (buf
) + strlen (command
) * 2 + 8/*misc*/) > PBUFSIZ
)
4373 error ("\"monitor\" command ``%s'' is too long\n", command
);
4375 /* Encode the actual command */
4376 for (i
= 0; command
[i
]; i
++)
4378 *p
++ = tohex ((command
[i
] >> 4) & 0xf);
4379 *p
++ = tohex (command
[i
] & 0xf);
4383 if (putpkt (buf
) < 0)
4384 error ("Communication problem with target\n");
4386 /* get/display the response */
4389 /* XXX - see also tracepoint.c:remote_get_noisy_reply() */
4393 error ("Target does not support this command\n");
4394 if (buf
[0] == 'O' && buf
[1] != 'K')
4396 remote_console_output (buf
+ 1); /* 'O' message from stub */
4399 if (strcmp (buf
, "OK") == 0)
4401 if (strlen (buf
) == 3 && buf
[0] == 'E'
4402 && isdigit (buf
[1]) && isdigit (buf
[2]))
4404 error ("Protocol error with Rcmd");
4406 for (p
= buf
; p
[0] != '\0' && p
[1] != '\0'; p
+= 2)
4408 char c
= (fromhex (p
[0]) << 4) + fromhex (p
[1]);
4409 fputc_unfiltered (c
, outbuf
);
4416 packet_command (args
, from_tty
)
4420 char *buf
= alloca (PBUFSIZ
);
4423 error ("command can only be used with remote target");
4426 error ("remote-packet command requires packet text as argument");
4428 puts_filtered ("sending: ");
4429 print_packet (args
);
4430 puts_filtered ("\n");
4434 puts_filtered ("received: ");
4436 puts_filtered ("\n");
4440 /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------------- */
4442 static void display_thread_info
PARAMS ((struct gdb_ext_thread_info
* info
));
4444 static void threadset_test_cmd
PARAMS ((char *cmd
, int tty
));
4446 static void threadalive_test
PARAMS ((char *cmd
, int tty
));
4448 static void threadlist_test_cmd
PARAMS ((char *cmd
, int tty
));
4450 int get_and_display_threadinfo
PARAMS ((threadref
* ref
));
4452 static void threadinfo_test_cmd
PARAMS ((char *cmd
, int tty
));
4454 static int thread_display_step
PARAMS ((threadref
* ref
, void *context
));
4456 static void threadlist_update_test_cmd
PARAMS ((char *cmd
, int tty
));
4458 static void init_remote_threadtests
PARAMS ((void));
4460 #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid */
4463 threadset_test_cmd (cmd
, tty
)
4467 int sample_thread
= SAMPLE_THREAD
;
4469 printf_filtered ("Remote threadset test\n");
4470 set_thread (sample_thread
, 1);
4475 threadalive_test (cmd
, tty
)
4479 int sample_thread
= SAMPLE_THREAD
;
4481 if (remote_thread_alive (sample_thread
))
4482 printf_filtered ("PASS: Thread alive test\n");
4484 printf_filtered ("FAIL: Thread alive test\n");
4487 void output_threadid
PARAMS ((char *title
, threadref
* ref
));
4490 output_threadid (title
, ref
)
4496 pack_threadid (&hexid
[0], ref
); /* Convert threead id into hex */
4498 printf_filtered ("%s %s\n", title
, (&hexid
[0]));
4502 threadlist_test_cmd (cmd
, tty
)
4507 threadref nextthread
;
4508 int done
, result_count
;
4509 threadref threadlist
[3];
4511 printf_filtered ("Remote Threadlist test\n");
4512 if (!remote_get_threadlist (startflag
, &nextthread
, 3, &done
,
4513 &result_count
, &threadlist
[0]))
4514 printf_filtered ("FAIL: threadlist test\n");
4517 threadref
*scan
= threadlist
;
4518 threadref
*limit
= scan
+ result_count
;
4520 while (scan
< limit
)
4521 output_threadid (" thread ", scan
++);
4526 display_thread_info (info
)
4527 struct gdb_ext_thread_info
*info
;
4529 output_threadid ("Threadid: ", &info
->threadid
);
4530 printf_filtered ("Name: %s\n ", info
->shortname
);
4531 printf_filtered ("State: %s\n", info
->display
);
4532 printf_filtered ("other: %s\n\n", info
->more_display
);
4536 get_and_display_threadinfo (ref
)
4541 struct gdb_ext_thread_info threadinfo
;
4543 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
4544 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
4545 if (0 != (result
= remote_get_threadinfo (ref
, set
, &threadinfo
)))
4546 display_thread_info (&threadinfo
);
4551 threadinfo_test_cmd (cmd
, tty
)
4555 int athread
= SAMPLE_THREAD
;
4559 int_to_threadref (&thread
, athread
);
4560 printf_filtered ("Remote Threadinfo test\n");
4561 if (!get_and_display_threadinfo (&thread
))
4562 printf_filtered ("FAIL cannot get thread info\n");
4566 thread_display_step (ref
, context
)
4570 /* output_threadid(" threadstep ",ref); *//* simple test */
4571 return get_and_display_threadinfo (ref
);
4575 threadlist_update_test_cmd (cmd
, tty
)
4579 printf_filtered ("Remote Threadlist update test\n");
4580 remote_threadlist_iterator (thread_display_step
, 0, CRAZY_MAX_THREADS
);
4584 init_remote_threadtests (void)
4586 add_com ("tlist", class_obscure
, threadlist_test_cmd
,
4587 "Fetch and print the remote list of thread identifiers, one pkt only");
4588 add_com ("tinfo", class_obscure
, threadinfo_test_cmd
,
4589 "Fetch and display info about one thread");
4590 add_com ("tset", class_obscure
, threadset_test_cmd
,
4591 "Test setting to a different thread");
4592 add_com ("tupd", class_obscure
, threadlist_update_test_cmd
,
4593 "Iterate through updating all remote thread info");
4594 add_com ("talive", class_obscure
, threadalive_test
,
4595 " Remote thread alive test ");
4603 remote_ops
.to_shortname
= "remote";
4604 remote_ops
.to_longname
= "Remote serial target in gdb-specific protocol";
4606 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
4607 Specify the serial device it is connected to (e.g. /dev/ttya).";
4608 remote_ops
.to_open
= remote_open
;
4609 remote_ops
.to_close
= remote_close
;
4610 remote_ops
.to_detach
= remote_detach
;
4611 remote_ops
.to_resume
= remote_resume
;
4612 remote_ops
.to_wait
= remote_wait
;
4613 remote_ops
.to_fetch_registers
= remote_fetch_registers
;
4614 remote_ops
.to_store_registers
= remote_store_registers
;
4615 remote_ops
.to_prepare_to_store
= remote_prepare_to_store
;
4616 remote_ops
.to_xfer_memory
= remote_xfer_memory
;
4617 remote_ops
.to_files_info
= remote_files_info
;
4618 remote_ops
.to_insert_breakpoint
= remote_insert_breakpoint
;
4619 remote_ops
.to_remove_breakpoint
= remote_remove_breakpoint
;
4620 remote_ops
.to_kill
= remote_kill
;
4621 remote_ops
.to_load
= generic_load
;
4622 remote_ops
.to_mourn_inferior
= remote_mourn
;
4623 remote_ops
.to_thread_alive
= remote_thread_alive
;
4624 remote_ops
.to_find_new_threads
= remote_threads_info
;
4625 remote_ops
.to_stop
= remote_stop
;
4626 remote_ops
.to_query
= remote_query
;
4627 remote_ops
.to_rcmd
= remote_rcmd
;
4628 remote_ops
.to_stratum
= process_stratum
;
4629 remote_ops
.to_has_all_memory
= 1;
4630 remote_ops
.to_has_memory
= 1;
4631 remote_ops
.to_has_stack
= 1;
4632 remote_ops
.to_has_registers
= 1;
4633 remote_ops
.to_has_execution
= 1;
4634 remote_ops
.to_has_thread_control
= tc_schedlock
; /* can lock scheduler */
4635 remote_ops
.to_magic
= OPS_MAGIC
;
4638 /* Set up the extended remote vector by making a copy of the standard
4639 remote vector and adding to it. */
4642 init_extended_remote_ops ()
4644 extended_remote_ops
= remote_ops
;
4646 extended_remote_ops
.to_shortname
= "extended-remote";
4647 extended_remote_ops
.to_longname
=
4648 "Extended remote serial target in gdb-specific protocol";
4649 extended_remote_ops
.to_doc
=
4650 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
4651 Specify the serial device it is connected to (e.g. /dev/ttya).",
4652 extended_remote_ops
.to_open
= extended_remote_open
;
4653 extended_remote_ops
.to_create_inferior
= extended_remote_create_inferior
;
4654 extended_remote_ops
.to_mourn_inferior
= extended_remote_mourn
;
4658 * Command: info remote-process
4660 * This implements Cisco's version of the "info proc" command.
4662 * This query allows the target stub to return an arbitrary string
4663 * (or strings) giving arbitrary information about the target process.
4664 * This is optional; the target stub isn't required to implement it.
4666 * Syntax: qfProcessInfo request first string
4667 * qsProcessInfo request subsequent string
4668 * reply: 'O'<hex-encoded-string>
4669 * 'l' last reply (empty)
4673 remote_info_process (args
, from_tty
)
4677 char *buf
= alloca (PBUFSIZ
);
4679 if (remote_desc
== 0)
4680 error ("Command can only be used when connected to the remote target.");
4682 putpkt ("qfProcessInfo");
4685 return; /* Silently: target does not support this feature. */
4688 error ("info proc: target error.");
4690 while (buf
[0] == 'O') /* Capitol-O packet */
4692 remote_console_output (&buf
[1]);
4693 putpkt ("qsProcessInfo");
4703 remote_cisco_open (name
, from_tty
)
4709 "To open a remote debug connection, you need to specify what \n\
4710 device is attached to the remote system (e.g. host:port).");
4712 target_preopen (from_tty
);
4714 unpush_target (&remote_cisco_ops
);
4716 remote_dcache
= dcache_init (remote_read_bytes
, remote_write_bytes
);
4718 remote_desc
= SERIAL_OPEN (name
);
4720 perror_with_name (name
);
4723 * If a baud rate was specified on the gdb command line it will
4724 * be greater than the initial value of -1. If it is, use it otherwise
4728 baud_rate
= (baud_rate
> 0) ? baud_rate
: 9600;
4729 if (SERIAL_SETBAUDRATE (remote_desc
, baud_rate
))
4731 SERIAL_CLOSE (remote_desc
);
4732 perror_with_name (name
);
4735 SERIAL_RAW (remote_desc
);
4737 /* If there is something sitting in the buffer we might take it as a
4738 response to a command, which would be bad. */
4739 SERIAL_FLUSH_INPUT (remote_desc
);
4743 puts_filtered ("Remote debugging using ");
4744 puts_filtered (name
);
4745 puts_filtered ("\n");
4748 remote_cisco_mode
= 1;
4750 push_target (&remote_cisco_ops
); /* Switch to using cisco target now */
4752 init_packet_config (&remote_protocol_P
);
4753 init_packet_config (&remote_protocol_Z
);
4755 general_thread
= -2;
4756 continue_thread
= -2;
4758 /* Force remote_write_bytes to check whether target supports
4759 binary downloading. */
4760 init_packet_config (&remote_protocol_binary_download
);
4762 /* Without this, some commands which require an active target (such
4763 as kill) won't work. This variable serves (at least) double duty
4764 as both the pid of the target process (if it has such), and as a
4765 flag indicating that a target is active. These functions should
4766 be split out into seperate variables, especially since GDB will
4767 someday have a notion of debugging several processes. */
4768 inferior_pid
= MAGIC_NULL_PID
;
4770 /* Start the remote connection; if error (0), discard this target. */
4772 if (!catch_errors (remote_start_remote_dummy
, (char *) 0,
4773 "Couldn't establish connection to remote target\n",
4782 remote_cisco_close (quitting
)
4785 remote_cisco_mode
= 0;
4786 remote_close (quitting
);
4793 remote_mourn_1 (&remote_cisco_ops
);
4805 /* shared between readsocket() and readtty() */
4806 static char *tty_input
;
4808 static int escape_count
;
4809 static int echo_check
;
4810 extern int quit_flag
;
4817 /* Loop until the socket doesn't have any more data */
4819 while ((data
= readchar (0)) >= 0)
4821 /* Check for the escape sequence */
4824 /* If this is the fourth escape, get out */
4825 if (++escape_count
== 4)
4830 { /* This is a '|', but not the fourth in a row.
4831 Continue without echoing it. If it isn't actually
4832 one of four in a row, it'll be echoed later. */
4839 /* Ensure any pending '|'s are flushed. */
4841 for (; escape_count
> 0; escape_count
--)
4845 if (data
== '\r') /* If this is a return character, */
4846 continue; /* - just supress it. */
4848 if (echo_check
!= -1) /* Check for echo of user input. */
4850 if (tty_input
[echo_check
] == data
)
4852 echo_check
++; /* Character matched user input: */
4853 continue; /* Continue without echoing it. */
4855 else if ((data
== '\n') && (tty_input
[echo_check
] == '\r'))
4856 { /* End of the line (and of echo checking). */
4857 echo_check
= -1; /* No more echo supression */
4858 continue; /* Continue without echoing. */
4861 { /* Failed check for echo of user input.
4862 We now have some suppressed output to flush! */
4865 for (j
= 0; j
< echo_check
; j
++)
4866 putchar (tty_input
[j
]);
4870 putchar (data
); /* Default case: output the char. */
4873 if (data
== SERIAL_TIMEOUT
) /* Timeout returned from readchar. */
4874 return READ_MORE
; /* Try to read some more */
4876 return FATAL_ERROR
; /* Trouble, bail out */
4885 /* First, read a buffer full from the terminal */
4886 tty_bytecount
= read (fileno (stdin
), tty_input
, sizeof (tty_input
) - 1);
4887 if (tty_bytecount
== -1)
4889 perror ("readtty: read failed");
4893 /* Remove a quoted newline. */
4894 if (tty_input
[tty_bytecount
- 1] == '\n' &&
4895 tty_input
[tty_bytecount
- 2] == '\\') /* line ending in backslash */
4897 tty_input
[--tty_bytecount
] = 0; /* remove newline */
4898 tty_input
[--tty_bytecount
] = 0; /* remove backslash */
4901 /* Turn trailing newlines into returns */
4902 if (tty_input
[tty_bytecount
- 1] == '\n')
4903 tty_input
[tty_bytecount
- 1] = '\r';
4905 /* If the line consists of a ~, enter debugging mode. */
4906 if ((tty_input
[0] == '~') && (tty_bytecount
== 2))
4909 /* Make this a zero terminated string and write it out */
4910 tty_input
[tty_bytecount
] = 0;
4911 if (SERIAL_WRITE (remote_desc
, tty_input
, tty_bytecount
))
4913 perror_with_name ("readtty: write failed");
4923 fd_set input
; /* file descriptors for select */
4924 int tablesize
; /* max number of FDs for select */
4928 extern int escape_count
; /* global shared by readsocket */
4929 extern int echo_check
; /* ditto */
4934 tablesize
= 8 * sizeof (input
);
4938 /* Check for anything from our socket - doesn't block. Note that
4939 this must be done *before* the select as there may be
4940 buffered I/O waiting to be processed. */
4942 if ((status
= readsocket ()) == FATAL_ERROR
)
4944 error ("Debugging terminated by communications error");
4946 else if (status
!= READ_MORE
)
4951 fflush (stdout
); /* Flush output before blocking */
4953 /* Now block on more socket input or TTY input */
4956 FD_SET (fileno (stdin
), &input
);
4957 FD_SET (remote_desc
->fd
, &input
);
4959 status
= select (tablesize
, &input
, 0, 0, 0);
4960 if ((status
== -1) && (errno
!= EINTR
))
4962 error ("Communications error on select %d", errno
);
4965 /* Handle Control-C typed */
4969 if ((++quit_count
) == 2)
4971 if (query ("Interrupt GDB? "))
4973 printf_filtered ("Interrupted by user.\n");
4974 return_to_top_level (RETURN_QUIT
);
4981 SERIAL_SEND_BREAK (remote_desc
);
4983 SERIAL_WRITE (remote_desc
, "\003", 1);
4988 /* Handle console input */
4990 if (FD_ISSET (fileno (stdin
), &input
))
4994 status
= readtty ();
4995 if (status
== READ_MORE
)
4998 return status
; /* telnet session ended */
5004 remote_cisco_wait (pid
, status
)
5006 struct target_waitstatus
*status
;
5008 if (minitelnet () != ENTER_DEBUG
)
5010 error ("Debugging session terminated by protocol error");
5013 return remote_wait (pid
, status
);
5017 init_remote_cisco_ops ()
5019 remote_cisco_ops
.to_shortname
= "cisco";
5020 remote_cisco_ops
.to_longname
= "Remote serial target in cisco-specific protocol";
5021 remote_cisco_ops
.to_doc
=
5022 "Use a remote machine via TCP, using a cisco-specific protocol.\n\
5023 Specify the serial device it is connected to (e.g. host:2020).";
5024 remote_cisco_ops
.to_open
= remote_cisco_open
;
5025 remote_cisco_ops
.to_close
= remote_cisco_close
;
5026 remote_cisco_ops
.to_detach
= remote_detach
;
5027 remote_cisco_ops
.to_resume
= remote_resume
;
5028 remote_cisco_ops
.to_wait
= remote_cisco_wait
;
5029 remote_cisco_ops
.to_fetch_registers
= remote_fetch_registers
;
5030 remote_cisco_ops
.to_store_registers
= remote_store_registers
;
5031 remote_cisco_ops
.to_prepare_to_store
= remote_prepare_to_store
;
5032 remote_cisco_ops
.to_xfer_memory
= remote_xfer_memory
;
5033 remote_cisco_ops
.to_files_info
= remote_files_info
;
5034 remote_cisco_ops
.to_insert_breakpoint
= remote_insert_breakpoint
;
5035 remote_cisco_ops
.to_remove_breakpoint
= remote_remove_breakpoint
;
5036 remote_cisco_ops
.to_kill
= remote_kill
;
5037 remote_cisco_ops
.to_load
= generic_load
;
5038 remote_cisco_ops
.to_mourn_inferior
= remote_cisco_mourn
;
5039 remote_cisco_ops
.to_thread_alive
= remote_thread_alive
;
5040 remote_cisco_ops
.to_find_new_threads
= remote_threads_info
;
5041 remote_cisco_ops
.to_stratum
= process_stratum
;
5042 remote_cisco_ops
.to_has_all_memory
= 1;
5043 remote_cisco_ops
.to_has_memory
= 1;
5044 remote_cisco_ops
.to_has_stack
= 1;
5045 remote_cisco_ops
.to_has_registers
= 1;
5046 remote_cisco_ops
.to_has_execution
= 1;
5047 remote_cisco_ops
.to_magic
= OPS_MAGIC
;
5050 /* Target async and target extended-async.
5052 This are temporary targets, until it is all tested. Eventually
5053 async support will be incorporated int the usual 'remote'
5057 init_remote_async_ops ()
5059 remote_async_ops
.to_shortname
= "async";
5060 remote_async_ops
.to_longname
= "Remote serial target in async version of the gdb-specific protocol";
5061 remote_async_ops
.to_doc
=
5062 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5063 Specify the serial device it is connected to (e.g. /dev/ttya).";
5064 remote_async_ops
.to_open
= remote_async_open
;
5065 remote_async_ops
.to_close
= remote_close
;
5066 remote_async_ops
.to_detach
= remote_async_detach
;
5067 remote_async_ops
.to_resume
= remote_async_resume
;
5068 remote_async_ops
.to_wait
= remote_async_wait
;
5069 remote_async_ops
.to_fetch_registers
= remote_fetch_registers
;
5070 remote_async_ops
.to_store_registers
= remote_store_registers
;
5071 remote_async_ops
.to_prepare_to_store
= remote_prepare_to_store
;
5072 remote_async_ops
.to_xfer_memory
= remote_xfer_memory
;
5073 remote_async_ops
.to_files_info
= remote_files_info
;
5074 remote_async_ops
.to_insert_breakpoint
= remote_insert_breakpoint
;
5075 remote_async_ops
.to_remove_breakpoint
= remote_remove_breakpoint
;
5076 remote_async_ops
.to_kill
= remote_async_kill
;
5077 remote_async_ops
.to_load
= generic_load
;
5078 remote_async_ops
.to_mourn_inferior
= remote_async_mourn
;
5079 remote_async_ops
.to_thread_alive
= remote_thread_alive
;
5080 remote_async_ops
.to_find_new_threads
= remote_threads_info
;
5081 remote_async_ops
.to_stop
= remote_stop
;
5082 remote_async_ops
.to_query
= remote_query
;
5083 remote_async_ops
.to_rcmd
= remote_rcmd
;
5084 remote_async_ops
.to_stratum
= process_stratum
;
5085 remote_async_ops
.to_has_all_memory
= 1;
5086 remote_async_ops
.to_has_memory
= 1;
5087 remote_async_ops
.to_has_stack
= 1;
5088 remote_async_ops
.to_has_registers
= 1;
5089 remote_async_ops
.to_has_execution
= 1;
5090 remote_async_ops
.to_has_thread_control
= tc_schedlock
; /* can lock scheduler */
5091 remote_async_ops
.to_has_async_exec
= 1;
5092 remote_async_ops
.to_magic
= OPS_MAGIC
;
5095 /* Set up the async extended remote vector by making a copy of the standard
5096 remote vector and adding to it. */
5099 init_extended_async_remote_ops ()
5101 extended_async_remote_ops
= remote_async_ops
;
5103 extended_async_remote_ops
.to_shortname
= "extended-async";
5104 extended_async_remote_ops
.to_longname
=
5105 "Extended remote serial target in async gdb-specific protocol";
5106 extended_async_remote_ops
.to_doc
=
5107 "Use a remote computer via a serial line, using an async gdb-specific protocol.\n\
5108 Specify the serial device it is connected to (e.g. /dev/ttya).",
5109 extended_async_remote_ops
.to_open
= extended_remote_async_open
;
5110 extended_async_remote_ops
.to_create_inferior
= extended_remote_async_create_inferior
;
5111 extended_async_remote_ops
.to_mourn_inferior
= extended_remote_mourn
;
5115 set_remote_cmd (args
, from_tty
)
5124 build_remote_gdbarch_data ()
5126 tty_input
= xmalloc (PBUFSIZ
);
5130 _initialize_remote ()
5132 static struct cmd_list_element
*remote_set_cmdlist
;
5133 static struct cmd_list_element
*remote_show_cmdlist
;
5135 /* architecture specific data */
5136 build_remote_gdbarch_data ();
5137 register_gdbarch_swap (&tty_input
, sizeof (&tty_input
), NULL
);
5138 register_gdbarch_swap (NULL
, 0, build_remote_gdbarch_data
);
5140 /* runtime constants - we retain the value of remote_write_size
5141 across architecture swaps. */
5142 remote_write_size
= PBUFSIZ
;
5145 add_target (&remote_ops
);
5147 init_extended_remote_ops ();
5148 add_target (&extended_remote_ops
);
5150 init_remote_async_ops ();
5151 add_target (&remote_async_ops
);
5153 init_extended_async_remote_ops ();
5154 add_target (&extended_async_remote_ops
);
5156 init_remote_cisco_ops ();
5157 add_target (&remote_cisco_ops
);
5160 init_remote_threadtests ();
5163 add_prefix_cmd ("remote", class_maintenance
, set_remote_cmd
, "\
5164 Remote protocol specific variables\n\
5165 Configure various remote-protocol specific variables such as\n\
5166 the packets being used",
5167 &remote_set_cmdlist
, "remote ",
5168 0/*allow-unknown*/, &setlist
);
5169 add_prefix_cmd ("remote", class_maintenance
, set_remote_cmd
, "\
5170 Remote protocol specific variables\n\
5171 Configure various remote-protocol specific variables such as\n\
5172 the packets being used",
5173 &remote_show_cmdlist
, "remote ",
5174 0/*allow-unknown*/, &showlist
);
5176 add_cmd ("compare-sections", class_obscure
, compare_sections_command
,
5177 "Compare section data on target to the exec file.\n\
5178 Argument is a single section name (default: all loaded sections).",
5181 add_cmd ("packet", class_maintenance
, packet_command
,
5182 "Send an arbitrary packet to a remote target.\n\
5183 maintenance packet TEXT\n\
5184 If GDB is talking to an inferior via the GDB serial protocol, then\n\
5185 this command sends the string TEXT to the inferior, and displays the\n\
5186 response packet. GDB supplies the initial `$' character, and the\n\
5187 terminating `#' character and checksum.",
5191 (add_set_cmd ("remotetimeout", no_class
,
5192 var_integer
, (char *) &remote_timeout
,
5193 "Set timeout value for remote read.\n",
5198 (add_set_cmd ("remotebreak", no_class
,
5199 var_boolean
, (char *) &remote_break
,
5200 "Set whether to send break if interrupted.\n",
5205 (add_set_cmd ("remotewritesize", no_class
,
5206 var_integer
, (char *) &remote_write_size
,
5207 "Set the maximum number of bytes per memory write packet.\n",
5211 remote_address_size
= TARGET_PTR_BIT
;
5213 (add_set_cmd ("remoteaddresssize", class_obscure
,
5214 var_integer
, (char *) &remote_address_size
,
5215 "Set the maximum size of the address (in bits) \
5216 in a memory packet.\n",
5220 add_packet_config_cmd (&remote_protocol_binary_download
,
5221 "X", "binary-download",
5222 set_remote_protocol_binary_download_cmd
,
5223 show_remote_protocol_binary_download_cmd
,
5224 &remote_set_cmdlist
, &remote_show_cmdlist
);
5226 /* XXXX - should ``set remotebinarydownload'' be retained for
5229 (add_set_cmd ("remotebinarydownload", no_class
,
5230 var_boolean
, (char *) &remote_binary_download
,
5231 "Set binary downloads.\n", &setlist
),
5235 add_info ("remote-process", remote_info_process
,
5236 "Query the remote system for process info.");
5238 add_packet_config_cmd (&remote_protocol_P
, "P", "set-register",
5239 set_remote_protocol_P_packet_cmd
,
5240 show_remote_protocol_P_packet_cmd
,
5241 &remote_set_cmdlist
, &remote_show_cmdlist
);
5243 add_packet_config_cmd (&remote_protocol_Z
, "Z", "breakpoint",
5244 set_remote_protocol_Z_packet_cmd
,
5245 show_remote_protocol_Z_packet_cmd
,
5246 &remote_set_cmdlist
, &remote_show_cmdlist
);