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