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