Add h8300.exp.
[deliverable/binutils-gdb.git] / gdb / remote.c
1 /* Remote target communications for serial-line targets in custom GDB protocol
2 Copyright 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19
20 /* Remote communication protocol.
21
22 A debug packet whose contents are <data>
23 is encapsulated for transmission in the form:
24
25 $ <data> # CSUM1 CSUM2
26
27 <data> must be ASCII alphanumeric and cannot include characters
28 '$' or '#'. If <data> starts with two characters followed by
29 ':', then the existing stubs interpret this as a sequence number.
30
31 CSUM1 and CSUM2 are ascii hex representation of an 8-bit
32 checksum of <data>, the most significant nibble is sent first.
33 the hex digits 0-9,a-f are used.
34
35 Receiver responds with:
36
37 + - if CSUM is correct and ready for next packet
38 - - if CSUM is incorrect
39
40 <data> is as follows:
41 Most values are encoded in ascii hex digits. Signal numbers are according
42 to the numbering in target.h.
43
44 Request Packet
45
46 set thread Hct... Set thread for subsequent operations.
47 c = 'c' for thread used in step and
48 continue; t... can be -1 for all
49 threads.
50 c = 'g' for thread used in other
51 operations. If zero, pick a thread,
52 any thread.
53 reply OK for success
54 ENN for an error.
55
56 read registers g
57 reply XX....X Each byte of register data
58 is described by two hex digits.
59 Registers are in the internal order
60 for GDB, and the bytes in a register
61 are in the same order the machine uses.
62 or ENN for an error.
63
64 write regs GXX..XX Each byte of register data
65 is described by two hex digits.
66 reply OK for success
67 ENN for an error
68
69 write reg Pn...=r... Write register n... with value r...,
70 which contains two hex digits for each
71 byte in the register (target byte
72 order).
73 reply OK for success
74 ENN for an error
75 (not supported by all stubs).
76
77 read mem mAA..AA,LLLL AA..AA is address, LLLL is length.
78 reply XX..XX XX..XX is mem contents
79 Can be fewer bytes than requested
80 if able to read only part of the data.
81 or ENN NN is errno
82
83 write mem MAA..AA,LLLL:XX..XX
84 AA..AA is address,
85 LLLL is number of bytes,
86 XX..XX is data
87 reply OK for success
88 ENN for an error (this includes the case
89 where only part of the data was
90 written).
91
92 continue cAA..AA AA..AA is address to resume
93 If AA..AA is omitted,
94 resume at same address.
95
96 step sAA..AA AA..AA is address to resume
97 If AA..AA is omitted,
98 resume at same address.
99
100 continue with Csig;AA..AA Continue with signal sig (hex signal
101 signal number). If ;AA..AA is omitted, resume
102 at same address.
103
104 step with Ssig;AA..AA Like 'C' but step not continue.
105 signal
106
107 last signal ? Reply the current reason for stopping.
108 This is the same reply as is generated
109 for step or cont : SAA where AA is the
110 signal number.
111
112 detach D Reply OK.
113
114 There is no immediate reply to step or cont.
115 The reply comes when the machine stops.
116 It is SAA AA is the signal number.
117
118 or... TAAn...:r...;n...:r...;n...:r...;
119 AA = signal number
120 n... = register number (hex)
121 r... = register contents
122 n... = `thread'
123 r... = thread process ID. This is
124 a hex integer.
125 n... = other string not starting
126 with valid hex digit.
127 gdb should ignore this n,r pair
128 and go on to the next. This way
129 we can extend the protocol.
130 or... WAA The process exited, and AA is
131 the exit status. This is only
132 applicable for certains sorts of
133 targets.
134 or... XAA The process terminated with signal
135 AA.
136 or... OXX..XX XX..XX is hex encoding of ASCII data. This
137 can happen at any time while the program is
138 running and the debugger should
139 continue to wait for 'W', 'T', etc.
140
141 thread alive TXX Find out if the thread XX is alive.
142 reply OK thread is still alive
143 ENN thread is dead
144
145 remote restart RXX Restart the remote server
146
147 extended ops ! Use the extended remote protocol.
148 Sticky -- only needs to be set once.
149
150 kill request k
151
152 toggle debug d toggle debug flag (see 386 & 68k stubs)
153 reset r reset -- see sparc stub.
154 reserved <other> On other requests, the stub should
155 ignore the request and send an empty
156 response ($#<checksum>). This way
157 we can extend the protocol and GDB
158 can tell whether the stub it is
159 talking to uses the old or the new.
160 search tAA:PP,MM Search backwards starting at address
161 AA for a match with pattern PP and
162 mask MM. PP and MM are 4 bytes.
163 Not supported by all stubs.
164
165 general query qXXXX Request info about XXXX.
166 general set QXXXX=yyyy Set value of XXXX to yyyy.
167 query sect offs qOffsets Get section offsets. Reply is
168 Text=xxx;Data=yyy;Bss=zzz
169
170 Responses can be run-length encoded to save space. A '*' means that
171 the next character is an ASCII encoding giving a repeat count which
172 stands for that many repititions of the character preceding the '*'.
173 The encoding is n+29, yielding a printable character where n >=3
174 (which is where rle starts to win). Don't use an n > 126.
175
176 So
177 "0* " means the same as "0000". */
178
179 #include "defs.h"
180 #include "gdb_string.h"
181 #include <fcntl.h>
182 #include "frame.h"
183 #include "inferior.h"
184 #include "bfd.h"
185 #include "symfile.h"
186 #include "target.h"
187 #include "wait.h"
188 /*#include "terminal.h"*/
189 #include "gdbcmd.h"
190 #include "objfiles.h"
191 #include "gdb-stabs.h"
192 #include "gdbthread.h"
193
194 #include "dcache.h"
195
196 #ifdef USG
197 #include <sys/types.h>
198 #endif
199
200 #include <signal.h>
201 #include "serial.h"
202
203 /* Prototypes for local functions */
204
205 static int remote_write_bytes PARAMS ((CORE_ADDR memaddr,
206 char *myaddr, int len));
207
208 static int remote_read_bytes PARAMS ((CORE_ADDR memaddr,
209 char *myaddr, int len));
210
211 static void remote_files_info PARAMS ((struct target_ops *ignore));
212
213 static int remote_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr,
214 int len, int should_write,
215 struct target_ops *target));
216
217 static void remote_prepare_to_store PARAMS ((void));
218
219 static void remote_fetch_registers PARAMS ((int regno));
220
221 static void remote_resume PARAMS ((int pid, int step,
222 enum target_signal siggnal));
223
224 static int remote_start_remote PARAMS ((char *dummy));
225
226 static void remote_open PARAMS ((char *name, int from_tty));
227
228 static void extended_remote_open PARAMS ((char *name, int from_tty));
229
230 static void remote_open_1 PARAMS ((char *, int, struct target_ops *));
231
232 static void remote_close PARAMS ((int quitting));
233
234 static void remote_store_registers PARAMS ((int regno));
235
236 static void remote_mourn PARAMS ((void));
237
238 static void extended_remote_restart PARAMS ((void));
239
240 static void extended_remote_mourn PARAMS ((void));
241
242 static void extended_remote_create_inferior PARAMS ((char *, char *, char **));
243
244 static void remote_mourn_1 PARAMS ((struct target_ops *));
245
246 static void getpkt PARAMS ((char *buf, int forever));
247
248 static int putpkt PARAMS ((char *buf));
249
250 static void remote_send PARAMS ((char *buf));
251
252 static int readchar PARAMS ((int timeout));
253
254 static int remote_wait PARAMS ((int pid, struct target_waitstatus *status));
255
256 static void remote_kill PARAMS ((void));
257
258 static int tohex PARAMS ((int nib));
259
260 static int fromhex PARAMS ((int a));
261
262 static void remote_detach PARAMS ((char *args, int from_tty));
263
264 static void remote_interrupt PARAMS ((int signo));
265
266 static void remote_interrupt_twice PARAMS ((int signo));
267
268 static void interrupt_query PARAMS ((void));
269
270 static void set_thread PARAMS ((int, int));
271
272 static int remote_thread_alive PARAMS ((int));
273
274 static void get_offsets PARAMS ((void));
275
276 static int read_frame PARAMS ((char *));
277
278 static int remote_insert_breakpoint PARAMS ((CORE_ADDR, char *));
279
280 static int remote_remove_breakpoint PARAMS ((CORE_ADDR, char *));
281
282 static struct target_ops remote_ops; /* Forward decl */
283 static struct target_ops extended_remote_ops; /* Forward decl */
284
285 /* This was 5 seconds, which is a long time to sit and wait.
286 Unless this is going though some terminal server or multiplexer or
287 other form of hairy serial connection, I would think 2 seconds would
288 be plenty. */
289
290 /* Changed to allow option to set timeout value.
291 was static int remote_timeout = 2; */
292 extern int remote_timeout;
293
294 /* This variable chooses whether to send a ^C or a break when the user
295 requests program interruption. Although ^C is usually what remote
296 systems expect, and that is the default here, sometimes a break is
297 preferable instead. */
298
299 static int remote_break;
300
301 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
302 remote_open knows that we don't have a file open when the program
303 starts. */
304 static serial_t remote_desc = NULL;
305
306 /* Having this larger than 400 causes us to be incompatible with m68k-stub.c
307 and i386-stub.c. Normally, no one would notice because it only matters
308 for writing large chunks of memory (e.g. in downloads). Also, this needs
309 to be more than 400 if required to hold the registers (see below, where
310 we round it up based on REGISTER_BYTES). */
311 #define PBUFSIZ 400
312
313 /* Maximum number of bytes to read/write at once. The value here
314 is chosen to fill up a packet (the headers account for the 32). */
315 #define MAXBUFBYTES ((PBUFSIZ-32)/2)
316
317 /* Round up PBUFSIZ to hold all the registers, at least. */
318 /* The blank line after the #if seems to be required to work around a
319 bug in HP's PA compiler. */
320 #if REGISTER_BYTES > MAXBUFBYTES
321
322 #undef PBUFSIZ
323 #define PBUFSIZ (REGISTER_BYTES * 2 + 32)
324 #endif
325
326 /* This variable sets the number of bytes to be written to the target
327 in a single packet. Normally PBUFSIZ is satisfactory, but some
328 targets need smaller values (perhaps because the receiving end
329 is slow). */
330
331 static int remote_write_size = PBUFSIZ;
332
333 /* This is the size (in chars) of the first response to the `g' command. This
334 is used to limit the size of the memory read and write commands to prevent
335 stub buffers from overflowing. */
336
337 static int remote_register_buf_size = 0;
338
339 /* Should we try the 'P' request? If this is set to one when the stub
340 doesn't support 'P', the only consequence is some unnecessary traffic. */
341 static int stub_supports_P = 1;
342
343 /* These are pointers to hook functions that may be set in order to
344 modify resume/wait behavior for a particular architecture. */
345
346 void (*target_resume_hook) PARAMS ((void));
347 void (*target_wait_loop_hook) PARAMS ((void));
348
349 \f
350 /* These are the threads which we last sent to the remote system. -1 for all
351 or -2 for not sent yet. */
352 int general_thread;
353 int cont_thread;
354
355 static void
356 set_thread (th, gen)
357 int th;
358 int gen;
359 {
360 char buf[PBUFSIZ];
361 int state = gen ? general_thread : cont_thread;
362 if (state == th)
363 return;
364 buf[0] = 'H';
365 buf[1] = gen ? 'g' : 'c';
366 if (th == 42000)
367 {
368 buf[2] = '0';
369 buf[3] = '\0';
370 }
371 else if (th < 0)
372 sprintf (&buf[2], "-%x", -th);
373 else
374 sprintf (&buf[2], "%x", th);
375 putpkt (buf);
376 getpkt (buf, 0);
377 if (gen)
378 general_thread = th;
379 else
380 cont_thread = th;
381 }
382 \f
383 /* Return nonzero if the thread TH is still alive on the remote system. */
384
385 static int
386 remote_thread_alive (th)
387 int th;
388 {
389 char buf[PBUFSIZ];
390
391 buf[0] = 'T';
392 if (th < 0)
393 sprintf (&buf[1], "-%x", -th);
394 else
395 sprintf (&buf[1], "%x", th);
396 putpkt (buf);
397 getpkt (buf, 0);
398 return (buf[0] == 'O' && buf[1] == 'K');
399 }
400
401 /* Restart the remote side; this is an extended protocol operation. */
402
403 static void
404 extended_remote_restart ()
405 {
406 char buf[PBUFSIZ];
407
408 /* Send the restart command; for reasons I don't understand the
409 remote side really expects a number after the "R". */
410 buf[0] = 'R';
411 sprintf (&buf[1], "%x", 0);
412 putpkt (buf);
413
414 /* Now query for status so this looks just like we restarted
415 gdbserver from scratch. */
416 putpkt ("?");
417 getpkt (buf, 0);
418 }
419 \f
420 /* Clean up connection to a remote debugger. */
421
422 /* ARGSUSED */
423 static void
424 remote_close (quitting)
425 int quitting;
426 {
427 if (remote_desc)
428 SERIAL_CLOSE (remote_desc);
429 remote_desc = NULL;
430 }
431
432 /* Query the remote side for the text, data and bss offsets. */
433
434 static void
435 get_offsets ()
436 {
437 char buf[PBUFSIZ];
438 int nvals;
439 CORE_ADDR text_addr, data_addr, bss_addr;
440 struct section_offsets *offs;
441
442 putpkt ("qOffsets");
443
444 getpkt (buf, 0);
445
446 if (buf[0] == '\000')
447 return; /* Return silently. Stub doesn't support this
448 command. */
449 if (buf[0] == 'E')
450 {
451 warning ("Remote failure reply: %s", buf);
452 return;
453 }
454
455 nvals = sscanf (buf, "Text=%lx;Data=%lx;Bss=%lx", &text_addr, &data_addr,
456 &bss_addr);
457 if (nvals != 3)
458 error ("Malformed response to offset query, %s", buf);
459
460 if (symfile_objfile == NULL)
461 return;
462
463 offs = (struct section_offsets *) alloca (sizeof (struct section_offsets)
464 + symfile_objfile->num_sections
465 * sizeof (offs->offsets));
466 memcpy (offs, symfile_objfile->section_offsets,
467 sizeof (struct section_offsets)
468 + symfile_objfile->num_sections
469 * sizeof (offs->offsets));
470
471 ANOFFSET (offs, SECT_OFF_TEXT) = text_addr;
472
473 /* This is a temporary kludge to force data and bss to use the same offsets
474 because that's what nlmconv does now. The real solution requires changes
475 to the stub and remote.c that I don't have time to do right now. */
476
477 ANOFFSET (offs, SECT_OFF_DATA) = data_addr;
478 ANOFFSET (offs, SECT_OFF_BSS) = data_addr;
479
480 objfile_relocate (symfile_objfile, offs);
481 }
482
483 /* Stub for catch_errors. */
484
485 static int
486 remote_start_remote (dummy)
487 char *dummy;
488 {
489 immediate_quit = 1; /* Allow user to interrupt it */
490
491 /* Ack any packet which the remote side has already sent. */
492 SERIAL_WRITE (remote_desc, "+", 1);
493
494 /* Let the stub know that we want it to return the thread. */
495 set_thread (-1, 0);
496
497 get_offsets (); /* Get text, data & bss offsets */
498
499 putpkt ("?"); /* initiate a query from remote machine */
500 immediate_quit = 0;
501
502 start_remote (); /* Initialize gdb process mechanisms */
503 return 1;
504 }
505
506 /* Open a connection to a remote debugger.
507 NAME is the filename used for communication. */
508
509 static void
510 remote_open (name, from_tty)
511 char *name;
512 int from_tty;
513 {
514 remote_open_1 (name, from_tty, &remote_ops);
515 }
516
517 /* Open a connection to a remote debugger using the extended
518 remote gdb protocol. NAME is the filename used for communication. */
519
520 static void
521 extended_remote_open (name, from_tty)
522 char *name;
523 int from_tty;
524 {
525 char buf[PBUFSIZ];
526
527 /* Do the basic remote open stuff. */
528 remote_open_1 (name, from_tty, &extended_remote_ops);
529
530 /* Now tell the remote that we're using the extended protocol. */
531 putpkt ("!");
532 getpkt (buf, 0);
533
534 }
535
536 /* Generic code for opening a connection to a remote target. */
537 static DCACHE *remote_dcache;
538
539 static void
540 remote_open_1 (name, from_tty, target)
541 char *name;
542 int from_tty;
543 struct target_ops *target;
544 {
545 if (name == 0)
546 error ("To open a remote debug connection, you need to specify what serial\n\
547 device is attached to the remote system (e.g. /dev/ttya).");
548
549 target_preopen (from_tty);
550
551 unpush_target (target);
552
553 remote_dcache = dcache_init (remote_read_bytes, remote_write_bytes);
554
555 remote_desc = SERIAL_OPEN (name);
556 if (!remote_desc)
557 perror_with_name (name);
558
559 if (baud_rate != -1)
560 {
561 if (SERIAL_SETBAUDRATE (remote_desc, baud_rate))
562 {
563 SERIAL_CLOSE (remote_desc);
564 perror_with_name (name);
565 }
566 }
567
568
569 SERIAL_RAW (remote_desc);
570
571 /* If there is something sitting in the buffer we might take it as a
572 response to a command, which would be bad. */
573 SERIAL_FLUSH_INPUT (remote_desc);
574
575 if (from_tty)
576 {
577 puts_filtered ("Remote debugging using ");
578 puts_filtered (name);
579 puts_filtered ("\n");
580 }
581 push_target (target); /* Switch to using remote target now */
582
583 /* Start out by trying the 'P' request to set registers. We set this each
584 time that we open a new target so that if the user switches from one
585 stub to another, we can (if the target is closed and reopened) cope. */
586 stub_supports_P = 1;
587
588 general_thread = -2;
589 cont_thread = -2;
590
591 /* Without this, some commands which require an active target (such as kill)
592 won't work. This variable serves (at least) double duty as both the pid
593 of the target process (if it has such), and as a flag indicating that a
594 target is active. These functions should be split out into seperate
595 variables, especially since GDB will someday have a notion of debugging
596 several processes. */
597
598 inferior_pid = 42000;
599 /* Start the remote connection; if error (0), discard this target.
600 In particular, if the user quits, be sure to discard it
601 (we'd be in an inconsistent state otherwise). */
602 if (!catch_errors (remote_start_remote, (char *)0,
603 "Couldn't establish connection to remote target\n", RETURN_MASK_ALL))
604 pop_target();
605 }
606
607 /* This takes a program previously attached to and detaches it. After
608 this is done, GDB can be used to debug some other program. We
609 better not have left any breakpoints in the target program or it'll
610 die when it hits one. */
611
612 static void
613 remote_detach (args, from_tty)
614 char *args;
615 int from_tty;
616 {
617 char buf[PBUFSIZ];
618
619 if (args)
620 error ("Argument given to \"detach\" when remotely debugging.");
621
622 /* Tell the remote target to detach. */
623 strcpy (buf, "D");
624 remote_send (buf);
625
626 pop_target ();
627 if (from_tty)
628 puts_filtered ("Ending remote debugging.\n");
629 }
630
631 /* Convert hex digit A to a number. */
632
633 static int
634 fromhex (a)
635 int a;
636 {
637 if (a >= '0' && a <= '9')
638 return a - '0';
639 else if (a >= 'a' && a <= 'f')
640 return a - 'a' + 10;
641 else
642 error ("Reply contains invalid hex digit %d", a);
643 }
644
645 /* Convert number NIB to a hex digit. */
646
647 static int
648 tohex (nib)
649 int nib;
650 {
651 if (nib < 10)
652 return '0'+nib;
653 else
654 return 'a'+nib-10;
655 }
656 \f
657 /* Tell the remote machine to resume. */
658
659 static enum target_signal last_sent_signal = TARGET_SIGNAL_0;
660 int last_sent_step;
661
662 static void
663 remote_resume (pid, step, siggnal)
664 int pid, step;
665 enum target_signal siggnal;
666 {
667 char buf[PBUFSIZ];
668
669 if (pid == -1)
670 set_thread (inferior_pid, 0);
671 else
672 set_thread (pid, 0);
673
674 dcache_flush (remote_dcache);
675
676 last_sent_signal = siggnal;
677 last_sent_step = step;
678
679 /* A hook for when we need to do something at the last moment before
680 resumption. */
681 if (target_resume_hook)
682 (*target_resume_hook) ();
683
684 if (siggnal != TARGET_SIGNAL_0)
685 {
686 buf[0] = step ? 'S' : 'C';
687 buf[1] = tohex (((int)siggnal >> 4) & 0xf);
688 buf[2] = tohex ((int)siggnal & 0xf);
689 buf[3] = '\0';
690 }
691 else
692 strcpy (buf, step ? "s": "c");
693
694 putpkt (buf);
695 }
696 \f
697 /* Send ^C to target to halt it. Target will respond, and send us a
698 packet. */
699
700 static void
701 remote_interrupt (signo)
702 int signo;
703 {
704 /* If this doesn't work, try more severe steps. */
705 signal (signo, remote_interrupt_twice);
706
707 if (remote_debug)
708 printf_unfiltered ("remote_interrupt called\n");
709
710 /* Send a break or a ^C, depending on user preference. */
711 if (remote_break)
712 SERIAL_SEND_BREAK (remote_desc);
713 else
714 SERIAL_WRITE (remote_desc, "\003", 1);
715 }
716
717 static void (*ofunc)();
718
719 /* The user typed ^C twice. */
720 static void
721 remote_interrupt_twice (signo)
722 int signo;
723 {
724 signal (signo, ofunc);
725
726 interrupt_query ();
727
728 signal (signo, remote_interrupt);
729 }
730
731 /* Ask the user what to do when an interrupt is received. */
732
733 static void
734 interrupt_query ()
735 {
736 target_terminal_ours ();
737
738 if (query ("Interrupted while waiting for the program.\n\
739 Give up (and stop debugging it)? "))
740 {
741 target_mourn_inferior ();
742 return_to_top_level (RETURN_QUIT);
743 }
744
745 target_terminal_inferior ();
746 }
747
748 /* If nonzero, ignore the next kill. */
749 int kill_kludge;
750
751 /* Wait until the remote machine stops, then return,
752 storing status in STATUS just as `wait' would.
753 Returns "pid" (though it's not clear what, if anything, that
754 means in the case of this target). */
755
756 static int
757 remote_wait (pid, status)
758 int pid;
759 struct target_waitstatus *status;
760 {
761 unsigned char buf[PBUFSIZ];
762 int thread_num = -1;
763
764 status->kind = TARGET_WAITKIND_EXITED;
765 status->value.integer = 0;
766
767 while (1)
768 {
769 unsigned char *p;
770
771 ofunc = (void (*)()) signal (SIGINT, remote_interrupt);
772 getpkt ((char *) buf, 1);
773 signal (SIGINT, ofunc);
774
775 /* This is a hook for when we need to do something (perhaps the
776 collection of trace data) every time the target stops. */
777 if (target_wait_loop_hook)
778 (*target_wait_loop_hook) ();
779
780 switch (buf[0])
781 {
782 case 'E': /* Error of some sort */
783 warning ("Remote failure reply: %s", buf);
784 continue;
785 case 'T': /* Status with PC, SP, FP, ... */
786 {
787 int i;
788 long regno;
789 char regs[MAX_REGISTER_RAW_SIZE];
790
791 /* Expedited reply, containing Signal, {regno, reg} repeat */
792 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
793 ss = signal number
794 n... = register number
795 r... = register contents
796 */
797
798 p = &buf[3]; /* after Txx */
799
800 while (*p)
801 {
802 unsigned char *p1;
803 char *p_temp;
804
805 regno = strtol ((const char *) p, &p_temp, 16); /* Read the register number */
806 p1 = (unsigned char *)p_temp;
807
808 if (p1 == p)
809 {
810 p1 = (unsigned char *) strchr ((const char *) p, ':');
811 if (p1 == NULL)
812 warning ("Malformed packet (missing colon): %s\n\
813 Packet: '%s'\n",
814 p, buf);
815 if (strncmp ((const char *) p, "thread", p1 - p) == 0)
816 {
817 thread_num = strtol ((const char *) ++p1, &p_temp, 16);
818 p = (unsigned char *)p_temp;
819 }
820 }
821 else
822 {
823 p = p1;
824
825 if (*p++ != ':')
826 warning ("Malformed packet (missing colon): %s\n\
827 Packet: '%s'\n",
828 p, buf);
829
830 if (regno >= NUM_REGS)
831 warning ("Remote sent bad register number %ld: %s\n\
832 Packet: '%s'\n",
833 regno, p, buf);
834
835 for (i = 0; i < REGISTER_RAW_SIZE (regno); i++)
836 {
837 if (p[0] == 0 || p[1] == 0)
838 warning ("Remote reply is too short: %s", buf);
839 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
840 p += 2;
841 }
842 supply_register (regno, regs);
843 }
844
845 if (*p++ != ';')
846 warning ("Remote register badly formatted: %s", buf);
847 }
848 }
849 /* fall through */
850 case 'S': /* Old style status, just signal only */
851 status->kind = TARGET_WAITKIND_STOPPED;
852 status->value.sig = (enum target_signal)
853 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
854
855 goto got_status;
856 case 'W': /* Target exited */
857 {
858 /* The remote process exited. */
859 status->kind = TARGET_WAITKIND_EXITED;
860 status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
861 goto got_status;
862 }
863 case 'X':
864 status->kind = TARGET_WAITKIND_SIGNALLED;
865 status->value.sig = (enum target_signal)
866 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
867 kill_kludge = 1;
868
869 goto got_status;
870 case 'O': /* Console output */
871 for (p = buf + 1; *p; p +=2)
872 {
873 char tb[2];
874 char c = fromhex (p[0]) * 16 + fromhex (p[1]);
875 tb[0] = c;
876 tb[1] = 0;
877 if (target_output_hook)
878 target_output_hook (tb);
879 else
880 fputs_filtered (tb, gdb_stdout);
881 }
882 continue;
883 case '\0':
884 if (last_sent_signal != TARGET_SIGNAL_0)
885 {
886 /* Zero length reply means that we tried 'S' or 'C' and
887 the remote system doesn't support it. */
888 target_terminal_ours_for_output ();
889 printf_filtered
890 ("Can't send signals to this remote system. %s not sent.\n",
891 target_signal_to_name (last_sent_signal));
892 last_sent_signal = TARGET_SIGNAL_0;
893 target_terminal_inferior ();
894
895 strcpy ((char *) buf, last_sent_step ? "s" : "c");
896 putpkt ((char *) buf);
897 continue;
898 }
899 /* else fallthrough */
900 default:
901 warning ("Invalid remote reply: %s", buf);
902 continue;
903 }
904 }
905 got_status:
906 if (thread_num != -1)
907 {
908 /* Initial thread value can only be acquired via wait, so deal with
909 this marker which is used before the first thread value is
910 acquired. */
911 if (inferior_pid == 42000)
912 {
913 inferior_pid = thread_num;
914 add_thread (inferior_pid);
915 }
916 return thread_num;
917 }
918 return inferior_pid;
919 }
920
921 /* Number of bytes of registers this stub implements. */
922 static int register_bytes_found;
923
924 /* Read the remote registers into the block REGS. */
925 /* Currently we just read all the registers, so we don't use regno. */
926 /* ARGSUSED */
927 static void
928 remote_fetch_registers (regno)
929 int regno;
930 {
931 char buf[PBUFSIZ];
932 int i;
933 char *p;
934 char regs[REGISTER_BYTES];
935
936 set_thread (inferior_pid, 1);
937
938 sprintf (buf, "g");
939 remote_send (buf);
940
941 if (remote_register_buf_size == 0)
942 remote_register_buf_size = strlen (buf);
943
944 /* Unimplemented registers read as all bits zero. */
945 memset (regs, 0, REGISTER_BYTES);
946
947 /* We can get out of synch in various cases. If the first character
948 in the buffer is not a hex character, assume that has happened
949 and try to fetch another packet to read. */
950 while ((buf[0] < '0' || buf[0] > '9')
951 && (buf[0] < 'a' || buf[0] > 'f'))
952 {
953 if (remote_debug)
954 printf_unfiltered ("Bad register packet; fetching a new packet\n");
955 getpkt (buf, 0);
956 }
957
958 /* Reply describes registers byte by byte, each byte encoded as two
959 hex characters. Suck them all up, then supply them to the
960 register cacheing/storage mechanism. */
961
962 p = buf;
963 for (i = 0; i < REGISTER_BYTES; i++)
964 {
965 if (p[0] == 0)
966 break;
967 if (p[1] == 0)
968 {
969 warning ("Remote reply is of odd length: %s", buf);
970 /* Don't change register_bytes_found in this case, and don't
971 print a second warning. */
972 goto supply_them;
973 }
974 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
975 p += 2;
976 }
977
978 if (i != register_bytes_found)
979 {
980 register_bytes_found = i;
981 #ifdef REGISTER_BYTES_OK
982 if (!REGISTER_BYTES_OK (i))
983 warning ("Remote reply is too short: %s", buf);
984 #endif
985 }
986
987 supply_them:
988 for (i = 0; i < NUM_REGS; i++)
989 supply_register (i, &regs[REGISTER_BYTE(i)]);
990 }
991
992 /* Prepare to store registers. Since we may send them all (using a
993 'G' request), we have to read out the ones we don't want to change
994 first. */
995
996 static void
997 remote_prepare_to_store ()
998 {
999 /* Make sure the entire registers array is valid. */
1000 read_register_bytes (0, (char *)NULL, REGISTER_BYTES);
1001 }
1002
1003 /* Store register REGNO, or all registers if REGNO == -1, from the contents
1004 of REGISTERS. FIXME: ignores errors. */
1005
1006 static void
1007 remote_store_registers (regno)
1008 int regno;
1009 {
1010 char buf[PBUFSIZ];
1011 int i;
1012 char *p;
1013
1014 set_thread (inferior_pid, 1);
1015
1016 if (regno >= 0 && stub_supports_P)
1017 {
1018 /* Try storing a single register. */
1019 char *regp;
1020
1021 sprintf (buf, "P%x=", regno);
1022 p = buf + strlen (buf);
1023 regp = &registers[REGISTER_BYTE (regno)];
1024 for (i = 0; i < REGISTER_RAW_SIZE (regno); ++i)
1025 {
1026 *p++ = tohex ((regp[i] >> 4) & 0xf);
1027 *p++ = tohex (regp[i] & 0xf);
1028 }
1029 *p = '\0';
1030 remote_send (buf);
1031 if (buf[0] != '\0')
1032 {
1033 /* The stub understands the 'P' request. We are done. */
1034 return;
1035 }
1036
1037 /* The stub does not support the 'P' request. Use 'G' instead,
1038 and don't try using 'P' in the future (it will just waste our
1039 time). */
1040 stub_supports_P = 0;
1041 }
1042
1043 buf[0] = 'G';
1044
1045 /* Command describes registers byte by byte,
1046 each byte encoded as two hex characters. */
1047
1048 p = buf + 1;
1049 /* remote_prepare_to_store insures that register_bytes_found gets set. */
1050 for (i = 0; i < register_bytes_found; i++)
1051 {
1052 *p++ = tohex ((registers[i] >> 4) & 0xf);
1053 *p++ = tohex (registers[i] & 0xf);
1054 }
1055 *p = '\0';
1056
1057 remote_send (buf);
1058 }
1059
1060 /*
1061 Use of the data cache *used* to be disabled because it loses for looking at
1062 and changing hardware I/O ports and the like. Accepting `volatile'
1063 would perhaps be one way to fix it. Another idea would be to use the
1064 executable file for the text segment (for all SEC_CODE sections?
1065 For all SEC_READONLY sections?). This has problems if you want to
1066 actually see what the memory contains (e.g. self-modifying code,
1067 clobbered memory, user downloaded the wrong thing).
1068
1069 Because it speeds so much up, it's now enabled, if you're playing
1070 with registers you turn it of (set remotecache 0)
1071 */
1072
1073 /* Read a word from remote address ADDR and return it.
1074 This goes through the data cache. */
1075
1076 #if 0 /* unused? */
1077 static int
1078 remote_fetch_word (addr)
1079 CORE_ADDR addr;
1080 {
1081 return dcache_fetch (remote_dcache, addr);
1082 }
1083
1084 /* Write a word WORD into remote address ADDR.
1085 This goes through the data cache. */
1086
1087 static void
1088 remote_store_word (addr, word)
1089 CORE_ADDR addr;
1090 int word;
1091 {
1092 dcache_poke (remote_dcache, addr, word);
1093 }
1094 #endif /* 0 (unused?) */
1095
1096 \f
1097
1098 /* Return the number of hex digits in num. */
1099
1100 static int
1101 hexnumlen (num)
1102 ULONGEST num;
1103 {
1104 int i;
1105
1106 for (i = 0; num != 0; i++)
1107 num >>= 4;
1108
1109 return min (i, 1);
1110 }
1111
1112 /* Write memory data directly to the remote machine.
1113 This does not inform the data cache; the data cache uses this.
1114 MEMADDR is the address in the remote memory space.
1115 MYADDR is the address of the buffer in our space.
1116 LEN is the number of bytes.
1117
1118 Returns number of bytes transferred, or 0 for error. */
1119
1120 static int
1121 remote_write_bytes (memaddr, myaddr, len)
1122 CORE_ADDR memaddr;
1123 char *myaddr;
1124 int len;
1125 {
1126 int max_buf_size; /* Max size of packet output buffer */
1127 int origlen;
1128
1129 /* Chop the transfer down if necessary */
1130
1131 max_buf_size = min (remote_write_size, PBUFSIZ);
1132 max_buf_size = min (max_buf_size, remote_register_buf_size);
1133
1134 #define PACKET_OVERHEAD (1 + 1 + 1 + 2) /* $x#xx - Overhead for all types of packets */
1135
1136 /* packet overhead + <memaddr>,<len>: */
1137 max_buf_size -= PACKET_OVERHEAD + hexnumlen (memaddr + len - 1) + 1 + hexnumlen (len) + 1;
1138
1139 origlen = len;
1140 while (len > 0)
1141 {
1142 char buf[PBUFSIZ];
1143 char *p;
1144 int todo;
1145 int i;
1146
1147 todo = min (len, max_buf_size / 2); /* num bytes that will fit */
1148
1149 /* FIXME-32x64: Need a version of print_address_numeric which puts the
1150 result in a buffer like sprintf. */
1151 sprintf (buf, "M%lx,%x:", (unsigned long) memaddr, todo);
1152
1153 /* We send target system values byte by byte, in increasing byte addresses,
1154 each byte encoded as two hex characters. */
1155
1156 p = buf + strlen (buf);
1157 for (i = 0; i < todo; i++)
1158 {
1159 *p++ = tohex ((myaddr[i] >> 4) & 0xf);
1160 *p++ = tohex (myaddr[i] & 0xf);
1161 }
1162 *p = '\0';
1163
1164 putpkt (buf);
1165 getpkt (buf, 0);
1166
1167 if (buf[0] == 'E')
1168 {
1169 /* There is no correspondance between what the remote protocol uses
1170 for errors and errno codes. We would like a cleaner way of
1171 representing errors (big enough to include errno codes, bfd_error
1172 codes, and others). But for now just return EIO. */
1173 errno = EIO;
1174 return 0;
1175 }
1176 myaddr += todo;
1177 memaddr += todo;
1178 len -= todo;
1179 }
1180 return origlen;
1181 }
1182
1183 /* Read memory data directly from the remote machine.
1184 This does not use the data cache; the data cache uses this.
1185 MEMADDR is the address in the remote memory space.
1186 MYADDR is the address of the buffer in our space.
1187 LEN is the number of bytes.
1188
1189 Returns number of bytes transferred, or 0 for error. */
1190
1191 static int
1192 remote_read_bytes (memaddr, myaddr, len)
1193 CORE_ADDR memaddr;
1194 char *myaddr;
1195 int len;
1196 {
1197 int max_buf_size; /* Max size of packet output buffer */
1198 int origlen;
1199
1200 /* Chop the transfer down if necessary */
1201
1202 max_buf_size = min (remote_write_size, PBUFSIZ);
1203 max_buf_size = min (max_buf_size, remote_register_buf_size);
1204
1205 /* packet overhead */
1206 max_buf_size -= PACKET_OVERHEAD;
1207
1208 origlen = len;
1209 while (len > 0)
1210 {
1211 char buf[PBUFSIZ];
1212 char *p;
1213 int todo;
1214 int i;
1215
1216 todo = min (len, max_buf_size / 2); /* num bytes that will fit */
1217
1218 /* FIXME-32x64: Need a version of print_address_numeric which puts the
1219 result in a buffer like sprintf. */
1220 sprintf (buf, "m%lx,%x", (unsigned long) memaddr, todo);
1221 putpkt (buf);
1222 getpkt (buf, 0);
1223
1224 if (buf[0] == 'E')
1225 {
1226 /* There is no correspondance between what the remote protocol uses
1227 for errors and errno codes. We would like a cleaner way of
1228 representing errors (big enough to include errno codes, bfd_error
1229 codes, and others). But for now just return EIO. */
1230 errno = EIO;
1231 return 0;
1232 }
1233
1234 /* Reply describes memory byte by byte,
1235 each byte encoded as two hex characters. */
1236
1237 p = buf;
1238 for (i = 0; i < todo; i++)
1239 {
1240 if (p[0] == 0 || p[1] == 0)
1241 /* Reply is short. This means that we were able to read only part
1242 of what we wanted to. */
1243 return i + (origlen - len);
1244 myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
1245 p += 2;
1246 }
1247 myaddr += todo;
1248 memaddr += todo;
1249 len -= todo;
1250 }
1251 return origlen;
1252 }
1253 \f
1254 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
1255 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
1256 nonzero. Returns length of data written or read; 0 for error. */
1257
1258 /* ARGSUSED */
1259 static int
1260 remote_xfer_memory(memaddr, myaddr, len, should_write, target)
1261 CORE_ADDR memaddr;
1262 char *myaddr;
1263 int len;
1264 int should_write;
1265 struct target_ops *target; /* ignored */
1266 {
1267 return dcache_xfer_memory (remote_dcache, memaddr, myaddr, len, should_write);
1268 }
1269
1270
1271 #if 0
1272 /* Enable after 4.12. */
1273
1274 void
1275 remote_search (len, data, mask, startaddr, increment, lorange, hirange
1276 addr_found, data_found)
1277 int len;
1278 char *data;
1279 char *mask;
1280 CORE_ADDR startaddr;
1281 int increment;
1282 CORE_ADDR lorange;
1283 CORE_ADDR hirange;
1284 CORE_ADDR *addr_found;
1285 char *data_found;
1286 {
1287 if (increment == -4 && len == 4)
1288 {
1289 long mask_long, data_long;
1290 long data_found_long;
1291 CORE_ADDR addr_we_found;
1292 char buf[PBUFSIZ];
1293 long returned_long[2];
1294 char *p;
1295
1296 mask_long = extract_unsigned_integer (mask, len);
1297 data_long = extract_unsigned_integer (data, len);
1298 sprintf (buf, "t%x:%x,%x", startaddr, data_long, mask_long);
1299 putpkt (buf);
1300 getpkt (buf, 0);
1301 if (buf[0] == '\0')
1302 {
1303 /* The stub doesn't support the 't' request. We might want to
1304 remember this fact, but on the other hand the stub could be
1305 switched on us. Maybe we should remember it only until
1306 the next "target remote". */
1307 generic_search (len, data, mask, startaddr, increment, lorange,
1308 hirange, addr_found, data_found);
1309 return;
1310 }
1311
1312 if (buf[0] == 'E')
1313 /* There is no correspondance between what the remote protocol uses
1314 for errors and errno codes. We would like a cleaner way of
1315 representing errors (big enough to include errno codes, bfd_error
1316 codes, and others). But for now just use EIO. */
1317 memory_error (EIO, startaddr);
1318 p = buf;
1319 addr_we_found = 0;
1320 while (*p != '\0' && *p != ',')
1321 addr_we_found = (addr_we_found << 4) + fromhex (*p++);
1322 if (*p == '\0')
1323 error ("Protocol error: short return for search");
1324
1325 data_found_long = 0;
1326 while (*p != '\0' && *p != ',')
1327 data_found_long = (data_found_long << 4) + fromhex (*p++);
1328 /* Ignore anything after this comma, for future extensions. */
1329
1330 if (addr_we_found < lorange || addr_we_found >= hirange)
1331 {
1332 *addr_found = 0;
1333 return;
1334 }
1335
1336 *addr_found = addr_we_found;
1337 *data_found = store_unsigned_integer (data_we_found, len);
1338 return;
1339 }
1340 generic_search (len, data, mask, startaddr, increment, lorange,
1341 hirange, addr_found, data_found);
1342 }
1343 #endif /* 0 */
1344 \f
1345 static void
1346 remote_files_info (ignore)
1347 struct target_ops *ignore;
1348 {
1349 puts_filtered ("Debugging a target over a serial line.\n");
1350 }
1351 \f
1352 /* Stuff for dealing with the packets which are part of this protocol.
1353 See comment at top of file for details. */
1354
1355 /* Read a single character from the remote end, masking it down to 7 bits. */
1356
1357 static int
1358 readchar (timeout)
1359 int timeout;
1360 {
1361 int ch;
1362
1363 ch = SERIAL_READCHAR (remote_desc, timeout);
1364
1365 switch (ch)
1366 {
1367 case SERIAL_EOF:
1368 error ("Remote connection closed");
1369 case SERIAL_ERROR:
1370 perror_with_name ("Remote communication error");
1371 case SERIAL_TIMEOUT:
1372 return ch;
1373 default:
1374 return ch & 0x7f;
1375 }
1376 }
1377
1378 /* Send the command in BUF to the remote machine,
1379 and read the reply into BUF.
1380 Report an error if we get an error reply. */
1381
1382 static void
1383 remote_send (buf)
1384 char *buf;
1385 {
1386 putpkt (buf);
1387 getpkt (buf, 0);
1388
1389 if (buf[0] == 'E')
1390 error ("Remote failure reply: %s", buf);
1391 }
1392
1393 /* Send a packet to the remote machine, with error checking.
1394 The data of the packet is in BUF. */
1395
1396 static int
1397 putpkt (buf)
1398 char *buf;
1399 {
1400 int i;
1401 unsigned char csum = 0;
1402 char buf2[PBUFSIZ];
1403 int cnt = strlen (buf);
1404 int ch;
1405 int tcount = 0;
1406 char *p;
1407
1408 /* Copy the packet into buffer BUF2, encapsulating it
1409 and giving it a checksum. */
1410
1411 if (cnt > (int) sizeof (buf2) - 5) /* Prosanity check */
1412 abort();
1413
1414 p = buf2;
1415 *p++ = '$';
1416
1417 for (i = 0; i < cnt; i++)
1418 {
1419 csum += buf[i];
1420 *p++ = buf[i];
1421 }
1422 *p++ = '#';
1423 *p++ = tohex ((csum >> 4) & 0xf);
1424 *p++ = tohex (csum & 0xf);
1425
1426 /* Send it over and over until we get a positive ack. */
1427
1428 while (1)
1429 {
1430 int started_error_output = 0;
1431
1432 if (remote_debug)
1433 {
1434 *p = '\0';
1435 printf_unfiltered ("Sending packet: %s...", buf2);
1436 gdb_flush(gdb_stdout);
1437 }
1438 if (SERIAL_WRITE (remote_desc, buf2, p - buf2))
1439 perror_with_name ("putpkt: write failed");
1440
1441 /* read until either a timeout occurs (-2) or '+' is read */
1442 while (1)
1443 {
1444 ch = readchar (remote_timeout);
1445
1446 if (remote_debug)
1447 {
1448 switch (ch)
1449 {
1450 case '+':
1451 case SERIAL_TIMEOUT:
1452 case '$':
1453 if (started_error_output)
1454 {
1455 putchar_unfiltered ('\n');
1456 started_error_output = 0;
1457 }
1458 }
1459 }
1460
1461 switch (ch)
1462 {
1463 case '+':
1464 if (remote_debug)
1465 printf_unfiltered("Ack\n");
1466 return 1;
1467 case SERIAL_TIMEOUT:
1468 tcount ++;
1469 if (tcount > 3)
1470 return 0;
1471 break; /* Retransmit buffer */
1472 case '$':
1473 {
1474 char junkbuf[PBUFSIZ];
1475
1476 /* It's probably an old response, and we're out of sync. Just
1477 gobble up the packet and ignore it. */
1478 getpkt (junkbuf, 0);
1479 continue; /* Now, go look for + */
1480 }
1481 default:
1482 if (remote_debug)
1483 {
1484 if (!started_error_output)
1485 {
1486 started_error_output = 1;
1487 printf_unfiltered ("putpkt: Junk: ");
1488 }
1489 putchar_unfiltered (ch & 0177);
1490 }
1491 continue;
1492 }
1493 break; /* Here to retransmit */
1494 }
1495
1496 #if 0
1497 /* This is wrong. If doing a long backtrace, the user should be
1498 able to get out next time we call QUIT, without anything as violent
1499 as interrupt_query. If we want to provide a way out of here
1500 without getting to the next QUIT, it should be based on hitting
1501 ^C twice as in remote_wait. */
1502 if (quit_flag)
1503 {
1504 quit_flag = 0;
1505 interrupt_query ();
1506 }
1507 #endif
1508 }
1509 }
1510
1511 /* Come here after finding the start of the frame. Collect the rest into BUF,
1512 verifying the checksum, length, and handling run-length compression.
1513 Returns 0 on any error, 1 on success. */
1514
1515 static int
1516 read_frame (buf)
1517 char *buf;
1518 {
1519 unsigned char csum;
1520 char *bp;
1521 int c;
1522
1523 csum = 0;
1524 bp = buf;
1525
1526 while (1)
1527 {
1528 c = readchar (remote_timeout);
1529
1530 switch (c)
1531 {
1532 case SERIAL_TIMEOUT:
1533 if (remote_debug)
1534 puts_filtered ("Timeout in mid-packet, retrying\n");
1535 return 0;
1536 case '$':
1537 if (remote_debug)
1538 puts_filtered ("Saw new packet start in middle of old one\n");
1539 return 0; /* Start a new packet, count retries */
1540 case '#':
1541 {
1542 unsigned char pktcsum;
1543
1544 *bp = '\000';
1545
1546 pktcsum = fromhex (readchar (remote_timeout)) << 4;
1547 pktcsum |= fromhex (readchar (remote_timeout));
1548
1549 if (csum == pktcsum)
1550 return 1;
1551
1552 if (remote_debug)
1553 {
1554 printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
1555 pktcsum, csum);
1556 puts_filtered (buf);
1557 puts_filtered ("\n");
1558 }
1559 return 0;
1560 }
1561 case '*': /* Run length encoding */
1562 csum += c;
1563 c = readchar (remote_timeout);
1564 csum += c;
1565 c = c - ' ' + 3; /* Compute repeat count */
1566
1567
1568 if (c > 0 && c < 255 && bp + c - 1 < buf + PBUFSIZ - 1)
1569 {
1570 memset (bp, *(bp - 1), c);
1571 bp += c;
1572 continue;
1573 }
1574
1575 *bp = '\0';
1576 printf_filtered ("Repeat count %d too large for buffer: ", c);
1577 puts_filtered (buf);
1578 puts_filtered ("\n");
1579 return 0;
1580
1581 default:
1582 if (bp < buf + PBUFSIZ - 1)
1583 {
1584 *bp++ = c;
1585 csum += c;
1586 continue;
1587 }
1588
1589 *bp = '\0';
1590 puts_filtered ("Remote packet too long: ");
1591 puts_filtered (buf);
1592 puts_filtered ("\n");
1593
1594 return 0;
1595 }
1596 }
1597 }
1598
1599 /* Read a packet from the remote machine, with error checking,
1600 and store it in BUF. BUF is expected to be of size PBUFSIZ.
1601 If FOREVER, wait forever rather than timing out; this is used
1602 while the target is executing user code. */
1603
1604 static void
1605 getpkt (buf, forever)
1606 char *buf;
1607 int forever;
1608 {
1609 int c;
1610 int tries;
1611 int timeout;
1612 int val;
1613
1614 strcpy (buf,"timeout");
1615
1616 if (forever)
1617 {
1618 #ifdef MAINTENANCE_CMDS
1619 timeout = watchdog > 0 ? watchdog : -1;
1620 #else
1621 timeout = -1;
1622 #endif
1623 }
1624
1625 else
1626 timeout = remote_timeout;
1627
1628 #define MAX_TRIES 3
1629
1630 for (tries = 1; tries <= MAX_TRIES; tries++)
1631 {
1632 /* This can loop forever if the remote side sends us characters
1633 continuously, but if it pauses, we'll get a zero from readchar
1634 because of timeout. Then we'll count that as a retry. */
1635
1636 /* Note that we will only wait forever prior to the start of a packet.
1637 After that, we expect characters to arrive at a brisk pace. They
1638 should show up within remote_timeout intervals. */
1639
1640 do
1641 {
1642 c = readchar (timeout);
1643
1644 if (c == SERIAL_TIMEOUT)
1645 {
1646 #ifdef MAINTENANCE_CMDS
1647 if (forever) /* Watchdog went off. Kill the target. */
1648 {
1649 target_mourn_inferior ();
1650 error ("Watchdog has expired. Target detached.\n");
1651 }
1652 #endif
1653 if (remote_debug)
1654 puts_filtered ("Timed out.\n");
1655 goto retry;
1656 }
1657 }
1658 while (c != '$');
1659
1660 /* We've found the start of a packet, now collect the data. */
1661
1662 val = read_frame (buf);
1663
1664 if (val == 1)
1665 {
1666 if (remote_debug)
1667 fprintf_unfiltered (gdb_stderr, "Packet received: %s\n", buf);
1668 SERIAL_WRITE (remote_desc, "+", 1);
1669 return;
1670 }
1671
1672 /* Try the whole thing again. */
1673 retry:
1674 SERIAL_WRITE (remote_desc, "-", 1);
1675 }
1676
1677 /* We have tried hard enough, and just can't receive the packet. Give up. */
1678
1679 printf_unfiltered ("Ignoring packet error, continuing...\n");
1680 SERIAL_WRITE (remote_desc, "+", 1);
1681 }
1682 \f
1683 static void
1684 remote_kill ()
1685 {
1686 /* For some mysterious reason, wait_for_inferior calls kill instead of
1687 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
1688 if (kill_kludge)
1689 {
1690 kill_kludge = 0;
1691 target_mourn_inferior ();
1692 return;
1693 }
1694
1695 /* Use catch_errors so the user can quit from gdb even when we aren't on
1696 speaking terms with the remote system. */
1697 catch_errors (putpkt, "k", "", RETURN_MASK_ERROR);
1698
1699 /* Don't wait for it to die. I'm not really sure it matters whether
1700 we do or not. For the existing stubs, kill is a noop. */
1701 target_mourn_inferior ();
1702 }
1703
1704 static void
1705 remote_mourn ()
1706 {
1707 remote_mourn_1 (&remote_ops);
1708 }
1709
1710 static void
1711 extended_remote_mourn ()
1712 {
1713 /* We do _not_ want to mourn the target like this; this will
1714 remove the extended remote target from the target stack,
1715 and the next time the user says "run" it'll fail.
1716
1717 FIXME: What is the right thing to do here? */
1718 #if 0
1719 remote_mourn_1 (&extended_remote_ops);
1720 #endif
1721 }
1722
1723 /* Worker function for remote_mourn. */
1724 static void
1725 remote_mourn_1 (target)
1726 struct target_ops *target;
1727 {
1728 unpush_target (target);
1729 generic_mourn_inferior ();
1730 }
1731
1732 /* In the extended protocol we want to be able to do things like
1733 "run" and have them basically work as expected. So we need
1734 a special create_inferior function.
1735
1736 FIXME: One day add support for changing the exec file
1737 we're debugging, arguments and an environment. */
1738
1739 static void
1740 extended_remote_create_inferior (exec_file, args, env)
1741 char *exec_file;
1742 char *args;
1743 char **env;
1744 {
1745 /* Rip out the breakpoints; we'll reinsert them after restarting
1746 the remote server. */
1747 remove_breakpoints ();
1748
1749 /* Now restart the remote server. */
1750 extended_remote_restart ();
1751
1752 /* Now put the breakpoints back in. This way we're safe if the
1753 restart function works via a unix fork on the remote side. */
1754 insert_breakpoints ();
1755
1756 /* Clean up from the last time we were running. */
1757 clear_proceed_status ();
1758
1759 /* Let the remote process run. */
1760 proceed (-1, TARGET_SIGNAL_0, 0);
1761 }
1762
1763 \f
1764 /* On some machines, e.g. 68k, we may use a different breakpoint instruction
1765 than other targets; in those use REMOTE_BREAKPOINT instead of just
1766 BREAKPOINT. Also, bi-endian targets may define LITTLE_REMOTE_BREAKPOINT
1767 and BIG_REMOTE_BREAKPOINT. If none of these are defined, we just call
1768 the standard routines that are in mem-break.c. */
1769
1770 /* FIXME, these ought to be done in a more dynamic fashion. For instance,
1771 the choice of breakpoint instruction affects target program design and
1772 vice versa, and by making it user-tweakable, the special code here
1773 goes away and we need fewer special GDB configurations. */
1774
1775 #if defined (LITTLE_REMOTE_BREAKPOINT) && defined (BIG_REMOTE_BREAKPOINT) && !defined(REMOTE_BREAKPOINT)
1776 #define REMOTE_BREAKPOINT
1777 #endif
1778
1779 #ifdef REMOTE_BREAKPOINT
1780
1781 /* If the target isn't bi-endian, just pretend it is. */
1782 #if !defined (LITTLE_REMOTE_BREAKPOINT) && !defined (BIG_REMOTE_BREAKPOINT)
1783 #define LITTLE_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
1784 #define BIG_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
1785 #endif
1786
1787 static unsigned char big_break_insn[] = BIG_REMOTE_BREAKPOINT;
1788 static unsigned char little_break_insn[] = LITTLE_REMOTE_BREAKPOINT;
1789
1790 #endif /* REMOTE_BREAKPOINT */
1791
1792 /* Insert a breakpoint on targets that don't have any better breakpoint
1793 support. We read the contents of the target location and stash it,
1794 then overwrite it with a breakpoint instruction. ADDR is the target
1795 location in the target machine. CONTENTS_CACHE is a pointer to
1796 memory allocated for saving the target contents. It is guaranteed
1797 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
1798 is accomplished via BREAKPOINT_MAX). */
1799
1800 static int
1801 remote_insert_breakpoint (addr, contents_cache)
1802 CORE_ADDR addr;
1803 char *contents_cache;
1804 {
1805 #ifdef REMOTE_BREAKPOINT
1806 int val;
1807
1808 val = target_read_memory (addr, contents_cache, sizeof big_break_insn);
1809
1810 if (val == 0)
1811 {
1812 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
1813 val = target_write_memory (addr, (char *) big_break_insn,
1814 sizeof big_break_insn);
1815 else
1816 val = target_write_memory (addr, (char *) little_break_insn,
1817 sizeof little_break_insn);
1818 }
1819
1820 return val;
1821 #else
1822 return memory_insert_breakpoint (addr, contents_cache);
1823 #endif /* REMOTE_BREAKPOINT */
1824 }
1825
1826 static int
1827 remote_remove_breakpoint (addr, contents_cache)
1828 CORE_ADDR addr;
1829 char *contents_cache;
1830 {
1831 #ifdef REMOTE_BREAKPOINT
1832 return target_write_memory (addr, contents_cache, sizeof big_break_insn);
1833 #else
1834 return memory_remove_breakpoint (addr, contents_cache);
1835 #endif /* REMOTE_BREAKPOINT */
1836 }
1837 \f
1838 /* Define the target subroutine names */
1839
1840 static struct target_ops remote_ops =
1841 {
1842 "remote", /* to_shortname */
1843 "Remote serial target in gdb-specific protocol", /* to_longname */
1844 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
1845 Specify the serial device it is connected to (e.g. /dev/ttya).", /* to_doc */
1846 remote_open, /* to_open */
1847 remote_close, /* to_close */
1848 NULL, /* to_attach */
1849 remote_detach, /* to_detach */
1850 remote_resume, /* to_resume */
1851 remote_wait, /* to_wait */
1852 remote_fetch_registers, /* to_fetch_registers */
1853 remote_store_registers, /* to_store_registers */
1854 remote_prepare_to_store, /* to_prepare_to_store */
1855 remote_xfer_memory, /* to_xfer_memory */
1856 remote_files_info, /* to_files_info */
1857 remote_insert_breakpoint, /* to_insert_breakpoint */
1858 remote_remove_breakpoint, /* to_remove_breakpoint */
1859 NULL, /* to_terminal_init */
1860 NULL, /* to_terminal_inferior */
1861 NULL, /* to_terminal_ours_for_output */
1862 NULL, /* to_terminal_ours */
1863 NULL, /* to_terminal_info */
1864 remote_kill, /* to_kill */
1865 generic_load, /* to_load */
1866 NULL, /* to_lookup_symbol */
1867 NULL, /* to_create_inferior */
1868 remote_mourn, /* to_mourn_inferior */
1869 0, /* to_can_run */
1870 0, /* to_notice_signals */
1871 remote_thread_alive, /* to_thread_alive */
1872 0, /* to_stop */
1873 process_stratum, /* to_stratum */
1874 NULL, /* to_next */
1875 1, /* to_has_all_memory */
1876 1, /* to_has_memory */
1877 1, /* to_has_stack */
1878 1, /* to_has_registers */
1879 1, /* to_has_execution */
1880 NULL, /* sections */
1881 NULL, /* sections_end */
1882 OPS_MAGIC /* to_magic */
1883 };
1884
1885 static struct target_ops extended_remote_ops =
1886 {
1887 "extended-remote", /* to_shortname */
1888 "Extended remote serial target in gdb-specific protocol",/* to_longname */
1889 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
1890 Specify the serial device it is connected to (e.g. /dev/ttya).", /* to_doc */
1891 extended_remote_open, /* to_open */
1892 remote_close, /* to_close */
1893 NULL, /* to_attach */
1894 remote_detach, /* to_detach */
1895 remote_resume, /* to_resume */
1896 remote_wait, /* to_wait */
1897 remote_fetch_registers, /* to_fetch_registers */
1898 remote_store_registers, /* to_store_registers */
1899 remote_prepare_to_store, /* to_prepare_to_store */
1900 remote_xfer_memory, /* to_xfer_memory */
1901 remote_files_info, /* to_files_info */
1902
1903 remote_insert_breakpoint, /* to_insert_breakpoint */
1904 remote_remove_breakpoint, /* to_remove_breakpoint */
1905
1906 NULL, /* to_terminal_init */
1907 NULL, /* to_terminal_inferior */
1908 NULL, /* to_terminal_ours_for_output */
1909 NULL, /* to_terminal_ours */
1910 NULL, /* to_terminal_info */
1911 remote_kill, /* to_kill */
1912 generic_load, /* to_load */
1913 NULL, /* to_lookup_symbol */
1914 extended_remote_create_inferior,/* to_create_inferior */
1915 extended_remote_mourn, /* to_mourn_inferior */
1916 0, /* to_can_run */
1917 0, /* to_notice_signals */
1918 remote_thread_alive, /* to_thread_alive */
1919 0, /* to_stop */
1920 process_stratum, /* to_stratum */
1921 NULL, /* to_next */
1922 1, /* to_has_all_memory */
1923 1, /* to_has_memory */
1924 1, /* to_has_stack */
1925 1, /* to_has_registers */
1926 1, /* to_has_execution */
1927 NULL, /* sections */
1928 NULL, /* sections_end */
1929 OPS_MAGIC /* to_magic */
1930 };
1931
1932 /* Some targets are only capable of doing downloads, and afterwards they switch
1933 to the remote serial protocol. This function provides a clean way to get
1934 from the download target to the remote target. It's basically just a
1935 wrapper so that we don't have to expose any of the internal workings of
1936 remote.c.
1937
1938 Prior to calling this routine, you should shutdown the current target code,
1939 else you will get the "A program is being debugged already..." message.
1940 Usually a call to pop_target() suffices.
1941 */
1942
1943 void
1944 push_remote_target (name, from_tty)
1945 char *name;
1946 int from_tty;
1947 {
1948 printf_filtered ("Switching to remote protocol\n");
1949 remote_open (name, from_tty);
1950 }
1951
1952 void
1953 _initialize_remote ()
1954 {
1955 add_target (&remote_ops);
1956 add_target (&extended_remote_ops);
1957
1958 add_show_from_set (add_set_cmd ("remotetimeout", no_class,
1959 var_integer, (char *)&remote_timeout,
1960 "Set timeout value for remote read.\n", &setlist),
1961 &showlist);
1962
1963 add_show_from_set (add_set_cmd ("remotebreak", no_class,
1964 var_integer, (char *)&remote_break,
1965 "Set whether to send break if interrupted.\n", &setlist),
1966 &showlist);
1967
1968 add_show_from_set (add_set_cmd ("remotewritesize", no_class,
1969 var_integer, (char *)&remote_write_size,
1970 "Set the maximum number of bytes in each memory write packet.\n", &setlist),
1971 &showlist);
1972 }
This page took 0.068696 seconds and 4 git commands to generate.