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