1 /* Remote target communications for serial-line targets in custom GDB protocol
2 Copyright 1988, 91, 92, 93, 94, 95, 96, 97, 98, 1999
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* See the GDB User Guide for details of the GDB remote protocol. */
25 #include "gdb_string.h"
34 /*#include "terminal.h" */
37 #include "gdb-stabs.h"
38 #include "gdbthread.h"
46 #include <sys/types.h>
49 #include "event-loop.h"
50 #include "event-top.h"
56 /* Prototypes for local functions */
57 static void cleanup_sigint_signal_handler (void *dummy
);
58 static void initialize_sigint_signal_handler (void);
60 static void handle_remote_sigint
PARAMS ((int));
61 static void handle_remote_sigint_twice
PARAMS ((int));
62 static void async_remote_interrupt
PARAMS ((gdb_client_data
));
63 void async_remote_interrupt_twice
PARAMS ((gdb_client_data
));
65 static void build_remote_gdbarch_data
PARAMS ((void));
67 static int remote_write_bytes (CORE_ADDR memaddr
, char *myaddr
, int len
);
69 static int remote_read_bytes
PARAMS ((CORE_ADDR memaddr
,
70 char *myaddr
, int len
));
72 static void remote_files_info
PARAMS ((struct target_ops
* ignore
));
74 static int remote_xfer_memory
PARAMS ((CORE_ADDR memaddr
, char *myaddr
,
75 int len
, int should_write
,
76 struct target_ops
* target
));
78 static void remote_prepare_to_store
PARAMS ((void));
80 static void remote_fetch_registers
PARAMS ((int regno
));
82 static void remote_resume
PARAMS ((int pid
, int step
,
83 enum target_signal siggnal
));
84 static void remote_async_resume
PARAMS ((int pid
, int step
,
85 enum target_signal siggnal
));
86 static int remote_start_remote
PARAMS ((PTR
));
88 static void remote_open
PARAMS ((char *name
, int from_tty
));
89 static void remote_async_open
PARAMS ((char *name
, int from_tty
));
91 static void extended_remote_open
PARAMS ((char *name
, int from_tty
));
92 static void extended_remote_async_open
PARAMS ((char *name
, int from_tty
));
94 static void remote_open_1
PARAMS ((char *, int, struct target_ops
*,
96 static void remote_async_open_1
PARAMS ((char *, int, struct target_ops
*,
99 static void remote_close
PARAMS ((int quitting
));
101 static void remote_store_registers
PARAMS ((int regno
));
103 static void remote_mourn
PARAMS ((void));
104 static void remote_async_mourn
PARAMS ((void));
106 static void extended_remote_restart
PARAMS ((void));
108 static void extended_remote_mourn
PARAMS ((void));
110 static void extended_remote_create_inferior
PARAMS ((char *, char *, char **));
111 static void extended_remote_async_create_inferior
PARAMS ((char *, char *, char **));
113 static void remote_mourn_1
PARAMS ((struct target_ops
*));
115 static void remote_send
PARAMS ((char *buf
));
117 static int readchar
PARAMS ((int timeout
));
119 static int remote_wait
PARAMS ((int pid
, struct target_waitstatus
* status
));
120 static int remote_async_wait
PARAMS ((int pid
, struct target_waitstatus
* status
));
122 static void remote_kill
PARAMS ((void));
123 static void remote_async_kill
PARAMS ((void));
125 static int tohex
PARAMS ((int nib
));
127 static void remote_detach
PARAMS ((char *args
, int from_tty
));
128 static void remote_async_detach
PARAMS ((char *args
, int from_tty
));
130 static void remote_interrupt
PARAMS ((int signo
));
132 static void remote_interrupt_twice
PARAMS ((int signo
));
134 static void interrupt_query
PARAMS ((void));
136 static void set_thread
PARAMS ((int, int));
138 static int remote_thread_alive
PARAMS ((int));
140 static void get_offsets
PARAMS ((void));
142 static int read_frame
PARAMS ((char *));
144 static int remote_insert_breakpoint
PARAMS ((CORE_ADDR
, char *));
146 static int remote_remove_breakpoint
PARAMS ((CORE_ADDR
, char *));
148 static int hexnumlen
PARAMS ((ULONGEST num
));
150 static void init_remote_ops
PARAMS ((void));
152 static void init_extended_remote_ops
PARAMS ((void));
154 static void init_remote_cisco_ops
PARAMS ((void));
156 static struct target_ops remote_cisco_ops
;
158 static void remote_stop
PARAMS ((void));
160 static int ishex
PARAMS ((int ch
, int *val
));
162 static int stubhex
PARAMS ((int ch
));
164 static int remote_query
PARAMS ((int /*char */ , char *, char *, int *));
166 static int hexnumstr
PARAMS ((char *, ULONGEST
));
168 static int hexnumnstr
PARAMS ((char *, ULONGEST
, int));
170 static CORE_ADDR remote_address_masked
PARAMS ((CORE_ADDR
));
172 static void print_packet
PARAMS ((char *));
174 static unsigned long crc32
PARAMS ((unsigned char *, int, unsigned int));
176 static void compare_sections_command
PARAMS ((char *, int));
178 static void packet_command
PARAMS ((char *, int));
180 static int stub_unpack_int
PARAMS ((char *buff
, int fieldlength
));
182 static int remote_current_thread
PARAMS ((int oldpid
));
184 static void remote_find_new_threads
PARAMS ((void));
186 static void record_currthread
PARAMS ((int currthread
));
188 /* exported functions */
190 extern int fromhex
PARAMS ((int a
));
192 static int putpkt_binary
PARAMS ((char *buf
, int cnt
));
194 static void check_binary_download
PARAMS ((CORE_ADDR addr
));
196 struct packet_config
;
198 static void show_packet_config_cmd
PARAMS ((struct packet_config
* config
));
200 static void set_packet_config_cmd
PARAMS ((struct packet_config
* config
,
201 struct cmd_list_element
* c
));
203 static void add_packet_config_cmd
PARAMS ((struct packet_config
* config
,
206 void (*set_func
) (char *args
, int from_tty
, struct cmd_list_element
* c
),
207 void (*show_func
) (char *name
, int from_tty
),
208 struct cmd_list_element
**setlist
,
209 struct cmd_list_element
**showlist
));
211 static void init_packet_config
PARAMS ((struct packet_config
* config
));
213 static void set_remote_protocol_P_packet_cmd
PARAMS ((char *args
,
215 struct cmd_list_element
* c
));
217 static void show_remote_protocol_P_packet_cmd
PARAMS ((char *args
,
220 static void set_remote_protocol_Z_packet_cmd
PARAMS ((char *args
,
222 struct cmd_list_element
* c
));
224 static void show_remote_protocol_Z_packet_cmd
PARAMS ((char *args
,
230 /* Define the target subroutine names */
232 void open_remote_target
PARAMS ((char *, int, struct target_ops
*, int));
234 void _initialize_remote
PARAMS ((void));
238 static struct target_ops remote_ops
;
240 static struct target_ops extended_remote_ops
;
242 /* Temporary target ops. Just like the remote_ops and
243 extended_remote_ops, but with asynchronous support. */
244 static struct target_ops remote_async_ops
;
246 static struct target_ops extended_async_remote_ops
;
248 /* This was 5 seconds, which is a long time to sit and wait.
249 Unless this is going though some terminal server or multiplexer or
250 other form of hairy serial connection, I would think 2 seconds would
253 /* Changed to allow option to set timeout value.
254 was static int remote_timeout = 2; */
255 extern int remote_timeout
;
257 /* FIXME: cagney/1999-09-23: Even though getpkt was called with
258 ``forever'' still use the normal timeout mechanism. This is
259 currently used by the ASYNC code to guarentee that target reads
260 during the initial connect always time-out. Once getpkt has been
261 modified to return a timeout indication and, in turn
262 remote_wait()/wait_for_inferior() have gained a timeout parameter
264 static int wait_forever_enabled_p
= 1;
267 /* This variable chooses whether to send a ^C or a break when the user
268 requests program interruption. Although ^C is usually what remote
269 systems expect, and that is the default here, sometimes a break is
270 preferable instead. */
272 static int remote_break
;
274 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
275 remote_open knows that we don't have a file open when the program
277 static serial_t remote_desc
= NULL
;
279 /* This is set by the target (thru the 'S' message)
280 to denote that the target is in kernel mode. */
281 static int cisco_kernel_mode
= 0;
283 /* Maximum number of bytes to read/write at once. The value here
284 is chosen to fill up a packet (the headers account for the 32). */
285 #define MAXBUFBYTES(N) (((N)-32)/2)
287 /* Having this larger than 400 causes us to be incompatible with m68k-stub.c
288 and i386-stub.c. Normally, no one would notice because it only matters
289 for writing large chunks of memory (e.g. in downloads). Also, this needs
290 to be more than 400 if required to hold the registers (see below, where
291 we round it up based on REGISTER_BYTES). */
292 /* Round up PBUFSIZ to hold all the registers, at least. */
293 #define PBUFSIZ ((REGISTER_BYTES > MAXBUFBYTES (400)) \
294 ? (REGISTER_BYTES * 2 + 32) \
298 /* This variable sets the number of bytes to be written to the target
299 in a single packet. Normally PBUFSIZ is satisfactory, but some
300 targets need smaller values (perhaps because the receiving end
303 static int remote_write_size
;
305 /* This variable sets the number of bits in an address that are to be
306 sent in a memory ("M" or "m") packet. Normally, after stripping
307 leading zeros, the entire address would be sent. This variable
308 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
309 initial implementation of remote.c restricted the address sent in
310 memory packets to ``host::sizeof long'' bytes - (typically 32
311 bits). Consequently, for 64 bit targets, the upper 32 bits of an
312 address was never sent. Since fixing this bug may cause a break in
313 some remote targets this variable is principly provided to
314 facilitate backward compatibility. */
316 static int remote_address_size
;
318 /* This is the size (in chars) of the first response to the `g' command. This
319 is used to limit the size of the memory read and write commands to prevent
320 stub buffers from overflowing. The size does not include headers and
321 trailers, it is only the payload size. */
323 static int remote_register_buf_size
= 0;
325 /* Tempoary to track who currently owns the terminal. See
326 target_async_terminal_* for more details. */
328 static int remote_async_terminal_ours_p
;
330 /* Generic configuration support for packets the stub optionally
331 supports. Allows the user to specify the use of the packet as well
332 as allowing GDB to auto-detect support in the remote stub. */
336 PACKET_SUPPORT_UNKNOWN
= 0,
343 PACKET_AUTO_DETECT
= 0,
352 enum packet_detect detect
;
353 enum packet_support support
;
356 static char packet_support_auto
[] = "auto";
357 static char packet_enable
[] = "enable";
358 static char packet_disable
[] = "disable";
359 static char *packet_support_enums
[] =
368 set_packet_config_cmd (config
, c
)
369 struct packet_config
*config
;
370 struct cmd_list_element
*c
;
372 if (config
->state
== packet_enable
)
374 config
->detect
= PACKET_MANUAL_DETECT
;
375 config
->support
= PACKET_ENABLE
;
377 else if (config
->state
== packet_disable
)
379 config
->detect
= PACKET_MANUAL_DETECT
;
380 config
->support
= PACKET_DISABLE
;
382 else if (config
->state
== packet_support_auto
)
384 config
->detect
= PACKET_AUTO_DETECT
;
385 config
->support
= PACKET_SUPPORT_UNKNOWN
;
388 internal_error ("Bad enum value");
392 show_packet_config_cmd (config
)
393 struct packet_config
*config
;
395 char *support
= "internal-error";
396 switch (config
->support
)
402 support
= "disabled";
404 case PACKET_SUPPORT_UNKNOWN
:
408 switch (config
->detect
)
410 case PACKET_AUTO_DETECT
:
411 printf_filtered ("Support for remote protocol `%s' (%s) packet is auto-detected, currently %s.\n",
412 config
->name
, config
->title
, support
);
414 case PACKET_MANUAL_DETECT
:
415 printf_filtered ("Support for remote protocol `%s' (%s) is currently %s.\n",
416 config
->name
, config
->title
, support
);
421 add_packet_config_cmd (config
, name
, title
, set_func
, show_func
,
423 struct packet_config
*config
;
426 void (*set_func
) PARAMS ((char *args
, int from_tty
,
427 struct cmd_list_element
* c
));
428 void (*show_func
) PARAMS ((char *name
, int from_tty
));
429 struct cmd_list_element
**setlist
;
430 struct cmd_list_element
**showlist
;
432 struct cmd_list_element
*c
;
437 config
->title
= title
;
438 asprintf (&set_doc
, "Set use of remote protocol `%s' (%s) packet",
440 asprintf (&show_doc
, "Show current use of remote protocol `%s' (%s) packet",
442 asprintf (&full_name
, "%s-packet", name
);
443 c
= add_set_enum_cmd (full_name
,
444 class_obscure
, packet_support_enums
,
445 (char *) &config
->state
,
447 c
->function
.sfunc
= set_func
;
448 add_cmd (full_name
, class_obscure
, show_func
, show_doc
, showlist
);
452 init_packet_config (config
)
453 struct packet_config
*config
;
455 switch (config
->detect
)
457 case PACKET_AUTO_DETECT
:
458 config
->support
= PACKET_SUPPORT_UNKNOWN
;
460 case PACKET_MANUAL_DETECT
:
461 /* let the user beware */
466 /* Should we try the 'P' (set register) request? */
468 static struct packet_config remote_protocol_P
;
471 set_remote_protocol_P_packet_cmd (args
, from_tty
, c
)
474 struct cmd_list_element
*c
;
476 set_packet_config_cmd (&remote_protocol_P
, c
);
480 show_remote_protocol_P_packet_cmd (args
, from_tty
)
484 show_packet_config_cmd (&remote_protocol_P
);
487 /* Should we try the 'Z' (set breakpoint) request? */
489 static struct packet_config remote_protocol_Z
;
492 set_remote_protocol_Z_packet_cmd (args
, from_tty
, c
)
495 struct cmd_list_element
*c
;
497 set_packet_config_cmd (&remote_protocol_Z
, c
);
501 show_remote_protocol_Z_packet_cmd (args
, from_tty
)
505 show_packet_config_cmd (&remote_protocol_Z
);
508 /* Should we try the 'X' (remote binary download) packet?
510 This variable (available to the user via "set remote X-packet")
511 dictates whether downloads are sent in binary (via the 'X' packet).
512 We assume that the stub can, and attempt to do it. This will be
513 cleared if the stub does not understand it. This switch is still
514 needed, though in cases when the packet is supported in the stub,
515 but the connection does not allow it (i.e., 7-bit serial connection
518 static struct packet_config remote_protocol_binary_download
;
521 set_remote_protocol_binary_download_cmd (char *args
,
523 struct cmd_list_element
*c
)
525 set_packet_config_cmd (&remote_protocol_binary_download
, c
);
529 show_remote_protocol_binary_download_cmd (char *args
,
532 show_packet_config_cmd (&remote_protocol_binary_download
);
536 /* Tokens for use by the asynchronous signal handlers for SIGINT */
537 PTR sigint_remote_twice_token
;
538 PTR sigint_remote_token
;
540 /* These are pointers to hook functions that may be set in order to
541 modify resume/wait behavior for a particular architecture. */
543 void (*target_resume_hook
) PARAMS ((void));
544 void (*target_wait_loop_hook
) PARAMS ((void));
548 /* These are the threads which we last sent to the remote system.
549 -1 for all or -2 for not sent yet. */
550 static int general_thread
;
551 static int continue_thread
;
553 /* Call this function as a result of
554 1) A halt indication (T packet) containing a thread id
555 2) A direct query of currthread
556 3) Successful execution of set thread
560 record_currthread (currthread
)
563 general_thread
= currthread
;
565 /* If this is a new thread, add it to GDB's thread list.
566 If we leave it up to WFI to do this, bad things will happen. */
567 if (!in_thread_list (currthread
))
569 add_thread (currthread
);
570 printf_filtered ("[New %s]\n", target_pid_to_str (currthread
));
574 #define MAGIC_NULL_PID 42000
581 char *buf
= alloca (PBUFSIZ
);
582 int state
= gen
? general_thread
: continue_thread
;
588 buf
[1] = gen
? 'g' : 'c';
589 if (th
== MAGIC_NULL_PID
)
595 sprintf (&buf
[2], "-%x", -th
);
597 sprintf (&buf
[2], "%x", th
);
603 continue_thread
= th
;
606 /* Return nonzero if the thread TH is still alive on the remote system. */
609 remote_thread_alive (tid
)
615 sprintf (buf
, "T-%08x", -tid
);
617 sprintf (buf
, "T%08x", tid
);
620 return (buf
[0] == 'O' && buf
[1] == 'K');
623 /* About these extended threadlist and threadinfo packets. They are
624 variable length packets but, the fields within them are often fixed
625 length. They are redundent enough to send over UDP as is the
626 remote protocol in general. There is a matching unit test module
629 #define OPAQUETHREADBYTES 8
631 /* a 64 bit opaque identifier */
632 typedef unsigned char threadref
[OPAQUETHREADBYTES
];
634 /* WARNING: This threadref data structure comes from the remote O.S., libstub
635 protocol encoding, and remote.c. it is not particularly changable */
637 /* Right now, the internal structure is int. We want it to be bigger.
641 typedef int gdb_threadref
; /* internal GDB thread reference */
643 /* gdb_ext_thread_info is an internal GDB data structure which is
644 equivalint to the reply of the remote threadinfo packet */
646 struct gdb_ext_thread_info
648 threadref threadid
; /* External form of thread reference */
649 int active
; /* Has state interesting to GDB? , regs, stack */
650 char display
[256]; /* Brief state display, name, blocked/syspended */
651 char shortname
[32]; /* To be used to name threads */
652 char more_display
[256]; /* Long info, statistics, queue depth, whatever */
655 /* The volume of remote transfers can be limited by submitting
656 a mask containing bits specifying the desired information.
657 Use a union of these values as the 'selection' parameter to
658 get_thread_info. FIXME: Make these TAG names more thread specific.
661 #define TAG_THREADID 1
663 #define TAG_DISPLAY 4
664 #define TAG_THREADNAME 8
665 #define TAG_MOREDISPLAY 16
667 #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES*2)
669 char *unpack_varlen_hex
PARAMS ((char *buff
, int *result
));
671 static char *unpack_nibble
PARAMS ((char *buf
, int *val
));
673 static char *pack_nibble
PARAMS ((char *buf
, int nibble
));
675 static char *pack_hex_byte
PARAMS ((char *pkt
, int /*unsigned char */ byte
));
677 static char *unpack_byte
PARAMS ((char *buf
, int *value
));
679 static char *pack_int
PARAMS ((char *buf
, int value
));
681 static char *unpack_int
PARAMS ((char *buf
, int *value
));
683 static char *unpack_string
PARAMS ((char *src
, char *dest
, int length
));
685 static char *pack_threadid
PARAMS ((char *pkt
, threadref
* id
));
687 static char *unpack_threadid
PARAMS ((char *inbuf
, threadref
* id
));
689 void int_to_threadref
PARAMS ((threadref
* id
, int value
));
691 static int threadref_to_int
PARAMS ((threadref
* ref
));
693 static void copy_threadref
PARAMS ((threadref
* dest
, threadref
* src
));
695 static int threadmatch
PARAMS ((threadref
* dest
, threadref
* src
));
697 static char *pack_threadinfo_request
PARAMS ((char *pkt
, int mode
,
700 static int remote_unpack_thread_info_response
PARAMS ((char *pkt
,
701 threadref
* expectedref
,
702 struct gdb_ext_thread_info
* info
));
705 static int remote_get_threadinfo
PARAMS ((threadref
* threadid
,
706 int fieldset
, /*TAG mask */
707 struct gdb_ext_thread_info
* info
));
709 static int adapt_remote_get_threadinfo
PARAMS ((gdb_threadref
* ref
,
711 struct gdb_ext_thread_info
* info
));
713 static char *pack_threadlist_request
PARAMS ((char *pkt
, int startflag
,
715 threadref
* nextthread
));
717 static int parse_threadlist_response
PARAMS ((char *pkt
,
719 threadref
* original_echo
,
720 threadref
* resultlist
,
723 static int remote_get_threadlist
PARAMS ((int startflag
,
724 threadref
* nextthread
,
728 threadref
* threadlist
));
730 typedef int (*rmt_thread_action
) (threadref
* ref
, void *context
);
732 static int remote_threadlist_iterator
PARAMS ((rmt_thread_action stepfunction
,
733 void *context
, int looplimit
));
735 static int remote_newthread_step
PARAMS ((threadref
* ref
, void *context
));
737 /* encode 64 bits in 16 chars of hex */
739 static const char hexchars
[] = "0123456789abcdef";
746 if ((ch
>= 'a') && (ch
<= 'f'))
748 *val
= ch
- 'a' + 10;
751 if ((ch
>= 'A') && (ch
<= 'F'))
753 *val
= ch
- 'A' + 10;
756 if ((ch
>= '0') && (ch
<= '9'))
768 if (ch
>= 'a' && ch
<= 'f')
769 return ch
- 'a' + 10;
770 if (ch
>= '0' && ch
<= '9')
772 if (ch
>= 'A' && ch
<= 'F')
773 return ch
- 'A' + 10;
778 stub_unpack_int (buff
, fieldlength
)
787 nibble
= stubhex (*buff
++);
791 retval
= retval
<< 4;
797 unpack_varlen_hex (buff
, result
)
798 char *buff
; /* packet to parse */
804 while (ishex (*buff
, &nibble
))
807 retval
= retval
<< 4;
808 retval
|= nibble
& 0x0f;
815 unpack_nibble (buf
, val
)
824 pack_nibble (buf
, nibble
)
828 *buf
++ = hexchars
[(nibble
& 0x0f)];
833 pack_hex_byte (pkt
, byte
)
837 *pkt
++ = hexchars
[(byte
>> 4) & 0xf];
838 *pkt
++ = hexchars
[(byte
& 0xf)];
843 unpack_byte (buf
, value
)
847 *value
= stub_unpack_int (buf
, 2);
852 pack_int (buf
, value
)
856 buf
= pack_hex_byte (buf
, (value
>> 24) & 0xff);
857 buf
= pack_hex_byte (buf
, (value
>> 16) & 0xff);
858 buf
= pack_hex_byte (buf
, (value
>> 8) & 0x0ff);
859 buf
= pack_hex_byte (buf
, (value
& 0xff));
864 unpack_int (buf
, value
)
868 *value
= stub_unpack_int (buf
, 8);
872 #if 0 /* currently unused, uncomment when needed */
873 static char *pack_string
PARAMS ((char *pkt
, char *string
));
876 pack_string (pkt
, string
)
883 len
= strlen (string
);
885 len
= 200; /* Bigger than most GDB packets, junk??? */
886 pkt
= pack_hex_byte (pkt
, len
);
890 if ((ch
== '\0') || (ch
== '#'))
891 ch
= '*'; /* Protect encapsulation */
896 #endif /* 0 (unused) */
899 unpack_string (src
, dest
, length
)
911 pack_threadid (pkt
, id
)
916 unsigned char *altid
;
918 altid
= (unsigned char *) id
;
919 limit
= pkt
+ BUF_THREAD_ID_SIZE
;
921 pkt
= pack_hex_byte (pkt
, *altid
++);
927 unpack_threadid (inbuf
, id
)
932 char *limit
= inbuf
+ BUF_THREAD_ID_SIZE
;
935 altref
= (char *) id
;
937 while (inbuf
< limit
)
939 x
= stubhex (*inbuf
++);
940 y
= stubhex (*inbuf
++);
941 *altref
++ = (x
<< 4) | y
;
946 /* Externally, threadrefs are 64 bits but internally, they are still
947 ints. This is due to a mismatch of specifications. We would like
948 to use 64bit thread references internally. This is an adapter
952 int_to_threadref (id
, value
)
958 scan
= (unsigned char *) id
;
964 *scan
++ = (value
>> 24) & 0xff;
965 *scan
++ = (value
>> 16) & 0xff;
966 *scan
++ = (value
>> 8) & 0xff;
967 *scan
++ = (value
& 0xff);
971 threadref_to_int (ref
)
981 value
= (value
<< 8) | ((*scan
++) & 0xff);
986 copy_threadref (dest
, src
)
991 unsigned char *csrc
, *cdest
;
993 csrc
= (unsigned char *) src
;
994 cdest
= (unsigned char *) dest
;
1001 threadmatch (dest
, src
)
1005 /* things are broken right now, so just assume we got a match */
1007 unsigned char *srcp
, *destp
;
1009 srcp
= (char *) src
;
1010 destp
= (char *) dest
;
1014 result
&= (*srcp
++ == *destp
++) ? 1 : 0;
1021 threadid:1, # always request threadid
1028 /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
1031 pack_threadinfo_request (pkt
, mode
, id
)
1036 *pkt
++ = 'q'; /* Info Query */
1037 *pkt
++ = 'P'; /* process or thread info */
1038 pkt
= pack_int (pkt
, mode
); /* mode */
1039 pkt
= pack_threadid (pkt
, id
); /* threadid */
1040 *pkt
= '\0'; /* terminate */
1044 /* These values tag the fields in a thread info response packet */
1045 /* Tagging the fields allows us to request specific fields and to
1046 add more fields as time goes by */
1048 #define TAG_THREADID 1 /* Echo the thread identifier */
1049 #define TAG_EXISTS 2 /* Is this process defined enough to
1050 fetch registers and its stack */
1051 #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
1052 #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is */
1053 #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
1057 remote_unpack_thread_info_response (pkt
, expectedref
, info
)
1059 threadref
*expectedref
;
1060 struct gdb_ext_thread_info
*info
;
1065 char *limit
= pkt
+ PBUFSIZ
; /* plausable parsing limit */
1068 /* info->threadid = 0; FIXME: implement zero_threadref */
1070 info
->display
[0] = '\0';
1071 info
->shortname
[0] = '\0';
1072 info
->more_display
[0] = '\0';
1074 /* Assume the characters indicating the packet type have been stripped */
1075 pkt
= unpack_int (pkt
, &mask
); /* arg mask */
1076 pkt
= unpack_threadid (pkt
, &ref
);
1079 warning ("Incomplete response to threadinfo request\n");
1080 if (!threadmatch (&ref
, expectedref
))
1081 { /* This is an answer to a different request */
1082 warning ("ERROR RMT Thread info mismatch\n");
1085 copy_threadref (&info
->threadid
, &ref
);
1087 /* Loop on tagged fields , try to bail if somthing goes wrong */
1089 while ((pkt
< limit
) && mask
&& *pkt
) /* packets are terminated with nulls */
1091 pkt
= unpack_int (pkt
, &tag
); /* tag */
1092 pkt
= unpack_byte (pkt
, &length
); /* length */
1093 if (!(tag
& mask
)) /* tags out of synch with mask */
1095 warning ("ERROR RMT: threadinfo tag mismatch\n");
1099 if (tag
== TAG_THREADID
)
1103 warning ("ERROR RMT: length of threadid is not 16\n");
1107 pkt
= unpack_threadid (pkt
, &ref
);
1108 mask
= mask
& ~TAG_THREADID
;
1111 if (tag
== TAG_EXISTS
)
1113 info
->active
= stub_unpack_int (pkt
, length
);
1115 mask
= mask
& ~(TAG_EXISTS
);
1118 warning ("ERROR RMT: 'exists' length too long\n");
1124 if (tag
== TAG_THREADNAME
)
1126 pkt
= unpack_string (pkt
, &info
->shortname
[0], length
);
1127 mask
= mask
& ~TAG_THREADNAME
;
1130 if (tag
== TAG_DISPLAY
)
1132 pkt
= unpack_string (pkt
, &info
->display
[0], length
);
1133 mask
= mask
& ~TAG_DISPLAY
;
1136 if (tag
== TAG_MOREDISPLAY
)
1138 pkt
= unpack_string (pkt
, &info
->more_display
[0], length
);
1139 mask
= mask
& ~TAG_MOREDISPLAY
;
1142 warning ("ERROR RMT: unknown thread info tag\n");
1143 break; /* Not a tag we know about */
1149 remote_get_threadinfo (threadid
, fieldset
, info
)
1150 threadref
*threadid
;
1151 int fieldset
; /* TAG mask */
1152 struct gdb_ext_thread_info
*info
;
1155 char *threadinfo_pkt
= alloca (PBUFSIZ
);
1157 pack_threadinfo_request (threadinfo_pkt
, fieldset
, threadid
);
1158 putpkt (threadinfo_pkt
);
1159 getpkt (threadinfo_pkt
, 0);
1160 result
= remote_unpack_thread_info_response (threadinfo_pkt
+ 2, threadid
,
1165 /* Unfortunately, 61 bit thread-ids are bigger than the internal
1166 representation of a threadid. */
1169 adapt_remote_get_threadinfo (ref
, selection
, info
)
1172 struct gdb_ext_thread_info
*info
;
1176 int_to_threadref (&lclref
, *ref
);
1177 return remote_get_threadinfo (&lclref
, selection
, info
);
1180 /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
1183 pack_threadlist_request (pkt
, startflag
, threadcount
, nextthread
)
1187 threadref
*nextthread
;
1189 *pkt
++ = 'q'; /* info query packet */
1190 *pkt
++ = 'L'; /* Process LIST or threadLIST request */
1191 pkt
= pack_nibble (pkt
, startflag
); /* initflag 1 bytes */
1192 pkt
= pack_hex_byte (pkt
, threadcount
); /* threadcount 2 bytes */
1193 pkt
= pack_threadid (pkt
, nextthread
); /* 64 bit thread identifier */
1198 /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
1201 parse_threadlist_response (pkt
, result_limit
, original_echo
, resultlist
,
1205 threadref
*original_echo
;
1206 threadref
*resultlist
;
1210 int count
, resultcount
, done
;
1213 /* Assume the 'q' and 'M chars have been stripped. */
1214 limit
= pkt
+ (PBUFSIZ
- BUF_THREAD_ID_SIZE
); /* done parse past here */
1215 pkt
= unpack_byte (pkt
, &count
); /* count field */
1216 pkt
= unpack_nibble (pkt
, &done
);
1217 /* The first threadid is the argument threadid. */
1218 pkt
= unpack_threadid (pkt
, original_echo
); /* should match query packet */
1219 while ((count
-- > 0) && (pkt
< limit
))
1221 pkt
= unpack_threadid (pkt
, resultlist
++);
1222 if (resultcount
++ >= result_limit
)
1231 remote_get_threadlist (startflag
, nextthread
, result_limit
,
1232 done
, result_count
, threadlist
)
1234 threadref
*nextthread
;
1238 threadref
*threadlist
;
1241 static threadref echo_nextthread
;
1242 char *threadlist_packet
= alloca (PBUFSIZ
);
1243 char *t_response
= alloca (PBUFSIZ
);
1246 /* Trancate result limit to be smaller than the packet size */
1247 if ((((result_limit
+ 1) * BUF_THREAD_ID_SIZE
) + 10) >= PBUFSIZ
)
1248 result_limit
= (PBUFSIZ
/ BUF_THREAD_ID_SIZE
) - 2;
1250 pack_threadlist_request (threadlist_packet
,
1251 startflag
, result_limit
, nextthread
);
1252 putpkt (threadlist_packet
);
1253 getpkt (t_response
, 0);
1256 parse_threadlist_response (t_response
+ 2, result_limit
, &echo_nextthread
,
1259 if (!threadmatch (&echo_nextthread
, nextthread
))
1261 /* FIXME: This is a good reason to drop the packet */
1262 /* Possably, there is a duplicate response */
1264 retransmit immediatly - race conditions
1265 retransmit after timeout - yes
1267 wait for packet, then exit
1269 warning ("HMM: threadlist did not echo arg thread, dropping it\n");
1270 return 0; /* I choose simply exiting */
1272 if (*result_count
<= 0)
1276 warning ("RMT ERROR : failed to get remote thread list\n");
1279 return result
; /* break; */
1281 if (*result_count
> result_limit
)
1284 warning ("RMT ERROR: threadlist response longer than requested\n");
1290 /* This is the interface between remote and threads, remotes upper interface */
1292 /* remote_find_new_threads retrieves the thread list and for each
1293 thread in the list, looks up the thread in GDB's internal list,
1294 ading the thread if it does not already exist. This involves
1295 getting partial thread lists from the remote target so, polling the
1296 quit_flag is required. */
1299 /* About this many threadisds fit in a packet. */
1301 #define MAXTHREADLISTRESULTS 32
1304 remote_threadlist_iterator (stepfunction
, context
, looplimit
)
1305 rmt_thread_action stepfunction
;
1309 int done
, i
, result_count
;
1313 static threadref nextthread
;
1314 static threadref resultthreadlist
[MAXTHREADLISTRESULTS
];
1319 if (loopcount
++ > looplimit
)
1322 warning ("Remote fetch threadlist -infinite loop-\n");
1325 if (!remote_get_threadlist (startflag
, &nextthread
, MAXTHREADLISTRESULTS
,
1326 &done
, &result_count
, resultthreadlist
))
1331 /* clear for later iterations */
1333 /* Setup to resume next batch of thread references, set nextthread. */
1334 if (result_count
>= 1)
1335 copy_threadref (&nextthread
, &resultthreadlist
[result_count
- 1]);
1337 while (result_count
--)
1338 if (!(result
= (*stepfunction
) (&resultthreadlist
[i
++], context
)))
1345 remote_newthread_step (ref
, context
)
1351 pid
= threadref_to_int (ref
);
1352 if (!in_thread_list (pid
))
1354 return 1; /* continue iterator */
1357 #define CRAZY_MAX_THREADS 1000
1360 remote_current_thread (oldpid
)
1363 char *buf
= alloca (PBUFSIZ
);
1367 if (buf
[0] == 'Q' && buf
[1] == 'C')
1368 return strtol (&buf
[2], NULL
, 16);
1373 /* Find new threads for info threads command. */
1376 remote_find_new_threads ()
1378 remote_threadlist_iterator (remote_newthread_step
, 0,
1380 if (inferior_pid
== MAGIC_NULL_PID
) /* ack ack ack */
1381 inferior_pid
= remote_current_thread (inferior_pid
);
1385 remote_threads_info (void)
1387 char *buf
= alloca (PBUFSIZ
);
1391 if (remote_desc
== 0) /* paranoia */
1392 error ("Command can only be used when connected to the remote target.");
1394 putpkt ("qfThreadInfo");
1395 getpkt (bufp
= buf
, 0);
1396 if (bufp
[0] == '\0') /* q packet not recognized! */
1397 { /* try old jmetzler method */
1398 remote_find_new_threads ();
1401 else /* try new 'q' method */
1402 while (*bufp
++ == 'm') /* reply contains one or more TID */
1406 tid
= strtol (bufp
, &bufp
, 16);
1407 if (tid
!= 0 && !in_thread_list (tid
))
1410 while (*bufp
++ == ','); /* comma-separated list */
1411 putpkt ("qsThreadInfo");
1412 getpkt (bufp
= buf
, 0);
1417 /* Restart the remote side; this is an extended protocol operation. */
1420 extended_remote_restart ()
1422 char *buf
= alloca (PBUFSIZ
);
1424 /* Send the restart command; for reasons I don't understand the
1425 remote side really expects a number after the "R". */
1427 sprintf (&buf
[1], "%x", 0);
1430 /* Now query for status so this looks just like we restarted
1431 gdbserver from scratch. */
1436 /* Clean up connection to a remote debugger. */
1440 remote_close (quitting
)
1444 SERIAL_CLOSE (remote_desc
);
1448 /* Query the remote side for the text, data and bss offsets. */
1453 char *buf
= alloca (PBUFSIZ
);
1456 CORE_ADDR text_addr
, data_addr
, bss_addr
;
1457 struct section_offsets
*offs
;
1459 putpkt ("qOffsets");
1463 if (buf
[0] == '\000')
1464 return; /* Return silently. Stub doesn't support
1468 warning ("Remote failure reply: %s", buf
);
1472 /* Pick up each field in turn. This used to be done with scanf, but
1473 scanf will make trouble if CORE_ADDR size doesn't match
1474 conversion directives correctly. The following code will work
1475 with any size of CORE_ADDR. */
1476 text_addr
= data_addr
= bss_addr
= 0;
1480 if (strncmp (ptr
, "Text=", 5) == 0)
1483 /* Don't use strtol, could lose on big values. */
1484 while (*ptr
&& *ptr
!= ';')
1485 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
1490 if (!lose
&& strncmp (ptr
, ";Data=", 6) == 0)
1493 while (*ptr
&& *ptr
!= ';')
1494 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
1499 if (!lose
&& strncmp (ptr
, ";Bss=", 5) == 0)
1502 while (*ptr
&& *ptr
!= ';')
1503 bss_addr
= (bss_addr
<< 4) + fromhex (*ptr
++);
1509 error ("Malformed response to offset query, %s", buf
);
1511 if (symfile_objfile
== NULL
)
1514 offs
= (struct section_offsets
*) alloca (SIZEOF_SECTION_OFFSETS
);
1515 memcpy (offs
, symfile_objfile
->section_offsets
, SIZEOF_SECTION_OFFSETS
);
1517 ANOFFSET (offs
, SECT_OFF_TEXT
) = text_addr
;
1519 /* This is a temporary kludge to force data and bss to use the same offsets
1520 because that's what nlmconv does now. The real solution requires changes
1521 to the stub and remote.c that I don't have time to do right now. */
1523 ANOFFSET (offs
, SECT_OFF_DATA
) = data_addr
;
1524 ANOFFSET (offs
, SECT_OFF_BSS
) = data_addr
;
1526 objfile_relocate (symfile_objfile
, offs
);
1530 * Cisco version of section offsets:
1532 * Instead of having GDB query the target for the section offsets,
1533 * Cisco lets the target volunteer the information! It's also in
1534 * a different format, so here are the functions that will decode
1535 * a section offset packet from a Cisco target.
1539 * Function: remote_cisco_section_offsets
1541 * Returns: zero for success, non-zero for failure
1545 remote_cisco_section_offsets (text_addr
, data_addr
, bss_addr
,
1546 text_offs
, data_offs
, bss_offs
)
1550 bfd_signed_vma
*text_offs
;
1551 bfd_signed_vma
*data_offs
;
1552 bfd_signed_vma
*bss_offs
;
1554 bfd_vma text_base
, data_base
, bss_base
;
1555 struct minimal_symbol
*start
;
1561 if (symfile_objfile
== NULL
)
1562 return -1; /* no can do nothin' */
1564 start
= lookup_minimal_symbol ("_start", NULL
, NULL
);
1566 return -1; /* Can't find "_start" symbol */
1568 data_base
= bss_base
= 0;
1569 text_base
= SYMBOL_VALUE_ADDRESS (start
);
1571 abfd
= symfile_objfile
->obfd
;
1572 for (sect
= abfd
->sections
;
1576 p
= (unsigned char *) bfd_get_section_name (abfd
, sect
);
1578 if (strcmp (p
+ len
- 4, "data") == 0) /* ends in "data" */
1579 if (data_base
== 0 ||
1580 data_base
> bfd_get_section_vma (abfd
, sect
))
1581 data_base
= bfd_get_section_vma (abfd
, sect
);
1582 if (strcmp (p
+ len
- 3, "bss") == 0) /* ends in "bss" */
1583 if (bss_base
== 0 ||
1584 bss_base
> bfd_get_section_vma (abfd
, sect
))
1585 bss_base
= bfd_get_section_vma (abfd
, sect
);
1587 *text_offs
= text_addr
- text_base
;
1588 *data_offs
= data_addr
- data_base
;
1589 *bss_offs
= bss_addr
- bss_base
;
1594 sprintf (tmp
, "VMA: text = 0x");
1595 sprintf_vma (tmp
+ strlen (tmp
), text_addr
);
1596 sprintf (tmp
+ strlen (tmp
), " data = 0x");
1597 sprintf_vma (tmp
+ strlen (tmp
), data_addr
);
1598 sprintf (tmp
+ strlen (tmp
), " bss = 0x");
1599 sprintf_vma (tmp
+ strlen (tmp
), bss_addr
);
1600 fprintf_filtered (gdb_stdlog
, tmp
);
1601 fprintf_filtered (gdb_stdlog
,
1602 "Reloc offset: text = 0x%s data = 0x%s bss = 0x%s\n",
1603 paddr_nz (*text_offs
),
1604 paddr_nz (*data_offs
),
1605 paddr_nz (*bss_offs
));
1612 * Function: remote_cisco_objfile_relocate
1614 * Relocate the symbol file for a remote target.
1618 remote_cisco_objfile_relocate (text_off
, data_off
, bss_off
)
1619 bfd_signed_vma text_off
;
1620 bfd_signed_vma data_off
;
1621 bfd_signed_vma bss_off
;
1623 struct section_offsets
*offs
;
1625 if (text_off
!= 0 || data_off
!= 0 || bss_off
!= 0)
1627 /* FIXME: This code assumes gdb-stabs.h is being used; it's
1628 broken for xcoff, dwarf, sdb-coff, etc. But there is no
1629 simple canonical representation for this stuff. */
1631 offs
= (struct section_offsets
*) alloca (SIZEOF_SECTION_OFFSETS
);
1632 memcpy (offs
, symfile_objfile
->section_offsets
, SIZEOF_SECTION_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 /* See FIXME above */
1737 wait_forever_enabled_p
= 1;
1739 target_preopen (from_tty
);
1741 unpush_target (target
);
1743 remote_dcache
= dcache_init (remote_read_bytes
, remote_write_bytes
);
1745 remote_desc
= SERIAL_OPEN (name
);
1747 perror_with_name (name
);
1749 if (baud_rate
!= -1)
1751 if (SERIAL_SETBAUDRATE (remote_desc
, baud_rate
))
1753 SERIAL_CLOSE (remote_desc
);
1754 perror_with_name (name
);
1758 SERIAL_RAW (remote_desc
);
1760 /* If there is something sitting in the buffer we might take it as a
1761 response to a command, which would be bad. */
1762 SERIAL_FLUSH_INPUT (remote_desc
);
1766 puts_filtered ("Remote debugging using ");
1767 puts_filtered (name
);
1768 puts_filtered ("\n");
1770 push_target (target
); /* Switch to using remote target now */
1772 init_packet_config (&remote_protocol_P
);
1773 init_packet_config (&remote_protocol_Z
);
1775 general_thread
= -2;
1776 continue_thread
= -2;
1778 /* Force remote_write_bytes to check whether target supports
1779 binary downloading. */
1780 init_packet_config (&remote_protocol_binary_download
);
1782 /* Without this, some commands which require an active target (such
1783 as kill) won't work. This variable serves (at least) double duty
1784 as both the pid of the target process (if it has such), and as a
1785 flag indicating that a target is active. These functions should
1786 be split out into seperate variables, especially since GDB will
1787 someday have a notion of debugging several processes. */
1789 inferior_pid
= MAGIC_NULL_PID
;
1790 /* Start the remote connection; if error (0), discard this target.
1791 In particular, if the user quits, be sure to discard it
1792 (we'd be in an inconsistent state otherwise). */
1793 if (!catch_errors (remote_start_remote
, NULL
,
1794 "Couldn't establish connection to remote target\n",
1803 /* tell the remote that we're using the extended protocol. */
1804 char *buf
= alloca (PBUFSIZ
);
1810 /* Just like remote_open but with asynchronous support. */
1812 remote_async_open_1 (name
, from_tty
, target
, extended_p
)
1815 struct target_ops
*target
;
1819 error ("To open a remote debug connection, you need to specify what\n\
1820 serial device is attached to the remote system (e.g. /dev/ttya).");
1822 target_preopen (from_tty
);
1824 unpush_target (target
);
1826 remote_dcache
= dcache_init (remote_read_bytes
, remote_write_bytes
);
1828 remote_desc
= SERIAL_OPEN (name
);
1830 perror_with_name (name
);
1832 if (baud_rate
!= -1)
1834 if (SERIAL_SETBAUDRATE (remote_desc
, baud_rate
))
1836 SERIAL_CLOSE (remote_desc
);
1837 perror_with_name (name
);
1841 SERIAL_RAW (remote_desc
);
1843 /* If there is something sitting in the buffer we might take it as a
1844 response to a command, which would be bad. */
1845 SERIAL_FLUSH_INPUT (remote_desc
);
1849 puts_filtered ("Remote debugging using ");
1850 puts_filtered (name
);
1851 puts_filtered ("\n");
1854 push_target (target
); /* Switch to using remote target now */
1856 init_packet_config (&remote_protocol_P
);
1857 init_packet_config (&remote_protocol_Z
);
1859 general_thread
= -2;
1860 continue_thread
= -2;
1862 /* Force remote_write_bytes to check whether target supports
1863 binary downloading. */
1864 init_packet_config (&remote_protocol_binary_download
);
1866 /* Without this, some commands which require an active target (such
1867 as kill) won't work. This variable serves (at least) double duty
1868 as both the pid of the target process (if it has such), and as a
1869 flag indicating that a target is active. These functions should
1870 be split out into seperate variables, especially since GDB will
1871 someday have a notion of debugging several processes. */
1872 inferior_pid
= MAGIC_NULL_PID
;
1874 /* With this target we start out by owning the terminal. */
1875 remote_async_terminal_ours_p
= 1;
1877 /* FIXME: cagney/1999-09-23: During the initial connection it is
1878 assumed that the target is already ready and able to respond to
1879 requests. Unfortunatly remote_start_remote() eventually calls
1880 wait_for_inferior() with no timeout. wait_forever_enabled_p gets
1881 around this. Eventually a mechanism that allows
1882 wait_for_inferior() to expect/get timeouts will be
1884 wait_forever_enabled_p
= 0;
1886 /* Start the remote connection; if error (0), discard this target.
1887 In particular, if the user quits, be sure to discard it
1888 (we'd be in an inconsistent state otherwise). */
1889 if (!catch_errors (remote_start_remote
, NULL
,
1890 "Couldn't establish connection to remote target\n",
1894 wait_forever_enabled_p
= 1;
1898 wait_forever_enabled_p
= 1;
1902 /* tell the remote that we're using the extended protocol. */
1903 char *buf
= alloca (PBUFSIZ
);
1909 /* This takes a program previously attached to and detaches it. After
1910 this is done, GDB can be used to debug some other program. We
1911 better not have left any breakpoints in the target program or it'll
1912 die when it hits one. */
1915 remote_detach (args
, from_tty
)
1919 char *buf
= alloca (PBUFSIZ
);
1922 error ("Argument given to \"detach\" when remotely debugging.");
1924 /* Tell the remote target to detach. */
1930 puts_filtered ("Ending remote debugging.\n");
1934 /* Same as remote_detach, but with async support. */
1936 remote_async_detach (args
, from_tty
)
1940 char *buf
= alloca (PBUFSIZ
);
1943 error ("Argument given to \"detach\" when remotely debugging.");
1945 /* Tell the remote target to detach. */
1949 /* Unregister the file descriptor from the event loop. */
1950 if (SERIAL_IS_ASYNC_P (remote_desc
))
1951 SERIAL_ASYNC (remote_desc
, NULL
, 0);
1955 puts_filtered ("Ending remote debugging.\n");
1958 /* Convert hex digit A to a number. */
1964 if (a
>= '0' && a
<= '9')
1966 else if (a
>= 'a' && a
<= 'f')
1967 return a
- 'a' + 10;
1968 else if (a
>= 'A' && a
<= 'F')
1969 return a
- 'A' + 10;
1971 error ("Reply contains invalid hex digit %d", a
);
1974 /* Convert number NIB to a hex digit. */
1983 return 'a' + nib
- 10;
1986 /* Tell the remote machine to resume. */
1988 static enum target_signal last_sent_signal
= TARGET_SIGNAL_0
;
1990 static int last_sent_step
;
1993 remote_resume (pid
, step
, siggnal
)
1995 enum target_signal siggnal
;
1997 char *buf
= alloca (PBUFSIZ
);
2000 set_thread (0, 0); /* run any thread */
2002 set_thread (pid
, 0); /* run this thread */
2004 dcache_flush (remote_dcache
);
2006 last_sent_signal
= siggnal
;
2007 last_sent_step
= step
;
2009 /* A hook for when we need to do something at the last moment before
2011 if (target_resume_hook
)
2012 (*target_resume_hook
) ();
2014 if (siggnal
!= TARGET_SIGNAL_0
)
2016 buf
[0] = step
? 'S' : 'C';
2017 buf
[1] = tohex (((int) siggnal
>> 4) & 0xf);
2018 buf
[2] = tohex ((int) siggnal
& 0xf);
2022 strcpy (buf
, step
? "s" : "c");
2027 /* Same as remote_resume, but with async support. */
2029 remote_async_resume (pid
, step
, siggnal
)
2031 enum target_signal siggnal
;
2033 char *buf
= alloca (PBUFSIZ
);
2036 set_thread (0, 0); /* run any thread */
2038 set_thread (pid
, 0); /* run this thread */
2040 dcache_flush (remote_dcache
);
2042 last_sent_signal
= siggnal
;
2043 last_sent_step
= step
;
2045 /* A hook for when we need to do something at the last moment before
2047 if (target_resume_hook
)
2048 (*target_resume_hook
) ();
2050 if (siggnal
!= TARGET_SIGNAL_0
)
2052 buf
[0] = step
? 'S' : 'C';
2053 buf
[1] = tohex (((int) siggnal
>> 4) & 0xf);
2054 buf
[2] = tohex ((int) siggnal
& 0xf);
2058 strcpy (buf
, step
? "s" : "c");
2060 /* We are about to start executing the inferior, let's register it
2061 with the event loop. NOTE: this is the one place where all the
2062 execution commands end up. We could alternatively do this in each
2063 of the execution commands in infcmd.c.*/
2064 /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
2065 into infcmd.c in order to allow inferior function calls to work
2066 NOT asynchronously. */
2067 if (event_loop_p
&& SERIAL_CAN_ASYNC_P (remote_desc
))
2068 target_async (inferior_event_handler
, 0);
2069 /* Tell the world that the target is now executing. */
2070 /* FIXME: cagney/1999-09-23: Is it the targets responsibility to set
2071 this? Instead, should the client of target just assume (for
2072 async targets) that the target is going to start executing? Is
2073 this information already found in the continuation block? */
2074 if (SERIAL_IS_ASYNC_P (remote_desc
))
2075 target_executing
= 1;
2080 /* Set up the signal handler for SIGINT, while the target is
2081 executing, ovewriting the 'regular' SIGINT signal handler. */
2083 initialize_sigint_signal_handler ()
2085 sigint_remote_token
=
2086 create_async_signal_handler (async_remote_interrupt
, NULL
);
2087 signal (SIGINT
, handle_remote_sigint
);
2090 /* Signal handler for SIGINT, while the target is executing. */
2092 handle_remote_sigint (sig
)
2095 signal (sig
, handle_remote_sigint_twice
);
2096 sigint_remote_twice_token
=
2097 create_async_signal_handler (async_remote_interrupt_twice
, NULL
);
2098 mark_async_signal_handler_wrapper (sigint_remote_token
);
2101 /* Signal handler for SIGINT, installed after SIGINT has already been
2102 sent once. It will take effect the second time that the user sends
2105 handle_remote_sigint_twice (sig
)
2108 signal (sig
, handle_sigint
);
2109 sigint_remote_twice_token
=
2110 create_async_signal_handler (inferior_event_handler_wrapper
, NULL
);
2111 mark_async_signal_handler_wrapper (sigint_remote_twice_token
);
2114 /* Perform the real interruption of the target execution, in response
2117 async_remote_interrupt (arg
)
2118 gdb_client_data arg
;
2121 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt called\n");
2126 /* Perform interrupt, if the first attempt did not succeed. Just give
2127 up on the target alltogether. */
2129 async_remote_interrupt_twice (arg
)
2130 gdb_client_data arg
;
2133 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt_twice called\n");
2134 /* Do something only if the target was not killed by the previous
2136 if (target_executing
)
2139 signal (SIGINT
, handle_remote_sigint
);
2143 /* Reinstall the usual SIGINT handlers, after the target has
2146 cleanup_sigint_signal_handler (void *dummy
)
2148 signal (SIGINT
, handle_sigint
);
2149 if (sigint_remote_twice_token
)
2150 delete_async_signal_handler ((struct async_signal_handler
**) & sigint_remote_twice_token
);
2151 if (sigint_remote_token
)
2152 delete_async_signal_handler ((struct async_signal_handler
**) & sigint_remote_token
);
2155 /* Send ^C to target to halt it. Target will respond, and send us a
2157 static void (*ofunc
) PARAMS ((int));
2159 /* The command line interface's stop routine. This function is installed
2160 as a signal handler for SIGINT. The first time a user requests a
2161 stop, we call remote_stop to send a break or ^C. If there is no
2162 response from the target (it didn't stop when the user requested it),
2163 we ask the user if he'd like to detach from the target. */
2165 remote_interrupt (signo
)
2168 /* If this doesn't work, try more severe steps. */
2169 signal (signo
, remote_interrupt_twice
);
2172 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt called\n");
2177 /* The user typed ^C twice. */
2180 remote_interrupt_twice (signo
)
2183 signal (signo
, ofunc
);
2185 signal (signo
, remote_interrupt
);
2188 /* This is the generic stop called via the target vector. When a target
2189 interrupt is requested, either by the command line or the GUI, we
2190 will eventually end up here. */
2194 /* Send a break or a ^C, depending on user preference. */
2196 fprintf_unfiltered (gdb_stdlog
, "remote_stop called\n");
2199 SERIAL_SEND_BREAK (remote_desc
);
2201 SERIAL_WRITE (remote_desc
, "\003", 1);
2204 /* Ask the user what to do when an interrupt is received. */
2209 target_terminal_ours ();
2211 if (query ("Interrupted while waiting for the program.\n\
2212 Give up (and stop debugging it)? "))
2214 target_mourn_inferior ();
2215 return_to_top_level (RETURN_QUIT
);
2218 target_terminal_inferior ();
2221 /* Enable/disable target terminal ownership. Most targets can use
2222 terminal groups to control terminal ownership. Remote targets are
2223 different in that explicit transfer of ownership to/from GDB/target
2227 remote_async_terminal_inferior (void)
2229 /* FIXME: cagney/1999-09-27: Shouldn't need to test for
2230 sync_execution here. This function should only be called when
2231 GDB is resuming the inferior in the forground. A background
2232 resume (``run&'') should leave GDB in control of the terminal and
2233 consequently should not call this code. */
2234 if (!sync_execution
)
2236 /* FIXME: cagney/1999-09-27: Closely related to the above. Make
2237 calls target_terminal_*() idenpotent. The event-loop GDB talking
2238 to an asynchronous target with a synchronous command calls this
2239 function from both event-top.c and infrun.c/infcmd.c. Once GDB
2240 stops trying to transfer the terminal to the target when it
2241 shouldn't this guard can go away. */
2242 if (!remote_async_terminal_ours_p
)
2244 delete_file_handler (input_fd
);
2245 remote_async_terminal_ours_p
= 0;
2246 initialize_sigint_signal_handler ();
2247 /* NOTE: At this point we could also register our selves as the
2248 recipient of all input. Any characters typed could then be
2249 passed on down to the target. */
2253 remote_async_terminal_ours (void)
2255 /* See FIXME in remote_async_terminal_inferior. */
2256 if (!sync_execution
)
2258 /* See FIXME in remote_async_terminal_inferior. */
2259 if (remote_async_terminal_ours_p
)
2261 cleanup_sigint_signal_handler (NULL
);
2262 add_file_handler (input_fd
, stdin_event_handler
, 0);
2263 remote_async_terminal_ours_p
= 1;
2266 /* If nonzero, ignore the next kill. */
2271 remote_console_output (char *msg
)
2275 for (p
= msg
; p
[0] && p
[1]; p
+= 2)
2278 char c
= fromhex (p
[0]) * 16 + fromhex (p
[1]);
2281 fputs_unfiltered (tb
, gdb_stdtarg
);
2283 gdb_flush (gdb_stdtarg
);
2286 /* Wait until the remote machine stops, then return,
2287 storing status in STATUS just as `wait' would.
2288 Returns "pid", which in the case of a multi-threaded
2289 remote OS, is the thread-id. */
2292 remote_wait (pid
, status
)
2294 struct target_waitstatus
*status
;
2296 unsigned char *buf
= alloca (PBUFSIZ
);
2297 int thread_num
= -1;
2299 status
->kind
= TARGET_WAITKIND_EXITED
;
2300 status
->value
.integer
= 0;
2306 ofunc
= signal (SIGINT
, remote_interrupt
);
2307 getpkt ((char *) buf
, 1);
2308 signal (SIGINT
, ofunc
);
2310 /* This is a hook for when we need to do something (perhaps the
2311 collection of trace data) every time the target stops. */
2312 if (target_wait_loop_hook
)
2313 (*target_wait_loop_hook
) ();
2317 case 'E': /* Error of some sort */
2318 warning ("Remote failure reply: %s", buf
);
2320 case 'T': /* Status with PC, SP, FP, ... */
2324 char regs
[MAX_REGISTER_RAW_SIZE
];
2326 /* Expedited reply, containing Signal, {regno, reg} repeat */
2327 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
2329 n... = register number
2330 r... = register contents
2332 p
= &buf
[3]; /* after Txx */
2339 /* Read the register number */
2340 regno
= strtol ((const char *) p
, &p_temp
, 16);
2341 p1
= (unsigned char *) p_temp
;
2343 if (p1
== p
) /* No register number present here */
2345 p1
= (unsigned char *) strchr ((const char *) p
, ':');
2347 warning ("Malformed packet(a) (missing colon): %s\n\
2350 if (strncmp ((const char *) p
, "thread", p1
- p
) == 0)
2352 p_temp
= unpack_varlen_hex (++p1
, &thread_num
);
2353 record_currthread (thread_num
);
2354 p
= (unsigned char *) p_temp
;
2362 warning ("Malformed packet(b) (missing colon): %s\n\
2366 if (regno
>= NUM_REGS
)
2367 warning ("Remote sent bad register number %ld: %s\n\
2371 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
++)
2373 if (p
[0] == 0 || p
[1] == 0)
2374 warning ("Remote reply is too short: %s", buf
);
2375 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
2378 supply_register (regno
, regs
);
2383 warning ("Remote register badly formatted: %s", buf
);
2384 warning (" here: %s", p
);
2389 case 'S': /* Old style status, just signal only */
2390 status
->kind
= TARGET_WAITKIND_STOPPED
;
2391 status
->value
.sig
= (enum target_signal
)
2392 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
2396 /* Export Cisco kernel mode as a convenience variable
2397 (so that it can be used in the GDB prompt if desired). */
2399 if (cisco_kernel_mode
== 1)
2400 set_internalvar (lookup_internalvar ("cisco_kernel_mode"),
2401 value_from_string ("PDEBUG-"));
2402 cisco_kernel_mode
= 0;
2403 thread_num
= strtol ((const char *) &buf
[4], NULL
, 16);
2404 record_currthread (thread_num
);
2406 else if (buf
[3] == 'k')
2408 /* Export Cisco kernel mode as a convenience variable
2409 (so that it can be used in the GDB prompt if desired). */
2411 if (cisco_kernel_mode
== 1)
2412 set_internalvar (lookup_internalvar ("cisco_kernel_mode"),
2413 value_from_string ("KDEBUG-"));
2414 cisco_kernel_mode
= 1;
2417 case 'N': /* Cisco special: status and offsets */
2419 bfd_vma text_addr
, data_addr
, bss_addr
;
2420 bfd_signed_vma text_off
, data_off
, bss_off
;
2423 status
->kind
= TARGET_WAITKIND_STOPPED
;
2424 status
->value
.sig
= (enum target_signal
)
2425 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
2427 if (symfile_objfile
== NULL
)
2429 warning ("Relocation packet received with no symbol file. \
2434 /* Relocate object file. Buffer format is NAATT;DD;BB
2435 * where AA is the signal number, TT is the new text
2436 * address, DD * is the new data address, and BB is the
2437 * new bss address. */
2440 text_addr
= strtoul (p
, (char **) &p1
, 16);
2441 if (p1
== p
|| *p1
!= ';')
2442 warning ("Malformed relocation packet: Packet '%s'", buf
);
2444 data_addr
= strtoul (p
, (char **) &p1
, 16);
2445 if (p1
== p
|| *p1
!= ';')
2446 warning ("Malformed relocation packet: Packet '%s'", buf
);
2448 bss_addr
= strtoul (p
, (char **) &p1
, 16);
2450 warning ("Malformed relocation packet: Packet '%s'", buf
);
2452 if (remote_cisco_section_offsets (text_addr
, data_addr
, bss_addr
,
2453 &text_off
, &data_off
, &bss_off
)
2455 if (text_off
!= 0 || data_off
!= 0 || bss_off
!= 0)
2456 remote_cisco_objfile_relocate (text_off
, data_off
, bss_off
);
2460 case 'W': /* Target exited */
2462 /* The remote process exited. */
2463 status
->kind
= TARGET_WAITKIND_EXITED
;
2464 status
->value
.integer
= (fromhex (buf
[1]) << 4) + fromhex (buf
[2]);
2468 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
2469 status
->value
.sig
= (enum target_signal
)
2470 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
2474 case 'O': /* Console output */
2475 remote_console_output (buf
+ 1);
2478 if (last_sent_signal
!= TARGET_SIGNAL_0
)
2480 /* Zero length reply means that we tried 'S' or 'C' and
2481 the remote system doesn't support it. */
2482 target_terminal_ours_for_output ();
2484 ("Can't send signals to this remote system. %s not sent.\n",
2485 target_signal_to_name (last_sent_signal
));
2486 last_sent_signal
= TARGET_SIGNAL_0
;
2487 target_terminal_inferior ();
2489 strcpy ((char *) buf
, last_sent_step
? "s" : "c");
2490 putpkt ((char *) buf
);
2493 /* else fallthrough */
2495 warning ("Invalid remote reply: %s", buf
);
2500 if (thread_num
!= -1)
2504 return inferior_pid
;
2507 /* Async version of remote_wait. */
2509 remote_async_wait (pid
, status
)
2511 struct target_waitstatus
*status
;
2513 unsigned char *buf
= alloca (PBUFSIZ
);
2514 int thread_num
= -1;
2516 status
->kind
= TARGET_WAITKIND_EXITED
;
2517 status
->value
.integer
= 0;
2523 if (!SERIAL_IS_ASYNC_P (remote_desc
))
2524 ofunc
= signal (SIGINT
, remote_interrupt
);
2525 /* FIXME: cagney/1999-09-27: If we're in async mode we should
2526 _never_ wait for ever -> test on target_is_async_p().
2527 However, before we do that we need to ensure that the caller
2528 knows how to take the target into/out of async mode. */
2529 getpkt ((char *) buf
, wait_forever_enabled_p
);
2530 if (!SERIAL_IS_ASYNC_P (remote_desc
))
2531 signal (SIGINT
, ofunc
);
2533 /* This is a hook for when we need to do something (perhaps the
2534 collection of trace data) every time the target stops. */
2535 if (target_wait_loop_hook
)
2536 (*target_wait_loop_hook
) ();
2540 case 'E': /* Error of some sort */
2541 warning ("Remote failure reply: %s", buf
);
2543 case 'T': /* Status with PC, SP, FP, ... */
2547 char regs
[MAX_REGISTER_RAW_SIZE
];
2549 /* Expedited reply, containing Signal, {regno, reg} repeat */
2550 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
2552 n... = register number
2553 r... = register contents
2555 p
= &buf
[3]; /* after Txx */
2562 /* Read the register number */
2563 regno
= strtol ((const char *) p
, &p_temp
, 16);
2564 p1
= (unsigned char *) p_temp
;
2566 if (p1
== p
) /* No register number present here */
2568 p1
= (unsigned char *) strchr ((const char *) p
, ':');
2570 warning ("Malformed packet(a) (missing colon): %s\n\
2573 if (strncmp ((const char *) p
, "thread", p1
- p
) == 0)
2575 p_temp
= unpack_varlen_hex (++p1
, &thread_num
);
2576 record_currthread (thread_num
);
2577 p
= (unsigned char *) p_temp
;
2585 warning ("Malformed packet(b) (missing colon): %s\n\
2589 if (regno
>= NUM_REGS
)
2590 warning ("Remote sent bad register number %ld: %s\n\
2594 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
++)
2596 if (p
[0] == 0 || p
[1] == 0)
2597 warning ("Remote reply is too short: %s", buf
);
2598 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
2601 supply_register (regno
, regs
);
2606 warning ("Remote register badly formatted: %s", buf
);
2607 warning (" here: %s", p
);
2612 case 'S': /* Old style status, just signal only */
2613 status
->kind
= TARGET_WAITKIND_STOPPED
;
2614 status
->value
.sig
= (enum target_signal
)
2615 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
2619 /* Export Cisco kernel mode as a convenience variable
2620 (so that it can be used in the GDB prompt if desired). */
2622 if (cisco_kernel_mode
== 1)
2623 set_internalvar (lookup_internalvar ("cisco_kernel_mode"),
2624 value_from_string ("PDEBUG-"));
2625 cisco_kernel_mode
= 0;
2626 thread_num
= strtol ((const char *) &buf
[4], NULL
, 16);
2627 record_currthread (thread_num
);
2629 else if (buf
[3] == 'k')
2631 /* Export Cisco kernel mode as a convenience variable
2632 (so that it can be used in the GDB prompt if desired). */
2634 if (cisco_kernel_mode
== 1)
2635 set_internalvar (lookup_internalvar ("cisco_kernel_mode"),
2636 value_from_string ("KDEBUG-"));
2637 cisco_kernel_mode
= 1;
2640 case 'N': /* Cisco special: status and offsets */
2642 bfd_vma text_addr
, data_addr
, bss_addr
;
2643 bfd_signed_vma text_off
, data_off
, bss_off
;
2646 status
->kind
= TARGET_WAITKIND_STOPPED
;
2647 status
->value
.sig
= (enum target_signal
)
2648 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
2650 if (symfile_objfile
== NULL
)
2652 warning ("Relocation packet recieved with no symbol file. \
2657 /* Relocate object file. Buffer format is NAATT;DD;BB
2658 * where AA is the signal number, TT is the new text
2659 * address, DD * is the new data address, and BB is the
2660 * new bss address. */
2663 text_addr
= strtoul (p
, (char **) &p1
, 16);
2664 if (p1
== p
|| *p1
!= ';')
2665 warning ("Malformed relocation packet: Packet '%s'", buf
);
2667 data_addr
= strtoul (p
, (char **) &p1
, 16);
2668 if (p1
== p
|| *p1
!= ';')
2669 warning ("Malformed relocation packet: Packet '%s'", buf
);
2671 bss_addr
= strtoul (p
, (char **) &p1
, 16);
2673 warning ("Malformed relocation packet: Packet '%s'", buf
);
2675 if (remote_cisco_section_offsets (text_addr
, data_addr
, bss_addr
,
2676 &text_off
, &data_off
, &bss_off
)
2678 if (text_off
!= 0 || data_off
!= 0 || bss_off
!= 0)
2679 remote_cisco_objfile_relocate (text_off
, data_off
, bss_off
);
2683 case 'W': /* Target exited */
2685 /* The remote process exited. */
2686 status
->kind
= TARGET_WAITKIND_EXITED
;
2687 status
->value
.integer
= (fromhex (buf
[1]) << 4) + fromhex (buf
[2]);
2691 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
2692 status
->value
.sig
= (enum target_signal
)
2693 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
2697 case 'O': /* Console output */
2698 remote_console_output (buf
+ 1);
2701 if (last_sent_signal
!= TARGET_SIGNAL_0
)
2703 /* Zero length reply means that we tried 'S' or 'C' and
2704 the remote system doesn't support it. */
2705 target_terminal_ours_for_output ();
2707 ("Can't send signals to this remote system. %s not sent.\n",
2708 target_signal_to_name (last_sent_signal
));
2709 last_sent_signal
= TARGET_SIGNAL_0
;
2710 target_terminal_inferior ();
2712 strcpy ((char *) buf
, last_sent_step
? "s" : "c");
2713 putpkt ((char *) buf
);
2716 /* else fallthrough */
2718 warning ("Invalid remote reply: %s", buf
);
2723 if (thread_num
!= -1)
2727 return inferior_pid
;
2730 /* Number of bytes of registers this stub implements. */
2732 static int register_bytes_found
;
2734 /* Read the remote registers into the block REGS. */
2735 /* Currently we just read all the registers, so we don't use regno. */
2739 remote_fetch_registers (regno
)
2742 char *buf
= alloca (PBUFSIZ
);
2745 char regs
[REGISTER_BYTES
];
2747 set_thread (inferior_pid
, 1);
2752 if (remote_register_buf_size
== 0)
2753 remote_register_buf_size
= strlen (buf
);
2755 /* Unimplemented registers read as all bits zero. */
2756 memset (regs
, 0, REGISTER_BYTES
);
2758 /* We can get out of synch in various cases. If the first character
2759 in the buffer is not a hex character, assume that has happened
2760 and try to fetch another packet to read. */
2761 while ((buf
[0] < '0' || buf
[0] > '9')
2762 && (buf
[0] < 'a' || buf
[0] > 'f')
2763 && buf
[0] != 'x') /* New: unavailable register value */
2766 fprintf_unfiltered (gdb_stdlog
,
2767 "Bad register packet; fetching a new packet\n");
2771 /* Reply describes registers byte by byte, each byte encoded as two
2772 hex characters. Suck them all up, then supply them to the
2773 register cacheing/storage mechanism. */
2776 for (i
= 0; i
< REGISTER_BYTES
; i
++)
2782 warning ("Remote reply is of odd length: %s", buf
);
2783 /* Don't change register_bytes_found in this case, and don't
2784 print a second warning. */
2787 if (p
[0] == 'x' && p
[1] == 'x')
2788 regs
[i
] = 0; /* 'x' */
2790 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
2794 if (i
!= register_bytes_found
)
2796 register_bytes_found
= i
;
2797 #ifdef REGISTER_BYTES_OK
2798 if (!REGISTER_BYTES_OK (i
))
2799 warning ("Remote reply is too short: %s", buf
);
2804 for (i
= 0; i
< NUM_REGS
; i
++)
2806 supply_register (i
, ®s
[REGISTER_BYTE (i
)]);
2807 if (buf
[REGISTER_BYTE (i
) * 2] == 'x')
2808 register_valid
[i
] = -1; /* register value not available */
2812 /* Prepare to store registers. Since we may send them all (using a
2813 'G' request), we have to read out the ones we don't want to change
2817 remote_prepare_to_store ()
2819 /* Make sure the entire registers array is valid. */
2820 switch (remote_protocol_P
.support
)
2822 case PACKET_DISABLE
:
2823 case PACKET_SUPPORT_UNKNOWN
:
2824 read_register_bytes (0, (char *) NULL
, REGISTER_BYTES
);
2831 /* Helper: Attempt to store REGNO using the P packet. Return fail IFF
2832 packet was not recognized. */
2835 store_register_using_P (regno
)
2838 /* Try storing a single register. */
2839 char *buf
= alloca (PBUFSIZ
);
2844 sprintf (buf
, "P%x=", regno
);
2845 p
= buf
+ strlen (buf
);
2846 regp
= ®isters
[REGISTER_BYTE (regno
)];
2847 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); ++i
)
2849 *p
++ = tohex ((regp
[i
] >> 4) & 0xf);
2850 *p
++ = tohex (regp
[i
] & 0xf);
2855 return buf
[0] != '\0';
2859 /* Store register REGNO, or all registers if REGNO == -1, from the contents
2860 of REGISTERS. FIXME: ignores errors. */
2863 remote_store_registers (regno
)
2866 char *buf
= alloca (PBUFSIZ
);
2870 set_thread (inferior_pid
, 1);
2874 switch (remote_protocol_P
.support
)
2876 case PACKET_DISABLE
:
2879 if (store_register_using_P (regno
))
2882 error ("Protocol error: P packet not recognized by stub");
2883 case PACKET_SUPPORT_UNKNOWN
:
2884 if (store_register_using_P (regno
))
2886 /* The stub recognized the 'P' packet. Remember this. */
2887 remote_protocol_P
.support
= PACKET_ENABLE
;
2892 /* The stub does not support the 'P' packet. Use 'G'
2893 instead, and don't try using 'P' in the future (it
2894 will just waste our time). */
2895 remote_protocol_P
.support
= PACKET_DISABLE
;
2903 /* Command describes registers byte by byte,
2904 each byte encoded as two hex characters. */
2907 /* remote_prepare_to_store insures that register_bytes_found gets set. */
2908 for (i
= 0; i
< register_bytes_found
; i
++)
2910 *p
++ = tohex ((registers
[i
] >> 4) & 0xf);
2911 *p
++ = tohex (registers
[i
] & 0xf);
2918 /* Use of the data cache *used* to be disabled because it loses for looking
2919 at and changing hardware I/O ports and the like. Accepting `volatile'
2920 would perhaps be one way to fix it. Another idea would be to use the
2921 executable file for the text segment (for all SEC_CODE sections?
2922 For all SEC_READONLY sections?). This has problems if you want to
2923 actually see what the memory contains (e.g. self-modifying code,
2924 clobbered memory, user downloaded the wrong thing).
2926 Because it speeds so much up, it's now enabled, if you're playing
2927 with registers you turn it of (set remotecache 0). */
2929 /* Read a word from remote address ADDR and return it.
2930 This goes through the data cache. */
2934 remote_fetch_word (addr
)
2937 return dcache_fetch (remote_dcache
, addr
);
2940 /* Write a word WORD into remote address ADDR.
2941 This goes through the data cache. */
2944 remote_store_word (addr
, word
)
2948 dcache_poke (remote_dcache
, addr
, word
);
2950 #endif /* 0 (unused?) */
2954 /* Return the number of hex digits in num. */
2962 for (i
= 0; num
!= 0; i
++)
2968 /* Set BUF to the minimum number of hex digits representing NUM. */
2971 hexnumstr (buf
, num
)
2975 int len
= hexnumlen (num
);
2976 return hexnumnstr (buf
, num
, len
);
2980 /* Set BUF to the hex digits representing NUM, padded to WIDTH characters. */
2983 hexnumnstr (buf
, num
, width
)
2992 for (i
= width
- 1; i
>= 0; i
--)
2994 buf
[i
] = "0123456789abcdef"[(num
& 0xf)];
3001 /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
3004 remote_address_masked (addr
)
3007 if (remote_address_size
> 0
3008 && remote_address_size
< (sizeof (ULONGEST
) * 8))
3010 /* Only create a mask when that mask can safely be constructed
3011 in a ULONGEST variable. */
3013 mask
= (mask
<< remote_address_size
) - 1;
3019 /* Determine whether the remote target supports binary downloading.
3020 This is accomplished by sending a no-op memory write of zero length
3021 to the target at the specified address. It does not suffice to send
3022 the whole packet, since many stubs strip the eighth bit and subsequently
3023 compute a wrong checksum, which causes real havoc with remote_write_bytes.
3025 NOTE: This can still lose if the serial line is not eight-bit
3026 clean. In cases like this, the user should clear "remote
3030 check_binary_download (addr
)
3033 switch (remote_protocol_binary_download
.support
)
3035 case PACKET_DISABLE
:
3039 case PACKET_SUPPORT_UNKNOWN
:
3041 char *buf
= alloca (PBUFSIZ
);
3046 p
+= hexnumstr (p
, (ULONGEST
) addr
);
3048 p
+= hexnumstr (p
, (ULONGEST
) 0);
3052 putpkt_binary (buf
, (int) (p
- buf
));
3058 fprintf_unfiltered (gdb_stdlog
,
3059 "binary downloading NOT suppported by target\n");
3060 remote_protocol_binary_download
.support
= PACKET_DISABLE
;
3065 fprintf_unfiltered (gdb_stdlog
,
3066 "binary downloading suppported by target\n");
3067 remote_protocol_binary_download
.support
= PACKET_ENABLE
;
3074 /* Write memory data directly to the remote machine.
3075 This does not inform the data cache; the data cache uses this.
3076 MEMADDR is the address in the remote memory space.
3077 MYADDR is the address of the buffer in our space.
3078 LEN is the number of bytes.
3080 Returns number of bytes transferred, or 0 (setting errno) for
3081 error. Only transfer a single packet. */
3084 remote_write_bytes (CORE_ADDR memaddr
, char *myaddr
, int len
)
3087 int max_buf_size
; /* Max size of packet output buffer */
3089 unsigned char *plen
;
3094 /* Verify that the target can support a binary download */
3095 check_binary_download (memaddr
);
3097 /* Determine the max packet size. */
3098 max_buf_size
= min (remote_write_size
, PBUFSIZ
);
3099 if (remote_register_buf_size
!= 0)
3100 max_buf_size
= min (max_buf_size
, remote_register_buf_size
);
3101 buf
= alloca (max_buf_size
+ 1);
3103 /* Subtract header overhead from max payload size - $M<memaddr>,<len>:#nn */
3104 max_buf_size
-= 2 + hexnumlen (memaddr
+ len
- 1) + 1 + hexnumlen (len
) + 4;
3106 /* construct "M"<memaddr>","<len>":" */
3107 /* sprintf (buf, "M%lx,%x:", (unsigned long) memaddr, todo); */
3110 /* Append [XM]. Compute a best guess of the number of bytes
3111 actually transfered. */
3112 switch (remote_protocol_binary_download
.support
)
3116 /* Best guess at number of bytes that will fit. */
3117 todo
= min (len
, max_buf_size
);
3119 case PACKET_DISABLE
:
3121 /* num bytes that will fit */
3122 todo
= min (len
, max_buf_size
/ 2);
3124 case PACKET_SUPPORT_UNKNOWN
:
3125 internal_error ("remote_write_bytes: bad switch");
3128 /* Append <memaddr> */
3129 memaddr
= remote_address_masked (memaddr
);
3130 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
3133 /* Append <len>. Retain the location/size of <len>. It may
3134 need to be adjusted once the packet body has been created. */
3136 plenlen
= hexnumstr (p
, (ULONGEST
) todo
);
3141 /* Append the packet body. */
3142 switch (remote_protocol_binary_download
.support
)
3145 /* Binary mode. Send target system values byte by byte, in
3146 increasing byte addresses. Only escape certain critical
3149 (nr_bytes
< todo
) && (p
- buf
) < (max_buf_size
- 2);
3152 switch (myaddr
[nr_bytes
] & 0xff)
3157 /* These must be escaped */
3159 *p
++ = (myaddr
[nr_bytes
] & 0xff) ^ 0x20;
3162 *p
++ = myaddr
[nr_bytes
] & 0xff;
3166 if (nr_bytes
< todo
)
3168 /* Escape chars have filled up the buffer prematurely,
3169 and we have actually sent fewer bytes than planned.
3170 Fix-up the length field of the packet. Use the same
3171 number of characters as before. */
3173 plen
+= hexnumnstr (plen
, (ULONGEST
) nr_bytes
, plenlen
);
3174 *plen
= ':'; /* overwrite \0 from hexnumnstr() */
3177 case PACKET_DISABLE
:
3178 /* Normal mode: Send target system values byte by byte, in
3179 increasing byte addresses. Each byte is encoded as a two hex
3181 for (nr_bytes
= 0; nr_bytes
< todo
; nr_bytes
++)
3183 *p
++ = tohex ((myaddr
[nr_bytes
] >> 4) & 0xf);
3184 *p
++ = tohex (myaddr
[nr_bytes
] & 0xf);
3188 case PACKET_SUPPORT_UNKNOWN
:
3189 internal_error ("remote_write_bytes: bad switch");
3192 putpkt_binary (buf
, (int) (p
- buf
));
3197 /* There is no correspondance between what the remote protocol
3198 uses for errors and errno codes. We would like a cleaner way
3199 of representing errors (big enough to include errno codes,
3200 bfd_error codes, and others). But for now just return EIO. */
3205 /* Return NR_BYTES, not TODO, in case escape chars caused us to send fewer
3206 bytes than we'd planned. */
3210 /* Read memory data directly from the remote machine.
3211 This does not use the data cache; the data cache uses this.
3212 MEMADDR is the address in the remote memory space.
3213 MYADDR is the address of the buffer in our space.
3214 LEN is the number of bytes.
3216 Returns number of bytes transferred, or 0 for error. */
3218 /* NOTE: cagney/1999-10-18: This function (and its siblings in other
3219 remote targets) shouldn't attempt to read the entire buffer.
3220 Instead it should read a single packet worth of data and then
3221 return the byte size of that packet to the caller. The caller (its
3222 caller and its callers caller ;-) already contains code for
3223 handling partial reads. */
3226 remote_read_bytes (memaddr
, myaddr
, len
)
3231 char *buf
= alloca (PBUFSIZ
);
3232 int max_buf_size
; /* Max size of packet output buffer */
3235 /* Chop the transfer down if necessary */
3237 max_buf_size
= min (remote_write_size
, PBUFSIZ
);
3238 if (remote_register_buf_size
!= 0)
3239 max_buf_size
= min (max_buf_size
, remote_register_buf_size
);
3248 todo
= min (len
, max_buf_size
/ 2); /* num bytes that will fit */
3250 /* construct "m"<memaddr>","<len>" */
3251 /* sprintf (buf, "m%lx,%x", (unsigned long) memaddr, todo); */
3252 memaddr
= remote_address_masked (memaddr
);
3255 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
3257 p
+= hexnumstr (p
, (ULONGEST
) todo
);
3265 /* There is no correspondance between what the remote protocol uses
3266 for errors and errno codes. We would like a cleaner way of
3267 representing errors (big enough to include errno codes, bfd_error
3268 codes, and others). But for now just return EIO. */
3273 /* Reply describes memory byte by byte,
3274 each byte encoded as two hex characters. */
3277 for (i
= 0; i
< todo
; i
++)
3279 if (p
[0] == 0 || p
[1] == 0)
3280 /* Reply is short. This means that we were able to read
3281 only part of what we wanted to. */
3282 return i
+ (origlen
- len
);
3283 myaddr
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
3293 /* Read or write LEN bytes from inferior memory at MEMADDR,
3294 transferring to or from debugger address BUFFER. Write to inferior if
3295 SHOULD_WRITE is nonzero. Returns length of data written or read; 0
3298 #ifndef REMOTE_TRANSLATE_XFER_ADDRESS
3299 #define REMOTE_TRANSLATE_XFER_ADDRESS(MEM_ADDR, MEM_LEN, TARG_ADDR, TARG_LEN) \
3300 (*(TARG_ADDR) = (MEM_ADDR), *(TARG_LEN) = (MEM_LEN))
3305 remote_xfer_memory (mem_addr
, buffer
, mem_len
, should_write
, target
)
3310 struct target_ops
*target
; /* ignored */
3312 CORE_ADDR targ_addr
;
3314 REMOTE_TRANSLATE_XFER_ADDRESS (mem_addr
, mem_len
, &targ_addr
, &targ_len
);
3318 return dcache_xfer_memory (remote_dcache
, targ_addr
, buffer
,
3319 targ_len
, should_write
);
3324 /* Enable after 4.12. */
3327 remote_search (len
, data
, mask
, startaddr
, increment
, lorange
, hirange
3328 addr_found
, data_found
)
3332 CORE_ADDR startaddr
;
3336 CORE_ADDR
*addr_found
;
3339 if (increment
== -4 && len
== 4)
3341 long mask_long
, data_long
;
3342 long data_found_long
;
3343 CORE_ADDR addr_we_found
;
3344 char *buf
= alloca (PBUFSIZ
);
3345 long returned_long
[2];
3348 mask_long
= extract_unsigned_integer (mask
, len
);
3349 data_long
= extract_unsigned_integer (data
, len
);
3350 sprintf (buf
, "t%x:%x,%x", startaddr
, data_long
, mask_long
);
3355 /* The stub doesn't support the 't' request. We might want to
3356 remember this fact, but on the other hand the stub could be
3357 switched on us. Maybe we should remember it only until
3358 the next "target remote". */
3359 generic_search (len
, data
, mask
, startaddr
, increment
, lorange
,
3360 hirange
, addr_found
, data_found
);
3365 /* There is no correspondance between what the remote protocol uses
3366 for errors and errno codes. We would like a cleaner way of
3367 representing errors (big enough to include errno codes, bfd_error
3368 codes, and others). But for now just use EIO. */
3369 memory_error (EIO
, startaddr
);
3372 while (*p
!= '\0' && *p
!= ',')
3373 addr_we_found
= (addr_we_found
<< 4) + fromhex (*p
++);
3375 error ("Protocol error: short return for search");
3377 data_found_long
= 0;
3378 while (*p
!= '\0' && *p
!= ',')
3379 data_found_long
= (data_found_long
<< 4) + fromhex (*p
++);
3380 /* Ignore anything after this comma, for future extensions. */
3382 if (addr_we_found
< lorange
|| addr_we_found
>= hirange
)
3388 *addr_found
= addr_we_found
;
3389 *data_found
= store_unsigned_integer (data_we_found
, len
);
3392 generic_search (len
, data
, mask
, startaddr
, increment
, lorange
,
3393 hirange
, addr_found
, data_found
);
3398 remote_files_info (ignore
)
3399 struct target_ops
*ignore
;
3401 puts_filtered ("Debugging a target over a serial line.\n");
3404 /* Stuff for dealing with the packets which are part of this protocol.
3405 See comment at top of file for details. */
3407 /* Read a single character from the remote end, masking it down to 7 bits. */
3415 ch
= SERIAL_READCHAR (remote_desc
, timeout
);
3420 switch ((enum serial_rc
) ch
)
3423 target_mourn_inferior ();
3424 error ("Remote connection closed");
3427 perror_with_name ("Remote communication error");
3429 case SERIAL_TIMEOUT
:
3435 /* Send the command in BUF to the remote machine, and read the reply
3436 into BUF. Report an error if we get an error reply. */
3446 error ("Remote failure reply: %s", buf
);
3449 /* Display a null-terminated packet on stdout, for debugging, using C
3456 puts_filtered ("\"");
3457 fputstr_filtered (buf
, '"', gdb_stdout
);
3458 puts_filtered ("\"");
3465 return putpkt_binary (buf
, strlen (buf
));
3468 /* Send a packet to the remote machine, with error checking. The data
3469 of the packet is in BUF. The string in BUF can be at most PBUFSIZ - 5
3470 to account for the $, # and checksum, and for a possible /0 if we are
3471 debugging (remote_debug) and want to print the sent packet as a string */
3474 putpkt_binary (buf
, cnt
)
3479 unsigned char csum
= 0;
3480 char *buf2
= alloca (PBUFSIZ
);
3481 char *junkbuf
= alloca (PBUFSIZ
);
3487 /* Copy the packet into buffer BUF2, encapsulating it
3488 and giving it a checksum. */
3490 if (cnt
> BUFSIZ
- 5) /* Prosanity check */
3496 for (i
= 0; i
< cnt
; i
++)
3502 *p
++ = tohex ((csum
>> 4) & 0xf);
3503 *p
++ = tohex (csum
& 0xf);
3505 /* Send it over and over until we get a positive ack. */
3509 int started_error_output
= 0;
3514 fprintf_unfiltered (gdb_stdlog
, "Sending packet: ");
3515 fputstrn_unfiltered (buf2
, p
- buf2
, 0, gdb_stdlog
);
3516 fprintf_unfiltered (gdb_stdlog
, "...");
3517 gdb_flush (gdb_stdlog
);
3519 if (SERIAL_WRITE (remote_desc
, buf2
, p
- buf2
))
3520 perror_with_name ("putpkt: write failed");
3522 /* read until either a timeout occurs (-2) or '+' is read */
3525 ch
= readchar (remote_timeout
);
3532 case SERIAL_TIMEOUT
:
3534 if (started_error_output
)
3536 putchar_unfiltered ('\n');
3537 started_error_output
= 0;
3546 fprintf_unfiltered (gdb_stdlog
, "Ack\n");
3548 case SERIAL_TIMEOUT
:
3552 break; /* Retransmit buffer */
3555 /* It's probably an old response, and we're out of sync.
3556 Just gobble up the packet and ignore it. */
3557 getpkt (junkbuf
, 0);
3558 continue; /* Now, go look for + */
3563 if (!started_error_output
)
3565 started_error_output
= 1;
3566 fprintf_unfiltered (gdb_stdlog
, "putpkt: Junk: ");
3568 fputc_unfiltered (ch
& 0177, gdb_stdlog
);
3572 break; /* Here to retransmit */
3576 /* This is wrong. If doing a long backtrace, the user should be
3577 able to get out next time we call QUIT, without anything as
3578 violent as interrupt_query. If we want to provide a way out of
3579 here without getting to the next QUIT, it should be based on
3580 hitting ^C twice as in remote_wait. */
3590 static int remote_cisco_mode
;
3592 /* Come here after finding the start of the frame. Collect the rest
3593 into BUF, verifying the checksum, length, and handling run-length
3594 compression. Returns 0 on any error, 1 on success. */
3609 c
= readchar (remote_timeout
);
3613 case SERIAL_TIMEOUT
:
3615 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog
);
3619 fputs_filtered ("Saw new packet start in middle of old one\n",
3621 return 0; /* Start a new packet, count retries */
3624 unsigned char pktcsum
;
3628 pktcsum
= fromhex (readchar (remote_timeout
)) << 4;
3629 pktcsum
|= fromhex (readchar (remote_timeout
));
3631 if (csum
== pktcsum
)
3636 fprintf_filtered (gdb_stdlog
,
3637 "Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
3639 fputs_filtered (buf
, gdb_stdlog
);
3640 fputs_filtered ("\n", gdb_stdlog
);
3644 case '*': /* Run length encoding */
3649 if (remote_cisco_mode
== 0)
3651 c
= readchar (remote_timeout
);
3653 repeat
= c
- ' ' + 3; /* Compute repeat count */
3657 /* Cisco's run-length encoding variant uses two
3658 hex chars to represent the repeat count. */
3660 c
= readchar (remote_timeout
);
3662 repeat
= fromhex (c
) << 4;
3663 c
= readchar (remote_timeout
);
3665 repeat
+= fromhex (c
);
3668 if (repeat
> 0 && repeat
<= 255
3669 && bp
+ repeat
- 1 < buf
+ PBUFSIZ
- 1)
3671 memset (bp
, *(bp
- 1), repeat
);
3677 printf_filtered ("Repeat count %d too large for buffer: ", repeat
);
3678 puts_filtered (buf
);
3679 puts_filtered ("\n");
3683 if (bp
< buf
+ PBUFSIZ
- 1)
3691 puts_filtered ("Remote packet too long: ");
3692 puts_filtered (buf
);
3693 puts_filtered ("\n");
3700 /* Read a packet from the remote machine, with error checking, and
3701 store it in BUF. BUF is expected to be of size PBUFSIZ. If
3702 FOREVER, wait forever rather than timing out; this is used while
3703 the target is executing user code. */
3706 getpkt (buf
, forever
)
3715 strcpy (buf
, "timeout");
3719 timeout
= watchdog
> 0 ? watchdog
: -1;
3723 timeout
= remote_timeout
;
3727 for (tries
= 1; tries
<= MAX_TRIES
; tries
++)
3729 /* This can loop forever if the remote side sends us characters
3730 continuously, but if it pauses, we'll get a zero from readchar
3731 because of timeout. Then we'll count that as a retry. */
3733 /* Note that we will only wait forever prior to the start of a packet.
3734 After that, we expect characters to arrive at a brisk pace. They
3735 should show up within remote_timeout intervals. */
3739 c
= readchar (timeout
);
3741 if (c
== SERIAL_TIMEOUT
)
3743 if (forever
) /* Watchdog went off? Kill the target. */
3746 target_mourn_inferior ();
3747 error ("Watchdog has expired. Target detached.\n");
3750 fputs_filtered ("Timed out.\n", gdb_stdlog
);
3756 /* We've found the start of a packet, now collect the data. */
3758 val
= read_frame (buf
);
3764 fprintf_unfiltered (gdb_stdlog
, "Packet received: ");
3765 fputstr_unfiltered (buf
, 0, gdb_stdlog
);
3766 fprintf_unfiltered (gdb_stdlog
, "\n");
3768 SERIAL_WRITE (remote_desc
, "+", 1);
3772 /* Try the whole thing again. */
3774 SERIAL_WRITE (remote_desc
, "-", 1);
3777 /* We have tried hard enough, and just can't receive the packet. Give up. */
3779 printf_unfiltered ("Ignoring packet error, continuing...\n");
3780 SERIAL_WRITE (remote_desc
, "+", 1);
3786 /* For some mysterious reason, wait_for_inferior calls kill instead of
3787 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
3791 target_mourn_inferior ();
3795 /* Use catch_errors so the user can quit from gdb even when we aren't on
3796 speaking terms with the remote system. */
3797 catch_errors ((catch_errors_ftype
*) putpkt
, "k", "", RETURN_MASK_ERROR
);
3799 /* Don't wait for it to die. I'm not really sure it matters whether
3800 we do or not. For the existing stubs, kill is a noop. */
3801 target_mourn_inferior ();
3804 /* Async version of remote_kill. */
3806 remote_async_kill ()
3808 /* Unregister the file descriptor from the event loop. */
3809 if (SERIAL_IS_ASYNC_P (remote_desc
))
3810 SERIAL_ASYNC (remote_desc
, NULL
, 0);
3812 /* For some mysterious reason, wait_for_inferior calls kill instead of
3813 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
3817 target_mourn_inferior ();
3821 /* Use catch_errors so the user can quit from gdb even when we aren't on
3822 speaking terms with the remote system. */
3823 catch_errors ((catch_errors_ftype
*) putpkt
, "k", "", RETURN_MASK_ERROR
);
3825 /* Don't wait for it to die. I'm not really sure it matters whether
3826 we do or not. For the existing stubs, kill is a noop. */
3827 target_mourn_inferior ();
3833 remote_mourn_1 (&remote_ops
);
3837 remote_async_mourn ()
3839 remote_mourn_1 (&remote_async_ops
);
3843 extended_remote_mourn ()
3845 /* We do _not_ want to mourn the target like this; this will
3846 remove the extended remote target from the target stack,
3847 and the next time the user says "run" it'll fail.
3849 FIXME: What is the right thing to do here? */
3851 remote_mourn_1 (&extended_remote_ops
);
3855 /* Worker function for remote_mourn. */
3857 remote_mourn_1 (target
)
3858 struct target_ops
*target
;
3860 unpush_target (target
);
3861 generic_mourn_inferior ();
3864 /* In the extended protocol we want to be able to do things like
3865 "run" and have them basically work as expected. So we need
3866 a special create_inferior function.
3868 FIXME: One day add support for changing the exec file
3869 we're debugging, arguments and an environment. */
3872 extended_remote_create_inferior (exec_file
, args
, env
)
3877 /* Rip out the breakpoints; we'll reinsert them after restarting
3878 the remote server. */
3879 remove_breakpoints ();
3881 /* Now restart the remote server. */
3882 extended_remote_restart ();
3884 /* Now put the breakpoints back in. This way we're safe if the
3885 restart function works via a unix fork on the remote side. */
3886 insert_breakpoints ();
3888 /* Clean up from the last time we were running. */
3889 clear_proceed_status ();
3891 /* Let the remote process run. */
3892 proceed (-1, TARGET_SIGNAL_0
, 0);
3895 /* Async version of extended_remote_create_inferior. */
3897 extended_remote_async_create_inferior (exec_file
, args
, env
)
3902 /* Rip out the breakpoints; we'll reinsert them after restarting
3903 the remote server. */
3904 remove_breakpoints ();
3906 /* If running asynchronously, register the target file descriptor
3907 with the event loop. */
3908 if (event_loop_p
&& target_can_async_p ())
3909 target_async (inferior_event_handler
, 0);
3911 /* Now restart the remote server. */
3912 extended_remote_restart ();
3914 /* Now put the breakpoints back in. This way we're safe if the
3915 restart function works via a unix fork on the remote side. */
3916 insert_breakpoints ();
3918 /* Clean up from the last time we were running. */
3919 clear_proceed_status ();
3921 /* Let the remote process run. */
3922 proceed (-1, TARGET_SIGNAL_0
, 0);
3926 /* On some machines, e.g. 68k, we may use a different breakpoint instruction
3927 than other targets; in those use REMOTE_BREAKPOINT instead of just
3928 BREAKPOINT. Also, bi-endian targets may define LITTLE_REMOTE_BREAKPOINT
3929 and BIG_REMOTE_BREAKPOINT. If none of these are defined, we just call
3930 the standard routines that are in mem-break.c. */
3932 /* FIXME, these ought to be done in a more dynamic fashion. For instance,
3933 the choice of breakpoint instruction affects target program design and
3934 vice versa, and by making it user-tweakable, the special code here
3935 goes away and we need fewer special GDB configurations. */
3937 #if defined (LITTLE_REMOTE_BREAKPOINT) && defined (BIG_REMOTE_BREAKPOINT) && !defined(REMOTE_BREAKPOINT)
3938 #define REMOTE_BREAKPOINT
3941 #ifdef REMOTE_BREAKPOINT
3943 /* If the target isn't bi-endian, just pretend it is. */
3944 #if !defined (LITTLE_REMOTE_BREAKPOINT) && !defined (BIG_REMOTE_BREAKPOINT)
3945 #define LITTLE_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
3946 #define BIG_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
3949 static unsigned char big_break_insn
[] = BIG_REMOTE_BREAKPOINT
;
3950 static unsigned char little_break_insn
[] = LITTLE_REMOTE_BREAKPOINT
;
3952 #endif /* REMOTE_BREAKPOINT */
3954 /* Insert a breakpoint on targets that don't have any better breakpoint
3955 support. We read the contents of the target location and stash it,
3956 then overwrite it with a breakpoint instruction. ADDR is the target
3957 location in the target machine. CONTENTS_CACHE is a pointer to
3958 memory allocated for saving the target contents. It is guaranteed
3959 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
3960 is accomplished via BREAKPOINT_MAX). */
3963 remote_insert_breakpoint (addr
, contents_cache
)
3965 char *contents_cache
;
3967 #ifdef REMOTE_BREAKPOINT
3972 /* Try the "Z" packet if it is not already disabled.
3973 If it succeeds, then set the support to PACKET_ENABLE.
3974 If it fails, and the user has explicitly requested the Z support
3975 then report an error, otherwise, mark it disabled and go on. */
3977 if ((remote_protocol_Z
.support
== PACKET_ENABLE
)
3978 || (remote_protocol_Z
.support
== PACKET_SUPPORT_UNKNOWN
))
3980 char buf
[PBUFSIZ
], *p
= buf
;
3982 addr
= remote_address_masked (addr
);
3986 p
+= hexnumstr (p
, (ULONGEST
) addr
);
3987 BREAKPOINT_FROM_PC (&addr
, &bp_size
);
3988 sprintf (p
, ",%d", bp_size
);
3995 remote_protocol_Z
.support
= PACKET_ENABLE
;
3996 return (buf
[0] == 'E');
3999 /* The stub does not support the 'Z' request. If the user has
4000 explicitly requested the Z support, or if the stub previously
4001 said it supported the packet, this is an error,
4002 otherwise, mark it disabled. */
4004 else if (remote_protocol_Z
.support
== PACKET_ENABLE
)
4006 error ("Protocol error: Z packet not recognized by stub");
4010 remote_protocol_Z
.support
= PACKET_DISABLE
;
4014 #ifdef REMOTE_BREAKPOINT
4015 val
= target_read_memory (addr
, contents_cache
, sizeof big_break_insn
);
4019 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
4020 val
= target_write_memory (addr
, (char *) big_break_insn
,
4021 sizeof big_break_insn
);
4023 val
= target_write_memory (addr
, (char *) little_break_insn
,
4024 sizeof little_break_insn
);
4029 return memory_insert_breakpoint (addr
, contents_cache
);
4030 #endif /* REMOTE_BREAKPOINT */
4034 remote_remove_breakpoint (addr
, contents_cache
)
4036 char *contents_cache
;
4040 if ((remote_protocol_Z
.support
== PACKET_ENABLE
)
4041 || (remote_protocol_Z
.support
== PACKET_SUPPORT_UNKNOWN
))
4043 char buf
[PBUFSIZ
], *p
= buf
;
4049 addr
= remote_address_masked (addr
);
4050 p
+= hexnumstr (p
, (ULONGEST
) addr
);
4051 BREAKPOINT_FROM_PC (&addr
, &bp_size
);
4052 sprintf (p
, ",%d", bp_size
);
4057 return (buf
[0] == 'E');
4060 #ifdef REMOTE_BREAKPOINT
4061 return target_write_memory (addr
, contents_cache
, sizeof big_break_insn
);
4063 return memory_remove_breakpoint (addr
, contents_cache
);
4064 #endif /* REMOTE_BREAKPOINT */
4067 #ifdef TARGET_HAS_HARDWARE_WATCHPOINTS
4069 remote_insert_watchpoint (addr
, len
, type
)
4074 char buf
[PBUFSIZ
], *p
;
4076 if (remote_protocol_Z
.support
== PACKET_DISABLE
)
4077 error ("Can't set hardware watchpoints without the 'Z' packet\n");
4079 sprintf (buf
, "Z%x,", type
+ 2 );
4080 p
= strchr (buf
, '\0');
4081 addr
= remote_address_masked (addr
);
4082 p
+= hexnumstr (p
, (ULONGEST
) addr
);
4083 sprintf (p
, ",%x", len
);
4088 if (buf
[0] == '\0' || buf
[0] == 'E')
4095 remote_remove_watchpoint (addr
, len
, type
)
4100 char buf
[PBUFSIZ
], *p
;
4102 sprintf (buf
, "z%x,", type
+ 2 );
4103 p
= strchr (buf
, '\0');
4104 addr
= remote_address_masked (addr
);
4105 p
+= hexnumstr (p
, (ULONGEST
) addr
);
4106 sprintf (p
, ",%x", len
);
4110 if (buf
[0] == '\0' || buf
[0] == 'E')
4117 remote_insert_hw_breakpoint (addr
, len
)
4121 char buf
[PBUFSIZ
], *p
= buf
;
4123 if (remote_protocol_Z
.support
== PACKET_DISABLE
)
4124 error ("Can't set hardware breakpoints without the 'Z' packet\n");
4130 addr
= remote_address_masked (addr
);
4131 p
+= hexnumstr (p
, (ULONGEST
) addr
);
4137 if (buf
[0] == '\0' || buf
[0] == 'E')
4144 remote_remove_hw_breakpoint (addr
, len
)
4148 char buf
[PBUFSIZ
], *p
= buf
;
4154 addr
= remote_address_masked (addr
);
4155 p
+= hexnumstr (p
, (ULONGEST
) addr
);
4161 if (buf
[0] == '\0' || buf
[0] == 'E')
4168 /* Some targets are only capable of doing downloads, and afterwards
4169 they switch to the remote serial protocol. This function provides
4170 a clean way to get from the download target to the remote target.
4171 It's basically just a wrapper so that we don't have to expose any
4172 of the internal workings of remote.c.
4174 Prior to calling this routine, you should shutdown the current
4175 target code, else you will get the "A program is being debugged
4176 already..." message. Usually a call to pop_target() suffices. */
4179 push_remote_target (name
, from_tty
)
4183 printf_filtered ("Switching to remote protocol\n");
4184 remote_open (name
, from_tty
);
4187 /* Other targets want to use the entire remote serial module but with
4188 certain remote_ops overridden. */
4191 open_remote_target (name
, from_tty
, target
, extended_p
)
4194 struct target_ops
*target
;
4197 printf_filtered ("Selecting the %sremote protocol\n",
4198 (extended_p
? "extended-" : ""));
4199 remote_open_1 (name
, from_tty
, target
, extended_p
);
4202 /* Table used by the crc32 function to calcuate the checksum. */
4204 static unsigned long crc32_table
[256] =
4207 static unsigned long
4208 crc32 (buf
, len
, crc
)
4213 if (!crc32_table
[1])
4215 /* Initialize the CRC table and the decoding table. */
4219 for (i
= 0; i
< 256; i
++)
4221 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
4222 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
4229 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ *buf
) & 255];
4235 /* compare-sections command
4237 With no arguments, compares each loadable section in the exec bfd
4238 with the same memory range on the target, and reports mismatches.
4239 Useful for verifying the image on the target against the exec file.
4240 Depends on the target understanding the new "qCRC:" request. */
4243 compare_sections_command (args
, from_tty
)
4248 unsigned long host_crc
, target_crc
;
4249 extern bfd
*exec_bfd
;
4250 struct cleanup
*old_chain
;
4254 char *buf
= alloca (PBUFSIZ
);
4261 error ("command cannot be used without an exec file");
4262 if (!current_target
.to_shortname
||
4263 strcmp (current_target
.to_shortname
, "remote") != 0)
4264 error ("command can only be used with remote target");
4266 for (s
= exec_bfd
->sections
; s
; s
= s
->next
)
4268 if (!(s
->flags
& SEC_LOAD
))
4269 continue; /* skip non-loadable section */
4271 size
= bfd_get_section_size_before_reloc (s
);
4273 continue; /* skip zero-length section */
4275 sectname
= (char *) bfd_get_section_name (exec_bfd
, s
);
4276 if (args
&& strcmp (args
, sectname
) != 0)
4277 continue; /* not the section selected by user */
4279 matched
= 1; /* do this section */
4281 /* FIXME: assumes lma can fit into long */
4282 sprintf (buf
, "qCRC:%lx,%lx", (long) lma
, (long) size
);
4285 /* be clever; compute the host_crc before waiting for target reply */
4286 sectdata
= xmalloc (size
);
4287 old_chain
= make_cleanup (free
, sectdata
);
4288 bfd_get_section_contents (exec_bfd
, s
, sectdata
, 0, size
);
4289 host_crc
= crc32 ((unsigned char *) sectdata
, size
, 0xffffffff);
4293 error ("target memory fault, section %s, range 0x%08x -- 0x%08x",
4294 sectname
, lma
, lma
+ size
);
4296 error ("remote target does not support this operation");
4298 for (target_crc
= 0, tmp
= &buf
[1]; *tmp
; tmp
++)
4299 target_crc
= target_crc
* 16 + fromhex (*tmp
);
4301 printf_filtered ("Section %s, range 0x%s -- 0x%s: ",
4302 sectname
, paddr (lma
), paddr (lma
+ size
));
4303 if (host_crc
== target_crc
)
4304 printf_filtered ("matched.\n");
4307 printf_filtered ("MIS-MATCHED!\n");
4311 do_cleanups (old_chain
);
4314 warning ("One or more sections of the remote executable does not match\n\
4315 the loaded file\n");
4316 if (args
&& !matched
)
4317 printf_filtered ("No loaded section named '%s'.\n", args
);
4321 remote_query (query_type
, buf
, outbuf
, bufsiz
)
4328 char *buf2
= alloca (PBUFSIZ
);
4329 char *p2
= &buf2
[0];
4332 error ("null pointer to remote bufer size specified");
4334 /* minimum outbuf size is PBUFSIZ - if bufsiz is not large enough let
4335 the caller know and return what the minimum size is */
4336 /* Note: a zero bufsiz can be used to query the minimum buffer size */
4337 if (*bufsiz
< PBUFSIZ
)
4343 /* except for querying the minimum buffer size, target must be open */
4345 error ("remote query is only available after target open");
4347 /* we only take uppercase letters as query types, at least for now */
4348 if ((query_type
< 'A') || (query_type
> 'Z'))
4349 error ("invalid remote query type");
4352 error ("null remote query specified");
4355 error ("remote query requires a buffer to receive data");
4362 /* we used one buffer char for the remote protocol q command and another
4363 for the query type. As the remote protocol encapsulation uses 4 chars
4364 plus one extra in case we are debugging (remote_debug),
4365 we have PBUFZIZ - 7 left to pack the query string */
4367 while (buf
[i
] && (i
< (PBUFSIZ
- 8)))
4369 /* bad caller may have sent forbidden characters */
4370 if ((!isprint (buf
[i
])) || (buf
[i
] == '$') || (buf
[i
] == '#'))
4371 error ("illegal characters in query string");
4379 error ("query larger than available buffer");
4391 remote_rcmd (char *command
,
4392 struct gdb_file
*outbuf
)
4395 char *buf
= alloca (PBUFSIZ
);
4399 error ("remote rcmd is only available after target open");
4401 /* Send a NULL command across as an empty command */
4402 if (command
== NULL
)
4405 /* The query prefix */
4406 strcpy (buf
, "qRcmd,");
4407 p
= strchr (buf
, '\0');
4409 if ((strlen (buf
) + strlen (command
) * 2 + 8/*misc*/) > PBUFSIZ
)
4410 error ("\"monitor\" command ``%s'' is too long\n", command
);
4412 /* Encode the actual command */
4413 for (i
= 0; command
[i
]; i
++)
4415 *p
++ = tohex ((command
[i
] >> 4) & 0xf);
4416 *p
++ = tohex (command
[i
] & 0xf);
4420 if (putpkt (buf
) < 0)
4421 error ("Communication problem with target\n");
4423 /* get/display the response */
4426 /* XXX - see also tracepoint.c:remote_get_noisy_reply() */
4430 error ("Target does not support this command\n");
4431 if (buf
[0] == 'O' && buf
[1] != 'K')
4433 remote_console_output (buf
+ 1); /* 'O' message from stub */
4436 if (strcmp (buf
, "OK") == 0)
4438 if (strlen (buf
) == 3 && buf
[0] == 'E'
4439 && isdigit (buf
[1]) && isdigit (buf
[2]))
4441 error ("Protocol error with Rcmd");
4443 for (p
= buf
; p
[0] != '\0' && p
[1] != '\0'; p
+= 2)
4445 char c
= (fromhex (p
[0]) << 4) + fromhex (p
[1]);
4446 fputc_unfiltered (c
, outbuf
);
4453 packet_command (args
, from_tty
)
4457 char *buf
= alloca (PBUFSIZ
);
4460 error ("command can only be used with remote target");
4463 error ("remote-packet command requires packet text as argument");
4465 puts_filtered ("sending: ");
4466 print_packet (args
);
4467 puts_filtered ("\n");
4471 puts_filtered ("received: ");
4473 puts_filtered ("\n");
4477 /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------------- */
4479 static void display_thread_info
PARAMS ((struct gdb_ext_thread_info
* info
));
4481 static void threadset_test_cmd
PARAMS ((char *cmd
, int tty
));
4483 static void threadalive_test
PARAMS ((char *cmd
, int tty
));
4485 static void threadlist_test_cmd
PARAMS ((char *cmd
, int tty
));
4487 int get_and_display_threadinfo
PARAMS ((threadref
* ref
));
4489 static void threadinfo_test_cmd
PARAMS ((char *cmd
, int tty
));
4491 static int thread_display_step
PARAMS ((threadref
* ref
, void *context
));
4493 static void threadlist_update_test_cmd
PARAMS ((char *cmd
, int tty
));
4495 static void init_remote_threadtests
PARAMS ((void));
4497 #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid */
4500 threadset_test_cmd (cmd
, tty
)
4504 int sample_thread
= SAMPLE_THREAD
;
4506 printf_filtered ("Remote threadset test\n");
4507 set_thread (sample_thread
, 1);
4512 threadalive_test (cmd
, tty
)
4516 int sample_thread
= SAMPLE_THREAD
;
4518 if (remote_thread_alive (sample_thread
))
4519 printf_filtered ("PASS: Thread alive test\n");
4521 printf_filtered ("FAIL: Thread alive test\n");
4524 void output_threadid
PARAMS ((char *title
, threadref
* ref
));
4527 output_threadid (title
, ref
)
4533 pack_threadid (&hexid
[0], ref
); /* Convert threead id into hex */
4535 printf_filtered ("%s %s\n", title
, (&hexid
[0]));
4539 threadlist_test_cmd (cmd
, tty
)
4544 threadref nextthread
;
4545 int done
, result_count
;
4546 threadref threadlist
[3];
4548 printf_filtered ("Remote Threadlist test\n");
4549 if (!remote_get_threadlist (startflag
, &nextthread
, 3, &done
,
4550 &result_count
, &threadlist
[0]))
4551 printf_filtered ("FAIL: threadlist test\n");
4554 threadref
*scan
= threadlist
;
4555 threadref
*limit
= scan
+ result_count
;
4557 while (scan
< limit
)
4558 output_threadid (" thread ", scan
++);
4563 display_thread_info (info
)
4564 struct gdb_ext_thread_info
*info
;
4566 output_threadid ("Threadid: ", &info
->threadid
);
4567 printf_filtered ("Name: %s\n ", info
->shortname
);
4568 printf_filtered ("State: %s\n", info
->display
);
4569 printf_filtered ("other: %s\n\n", info
->more_display
);
4573 get_and_display_threadinfo (ref
)
4578 struct gdb_ext_thread_info threadinfo
;
4580 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
4581 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
4582 if (0 != (result
= remote_get_threadinfo (ref
, set
, &threadinfo
)))
4583 display_thread_info (&threadinfo
);
4588 threadinfo_test_cmd (cmd
, tty
)
4592 int athread
= SAMPLE_THREAD
;
4596 int_to_threadref (&thread
, athread
);
4597 printf_filtered ("Remote Threadinfo test\n");
4598 if (!get_and_display_threadinfo (&thread
))
4599 printf_filtered ("FAIL cannot get thread info\n");
4603 thread_display_step (ref
, context
)
4607 /* output_threadid(" threadstep ",ref); *//* simple test */
4608 return get_and_display_threadinfo (ref
);
4612 threadlist_update_test_cmd (cmd
, tty
)
4616 printf_filtered ("Remote Threadlist update test\n");
4617 remote_threadlist_iterator (thread_display_step
, 0, CRAZY_MAX_THREADS
);
4621 init_remote_threadtests (void)
4623 add_com ("tlist", class_obscure
, threadlist_test_cmd
,
4624 "Fetch and print the remote list of thread identifiers, one pkt only");
4625 add_com ("tinfo", class_obscure
, threadinfo_test_cmd
,
4626 "Fetch and display info about one thread");
4627 add_com ("tset", class_obscure
, threadset_test_cmd
,
4628 "Test setting to a different thread");
4629 add_com ("tupd", class_obscure
, threadlist_update_test_cmd
,
4630 "Iterate through updating all remote thread info");
4631 add_com ("talive", class_obscure
, threadalive_test
,
4632 " Remote thread alive test ");
4640 remote_ops
.to_shortname
= "remote";
4641 remote_ops
.to_longname
= "Remote serial target in gdb-specific protocol";
4643 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
4644 Specify the serial device it is connected to (e.g. /dev/ttya).";
4645 remote_ops
.to_open
= remote_open
;
4646 remote_ops
.to_close
= remote_close
;
4647 remote_ops
.to_detach
= remote_detach
;
4648 remote_ops
.to_resume
= remote_resume
;
4649 remote_ops
.to_wait
= remote_wait
;
4650 remote_ops
.to_fetch_registers
= remote_fetch_registers
;
4651 remote_ops
.to_store_registers
= remote_store_registers
;
4652 remote_ops
.to_prepare_to_store
= remote_prepare_to_store
;
4653 remote_ops
.to_xfer_memory
= remote_xfer_memory
;
4654 remote_ops
.to_files_info
= remote_files_info
;
4655 remote_ops
.to_insert_breakpoint
= remote_insert_breakpoint
;
4656 remote_ops
.to_remove_breakpoint
= remote_remove_breakpoint
;
4657 remote_ops
.to_kill
= remote_kill
;
4658 remote_ops
.to_load
= generic_load
;
4659 remote_ops
.to_mourn_inferior
= remote_mourn
;
4660 remote_ops
.to_thread_alive
= remote_thread_alive
;
4661 remote_ops
.to_find_new_threads
= remote_threads_info
;
4662 remote_ops
.to_stop
= remote_stop
;
4663 remote_ops
.to_query
= remote_query
;
4664 remote_ops
.to_rcmd
= remote_rcmd
;
4665 remote_ops
.to_stratum
= process_stratum
;
4666 remote_ops
.to_has_all_memory
= 1;
4667 remote_ops
.to_has_memory
= 1;
4668 remote_ops
.to_has_stack
= 1;
4669 remote_ops
.to_has_registers
= 1;
4670 remote_ops
.to_has_execution
= 1;
4671 remote_ops
.to_has_thread_control
= tc_schedlock
; /* can lock scheduler */
4672 remote_ops
.to_magic
= OPS_MAGIC
;
4675 /* Set up the extended remote vector by making a copy of the standard
4676 remote vector and adding to it. */
4679 init_extended_remote_ops ()
4681 extended_remote_ops
= remote_ops
;
4683 extended_remote_ops
.to_shortname
= "extended-remote";
4684 extended_remote_ops
.to_longname
=
4685 "Extended remote serial target in gdb-specific protocol";
4686 extended_remote_ops
.to_doc
=
4687 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
4688 Specify the serial device it is connected to (e.g. /dev/ttya).",
4689 extended_remote_ops
.to_open
= extended_remote_open
;
4690 extended_remote_ops
.to_create_inferior
= extended_remote_create_inferior
;
4691 extended_remote_ops
.to_mourn_inferior
= extended_remote_mourn
;
4695 * Command: info remote-process
4697 * This implements Cisco's version of the "info proc" command.
4699 * This query allows the target stub to return an arbitrary string
4700 * (or strings) giving arbitrary information about the target process.
4701 * This is optional; the target stub isn't required to implement it.
4703 * Syntax: qfProcessInfo request first string
4704 * qsProcessInfo request subsequent string
4705 * reply: 'O'<hex-encoded-string>
4706 * 'l' last reply (empty)
4710 remote_info_process (args
, from_tty
)
4714 char *buf
= alloca (PBUFSIZ
);
4716 if (remote_desc
== 0)
4717 error ("Command can only be used when connected to the remote target.");
4719 putpkt ("qfProcessInfo");
4722 return; /* Silently: target does not support this feature. */
4725 error ("info proc: target error.");
4727 while (buf
[0] == 'O') /* Capitol-O packet */
4729 remote_console_output (&buf
[1]);
4730 putpkt ("qsProcessInfo");
4740 remote_cisco_open (name
, from_tty
)
4746 "To open a remote debug connection, you need to specify what \n\
4747 device is attached to the remote system (e.g. host:port).");
4749 /* See FIXME above */
4750 wait_forever_enabled_p
= 1;
4752 target_preopen (from_tty
);
4754 unpush_target (&remote_cisco_ops
);
4756 remote_dcache
= dcache_init (remote_read_bytes
, remote_write_bytes
);
4758 remote_desc
= SERIAL_OPEN (name
);
4760 perror_with_name (name
);
4763 * If a baud rate was specified on the gdb command line it will
4764 * be greater than the initial value of -1. If it is, use it otherwise
4768 baud_rate
= (baud_rate
> 0) ? baud_rate
: 9600;
4769 if (SERIAL_SETBAUDRATE (remote_desc
, baud_rate
))
4771 SERIAL_CLOSE (remote_desc
);
4772 perror_with_name (name
);
4775 SERIAL_RAW (remote_desc
);
4777 /* If there is something sitting in the buffer we might take it as a
4778 response to a command, which would be bad. */
4779 SERIAL_FLUSH_INPUT (remote_desc
);
4783 puts_filtered ("Remote debugging using ");
4784 puts_filtered (name
);
4785 puts_filtered ("\n");
4788 remote_cisco_mode
= 1;
4790 push_target (&remote_cisco_ops
); /* Switch to using cisco target now */
4792 init_packet_config (&remote_protocol_P
);
4793 init_packet_config (&remote_protocol_Z
);
4795 general_thread
= -2;
4796 continue_thread
= -2;
4798 /* Force remote_write_bytes to check whether target supports
4799 binary downloading. */
4800 init_packet_config (&remote_protocol_binary_download
);
4802 /* Without this, some commands which require an active target (such
4803 as kill) won't work. This variable serves (at least) double duty
4804 as both the pid of the target process (if it has such), and as a
4805 flag indicating that a target is active. These functions should
4806 be split out into seperate variables, especially since GDB will
4807 someday have a notion of debugging several processes. */
4808 inferior_pid
= MAGIC_NULL_PID
;
4810 /* Start the remote connection; if error (0), discard this target. */
4812 if (!catch_errors (remote_start_remote_dummy
, (char *) 0,
4813 "Couldn't establish connection to remote target\n",
4822 remote_cisco_close (quitting
)
4825 remote_cisco_mode
= 0;
4826 remote_close (quitting
);
4833 remote_mourn_1 (&remote_cisco_ops
);
4845 /* shared between readsocket() and readtty() */
4846 static char *tty_input
;
4848 static int escape_count
;
4849 static int echo_check
;
4850 extern int quit_flag
;
4857 /* Loop until the socket doesn't have any more data */
4859 while ((data
= readchar (0)) >= 0)
4861 /* Check for the escape sequence */
4864 /* If this is the fourth escape, get out */
4865 if (++escape_count
== 4)
4870 { /* This is a '|', but not the fourth in a row.
4871 Continue without echoing it. If it isn't actually
4872 one of four in a row, it'll be echoed later. */
4879 /* Ensure any pending '|'s are flushed. */
4881 for (; escape_count
> 0; escape_count
--)
4885 if (data
== '\r') /* If this is a return character, */
4886 continue; /* - just supress it. */
4888 if (echo_check
!= -1) /* Check for echo of user input. */
4890 if (tty_input
[echo_check
] == data
)
4892 echo_check
++; /* Character matched user input: */
4893 continue; /* Continue without echoing it. */
4895 else if ((data
== '\n') && (tty_input
[echo_check
] == '\r'))
4896 { /* End of the line (and of echo checking). */
4897 echo_check
= -1; /* No more echo supression */
4898 continue; /* Continue without echoing. */
4901 { /* Failed check for echo of user input.
4902 We now have some suppressed output to flush! */
4905 for (j
= 0; j
< echo_check
; j
++)
4906 putchar (tty_input
[j
]);
4910 putchar (data
); /* Default case: output the char. */
4913 if (data
== SERIAL_TIMEOUT
) /* Timeout returned from readchar. */
4914 return READ_MORE
; /* Try to read some more */
4916 return FATAL_ERROR
; /* Trouble, bail out */
4924 /* First, read a buffer full from the terminal */
4925 tty_bytecount
= read (fileno (stdin
), tty_input
, sizeof (tty_input
) - 1);
4926 if (tty_bytecount
== -1)
4928 perror ("readtty: read failed");
4932 /* Remove a quoted newline. */
4933 if (tty_input
[tty_bytecount
- 1] == '\n' &&
4934 tty_input
[tty_bytecount
- 2] == '\\') /* line ending in backslash */
4936 tty_input
[--tty_bytecount
] = 0; /* remove newline */
4937 tty_input
[--tty_bytecount
] = 0; /* remove backslash */
4940 /* Turn trailing newlines into returns */
4941 if (tty_input
[tty_bytecount
- 1] == '\n')
4942 tty_input
[tty_bytecount
- 1] = '\r';
4944 /* If the line consists of a ~, enter debugging mode. */
4945 if ((tty_input
[0] == '~') && (tty_bytecount
== 2))
4948 /* Make this a zero terminated string and write it out */
4949 tty_input
[tty_bytecount
] = 0;
4950 if (SERIAL_WRITE (remote_desc
, tty_input
, tty_bytecount
))
4952 perror_with_name ("readtty: write failed");
4962 fd_set input
; /* file descriptors for select */
4963 int tablesize
; /* max number of FDs for select */
4967 extern int escape_count
; /* global shared by readsocket */
4968 extern int echo_check
; /* ditto */
4973 tablesize
= 8 * sizeof (input
);
4977 /* Check for anything from our socket - doesn't block. Note that
4978 this must be done *before* the select as there may be
4979 buffered I/O waiting to be processed. */
4981 if ((status
= readsocket ()) == FATAL_ERROR
)
4983 error ("Debugging terminated by communications error");
4985 else if (status
!= READ_MORE
)
4990 fflush (stdout
); /* Flush output before blocking */
4992 /* Now block on more socket input or TTY input */
4995 FD_SET (fileno (stdin
), &input
);
4996 FD_SET (DEPRECATED_SERIAL_FD (remote_desc
), &input
);
4998 status
= select (tablesize
, &input
, 0, 0, 0);
4999 if ((status
== -1) && (errno
!= EINTR
))
5001 error ("Communications error on select %d", errno
);
5004 /* Handle Control-C typed */
5008 if ((++quit_count
) == 2)
5010 if (query ("Interrupt GDB? "))
5012 printf_filtered ("Interrupted by user.\n");
5013 return_to_top_level (RETURN_QUIT
);
5020 SERIAL_SEND_BREAK (remote_desc
);
5022 SERIAL_WRITE (remote_desc
, "\003", 1);
5027 /* Handle console input */
5029 if (FD_ISSET (fileno (stdin
), &input
))
5033 status
= readtty ();
5034 if (status
== READ_MORE
)
5037 return status
; /* telnet session ended */
5043 remote_cisco_wait (pid
, status
)
5045 struct target_waitstatus
*status
;
5047 if (minitelnet () != ENTER_DEBUG
)
5049 error ("Debugging session terminated by protocol error");
5052 return remote_wait (pid
, status
);
5056 init_remote_cisco_ops ()
5058 remote_cisco_ops
.to_shortname
= "cisco";
5059 remote_cisco_ops
.to_longname
= "Remote serial target in cisco-specific protocol";
5060 remote_cisco_ops
.to_doc
=
5061 "Use a remote machine via TCP, using a cisco-specific protocol.\n\
5062 Specify the serial device it is connected to (e.g. host:2020).";
5063 remote_cisco_ops
.to_open
= remote_cisco_open
;
5064 remote_cisco_ops
.to_close
= remote_cisco_close
;
5065 remote_cisco_ops
.to_detach
= remote_detach
;
5066 remote_cisco_ops
.to_resume
= remote_resume
;
5067 remote_cisco_ops
.to_wait
= remote_cisco_wait
;
5068 remote_cisco_ops
.to_fetch_registers
= remote_fetch_registers
;
5069 remote_cisco_ops
.to_store_registers
= remote_store_registers
;
5070 remote_cisco_ops
.to_prepare_to_store
= remote_prepare_to_store
;
5071 remote_cisco_ops
.to_xfer_memory
= remote_xfer_memory
;
5072 remote_cisco_ops
.to_files_info
= remote_files_info
;
5073 remote_cisco_ops
.to_insert_breakpoint
= remote_insert_breakpoint
;
5074 remote_cisco_ops
.to_remove_breakpoint
= remote_remove_breakpoint
;
5075 remote_cisco_ops
.to_kill
= remote_kill
;
5076 remote_cisco_ops
.to_load
= generic_load
;
5077 remote_cisco_ops
.to_mourn_inferior
= remote_cisco_mourn
;
5078 remote_cisco_ops
.to_thread_alive
= remote_thread_alive
;
5079 remote_cisco_ops
.to_find_new_threads
= remote_threads_info
;
5080 remote_cisco_ops
.to_stratum
= process_stratum
;
5081 remote_cisco_ops
.to_has_all_memory
= 1;
5082 remote_cisco_ops
.to_has_memory
= 1;
5083 remote_cisco_ops
.to_has_stack
= 1;
5084 remote_cisco_ops
.to_has_registers
= 1;
5085 remote_cisco_ops
.to_has_execution
= 1;
5086 remote_cisco_ops
.to_magic
= OPS_MAGIC
;
5090 remote_can_async_p (void)
5092 /* We're async whenever the serial device is. */
5093 return SERIAL_CAN_ASYNC_P (remote_desc
);
5097 remote_is_async_p (void)
5099 /* We're async whenever the serial device is. */
5100 return SERIAL_IS_ASYNC_P (remote_desc
);
5103 /* Pass the SERIAL event on and up to the client. One day this code
5104 will be able to delay notifying the client of an event until the
5105 point where an entire packet has been received. */
5107 static void (*async_client_callback
) (enum inferior_event_type event_type
, void *context
);
5108 static void *async_client_context
;
5109 static serial_event_ftype remote_async_serial_handler
;
5112 remote_async_serial_handler (serial_t scb
, void *context
)
5114 /* Don't propogate error information up to the client. Instead let
5115 the client find out about the error by querying the target. */
5116 async_client_callback (INF_REG_EVENT
, async_client_context
);
5120 remote_async (void (*callback
) (enum inferior_event_type event_type
, void *context
), void *context
)
5122 if (callback
!= NULL
)
5124 SERIAL_ASYNC (remote_desc
, remote_async_serial_handler
, NULL
);
5125 async_client_callback
= callback
;
5126 async_client_context
= context
;
5129 SERIAL_ASYNC (remote_desc
, NULL
, NULL
);
5132 /* Target async and target extended-async.
5134 This are temporary targets, until it is all tested. Eventually
5135 async support will be incorporated int the usual 'remote'
5139 init_remote_async_ops ()
5141 remote_async_ops
.to_shortname
= "async";
5142 remote_async_ops
.to_longname
= "Remote serial target in async version of the gdb-specific protocol";
5143 remote_async_ops
.to_doc
=
5144 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
5145 Specify the serial device it is connected to (e.g. /dev/ttya).";
5146 remote_async_ops
.to_open
= remote_async_open
;
5147 remote_async_ops
.to_close
= remote_close
;
5148 remote_async_ops
.to_detach
= remote_async_detach
;
5149 remote_async_ops
.to_resume
= remote_async_resume
;
5150 remote_async_ops
.to_wait
= remote_async_wait
;
5151 remote_async_ops
.to_fetch_registers
= remote_fetch_registers
;
5152 remote_async_ops
.to_store_registers
= remote_store_registers
;
5153 remote_async_ops
.to_prepare_to_store
= remote_prepare_to_store
;
5154 remote_async_ops
.to_xfer_memory
= remote_xfer_memory
;
5155 remote_async_ops
.to_files_info
= remote_files_info
;
5156 remote_async_ops
.to_insert_breakpoint
= remote_insert_breakpoint
;
5157 remote_async_ops
.to_remove_breakpoint
= remote_remove_breakpoint
;
5158 remote_async_ops
.to_terminal_inferior
= remote_async_terminal_inferior
;
5159 remote_async_ops
.to_terminal_ours
= remote_async_terminal_ours
;
5160 remote_async_ops
.to_kill
= remote_async_kill
;
5161 remote_async_ops
.to_load
= generic_load
;
5162 remote_async_ops
.to_mourn_inferior
= remote_async_mourn
;
5163 remote_async_ops
.to_thread_alive
= remote_thread_alive
;
5164 remote_async_ops
.to_find_new_threads
= remote_threads_info
;
5165 remote_async_ops
.to_stop
= remote_stop
;
5166 remote_async_ops
.to_query
= remote_query
;
5167 remote_async_ops
.to_rcmd
= remote_rcmd
;
5168 remote_async_ops
.to_stratum
= process_stratum
;
5169 remote_async_ops
.to_has_all_memory
= 1;
5170 remote_async_ops
.to_has_memory
= 1;
5171 remote_async_ops
.to_has_stack
= 1;
5172 remote_async_ops
.to_has_registers
= 1;
5173 remote_async_ops
.to_has_execution
= 1;
5174 remote_async_ops
.to_has_thread_control
= tc_schedlock
; /* can lock scheduler */
5175 remote_async_ops
.to_can_async_p
= remote_can_async_p
;
5176 remote_async_ops
.to_is_async_p
= remote_is_async_p
;
5177 remote_async_ops
.to_async
= remote_async
;
5178 remote_async_ops
.to_magic
= OPS_MAGIC
;
5181 /* Set up the async extended remote vector by making a copy of the standard
5182 remote vector and adding to it. */
5185 init_extended_async_remote_ops ()
5187 extended_async_remote_ops
= remote_async_ops
;
5189 extended_async_remote_ops
.to_shortname
= "extended-async";
5190 extended_async_remote_ops
.to_longname
=
5191 "Extended remote serial target in async gdb-specific protocol";
5192 extended_async_remote_ops
.to_doc
=
5193 "Use a remote computer via a serial line, using an async gdb-specific protocol.\n\
5194 Specify the serial device it is connected to (e.g. /dev/ttya).",
5195 extended_async_remote_ops
.to_open
= extended_remote_async_open
;
5196 extended_async_remote_ops
.to_create_inferior
= extended_remote_async_create_inferior
;
5197 extended_async_remote_ops
.to_mourn_inferior
= extended_remote_mourn
;
5201 set_remote_cmd (args
, from_tty
)
5210 build_remote_gdbarch_data ()
5212 tty_input
= xmalloc (PBUFSIZ
);
5216 _initialize_remote ()
5218 static struct cmd_list_element
*remote_set_cmdlist
;
5219 static struct cmd_list_element
*remote_show_cmdlist
;
5221 /* architecture specific data */
5222 build_remote_gdbarch_data ();
5223 register_gdbarch_swap (&tty_input
, sizeof (&tty_input
), NULL
);
5224 register_gdbarch_swap (NULL
, 0, build_remote_gdbarch_data
);
5226 /* runtime constants - we retain the value of remote_write_size
5227 across architecture swaps. */
5228 remote_write_size
= PBUFSIZ
;
5231 add_target (&remote_ops
);
5233 init_extended_remote_ops ();
5234 add_target (&extended_remote_ops
);
5236 init_remote_async_ops ();
5237 add_target (&remote_async_ops
);
5239 init_extended_async_remote_ops ();
5240 add_target (&extended_async_remote_ops
);
5242 init_remote_cisco_ops ();
5243 add_target (&remote_cisco_ops
);
5246 init_remote_threadtests ();
5249 add_prefix_cmd ("remote", class_maintenance
, set_remote_cmd
, "\
5250 Remote protocol specific variables\n\
5251 Configure various remote-protocol specific variables such as\n\
5252 the packets being used",
5253 &remote_set_cmdlist
, "set remote ",
5254 0/*allow-unknown*/, &setlist
);
5255 add_prefix_cmd ("remote", class_maintenance
, set_remote_cmd
, "\
5256 Remote protocol specific variables\n\
5257 Configure various remote-protocol specific variables such as\n\
5258 the packets being used",
5259 &remote_show_cmdlist
, "show remote ",
5260 0/*allow-unknown*/, &showlist
);
5262 add_cmd ("compare-sections", class_obscure
, compare_sections_command
,
5263 "Compare section data on target to the exec file.\n\
5264 Argument is a single section name (default: all loaded sections).",
5267 add_cmd ("packet", class_maintenance
, packet_command
,
5268 "Send an arbitrary packet to a remote target.\n\
5269 maintenance packet TEXT\n\
5270 If GDB is talking to an inferior via the GDB serial protocol, then\n\
5271 this command sends the string TEXT to the inferior, and displays the\n\
5272 response packet. GDB supplies the initial `$' character, and the\n\
5273 terminating `#' character and checksum.",
5277 (add_set_cmd ("remotetimeout", no_class
,
5278 var_integer
, (char *) &remote_timeout
,
5279 "Set timeout value for remote read.\n",
5284 (add_set_cmd ("remotebreak", no_class
,
5285 var_boolean
, (char *) &remote_break
,
5286 "Set whether to send break if interrupted.\n",
5291 (add_set_cmd ("remotewritesize", no_class
,
5292 var_integer
, (char *) &remote_write_size
,
5293 "Set the maximum number of bytes per memory write packet.\n",
5297 remote_address_size
= TARGET_PTR_BIT
;
5299 (add_set_cmd ("remoteaddresssize", class_obscure
,
5300 var_integer
, (char *) &remote_address_size
,
5301 "Set the maximum size of the address (in bits) \
5302 in a memory packet.\n",
5306 add_packet_config_cmd (&remote_protocol_binary_download
,
5307 "X", "binary-download",
5308 set_remote_protocol_binary_download_cmd
,
5309 show_remote_protocol_binary_download_cmd
,
5310 &remote_set_cmdlist
, &remote_show_cmdlist
);
5312 /* XXXX - should ``set remotebinarydownload'' be retained for
5315 (add_set_cmd ("remotebinarydownload", no_class
,
5316 var_boolean
, (char *) &remote_binary_download
,
5317 "Set binary downloads.\n", &setlist
),
5321 add_info ("remote-process", remote_info_process
,
5322 "Query the remote system for process info.");
5324 add_packet_config_cmd (&remote_protocol_P
, "P", "set-register",
5325 set_remote_protocol_P_packet_cmd
,
5326 show_remote_protocol_P_packet_cmd
,
5327 &remote_set_cmdlist
, &remote_show_cmdlist
);
5329 add_packet_config_cmd (&remote_protocol_Z
, "Z", "breakpoint",
5330 set_remote_protocol_Z_packet_cmd
,
5331 show_remote_protocol_Z_packet_cmd
,
5332 &remote_set_cmdlist
, &remote_show_cmdlist
);