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