1999-01-19 Fernando Nasser <fnasser@totem.to.cygnus.com>
[deliverable/binutils-gdb.git] / gdb / remote.c
CommitLineData
b543979c 1/* Remote target communications for serial-line targets in custom GDB protocol
2e7eeba9
MS
2 Copyright 1988, 91, 92, 93, 94, 95, 96, 97, 1998
3 Free Software Foundation, Inc.
bd5635a1
RP
4
5This file is part of GDB.
6
b543979c 7This program is free software; you can redistribute it and/or modify
bd5635a1 8it under the terms of the GNU General Public License as published by
b543979c
JG
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
bd5635a1 11
b543979c 12This program is distributed in the hope that it will be useful,
bd5635a1
RP
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
b543979c 18along with this program; if not, write to the Free Software
998cfe7d 19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
bd5635a1
RP
20
21/* Remote communication protocol.
e50ebec8
JK
22
23 A debug packet whose contents are <data>
24 is encapsulated for transmission in the form:
25
26 $ <data> # CSUM1 CSUM2
27
28 <data> must be ASCII alphanumeric and cannot include characters
0c993550
JK
29 '$' or '#'. If <data> starts with two characters followed by
30 ':', then the existing stubs interpret this as a sequence number.
e50ebec8
JK
31
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.
35
36 Receiver responds with:
37
38 + - if CSUM is correct and ready for next packet
39 - - if CSUM is incorrect
40
41 <data> is as follows:
4cc1b3f7
JK
42 Most values are encoded in ascii hex digits. Signal numbers are according
43 to the numbering in target.h.
bd5635a1
RP
44
45 Request Packet
46
4cc1b3f7
JK
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
50 threads.
51 c = 'g' for thread used in other
52 operations. If zero, pick a thread,
53 any thread.
54 reply OK for success
55 ENN for an error.
56
bd5635a1
RP
57 read registers g
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.
63 or ENN for an error.
64
65 write regs GXX..XX Each byte of register data
66 is described by two hex digits.
67 reply OK for success
68 ENN for an error
69
0c993550 70 write reg Pn...=r... Write register n... with value r...,
4aa6fe10
JK
71 which contains two hex digits for each
72 byte in the register (target byte
73 order).
74 reply OK for success
75 ENN for an error
76 (not supported by all stubs).
77
bd5635a1
RP
78 read mem mAA..AA,LLLL AA..AA is address, LLLL is length.
79 reply XX..XX XX..XX is mem contents
d538b510
RP
80 Can be fewer bytes than requested
81 if able to read only part of the data.
bd5635a1
RP
82 or ENN NN is errno
83
84 write mem MAA..AA,LLLL:XX..XX
85 AA..AA is address,
86 LLLL is number of bytes,
87 XX..XX is data
88 reply OK for success
d538b510
RP
89 ENN for an error (this includes the case
90 where only part of the data was
91 written).
bd5635a1 92
4cc1b3f7 93 continue cAA..AA AA..AA is address to resume
bd5635a1
RP
94 If AA..AA is omitted,
95 resume at same address.
96
97 step sAA..AA AA..AA is address to resume
98 If AA..AA is omitted,
99 resume at same address.
100
0a325463 101 continue with Csig;AA..AA Continue with signal sig (hex signal
d036abbf
MS
102 signal number). If ;AA..AA is omitted,
103 resume at same address.
4cc1b3f7 104
0a325463 105 step with Ssig;AA..AA Like 'C' but step not continue.
4cc1b3f7
JK
106 signal
107
bd5635a1
RP
108 last signal ? Reply the current reason for stopping.
109 This is the same reply as is generated
110 for step or cont : SAA where AA is the
111 signal number.
112
b52cac6b
FF
113 detach D Reply OK.
114
bd5635a1
RP
115 There is no immediate reply to step or cont.
116 The reply comes when the machine stops.
4cc1b3f7 117 It is SAA AA is the signal number.
bd5635a1 118
4cc1b3f7 119 or... TAAn...:r...;n...:r...;n...:r...;
e50ebec8 120 AA = signal number
4cc1b3f7
JK
121 n... = register number (hex)
122 r... = register contents
123 n... = `thread'
124 r... = thread process ID. This is
125 a hex integer.
126 n... = other string not starting
127 with valid hex digit.
128 gdb should ignore this n,r pair
129 and go on to the next. This way
130 we can extend the protocol.
72bba93b 131 or... WAA The process exited, and AA is
758aeb93
ILT
132 the exit status. This is only
133 applicable for certains sorts of
134 targets.
4cc1b3f7
JK
135 or... XAA The process terminated with signal
136 AA.
998cfe7d 137 or... OXX..XX XX..XX is hex encoding of ASCII data. This
d036abbf
MS
138 can happen at any time while the
139 program is running and the debugger
140 should continue to wait for
141 'W', 'T', etc.
4cc1b3f7 142
2b576293
C
143 thread alive TXX Find out if the thread XX is alive.
144 reply OK thread is still alive
145 ENN thread is dead
146
147 remote restart RXX Restart the remote server
148
149 extended ops ! Use the extended remote protocol.
150 Sticky -- only needs to be set once.
151
d538b510
RP
152 kill request k
153
154 toggle debug d toggle debug flag (see 386 & 68k stubs)
155 reset r reset -- see sparc stub.
156 reserved <other> On other requests, the stub should
157 ignore the request and send an empty
158 response ($#<checksum>). This way
159 we can extend the protocol and GDB
160 can tell whether the stub it is
161 talking to uses the old or the new.
72bba93b 162 search tAA:PP,MM Search backwards starting at address
94d4b713
JK
163 AA for a match with pattern PP and
164 mask MM. PP and MM are 4 bytes.
165 Not supported by all stubs.
166
72bba93b
SG
167 general query qXXXX Request info about XXXX.
168 general set QXXXX=yyyy Set value of XXXX to yyyy.
169 query sect offs qOffsets Get section offsets. Reply is
170 Text=xxx;Data=yyy;Bss=zzz
72bba93b 171
94d4b713 172 Responses can be run-length encoded to save space. A '*' means that
284f4ee9 173 the next character is an ASCII encoding giving a repeat count which
94d4b713 174 stands for that many repititions of the character preceding the '*'.
284f4ee9
SC
175 The encoding is n+29, yielding a printable character where n >=3
176 (which is where rle starts to win). Don't use an n > 126.
177
178 So
179 "0* " means the same as "0000". */
bd5635a1 180
d747e0af 181#include "defs.h"
2b576293 182#include "gdb_string.h"
bd5635a1 183#include <fcntl.h>
bd5635a1
RP
184#include "frame.h"
185#include "inferior.h"
e50ebec8 186#include "bfd.h"
6b27ebe8 187#include "symfile.h"
bd5635a1
RP
188#include "target.h"
189#include "wait.h"
0d14c7df 190/*#include "terminal.h"*/
8f86a4e4 191#include "gdbcmd.h"
758aeb93
ILT
192#include "objfiles.h"
193#include "gdb-stabs.h"
cb1709ae 194#include "gdbthread.h"
bd5635a1 195
d538b510
RP
196#include "dcache.h"
197
bd5635a1
RP
198#ifdef USG
199#include <sys/types.h>
200#endif
201
202#include <signal.h>
ebdb9ade 203#include "serial.h"
bd5635a1 204
b543979c
JG
205/* Prototypes for local functions */
206
45993f61 207static int remote_write_bytes PARAMS ((CORE_ADDR memaddr,
43fc25c8 208 char *myaddr, int len));
b543979c 209
45993f61 210static int remote_read_bytes PARAMS ((CORE_ADDR memaddr,
43fc25c8 211 char *myaddr, int len));
b543979c 212
7517f04b 213static void remote_files_info PARAMS ((struct target_ops *ignore));
b543979c 214
4ef1f467 215static int remote_xfer_memory PARAMS ((CORE_ADDR memaddr, char * myaddr,
45993f61 216 int len, int should_write,
4ef1f467 217 struct target_ops * target));
b543979c 218
45993f61 219static void remote_prepare_to_store PARAMS ((void));
b543979c 220
45993f61 221static void remote_fetch_registers PARAMS ((int regno));
b543979c 222
45993f61
SC
223static void remote_resume PARAMS ((int pid, int step,
224 enum target_signal siggnal));
b543979c 225
45993f61 226static int remote_start_remote PARAMS ((char *dummy));
7c622b41 227
45993f61 228static void remote_open PARAMS ((char *name, int from_tty));
b543979c 229
2b576293
C
230static void extended_remote_open PARAMS ((char *name, int from_tty));
231
7517f04b 232static void remote_open_1 PARAMS ((char *, int, struct target_ops *,
d036abbf 233 int extended_p));
2b576293 234
45993f61 235static void remote_close PARAMS ((int quitting));
b543979c 236
45993f61 237static void remote_store_registers PARAMS ((int regno));
b543979c 238
2b576293
C
239static void remote_mourn PARAMS ((void));
240
241static void extended_remote_restart PARAMS ((void));
242
243static void extended_remote_mourn PARAMS ((void));
244
245static void extended_remote_create_inferior PARAMS ((char *, char *, char **));
246
247static void remote_mourn_1 PARAMS ((struct target_ops *));
248
45993f61 249static void remote_send PARAMS ((char *buf));
b543979c 250
45993f61 251static int readchar PARAMS ((int timeout));
b543979c 252
4ef1f467 253static int remote_wait PARAMS ((int pid, struct target_waitstatus * status));
b543979c 254
45993f61 255static void remote_kill PARAMS ((void));
b543979c 256
45993f61 257static int tohex PARAMS ((int nib));
b543979c 258
45993f61 259static void remote_detach PARAMS ((char *args, int from_tty));
5af4f5f6 260
45993f61 261static void remote_interrupt PARAMS ((int signo));
b543979c 262
45993f61 263static void interrupt_query PARAMS ((void));
981a3309 264
b607efe7
FF
265static void set_thread PARAMS ((int, int));
266
267static int remote_thread_alive PARAMS ((int));
268
269static void get_offsets PARAMS ((void));
270
271static int read_frame PARAMS ((char *));
272
273static int remote_insert_breakpoint PARAMS ((CORE_ADDR, char *));
274
275static int remote_remove_breakpoint PARAMS ((CORE_ADDR, char *));
276
dd0ce8f6
AC
277static int hexnumlen PARAMS ((ULONGEST num));
278
0e05ecee
SS
279static void init_remote_ops PARAMS ((void));
280
281static void init_extended_remote_ops PARAMS ((void));
282
2e7eeba9
MS
283static void remote_stop PARAMS ((void));
284
7517f04b
SS
285static int ishex PARAMS ((int ch, int *val));
286
287static int stubhex PARAMS ((int ch));
288
2bef6075
FN
289static int remote_query PARAMS ((char, char *, char *, int *));
290
2e7eeba9
MS
291static int hexnumstr PARAMS ((char *, ULONGEST));
292
293static CORE_ADDR remote_address_masked PARAMS ((CORE_ADDR));
294
295static void print_packet PARAMS ((char *));
296
297static unsigned long crc32 PARAMS ((unsigned char *, int, unsigned int));
298
299static void compare_sections_command PARAMS ((char *, int));
300
301static void packet_command PARAMS ((char *, int));
302
7517f04b
SS
303static int stub_unpack_int PARAMS ((char *buff, int fieldlength));
304
305char *unpack_varlen_hex PARAMS ((char *buff, int *result));
306
307static char *unpack_nibble PARAMS ((char *buf, int *val));
308
309static char *pack_nibble PARAMS ((char *buf, int nibble));
310
311static char *pack_hex_byte PARAMS ((char *pkt, unsigned char byte));
312
313static char *unpack_byte PARAMS ((char *buf, int *value));
314
315static char *pack_int PARAMS ((char *buf, int value));
316
317static char *unpack_int PARAMS ((char *buf, int *value));
318
319static char *unpack_string PARAMS ((char *src, char *dest, int length));
320
321static char *pack_threadid PARAMS ((char *pkt, threadref *id));
322
323static char *unpack_threadid PARAMS ((char *inbuf, threadref *id));
324
325void int_to_threadref PARAMS ((threadref *id, int value));
326
327static int threadref_to_int PARAMS ((threadref *ref));
328
329static void copy_threadref PARAMS ((threadref *dest, threadref *src));
330
331static int threadmatch PARAMS ((threadref *dest, threadref *src));
332
333static char *pack_threadinfo_request PARAMS ((char *pkt, int mode,
334 threadref *id));
335
336static int remote_unpack_thread_info_response PARAMS ((char *pkt,
337 threadref *expectedref,
338 struct gdb_ext_thread_info *info));
339
340
341static int remote_get_threadinfo PARAMS ((threadref *threadid,
342 int fieldset, /*TAG mask */
343 struct gdb_ext_thread_info *info));
344
345static int adapt_remote_get_threadinfo PARAMS ((gdb_threadref *ref,
346 int selection,
347 struct gdb_ext_thread_info *info));
348
349static char *pack_threadlist_request PARAMS ((char *pkt, int startflag,
350 int threadcount,
351 threadref *nextthread));
352
353static int parse_threadlist_response PARAMS ((char *pkt,
354 int result_limit,
355 threadref *original_echo,
356 threadref *resultlist,
357 int *doneflag));
358
359static int remote_get_threadlist PARAMS ((int startflag,
360 threadref *nextthread,
361 int result_limit,
362 int *done,
363 int *result_count,
364 threadref *threadlist));
365
366typedef int (*rmt_thread_action) (threadref *ref, void *context);
367
368static int remote_threadlist_iterator PARAMS ((rmt_thread_action stepfunction,
369 void *context, int looplimit));
370
371static int remote_newthread_step PARAMS ((threadref *ref, void *context));
372
373static int remote_current_thread PARAMS ((int oldpid));
374
375int remote_find_new_threads PARAMS ((void));
376
377static void record_currthread PARAMS ((int currthread));
378
379static void init_remote_threads PARAMS ((void));
380
dd0ce8f6
AC
381/* exported functions */
382
383extern int fromhex PARAMS ((int a));
2e7eeba9 384
dd0ce8f6 385extern void getpkt PARAMS ((char *buf, int forever));
2e7eeba9 386
dd0ce8f6
AC
387extern int putpkt PARAMS ((char *buf));
388
2e7eeba9
MS
389void remote_console_output PARAMS ((char *));
390
4ef1f467
DT
391/* Define the target subroutine names */
392
2e7eeba9
MS
393void open_remote_target PARAMS ((char *, int, struct target_ops *, int));
394
395void _initialize_remote PARAMS ((void));
396
397/* */
398
399static struct target_ops remote_ops;
c719b714 400
2e7eeba9 401static struct target_ops extended_remote_ops;
c719b714 402
7517f04b
SS
403static struct target_thread_vector remote_thread_vec;
404
ebdb9ade
JK
405/* This was 5 seconds, which is a long time to sit and wait.
406 Unless this is going though some terminal server or multiplexer or
407 other form of hairy serial connection, I would think 2 seconds would
408 be plenty. */
bd5635a1 409
cb1709ae
DP
410/* Changed to allow option to set timeout value.
411 was static int remote_timeout = 2; */
412extern int remote_timeout;
bd5635a1 413
b52cac6b
FF
414/* This variable chooses whether to send a ^C or a break when the user
415 requests program interruption. Although ^C is usually what remote
416 systems expect, and that is the default here, sometimes a break is
417 preferable instead. */
418
419static int remote_break;
420
2e7eeba9
MS
421/* Has the user attempted to interrupt the target? If so, then offer
422 the user the opportunity to bail out completely if he interrupts
423 again. */
424static int interrupted_already = 0;
425
16e1d1d3 426/* Descriptor for I/O to remote machine. Initialize it to NULL so that
bd5635a1
RP
427 remote_open knows that we don't have a file open when the program
428 starts. */
0a325463 429static serial_t remote_desc = NULL;
bd5635a1 430
4d57c599
JK
431/* Having this larger than 400 causes us to be incompatible with m68k-stub.c
432 and i386-stub.c. Normally, no one would notice because it only matters
433 for writing large chunks of memory (e.g. in downloads). Also, this needs
434 to be more than 400 if required to hold the registers (see below, where
435 we round it up based on REGISTER_BYTES). */
436#define PBUFSIZ 400
bd5635a1
RP
437
438/* Maximum number of bytes to read/write at once. The value here
439 is chosen to fill up a packet (the headers account for the 32). */
440#define MAXBUFBYTES ((PBUFSIZ-32)/2)
441
b543979c 442/* Round up PBUFSIZ to hold all the registers, at least. */
2ddeed27
JK
443/* The blank line after the #if seems to be required to work around a
444 bug in HP's PA compiler. */
b543979c 445#if REGISTER_BYTES > MAXBUFBYTES
2ddeed27
JK
446
447#undef PBUFSIZ
b543979c 448#define PBUFSIZ (REGISTER_BYTES * 2 + 32)
bd5635a1 449#endif
4aa6fe10 450
4ef1f467 451
fea17b55
SS
452/* This variable sets the number of bytes to be written to the target
453 in a single packet. Normally PBUFSIZ is satisfactory, but some
454 targets need smaller values (perhaps because the receiving end
455 is slow). */
456
457static int remote_write_size = PBUFSIZ;
458
2e7eeba9
MS
459/* This variable sets the number of bits in an address that are to be
460 sent in a memory ("M" or "m") packet. Normally, after stripping
461 leading zeros, the entire address would be sent. This variable
462 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
463 initial implementation of remote.c restricted the address sent in
464 memory packets to ``host::sizeof long'' bytes - (typically 32
465 bits). Consequently, for 64 bit targets, the upper 32 bits of an
466 address was never sent. Since fixing this bug may cause a break in
467 some remote targets this variable is principly provided to
468 facilitate backward compatibility. */
469
470static int remote_address_size;
471
0a325463
SG
472/* This is the size (in chars) of the first response to the `g' command. This
473 is used to limit the size of the memory read and write commands to prevent
dd0ce8f6
AC
474 stub buffers from overflowing. The size does not include headers and
475 trailers, it is only the payload size. */
0a325463
SG
476
477static int remote_register_buf_size = 0;
478
4aa6fe10
JK
479/* Should we try the 'P' request? If this is set to one when the stub
480 doesn't support 'P', the only consequence is some unnecessary traffic. */
481static int stub_supports_P = 1;
482
0a325463
SG
483/* These are pointers to hook functions that may be set in order to
484 modify resume/wait behavior for a particular architecture. */
485
486void (*target_resume_hook) PARAMS ((void));
487void (*target_wait_loop_hook) PARAMS ((void));
488
4cc1b3f7 489\f
4ef1f467 490
d036abbf
MS
491/* These are the threads which we last sent to the remote system.
492 -1 for all or -2 for not sent yet. */
b53a1514
MS
493static int general_thread;
494static int cont_thread;
4cc1b3f7 495
4ef1f467
DT
496/* Call this function as a result of
497 1) A halt indication (T packet) containing a thread id
498 2) A direct query of currthread
499 3) Successful execution of set thread
500 */
501
502static void
503record_currthread (currthread)
504 int currthread;
505{
b53a1514 506#if 0 /* target_wait must not modify inferior_pid! */
4ef1f467 507 inferior_pid = currthread;
b53a1514 508#endif
4ef1f467 509 general_thread = currthread;
b53a1514
MS
510#if 0 /* setting cont_thread has a different meaning
511 from having the target report its thread id. */
4ef1f467 512 cont_thread = currthread;
b53a1514
MS
513#endif
514 /* If this is a new thread, add it to GDB's thread list.
515 If we leave it up to WFI to do this, bad things will happen. */
516 if (!in_thread_list (currthread))
517 add_thread (currthread);
4ef1f467
DT
518}
519
b53a1514
MS
520#define MAGIC_NULL_PID 42000
521
4cc1b3f7
JK
522static void
523set_thread (th, gen)
524 int th;
525 int gen;
526{
527 char buf[PBUFSIZ];
528 int state = gen ? general_thread : cont_thread;
d036abbf 529
4cc1b3f7
JK
530 if (state == th)
531 return;
d036abbf 532
4cc1b3f7
JK
533 buf[0] = 'H';
534 buf[1] = gen ? 'g' : 'c';
b53a1514 535 if (th == MAGIC_NULL_PID)
4cc1b3f7
JK
536 {
537 buf[2] = '0';
538 buf[3] = '\0';
539 }
540 else if (th < 0)
541 sprintf (&buf[2], "-%x", -th);
542 else
543 sprintf (&buf[2], "%x", th);
544 putpkt (buf);
545 getpkt (buf, 0);
546 if (gen)
547 general_thread = th;
548 else
549 cont_thread = th;
550}
bd5635a1 551\f
2b576293 552/* Return nonzero if the thread TH is still alive on the remote system. */
43fc25c8
JL
553
554static int
555remote_thread_alive (th)
556 int th;
557{
558 char buf[PBUFSIZ];
559
560 buf[0] = 'T';
561 if (th < 0)
4ef1f467 562 sprintf (&buf[1], "-%08x", -th);
43fc25c8 563 else
4ef1f467 564 sprintf (&buf[1], "%08x", th);
43fc25c8
JL
565 putpkt (buf);
566 getpkt (buf, 0);
567 return (buf[0] == 'O' && buf[1] == 'K');
568}
2b576293 569
7517f04b
SS
570/* About these extended threadlist and threadinfo packets. They are
571 variable length packets but, the fields within them are often fixed
572 length. They are redundent enough to send over UDP as is the
573 remote protocol in general. There is a matching unit test module
574 in libstub. */
4ef1f467
DT
575
576#define BUF_THREAD_ID_SIZE (OPAQUETHREADBYTES*2)
4ef1f467 577
7517f04b 578/* encode 64 bits in 16 chars of hex */
4ef1f467
DT
579
580static const char hexchars[] = "0123456789abcdef";
581
582static int
583ishex (ch, val)
7517f04b 584 int ch;
4ef1f467
DT
585 int *val;
586{
587 if ((ch >= 'a') && (ch <= 'f'))
588 {
589 *val = ch - 'a' + 10;
590 return 1;
591 }
592 if ((ch >= 'A') && (ch <= 'F'))
593 {
594 *val = ch - 'A' + 10;
595 return 1;
596 }
597 if ((ch >= '0') && (ch <= '9'))
598 {
599 *val = ch - '0';
600 return 1;
601 }
602 return 0;
603}
604
605static int
606stubhex (ch)
7517f04b 607 int ch;
4ef1f467
DT
608{
609 if (ch >= 'a' && ch <= 'f')
610 return ch - 'a' + 10;
611 if (ch >= '0' && ch <= '9')
612 return ch - '0';
613 if (ch >= 'A' && ch <= 'F')
614 return ch - 'A' + 10;
615 return -1;
616}
617
618static int
619stub_unpack_int (buff, fieldlength)
620 char *buff;
621 int fieldlength;
622{
4ef1f467 623 int nibble;
7517f04b
SS
624 int retval = 0;
625
4ef1f467
DT
626 while (fieldlength)
627 {
628 nibble = stubhex (*buff++);
629 retval |= nibble;
630 fieldlength--;
631 if (fieldlength)
632 retval = retval << 4;
633 }
634 return retval;
635}
636
637char *
638unpack_varlen_hex (buff, result)
639 char *buff; /* packet to parse */
640 int *result;
641{
642 int nibble;
7517f04b 643 int retval = 0;
4ef1f467
DT
644
645 while (ishex (*buff, &nibble))
646 {
647 buff++;
648 retval = retval << 4;
649 retval |= nibble & 0x0f;
650 }
651 *result = retval;
652 return buff;
653}
654
655static char *
656unpack_nibble (buf, val)
657 char *buf;
658 int *val;
4ef1f467
DT
659{
660 ishex (*buf++, val);
661 return buf;
662}
663
664static char *
665pack_nibble (buf, nibble)
666 char *buf;
667 int nibble;
4ef1f467
DT
668{
669 *buf++ = hexchars[(nibble & 0x0f)];
670 return buf;
671}
672
673static char *
674pack_hex_byte (pkt, byte)
675 char *pkt;
676 unsigned char byte;
677{
678 *pkt++ = hexchars[(byte >> 4) & 0xf];
679 *pkt++ = hexchars[(byte & 0xf)];
680 return pkt;
681}
682
683static char *
684unpack_byte (buf, value)
685 char *buf;
686 int *value;
687{
688 *value = stub_unpack_int (buf, 2);
689 return buf + 2;
690}
691
4ef1f467
DT
692static char *
693pack_int (buf, value)
694 char *buf;
695 int value;
696{
697 buf = pack_hex_byte (buf, (value >> 24) & 0xff);
698 buf = pack_hex_byte (buf, (value >> 16) & 0xff);
699 buf = pack_hex_byte (buf, (value >> 8) & 0x0ff);
700 buf = pack_hex_byte (buf, (value & 0xff));
701 return buf;
702}
703
4ef1f467
DT
704static char *
705unpack_int (buf, value)
706 char *buf;
707 int *value;
708{
709 *value = stub_unpack_int (buf, 8);
710 return buf + 8;
711}
712
7517f04b
SS
713#if 0 /* currently unused, uncomment when needed */
714static char *pack_string PARAMS ((char *pkt, char *string));
4ef1f467
DT
715
716static char *
717pack_string (pkt, string)
718 char *pkt;
719 char *string;
720{
721 char ch;
722 int len;
7517f04b 723
4ef1f467
DT
724 len = strlen (string);
725 if (len > 200)
726 len = 200; /* Bigger than most GDB packets, junk??? */
727 pkt = pack_hex_byte (pkt, len);
728 while (len-- > 0)
729 {
730 ch = *string++;
731 if ((ch == '\0') || (ch == '#'))
732 ch = '*'; /* Protect encapsulation */
733 *pkt++ = ch;
734 }
735 return pkt;
736}
7517f04b 737#endif /* 0 (unused) */
4ef1f467
DT
738
739static char *
740unpack_string (src, dest, length)
741 char *src;
742 char *dest;
743 int length;
744{
745 while (length--)
746 *dest++ = *src++;
747 *dest = '\0';
748 return src;
749}
750
751static char *
752pack_threadid (pkt, id)
753 char *pkt;
754 threadref *id;
755{
756 char *limit;
757 unsigned char *altid;
7517f04b 758
4ef1f467
DT
759 altid = (unsigned char *) id;
760 limit = pkt + BUF_THREAD_ID_SIZE;
761 while (pkt < limit)
762 pkt = pack_hex_byte (pkt, *altid++);
763 return pkt;
764}
765
766
767static char *
768unpack_threadid (inbuf, id)
769 char *inbuf;
770 threadref *id;
771{
772 char *altref;
773 char *limit = inbuf + BUF_THREAD_ID_SIZE;
774 int x, y;
7517f04b 775
4ef1f467
DT
776 altref = (char *) id;
777
778 while (inbuf < limit)
779 {
780 x = stubhex (*inbuf++);
781 y = stubhex (*inbuf++);
782 *altref++ = (x << 4) | y;
783 }
784 return inbuf;
785}
786
787/* Externally, threadrefs are 64 bits but internally, they are still
7517f04b
SS
788 ints. This is due to a mismatch of specifications. We would like
789 to use 64bit thread references internally. This is an adapter
790 function. */
4ef1f467
DT
791
792void
793int_to_threadref (id, value)
794 threadref *id;
795 int value;
796{
797 unsigned char *scan;
7517f04b 798
4ef1f467
DT
799 scan = (unsigned char *) id;
800 {
801 int i = 4;
802 while (i--)
803 *scan++ = 0;
804 }
805 *scan++ = (value >> 24) & 0xff;
806 *scan++ = (value >> 16) & 0xff;
807 *scan++ = (value >> 8) & 0xff;
808 *scan++ = (value & 0xff);
809}
810
7517f04b 811static int
4ef1f467
DT
812threadref_to_int (ref)
813 threadref *ref;
814{
7517f04b 815 int i, value = 0;
4ef1f467 816 unsigned char *scan;
4ef1f467
DT
817
818 scan = (char *) ref;
819 scan += 4;
820 i = 4;
821 while (i-- > 0)
822 value = (value << 8) | ((*scan++) & 0xff);
823 return value;
824}
825
826static void
827copy_threadref (dest, src)
828 threadref *dest;
829 threadref *src;
830{
831 int i;
832 unsigned char *csrc, *cdest;
7517f04b 833
4ef1f467
DT
834 csrc = (unsigned char *) src;
835 cdest = (unsigned char *) dest;
836 i = 8;
837 while (i--)
838 *cdest++ = *csrc++;
839}
840
4ef1f467
DT
841static int
842threadmatch (dest, src)
843 threadref *dest;
844 threadref *src;
845{
846 /* things are broken right now, so just assume we got a match */
847#if 0
848 unsigned char *srcp, *destp;
849 int i, result;
850 srcp = (char *) src;
851 destp = (char *) dest;
852
853 result = 1;
854 while (i-- > 0)
855 result &= (*srcp++ == *destp++) ? 1 : 0;
856 return result;
857#endif
858 return 1;
859}
860
4ef1f467
DT
861/*
862 threadid:1, # always request threadid
863 context_exists:2,
864 display:4,
865 unique_name:8,
866 more_display:16
867*/
868
869/* Encoding: 'Q':8,'P':8,mask:32,threadid:64 */
870
4ef1f467
DT
871static char *
872pack_threadinfo_request (pkt, mode, id)
873 char *pkt;
874 int mode;
875 threadref *id;
876{
4ef1f467
DT
877 *pkt++ = 'q'; /* Info Query */
878 *pkt++ = 'P'; /* process or thread info */
879 pkt = pack_int (pkt, mode); /* mode */
880 pkt = pack_threadid (pkt, id); /* threadid */
881 *pkt = '\0'; /* terminate */
4ef1f467
DT
882 return pkt;
883}
884
4ef1f467
DT
885/* These values tag the fields in a thread info response packet */
886/* Tagging the fields allows us to request specific fields and to
887 add more fields as time goes by */
7517f04b 888
4ef1f467 889#define TAG_THREADID 1 /* Echo the thread identifier */
7517f04b 890#define TAG_EXISTS 2 /* Is this process defined enough to
4ef1f467
DT
891 fetch registers and its stack */
892#define TAG_DISPLAY 4 /* A short thing maybe to put on a window */
893#define TAG_THREADNAME 8 /* string, maps 1-to-1 with a thread is */
d036abbf
MS
894#define TAG_MOREDISPLAY 16 /* Whatever the kernel wants to say about
895 the process*/
4ef1f467 896
4ef1f467
DT
897static int
898remote_unpack_thread_info_response (pkt, expectedref, info)
899 char *pkt;
900 threadref *expectedref;
901 struct gdb_ext_thread_info *info;
902{
903 int mask, length;
904 unsigned int tag;
905 threadref ref;
906 char *limit = pkt + PBUFSIZ; /* plausable parsing limit */
907 int retval = 1;
908
4ef1f467
DT
909 /* info->threadid = 0; FIXME: implement zero_threadref */
910 info->active = 0;
911 info->display[0] = '\0';
912 info->shortname[0] = '\0';
913 info->more_display[0] = '\0';
914
915 /* Assume the characters indicating the packet type have been stripped */
916 pkt = unpack_int (pkt, &mask); /* arg mask */
917 pkt = unpack_threadid (pkt, &ref);
918
919 if (mask == 0)
7517f04b 920 warning ("Incomplete response to threadinfo request\n");
4ef1f467
DT
921 if (!threadmatch (&ref, expectedref))
922 { /* This is an answer to a different request */
7517f04b 923 warning ("ERROR RMT Thread info mismatch\n");
4ef1f467
DT
924 return 0;
925 }
926 copy_threadref (&info->threadid, &ref);
927
928 /* Loop on tagged fields , try to bail if somthing goes wrong */
929
d036abbf 930 while ((pkt < limit) && mask && *pkt) /* packets are terminated with nulls */
4ef1f467
DT
931 {
932 pkt = unpack_int (pkt, &tag); /* tag */
933 pkt = unpack_byte (pkt, &length); /* length */
934 if (!(tag & mask)) /* tags out of synch with mask */
935 {
936 warning ("ERROR RMT: threadinfo tag mismatch\n");
937 retval = 0;
938 break;
939 }
940 if (tag == TAG_THREADID)
941 {
942 if (length != 16)
943 {
944 warning ("ERROR RMT: length of threadid is not 16\n");
945 retval = 0;
946 break;
947 }
948 pkt = unpack_threadid (pkt, &ref);
949 mask = mask & ~TAG_THREADID;
950 continue;
951 }
952 if (tag == TAG_EXISTS)
953 {
954 info->active = stub_unpack_int (pkt, length);
955 pkt += length;
956 mask = mask & ~(TAG_EXISTS);
957 if (length > 8)
958 {
959 warning ("ERROR RMT: 'exists' length too long\n");
960 retval = 0;
961 break;
962 }
963 continue;
964 }
965 if (tag == TAG_THREADNAME)
966 {
967 pkt = unpack_string (pkt, &info->shortname[0], length);
968 mask = mask & ~TAG_THREADNAME;
969 continue;
970 }
971 if (tag == TAG_DISPLAY)
972 {
973 pkt = unpack_string (pkt, &info->display[0], length);
974 mask = mask & ~TAG_DISPLAY;
975 continue;
976 }
977 if (tag == TAG_MOREDISPLAY)
978 {
979 pkt = unpack_string (pkt, &info->more_display[0], length);
980 mask = mask & ~TAG_MOREDISPLAY;
981 continue;
982 }
983 warning ("ERROR RMT: unknown thread info tag\n");
984 break; /* Not a tag we know about */
985 }
986 return retval;
987}
988
7517f04b 989static int
4ef1f467
DT
990remote_get_threadinfo (threadid, fieldset, info)
991 threadref *threadid;
992 int fieldset; /* TAG mask */
993 struct gdb_ext_thread_info *info;
994{
995 int result;
996 char threadinfo_pkt[PBUFSIZ];
7517f04b 997
4ef1f467
DT
998 pack_threadinfo_request (threadinfo_pkt, fieldset, threadid);
999 putpkt (threadinfo_pkt);
1000 getpkt (threadinfo_pkt, 0);
7517f04b
SS
1001 result = remote_unpack_thread_info_response (threadinfo_pkt + 2, threadid,
1002 info);
4ef1f467
DT
1003 return result;
1004}
1005
7517f04b 1006/* Unfortunately, 61 bit thread-ids are bigger than the internal
4ef1f467
DT
1007 representation of a threadid. */
1008
7517f04b 1009static int
4ef1f467
DT
1010adapt_remote_get_threadinfo (ref, selection, info)
1011 gdb_threadref *ref;
1012 int selection;
1013 struct gdb_ext_thread_info *info;
1014{
1015 threadref lclref;
7517f04b 1016
4ef1f467
DT
1017 int_to_threadref (&lclref, *ref);
1018 return remote_get_threadinfo (&lclref, selection, info);
1019}
1020
4ef1f467
DT
1021/* Format: i'Q':8,i"L":8,initflag:8,batchsize:16,lastthreadid:32 */
1022
1023static char *
1024pack_threadlist_request (pkt, startflag, threadcount, nextthread)
1025 char *pkt;
1026 int startflag;
1027 int threadcount;
1028 threadref *nextthread;
1029{
1030 *pkt++ = 'q'; /* info query packet */
1031 *pkt++ = 'L'; /* Process LIST or threadLIST request */
1032 pkt = pack_nibble (pkt, startflag); /* initflag 1 bytes */
1033 pkt = pack_hex_byte (pkt, threadcount); /* threadcount 2 bytes */
1034 pkt = pack_threadid (pkt, nextthread); /* 64 bit thread identifier */
1035 *pkt = '\0';
1036 return pkt;
1037}
1038
4ef1f467
DT
1039/* Encoding: 'q':8,'M':8,count:16,done:8,argthreadid:64,(threadid:64)* */
1040
4ef1f467 1041static int
7517f04b
SS
1042parse_threadlist_response (pkt, result_limit, original_echo, resultlist,
1043 doneflag)
4ef1f467
DT
1044 char *pkt;
1045 int result_limit;
1046 threadref *original_echo;
1047 threadref *resultlist;
1048 int *doneflag;
1049{
1050 char *limit;
1051 int count, resultcount, done;
4ef1f467 1052
7517f04b
SS
1053 resultcount = 0;
1054 /* Assume the 'q' and 'M chars have been stripped. */
4ef1f467
DT
1055 limit = pkt + (PBUFSIZ - BUF_THREAD_ID_SIZE); /* done parse past here */
1056 pkt = unpack_byte (pkt, &count); /* count field */
1057 pkt = unpack_nibble (pkt, &done);
7517f04b 1058 /* The first threadid is the argument threadid. */
4ef1f467
DT
1059 pkt = unpack_threadid (pkt, original_echo); /* should match query packet */
1060 while ((count-- > 0) && (pkt < limit))
1061 {
1062 pkt = unpack_threadid (pkt, resultlist++);
1063 if (resultcount++ >= result_limit)
1064 break;
1065 }
1066 if (doneflag)
1067 *doneflag = done;
7517f04b 1068 return resultcount;
4ef1f467
DT
1069}
1070
4ef1f467
DT
1071static int
1072remote_get_threadlist (startflag, nextthread, result_limit,
1073 done, result_count, threadlist)
1074 int startflag;
1075 threadref *nextthread;
1076 int result_limit;
1077 int *done;
1078 int *result_count;
1079 threadref *threadlist;
1080
1081{
1082 static threadref echo_nextthread;
1083 char threadlist_packet[PBUFSIZ];
1084 char t_response[PBUFSIZ];
1085 int result = 1;
1086
1087 /* Trancate result limit to be smaller than the packet size */
1088 if ((((result_limit + 1) * BUF_THREAD_ID_SIZE) + 10) >= PBUFSIZ)
1089 result_limit = (PBUFSIZ / BUF_THREAD_ID_SIZE) - 2;
1090
1091 pack_threadlist_request (threadlist_packet,
1092 startflag, result_limit, nextthread);
1093 putpkt (threadlist_packet);
1094 getpkt (t_response, 0);
7517f04b
SS
1095
1096 *result_count =
1097 parse_threadlist_response (t_response + 2, result_limit, &echo_nextthread,
1098 threadlist, done);
1099
4ef1f467
DT
1100 if (!threadmatch (&echo_nextthread, nextthread))
1101 {
1102 /* FIXME: This is a good reason to drop the packet */
1103 /* Possably, there is a duplicate response */
1104 /* Possabilities :
1105 retransmit immediatly - race conditions
1106 retransmit after timeout - yes
1107 exit
1108 wait for packet, then exit
1109 */
1110 warning ("HMM: threadlist did not echo arg thread, dropping it\n");
1111 return 0; /* I choose simply exiting */
1112 }
1113 if (*result_count <= 0)
1114 {
1115 if (*done != 1)
1116 {
1117 warning ("RMT ERROR : failed to get remote thread list\n");
1118 result = 0;
1119 }
1120 return result; /* break; */
1121 }
1122 if (*result_count > result_limit)
1123 {
1124 *result_count = 0;
1125 warning ("RMT ERROR: threadlist response longer than requested\n");
1126 return 0;
1127 }
1128 return result;
1129}
1130
4ef1f467 1131/* This is the interface between remote and threads, remotes upper interface */
7517f04b
SS
1132
1133/* remote_find_new_threads retrieves the thread list and for each
4ef1f467 1134 thread in the list, looks up the thread in GDB's internal list,
7517f04b
SS
1135 ading the thread if it does not already exist. This involves
1136 getting partial thread lists from the remote target so, polling the
1137 quit_flag is required. */
4ef1f467 1138
4ef1f467 1139
7517f04b 1140/* About this many threadisds fit in a packet. */
4ef1f467 1141
7517f04b 1142#define MAXTHREADLISTRESULTS 32
4ef1f467
DT
1143
1144static int
1145remote_threadlist_iterator (stepfunction, context, looplimit)
1146 rmt_thread_action stepfunction;
1147 void *context;
1148 int looplimit;
1149{
1150 int done, i, result_count;
1151 int startflag = 1;
1152 int result = 1;
1153 int loopcount = 0;
1154 static threadref nextthread;
4ef1f467
DT
1155 static threadref resultthreadlist[MAXTHREADLISTRESULTS];
1156
1157 done = 0;
1158 while (!done)
1159 {
1160 if (loopcount++ > looplimit)
1161 {
1162 result = 0;
1163 warning ("Remote fetch threadlist -infinite loop-\n");
1164 break;
1165 }
7517f04b
SS
1166 if (!remote_get_threadlist (startflag, &nextthread, MAXTHREADLISTRESULTS,
1167 &done, &result_count, resultthreadlist))
4ef1f467
DT
1168 {
1169 result = 0;
1170 break;
1171 }
7517f04b
SS
1172 /* clear for later iterations */
1173 startflag = 0;
1174 /* Setup to resume next batch of thread references, set nextthread. */
4ef1f467
DT
1175 if (result_count >= 1)
1176 copy_threadref (&nextthread, &resultthreadlist[result_count - 1]);
4ef1f467
DT
1177 i = 0;
1178 while (result_count--)
1179 if (!(result = (*stepfunction) (&resultthreadlist[i++], context)))
1180 break;
1181 }
1182 return result;
1183}
1184
4ef1f467
DT
1185static int
1186remote_newthread_step (ref, context)
1187 threadref *ref;
d036abbf 1188 void *context;
4ef1f467
DT
1189{
1190 int pid;
7517f04b 1191
4ef1f467
DT
1192 pid = threadref_to_int (ref);
1193 if (!in_thread_list (pid))
1194 add_thread (pid);
1195 return 1; /* continue iterator */
1196}
1197
1198#define CRAZY_MAX_THREADS 1000
1199
7517f04b
SS
1200static int
1201remote_current_thread (oldpid)
1202 int oldpid;
b53a1514
MS
1203{
1204 char buf[PBUFSIZ];
1205
1206 putpkt ("qC");
1207 getpkt (buf, 0);
1208 if (buf[0] == 'Q' && buf[1] == 'C')
1209 return strtol (&buf[2], NULL, 16);
1210 else
1211 return oldpid;
1212}
1213
4ef1f467 1214int
7517f04b 1215remote_find_new_threads ()
4ef1f467 1216{
d036abbf
MS
1217 int ret;
1218
1219 ret = remote_threadlist_iterator (remote_newthread_step, 0,
1220 CRAZY_MAX_THREADS);
b53a1514
MS
1221 if (inferior_pid == MAGIC_NULL_PID) /* ack ack ack */
1222 inferior_pid = remote_current_thread (inferior_pid);
d036abbf 1223 return ret;
4ef1f467
DT
1224}
1225
4ef1f467 1226/* Initialize the thread vector which is used by threads.c */
7517f04b 1227/* The thread stub is a package, it has an initializer */
4ef1f467
DT
1228
1229static void
7517f04b 1230init_remote_threads ()
4ef1f467 1231{
7517f04b
SS
1232 remote_thread_vec.find_new_threads = remote_find_new_threads;
1233 remote_thread_vec.get_thread_info = adapt_remote_get_threadinfo;
4ef1f467
DT
1234}
1235
4ef1f467 1236\f
2b576293
C
1237/* Restart the remote side; this is an extended protocol operation. */
1238
1239static void
1240extended_remote_restart ()
1241{
1242 char buf[PBUFSIZ];
1243
1244 /* Send the restart command; for reasons I don't understand the
1245 remote side really expects a number after the "R". */
1246 buf[0] = 'R';
1247 sprintf (&buf[1], "%x", 0);
1248 putpkt (buf);
1249
1250 /* Now query for status so this looks just like we restarted
1251 gdbserver from scratch. */
1252 putpkt ("?");
1253 getpkt (buf, 0);
1254}
43fc25c8 1255\f
bd5635a1
RP
1256/* Clean up connection to a remote debugger. */
1257
e1ce8aa5 1258/* ARGSUSED */
b543979c 1259static void
bd5635a1
RP
1260remote_close (quitting)
1261 int quitting;
1262{
ebdb9ade
JK
1263 if (remote_desc)
1264 SERIAL_CLOSE (remote_desc);
1265 remote_desc = NULL;
b543979c
JG
1266}
1267
72bba93b
SG
1268/* Query the remote side for the text, data and bss offsets. */
1269
1270static void
1271get_offsets ()
1272{
dd0ce8f6
AC
1273 char buf[PBUFSIZ], *ptr;
1274 int lose;
72bba93b
SG
1275 CORE_ADDR text_addr, data_addr, bss_addr;
1276 struct section_offsets *offs;
1277
1278 putpkt ("qOffsets");
1279
1c95d7ab 1280 getpkt (buf, 0);
72bba93b 1281
1c95d7ab 1282 if (buf[0] == '\000')
d036abbf
MS
1283 return; /* Return silently. Stub doesn't support
1284 this command. */
72bba93b
SG
1285 if (buf[0] == 'E')
1286 {
1287 warning ("Remote failure reply: %s", buf);
1288 return;
1289 }
1290
dd0ce8f6
AC
1291 /* Pick up each field in turn. This used to be done with scanf, but
1292 scanf will make trouble if CORE_ADDR size doesn't match
1293 conversion directives correctly. The following code will work
1294 with any size of CORE_ADDR. */
1295 text_addr = data_addr = bss_addr = 0;
1296 ptr = buf;
1297 lose = 0;
1298
1299 if (strncmp (ptr, "Text=", 5) == 0)
1300 {
1301 ptr += 5;
1302 /* Don't use strtol, could lose on big values. */
1303 while (*ptr && *ptr != ';')
1304 text_addr = (text_addr << 4) + fromhex (*ptr++);
1305 }
1306 else
1307 lose = 1;
1308
1309 if (!lose && strncmp (ptr, ";Data=", 6) == 0)
1310 {
1311 ptr += 6;
1312 while (*ptr && *ptr != ';')
1313 data_addr = (data_addr << 4) + fromhex (*ptr++);
1314 }
1315 else
1316 lose = 1;
1317
1318 if (!lose && strncmp (ptr, ";Bss=", 5) == 0)
1319 {
1320 ptr += 5;
1321 while (*ptr && *ptr != ';')
1322 bss_addr = (bss_addr << 4) + fromhex (*ptr++);
1323 }
1324 else
1325 lose = 1;
1326
1327 if (lose)
72bba93b
SG
1328 error ("Malformed response to offset query, %s", buf);
1329
1330 if (symfile_objfile == NULL)
1331 return;
1332
1333 offs = (struct section_offsets *) alloca (sizeof (struct section_offsets)
1334 + symfile_objfile->num_sections
1335 * sizeof (offs->offsets));
1336 memcpy (offs, symfile_objfile->section_offsets,
1337 sizeof (struct section_offsets)
1338 + symfile_objfile->num_sections
1339 * sizeof (offs->offsets));
1340
1341 ANOFFSET (offs, SECT_OFF_TEXT) = text_addr;
1624c38f
SG
1342
1343 /* This is a temporary kludge to force data and bss to use the same offsets
1344 because that's what nlmconv does now. The real solution requires changes
1345 to the stub and remote.c that I don't have time to do right now. */
1346
72bba93b 1347 ANOFFSET (offs, SECT_OFF_DATA) = data_addr;
1624c38f 1348 ANOFFSET (offs, SECT_OFF_BSS) = data_addr;
72bba93b
SG
1349
1350 objfile_relocate (symfile_objfile, offs);
1351}
1352
7c622b41
JG
1353/* Stub for catch_errors. */
1354
1355static int
1356remote_start_remote (dummy)
1357 char *dummy;
1358{
ac7a377f
JK
1359 immediate_quit = 1; /* Allow user to interrupt it */
1360
7c622b41 1361 /* Ack any packet which the remote side has already sent. */
72bba93b
SG
1362 SERIAL_WRITE (remote_desc, "+", 1);
1363
4cc1b3f7
JK
1364 /* Let the stub know that we want it to return the thread. */
1365 set_thread (-1, 0);
1366
b53a1514
MS
1367 inferior_pid = remote_current_thread (inferior_pid);
1368
72bba93b
SG
1369 get_offsets (); /* Get text, data & bss offsets */
1370
7c622b41 1371 putpkt ("?"); /* initiate a query from remote machine */
ac7a377f 1372 immediate_quit = 0;
7c622b41
JG
1373
1374 start_remote (); /* Initialize gdb process mechanisms */
1375 return 1;
1376}
1377
bd5635a1
RP
1378/* Open a connection to a remote debugger.
1379 NAME is the filename used for communication. */
1380
2b576293
C
1381static void
1382remote_open (name, from_tty)
1383 char *name;
1384 int from_tty;
1385{
dd0ce8f6 1386 remote_open_1 (name, from_tty, &remote_ops, 0);
2b576293
C
1387}
1388
1389/* Open a connection to a remote debugger using the extended
935e77f5 1390 remote gdb protocol. NAME is the filename used for communication. */
2b576293
C
1391
1392static void
1393extended_remote_open (name, from_tty)
1394 char *name;
1395 int from_tty;
1396{
dd0ce8f6 1397 remote_open_1 (name, from_tty, &extended_remote_ops, 1/*extended_p*/);
2b576293
C
1398}
1399
1400/* Generic code for opening a connection to a remote target. */
7517f04b 1401
d538b510
RP
1402static DCACHE *remote_dcache;
1403
b543979c 1404static void
dd0ce8f6 1405remote_open_1 (name, from_tty, target, extended_p)
bd5635a1
RP
1406 char *name;
1407 int from_tty;
2b576293 1408 struct target_ops *target;
dd0ce8f6 1409 int extended_p;
bd5635a1 1410{
bd5635a1 1411 if (name == 0)
d036abbf
MS
1412 error ("To open a remote debug connection, you need to specify what\n\
1413serial device is attached to the remote system (e.g. /dev/ttya).");
bd5635a1 1414
f2fc6e7a
JK
1415 target_preopen (from_tty);
1416
2b576293 1417 unpush_target (target);
bd5635a1 1418
d538b510 1419 remote_dcache = dcache_init (remote_read_bytes, remote_write_bytes);
bd5635a1 1420
ebdb9ade
JK
1421 remote_desc = SERIAL_OPEN (name);
1422 if (!remote_desc)
bd5635a1
RP
1423 perror_with_name (name);
1424
94d4b713 1425 if (baud_rate != -1)
b543979c 1426 {
94d4b713
JK
1427 if (SERIAL_SETBAUDRATE (remote_desc, baud_rate))
1428 {
1429 SERIAL_CLOSE (remote_desc);
1430 perror_with_name (name);
1431 }
b543979c 1432 }
ebdb9ade 1433
45993f61 1434
ebdb9ade 1435 SERIAL_RAW (remote_desc);
bd5635a1 1436
e15f2a54
JK
1437 /* If there is something sitting in the buffer we might take it as a
1438 response to a command, which would be bad. */
1439 SERIAL_FLUSH_INPUT (remote_desc);
1440
bd5635a1 1441 if (from_tty)
7c622b41
JG
1442 {
1443 puts_filtered ("Remote debugging using ");
1444 puts_filtered (name);
1445 puts_filtered ("\n");
1446 }
2b576293 1447 push_target (target); /* Switch to using remote target now */
7517f04b 1448
4ef1f467
DT
1449 /* The target vector does not have the thread functions in it yet,
1450 so we use this function to call back into the thread module and
1451 register the thread vector and its contained functions. */
7517f04b
SS
1452 bind_target_thread_vector (&remote_thread_vec);
1453
1454 /* Start out by trying the 'P' request to set registers. We set
1455 this each time that we open a new target so that if the user
1456 switches from one stub to another, we can (if the target is
1457 closed and reopened) cope. */
4aa6fe10
JK
1458 stub_supports_P = 1;
1459
4cc1b3f7
JK
1460 general_thread = -2;
1461 cont_thread = -2;
1462
7517f04b
SS
1463 /* Without this, some commands which require an active target (such
1464 as kill) won't work. This variable serves (at least) double duty
1465 as both the pid of the target process (if it has such), and as a
1466 flag indicating that a target is active. These functions should
1467 be split out into seperate variables, especially since GDB will
1468 someday have a notion of debugging several processes. */
a1e0ba7a 1469
b53a1514 1470 inferior_pid = MAGIC_NULL_PID;
4fb7359d
SG
1471 /* Start the remote connection; if error (0), discard this target.
1472 In particular, if the user quits, be sure to discard it
1473 (we'd be in an inconsistent state otherwise). */
1474 if (!catch_errors (remote_start_remote, (char *)0,
d036abbf
MS
1475 "Couldn't establish connection to remote target\n",
1476 RETURN_MASK_ALL))
b85cf6ae 1477 {
7517f04b 1478 pop_target ();
b85cf6ae
MS
1479 return;
1480 }
1481
1482 if (extended_p)
1483 {
1484 /* tell the remote that we're using the extended protocol. */
1485 char buf[PBUFSIZ];
1486 putpkt ("!");
1487 getpkt (buf, 0);
1488 }
1489}
1490
1491/* This takes a program previously attached to and detaches it. After
1492 this is done, GDB can be used to debug some other program. We
1493 better not have left any breakpoints in the target program or it'll
1494 die when it hits one. */
1495
1496static void
1497remote_detach (args, from_tty)
1498 char *args;
1499 int from_tty;
1500{
1501 char buf[PBUFSIZ];
1502
1503 if (args)
1504 error ("Argument given to \"detach\" when remotely debugging.");
1505
1506 /* Tell the remote target to detach. */
1507 strcpy (buf, "D");
1508 remote_send (buf);
1509
1510 pop_target ();
1511 if (from_tty)
1512 puts_filtered ("Ending remote debugging.\n");
1513}
1514
1515/* Convert hex digit A to a number. */
1516
1517int
1518fromhex (a)
1519 int a;
1520{
1521 if (a >= '0' && a <= '9')
1522 return a - '0';
1523 else if (a >= 'a' && a <= 'f')
1524 return a - 'a' + 10;
1525 else if (a >= 'A' && a <= 'F')
1526 return a - 'A' + 10;
1527 else
1528 error ("Reply contains invalid hex digit %d", a);
1529}
1530
1531/* Convert number NIB to a hex digit. */
1532
1533static int
1534tohex (nib)
1535 int nib;
1536{
1537 if (nib < 10)
1538 return '0'+nib;
1539 else
1540 return 'a'+nib-10;
1541}
1542\f
1543/* Tell the remote machine to resume. */
1544
1545static enum target_signal last_sent_signal = TARGET_SIGNAL_0;
7517f04b
SS
1546
1547static int last_sent_step;
b85cf6ae
MS
1548
1549static void
1550remote_resume (pid, step, siggnal)
1551 int pid, step;
1552 enum target_signal siggnal;
1553{
1554 char buf[PBUFSIZ];
1555
1556 if (pid == -1)
b53a1514 1557 set_thread (0, 0); /* run any thread */
b85cf6ae 1558 else
b53a1514 1559 set_thread (pid, 0); /* run this thread */
b85cf6ae
MS
1560
1561 dcache_flush (remote_dcache);
1562
1563 last_sent_signal = siggnal;
1564 last_sent_step = step;
1565
1566 /* A hook for when we need to do something at the last moment before
1567 resumption. */
1568 if (target_resume_hook)
1569 (*target_resume_hook) ();
1570
1571 if (siggnal != TARGET_SIGNAL_0)
1572 {
1573 buf[0] = step ? 'S' : 'C';
1574 buf[1] = tohex (((int)siggnal >> 4) & 0xf);
1575 buf[2] = tohex ((int)siggnal & 0xf);
1576 buf[3] = '\0';
1577 }
1578 else
1579 strcpy (buf, step ? "s": "c");
1580
1581 putpkt (buf);
1582}
1583\f
1584/* Send ^C to target to halt it. Target will respond, and send us a
1585 packet. */
7517f04b 1586
2e7eeba9 1587static void (*ofunc) PARAMS ((int));
b85cf6ae
MS
1588
1589static void
1590remote_interrupt (signo)
1591 int signo;
1592{
2e7eeba9
MS
1593 remote_stop ();
1594 signal (signo, remote_interrupt);
b85cf6ae 1595}
2e7eeba9 1596
b85cf6ae 1597static void
2e7eeba9 1598remote_stop ()
b85cf6ae 1599{
2e7eeba9
MS
1600 if (!interrupted_already)
1601 {
1602 /* Send a break or a ^C, depending on user preference. */
1603 interrupted_already = 1;
b85cf6ae 1604
2e7eeba9
MS
1605 if (remote_debug)
1606 printf_unfiltered ("remote_stop called\n");
1607
1608 if (remote_break)
1609 SERIAL_SEND_BREAK (remote_desc);
1610 else
1611 SERIAL_WRITE (remote_desc, "\003", 1);
1612 }
1613 else
1614 {
1615 signal (SIGINT, ofunc);
1616 interrupt_query ();
1617 signal (SIGINT, remote_interrupt);
1618 interrupted_already = 0;
1619 }
b85cf6ae
MS
1620}
1621
1622/* Ask the user what to do when an interrupt is received. */
1623
1624static void
1625interrupt_query ()
1626{
1627 target_terminal_ours ();
1628
1629 if (query ("Interrupted while waiting for the program.\n\
1630Give up (and stop debugging it)? "))
1631 {
1632 target_mourn_inferior ();
1633 return_to_top_level (RETURN_QUIT);
1634 }
1635
1636 target_terminal_inferior ();
1637}
1638
1639/* If nonzero, ignore the next kill. */
7517f04b 1640
b85cf6ae
MS
1641int kill_kludge;
1642
1643void
1644remote_console_output (msg)
1645 char *msg;
1646{
1647 char *p;
1648
1649 for (p = msg; *p; p +=2)
1650 {
1651 char tb[2];
1652 char c = fromhex (p[0]) * 16 + fromhex (p[1]);
1653 tb[0] = c;
1654 tb[1] = 0;
1655 if (target_output_hook)
1656 target_output_hook (tb);
1657 else
1658 fputs_filtered (tb, gdb_stdout);
1659 }
1660}
1661
7517f04b
SS
1662/* Wait until the remote machine stops, then return, storing status in
1663 STATUS just as `wait' would. Returns "pid" (though it's not clear
1664 what, if anything, that means in the case of this target). */
b85cf6ae
MS
1665
1666static int
1667remote_wait (pid, status)
1668 int pid;
1669 struct target_waitstatus *status;
1670{
1671 unsigned char buf[PBUFSIZ];
1672 int thread_num = -1;
1673
1674 status->kind = TARGET_WAITKIND_EXITED;
1675 status->value.integer = 0;
1676
1677 while (1)
1678 {
1679 unsigned char *p;
1680
2e7eeba9
MS
1681 interrupted_already = 0;
1682 ofunc = signal (SIGINT, remote_interrupt);
b85cf6ae
MS
1683 getpkt ((char *) buf, 1);
1684 signal (SIGINT, ofunc);
1685
1686 /* This is a hook for when we need to do something (perhaps the
1687 collection of trace data) every time the target stops. */
1688 if (target_wait_loop_hook)
1689 (*target_wait_loop_hook) ();
1690
1691 switch (buf[0])
1692 {
1693 case 'E': /* Error of some sort */
1694 warning ("Remote failure reply: %s", buf);
1695 continue;
1696 case 'T': /* Status with PC, SP, FP, ... */
1697 {
1698 int i;
1699 long regno;
1700 char regs[MAX_REGISTER_RAW_SIZE];
1701
1702 /* Expedited reply, containing Signal, {regno, reg} repeat */
1703 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
1704 ss = signal number
1705 n... = register number
1706 r... = register contents
1707 */
1708 p = &buf[3]; /* after Txx */
1709
1710 while (*p)
1711 {
1712 unsigned char *p1;
1713 char *p_temp;
1714
d036abbf
MS
1715 /* Read the register number */
1716 regno = strtol ((const char *) p, &p_temp, 16);
b85cf6ae
MS
1717 p1 = (unsigned char *)p_temp;
1718
4ef1f467 1719 if (p1 == p) /* No register number present here */
b85cf6ae
MS
1720 {
1721 p1 = (unsigned char *) strchr ((const char *) p, ':');
1722 if (p1 == NULL)
4ef1f467 1723 warning ("Malformed packet(a) (missing colon): %s\n\
b85cf6ae
MS
1724Packet: '%s'\n",
1725 p, buf);
1726 if (strncmp ((const char *) p, "thread", p1 - p) == 0)
1727 {
7517f04b
SS
1728 p_temp = unpack_varlen_hex (++p1, &thread_num);
1729 record_currthread (thread_num);
1730 p = (unsigned char *) p_temp;
b85cf6ae
MS
1731 }
1732 }
1733 else
1734 {
1735 p = p1;
1736
1737 if (*p++ != ':')
4ef1f467 1738 warning ("Malformed packet(b) (missing colon): %s\n\
b85cf6ae
MS
1739Packet: '%s'\n",
1740 p, buf);
1741
1742 if (regno >= NUM_REGS)
1743 warning ("Remote sent bad register number %ld: %s\n\
1744Packet: '%s'\n",
1745 regno, p, buf);
1746
1747 for (i = 0; i < REGISTER_RAW_SIZE (regno); i++)
1748 {
1749 if (p[0] == 0 || p[1] == 0)
1750 warning ("Remote reply is too short: %s", buf);
1751 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
1752 p += 2;
1753 }
1754 supply_register (regno, regs);
1755 }
1756
1757 if (*p++ != ';')
4ef1f467
DT
1758 {
1759 warning ("Remote register badly formatted: %s", buf);
1760 warning (" here: %s",p);
1761 }
b85cf6ae
MS
1762 }
1763 }
1764 /* fall through */
1765 case 'S': /* Old style status, just signal only */
1766 status->kind = TARGET_WAITKIND_STOPPED;
1767 status->value.sig = (enum target_signal)
1768 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
1769
1770 goto got_status;
1771 case 'W': /* Target exited */
1772 {
1773 /* The remote process exited. */
1774 status->kind = TARGET_WAITKIND_EXITED;
1775 status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
1776 goto got_status;
1777 }
1778 case 'X':
1779 status->kind = TARGET_WAITKIND_SIGNALLED;
1780 status->value.sig = (enum target_signal)
1781 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
1782 kill_kludge = 1;
1783
1784 goto got_status;
1785 case 'O': /* Console output */
1786 remote_console_output (buf + 1);
1787 continue;
1788 case '\0':
1789 if (last_sent_signal != TARGET_SIGNAL_0)
1790 {
1791 /* Zero length reply means that we tried 'S' or 'C' and
1792 the remote system doesn't support it. */
1793 target_terminal_ours_for_output ();
1794 printf_filtered
1795 ("Can't send signals to this remote system. %s not sent.\n",
1796 target_signal_to_name (last_sent_signal));
1797 last_sent_signal = TARGET_SIGNAL_0;
1798 target_terminal_inferior ();
1799
1800 strcpy ((char *) buf, last_sent_step ? "s" : "c");
1801 putpkt ((char *) buf);
1802 continue;
1803 }
1804 /* else fallthrough */
1805 default:
1806 warning ("Invalid remote reply: %s", buf);
1807 continue;
1808 }
1809 }
1810 got_status:
1811 if (thread_num != -1)
1812 {
1813 /* Initial thread value can only be acquired via wait, so deal with
1814 this marker which is used before the first thread value is
1815 acquired. */
b53a1514 1816 if (inferior_pid == MAGIC_NULL_PID)
b85cf6ae
MS
1817 {
1818 inferior_pid = thread_num;
98c5c192
MS
1819 if (!in_thread_list (inferior_pid))
1820 add_thread (inferior_pid);
b85cf6ae
MS
1821 }
1822 return thread_num;
1823 }
1824 return inferior_pid;
1825}
1826
1827/* Number of bytes of registers this stub implements. */
7517f04b 1828
b85cf6ae
MS
1829static int register_bytes_found;
1830
1831/* Read the remote registers into the block REGS. */
1832/* Currently we just read all the registers, so we don't use regno. */
7517f04b 1833
b85cf6ae
MS
1834/* ARGSUSED */
1835static void
1836remote_fetch_registers (regno)
1837 int regno;
1838{
1839 char buf[PBUFSIZ];
1840 int i;
1841 char *p;
1842 char regs[REGISTER_BYTES];
1843
1844 set_thread (inferior_pid, 1);
1845
1846 sprintf (buf, "g");
1847 remote_send (buf);
1848
1849 if (remote_register_buf_size == 0)
1850 remote_register_buf_size = strlen (buf);
1851
1852 /* Unimplemented registers read as all bits zero. */
1853 memset (regs, 0, REGISTER_BYTES);
1854
1855 /* We can get out of synch in various cases. If the first character
1856 in the buffer is not a hex character, assume that has happened
1857 and try to fetch another packet to read. */
1858 while ((buf[0] < '0' || buf[0] > '9')
2e7eeba9
MS
1859 && (buf[0] < 'a' || buf[0] > 'f')
1860 && buf[0] != 'x') /* New: unavailable register value */
b85cf6ae
MS
1861 {
1862 if (remote_debug)
1863 printf_unfiltered ("Bad register packet; fetching a new packet\n");
1864 getpkt (buf, 0);
1865 }
1866
1867 /* Reply describes registers byte by byte, each byte encoded as two
1868 hex characters. Suck them all up, then supply them to the
1869 register cacheing/storage mechanism. */
1870
1871 p = buf;
1872 for (i = 0; i < REGISTER_BYTES; i++)
1873 {
1874 if (p[0] == 0)
1875 break;
1876 if (p[1] == 0)
1877 {
1878 warning ("Remote reply is of odd length: %s", buf);
1879 /* Don't change register_bytes_found in this case, and don't
1880 print a second warning. */
1881 goto supply_them;
1882 }
2e7eeba9
MS
1883 if (p[0] == 'x' && p[1] == 'x')
1884 regs[i] = 0; /* 'x' */
1885 else
1886 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
b85cf6ae
MS
1887 p += 2;
1888 }
1889
1890 if (i != register_bytes_found)
1891 {
1892 register_bytes_found = i;
1893#ifdef REGISTER_BYTES_OK
1894 if (!REGISTER_BYTES_OK (i))
1895 warning ("Remote reply is too short: %s", buf);
1896#endif
1897 }
2e7eeba9
MS
1898
1899 supply_them:
b85cf6ae 1900 for (i = 0; i < NUM_REGS; i++)
2e7eeba9 1901 {
b85cf6ae 1902 supply_register (i, &regs[REGISTER_BYTE(i)]);
2e7eeba9
MS
1903 if (buf[REGISTER_BYTE(i) * 2] == 'x')
1904 register_valid[i] = -1; /* register value not available */
1905 }
b85cf6ae
MS
1906}
1907
1908/* Prepare to store registers. Since we may send them all (using a
1909 'G' request), we have to read out the ones we don't want to change
1910 first. */
1911
1912static void
1913remote_prepare_to_store ()
1914{
1915 /* Make sure the entire registers array is valid. */
1916 read_register_bytes (0, (char *)NULL, REGISTER_BYTES);
1917}
1918
1919/* Store register REGNO, or all registers if REGNO == -1, from the contents
1920 of REGISTERS. FIXME: ignores errors. */
1921
1922static void
1923remote_store_registers (regno)
1924 int regno;
1925{
1926 char buf[PBUFSIZ];
1927 int i;
1928 char *p;
1929
1930 set_thread (inferior_pid, 1);
1931
1932 if (regno >= 0 && stub_supports_P)
1933 {
1934 /* Try storing a single register. */
1935 char *regp;
1936
1937 sprintf (buf, "P%x=", regno);
1938 p = buf + strlen (buf);
1939 regp = &registers[REGISTER_BYTE (regno)];
1940 for (i = 0; i < REGISTER_RAW_SIZE (regno); ++i)
1941 {
1942 *p++ = tohex ((regp[i] >> 4) & 0xf);
1943 *p++ = tohex (regp[i] & 0xf);
1944 }
1945 *p = '\0';
1946 remote_send (buf);
1947 if (buf[0] != '\0')
1948 {
1949 /* The stub understands the 'P' request. We are done. */
1950 return;
1951 }
1952
1953 /* The stub does not support the 'P' request. Use 'G' instead,
1954 and don't try using 'P' in the future (it will just waste our
1955 time). */
1956 stub_supports_P = 0;
1957 }
1958
1959 buf[0] = 'G';
1960
1961 /* Command describes registers byte by byte,
1962 each byte encoded as two hex characters. */
1963
1964 p = buf + 1;
1965 /* remote_prepare_to_store insures that register_bytes_found gets set. */
1966 for (i = 0; i < register_bytes_found; i++)
1967 {
1968 *p++ = tohex ((registers[i] >> 4) & 0xf);
1969 *p++ = tohex (registers[i] & 0xf);
1970 }
1971 *p = '\0';
1972
1973 remote_send (buf);
1974}
1975
7517f04b 1976/* Use of the data cache *used* to be disabled because it loses for looking
d036abbf 1977 at and changing hardware I/O ports and the like. Accepting `volatile'
b85cf6ae
MS
1978 would perhaps be one way to fix it. Another idea would be to use the
1979 executable file for the text segment (for all SEC_CODE sections?
1980 For all SEC_READONLY sections?). This has problems if you want to
1981 actually see what the memory contains (e.g. self-modifying code,
1982 clobbered memory, user downloaded the wrong thing).
1983
1984 Because it speeds so much up, it's now enabled, if you're playing
7517f04b 1985 with registers you turn it of (set remotecache 0). */
b85cf6ae
MS
1986
1987/* Read a word from remote address ADDR and return it.
1988 This goes through the data cache. */
1989
1990#if 0 /* unused? */
1991static int
1992remote_fetch_word (addr)
1993 CORE_ADDR addr;
1994{
1995 return dcache_fetch (remote_dcache, addr);
1996}
1997
1998/* Write a word WORD into remote address ADDR.
1999 This goes through the data cache. */
2000
2001static void
2002remote_store_word (addr, word)
2003 CORE_ADDR addr;
2004 int word;
2005{
2006 dcache_poke (remote_dcache, addr, word);
2007}
2008#endif /* 0 (unused?) */
2009
2010\f
2011
2012/* Return the number of hex digits in num. */
2013
2014static int
2015hexnumlen (num)
2016 ULONGEST num;
2017{
2018 int i;
2019
2020 for (i = 0; num != 0; i++)
2021 num >>= 4;
2022
2023 return max (i, 1);
2024}
2025
7517f04b 2026/* Set BUF to the hex digits representing NUM. */
2e7eeba9
MS
2027
2028static int
2029hexnumstr (buf, num)
2030 char *buf;
2031 ULONGEST num;
2032{
2033 int i;
2034 int len = hexnumlen (num);
2035
2036 buf[len] = '\0';
2037
2038 for (i = len - 1; i >= 0; i--)
2039 {
2040 buf[i] = "0123456789abcdef" [(num & 0xf)];
2041 num >>= 4;
2042 }
2043
2044 return len;
2045}
2046
7517f04b 2047/* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
2e7eeba9
MS
2048
2049static CORE_ADDR
2050remote_address_masked (addr)
2051 CORE_ADDR addr;
2052{
2053 if (remote_address_size > 0
2054 && remote_address_size < (sizeof (ULONGEST) * 8))
2055 {
2056 /* Only create a mask when that mask can safely be constructed
2057 in a ULONGEST variable. */
2058 ULONGEST mask = 1;
2059 mask = (mask << remote_address_size) - 1;
2060 addr &= mask;
2061 }
2062 return addr;
2063}
2064
b85cf6ae
MS
2065/* Write memory data directly to the remote machine.
2066 This does not inform the data cache; the data cache uses this.
2067 MEMADDR is the address in the remote memory space.
2068 MYADDR is the address of the buffer in our space.
2069 LEN is the number of bytes.
2070
2071 Returns number of bytes transferred, or 0 for error. */
2072
2073static int
2074remote_write_bytes (memaddr, myaddr, len)
2075 CORE_ADDR memaddr;
2076 char *myaddr;
2077 int len;
2078{
2079 int max_buf_size; /* Max size of packet output buffer */
2080 int origlen;
2081
2082 /* Chop the transfer down if necessary */
2083
2084 max_buf_size = min (remote_write_size, PBUFSIZ);
2085 if (remote_register_buf_size != 0)
2086 max_buf_size = min (max_buf_size, remote_register_buf_size);
2087
2088 /* Subtract header overhead from max payload size - $M<memaddr>,<len>:#nn */
2089 max_buf_size -= 2 + hexnumlen (memaddr + len - 1) + 1 + hexnumlen (len) + 4;
2090
2091 origlen = len;
2092 while (len > 0)
2093 {
2094 char buf[PBUFSIZ];
2095 char *p;
2096 int todo;
2097 int i;
2098
2099 todo = min (len, max_buf_size / 2); /* num bytes that will fit */
2100
2e7eeba9
MS
2101 /* construct "M"<memaddr>","<len>":" */
2102 /* sprintf (buf, "M%lx,%x:", (unsigned long) memaddr, todo); */
2103 memaddr = remote_address_masked (memaddr);
2104 p = buf;
2105 *p++ = 'M';
2106 p += hexnumstr (p, (ULONGEST) memaddr);
2107 *p++ = ',';
2108 p += hexnumstr (p, (ULONGEST) todo);
2109 *p++ = ':';
2110 *p = '\0';
b85cf6ae 2111
d036abbf
MS
2112 /* We send target system values byte by byte, in increasing byte
2113 addresses, each byte encoded as two hex characters. */
b85cf6ae 2114
b85cf6ae
MS
2115 for (i = 0; i < todo; i++)
2116 {
2117 *p++ = tohex ((myaddr[i] >> 4) & 0xf);
2118 *p++ = tohex (myaddr[i] & 0xf);
2119 }
2120 *p = '\0';
2121
2122 putpkt (buf);
2123 getpkt (buf, 0);
2124
2125 if (buf[0] == 'E')
2126 {
2127 /* There is no correspondance between what the remote protocol uses
2128 for errors and errno codes. We would like a cleaner way of
2129 representing errors (big enough to include errno codes, bfd_error
2130 codes, and others). But for now just return EIO. */
2131 errno = EIO;
2132 return 0;
2133 }
2134 myaddr += todo;
2135 memaddr += todo;
2136 len -= todo;
2137 }
2138 return origlen;
2139}
2140
2141/* Read memory data directly from the remote machine.
2142 This does not use the data cache; the data cache uses this.
2143 MEMADDR is the address in the remote memory space.
2144 MYADDR is the address of the buffer in our space.
2145 LEN is the number of bytes.
2146
2147 Returns number of bytes transferred, or 0 for error. */
2148
2149static int
2150remote_read_bytes (memaddr, myaddr, len)
2151 CORE_ADDR memaddr;
2152 char *myaddr;
2153 int len;
2154{
2155 int max_buf_size; /* Max size of packet output buffer */
2156 int origlen;
2157
2158 /* Chop the transfer down if necessary */
2159
2160 max_buf_size = min (remote_write_size, PBUFSIZ);
2161 if (remote_register_buf_size != 0)
2162 max_buf_size = min (max_buf_size, remote_register_buf_size);
2163
2164 origlen = len;
2165 while (len > 0)
2166 {
2167 char buf[PBUFSIZ];
2168 char *p;
2169 int todo;
2170 int i;
2171
2172 todo = min (len, max_buf_size / 2); /* num bytes that will fit */
2173
2e7eeba9
MS
2174 /* construct "m"<memaddr>","<len>" */
2175 /* sprintf (buf, "m%lx,%x", (unsigned long) memaddr, todo); */
2176 memaddr = remote_address_masked (memaddr);
2177 p = buf;
2178 *p++ = 'm';
2179 p += hexnumstr (p, (ULONGEST) memaddr);
2180 *p++ = ',';
2181 p += hexnumstr (p, (ULONGEST) todo);
2182 *p = '\0';
2183
b85cf6ae
MS
2184 putpkt (buf);
2185 getpkt (buf, 0);
2186
2187 if (buf[0] == 'E')
2188 {
2189 /* There is no correspondance between what the remote protocol uses
2190 for errors and errno codes. We would like a cleaner way of
2191 representing errors (big enough to include errno codes, bfd_error
2192 codes, and others). But for now just return EIO. */
2193 errno = EIO;
2194 return 0;
2195 }
2196
2197 /* Reply describes memory byte by byte,
2198 each byte encoded as two hex characters. */
2199
2200 p = buf;
2201 for (i = 0; i < todo; i++)
2202 {
2203 if (p[0] == 0 || p[1] == 0)
d036abbf
MS
2204 /* Reply is short. This means that we were able to read
2205 only part of what we wanted to. */
b85cf6ae
MS
2206 return i + (origlen - len);
2207 myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
2208 p += 2;
2209 }
2210 myaddr += todo;
2211 memaddr += todo;
2212 len -= todo;
2213 }
2214 return origlen;
2215}
2216\f
7517f04b
SS
2217/* Read or write LEN bytes from inferior memory at MEMADDR,
2218 transferring to or from debugger address MYADDR. Write to inferior
2219 if SHOULD_WRITE is nonzero. Returns length of data written or
2220 read; 0 for error. */
b85cf6ae
MS
2221
2222/* ARGSUSED */
2223static int
7517f04b 2224remote_xfer_memory (memaddr, myaddr, len, should_write, target)
b85cf6ae
MS
2225 CORE_ADDR memaddr;
2226 char *myaddr;
2227 int len;
2228 int should_write;
2229 struct target_ops *target; /* ignored */
2230{
2231#ifdef REMOTE_TRANSLATE_XFER_ADDRESS
2232 CORE_ADDR targaddr;
2233 int targlen;
2234 REMOTE_TRANSLATE_XFER_ADDRESS (memaddr, len, targaddr, targlen);
2235 if (targlen == 0)
2236 return 0;
2237 memaddr = targaddr;
2238 len = targlen;
2239#endif
2240
d036abbf
MS
2241 return dcache_xfer_memory (remote_dcache, memaddr, myaddr,
2242 len, should_write);
b85cf6ae
MS
2243}
2244
2245
2246#if 0
2247/* Enable after 4.12. */
2248
2249void
2250remote_search (len, data, mask, startaddr, increment, lorange, hirange
2251 addr_found, data_found)
2252 int len;
2253 char *data;
2254 char *mask;
2255 CORE_ADDR startaddr;
2256 int increment;
2257 CORE_ADDR lorange;
2258 CORE_ADDR hirange;
2259 CORE_ADDR *addr_found;
2260 char *data_found;
2261{
2262 if (increment == -4 && len == 4)
2263 {
2264 long mask_long, data_long;
2265 long data_found_long;
2266 CORE_ADDR addr_we_found;
2267 char buf[PBUFSIZ];
2268 long returned_long[2];
2269 char *p;
2270
2271 mask_long = extract_unsigned_integer (mask, len);
2272 data_long = extract_unsigned_integer (data, len);
2273 sprintf (buf, "t%x:%x,%x", startaddr, data_long, mask_long);
2274 putpkt (buf);
2275 getpkt (buf, 0);
2276 if (buf[0] == '\0')
2277 {
2278 /* The stub doesn't support the 't' request. We might want to
2279 remember this fact, but on the other hand the stub could be
2280 switched on us. Maybe we should remember it only until
2281 the next "target remote". */
2282 generic_search (len, data, mask, startaddr, increment, lorange,
2283 hirange, addr_found, data_found);
2284 return;
2285 }
2286
2287 if (buf[0] == 'E')
2288 /* There is no correspondance between what the remote protocol uses
2289 for errors and errno codes. We would like a cleaner way of
2290 representing errors (big enough to include errno codes, bfd_error
2291 codes, and others). But for now just use EIO. */
2292 memory_error (EIO, startaddr);
2293 p = buf;
2294 addr_we_found = 0;
2295 while (*p != '\0' && *p != ',')
2296 addr_we_found = (addr_we_found << 4) + fromhex (*p++);
2297 if (*p == '\0')
2298 error ("Protocol error: short return for search");
2299
2300 data_found_long = 0;
2301 while (*p != '\0' && *p != ',')
2302 data_found_long = (data_found_long << 4) + fromhex (*p++);
2303 /* Ignore anything after this comma, for future extensions. */
2304
2305 if (addr_we_found < lorange || addr_we_found >= hirange)
2306 {
2307 *addr_found = 0;
2308 return;
2309 }
2310
2311 *addr_found = addr_we_found;
2312 *data_found = store_unsigned_integer (data_we_found, len);
2313 return;
2314 }
2315 generic_search (len, data, mask, startaddr, increment, lorange,
2316 hirange, addr_found, data_found);
2317}
2318#endif /* 0 */
2319\f
2320static void
2321remote_files_info (ignore)
2322 struct target_ops *ignore;
2323{
2324 puts_filtered ("Debugging a target over a serial line.\n");
2325}
2326\f
2327/* Stuff for dealing with the packets which are part of this protocol.
2328 See comment at top of file for details. */
2329
2330/* Read a single character from the remote end, masking it down to 7 bits. */
2331
2332static int
2333readchar (timeout)
2334 int timeout;
2335{
2336 int ch;
2337
2338 ch = SERIAL_READCHAR (remote_desc, timeout);
2339
2340 switch (ch)
2341 {
2342 case SERIAL_EOF:
2343 error ("Remote connection closed");
2344 case SERIAL_ERROR:
2345 perror_with_name ("Remote communication error");
2346 case SERIAL_TIMEOUT:
2347 return ch;
2348 default:
2349 return ch & 0x7f;
2350 }
2351}
2352
7517f04b
SS
2353/* Send the command in BUF to the remote machine, and read the reply
2354 into BUF. Report an error if we get an error reply. */
b85cf6ae
MS
2355
2356static void
2357remote_send (buf)
2358 char *buf;
2359{
2360 putpkt (buf);
2361 getpkt (buf, 0);
2362
2363 if (buf[0] == 'E')
2364 error ("Remote failure reply: %s", buf);
2365}
2366
ecc6e842
EZ
2367/* Display a null-terminated packet on stdout, for debugging, using C
2368 string notation. */
f8059b97 2369
ecc6e842 2370static void
f8059b97
AC
2371print_packet (buf)
2372 char *buf;
ecc6e842
EZ
2373{
2374 puts_filtered ("\"");
2375 while (*buf)
2376 gdb_printchar (*buf++, gdb_stdout, '"');
2377 puts_filtered ("\"");
2378}
2379
2380
7517f04b
SS
2381/* Send a packet to the remote machine, with error checking. The data
2382 of the packet is in BUF. */
b85cf6ae
MS
2383
2384int
2385putpkt (buf)
2386 char *buf;
2387{
2388 int i;
2389 unsigned char csum = 0;
2390 char buf2[PBUFSIZ];
2391 int cnt = strlen (buf);
2392 int ch;
2393 int tcount = 0;
2394 char *p;
2395
2396 /* Copy the packet into buffer BUF2, encapsulating it
2397 and giving it a checksum. */
2398
2399 if (cnt > (int) sizeof (buf2) - 5) /* Prosanity check */
7517f04b 2400 abort ();
b85cf6ae
MS
2401
2402 p = buf2;
2403 *p++ = '$';
2404
2405 for (i = 0; i < cnt; i++)
2406 {
2407 csum += buf[i];
2408 *p++ = buf[i];
2409 }
2410 *p++ = '#';
2411 *p++ = tohex ((csum >> 4) & 0xf);
2412 *p++ = tohex (csum & 0xf);
2413
2414 /* Send it over and over until we get a positive ack. */
2415
2416 while (1)
2417 {
2418 int started_error_output = 0;
2419
2420 if (remote_debug)
2421 {
2422 *p = '\0';
2423 printf_unfiltered ("Sending packet: %s...", buf2);
7517f04b 2424 gdb_flush (gdb_stdout);
b85cf6ae
MS
2425 }
2426 if (SERIAL_WRITE (remote_desc, buf2, p - buf2))
2427 perror_with_name ("putpkt: write failed");
2428
2429 /* read until either a timeout occurs (-2) or '+' is read */
2430 while (1)
2431 {
2432 ch = readchar (remote_timeout);
2433
2434 if (remote_debug)
2435 {
2436 switch (ch)
2437 {
2438 case '+':
2439 case SERIAL_TIMEOUT:
2440 case '$':
2441 if (started_error_output)
2442 {
2443 putchar_unfiltered ('\n');
2444 started_error_output = 0;
2445 }
2446 }
2447 }
2448
2449 switch (ch)
2450 {
2451 case '+':
2452 if (remote_debug)
7517f04b 2453 printf_unfiltered ("Ack\n");
b85cf6ae
MS
2454 return 1;
2455 case SERIAL_TIMEOUT:
2456 tcount ++;
2457 if (tcount > 3)
2458 return 0;
2459 break; /* Retransmit buffer */
2460 case '$':
2461 {
2462 char junkbuf[PBUFSIZ];
2463
d036abbf
MS
2464 /* It's probably an old response, and we're out of sync.
2465 Just gobble up the packet and ignore it. */
b85cf6ae
MS
2466 getpkt (junkbuf, 0);
2467 continue; /* Now, go look for + */
2468 }
2469 default:
2470 if (remote_debug)
2471 {
2472 if (!started_error_output)
2473 {
2474 started_error_output = 1;
2475 printf_unfiltered ("putpkt: Junk: ");
2476 }
2477 putchar_unfiltered (ch & 0177);
2478 }
2479 continue;
2480 }
2481 break; /* Here to retransmit */
2482 }
2483
2484#if 0
2485 /* This is wrong. If doing a long backtrace, the user should be
d036abbf
MS
2486 able to get out next time we call QUIT, without anything as
2487 violent as interrupt_query. If we want to provide a way out of
2488 here without getting to the next QUIT, it should be based on
2489 hitting ^C twice as in remote_wait. */
b85cf6ae
MS
2490 if (quit_flag)
2491 {
2492 quit_flag = 0;
2493 interrupt_query ();
2494 }
2495#endif
2496 }
2497}
2498
7517f04b
SS
2499/* Come here after finding the start of the frame. Collect the rest
2500 into BUF, verifying the checksum, length, and handling run-length
2501 compression. Returns 0 on any error, 1 on success. */
b85cf6ae
MS
2502
2503static int
2504read_frame (buf)
2505 char *buf;
2506{
2507 unsigned char csum;
2508 char *bp;
2509 int c;
2510
2511 csum = 0;
2512 bp = buf;
2513
2514 while (1)
2515 {
2516 c = readchar (remote_timeout);
2517
2518 switch (c)
2519 {
2520 case SERIAL_TIMEOUT:
2521 if (remote_debug)
2522 puts_filtered ("Timeout in mid-packet, retrying\n");
2523 return 0;
2524 case '$':
2525 if (remote_debug)
2526 puts_filtered ("Saw new packet start in middle of old one\n");
2527 return 0; /* Start a new packet, count retries */
2528 case '#':
2529 {
2530 unsigned char pktcsum;
2531
2532 *bp = '\000';
2533
2534 pktcsum = fromhex (readchar (remote_timeout)) << 4;
2535 pktcsum |= fromhex (readchar (remote_timeout));
2536
2537 if (csum == pktcsum)
2538 return 1;
2539
2540 if (remote_debug)
2541 {
2542 printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
2543 pktcsum, csum);
2544 puts_filtered (buf);
2545 puts_filtered ("\n");
2546 }
2547 return 0;
2548 }
2549 case '*': /* Run length encoding */
2550 csum += c;
2551 c = readchar (remote_timeout);
2552 csum += c;
2553 c = c - ' ' + 3; /* Compute repeat count */
2554
2555
2556 if (c > 0 && c < 255 && bp + c - 1 < buf + PBUFSIZ - 1)
2557 {
2558 memset (bp, *(bp - 1), c);
2559 bp += c;
2560 continue;
2561 }
2562
2563 *bp = '\0';
2564 printf_filtered ("Repeat count %d too large for buffer: ", c);
2565 puts_filtered (buf);
2566 puts_filtered ("\n");
2567 return 0;
2568
2569 default:
2570 if (bp < buf + PBUFSIZ - 1)
2571 {
2572 *bp++ = c;
2573 csum += c;
2574 continue;
2575 }
2576
2577 *bp = '\0';
2578 puts_filtered ("Remote packet too long: ");
2579 puts_filtered (buf);
2580 puts_filtered ("\n");
2581
2582 return 0;
2583 }
2584 }
2585}
2586
7517f04b
SS
2587/* Read a packet from the remote machine, with error checking, and
2588 store it in BUF. BUF is expected to be of size PBUFSIZ. If
2589 FOREVER, wait forever rather than timing out; this is used while
2590 the target is executing user code. */
b85cf6ae
MS
2591
2592void
2593getpkt (buf, forever)
2594 char *buf;
2595 int forever;
2596{
2597 int c;
2598 int tries;
2599 int timeout;
2600 int val;
2601
2602 strcpy (buf,"timeout");
2603
2604 if (forever)
2605 {
2606#ifdef MAINTENANCE_CMDS
2607 timeout = watchdog > 0 ? watchdog : -1;
2608#else
2609 timeout = -1;
2610#endif
2611 }
2612
2613 else
2614 timeout = remote_timeout;
2615
2616#define MAX_TRIES 3
2617
2618 for (tries = 1; tries <= MAX_TRIES; tries++)
2619 {
2620 /* This can loop forever if the remote side sends us characters
2621 continuously, but if it pauses, we'll get a zero from readchar
2622 because of timeout. Then we'll count that as a retry. */
2623
2624 /* Note that we will only wait forever prior to the start of a packet.
2625 After that, we expect characters to arrive at a brisk pace. They
2626 should show up within remote_timeout intervals. */
2627
2628 do
2629 {
2630 c = readchar (timeout);
2631
2632 if (c == SERIAL_TIMEOUT)
2633 {
2634#ifdef MAINTENANCE_CMDS
2635 if (forever) /* Watchdog went off. Kill the target. */
2636 {
2637 target_mourn_inferior ();
2638 error ("Watchdog has expired. Target detached.\n");
2639 }
2640#endif
2641 if (remote_debug)
2642 puts_filtered ("Timed out.\n");
2643 goto retry;
2644 }
2645 }
2646 while (c != '$');
2647
2648 /* We've found the start of a packet, now collect the data. */
2649
2650 val = read_frame (buf);
2651
2652 if (val == 1)
2653 {
2654 if (remote_debug)
2655 fprintf_unfiltered (gdb_stdout, "Packet received: %s\n", buf);
2656 SERIAL_WRITE (remote_desc, "+", 1);
2657 return;
2658 }
2659
2660 /* Try the whole thing again. */
2661 retry:
2662 SERIAL_WRITE (remote_desc, "-", 1);
2663 }
2664
2665 /* We have tried hard enough, and just can't receive the packet. Give up. */
2666
2667 printf_unfiltered ("Ignoring packet error, continuing...\n");
2668 SERIAL_WRITE (remote_desc, "+", 1);
2669}
2670\f
2671static void
2672remote_kill ()
2673{
2674 /* For some mysterious reason, wait_for_inferior calls kill instead of
2675 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
2676 if (kill_kludge)
2677 {
2678 kill_kludge = 0;
2679 target_mourn_inferior ();
2680 return;
2681 }
2682
2683 /* Use catch_errors so the user can quit from gdb even when we aren't on
2684 speaking terms with the remote system. */
2685 catch_errors (putpkt, "k", "", RETURN_MASK_ERROR);
2686
2687 /* Don't wait for it to die. I'm not really sure it matters whether
2688 we do or not. For the existing stubs, kill is a noop. */
2689 target_mourn_inferior ();
2690}
2691
2692static void
2693remote_mourn ()
2694{
2695 remote_mourn_1 (&remote_ops);
2696}
2697
2698static void
2699extended_remote_mourn ()
2700{
2701 /* We do _not_ want to mourn the target like this; this will
2702 remove the extended remote target from the target stack,
2703 and the next time the user says "run" it'll fail.
2704
2705 FIXME: What is the right thing to do here? */
2706#if 0
2707 remote_mourn_1 (&extended_remote_ops);
2708#endif
2709}
2710
2711/* Worker function for remote_mourn. */
2712static void
2713remote_mourn_1 (target)
2714 struct target_ops *target;
2715{
2716 unpush_target (target);
2717 generic_mourn_inferior ();
2718}
2719
2720/* In the extended protocol we want to be able to do things like
2721 "run" and have them basically work as expected. So we need
2722 a special create_inferior function.
2723
2724 FIXME: One day add support for changing the exec file
2725 we're debugging, arguments and an environment. */
2726
2727static void
2728extended_remote_create_inferior (exec_file, args, env)
2729 char *exec_file;
2730 char *args;
2731 char **env;
2732{
2733 /* Rip out the breakpoints; we'll reinsert them after restarting
2734 the remote server. */
2735 remove_breakpoints ();
2736
2737 /* Now restart the remote server. */
2738 extended_remote_restart ();
2739
2740 /* Now put the breakpoints back in. This way we're safe if the
2741 restart function works via a unix fork on the remote side. */
2742 insert_breakpoints ();
2743
2744 /* Clean up from the last time we were running. */
2745 clear_proceed_status ();
2746
2747 /* Let the remote process run. */
2748 proceed (-1, TARGET_SIGNAL_0, 0);
2749}
2750
2751\f
2752/* On some machines, e.g. 68k, we may use a different breakpoint instruction
2753 than other targets; in those use REMOTE_BREAKPOINT instead of just
2754 BREAKPOINT. Also, bi-endian targets may define LITTLE_REMOTE_BREAKPOINT
2755 and BIG_REMOTE_BREAKPOINT. If none of these are defined, we just call
2756 the standard routines that are in mem-break.c. */
2757
2758/* FIXME, these ought to be done in a more dynamic fashion. For instance,
2759 the choice of breakpoint instruction affects target program design and
2760 vice versa, and by making it user-tweakable, the special code here
2761 goes away and we need fewer special GDB configurations. */
2762
2763#if defined (LITTLE_REMOTE_BREAKPOINT) && defined (BIG_REMOTE_BREAKPOINT) && !defined(REMOTE_BREAKPOINT)
2764#define REMOTE_BREAKPOINT
2765#endif
2766
2767#ifdef REMOTE_BREAKPOINT
2768
2769/* If the target isn't bi-endian, just pretend it is. */
2770#if !defined (LITTLE_REMOTE_BREAKPOINT) && !defined (BIG_REMOTE_BREAKPOINT)
2771#define LITTLE_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
2772#define BIG_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
2773#endif
2774
2775static unsigned char big_break_insn[] = BIG_REMOTE_BREAKPOINT;
2776static unsigned char little_break_insn[] = LITTLE_REMOTE_BREAKPOINT;
2777
2778#endif /* REMOTE_BREAKPOINT */
2779
2780/* Insert a breakpoint on targets that don't have any better breakpoint
2781 support. We read the contents of the target location and stash it,
2782 then overwrite it with a breakpoint instruction. ADDR is the target
2783 location in the target machine. CONTENTS_CACHE is a pointer to
2784 memory allocated for saving the target contents. It is guaranteed
2785 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
2786 is accomplished via BREAKPOINT_MAX). */
2787
2788static int
2789remote_insert_breakpoint (addr, contents_cache)
2790 CORE_ADDR addr;
2791 char *contents_cache;
2792{
2793#ifdef REMOTE_BREAKPOINT
2794 int val;
2795
2796 val = target_read_memory (addr, contents_cache, sizeof big_break_insn);
2797
2798 if (val == 0)
2799 {
2800 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
2801 val = target_write_memory (addr, (char *) big_break_insn,
2802 sizeof big_break_insn);
2803 else
2804 val = target_write_memory (addr, (char *) little_break_insn,
2805 sizeof little_break_insn);
2806 }
2807
2808 return val;
2809#else
2810 return memory_insert_breakpoint (addr, contents_cache);
2811#endif /* REMOTE_BREAKPOINT */
2812}
2813
2814static int
2815remote_remove_breakpoint (addr, contents_cache)
2816 CORE_ADDR addr;
2817 char *contents_cache;
2818{
2819#ifdef REMOTE_BREAKPOINT
2820 return target_write_memory (addr, contents_cache, sizeof big_break_insn);
2821#else
2822 return memory_remove_breakpoint (addr, contents_cache);
2823#endif /* REMOTE_BREAKPOINT */
2824}
2825
7517f04b
SS
2826/* Some targets are only capable of doing downloads, and afterwards
2827 they switch to the remote serial protocol. This function provides
2828 a clean way to get from the download target to the remote target.
2829 It's basically just a wrapper so that we don't have to expose any
2830 of the internal workings of remote.c.
b85cf6ae 2831
7517f04b
SS
2832 Prior to calling this routine, you should shutdown the current
2833 target code, else you will get the "A program is being debugged
2834 already..." message. Usually a call to pop_target() suffices. */
b85cf6ae
MS
2835
2836void
2837push_remote_target (name, from_tty)
2838 char *name;
2839 int from_tty;
2840{
2841 printf_filtered ("Switching to remote protocol\n");
2842 remote_open (name, from_tty);
2843}
2844
2845/* Other targets want to use the entire remote serial module but with
2846 certain remote_ops overridden. */
2847
2848void
2849open_remote_target (name, from_tty, target, extended_p)
2850 char *name;
2851 int from_tty;
2852 struct target_ops *target;
2853 int extended_p;
2854{
2855 printf_filtered ("Selecting the %sremote protocol\n",
2856 (extended_p ? "extended-" : ""));
2857 remote_open_1 (name, from_tty, target, extended_p);
2858}
2859
b85cf6ae 2860/* Table used by the crc32 function to calcuate the checksum. */
7517f04b 2861
b85cf6ae
MS
2862static unsigned long crc32_table[256] = {0, 0};
2863
2864static unsigned long
2865crc32 (buf, len, crc)
2866 unsigned char *buf;
2867 int len;
2868 unsigned int crc;
2869{
2870 if (! crc32_table[1])
2871 {
2872 /* Initialize the CRC table and the decoding table. */
2873 int i, j;
2874 unsigned int c;
2875
2876 for (i = 0; i < 256; i++)
2877 {
2878 for (c = i << 24, j = 8; j > 0; --j)
2879 c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
2880 crc32_table[i] = c;
2881 }
2882 }
2883
2884 while (len--)
2885 {
2886 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *buf) & 255];
2887 buf++;
2888 }
2889 return crc;
2890}
2891
2e7eeba9
MS
2892/* compare-sections command
2893
2894 With no arguments, compares each loadable section in the exec bfd
2895 with the same memory range on the target, and reports mismatches.
2896 Useful for verifying the image on the target against the exec file.
2897 Depends on the target understanding the new "qCRC:" request. */
2898
b85cf6ae 2899static void
ecc6e842 2900compare_sections_command (args, from_tty)
b85cf6ae
MS
2901 char *args;
2902 int from_tty;
2903{
2904 asection *s;
2905 unsigned long host_crc, target_crc;
2906 extern bfd *exec_bfd;
2907 struct cleanup *old_chain;
2908 char *tmp, *sectdata, *sectname, buf[PBUFSIZ];
2909 bfd_size_type size;
2910 bfd_vma lma;
2911 int matched = 0;
ecc6e842 2912 int mismatched = 0;
b85cf6ae
MS
2913
2914 if (!exec_bfd)
2915 error ("command cannot be used without an exec file");
2916 if (!current_target.to_shortname ||
2917 strcmp (current_target.to_shortname, "remote") != 0)
2918 error ("command can only be used with remote target");
2919
2920 for (s = exec_bfd->sections; s; s = s->next)
2921 {
2922 if (!(s->flags & SEC_LOAD))
2923 continue; /* skip non-loadable section */
2924
2925 size = bfd_get_section_size_before_reloc (s);
2926 if (size == 0)
2927 continue; /* skip zero-length section */
2928
2929 sectname = (char *) bfd_get_section_name (exec_bfd, s);
2930 if (args && strcmp (args, sectname) != 0)
2931 continue; /* not the section selected by user */
2932
2933 matched = 1; /* do this section */
2934 lma = s->lma;
2935 /* FIXME: assumes lma can fit into long */
2936 sprintf (buf, "qCRC:%lx,%lx", (long) lma, (long) size);
2937 putpkt (buf);
2938
2939 /* be clever; compute the host_crc before waiting for target reply */
2940 sectdata = xmalloc (size);
2941 old_chain = make_cleanup (free, sectdata);
2942 bfd_get_section_contents (exec_bfd, s, sectdata, 0, size);
2943 host_crc = crc32 ((unsigned char *) sectdata, size, 0xffffffff);
2944
2945 getpkt (buf, 0);
2946 if (buf[0] == 'E')
2947 error ("target memory fault, section %s, range 0x%08x -- 0x%08x",
2948 sectname, lma, lma + size);
2949 if (buf[0] != 'C')
2950 error ("remote target does not support this operation");
2951
2952 for (target_crc = 0, tmp = &buf[1]; *tmp; tmp++)
2953 target_crc = target_crc * 16 + fromhex (*tmp);
2954
2955 printf_filtered ("Section %s, range 0x%08x -- 0x%08x: ",
2956 sectname, lma, lma + size);
2957 if (host_crc == target_crc)
2958 printf_filtered ("matched.\n");
2959 else
ecc6e842 2960 {
f8059b97
AC
2961 printf_filtered ("MIS-MATCHED!\n");
2962 mismatched++;
ecc6e842 2963 }
b85cf6ae
MS
2964
2965 do_cleanups (old_chain);
2966 }
ecc6e842 2967 if (mismatched > 0)
d036abbf
MS
2968 warning ("One or more sections of the remote executable does not match\n\
2969the loaded file\n");
b85cf6ae
MS
2970 if (args && !matched)
2971 printf_filtered ("No loaded section named '%s'.\n", args);
b85cf6ae 2972}
c719b714 2973
2bef6075
FN
2974static int
2975remote_query (query_type, buf, outbuf, bufsiz)
2976 char query_type;
2977 char *buf;
2978 char *outbuf;
2979 int *bufsiz;
2980{
2981 int i;
2982 char buf2[PBUFSIZ];
2983 char *p2 = &buf2[0];
2984 char *p = buf;
2985
2986 if (! bufsiz)
2987 error ("null pointer to remote bufer size specified");
2988
2989 /* minimum outbuf size is PBUFSIZE - if bufsiz is not large enough let
2990 the caller know and return what the minimum size is */
2991 /* Note: a zero bufsiz can be used to query the minimum buffer size */
2992 if ( *bufsiz < PBUFSIZ )
2993 {
2994 *bufsiz = PBUFSIZ;
2995 return -1;
2996 }
2997
2998 /* except for querying the minimum buffer size, target must be open */
2999 if (! remote_desc)
3000 error ("remote query is only available after target open");
3001
3002 /* we only take uppercase letters as query types, at least for now */
3003 if ( (query_type < 'A') || (query_type > 'Z') )
3004 error ("invalid remote query type");
3005
3006 if (! buf)
3007 error ("null remote query specified");
3008
3009 if (! outbuf)
3010 error ("remote query requires a buffer to receive data");
3011
3012 outbuf[0] = '\0';
3013
3014 *p2++ = 'q';
3015 *p2++ = query_type;
3016
3017 /* we used one buffer char for the remote protocol q command and another
3018 for the query type. As the remote protocol encapsulation uses 4 chars
3019 we have PBUFZIZ -6 left to pack the query string */
3020 i = 0;
3021 while ( buf[i] && (i < (PBUFSIZ - 7)) )
3022 {
3023 /* bad caller may have sent forbidden characters */
3024 if ( (!isprint(buf[i])) || (buf[i] == '$') || (buf[i] == '#') )
3025 error ("illegal characters in query string");
3026
3027 *p2++ = buf[i];
3028 i++;
3029 }
3030 *p2 = buf[i];
3031
3032 if ( buf[i] )
3033 error ("query larger than avaiable buffer");
3034
3035 i = putpkt (buf2);
3036 if ( i < 0 ) return i;
3037
3038 getpkt (outbuf, 0);
3039
3040 return 0;
3041}
3042
ecc6e842
EZ
3043static void
3044packet_command (args, from_tty)
3045 char *args;
3046 int from_tty;
ecc6e842
EZ
3047{
3048 char buf[PBUFSIZ];
3049
abf80d3b 3050 if (! remote_desc)
ecc6e842
EZ
3051 error ("command can only be used with remote target");
3052
3053 if (! args)
3054 error ("remote-packet command requires packet text as argument");
3055
3056 puts_filtered ("sending: ");
3057 print_packet (args);
3058 puts_filtered ("\n");
3059 putpkt (args);
3060
3061 getpkt (buf, 0);
3062 puts_filtered ("received: ");
3063 print_packet (buf);
3064 puts_filtered ("\n");
3065}
3066
7517f04b
SS
3067#if 0
3068/* --------- UNIT_TEST for THREAD oriented PACKETS ------------------------- */
3069
3070static void display_thread_info PARAMS ((struct gdb_ext_thread_info *info));
3071
3072static void threadset_test_cmd PARAMS ((char *cmd, int tty));
3073
3074static void threadalive_test PARAMS ((char *cmd, int tty));
3075
3076static void threadlist_test_cmd PARAMS ((char *cmd, int tty));
3077
3078int get_and_display_threadinfo PARAMS ((threadref *ref));
3079
3080static void threadinfo_test_cmd PARAMS ((char *cmd, int tty));
3081
3082static int thread_display_step PARAMS ((threadref *ref, void *context));
3083
3084static void threadlist_update_test_cmd PARAMS ((char *cmd, int tty));
3085
3086static void init_remote_threadtests PARAMS ((void));
3087
3088#define SAMPLE_THREAD 0x05060708 /* Truncated 64 bit threadid */
3089
3090static void
3091threadset_test_cmd (cmd, tty)
3092 char *cmd;
3093 int tty;
3094{
3095 int sample_thread = SAMPLE_THREAD;
3096
3097 printf_filtered ("Remote threadset test\n");
3098 set_thread (sample_thread, 1);
3099}
3100
3101
3102static void
3103threadalive_test (cmd, tty)
3104 char *cmd;
3105 int tty;
3106{
3107 int sample_thread = SAMPLE_THREAD;
3108
3109 if (remote_thread_alive (sample_thread))
3110 printf_filtered ("PASS: Thread alive test\n");
3111 else
3112 printf_filtered ("FAIL: Thread alive test\n");
3113}
3114
3115void output_threadid PARAMS ((char *title, threadref * ref));
3116
3117void
3118output_threadid (title, ref)
3119 char *title;
3120 threadref *ref;
3121{
3122 char hexid[20];
3123
3124 pack_threadid (&hexid[0], ref); /* Convert threead id into hex */
3125 hexid[16] = 0;
3126 printf_filtered ("%s %s\n", title, (&hexid[0]));
3127}
3128
3129static void
3130threadlist_test_cmd (cmd, tty)
3131 char *cmd;
3132 int tty;
3133{
3134 int startflag = 1;
3135 threadref nextthread;
3136 int done, result_count;
3137 threadref threadlist[3];
3138
3139 printf_filtered ("Remote Threadlist test\n");
3140 if (!remote_get_threadlist (startflag, &nextthread, 3, &done,
3141 &result_count, &threadlist[0]))
3142 printf_filtered ("FAIL: threadlist test\n");
3143 else
3144 {
3145 threadref *scan = threadlist;
3146 threadref *limit = scan + result_count;
3147
3148 while (scan < limit)
3149 output_threadid (" thread ", scan++);
3150 }
3151}
3152
3153void
3154display_thread_info (info)
3155 struct gdb_ext_thread_info *info;
3156{
3157 output_threadid ("Threadid: ", &info->threadid);
3158 printf_filtered ("Name: %s\n ", info->shortname);
3159 printf_filtered ("State: %s\n", info->display);
3160 printf_filtered ("other: %s\n\n", info->more_display);
3161}
3162
3163int
3164get_and_display_threadinfo (ref)
3165 threadref *ref;
3166{
3167 int result;
3168 int set;
3169 struct gdb_ext_thread_info threadinfo;
3170
3171 set = TAG_THREADID | TAG_EXISTS | TAG_THREADNAME
3172 | TAG_MOREDISPLAY | TAG_DISPLAY;
3173 if (0 != (result = remote_get_threadinfo (ref, set, &threadinfo)))
3174 display_thread_info (&threadinfo);
3175 return result;
3176}
3177
3178static void
3179threadinfo_test_cmd (cmd, tty)
3180 char *cmd;
3181 int tty;
3182{
3183 int athread = SAMPLE_THREAD;
3184 threadref thread;
3185 int set;
3186
3187 int_to_threadref (&thread, athread);
3188 printf_filtered ("Remote Threadinfo test\n");
3189 if (!get_and_display_threadinfo (&thread))
3190 printf_filtered ("FAIL cannot get thread info\n");
3191}
3192
3193static int
3194thread_display_step (ref, context)
3195 threadref *ref;
3196 void *context;
3197{
3198 /* output_threadid(" threadstep ",ref); *//* simple test */
3199 return get_and_display_threadinfo (ref);
3200}
3201
3202static void
3203threadlist_update_test_cmd (cmd, tty)
3204 char *cmd;
3205 int tty;
3206{
3207 printf_filtered ("Remote Threadlist update test\n");
3208 remote_threadlist_iterator (thread_display_step, 0, CRAZY_MAX_THREADS);
3209}
3210
3211static void
3212init_remote_threadtests (void)
3213{
3214 add_com ("tlist", class_obscure, threadlist_test_cmd,
3215 "Fetch and print the remote list of thread identifiers, one pkt only");
3216 add_com ("tinfo", class_obscure, threadinfo_test_cmd,
3217 "Fetch and display info about one thread");
3218 add_com ("tset", class_obscure, threadset_test_cmd,
3219 "Test setting to a different thread");
3220 add_com ("tupd", class_obscure, threadlist_update_test_cmd,
3221 "Iterate through updating all remote thread info");
3222 add_com ("talive", class_obscure, threadalive_test,
3223 " Remote thread alive test ");
3224}
3225
3226#endif /* 0 */
3227
0e05ecee
SS
3228static void
3229init_remote_ops ()
3230{
3231 remote_ops.to_shortname = "remote";
3232 remote_ops.to_longname = "Remote serial target in gdb-specific protocol";
d036abbf
MS
3233 remote_ops.to_doc =
3234 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
0e05ecee
SS
3235Specify the serial device it is connected to (e.g. /dev/ttya).";
3236 remote_ops.to_open = remote_open;
3237 remote_ops.to_close = remote_close;
4ef1f467 3238 remote_ops.to_detach = remote_detach;
0e05ecee 3239 remote_ops.to_resume = remote_resume;
4ef1f467 3240 remote_ops.to_wait = remote_wait;
0e05ecee
SS
3241 remote_ops.to_fetch_registers = remote_fetch_registers;
3242 remote_ops.to_store_registers = remote_store_registers;
3243 remote_ops.to_prepare_to_store = remote_prepare_to_store;
3244 remote_ops.to_xfer_memory = remote_xfer_memory;
3245 remote_ops.to_files_info = remote_files_info;
3246 remote_ops.to_insert_breakpoint = remote_insert_breakpoint;
3247 remote_ops.to_remove_breakpoint = remote_remove_breakpoint;
3248 remote_ops.to_kill = remote_kill;
3249 remote_ops.to_load = generic_load;
3250 remote_ops.to_mourn_inferior = remote_mourn;
3251 remote_ops.to_thread_alive = remote_thread_alive;
2e7eeba9 3252 remote_ops.to_stop = remote_stop;
2bef6075 3253 remote_ops.to_query = remote_query;
0e05ecee
SS
3254 remote_ops.to_stratum = process_stratum;
3255 remote_ops.to_has_all_memory = 1;
3256 remote_ops.to_has_memory = 1;
3257 remote_ops.to_has_stack = 1;
3258 remote_ops.to_has_registers = 1;
3259 remote_ops.to_has_execution = 1;
3ab2abae 3260 remote_ops.to_has_thread_control = tc_schedlock; /* can lock scheduler */
0e05ecee
SS
3261 remote_ops.to_magic = OPS_MAGIC;
3262}
3263
7517f04b
SS
3264/* Set up the extended remote vector by making a copy of the standard
3265 remote vector and adding to it. */
3266
0e05ecee 3267static void
4ef1f467 3268init_extended_remote_ops ()
0e05ecee
SS
3269{
3270 extended_remote_ops = remote_ops;
3271
3272 extended_remote_ops.to_shortname = "extended-remote";
d036abbf
MS
3273 extended_remote_ops.to_longname =
3274 "Extended remote serial target in gdb-specific protocol";
3275 extended_remote_ops.to_doc =
3276 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
0e05ecee
SS
3277Specify the serial device it is connected to (e.g. /dev/ttya).",
3278 extended_remote_ops.to_open = extended_remote_open;
3279 extended_remote_ops.to_create_inferior = extended_remote_create_inferior;
3280 extended_remote_ops.to_mourn_inferior = extended_remote_mourn;
3281}
3282
bd5635a1
RP
3283void
3284_initialize_remote ()
3285{
0e05ecee 3286 init_remote_ops ();
bd5635a1 3287 add_target (&remote_ops);
0e05ecee
SS
3288
3289 init_extended_remote_ops ();
2b576293 3290 add_target (&extended_remote_ops);
7517f04b
SS
3291 init_remote_threads ();
3292#if 0
3293 init_remote_threadtests ();
3294#endif
2c441e7f 3295
ecc6e842
EZ
3296 add_cmd ("compare-sections", class_obscure, compare_sections_command,
3297 "Compare section data on target to the exec file.\n\
3298Argument is a single section name (default: all loaded sections).",
b85cf6ae
MS
3299 &cmdlist);
3300
ecc6e842
EZ
3301 add_cmd ("packet", class_maintenance, packet_command,
3302 "Send an arbitrary packet to a remote target.\n\
3303 maintenance packet TEXT\n\
3304If GDB is talking to an inferior via the GDB serial protocol, then\n\
3305this command sends the string TEXT to the inferior, and displays the\n\
3306response packet. GDB supplies the initial `$' character, and the\n\
f8059b97 3307terminating `#' character and checksum.",
ecc6e842
EZ
3308 &maintenancelist);
3309
d036abbf
MS
3310 add_show_from_set
3311 (add_set_cmd ("remotetimeout", no_class,
3312 var_integer, (char *)&remote_timeout,
3313 "Set timeout value for remote read.\n",
3314 &setlist),
3315 &showlist);
3316
3317 add_show_from_set
3318 (add_set_cmd ("remotebreak", no_class,
3319 var_integer, (char *)&remote_break,
3320 "Set whether to send break if interrupted.\n",
3321 &setlist),
3322 &showlist);
3323
3324 add_show_from_set
3325 (add_set_cmd ("remotewritesize", no_class,
3326 var_integer, (char *)&remote_write_size,
3327 "Set the maximum number of bytes per memory write packet.\n",
3328 &setlist),
3329 &showlist);
2e7eeba9
MS
3330
3331 remote_address_size = TARGET_PTR_BIT;
d036abbf
MS
3332 add_show_from_set
3333 (add_set_cmd ("remoteaddresssize", class_obscure,
3334 var_integer, (char *)&remote_address_size,
3335 "Set the maximum size of the address (in bits) \
3336in a memory packet.\n",
3337 &setlist),
3338 &showlist);
976bb0be 3339}
This page took 0.516197 seconds and 4 git commands to generate.