1 /* Remote target communications for serial-line targets in custom GDB protocol
2 Copyright 1988, 91, 92, 93, 94, 95, 96, 97, 1998
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, Boston, MA 02111-1307, USA. */
21 /* Remote communication protocol.
23 A debug packet whose contents are <data>
24 is encapsulated for transmission in the form:
26 $ <data> # CSUM1 CSUM2
28 <data> must be ASCII alphanumeric and cannot include characters
29 '$' or '#'. If <data> starts with two characters followed by
30 ':', then the existing stubs interpret this as a sequence number.
32 CSUM1 and CSUM2 are ascii hex representation of an 8-bit
33 checksum of <data>, the most significant nibble is sent first.
34 the hex digits 0-9,a-f are used.
36 Receiver responds with:
38 + - if CSUM is correct and ready for next packet
39 - - if CSUM is incorrect
42 Most values are encoded in ascii hex digits. Signal numbers are according
43 to the numbering in target.h.
47 set thread Hct... Set thread for subsequent operations.
48 c = 'c' for thread used in step and
49 continue; t... can be -1 for all
51 c = 'g' for thread used in other
52 operations. If zero, pick a thread,
58 reply XX....X Each byte of register data
59 is described by two hex digits.
60 Registers are in the internal order
61 for GDB, and the bytes in a register
62 are in the same order the machine uses.
65 write regs GXX..XX Each byte of register data
66 is described by two hex digits.
70 write reg Pn...=r... Write register n... with value r...,
71 which contains two hex digits for each
72 byte in the register (target byte
76 (not supported by all stubs).
78 read mem mAA..AA,LLLL AA..AA is address, LLLL is length.
79 reply XX..XX XX..XX is mem contents
80 Can be fewer bytes than requested
81 if able to read only part of the data.
84 write mem MAA..AA,LLLL:XX..XX
86 LLLL is number of bytes,
89 ENN for an error (this includes the case
90 where only part of the data was
93 write mem XAA..AA,LLLL:XX..XX
94 (binary) AA..AA is address,
95 LLLL is number of bytes,
100 continue cAA..AA AA..AA is address to resume
101 If AA..AA is omitted,
102 resume at same address.
104 step sAA..AA AA..AA is address to resume
105 If AA..AA is omitted,
106 resume at same address.
108 continue with Csig;AA..AA Continue with signal sig (hex signal
109 signal number). If ;AA..AA is omitted,
110 resume at same address.
112 step with Ssig;AA..AA Like 'C' but step not continue.
115 last signal ? Reply the current reason for stopping.
116 This is the same reply as is generated
117 for step or cont : SAA where AA is the
122 There is no immediate reply to step or cont.
123 The reply comes when the machine stops.
124 It is SAA AA is the signal number.
126 or... TAAn...:r...;n...:r...;n...:r...;
128 n... = register number (hex)
129 r... = register contents
131 r... = thread process ID. This is
133 n... = other string not starting
134 with valid hex digit.
135 gdb should ignore this n,r pair
136 and go on to the next. This way
137 we can extend the protocol.
138 or... WAA The process exited, and AA is
139 the exit status. This is only
140 applicable for certains sorts of
142 or... XAA The process terminated with signal
144 or (obsolete) NAA;tttttttt;dddddddd;bbbbbbbb
146 tttttttt = address of symbol "_start"
147 dddddddd = base of data section
148 bbbbbbbb = base of bss section.
149 Note: only used by Cisco Systems
150 targets. The difference between this
151 reply and the "qOffsets" query is that
152 the 'N' packet may arrive spontaneously
153 whereas the 'qOffsets' is a query
154 initiated by the host debugger.
155 or... OXX..XX XX..XX is hex encoding of ASCII data. This
156 can happen at any time while the
157 program is running and the debugger
158 should continue to wait for
161 thread alive TXX Find out if the thread XX is alive.
162 reply OK thread is still alive
165 remote restart RXX Restart the remote server
167 extended ops ! Use the extended remote protocol.
168 Sticky -- only needs to be set once.
172 toggle debug d toggle debug flag (see 386 & 68k stubs)
173 reset r reset -- see sparc stub.
174 reserved <other> On other requests, the stub should
175 ignore the request and send an empty
176 response ($#<checksum>). This way
177 we can extend the protocol and GDB
178 can tell whether the stub it is
179 talking to uses the old or the new.
180 search tAA:PP,MM Search backwards starting at address
181 AA for a match with pattern PP and
182 mask MM. PP and MM are 4 bytes.
183 Not supported by all stubs.
185 general query qXXXX Request info about XXXX.
186 general set QXXXX=yyyy Set value of XXXX to yyyy.
187 query sect offs qOffsets Get section offsets. Reply is
188 Text=xxx;Data=yyy;Bss=zzz
190 Responses can be run-length encoded to save space. A '*' means that
191 the next character is an ASCII encoding giving a repeat count which
192 stands for that many repititions of the character preceding the '*'.
193 The encoding is n+29, yielding a printable character where n >=3
194 (which is where rle starts to win). Don't use an n > 126.
197 "0* " means the same as "0000". */
200 #include "gdb_string.h"
204 #include "inferior.h"
209 /*#include "terminal.h"*/
211 #include "objfiles.h"
212 #include "gdb-stabs.h"
213 #include "gdbthread.h"
219 #include <sys/types.h>
225 /* Prototypes for local functions */
227 static void build_remote_gdbarch_data
PARAMS ((void));
229 static int remote_write_bytes
PARAMS ((CORE_ADDR memaddr
,
230 char *myaddr
, int len
));
232 static int remote_read_bytes
PARAMS ((CORE_ADDR memaddr
,
233 char *myaddr
, int len
));
235 static void remote_files_info
PARAMS ((struct target_ops
*ignore
));
237 static int remote_xfer_memory
PARAMS ((CORE_ADDR memaddr
, char * myaddr
,
238 int len
, int should_write
,
239 struct target_ops
* target
));
241 static void remote_prepare_to_store
PARAMS ((void));
243 static void remote_fetch_registers
PARAMS ((int regno
));
245 static void remote_resume
PARAMS ((int pid
, int step
,
246 enum target_signal siggnal
));
248 static int remote_start_remote
PARAMS ((PTR
));
250 static void remote_open
PARAMS ((char *name
, int from_tty
));
252 static void extended_remote_open
PARAMS ((char *name
, int from_tty
));
254 static void remote_open_1
PARAMS ((char *, int, struct target_ops
*,
257 static void remote_close
PARAMS ((int quitting
));
259 static void remote_store_registers
PARAMS ((int regno
));
261 static void remote_mourn
PARAMS ((void));
263 static void extended_remote_restart
PARAMS ((void));
265 static void extended_remote_mourn
PARAMS ((void));
267 static void extended_remote_create_inferior
PARAMS ((char *, char *, char **));
269 static void remote_mourn_1
PARAMS ((struct target_ops
*));
271 static void remote_send
PARAMS ((char *buf
));
273 static int readchar
PARAMS ((int timeout
));
275 static int remote_wait
PARAMS ((int pid
, struct target_waitstatus
* status
));
277 static void remote_kill
PARAMS ((void));
279 static int tohex
PARAMS ((int nib
));
281 static void remote_detach
PARAMS ((char *args
, int from_tty
));
283 static void remote_interrupt
PARAMS ((int signo
));
285 static void remote_interrupt_twice
PARAMS ((int signo
));
287 static void interrupt_query
PARAMS ((void));
289 static void set_thread
PARAMS ((int, int));
291 static int remote_thread_alive
PARAMS ((int));
293 static void get_offsets
PARAMS ((void));
295 static int read_frame
PARAMS ((char *));
297 static int remote_insert_breakpoint
PARAMS ((CORE_ADDR
, char *));
299 static int remote_remove_breakpoint
PARAMS ((CORE_ADDR
, char *));
301 static int hexnumlen
PARAMS ((ULONGEST num
));
303 static void init_remote_ops
PARAMS ((void));
305 static void init_extended_remote_ops
PARAMS ((void));
307 static void init_remote_cisco_ops
PARAMS ((void));
309 static struct target_ops remote_cisco_ops
;
311 static void remote_stop
PARAMS ((void));
313 static int ishex
PARAMS ((int ch
, int *val
));
315 static int stubhex
PARAMS ((int ch
));
317 static int remote_query
PARAMS ((int/*char*/, char *, char *, int *));
319 static int hexnumstr
PARAMS ((char *, ULONGEST
));
321 static CORE_ADDR remote_address_masked
PARAMS ((CORE_ADDR
));
323 static void print_packet
PARAMS ((char *));
325 static unsigned long crc32
PARAMS ((unsigned char *, int, unsigned int));
327 static void compare_sections_command
PARAMS ((char *, int));
329 static void packet_command
PARAMS ((char *, int));
331 static int stub_unpack_int
PARAMS ((char *buff
, int fieldlength
));
333 static int remote_current_thread
PARAMS ((int oldpid
));
335 static void remote_find_new_threads
PARAMS ((void));
337 static void record_currthread
PARAMS ((int currthread
));
339 /* exported functions */
341 extern int fromhex
PARAMS ((int a
));
343 extern void getpkt
PARAMS ((char *buf
, int forever
));
345 extern int putpkt
PARAMS ((char *buf
));
347 static int putpkt_binary
PARAMS ((char *buf
, int cnt
));
349 void remote_console_output
PARAMS ((char *));
351 static void check_binary_download
PARAMS ((CORE_ADDR addr
));
353 /* Define the target subroutine names */
355 void open_remote_target
PARAMS ((char *, int, struct target_ops
*, int));
357 void _initialize_remote
PARAMS ((void));
361 static struct target_ops remote_ops
;
363 static struct target_ops extended_remote_ops
;
365 /* This was 5 seconds, which is a long time to sit and wait.
366 Unless this is going though some terminal server or multiplexer or
367 other form of hairy serial connection, I would think 2 seconds would
370 /* Changed to allow option to set timeout value.
371 was static int remote_timeout = 2; */
372 extern int remote_timeout
;
374 /* This variable chooses whether to send a ^C or a break when the user
375 requests program interruption. Although ^C is usually what remote
376 systems expect, and that is the default here, sometimes a break is
377 preferable instead. */
379 static int remote_break
;
381 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
382 remote_open knows that we don't have a file open when the program
384 static serial_t remote_desc
= NULL
;
386 /* This is set by the target (thru the 'S' message)
387 to denote that the target is in kernel mode. */
388 static int cisco_kernel_mode
= 0;
390 /* This variable (available to the user via "set remotebinarydownload")
391 dictates whether downloads are sent in binary (via the 'X' packet).
392 We assume that the stub can, and attempt to do it. This will be cleared if
393 the stub does not understand it. This switch is still needed, though
394 in cases when the packet is supported in the stub, but the connection
395 does not allow it (i.e., 7-bit serial connection only). */
396 static int remote_binary_download
= 1;
398 /* Have we already checked whether binary downloads work? */
399 static int remote_binary_checked
;
401 /* Maximum number of bytes to read/write at once. The value here
402 is chosen to fill up a packet (the headers account for the 32). */
403 #define MAXBUFBYTES(N) (((N)-32)/2)
405 /* Having this larger than 400 causes us to be incompatible with m68k-stub.c
406 and i386-stub.c. Normally, no one would notice because it only matters
407 for writing large chunks of memory (e.g. in downloads). Also, this needs
408 to be more than 400 if required to hold the registers (see below, where
409 we round it up based on REGISTER_BYTES). */
410 /* Round up PBUFSIZ to hold all the registers, at least. */
411 #define PBUFSIZ ((REGISTER_BYTES > MAXBUFBYTES (400)) \
412 ? (REGISTER_BYTES * 2 + 32) \
416 /* This variable sets the number of bytes to be written to the target
417 in a single packet. Normally PBUFSIZ is satisfactory, but some
418 targets need smaller values (perhaps because the receiving end
421 static int remote_write_size
;
423 /* This variable sets the number of bits in an address that are to be
424 sent in a memory ("M" or "m") packet. Normally, after stripping
425 leading zeros, the entire address would be sent. This variable
426 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
427 initial implementation of remote.c restricted the address sent in
428 memory packets to ``host::sizeof long'' bytes - (typically 32
429 bits). Consequently, for 64 bit targets, the upper 32 bits of an
430 address was never sent. Since fixing this bug may cause a break in
431 some remote targets this variable is principly provided to
432 facilitate backward compatibility. */
434 static int remote_address_size
;
436 /* This is the size (in chars) of the first response to the `g' command. This
437 is used to limit the size of the memory read and write commands to prevent
438 stub buffers from overflowing. The size does not include headers and
439 trailers, it is only the payload size. */
441 static int remote_register_buf_size
= 0;
443 /* Should we try the 'P' request? If this is set to one when the stub
444 doesn't support 'P', the only consequence is some unnecessary traffic. */
445 static int stub_supports_P
= 1;
447 /* These are pointers to hook functions that may be set in order to
448 modify resume/wait behavior for a particular architecture. */
450 void (*target_resume_hook
) PARAMS ((void));
451 void (*target_wait_loop_hook
) PARAMS ((void));
455 /* These are the threads which we last sent to the remote system.
456 -1 for all or -2 for not sent yet. */
457 static int general_thread
;
458 static int continue_thread
;
460 /* Call this function as a result of
461 1) A halt indication (T packet) containing a thread id
462 2) A direct query of currthread
463 3) Successful execution of set thread
467 record_currthread (currthread
)
470 general_thread
= currthread
;
472 /* If this is a new thread, add it to GDB's thread list.
473 If we leave it up to WFI to do this, bad things will happen. */
474 if (!in_thread_list (currthread
))
476 add_thread (currthread
);
477 printf_filtered ("[New %s]\n", target_pid_to_str (currthread
));
481 #define MAGIC_NULL_PID 42000
488 char *buf
= alloca (PBUFSIZ
);
489 int state
= gen
? general_thread
: continue_thread
;
495 buf
[1] = gen
? 'g' : 'c';
496 if (th
== MAGIC_NULL_PID
)
502 sprintf (&buf
[2], "-%x", -th
);
504 sprintf (&buf
[2], "%x", th
);
510 continue_thread
= th
;
513 /* Return nonzero if the thread TH is still alive on the remote system. */
516 remote_thread_alive (tid
)
522 sprintf (buf
, "T-%08x", -tid
);
524 sprintf (buf
, "T%08x", tid
);
527 return (buf
[0] == 'O' && buf
[1] == 'K');
530 /* About these extended threadlist and threadinfo packets. They are
531 variable length packets but, the fields within them are often fixed
532 length. They are redundent enough to send over UDP as is the
533 remote protocol in general. There is a matching unit test module
536 #define OPAQUETHREADBYTES 8
538 /* a 64 bit opaque identifier */
539 typedef unsigned char threadref
[OPAQUETHREADBYTES
];
541 /* WARNING: This threadref data structure comes from the remote O.S., libstub
542 protocol encoding, and remote.c. it is not particularly changable */
544 /* Right now, the internal structure is int. We want it to be bigger.
548 typedef int gdb_threadref
; /* internal GDB thread reference */
550 /* gdb_ext_thread_info is an internal GDB data structure which is
551 equivalint to the reply of the remote threadinfo packet */
553 struct gdb_ext_thread_info
555 threadref threadid
; /* External form of thread reference */
556 int active
; /* Has state interesting to GDB? , regs, stack */
557 char display
[256]; /* Brief state display, name, blocked/syspended */
558 char shortname
[32]; /* To be used to name threads */
559 char more_display
[256]; /* Long info, statistics, queue depth, whatever */
562 /* The volume of remote transfers can be limited by submitting
563 a mask containing bits specifying the desired information.
564 Use a union of these values as the 'selection' parameter to
565 get_thread_info. FIXME: Make these TAG names more thread specific.
568 #define TAG_THREADID 1
570 #define TAG_DISPLAY 4
571 #define TAG_THREADNAME 8
572 #define TAG_MOREDISPLAY 16
574 #define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES*2)
576 char *unpack_varlen_hex
PARAMS ((char *buff
, int *result
));
578 static char *unpack_nibble
PARAMS ((char *buf
, int *val
));
580 static char *pack_nibble
PARAMS ((char *buf
, int nibble
));
582 static char *pack_hex_byte
PARAMS ((char *pkt
, int/*unsigned char*/ byte
));
584 static char *unpack_byte
PARAMS ((char *buf
, int *value
));
586 static char *pack_int
PARAMS ((char *buf
, int value
));
588 static char *unpack_int
PARAMS ((char *buf
, int *value
));
590 static char *unpack_string
PARAMS ((char *src
, char *dest
, int length
));
592 static char *pack_threadid
PARAMS ((char *pkt
, threadref
*id
));
594 static char *unpack_threadid
PARAMS ((char *inbuf
, threadref
*id
));
596 void int_to_threadref
PARAMS ((threadref
*id
, int value
));
598 static int threadref_to_int
PARAMS ((threadref
*ref
));
600 static void copy_threadref
PARAMS ((threadref
*dest
, threadref
*src
));
602 static int threadmatch
PARAMS ((threadref
*dest
, threadref
*src
));
604 static char *pack_threadinfo_request
PARAMS ((char *pkt
, int mode
,
607 static int remote_unpack_thread_info_response
PARAMS ((char *pkt
,
608 threadref
*expectedref
,
609 struct gdb_ext_thread_info
*info
));
612 static int remote_get_threadinfo
PARAMS ((threadref
*threadid
,
613 int fieldset
, /*TAG mask */
614 struct gdb_ext_thread_info
*info
));
616 static int adapt_remote_get_threadinfo
PARAMS ((gdb_threadref
*ref
,
618 struct gdb_ext_thread_info
*info
));
620 static char *pack_threadlist_request
PARAMS ((char *pkt
, int startflag
,
622 threadref
*nextthread
));
624 static int parse_threadlist_response
PARAMS ((char *pkt
,
626 threadref
*original_echo
,
627 threadref
*resultlist
,
630 static int remote_get_threadlist
PARAMS ((int startflag
,
631 threadref
*nextthread
,
635 threadref
*threadlist
));
637 typedef int (*rmt_thread_action
) (threadref
*ref
, void *context
);
639 static int remote_threadlist_iterator
PARAMS ((rmt_thread_action stepfunction
,
640 void *context
, int looplimit
));
642 static int remote_newthread_step
PARAMS ((threadref
*ref
, void *context
));
644 /* encode 64 bits in 16 chars of hex */
646 static const char hexchars
[] = "0123456789abcdef";
653 if ((ch
>= 'a') && (ch
<= 'f'))
655 *val
= ch
- 'a' + 10;
658 if ((ch
>= 'A') && (ch
<= 'F'))
660 *val
= ch
- 'A' + 10;
663 if ((ch
>= '0') && (ch
<= '9'))
675 if (ch
>= 'a' && ch
<= 'f')
676 return ch
- 'a' + 10;
677 if (ch
>= '0' && ch
<= '9')
679 if (ch
>= 'A' && ch
<= 'F')
680 return ch
- 'A' + 10;
685 stub_unpack_int (buff
, fieldlength
)
694 nibble
= stubhex (*buff
++);
698 retval
= retval
<< 4;
704 unpack_varlen_hex (buff
, result
)
705 char *buff
; /* packet to parse */
711 while (ishex (*buff
, &nibble
))
714 retval
= retval
<< 4;
715 retval
|= nibble
& 0x0f;
722 unpack_nibble (buf
, val
)
731 pack_nibble (buf
, nibble
)
735 *buf
++ = hexchars
[(nibble
& 0x0f)];
740 pack_hex_byte (pkt
, byte
)
744 *pkt
++ = hexchars
[(byte
>> 4) & 0xf];
745 *pkt
++ = hexchars
[(byte
& 0xf)];
750 unpack_byte (buf
, value
)
754 *value
= stub_unpack_int (buf
, 2);
759 pack_int (buf
, value
)
763 buf
= pack_hex_byte (buf
, (value
>> 24) & 0xff);
764 buf
= pack_hex_byte (buf
, (value
>> 16) & 0xff);
765 buf
= pack_hex_byte (buf
, (value
>> 8) & 0x0ff);
766 buf
= pack_hex_byte (buf
, (value
& 0xff));
771 unpack_int (buf
, value
)
775 *value
= stub_unpack_int (buf
, 8);
779 #if 0 /* currently unused, uncomment when needed */
780 static char *pack_string
PARAMS ((char *pkt
, char *string
));
783 pack_string (pkt
, string
)
790 len
= strlen (string
);
792 len
= 200; /* Bigger than most GDB packets, junk??? */
793 pkt
= pack_hex_byte (pkt
, len
);
797 if ((ch
== '\0') || (ch
== '#'))
798 ch
= '*'; /* Protect encapsulation */
803 #endif /* 0 (unused) */
806 unpack_string (src
, dest
, length
)
818 pack_threadid (pkt
, id
)
823 unsigned char *altid
;
825 altid
= (unsigned char *) id
;
826 limit
= pkt
+ BUF_THREAD_ID_SIZE
;
828 pkt
= pack_hex_byte (pkt
, *altid
++);
834 unpack_threadid (inbuf
, id
)
839 char *limit
= inbuf
+ BUF_THREAD_ID_SIZE
;
842 altref
= (char *) id
;
844 while (inbuf
< limit
)
846 x
= stubhex (*inbuf
++);
847 y
= stubhex (*inbuf
++);
848 *altref
++ = (x
<< 4) | y
;
853 /* Externally, threadrefs are 64 bits but internally, they are still
854 ints. This is due to a mismatch of specifications. We would like
855 to use 64bit thread references internally. This is an adapter
859 int_to_threadref (id
, value
)
865 scan
= (unsigned char *) id
;
871 *scan
++ = (value
>> 24) & 0xff;
872 *scan
++ = (value
>> 16) & 0xff;
873 *scan
++ = (value
>> 8) & 0xff;
874 *scan
++ = (value
& 0xff);
878 threadref_to_int (ref
)
888 value
= (value
<< 8) | ((*scan
++) & 0xff);
893 copy_threadref (dest
, src
)
898 unsigned char *csrc
, *cdest
;
900 csrc
= (unsigned char *) src
;
901 cdest
= (unsigned char *) dest
;
908 threadmatch (dest
, src
)
912 /* things are broken right now, so just assume we got a match */
914 unsigned char *srcp
, *destp
;
917 destp
= (char *) dest
;
921 result
&= (*srcp
++ == *destp
++) ? 1 : 0;
928 threadid:1, # always request threadid
935 /* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
938 pack_threadinfo_request (pkt
, mode
, id
)
943 *pkt
++ = 'q'; /* Info Query */
944 *pkt
++ = 'P'; /* process or thread info */
945 pkt
= pack_int (pkt
, mode
); /* mode */
946 pkt
= pack_threadid (pkt
, id
); /* threadid */
947 *pkt
= '\0'; /* terminate */
951 /* These values tag the fields in a thread info response packet */
952 /* Tagging the fields allows us to request specific fields and to
953 add more fields as time goes by */
955 #define TAG_THREADID 1 /* Echo the thread identifier */
956 #define TAG_EXISTS 2 /* Is this process defined enough to
957 fetch registers and its stack */
958 #define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
959 #define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is */
960 #define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
964 remote_unpack_thread_info_response (pkt
, expectedref
, info
)
966 threadref
*expectedref
;
967 struct gdb_ext_thread_info
*info
;
972 char *limit
= pkt
+ PBUFSIZ
; /* plausable parsing limit */
975 /* info->threadid = 0; FIXME: implement zero_threadref */
977 info
->display
[0] = '\0';
978 info
->shortname
[0] = '\0';
979 info
->more_display
[0] = '\0';
981 /* Assume the characters indicating the packet type have been stripped */
982 pkt
= unpack_int (pkt
, &mask
); /* arg mask */
983 pkt
= unpack_threadid (pkt
, &ref
);
986 warning ("Incomplete response to threadinfo request\n");
987 if (!threadmatch (&ref
, expectedref
))
988 { /* This is an answer to a different request */
989 warning ("ERROR RMT Thread info mismatch\n");
992 copy_threadref (&info
->threadid
, &ref
);
994 /* Loop on tagged fields , try to bail if somthing goes wrong */
996 while ((pkt
< limit
) && mask
&& *pkt
) /* packets are terminated with nulls */
998 pkt
= unpack_int (pkt
, &tag
); /* tag */
999 pkt
= unpack_byte (pkt
, &length
); /* length */
1000 if (!(tag
& mask
)) /* tags out of synch with mask */
1002 warning ("ERROR RMT: threadinfo tag mismatch\n");
1006 if (tag
== TAG_THREADID
)
1010 warning ("ERROR RMT: length of threadid is not 16\n");
1014 pkt
= unpack_threadid (pkt
, &ref
);
1015 mask
= mask
& ~TAG_THREADID
;
1018 if (tag
== TAG_EXISTS
)
1020 info
->active
= stub_unpack_int (pkt
, length
);
1022 mask
= mask
& ~(TAG_EXISTS
);
1025 warning ("ERROR RMT: 'exists' length too long\n");
1031 if (tag
== TAG_THREADNAME
)
1033 pkt
= unpack_string (pkt
, &info
->shortname
[0], length
);
1034 mask
= mask
& ~TAG_THREADNAME
;
1037 if (tag
== TAG_DISPLAY
)
1039 pkt
= unpack_string (pkt
, &info
->display
[0], length
);
1040 mask
= mask
& ~TAG_DISPLAY
;
1043 if (tag
== TAG_MOREDISPLAY
)
1045 pkt
= unpack_string (pkt
, &info
->more_display
[0], length
);
1046 mask
= mask
& ~TAG_MOREDISPLAY
;
1049 warning ("ERROR RMT: unknown thread info tag\n");
1050 break; /* Not a tag we know about */
1056 remote_get_threadinfo (threadid
, fieldset
, info
)
1057 threadref
*threadid
;
1058 int fieldset
; /* TAG mask */
1059 struct gdb_ext_thread_info
*info
;
1062 char *threadinfo_pkt
= alloca (PBUFSIZ
);
1064 pack_threadinfo_request (threadinfo_pkt
, fieldset
, threadid
);
1065 putpkt (threadinfo_pkt
);
1066 getpkt (threadinfo_pkt
, 0);
1067 result
= remote_unpack_thread_info_response (threadinfo_pkt
+ 2, threadid
,
1072 /* Unfortunately, 61 bit thread-ids are bigger than the internal
1073 representation of a threadid. */
1076 adapt_remote_get_threadinfo (ref
, selection
, info
)
1079 struct gdb_ext_thread_info
*info
;
1083 int_to_threadref (&lclref
, *ref
);
1084 return remote_get_threadinfo (&lclref
, selection
, info
);
1087 /* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
1090 pack_threadlist_request (pkt
, startflag
, threadcount
, nextthread
)
1094 threadref
*nextthread
;
1096 *pkt
++ = 'q'; /* info query packet */
1097 *pkt
++ = 'L'; /* Process LIST or threadLIST request */
1098 pkt
= pack_nibble (pkt
, startflag
); /* initflag 1 bytes */
1099 pkt
= pack_hex_byte (pkt
, threadcount
); /* threadcount 2 bytes */
1100 pkt
= pack_threadid (pkt
, nextthread
); /* 64 bit thread identifier */
1105 /* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
1108 parse_threadlist_response (pkt
, result_limit
, original_echo
, resultlist
,
1112 threadref
*original_echo
;
1113 threadref
*resultlist
;
1117 int count
, resultcount
, done
;
1120 /* Assume the 'q' and 'M chars have been stripped. */
1121 limit
= pkt
+ (PBUFSIZ
- BUF_THREAD_ID_SIZE
); /* done parse past here */
1122 pkt
= unpack_byte (pkt
, &count
); /* count field */
1123 pkt
= unpack_nibble (pkt
, &done
);
1124 /* The first threadid is the argument threadid. */
1125 pkt
= unpack_threadid (pkt
, original_echo
); /* should match query packet */
1126 while ((count
-- > 0) && (pkt
< limit
))
1128 pkt
= unpack_threadid (pkt
, resultlist
++);
1129 if (resultcount
++ >= result_limit
)
1138 remote_get_threadlist (startflag
, nextthread
, result_limit
,
1139 done
, result_count
, threadlist
)
1141 threadref
*nextthread
;
1145 threadref
*threadlist
;
1148 static threadref echo_nextthread
;
1149 char *threadlist_packet
= alloca (PBUFSIZ
);
1150 char *t_response
= alloca (PBUFSIZ
);
1153 /* Trancate result limit to be smaller than the packet size */
1154 if ((((result_limit
+ 1) * BUF_THREAD_ID_SIZE
) + 10) >= PBUFSIZ
)
1155 result_limit
= (PBUFSIZ
/ BUF_THREAD_ID_SIZE
) - 2;
1157 pack_threadlist_request (threadlist_packet
,
1158 startflag
, result_limit
, nextthread
);
1159 putpkt (threadlist_packet
);
1160 getpkt (t_response
, 0);
1163 parse_threadlist_response (t_response
+ 2, result_limit
, &echo_nextthread
,
1166 if (!threadmatch (&echo_nextthread
, nextthread
))
1168 /* FIXME: This is a good reason to drop the packet */
1169 /* Possably, there is a duplicate response */
1171 retransmit immediatly - race conditions
1172 retransmit after timeout - yes
1174 wait for packet, then exit
1176 warning ("HMM: threadlist did not echo arg thread, dropping it\n");
1177 return 0; /* I choose simply exiting */
1179 if (*result_count
<= 0)
1183 warning ("RMT ERROR : failed to get remote thread list\n");
1186 return result
; /* break; */
1188 if (*result_count
> result_limit
)
1191 warning ("RMT ERROR: threadlist response longer than requested\n");
1197 /* This is the interface between remote and threads, remotes upper interface */
1199 /* remote_find_new_threads retrieves the thread list and for each
1200 thread in the list, looks up the thread in GDB's internal list,
1201 ading the thread if it does not already exist. This involves
1202 getting partial thread lists from the remote target so, polling the
1203 quit_flag is required. */
1206 /* About this many threadisds fit in a packet. */
1208 #define MAXTHREADLISTRESULTS 32
1211 remote_threadlist_iterator (stepfunction
, context
, looplimit
)
1212 rmt_thread_action stepfunction
;
1216 int done
, i
, result_count
;
1220 static threadref nextthread
;
1221 static threadref resultthreadlist
[MAXTHREADLISTRESULTS
];
1226 if (loopcount
++ > looplimit
)
1229 warning ("Remote fetch threadlist -infinite loop-\n");
1232 if (!remote_get_threadlist (startflag
, &nextthread
, MAXTHREADLISTRESULTS
,
1233 &done
, &result_count
, resultthreadlist
))
1238 /* clear for later iterations */
1240 /* Setup to resume next batch of thread references, set nextthread. */
1241 if (result_count
>= 1)
1242 copy_threadref (&nextthread
, &resultthreadlist
[result_count
- 1]);
1244 while (result_count
--)
1245 if (!(result
= (*stepfunction
) (&resultthreadlist
[i
++], context
)))
1252 remote_newthread_step (ref
, context
)
1258 pid
= threadref_to_int (ref
);
1259 if (!in_thread_list (pid
))
1261 return 1; /* continue iterator */
1264 #define CRAZY_MAX_THREADS 1000
1267 remote_current_thread (oldpid
)
1270 char *buf
= alloca (PBUFSIZ
);
1274 if (buf
[0] == 'Q' && buf
[1] == 'C')
1275 return strtol (&buf
[2], NULL
, 16);
1280 /* Find new threads for info threads command. */
1283 remote_find_new_threads ()
1285 remote_threadlist_iterator (remote_newthread_step
, 0,
1287 if (inferior_pid
== MAGIC_NULL_PID
) /* ack ack ack */
1288 inferior_pid
= remote_current_thread (inferior_pid
);
1292 remote_threads_info (void)
1294 char *buf
= alloca (PBUFSIZ
);
1298 if (remote_desc
== 0) /* paranoia */
1299 error ("Command can only be used when connected to the remote target.");
1301 putpkt ("qfThreadInfo");
1302 getpkt (bufp
= buf
, 0);
1303 if (bufp
[0] == '\0') /* q packet not recognized! */
1304 { /* try old jmetzler method */
1305 remote_find_new_threads ();
1308 else /* try new 'q' method */
1309 while (*bufp
++ == 'm') /* reply contains one or more TID */
1312 tid
= strtol(bufp
, &bufp
, 16);
1313 if (tid
!= 0 && !in_thread_list (tid
))
1315 } while (*bufp
++ == ','); /* comma-separated list */
1316 putpkt ("qsThreadInfo");
1317 getpkt (bufp
= buf
, 0);
1322 /* Restart the remote side; this is an extended protocol operation. */
1325 extended_remote_restart ()
1327 char *buf
= alloca (PBUFSIZ
);
1329 /* Send the restart command; for reasons I don't understand the
1330 remote side really expects a number after the "R". */
1332 sprintf (&buf
[1], "%x", 0);
1335 /* Now query for status so this looks just like we restarted
1336 gdbserver from scratch. */
1341 /* Clean up connection to a remote debugger. */
1345 remote_close (quitting
)
1349 SERIAL_CLOSE (remote_desc
);
1353 /* Query the remote side for the text, data and bss offsets. */
1358 char *buf
= alloca (PBUFSIZ
);
1361 CORE_ADDR text_addr
, data_addr
, bss_addr
;
1362 struct section_offsets
*offs
;
1364 putpkt ("qOffsets");
1368 if (buf
[0] == '\000')
1369 return; /* Return silently. Stub doesn't support
1373 warning ("Remote failure reply: %s", buf
);
1377 /* Pick up each field in turn. This used to be done with scanf, but
1378 scanf will make trouble if CORE_ADDR size doesn't match
1379 conversion directives correctly. The following code will work
1380 with any size of CORE_ADDR. */
1381 text_addr
= data_addr
= bss_addr
= 0;
1385 if (strncmp (ptr
, "Text=", 5) == 0)
1388 /* Don't use strtol, could lose on big values. */
1389 while (*ptr
&& *ptr
!= ';')
1390 text_addr
= (text_addr
<< 4) + fromhex (*ptr
++);
1395 if (!lose
&& strncmp (ptr
, ";Data=", 6) == 0)
1398 while (*ptr
&& *ptr
!= ';')
1399 data_addr
= (data_addr
<< 4) + fromhex (*ptr
++);
1404 if (!lose
&& strncmp (ptr
, ";Bss=", 5) == 0)
1407 while (*ptr
&& *ptr
!= ';')
1408 bss_addr
= (bss_addr
<< 4) + fromhex (*ptr
++);
1414 error ("Malformed response to offset query, %s", buf
);
1416 if (symfile_objfile
== NULL
)
1419 offs
= alloca (sizeof (struct section_offsets
)
1420 + symfile_objfile
->num_sections
1421 * sizeof (offs
->offsets
));
1422 memcpy (offs
, symfile_objfile
->section_offsets
,
1423 sizeof (struct section_offsets
)
1424 + symfile_objfile
->num_sections
1425 * sizeof (offs
->offsets
));
1427 ANOFFSET (offs
, SECT_OFF_TEXT
) = text_addr
;
1429 /* This is a temporary kludge to force data and bss to use the same offsets
1430 because that's what nlmconv does now. The real solution requires changes
1431 to the stub and remote.c that I don't have time to do right now. */
1433 ANOFFSET (offs
, SECT_OFF_DATA
) = data_addr
;
1434 ANOFFSET (offs
, SECT_OFF_BSS
) = data_addr
;
1436 objfile_relocate (symfile_objfile
, offs
);
1440 * Cisco version of section offsets:
1442 * Instead of having GDB query the target for the section offsets,
1443 * Cisco lets the target volunteer the information! It's also in
1444 * a different format, so here are the functions that will decode
1445 * a section offset packet from a Cisco target.
1449 * Function: remote_cisco_section_offsets
1451 * Returns: zero for success, non-zero for failure
1455 remote_cisco_section_offsets (text_addr
, data_addr
, bss_addr
,
1456 text_offs
, data_offs
, bss_offs
)
1460 bfd_signed_vma
* text_offs
;
1461 bfd_signed_vma
* data_offs
;
1462 bfd_signed_vma
* bss_offs
;
1464 bfd_vma text_base
, data_base
, bss_base
;
1465 struct minimal_symbol
*start
;
1471 if (symfile_objfile
== NULL
)
1472 return -1; /* no can do nothin' */
1474 start
= lookup_minimal_symbol ("_start", NULL
, NULL
);
1476 return -1; /* Can't find "_start" symbol */
1478 data_base
= bss_base
= 0;
1479 text_base
= SYMBOL_VALUE_ADDRESS (start
);
1481 abfd
= symfile_objfile
->obfd
;
1482 for (sect
= abfd
->sections
;
1486 p
= (unsigned char *) bfd_get_section_name (abfd
, sect
);
1488 if (strcmp (p
+ len
- 4, "data") == 0) /* ends in "data" */
1489 if (data_base
== 0 ||
1490 data_base
> bfd_get_section_vma (abfd
, sect
))
1491 data_base
= bfd_get_section_vma (abfd
, sect
);
1492 if (strcmp (p
+ len
- 3, "bss") == 0) /* ends in "bss" */
1493 if (bss_base
== 0 ||
1494 bss_base
> bfd_get_section_vma (abfd
, sect
))
1495 bss_base
= bfd_get_section_vma (abfd
, sect
);
1497 *text_offs
= text_addr
- text_base
;
1498 *data_offs
= data_addr
- data_base
;
1499 *bss_offs
= bss_addr
- bss_base
;
1504 sprintf (tmp
, "VMA: text = 0x");
1505 sprintf_vma (tmp
+ strlen (tmp
), text_addr
);
1506 sprintf (tmp
+ strlen (tmp
), " data = 0x");
1507 sprintf_vma (tmp
+ strlen (tmp
), data_addr
);
1508 sprintf (tmp
+ strlen (tmp
), " bss = 0x");
1509 sprintf_vma (tmp
+ strlen (tmp
), bss_addr
);
1510 fprintf_filtered (gdb_stdlog
, tmp
);
1511 fprintf_filtered (gdb_stdlog
,
1512 "Reloc offset: text = 0x%x data = 0x%x bss = 0x%x\n",
1513 (long) *text_offs
, (long) *data_offs
, (long) *bss_offs
);
1520 * Function: remote_cisco_objfile_relocate
1522 * Relocate the symbol file for a remote target.
1526 remote_cisco_objfile_relocate (text_off
, data_off
, bss_off
)
1527 bfd_signed_vma text_off
;
1528 bfd_signed_vma data_off
;
1529 bfd_signed_vma bss_off
;
1531 struct section_offsets
*offs
;
1533 if (text_off
!= 0 || data_off
!= 0 || bss_off
!= 0)
1535 /* FIXME: This code assumes gdb-stabs.h is being used; it's
1536 broken for xcoff, dwarf, sdb-coff, etc. But there is no
1537 simple canonical representation for this stuff. */
1539 offs
= ((struct section_offsets
*)
1540 alloca (sizeof (struct section_offsets
)
1541 + (symfile_objfile
->num_sections
1542 * sizeof (offs
->offsets
))));
1544 memcpy (offs
, symfile_objfile
->section_offsets
,
1545 (sizeof (struct section_offsets
)
1546 + (symfile_objfile
->num_sections
1547 * sizeof (offs
->offsets
))));
1549 ANOFFSET (offs
, SECT_OFF_TEXT
) = text_off
;
1550 ANOFFSET (offs
, SECT_OFF_DATA
) = data_off
;
1551 ANOFFSET (offs
, SECT_OFF_BSS
) = bss_off
;
1553 /* First call the standard objfile_relocate. */
1554 objfile_relocate (symfile_objfile
, offs
);
1556 /* Now we need to fix up the section entries already attached to
1557 the exec target. These entries will control memory transfers
1558 from the exec file. */
1560 exec_set_section_offsets (text_off
, data_off
, bss_off
);
1564 /* Stub for catch_errors. */
1567 remote_start_remote_dummy (dummy
)
1570 start_remote (); /* Initialize gdb process mechanisms */
1575 remote_start_remote (dummy
)
1578 immediate_quit
= 1; /* Allow user to interrupt it */
1580 /* Ack any packet which the remote side has already sent. */
1581 SERIAL_WRITE (remote_desc
, "+", 1);
1583 /* Let the stub know that we want it to return the thread. */
1586 inferior_pid
= remote_current_thread (inferior_pid
);
1588 get_offsets (); /* Get text, data & bss offsets */
1590 putpkt ("?"); /* initiate a query from remote machine */
1593 return remote_start_remote_dummy (dummy
);
1596 /* Open a connection to a remote debugger.
1597 NAME is the filename used for communication. */
1600 remote_open (name
, from_tty
)
1604 remote_open_1 (name
, from_tty
, &remote_ops
, 0);
1607 /* Open a connection to a remote debugger using the extended
1608 remote gdb protocol. NAME is the filename used for communication. */
1611 extended_remote_open (name
, from_tty
)
1615 remote_open_1 (name
, from_tty
, &extended_remote_ops
, 1/*extended_p*/);
1618 /* Generic code for opening a connection to a remote target. */
1620 static DCACHE
*remote_dcache
;
1623 remote_open_1 (name
, from_tty
, target
, extended_p
)
1626 struct target_ops
*target
;
1630 error ("To open a remote debug connection, you need to specify what\n\
1631 serial device is attached to the remote system (e.g. /dev/ttya).");
1633 target_preopen (from_tty
);
1635 unpush_target (target
);
1637 remote_dcache
= dcache_init (remote_read_bytes
, remote_write_bytes
);
1639 remote_desc
= SERIAL_OPEN (name
);
1641 perror_with_name (name
);
1643 if (baud_rate
!= -1)
1645 if (SERIAL_SETBAUDRATE (remote_desc
, baud_rate
))
1647 SERIAL_CLOSE (remote_desc
);
1648 perror_with_name (name
);
1653 SERIAL_RAW (remote_desc
);
1655 /* If there is something sitting in the buffer we might take it as a
1656 response to a command, which would be bad. */
1657 SERIAL_FLUSH_INPUT (remote_desc
);
1661 puts_filtered ("Remote debugging using ");
1662 puts_filtered (name
);
1663 puts_filtered ("\n");
1665 push_target (target
); /* Switch to using remote target now */
1667 /* Start out by trying the 'P' request to set registers. We set
1668 this each time that we open a new target so that if the user
1669 switches from one stub to another, we can (if the target is
1670 closed and reopened) cope. */
1671 stub_supports_P
= 1;
1673 general_thread
= -2;
1674 continue_thread
= -2;
1676 /* Force remote_write_bytes to check whether target supports
1677 binary downloading. */
1678 remote_binary_checked
= 0;
1680 /* Without this, some commands which require an active target (such
1681 as kill) won't work. This variable serves (at least) double duty
1682 as both the pid of the target process (if it has such), and as a
1683 flag indicating that a target is active. These functions should
1684 be split out into seperate variables, especially since GDB will
1685 someday have a notion of debugging several processes. */
1687 inferior_pid
= MAGIC_NULL_PID
;
1688 /* Start the remote connection; if error (0), discard this target.
1689 In particular, if the user quits, be sure to discard it
1690 (we'd be in an inconsistent state otherwise). */
1691 if (!catch_errors (remote_start_remote
, NULL
,
1692 "Couldn't establish connection to remote target\n",
1701 /* tell the remote that we're using the extended protocol. */
1702 char *buf
= alloca (PBUFSIZ
);
1708 /* This takes a program previously attached to and detaches it. After
1709 this is done, GDB can be used to debug some other program. We
1710 better not have left any breakpoints in the target program or it'll
1711 die when it hits one. */
1714 remote_detach (args
, from_tty
)
1718 char *buf
= alloca (PBUFSIZ
);
1721 error ("Argument given to \"detach\" when remotely debugging.");
1723 /* Tell the remote target to detach. */
1729 puts_filtered ("Ending remote debugging.\n");
1732 /* Convert hex digit A to a number. */
1738 if (a
>= '0' && a
<= '9')
1740 else if (a
>= 'a' && a
<= 'f')
1741 return a
- 'a' + 10;
1742 else if (a
>= 'A' && a
<= 'F')
1743 return a
- 'A' + 10;
1745 error ("Reply contains invalid hex digit %d", a
);
1748 /* Convert number NIB to a hex digit. */
1760 /* Tell the remote machine to resume. */
1762 static enum target_signal last_sent_signal
= TARGET_SIGNAL_0
;
1764 static int last_sent_step
;
1767 remote_resume (pid
, step
, siggnal
)
1769 enum target_signal siggnal
;
1771 char *buf
= alloca (PBUFSIZ
);
1774 set_thread (0, 0); /* run any thread */
1776 set_thread (pid
, 0); /* run this thread */
1778 dcache_flush (remote_dcache
);
1780 last_sent_signal
= siggnal
;
1781 last_sent_step
= step
;
1783 /* A hook for when we need to do something at the last moment before
1785 if (target_resume_hook
)
1786 (*target_resume_hook
) ();
1788 if (siggnal
!= TARGET_SIGNAL_0
)
1790 buf
[0] = step
? 'S' : 'C';
1791 buf
[1] = tohex (((int)siggnal
>> 4) & 0xf);
1792 buf
[2] = tohex ((int)siggnal
& 0xf);
1796 strcpy (buf
, step
? "s": "c");
1801 /* Send ^C to target to halt it. Target will respond, and send us a
1804 static void (*ofunc
) PARAMS ((int));
1806 /* The command line interface's stop routine. This function is installed
1807 as a signal handler for SIGINT. The first time a user requests a
1808 stop, we call remote_stop to send a break or ^C. If there is no
1809 response from the target (it didn't stop when the user requested it),
1810 we ask the user if he'd like to detach from the target. */
1812 remote_interrupt (signo
)
1815 /* If this doesn't work, try more severe steps. */
1816 signal (signo
, remote_interrupt_twice
);
1819 fprintf_unfiltered (gdb_stdlog
, "remote_interrupt called\n");
1824 /* The user typed ^C twice. */
1827 remote_interrupt_twice (signo
)
1830 signal (signo
, ofunc
);
1832 signal (signo
, remote_interrupt
);
1835 /* This is the generic stop called via the target vector. When a target
1836 interrupt is requested, either by the command line or the GUI, we
1837 will eventually end up here. */
1841 /* Send a break or a ^C, depending on user preference. */
1843 fprintf_unfiltered (gdb_stdlog
, "remote_stop called\n");
1846 SERIAL_SEND_BREAK (remote_desc
);
1848 SERIAL_WRITE (remote_desc
, "\003", 1);
1851 /* Ask the user what to do when an interrupt is received. */
1856 target_terminal_ours ();
1858 if (query ("Interrupted while waiting for the program.\n\
1859 Give up (and stop debugging it)? "))
1861 target_mourn_inferior ();
1862 return_to_top_level (RETURN_QUIT
);
1865 target_terminal_inferior ();
1868 /* If nonzero, ignore the next kill. */
1873 remote_console_output (msg
)
1878 for (p
= msg
; *p
; p
+=2)
1881 char c
= fromhex (p
[0]) * 16 + fromhex (p
[1]);
1884 if (target_output_hook
)
1885 target_output_hook (tb
);
1887 fputs_filtered (tb
, gdb_stdout
);
1891 /* Wait until the remote machine stops, then return,
1892 storing status in STATUS just as `wait' would.
1893 Returns "pid", which in the case of a multi-threaded
1894 remote OS, is the thread-id. */
1897 remote_wait (pid
, status
)
1899 struct target_waitstatus
*status
;
1901 unsigned char *buf
= alloca (PBUFSIZ
);
1902 int thread_num
= -1;
1904 status
->kind
= TARGET_WAITKIND_EXITED
;
1905 status
->value
.integer
= 0;
1911 ofunc
= signal (SIGINT
, remote_interrupt
);
1912 getpkt ((char *) buf
, 1);
1913 signal (SIGINT
, ofunc
);
1915 /* This is a hook for when we need to do something (perhaps the
1916 collection of trace data) every time the target stops. */
1917 if (target_wait_loop_hook
)
1918 (*target_wait_loop_hook
) ();
1922 case 'E': /* Error of some sort */
1923 warning ("Remote failure reply: %s", buf
);
1925 case 'T': /* Status with PC, SP, FP, ... */
1929 char regs
[MAX_REGISTER_RAW_SIZE
];
1931 /* Expedited reply, containing Signal, {regno, reg} repeat */
1932 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
1934 n... = register number
1935 r... = register contents
1937 p
= &buf
[3]; /* after Txx */
1944 /* Read the register number */
1945 regno
= strtol ((const char *) p
, &p_temp
, 16);
1946 p1
= (unsigned char *)p_temp
;
1948 if (p1
== p
) /* No register number present here */
1950 p1
= (unsigned char *) strchr ((const char *) p
, ':');
1952 warning ("Malformed packet(a) (missing colon): %s\n\
1955 if (strncmp ((const char *) p
, "thread", p1
- p
) == 0)
1957 p_temp
= unpack_varlen_hex (++p1
, &thread_num
);
1958 record_currthread (thread_num
);
1959 p
= (unsigned char *) p_temp
;
1967 warning ("Malformed packet(b) (missing colon): %s\n\
1971 if (regno
>= NUM_REGS
)
1972 warning ("Remote sent bad register number %ld: %s\n\
1976 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
++)
1978 if (p
[0] == 0 || p
[1] == 0)
1979 warning ("Remote reply is too short: %s", buf
);
1980 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
1983 supply_register (regno
, regs
);
1988 warning ("Remote register badly formatted: %s", buf
);
1989 warning (" here: %s",p
);
1994 case 'S': /* Old style status, just signal only */
1995 status
->kind
= TARGET_WAITKIND_STOPPED
;
1996 status
->value
.sig
= (enum target_signal
)
1997 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
2001 /* Export Cisco kernel mode as a convenience variable
2002 (so that it can be used in the GDB prompt if desired). */
2004 if (cisco_kernel_mode
== 1)
2005 set_internalvar (lookup_internalvar ("cisco_kernel_mode"),
2006 value_from_string ("PDEBUG-"));
2007 cisco_kernel_mode
= 0;
2008 thread_num
= strtol ((const char *) &buf
[4], NULL
, 16);
2009 record_currthread (thread_num
);
2011 else if (buf
[3] == 'k')
2013 /* Export Cisco kernel mode as a convenience variable
2014 (so that it can be used in the GDB prompt if desired). */
2016 if (cisco_kernel_mode
== 1)
2017 set_internalvar (lookup_internalvar ("cisco_kernel_mode"),
2018 value_from_string ("KDEBUG-"));
2019 cisco_kernel_mode
= 1;
2022 case 'N': /* Cisco special: status and offsets */
2024 bfd_vma text_addr
, data_addr
, bss_addr
;
2025 bfd_signed_vma text_off
, data_off
, bss_off
;
2028 status
->kind
= TARGET_WAITKIND_STOPPED
;
2029 status
->value
.sig
= (enum target_signal
)
2030 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
2032 if (symfile_objfile
== NULL
)
2034 warning ("Relocation packet recieved with no symbol file. \
2039 /* Relocate object file. Buffer format is NAATT;DD;BB
2040 * where AA is the signal number, TT is the new text
2041 * address, DD * is the new data address, and BB is the
2042 * new bss address. */
2045 text_addr
= strtoul (p
, (char **) &p1
, 16);
2046 if (p1
== p
|| *p1
!= ';')
2047 warning ("Malformed relocation packet: Packet '%s'", buf
);
2049 data_addr
= strtoul (p
, (char **) &p1
, 16);
2050 if (p1
== p
|| *p1
!= ';')
2051 warning ("Malformed relocation packet: Packet '%s'", buf
);
2053 bss_addr
= strtoul (p
, (char **) &p1
, 16);
2055 warning ("Malformed relocation packet: Packet '%s'", buf
);
2057 if (remote_cisco_section_offsets (text_addr
, data_addr
, bss_addr
,
2058 &text_off
, &data_off
, &bss_off
)
2060 if (text_off
!= 0 || data_off
!= 0 || bss_off
!= 0)
2061 remote_cisco_objfile_relocate (text_off
, data_off
, bss_off
);
2065 case 'W': /* Target exited */
2067 /* The remote process exited. */
2068 status
->kind
= TARGET_WAITKIND_EXITED
;
2069 status
->value
.integer
= (fromhex (buf
[1]) << 4) + fromhex (buf
[2]);
2073 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
2074 status
->value
.sig
= (enum target_signal
)
2075 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
2079 case 'O': /* Console output */
2080 remote_console_output (buf
+ 1);
2083 if (last_sent_signal
!= TARGET_SIGNAL_0
)
2085 /* Zero length reply means that we tried 'S' or 'C' and
2086 the remote system doesn't support it. */
2087 target_terminal_ours_for_output ();
2089 ("Can't send signals to this remote system. %s not sent.\n",
2090 target_signal_to_name (last_sent_signal
));
2091 last_sent_signal
= TARGET_SIGNAL_0
;
2092 target_terminal_inferior ();
2094 strcpy ((char *) buf
, last_sent_step
? "s" : "c");
2095 putpkt ((char *) buf
);
2098 /* else fallthrough */
2100 warning ("Invalid remote reply: %s", buf
);
2105 if (thread_num
!= -1)
2109 return inferior_pid
;
2112 /* Number of bytes of registers this stub implements. */
2114 static int register_bytes_found
;
2116 /* Read the remote registers into the block REGS. */
2117 /* Currently we just read all the registers, so we don't use regno. */
2121 remote_fetch_registers (regno
)
2124 char *buf
= alloca (PBUFSIZ
);
2127 char regs
[REGISTER_BYTES
];
2129 set_thread (inferior_pid
, 1);
2134 if (remote_register_buf_size
== 0)
2135 remote_register_buf_size
= strlen (buf
);
2137 /* Unimplemented registers read as all bits zero. */
2138 memset (regs
, 0, REGISTER_BYTES
);
2140 /* We can get out of synch in various cases. If the first character
2141 in the buffer is not a hex character, assume that has happened
2142 and try to fetch another packet to read. */
2143 while ((buf
[0] < '0' || buf
[0] > '9')
2144 && (buf
[0] < 'a' || buf
[0] > 'f')
2145 && buf
[0] != 'x') /* New: unavailable register value */
2148 fprintf_unfiltered (gdb_stdlog
,
2149 "Bad register packet; fetching a new packet\n");
2153 /* Reply describes registers byte by byte, each byte encoded as two
2154 hex characters. Suck them all up, then supply them to the
2155 register cacheing/storage mechanism. */
2158 for (i
= 0; i
< REGISTER_BYTES
; i
++)
2164 warning ("Remote reply is of odd length: %s", buf
);
2165 /* Don't change register_bytes_found in this case, and don't
2166 print a second warning. */
2169 if (p
[0] == 'x' && p
[1] == 'x')
2170 regs
[i
] = 0; /* 'x' */
2172 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
2176 if (i
!= register_bytes_found
)
2178 register_bytes_found
= i
;
2179 #ifdef REGISTER_BYTES_OK
2180 if (!REGISTER_BYTES_OK (i
))
2181 warning ("Remote reply is too short: %s", buf
);
2186 for (i
= 0; i
< NUM_REGS
; i
++)
2188 supply_register (i
, ®s
[REGISTER_BYTE(i
)]);
2189 if (buf
[REGISTER_BYTE(i
) * 2] == 'x')
2190 register_valid
[i
] = -1; /* register value not available */
2194 /* Prepare to store registers. Since we may send them all (using a
2195 'G' request), we have to read out the ones we don't want to change
2199 remote_prepare_to_store ()
2201 /* Make sure the entire registers array is valid. */
2202 read_register_bytes (0, (char *)NULL
, REGISTER_BYTES
);
2205 /* Store register REGNO, or all registers if REGNO == -1, from the contents
2206 of REGISTERS. FIXME: ignores errors. */
2209 remote_store_registers (regno
)
2212 char *buf
= alloca (PBUFSIZ
);
2216 set_thread (inferior_pid
, 1);
2218 if (regno
>= 0 && stub_supports_P
)
2220 /* Try storing a single register. */
2223 sprintf (buf
, "P%x=", regno
);
2224 p
= buf
+ strlen (buf
);
2225 regp
= ®isters
[REGISTER_BYTE (regno
)];
2226 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); ++i
)
2228 *p
++ = tohex ((regp
[i
] >> 4) & 0xf);
2229 *p
++ = tohex (regp
[i
] & 0xf);
2235 /* The stub understands the 'P' request. We are done. */
2239 /* The stub does not support the 'P' request. Use 'G' instead,
2240 and don't try using 'P' in the future (it will just waste our
2242 stub_supports_P
= 0;
2247 /* Command describes registers byte by byte,
2248 each byte encoded as two hex characters. */
2251 /* remote_prepare_to_store insures that register_bytes_found gets set. */
2252 for (i
= 0; i
< register_bytes_found
; i
++)
2254 *p
++ = tohex ((registers
[i
] >> 4) & 0xf);
2255 *p
++ = tohex (registers
[i
] & 0xf);
2262 /* Use of the data cache *used* to be disabled because it loses for looking
2263 at and changing hardware I/O ports and the like. Accepting `volatile'
2264 would perhaps be one way to fix it. Another idea would be to use the
2265 executable file for the text segment (for all SEC_CODE sections?
2266 For all SEC_READONLY sections?). This has problems if you want to
2267 actually see what the memory contains (e.g. self-modifying code,
2268 clobbered memory, user downloaded the wrong thing).
2270 Because it speeds so much up, it's now enabled, if you're playing
2271 with registers you turn it of (set remotecache 0). */
2273 /* Read a word from remote address ADDR and return it.
2274 This goes through the data cache. */
2278 remote_fetch_word (addr
)
2281 return dcache_fetch (remote_dcache
, addr
);
2284 /* Write a word WORD into remote address ADDR.
2285 This goes through the data cache. */
2288 remote_store_word (addr
, word
)
2292 dcache_poke (remote_dcache
, addr
, word
);
2294 #endif /* 0 (unused?) */
2298 /* Return the number of hex digits in num. */
2306 for (i
= 0; num
!= 0; i
++)
2312 /* Set BUF to the hex digits representing NUM. */
2315 hexnumstr (buf
, num
)
2320 int len
= hexnumlen (num
);
2324 for (i
= len
- 1; i
>= 0; i
--)
2326 buf
[i
] = "0123456789abcdef" [(num
& 0xf)];
2333 /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
2336 remote_address_masked (addr
)
2339 if (remote_address_size
> 0
2340 && remote_address_size
< (sizeof (ULONGEST
) * 8))
2342 /* Only create a mask when that mask can safely be constructed
2343 in a ULONGEST variable. */
2345 mask
= (mask
<< remote_address_size
) - 1;
2351 /* Determine whether the remote target supports binary downloading.
2352 This is accomplished by sending a no-op memory write of zero length
2353 to the target at the specified address. It does not suffice to send
2354 the whole packet, since many stubs strip the eighth bit and subsequently
2355 compute a wrong checksum, which causes real havoc with remote_write_bytes.
2357 NOTE: This can still lose if the serial line is not eight-bit clean. In
2358 cases like this, the user should clear "remotebinarydownload". */
2360 check_binary_download (addr
)
2363 if (remote_binary_download
&& !remote_binary_checked
)
2365 char *buf
= alloca (PBUFSIZ
);
2367 remote_binary_checked
= 1;
2371 p
+= hexnumstr (p
, (ULONGEST
) addr
);
2373 p
+= hexnumstr (p
, (ULONGEST
) 0);
2377 putpkt_binary (buf
, (int) (p
- buf
));
2381 remote_binary_download
= 0;
2386 if (remote_binary_download
)
2387 fprintf_unfiltered (gdb_stdlog
,
2388 "binary downloading suppported by target\n");
2390 fprintf_unfiltered (gdb_stdlog
,
2391 "binary downloading NOT suppported by target\n");
2395 /* Write memory data directly to the remote machine.
2396 This does not inform the data cache; the data cache uses this.
2397 MEMADDR is the address in the remote memory space.
2398 MYADDR is the address of the buffer in our space.
2399 LEN is the number of bytes.
2401 Returns number of bytes transferred, or 0 for error. */
2404 remote_write_bytes (memaddr
, myaddr
, len
)
2409 unsigned char *buf
= alloca (PBUFSIZ
);
2410 int max_buf_size
; /* Max size of packet output buffer */
2413 /* Verify that the target can support a binary download */
2414 check_binary_download (memaddr
);
2416 /* Chop the transfer down if necessary */
2418 max_buf_size
= min (remote_write_size
, PBUFSIZ
);
2419 if (remote_register_buf_size
!= 0)
2420 max_buf_size
= min (max_buf_size
, remote_register_buf_size
);
2422 /* Subtract header overhead from max payload size - $M<memaddr>,<len>:#nn */
2423 max_buf_size
-= 2 + hexnumlen (memaddr
+ len
- 1) + 1 + hexnumlen (len
) + 4;
2428 unsigned char *p
, *plen
;
2432 /* construct "M"<memaddr>","<len>":" */
2433 /* sprintf (buf, "M%lx,%x:", (unsigned long) memaddr, todo); */
2434 memaddr
= remote_address_masked (memaddr
);
2436 if (remote_binary_download
)
2439 todo
= min (len
, max_buf_size
);
2444 todo
= min (len
, max_buf_size
/ 2); /* num bytes that will fit */
2447 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
2450 plen
= p
; /* remember where len field goes */
2451 p
+= hexnumstr (p
, (ULONGEST
) todo
);
2455 /* We send target system values byte by byte, in increasing byte
2456 addresses, each byte encoded as two hex characters (or one
2457 binary character). */
2458 if (remote_binary_download
)
2462 (i
< todo
) && (i
+ escaped
) < (max_buf_size
- 2);
2465 switch (myaddr
[i
] & 0xff)
2470 /* These must be escaped */
2473 *p
++ = (myaddr
[i
] & 0xff) ^ 0x20;
2476 *p
++ = myaddr
[i
] & 0xff;
2483 /* Escape chars have filled up the buffer prematurely,
2484 and we have actually sent fewer bytes than planned.
2485 Fix-up the length field of the packet. */
2487 /* FIXME: will fail if new len is a shorter string than
2490 plen
+= hexnumstr (plen
, (ULONGEST
) i
);
2496 for (i
= 0; i
< todo
; i
++)
2498 *p
++ = tohex ((myaddr
[i
] >> 4) & 0xf);
2499 *p
++ = tohex (myaddr
[i
] & 0xf);
2504 putpkt_binary (buf
, (int) (p
- buf
));
2509 /* There is no correspondance between what the remote protocol uses
2510 for errors and errno codes. We would like a cleaner way of
2511 representing errors (big enough to include errno codes, bfd_error
2512 codes, and others). But for now just return EIO. */
2517 /* Increment by i, not by todo, in case escape chars
2518 caused us to send fewer bytes than we'd planned. */
2526 /* Read memory data directly from the remote machine.
2527 This does not use the data cache; the data cache uses this.
2528 MEMADDR is the address in the remote memory space.
2529 MYADDR is the address of the buffer in our space.
2530 LEN is the number of bytes.
2532 Returns number of bytes transferred, or 0 for error. */
2535 remote_read_bytes (memaddr
, myaddr
, len
)
2540 char *buf
= alloca (PBUFSIZ
);
2541 int max_buf_size
; /* Max size of packet output buffer */
2544 /* Chop the transfer down if necessary */
2546 max_buf_size
= min (remote_write_size
, PBUFSIZ
);
2547 if (remote_register_buf_size
!= 0)
2548 max_buf_size
= min (max_buf_size
, remote_register_buf_size
);
2557 todo
= min (len
, max_buf_size
/ 2); /* num bytes that will fit */
2559 /* construct "m"<memaddr>","<len>" */
2560 /* sprintf (buf, "m%lx,%x", (unsigned long) memaddr, todo); */
2561 memaddr
= remote_address_masked (memaddr
);
2564 p
+= hexnumstr (p
, (ULONGEST
) memaddr
);
2566 p
+= hexnumstr (p
, (ULONGEST
) todo
);
2574 /* There is no correspondance between what the remote protocol uses
2575 for errors and errno codes. We would like a cleaner way of
2576 representing errors (big enough to include errno codes, bfd_error
2577 codes, and others). But for now just return EIO. */
2582 /* Reply describes memory byte by byte,
2583 each byte encoded as two hex characters. */
2586 for (i
= 0; i
< todo
; i
++)
2588 if (p
[0] == 0 || p
[1] == 0)
2589 /* Reply is short. This means that we were able to read
2590 only part of what we wanted to. */
2591 return i
+ (origlen
- len
);
2592 myaddr
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
2602 /* Read or write LEN bytes from inferior memory at MEMADDR,
2603 transferring to or from debugger address BUFFER. Write to inferior if
2604 SHOULD_WRITE is nonzero. Returns length of data written or read; 0
2607 #ifndef REMOTE_TRANSLATE_XFER_ADDRESS
2608 #define REMOTE_TRANSLATE_XFER_ADDRESS(MEM_ADDR, MEM_LEN, TARG_ADDR, TARG_LEN) \
2609 (*(TARG_ADDR) = (MEM_ADDR), *(TARG_LEN) = (MEM_LEN))
2614 remote_xfer_memory (mem_addr
, buffer
, mem_len
, should_write
, target
)
2619 struct target_ops
*target
; /* ignored */
2621 CORE_ADDR targ_addr
;
2623 REMOTE_TRANSLATE_XFER_ADDRESS (mem_addr
, mem_len
, &targ_addr
, &targ_len
);
2627 return dcache_xfer_memory (remote_dcache
, targ_addr
, buffer
,
2628 targ_len
, should_write
);
2633 /* Enable after 4.12. */
2636 remote_search (len
, data
, mask
, startaddr
, increment
, lorange
, hirange
2637 addr_found
, data_found
)
2641 CORE_ADDR startaddr
;
2645 CORE_ADDR
*addr_found
;
2648 if (increment
== -4 && len
== 4)
2650 long mask_long
, data_long
;
2651 long data_found_long
;
2652 CORE_ADDR addr_we_found
;
2653 char *buf
= alloca (PBUFSIZ
);
2654 long returned_long
[2];
2657 mask_long
= extract_unsigned_integer (mask
, len
);
2658 data_long
= extract_unsigned_integer (data
, len
);
2659 sprintf (buf
, "t%x:%x,%x", startaddr
, data_long
, mask_long
);
2664 /* The stub doesn't support the 't' request. We might want to
2665 remember this fact, but on the other hand the stub could be
2666 switched on us. Maybe we should remember it only until
2667 the next "target remote". */
2668 generic_search (len
, data
, mask
, startaddr
, increment
, lorange
,
2669 hirange
, addr_found
, data_found
);
2674 /* There is no correspondance between what the remote protocol uses
2675 for errors and errno codes. We would like a cleaner way of
2676 representing errors (big enough to include errno codes, bfd_error
2677 codes, and others). But for now just use EIO. */
2678 memory_error (EIO
, startaddr
);
2681 while (*p
!= '\0' && *p
!= ',')
2682 addr_we_found
= (addr_we_found
<< 4) + fromhex (*p
++);
2684 error ("Protocol error: short return for search");
2686 data_found_long
= 0;
2687 while (*p
!= '\0' && *p
!= ',')
2688 data_found_long
= (data_found_long
<< 4) + fromhex (*p
++);
2689 /* Ignore anything after this comma, for future extensions. */
2691 if (addr_we_found
< lorange
|| addr_we_found
>= hirange
)
2697 *addr_found
= addr_we_found
;
2698 *data_found
= store_unsigned_integer (data_we_found
, len
);
2701 generic_search (len
, data
, mask
, startaddr
, increment
, lorange
,
2702 hirange
, addr_found
, data_found
);
2707 remote_files_info (ignore
)
2708 struct target_ops
*ignore
;
2710 puts_filtered ("Debugging a target over a serial line.\n");
2713 /* Stuff for dealing with the packets which are part of this protocol.
2714 See comment at top of file for details. */
2716 /* Read a single character from the remote end, masking it down to 7 bits. */
2724 ch
= SERIAL_READCHAR (remote_desc
, timeout
);
2729 error ("Remote connection closed");
2731 perror_with_name ("Remote communication error");
2732 case SERIAL_TIMEOUT
:
2739 /* Send the command in BUF to the remote machine, and read the reply
2740 into BUF. Report an error if we get an error reply. */
2750 error ("Remote failure reply: %s", buf
);
2753 /* Display a null-terminated packet on stdout, for debugging, using C
2760 puts_filtered ("\"");
2762 gdb_printchar (*buf
++, gdb_stdout
, '"');
2763 puts_filtered ("\"");
2770 return putpkt_binary (buf
, strlen (buf
));
2773 /* Send a packet to the remote machine, with error checking. The data
2774 of the packet is in BUF. The string in BUF can be at most PBUFSIZ - 5
2775 to account for the $, # and checksum, and for a possible /0 if we are
2776 debugging (remote_debug) and want to print the sent packet as a string */
2779 putpkt_binary (buf
, cnt
)
2784 unsigned char csum
= 0;
2785 char *buf2
= alloca (PBUFSIZ
);
2786 char *junkbuf
= alloca (PBUFSIZ
);
2792 /* Copy the packet into buffer BUF2, encapsulating it
2793 and giving it a checksum. */
2795 if (cnt
> (int) sizeof (buf2
) - 5) /* Prosanity check */
2801 for (i
= 0; i
< cnt
; i
++)
2807 *p
++ = tohex ((csum
>> 4) & 0xf);
2808 *p
++ = tohex (csum
& 0xf);
2810 /* Send it over and over until we get a positive ack. */
2814 int started_error_output
= 0;
2819 fprintf_unfiltered (gdb_stdlog
, "Sending packet: %s...", buf2
);
2820 gdb_flush (gdb_stdlog
);
2822 if (SERIAL_WRITE (remote_desc
, buf2
, p
- buf2
))
2823 perror_with_name ("putpkt: write failed");
2825 /* read until either a timeout occurs (-2) or '+' is read */
2828 ch
= readchar (remote_timeout
);
2835 case SERIAL_TIMEOUT
:
2837 if (started_error_output
)
2839 putchar_unfiltered ('\n');
2840 started_error_output
= 0;
2849 fprintf_unfiltered (gdb_stdlog
, "Ack\n");
2851 case SERIAL_TIMEOUT
:
2855 break; /* Retransmit buffer */
2858 /* It's probably an old response, and we're out of sync.
2859 Just gobble up the packet and ignore it. */
2860 getpkt (junkbuf
, 0);
2861 continue; /* Now, go look for + */
2866 if (!started_error_output
)
2868 started_error_output
= 1;
2869 fprintf_unfiltered (gdb_stdlog
, "putpkt: Junk: ");
2871 fputc_unfiltered (ch
& 0177, gdb_stdlog
);
2875 break; /* Here to retransmit */
2879 /* This is wrong. If doing a long backtrace, the user should be
2880 able to get out next time we call QUIT, without anything as
2881 violent as interrupt_query. If we want to provide a way out of
2882 here without getting to the next QUIT, it should be based on
2883 hitting ^C twice as in remote_wait. */
2893 static int remote_cisco_mode
;
2895 static void remote_cisco_expand (src
, dest
)
2905 repeat
= (fromhex (src
[1]) << 4) + fromhex (src
[2]);
2906 for (i
= 0; i
< repeat
; i
++)
2919 /* Come here after finding the start of the frame. Collect the rest
2920 into BUF, verifying the checksum, length, and handling run-length
2921 compression. Returns 0 on any error, 1 on success. */
2936 c
= readchar (remote_timeout
);
2940 case SERIAL_TIMEOUT
:
2942 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog
);
2946 fputs_filtered ("Saw new packet start in middle of old one\n",
2948 return 0; /* Start a new packet, count retries */
2951 unsigned char pktcsum
;
2955 pktcsum
= fromhex (readchar (remote_timeout
)) << 4;
2956 pktcsum
|= fromhex (readchar (remote_timeout
));
2958 if (csum
== pktcsum
)
2960 if (remote_cisco_mode
) /* variant run-length-encoding */
2962 char *tmp_buf
= alloca (PBUFSIZ
);
2964 remote_cisco_expand (buf
, tmp_buf
);
2965 strcpy (buf
, tmp_buf
);
2972 fprintf_filtered (gdb_stdlog
,
2973 "Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
2975 fputs_filtered (buf
, gdb_stdlog
);
2976 fputs_filtered ("\n", gdb_stdlog
);
2980 case '*': /* Run length encoding */
2981 if (remote_cisco_mode
== 0) /* variant run-length-encoding */
2984 c
= readchar (remote_timeout
);
2986 c
= c
- ' ' + 3; /* Compute repeat count */
2988 if (c
> 0 && c
< 255 && bp
+ c
- 1 < buf
+ PBUFSIZ
- 1)
2990 memset (bp
, *(bp
- 1), c
);
2996 printf_filtered ("Repeat count %d too large for buffer: ", c
);
2997 puts_filtered (buf
);
2998 puts_filtered ("\n");
3001 /* else fall thru to treat like default */
3003 if (bp
< buf
+ PBUFSIZ
- 1)
3011 puts_filtered ("Remote packet too long: ");
3012 puts_filtered (buf
);
3013 puts_filtered ("\n");
3020 /* Read a packet from the remote machine, with error checking, and
3021 store it in BUF. BUF is expected to be of size PBUFSIZ. If
3022 FOREVER, wait forever rather than timing out; this is used while
3023 the target is executing user code. */
3026 getpkt (buf
, forever
)
3035 strcpy (buf
,"timeout");
3039 timeout
= watchdog
> 0 ? watchdog
: -1;
3043 timeout
= remote_timeout
;
3047 for (tries
= 1; tries
<= MAX_TRIES
; tries
++)
3049 /* This can loop forever if the remote side sends us characters
3050 continuously, but if it pauses, we'll get a zero from readchar
3051 because of timeout. Then we'll count that as a retry. */
3053 /* Note that we will only wait forever prior to the start of a packet.
3054 After that, we expect characters to arrive at a brisk pace. They
3055 should show up within remote_timeout intervals. */
3059 c
= readchar (timeout
);
3061 if (c
== SERIAL_TIMEOUT
)
3063 if (forever
) /* Watchdog went off. Kill the target. */
3065 target_mourn_inferior ();
3066 error ("Watchdog has expired. Target detached.\n");
3069 fputs_filtered ("Timed out.\n", gdb_stdlog
);
3075 /* We've found the start of a packet, now collect the data. */
3077 val
= read_frame (buf
);
3082 fprintf_unfiltered (gdb_stdlog
, "Packet received: %s\n", buf
);
3083 SERIAL_WRITE (remote_desc
, "+", 1);
3087 /* Try the whole thing again. */
3089 SERIAL_WRITE (remote_desc
, "-", 1);
3092 /* We have tried hard enough, and just can't receive the packet. Give up. */
3094 printf_unfiltered ("Ignoring packet error, continuing...\n");
3095 SERIAL_WRITE (remote_desc
, "+", 1);
3101 /* For some mysterious reason, wait_for_inferior calls kill instead of
3102 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
3106 target_mourn_inferior ();
3110 /* Use catch_errors so the user can quit from gdb even when we aren't on
3111 speaking terms with the remote system. */
3112 catch_errors ((catch_errors_ftype
*) putpkt
, "k", "", RETURN_MASK_ERROR
);
3114 /* Don't wait for it to die. I'm not really sure it matters whether
3115 we do or not. For the existing stubs, kill is a noop. */
3116 target_mourn_inferior ();
3122 remote_mourn_1 (&remote_ops
);
3126 extended_remote_mourn ()
3128 /* We do _not_ want to mourn the target like this; this will
3129 remove the extended remote target from the target stack,
3130 and the next time the user says "run" it'll fail.
3132 FIXME: What is the right thing to do here? */
3134 remote_mourn_1 (&extended_remote_ops
);
3138 /* Worker function for remote_mourn. */
3140 remote_mourn_1 (target
)
3141 struct target_ops
*target
;
3143 unpush_target (target
);
3144 generic_mourn_inferior ();
3147 /* In the extended protocol we want to be able to do things like
3148 "run" and have them basically work as expected. So we need
3149 a special create_inferior function.
3151 FIXME: One day add support for changing the exec file
3152 we're debugging, arguments and an environment. */
3155 extended_remote_create_inferior (exec_file
, args
, env
)
3160 /* Rip out the breakpoints; we'll reinsert them after restarting
3161 the remote server. */
3162 remove_breakpoints ();
3164 /* Now restart the remote server. */
3165 extended_remote_restart ();
3167 /* Now put the breakpoints back in. This way we're safe if the
3168 restart function works via a unix fork on the remote side. */
3169 insert_breakpoints ();
3171 /* Clean up from the last time we were running. */
3172 clear_proceed_status ();
3174 /* Let the remote process run. */
3175 proceed (-1, TARGET_SIGNAL_0
, 0);
3179 /* On some machines, e.g. 68k, we may use a different breakpoint instruction
3180 than other targets; in those use REMOTE_BREAKPOINT instead of just
3181 BREAKPOINT. Also, bi-endian targets may define LITTLE_REMOTE_BREAKPOINT
3182 and BIG_REMOTE_BREAKPOINT. If none of these are defined, we just call
3183 the standard routines that are in mem-break.c. */
3185 /* FIXME, these ought to be done in a more dynamic fashion. For instance,
3186 the choice of breakpoint instruction affects target program design and
3187 vice versa, and by making it user-tweakable, the special code here
3188 goes away and we need fewer special GDB configurations. */
3190 #if defined (LITTLE_REMOTE_BREAKPOINT) && defined (BIG_REMOTE_BREAKPOINT) && !defined(REMOTE_BREAKPOINT)
3191 #define REMOTE_BREAKPOINT
3194 #ifdef REMOTE_BREAKPOINT
3196 /* If the target isn't bi-endian, just pretend it is. */
3197 #if !defined (LITTLE_REMOTE_BREAKPOINT) && !defined (BIG_REMOTE_BREAKPOINT)
3198 #define LITTLE_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
3199 #define BIG_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
3202 static unsigned char big_break_insn
[] = BIG_REMOTE_BREAKPOINT
;
3203 static unsigned char little_break_insn
[] = LITTLE_REMOTE_BREAKPOINT
;
3205 #endif /* REMOTE_BREAKPOINT */
3207 /* Insert a breakpoint on targets that don't have any better breakpoint
3208 support. We read the contents of the target location and stash it,
3209 then overwrite it with a breakpoint instruction. ADDR is the target
3210 location in the target machine. CONTENTS_CACHE is a pointer to
3211 memory allocated for saving the target contents. It is guaranteed
3212 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
3213 is accomplished via BREAKPOINT_MAX). */
3216 remote_insert_breakpoint (addr
, contents_cache
)
3218 char *contents_cache
;
3220 #ifdef REMOTE_BREAKPOINT
3223 val
= target_read_memory (addr
, contents_cache
, sizeof big_break_insn
);
3227 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
3228 val
= target_write_memory (addr
, (char *) big_break_insn
,
3229 sizeof big_break_insn
);
3231 val
= target_write_memory (addr
, (char *) little_break_insn
,
3232 sizeof little_break_insn
);
3237 return memory_insert_breakpoint (addr
, contents_cache
);
3238 #endif /* REMOTE_BREAKPOINT */
3242 remote_remove_breakpoint (addr
, contents_cache
)
3244 char *contents_cache
;
3246 #ifdef REMOTE_BREAKPOINT
3247 return target_write_memory (addr
, contents_cache
, sizeof big_break_insn
);
3249 return memory_remove_breakpoint (addr
, contents_cache
);
3250 #endif /* REMOTE_BREAKPOINT */
3253 /* Some targets are only capable of doing downloads, and afterwards
3254 they switch to the remote serial protocol. This function provides
3255 a clean way to get from the download target to the remote target.
3256 It's basically just a wrapper so that we don't have to expose any
3257 of the internal workings of remote.c.
3259 Prior to calling this routine, you should shutdown the current
3260 target code, else you will get the "A program is being debugged
3261 already..." message. Usually a call to pop_target() suffices. */
3264 push_remote_target (name
, from_tty
)
3268 printf_filtered ("Switching to remote protocol\n");
3269 remote_open (name
, from_tty
);
3272 /* Other targets want to use the entire remote serial module but with
3273 certain remote_ops overridden. */
3276 open_remote_target (name
, from_tty
, target
, extended_p
)
3279 struct target_ops
*target
;
3282 printf_filtered ("Selecting the %sremote protocol\n",
3283 (extended_p
? "extended-" : ""));
3284 remote_open_1 (name
, from_tty
, target
, extended_p
);
3287 /* Table used by the crc32 function to calcuate the checksum. */
3289 static unsigned long crc32_table
[256] = {0, 0};
3291 static unsigned long
3292 crc32 (buf
, len
, crc
)
3297 if (! crc32_table
[1])
3299 /* Initialize the CRC table and the decoding table. */
3303 for (i
= 0; i
< 256; i
++)
3305 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
3306 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
3313 crc
= (crc
<< 8) ^ crc32_table
[((crc
>> 24) ^ *buf
) & 255];
3319 /* compare-sections command
3321 With no arguments, compares each loadable section in the exec bfd
3322 with the same memory range on the target, and reports mismatches.
3323 Useful for verifying the image on the target against the exec file.
3324 Depends on the target understanding the new "qCRC:" request. */
3327 compare_sections_command (args
, from_tty
)
3332 unsigned long host_crc
, target_crc
;
3333 extern bfd
*exec_bfd
;
3334 struct cleanup
*old_chain
;
3338 char *buf
= alloca (PBUFSIZ
);
3345 error ("command cannot be used without an exec file");
3346 if (!current_target
.to_shortname
||
3347 strcmp (current_target
.to_shortname
, "remote") != 0)
3348 error ("command can only be used with remote target");
3350 for (s
= exec_bfd
->sections
; s
; s
= s
->next
)
3352 if (!(s
->flags
& SEC_LOAD
))
3353 continue; /* skip non-loadable section */
3355 size
= bfd_get_section_size_before_reloc (s
);
3357 continue; /* skip zero-length section */
3359 sectname
= (char *) bfd_get_section_name (exec_bfd
, s
);
3360 if (args
&& strcmp (args
, sectname
) != 0)
3361 continue; /* not the section selected by user */
3363 matched
= 1; /* do this section */
3365 /* FIXME: assumes lma can fit into long */
3366 sprintf (buf
, "qCRC:%lx,%lx", (long) lma
, (long) size
);
3369 /* be clever; compute the host_crc before waiting for target reply */
3370 sectdata
= xmalloc (size
);
3371 old_chain
= make_cleanup (free
, sectdata
);
3372 bfd_get_section_contents (exec_bfd
, s
, sectdata
, 0, size
);
3373 host_crc
= crc32 ((unsigned char *) sectdata
, size
, 0xffffffff);
3377 error ("target memory fault, section %s, range 0x%08x -- 0x%08x",
3378 sectname
, lma
, lma
+ size
);
3380 error ("remote target does not support this operation");
3382 for (target_crc
= 0, tmp
= &buf
[1]; *tmp
; tmp
++)
3383 target_crc
= target_crc
* 16 + fromhex (*tmp
);
3385 printf_filtered ("Section %s, range 0x%08x -- 0x%08x: ",
3386 sectname
, lma
, lma
+ size
);
3387 if (host_crc
== target_crc
)
3388 printf_filtered ("matched.\n");
3391 printf_filtered ("MIS-MATCHED!\n");
3395 do_cleanups (old_chain
);
3398 warning ("One or more sections of the remote executable does not match\n\
3399 the loaded file\n");
3400 if (args
&& !matched
)
3401 printf_filtered ("No loaded section named '%s'.\n", args
);
3405 remote_query (query_type
, buf
, outbuf
, bufsiz
)
3412 char *buf2
= alloca (PBUFSIZ
);
3413 char *p2
= &buf2
[0];
3417 error ("null pointer to remote bufer size specified");
3419 /* minimum outbuf size is PBUFSIZ - if bufsiz is not large enough let
3420 the caller know and return what the minimum size is */
3421 /* Note: a zero bufsiz can be used to query the minimum buffer size */
3422 if ( *bufsiz
< PBUFSIZ
)
3428 /* except for querying the minimum buffer size, target must be open */
3430 error ("remote query is only available after target open");
3432 /* we only take uppercase letters as query types, at least for now */
3433 if ( (query_type
< 'A') || (query_type
> 'Z') )
3434 error ("invalid remote query type");
3437 error ("null remote query specified");
3440 error ("remote query requires a buffer to receive data");
3447 /* we used one buffer char for the remote protocol q command and another
3448 for the query type. As the remote protocol encapsulation uses 4 chars
3449 plus one extra in case we are debugging (remote_debug),
3450 we have PBUFZIZ - 7 left to pack the query string */
3452 while ( buf
[i
] && (i
< (PBUFSIZ
- 8)) )
3454 /* bad caller may have sent forbidden characters */
3455 if ( (!isprint(buf
[i
])) || (buf
[i
] == '$') || (buf
[i
] == '#') )
3456 error ("illegal characters in query string");
3464 error ("query larger than available buffer");
3467 if ( i
< 0 ) return i
;
3475 packet_command (args
, from_tty
)
3479 char *buf
= alloca (PBUFSIZ
);
3482 error ("command can only be used with remote target");
3485 error ("remote-packet command requires packet text as argument");
3487 puts_filtered ("sending: ");
3488 print_packet (args
);
3489 puts_filtered ("\n");
3493 puts_filtered ("received: ");
3495 puts_filtered ("\n");
3499 /* --------- UNIT_TEST for THREAD oriented PACKETS ------------------------- */
3501 static void display_thread_info
PARAMS ((struct gdb_ext_thread_info
*info
));
3503 static void threadset_test_cmd
PARAMS ((char *cmd
, int tty
));
3505 static void threadalive_test
PARAMS ((char *cmd
, int tty
));
3507 static void threadlist_test_cmd
PARAMS ((char *cmd
, int tty
));
3509 int get_and_display_threadinfo
PARAMS ((threadref
*ref
));
3511 static void threadinfo_test_cmd
PARAMS ((char *cmd
, int tty
));
3513 static int thread_display_step
PARAMS ((threadref
*ref
, void *context
));
3515 static void threadlist_update_test_cmd
PARAMS ((char *cmd
, int tty
));
3517 static void init_remote_threadtests
PARAMS ((void));
3519 #define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid */
3522 threadset_test_cmd (cmd
, tty
)
3526 int sample_thread
= SAMPLE_THREAD
;
3528 printf_filtered ("Remote threadset test\n");
3529 set_thread (sample_thread
, 1);
3534 threadalive_test (cmd
, tty
)
3538 int sample_thread
= SAMPLE_THREAD
;
3540 if (remote_thread_alive (sample_thread
))
3541 printf_filtered ("PASS: Thread alive test\n");
3543 printf_filtered ("FAIL: Thread alive test\n");
3546 void output_threadid
PARAMS ((char *title
, threadref
* ref
));
3549 output_threadid (title
, ref
)
3555 pack_threadid (&hexid
[0], ref
); /* Convert threead id into hex */
3557 printf_filtered ("%s %s\n", title
, (&hexid
[0]));
3561 threadlist_test_cmd (cmd
, tty
)
3566 threadref nextthread
;
3567 int done
, result_count
;
3568 threadref threadlist
[3];
3570 printf_filtered ("Remote Threadlist test\n");
3571 if (!remote_get_threadlist (startflag
, &nextthread
, 3, &done
,
3572 &result_count
, &threadlist
[0]))
3573 printf_filtered ("FAIL: threadlist test\n");
3576 threadref
*scan
= threadlist
;
3577 threadref
*limit
= scan
+ result_count
;
3579 while (scan
< limit
)
3580 output_threadid (" thread ", scan
++);
3585 display_thread_info (info
)
3586 struct gdb_ext_thread_info
*info
;
3588 output_threadid ("Threadid: ", &info
->threadid
);
3589 printf_filtered ("Name: %s\n ", info
->shortname
);
3590 printf_filtered ("State: %s\n", info
->display
);
3591 printf_filtered ("other: %s\n\n", info
->more_display
);
3595 get_and_display_threadinfo (ref
)
3600 struct gdb_ext_thread_info threadinfo
;
3602 set
= TAG_THREADID
| TAG_EXISTS
| TAG_THREADNAME
3603 | TAG_MOREDISPLAY
| TAG_DISPLAY
;
3604 if (0 != (result
= remote_get_threadinfo (ref
, set
, &threadinfo
)))
3605 display_thread_info (&threadinfo
);
3610 threadinfo_test_cmd (cmd
, tty
)
3614 int athread
= SAMPLE_THREAD
;
3618 int_to_threadref (&thread
, athread
);
3619 printf_filtered ("Remote Threadinfo test\n");
3620 if (!get_and_display_threadinfo (&thread
))
3621 printf_filtered ("FAIL cannot get thread info\n");
3625 thread_display_step (ref
, context
)
3629 /* output_threadid(" threadstep ",ref); *//* simple test */
3630 return get_and_display_threadinfo (ref
);
3634 threadlist_update_test_cmd (cmd
, tty
)
3638 printf_filtered ("Remote Threadlist update test\n");
3639 remote_threadlist_iterator (thread_display_step
, 0, CRAZY_MAX_THREADS
);
3643 init_remote_threadtests (void)
3645 add_com ("tlist", class_obscure
, threadlist_test_cmd
,
3646 "Fetch and print the remote list of thread identifiers, one pkt only");
3647 add_com ("tinfo", class_obscure
, threadinfo_test_cmd
,
3648 "Fetch and display info about one thread");
3649 add_com ("tset", class_obscure
, threadset_test_cmd
,
3650 "Test setting to a different thread");
3651 add_com ("tupd", class_obscure
, threadlist_update_test_cmd
,
3652 "Iterate through updating all remote thread info");
3653 add_com ("talive", class_obscure
, threadalive_test
,
3654 " Remote thread alive test ");
3662 remote_ops
.to_shortname
= "remote";
3663 remote_ops
.to_longname
= "Remote serial target in gdb-specific protocol";
3665 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
3666 Specify the serial device it is connected to (e.g. /dev/ttya).";
3667 remote_ops
.to_open
= remote_open
;
3668 remote_ops
.to_close
= remote_close
;
3669 remote_ops
.to_detach
= remote_detach
;
3670 remote_ops
.to_resume
= remote_resume
;
3671 remote_ops
.to_wait
= remote_wait
;
3672 remote_ops
.to_fetch_registers
= remote_fetch_registers
;
3673 remote_ops
.to_store_registers
= remote_store_registers
;
3674 remote_ops
.to_prepare_to_store
= remote_prepare_to_store
;
3675 remote_ops
.to_xfer_memory
= remote_xfer_memory
;
3676 remote_ops
.to_files_info
= remote_files_info
;
3677 remote_ops
.to_insert_breakpoint
= remote_insert_breakpoint
;
3678 remote_ops
.to_remove_breakpoint
= remote_remove_breakpoint
;
3679 remote_ops
.to_kill
= remote_kill
;
3680 remote_ops
.to_load
= generic_load
;
3681 remote_ops
.to_mourn_inferior
= remote_mourn
;
3682 remote_ops
.to_thread_alive
= remote_thread_alive
;
3683 remote_ops
.to_find_new_threads
= remote_threads_info
;
3684 remote_ops
.to_stop
= remote_stop
;
3685 remote_ops
.to_query
= remote_query
;
3686 remote_ops
.to_stratum
= process_stratum
;
3687 remote_ops
.to_has_all_memory
= 1;
3688 remote_ops
.to_has_memory
= 1;
3689 remote_ops
.to_has_stack
= 1;
3690 remote_ops
.to_has_registers
= 1;
3691 remote_ops
.to_has_execution
= 1;
3692 remote_ops
.to_has_thread_control
= tc_schedlock
; /* can lock scheduler */
3693 remote_ops
.to_magic
= OPS_MAGIC
;
3696 /* Set up the extended remote vector by making a copy of the standard
3697 remote vector and adding to it. */
3700 init_extended_remote_ops ()
3702 extended_remote_ops
= remote_ops
;
3704 extended_remote_ops
.to_shortname
= "extended-remote";
3705 extended_remote_ops
.to_longname
=
3706 "Extended remote serial target in gdb-specific protocol";
3707 extended_remote_ops
.to_doc
=
3708 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
3709 Specify the serial device it is connected to (e.g. /dev/ttya).",
3710 extended_remote_ops
.to_open
= extended_remote_open
;
3711 extended_remote_ops
.to_create_inferior
= extended_remote_create_inferior
;
3712 extended_remote_ops
.to_mourn_inferior
= extended_remote_mourn
;
3716 * Command: info remote-process
3718 * This implements Cisco's version of the "info proc" command.
3720 * This query allows the target stub to return an arbitrary string
3721 * (or strings) giving arbitrary information about the target process.
3722 * This is optional; the target stub isn't required to implement it.
3724 * Syntax: qfProcessInfo request first string
3725 * qsProcessInfo request subsequent string
3726 * reply: 'O'<hex-encoded-string>
3727 * 'l' last reply (empty)
3731 remote_info_process (args
, from_tty
)
3735 char *buf
= alloca (PBUFSIZ
);
3737 if (remote_desc
== 0)
3738 error ("Command can only be used when connected to the remote target.");
3740 putpkt ("qfProcessInfo");
3743 return; /* Silently: target does not support this feature. */
3746 error ("info proc: target error.");
3748 while (buf
[0] == 'O') /* Capitol-O packet */
3750 remote_console_output (&buf
[1]);
3751 putpkt ("qsProcessInfo");
3761 remote_cisco_open (name
, from_tty
)
3767 "To open a remote debug connection, you need to specify what \n\
3768 device is attached to the remote system (e.g. host:port).");
3770 target_preopen (from_tty
);
3772 unpush_target (&remote_cisco_ops
);
3774 remote_dcache
= dcache_init (remote_read_bytes
, remote_write_bytes
);
3776 remote_desc
= SERIAL_OPEN (name
);
3778 perror_with_name (name
);
3781 * If a baud rate was specified on the gdb command line it will
3782 * be greater than the initial value of -1. If it is, use it otherwise
3786 baud_rate
= (baud_rate
> 0) ? baud_rate
: 9600;
3787 if (SERIAL_SETBAUDRATE (remote_desc
, baud_rate
))
3789 SERIAL_CLOSE (remote_desc
);
3790 perror_with_name (name
);
3793 SERIAL_RAW (remote_desc
);
3795 /* If there is something sitting in the buffer we might take it as a
3796 response to a command, which would be bad. */
3797 SERIAL_FLUSH_INPUT (remote_desc
);
3801 puts_filtered ("Remote debugging using ");
3802 puts_filtered (name
);
3803 puts_filtered ("\n");
3806 remote_cisco_mode
= 1;
3808 push_target (&remote_cisco_ops
); /* Switch to using cisco target now */
3810 /* Start out by trying the 'P' request to set registers. We set this each
3811 time that we open a new target so that if the user switches from one
3812 stub to another, we can (if the target is closed and reopened) cope. */
3813 stub_supports_P
= 1;
3815 general_thread
= -2;
3816 continue_thread
= -2;
3818 /* Force remote_write_bytes to check whether target supports
3819 binary downloading. */
3820 remote_binary_checked
= 0;
3822 /* Without this, some commands which require an active target (such
3823 as kill) won't work. This variable serves (at least) double duty
3824 as both the pid of the target process (if it has such), and as a
3825 flag indicating that a target is active. These functions should
3826 be split out into seperate variables, especially since GDB will
3827 someday have a notion of debugging several processes. */
3828 inferior_pid
= MAGIC_NULL_PID
;
3830 /* Start the remote connection; if error (0), discard this target. */
3832 if (!catch_errors (remote_start_remote_dummy
, (char *) 0,
3833 "Couldn't establish connection to remote target\n",
3842 remote_cisco_close (quitting
)
3845 remote_cisco_mode
= 0;
3846 remote_close (quitting
);
3850 remote_cisco_mourn
PARAMS ((void))
3852 remote_mourn_1 (&remote_cisco_ops
);
3860 } minitelnet_return
;
3862 /* shared between readsocket() and readtty() */
3863 static char *tty_input
;
3865 static int escape_count
;
3866 static int echo_check
;
3867 extern int quit_flag
;
3874 /* Loop until the socket doesn't have any more data */
3876 while ((data
= readchar (0)) >= 0)
3878 /* Check for the escape sequence */
3881 /* If this is the fourth escape, get out */
3882 if (++escape_count
== 4)
3887 { /* This is a '|', but not the fourth in a row.
3888 Continue without echoing it. If it isn't actually
3889 one of four in a row, it'll be echoed later. */
3893 else /* Not a '|' */
3895 /* Ensure any pending '|'s are flushed. */
3897 for ( ; escape_count
> 0; escape_count
--)
3901 if (data
== '\r') /* If this is a return character, */
3902 continue; /* - just supress it. */
3904 if (echo_check
!= -1) /* Check for echo of user input. */
3906 if (tty_input
[echo_check
] == data
)
3908 echo_check
++; /* Character matched user input: */
3909 continue; /* Continue without echoing it. */
3911 else if ((data
== '\n') && (tty_input
[echo_check
] == '\r'))
3912 { /* End of the line (and of echo checking). */
3913 echo_check
= -1; /* No more echo supression */
3914 continue; /* Continue without echoing. */
3917 { /* Failed check for echo of user input.
3918 We now have some suppressed output to flush! */
3921 for (j
= 0; j
< echo_check
; j
++)
3922 putchar (tty_input
[j
]);
3926 putchar (data
); /* Default case: output the char. */
3929 if (data
== SERIAL_TIMEOUT
) /* Timeout returned from readchar. */
3930 return READ_MORE
; /* Try to read some more */
3932 return FATAL_ERROR
; /* Trouble, bail out */
3941 /* First, read a buffer full from the terminal */
3942 tty_bytecount
= read (fileno (stdin
), tty_input
, sizeof (tty_input
) - 1);
3943 if (tty_bytecount
== -1)
3945 perror ("readtty: read failed");
3949 /* Remove a quoted newline. */
3950 if (tty_input
[tty_bytecount
- 1] == '\n' &&
3951 tty_input
[tty_bytecount
- 2] == '\\') /* line ending in backslash */
3953 tty_input
[--tty_bytecount
] = 0; /* remove newline */
3954 tty_input
[--tty_bytecount
] = 0; /* remove backslash */
3957 /* Turn trailing newlines into returns */
3958 if (tty_input
[tty_bytecount
- 1] == '\n')
3959 tty_input
[tty_bytecount
- 1] = '\r';
3961 /* If the line consists of a ~, enter debugging mode. */
3962 if ((tty_input
[0] == '~') && (tty_bytecount
== 2))
3965 /* Make this a zero terminated string and write it out */
3966 tty_input
[tty_bytecount
] = 0;
3967 if (SERIAL_WRITE (remote_desc
, tty_input
, tty_bytecount
))
3969 perror_with_name ("readtty: write failed");
3979 fd_set input
; /* file descriptors for select */
3980 int tablesize
; /* max number of FDs for select */
3984 extern int escape_count
; /* global shared by readsocket */
3985 extern int echo_check
; /* ditto */
3990 tablesize
= 8 * sizeof (input
);
3994 /* Check for anything from our socket - doesn't block. Note that
3995 this must be done *before* the select as there may be
3996 buffered I/O waiting to be processed. */
3998 if ((status
= readsocket ()) == FATAL_ERROR
)
4000 error ("Debugging terminated by communications error");
4002 else if (status
!= READ_MORE
)
4007 fflush(stdout
); /* Flush output before blocking */
4009 /* Now block on more socket input or TTY input */
4012 FD_SET (fileno(stdin
), &input
);
4013 FD_SET (remote_desc
->fd
, &input
);
4015 status
= select (tablesize
, &input
, 0, 0, 0);
4016 if ((status
== -1) && (errno
!= EINTR
))
4018 error ("Communications error on select %d", errno
);
4021 /* Handle Control-C typed */
4025 if ((++quit_count
) == 2)
4027 if (query ("Interrupt GDB? "))
4029 printf_filtered ("Interrupted by user.\n");
4030 return_to_top_level (RETURN_QUIT
);
4037 SERIAL_SEND_BREAK (remote_desc
);
4039 SERIAL_WRITE (remote_desc
, "\003", 1);
4044 /* Handle console input */
4046 if (FD_ISSET (fileno (stdin
), &input
))
4050 status
= readtty ();
4051 if (status
== READ_MORE
)
4054 return status
; /* telnet session ended */
4060 remote_cisco_wait (pid
, status
)
4062 struct target_waitstatus
*status
;
4064 if (minitelnet() != ENTER_DEBUG
)
4066 error ("Debugging session terminated by protocol error");
4069 return remote_wait (pid
, status
);
4073 init_remote_cisco_ops ()
4075 remote_cisco_ops
.to_shortname
= "cisco";
4076 remote_cisco_ops
.to_longname
= "Remote serial target in cisco-specific protocol";
4077 remote_cisco_ops
.to_doc
=
4078 "Use a remote machine via TCP, using a cisco-specific protocol.\n\
4079 Specify the serial device it is connected to (e.g. host:2020).";
4080 remote_cisco_ops
.to_open
= remote_cisco_open
;
4081 remote_cisco_ops
.to_close
= remote_cisco_close
;
4082 remote_cisco_ops
.to_detach
= remote_detach
;
4083 remote_cisco_ops
.to_resume
= remote_resume
;
4084 remote_cisco_ops
.to_wait
= remote_cisco_wait
;
4085 remote_cisco_ops
.to_fetch_registers
= remote_fetch_registers
;
4086 remote_cisco_ops
.to_store_registers
= remote_store_registers
;
4087 remote_cisco_ops
.to_prepare_to_store
= remote_prepare_to_store
;
4088 remote_cisco_ops
.to_xfer_memory
= remote_xfer_memory
;
4089 remote_cisco_ops
.to_files_info
= remote_files_info
;
4090 remote_cisco_ops
.to_insert_breakpoint
= remote_insert_breakpoint
;
4091 remote_cisco_ops
.to_remove_breakpoint
= remote_remove_breakpoint
;
4092 remote_cisco_ops
.to_kill
= remote_kill
;
4093 remote_cisco_ops
.to_load
= generic_load
;
4094 remote_cisco_ops
.to_mourn_inferior
= remote_cisco_mourn
;
4095 remote_cisco_ops
.to_thread_alive
= remote_thread_alive
;
4096 remote_cisco_ops
.to_find_new_threads
= remote_threads_info
;
4097 remote_cisco_ops
.to_stratum
= process_stratum
;
4098 remote_cisco_ops
.to_has_all_memory
= 1;
4099 remote_cisco_ops
.to_has_memory
= 1;
4100 remote_cisco_ops
.to_has_stack
= 1;
4101 remote_cisco_ops
.to_has_registers
= 1;
4102 remote_cisco_ops
.to_has_execution
= 1;
4103 remote_cisco_ops
.to_magic
= OPS_MAGIC
;
4107 build_remote_gdbarch_data ()
4109 tty_input
= xmalloc (PBUFSIZ
);
4114 _initialize_remote ()
4116 /* architecture specific data */
4117 build_remote_gdbarch_data ();
4118 register_gdbarch_swap (&tty_input
, sizeof (&tty_input
), NULL
);
4119 register_gdbarch_swap (NULL
, 0, build_remote_gdbarch_data
);
4121 /* runtime constants - we retain the value of remote_write_size
4122 across architecture swaps. */
4123 remote_write_size
= PBUFSIZ
;
4126 add_target (&remote_ops
);
4128 init_extended_remote_ops ();
4129 add_target (&extended_remote_ops
);
4131 init_remote_cisco_ops ();
4132 add_target (&remote_cisco_ops
);
4135 init_remote_threadtests ();
4138 add_cmd ("compare-sections", class_obscure
, compare_sections_command
,
4139 "Compare section data on target to the exec file.\n\
4140 Argument is a single section name (default: all loaded sections).",
4143 add_cmd ("packet", class_maintenance
, packet_command
,
4144 "Send an arbitrary packet to a remote target.\n\
4145 maintenance packet TEXT\n\
4146 If GDB is talking to an inferior via the GDB serial protocol, then\n\
4147 this command sends the string TEXT to the inferior, and displays the\n\
4148 response packet. GDB supplies the initial `$' character, and the\n\
4149 terminating `#' character and checksum.",
4153 (add_set_cmd ("remotetimeout", no_class
,
4154 var_integer
, (char *)&remote_timeout
,
4155 "Set timeout value for remote read.\n",
4160 (add_set_cmd ("remotebreak", no_class
,
4161 var_integer
, (char *)&remote_break
,
4162 "Set whether to send break if interrupted.\n",
4167 (add_set_cmd ("remotewritesize", no_class
,
4168 var_integer
, (char *)&remote_write_size
,
4169 "Set the maximum number of bytes per memory write packet.\n",
4173 remote_address_size
= TARGET_PTR_BIT
;
4175 (add_set_cmd ("remoteaddresssize", class_obscure
,
4176 var_integer
, (char *)&remote_address_size
,
4177 "Set the maximum size of the address (in bits) \
4178 in a memory packet.\n",
4183 (add_set_cmd ("remotebinarydownload", no_class
,
4184 var_boolean
, (char *) &remote_binary_download
,
4185 "Set binary downloads.\n", &setlist
),
4188 add_info ("remote-process", remote_info_process
,
4189 "Query the remote system for process info.");