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