* corelow.c, exec.c, inftarg.c, m3-nat.c, op50-rom.c, procfs.c,
[deliverable/binutils-gdb.git] / gdb / remote-pa.c
1 /* Remote target communications for serial-line targets in custom GDB protocol
2 Copyright 1988, 1991, 1992, 1993, 1994 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., 675 Mass Ave, Cambridge, MA 02139, 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 All values are encoded in ascii hex digits.
42
43 Request Packet
44
45 read registers g
46 reply XX....X Each byte of register data
47 is described by two hex digits.
48 Registers are in the internal order
49 for GDB, and the bytes in a register
50 are in the same order the machine uses.
51 or ENN for an error.
52
53 write regs GXX..XX Each byte of register data
54 is described by two hex digits.
55 reply OK for success
56 ENN for an error
57
58 write reg Pn...=r... Write register n... with value r...,
59 which contains two hex digits for each
60 byte in the register (target byte
61 order).
62 reply OK for success
63 ENN for an error
64 (not supported by all stubs).
65
66 read mem mAA..AA,LLLL AA..AA is address, LLLL is length.
67 reply XX..XX XX..XX is mem contents
68 Can be fewer bytes than requested
69 if able to read only part of the data.
70 or ENN NN is errno
71
72 write mem MAA..AA,LLLL:XX..XX
73 AA..AA is address,
74 LLLL is number of bytes,
75 XX..XX is data
76 reply OK for success
77 ENN for an error (this includes the case
78 where only part of the data was
79 written).
80
81 cont cAA..AA AA..AA is address to resume
82 If AA..AA is omitted,
83 resume at same address.
84
85 step sAA..AA AA..AA is address to resume
86 If AA..AA is omitted,
87 resume at same address.
88
89 last signal ? Reply the current reason for stopping.
90 This is the same reply as is generated
91 for step or cont : SAA where AA is the
92 signal number.
93
94 There is no immediate reply to step or cont.
95 The reply comes when the machine stops.
96 It is SAA AA is the "signal number"
97
98 or... TAAn...:r...;n:r...;n...:r...;
99 AA = signal number
100 n... = register number
101 r... = register contents
102 or... WAA The process exited, and AA is
103 the exit status. This is only
104 applicable for certains sorts of
105 targets.
106 kill request k
107
108 toggle debug d toggle debug flag (see 386 & 68k stubs)
109 reset r reset -- see sparc stub.
110 reserved <other> On other requests, the stub should
111 ignore the request and send an empty
112 response ($#<checksum>). This way
113 we can extend the protocol and GDB
114 can tell whether the stub it is
115 talking to uses the old or the new.
116 search tAA:PP,MM Search backwards starting at address
117 AA for a match with pattern PP and
118 mask MM. PP and MM are 4 bytes.
119 Not supported by all stubs.
120
121 general query qXXXX Request info about XXXX.
122 general set QXXXX=yyyy Set value of XXXX to yyyy.
123 query sect offs qOffsets Get section offsets. Reply is
124 Text=xxx;Data=yyy;Bss=zzz
125 console output Otext Send text to stdout. Only comes from
126 remote target.
127
128 Responses can be run-length encoded to save space. A '*' means that
129 the next character is an ASCII encoding giving a repeat count which
130 stands for that many repititions of the character preceding the '*'.
131 The encoding is n+29, yielding a printable character where n >=3
132 (which is where rle starts to win). Don't use an n > 126.
133
134 So
135 "0* " means the same as "0000". */
136
137 #include "defs.h"
138 #include <string.h>
139 #include <fcntl.h>
140 #include "frame.h"
141 #include "inferior.h"
142 #include "bfd.h"
143 #include "symfile.h"
144 #include "target.h"
145 #include "wait.h"
146 #include "terminal.h"
147 #include "gdbcmd.h"
148 #include "objfiles.h"
149 #include "gdb-stabs.h"
150 #include "remote-utils.h"
151 #include "dcache.h"
152
153 #ifdef USG
154 #include <sys/types.h>
155 #endif
156
157 #include <signal.h>
158 #include "serial.h"
159
160 /* Prototypes for local functions */
161
162 static int
163 remote_write_bytes PARAMS ((CORE_ADDR memaddr, unsigned char *myaddr, int len));
164
165 static int
166 remote_read_bytes PARAMS ((CORE_ADDR memaddr, unsigned char *myaddr, int len));
167
168 static void
169 remote_files_info PARAMS ((struct target_ops *ignore));
170
171 static int
172 remote_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr, int len,
173 int should_write, struct target_ops *target));
174
175 static void
176 remote_prepare_to_store PARAMS ((void));
177
178 static void
179 remote_fetch_registers PARAMS ((int regno));
180
181 static void
182 remote_resume PARAMS ((int pid, int step, enum target_signal siggnal));
183
184 static int
185 remote_start_remote PARAMS ((char *dummy));
186
187 static void
188 remote_open PARAMS ((char *name, int from_tty));
189
190 static void
191 remote_close PARAMS ((int quitting));
192
193 static void
194 remote_store_registers PARAMS ((int regno));
195
196 static void
197 getpkt PARAMS ((char *buf, int forever));
198
199 static void
200 putpkt PARAMS ((char *buf));
201
202 static void
203 remote_send PARAMS ((char *buf));
204
205 static int
206 readchar PARAMS ((int timeout));
207
208 static int
209 remote_wait PARAMS ((int pid, struct target_waitstatus *status));
210
211 static int
212 tohex PARAMS ((int nib));
213
214 static int
215 fromhex PARAMS ((int a));
216
217 static void
218 remote_detach PARAMS ((char *args, int from_tty));
219
220 static void
221 remote_interrupt PARAMS ((int signo));
222
223 static void
224 remote_interrupt_twice PARAMS ((int signo));
225
226 static void
227 interrupt_query PARAMS ((void));
228
229 static void
230 hppro_load PARAMS ((char *name, int from_tty));
231
232 extern struct target_ops remote_ops; /* Forward decl */
233
234 /* This was 5 seconds, which is a long time to sit and wait.
235 Unless this is going though some terminal server or multiplexer or
236 other form of hairy serial connection, I would think 2 seconds would
237 be plenty. */
238 static int remote_timeout = 2;
239
240 #if 0
241 int icache;
242 #endif
243
244 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
245 remote_open knows that we don't have a file open when the program
246 starts. */
247 extern serial_t remote_desc;
248
249 /* Having this larger than 400 causes us to be incompatible with m68k-stub.c
250 and i386-stub.c. Normally, no one would notice because it only matters
251 for writing large chunks of memory (e.g. in downloads). Also, this needs
252 to be more than 400 if required to hold the registers (see below, where
253 we round it up based on REGISTER_BYTES). */
254 #define PBUFSIZ 400
255
256 /* Maximum number of bytes to read/write at once. The value here
257 is chosen to fill up a packet (the headers account for the 32). */
258 #define MAXBUFBYTES ((PBUFSIZ-32)/2)
259
260 /* Round up PBUFSIZ to hold all the registers, at least. */
261 /* The blank line after the #if seems to be required to work around a
262 bug in HP's PA compiler. */
263 #if REGISTER_BYTES > MAXBUFBYTES
264
265 #undef PBUFSIZ
266 #define PBUFSIZ (REGISTER_BYTES * 2 + 32)
267 #endif
268
269 /* Should we try the 'P' request? If this is set to one when the stub
270 doesn't support 'P', the only consequence is some unnecessary traffic. */
271 static int stub_supports_P = 1;
272
273 /* sets the download protocol, choices are srec, generic, boot */
274 char *loadtype;
275 static char *loadtype_str;
276 static void set_loadtype_command
277 PARAMS ((char *, int, struct cmd_list_element *));
278
279 static void
280 hppro_load (file, from_tty)
281 char *file;
282 int from_tty;
283 {
284 puts ("Loading... HA!");
285 }
286
287 \f
288 /* Clean up connection to a remote debugger. */
289
290 /* ARGSUSED */
291 static void
292 remote_close (quitting)
293 int quitting;
294 {
295 if (remote_desc)
296 SERIAL_CLOSE (remote_desc);
297 remote_desc = NULL;
298 }
299
300 /* Query the remote side for the text, data and bss offsets. */
301
302 static void
303 get_offsets ()
304 {
305 unsigned char buf[PBUFSIZ];
306 int nvals;
307 CORE_ADDR text_addr, data_addr, bss_addr;
308 struct section_offsets *offs;
309
310 putpkt ("qOffsets");
311
312 getpkt (buf, 0);
313
314 if (buf[0] == '\000')
315 return; /* Return silently. Stub doesn't support this
316 command. */
317 if (buf[0] == 'E')
318 {
319 warning ("Remote failure reply: %s", buf);
320 return;
321 }
322
323 nvals = sscanf (buf, "Text=%lx;Data=%lx;Bss=%lx", &text_addr, &data_addr,
324 &bss_addr);
325 if (nvals != 3)
326 error ("Malformed response to offset query, %s", buf);
327
328 if (symfile_objfile == NULL)
329 return;
330
331 offs = (struct section_offsets *) alloca (sizeof (struct section_offsets)
332 + symfile_objfile->num_sections
333 * sizeof (offs->offsets));
334 memcpy (offs, symfile_objfile->section_offsets,
335 sizeof (struct section_offsets)
336 + symfile_objfile->num_sections
337 * sizeof (offs->offsets));
338
339 /* FIXME: This code assumes gdb-stabs.h is being used; it's broken
340 for xcoff, dwarf, sdb-coff, etc. But there is no simple
341 canonical representation for this stuff. (Just what does "text"
342 as seen by the stub mean, anyway? I think it means all sections
343 with SEC_CODE set, but we currently have no way to deal with that). */
344
345 ANOFFSET (offs, SECT_OFF_TEXT) = text_addr;
346
347 /* This is a temporary kludge to force data and bss to use the same offsets
348 because that's what nlmconv does now. The real solution requires changes
349 to the stub and remote.c that I don't have time to do right now. */
350
351 ANOFFSET (offs, SECT_OFF_DATA) = data_addr;
352 ANOFFSET (offs, SECT_OFF_BSS) = data_addr;
353
354 objfile_relocate (symfile_objfile, offs);
355 }
356
357 #define INBUFSIZE 10
358
359 void
360 boot_board()
361 {
362 char c;
363 char buf[INBUFSIZE];
364 char *ptr;
365
366 /* See if we can connect to the boot ROM command line */
367 ptr = buf;
368 while (1) {
369 SERIAL_WRITE (remote_desc, "\r\n", 2);
370 c = readchar (2);
371 if ((sr_get_debug() > 2) && (isascii(c)))
372 putchar (c);
373 if (c == SERIAL_TIMEOUT) {
374 if (sr_get_debug())
375 puts_filtered ("Timed out.\n");
376 break;
377 }
378 if (c == '&') {
379 if (sr_get_debug() > 2)
380 puts ("Got ACK from stub");
381 break;
382 }
383 if (c == '>') {
384 if (sr_get_debug() > 2)
385 puts ("Got prompt from ROM monitor");
386 break;
387 }
388 }
389
390 }
391
392 /* Stub for catch_errors. */
393 static int
394 remote_start_remote (dummy)
395 char *dummy;
396 {
397 int timeout;
398
399 immediate_quit = 1; /* Allow user to interrupt it */
400
401 /* Ack any packet which the remote side has already sent. */
402
403 if (sr_get_debug())
404 puts ("Trying a '+' to ACK the target.");
405
406 SERIAL_WRITE (remote_desc, "+", 1);
407
408 #if 0
409 boot_board();
410
411 get_offsets (); /* Get text, data & bss offsets */
412 #endif
413
414 putpkt ("?"); /* initiate a query from remote machine */
415 immediate_quit = 0;
416
417 start_remote (); /* Initialize gdb process mechanisms */
418
419 return 1;
420 }
421
422 /* Open a connection to a remote debugger.
423 NAME is the filename used for communication. */
424
425 static DCACHE *remote_dcache;
426
427 static void
428 remote_open (name, from_tty)
429 char *name;
430 int from_tty;
431 {
432 if (name == 0)
433 error (
434 "To open a remote debug connection, you need to specify what serial\n\
435 device is attached to the remote system (e.g. /dev/ttya).");
436
437 target_preopen (from_tty);
438
439 unpush_target (&remote_ops);
440
441 remote_dcache = dcache_init (remote_read_bytes, remote_write_bytes);
442
443 remote_desc = SERIAL_OPEN (name);
444 if (!remote_desc)
445 perror_with_name (name);
446
447 if (baud_rate != -1)
448 {
449 if (SERIAL_SETBAUDRATE (remote_desc, baud_rate))
450 {
451 SERIAL_CLOSE (remote_desc);
452 perror_with_name (name);
453 }
454 }
455
456 SERIAL_RAW (remote_desc);
457
458 /* If there is something sitting in the buffer we might take it as a
459 response to a command, which would be bad. */
460 SERIAL_FLUSH_INPUT (remote_desc);
461
462 if (from_tty)
463 {
464 puts_filtered ("Remote debugging using ");
465 puts_filtered (name);
466 puts_filtered ("\n");
467 }
468 push_target (&remote_ops); /* Switch to using remote target now */
469
470 /* Start out by trying the 'P' request to set registers. We set this each
471 time that we open a new target so that if the user switches from one
472 stub to another, we can (if the target is closed and reopened) cope. */
473 stub_supports_P = 1;
474
475 /* Without this, some commands which require an active target (such as kill)
476 won't work. This variable serves (at least) double duty as both the pid
477 of the target process (if it has such), and as a flag indicating that a
478 target is active. These functions should be split out into seperate
479 variables, especially since GDB will someday have a notion of debugging
480 several processes. */
481
482 inferior_pid = 42000;
483
484 /* Start the remote connection; if error (0), discard this target.
485 In particular, if the user quits, be sure to discard it
486 (we'd be in an inconsistent state otherwise). */
487 if (!catch_errors (remote_start_remote, (char *)0,
488 "Couldn't establish connection to remote target\n", RETURN_MASK_ALL))
489 pop_target();
490 }
491
492 /* remote_detach()
493 takes a program previously attached to and detaches it.
494 We better not have left any breakpoints
495 in the program or it'll die when it hits one.
496 Close the open connection to the remote debugger.
497 Use this when you want to detach and do something else
498 with your gdb. */
499
500 static void
501 remote_detach (args, from_tty)
502 char *args;
503 int from_tty;
504 {
505 if (args)
506 error ("Argument given to \"detach\" when remotely debugging.");
507
508 pop_target ();
509 if (from_tty)
510 puts_filtered ("Ending remote debugging.\n");
511 }
512
513 /* Convert hex digit A to a number. */
514
515 static int
516 fromhex (a)
517 int a;
518 {
519 if (a >= '0' && a <= '9')
520 return a - '0';
521 else if (a >= 'a' && a <= 'f')
522 return a - 'a' + 10;
523 else
524 error ("Reply contains invalid hex digit");
525 }
526
527 /* Convert number NIB to a hex digit. */
528
529 static int
530 tohex (nib)
531 int nib;
532 {
533 if (nib < 10)
534 return '0'+nib;
535 else
536 return 'a'+nib-10;
537 }
538 \f
539 /* Tell the remote machine to resume. */
540
541 static void
542 remote_resume (pid, step, siggnal)
543 int pid, step;
544 enum target_signal siggnal;
545 {
546 char buf[PBUFSIZ];
547
548 if (siggnal)
549 {
550 target_terminal_ours_for_output ();
551 printf_filtered
552 ("Can't send signals to a remote system. %s not sent.\n",
553 target_signal_to_name (siggnal)); target_terminal_inferior ();
554 }
555
556 dcache_flush (remote_dcache);
557
558 strcpy (buf, step ? "s": "c");
559
560 putpkt (buf);
561 }
562 \f
563 /* Send ^C to target to halt it. Target will respond, and send us a
564 packet. */
565
566 static void
567 remote_interrupt (signo)
568 int signo;
569 {
570 /* If this doesn't work, try more severe steps. */
571 signal (signo, remote_interrupt_twice);
572
573 if (remote_debug)
574 printf_unfiltered ("remote_interrupt called\n");
575
576 SERIAL_WRITE (remote_desc, "\003", 1); /* Send a ^C */
577 }
578
579 static void (*ofunc)();
580
581 /* The user typed ^C twice. */
582 static void
583 remote_interrupt_twice (signo)
584 int signo;
585 {
586 signal (signo, ofunc);
587
588 interrupt_query ();
589
590 signal (signo, remote_interrupt);
591 }
592
593 /* Ask the user what to do when an interrupt is received. */
594
595 static void
596 interrupt_query ()
597 {
598 target_terminal_ours ();
599
600 if (query ("Interrupted while waiting for the program.\n\
601 Give up (and stop debugging it)? "))
602 {
603 target_mourn_inferior ();
604 return_to_top_level (RETURN_QUIT);
605 }
606
607 target_terminal_inferior ();
608 }
609
610 /* Wait until the remote machine stops, then return,
611 storing status in STATUS just as `wait' would.
612 Returns "pid" (though it's not clear what, if anything, that
613 means in the case of this target). */
614
615 static int
616 remote_wait (pid, status)
617 int pid;
618 struct target_waitstatus *status;
619 {
620 unsigned char buf[PBUFSIZ];
621
622 status->kind = TARGET_WAITKIND_EXITED;
623 status->value.integer = 0;
624
625 while (1)
626 {
627 unsigned char *p;
628
629 ofunc = (void (*)()) signal (SIGINT, remote_interrupt);
630 getpkt ((char *) buf, 1);
631 signal (SIGINT, ofunc);
632
633 switch (buf[0])
634 {
635 case 'E': /* Error of some sort */
636 warning ("Remote failure reply: %s", buf);
637 continue;
638 case 'T': /* Status with PC, SP, FP, ... */
639 {
640 int i;
641 long regno;
642 char regs[MAX_REGISTER_RAW_SIZE];
643
644 /* Expedited reply, containing Signal, {regno, reg} repeat */
645 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
646 ss = signal number
647 n... = register number
648 r... = register contents
649 */
650
651 p = &buf[3]; /* after Txx */
652
653 while (*p)
654 {
655 unsigned char *p1;
656
657 regno = strtol (p, &p1, 16); /* Read the register number */
658
659 if (p1 == p)
660 warning ("Remote sent badly formed register number: %s\nPacket: '%s'\n",
661 p1, buf);
662
663 p = p1;
664
665 if (*p++ != ':')
666 warning ("Malformed packet (missing colon): %s\nPacket: '%s'\n",
667 p, buf);
668
669 if (regno >= NUM_REGS)
670 warning ("Remote sent bad register number %d: %s\nPacket: '%s'\n",
671 regno, p, buf);
672
673 for (i = 0; i < REGISTER_RAW_SIZE (regno); i++)
674 {
675 if (p[0] == 0 || p[1] == 0)
676 warning ("Remote reply is too short: %s", buf);
677 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
678 p += 2;
679 }
680
681 if (*p++ != ';')
682 warning ("Remote register badly formatted: %s", buf);
683
684 supply_register (regno, regs);
685 }
686 }
687 /* fall through */
688 case 'S': /* Old style status, just signal only */
689 status->kind = TARGET_WAITKIND_STOPPED;
690 status->value.sig = (enum target_signal)
691 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
692
693 return inferior_pid;
694 case 'W': /* Target exited */
695 {
696 /* The remote process exited. */
697 status->kind = TARGET_WAITKIND_EXITED;
698 status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
699 return inferior_pid;
700 }
701 case 'O': /* Console output */
702 fputs_filtered (buf + 1, gdb_stdout);
703 continue;
704 default:
705 warning ("Invalid remote reply: %s", buf);
706 continue;
707 }
708 }
709 return inferior_pid;
710 }
711
712 /* Number of bytes of registers this stub implements. */
713 static int register_bytes_found;
714
715 /* Read the remote registers into the block REGS. */
716 /* Currently we just read all the registers, so we don't use regno. */
717 /* ARGSUSED */
718 static void
719 remote_fetch_registers (regno)
720 int regno;
721 {
722 char buf[PBUFSIZ];
723 int i;
724 char *p;
725 char regs[REGISTER_BYTES];
726
727 sprintf (buf, "g");
728 remote_send (buf);
729
730 /* Unimplemented registers read as all bits zero. */
731 memset (regs, 0, REGISTER_BYTES);
732
733 /* We can get out of synch in various cases. If the first character
734 in the buffer is not a hex character, assume that has happened
735 and try to fetch another packet to read. */
736 while ((buf[0] < '0' || buf[0] > '9')
737 && (buf[0] < 'a' || buf[0] > 'f'))
738 {
739 if (remote_debug)
740 printf_unfiltered ("Bad register packet; fetching a new packet\n");
741 getpkt (buf, 0);
742 }
743
744 /* Reply describes registers byte by byte, each byte encoded as two
745 hex characters. Suck them all up, then supply them to the
746 register cacheing/storage mechanism. */
747
748 p = buf;
749 for (i = 0; i < REGISTER_BYTES; i++)
750 {
751 if (p[0] == 0)
752 break;
753 if (p[1] == 0)
754 {
755 warning ("Remote reply is of odd length: %s", buf);
756 /* Don't change register_bytes_found in this case, and don't
757 print a second warning. */
758 goto supply_them;
759 }
760 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
761 p += 2;
762 }
763
764 if (i != register_bytes_found)
765 {
766 register_bytes_found = i;
767 #ifdef REGISTER_BYTES_OK
768 if (!REGISTER_BYTES_OK (i))
769 warning ("Remote reply is too short: %s", buf);
770 #endif
771 }
772
773 supply_them:
774 for (i = 0; i < NUM_REGS; i++)
775 supply_register (i, &regs[REGISTER_BYTE(i)]);
776 }
777
778 /* Prepare to store registers. Since we may send them all (using a
779 'G' request), we have to read out the ones we don't want to change
780 first. */
781
782 static void
783 remote_prepare_to_store ()
784 {
785 /* Make sure the entire registers array is valid. */
786 read_register_bytes (0, (char *)NULL, REGISTER_BYTES);
787 }
788
789 /* Store register REGNO, or all registers if REGNO == -1, from the contents
790 of REGISTERS. FIXME: ignores errors. */
791
792 static void
793 remote_store_registers (regno)
794 int regno;
795 {
796 char buf[PBUFSIZ];
797 int i;
798 char *p;
799
800 if (regno >= 0 && stub_supports_P)
801 {
802 /* Try storing a single register. */
803 char *regp;
804
805 sprintf (buf, "P%x=", regno);
806 p = buf + strlen (buf);
807 regp = &registers[REGISTER_BYTE (regno)];
808 for (i = 0; i < REGISTER_RAW_SIZE (regno); ++i)
809 {
810 *p++ = tohex ((regp[i] >> 4) & 0xf);
811 *p++ = tohex (regp[i] & 0xf);
812 }
813 *p = '\0';
814 remote_send (buf);
815 if (buf[0] != '\0')
816 {
817 /* The stub understands the 'P' request. We are done. */
818 return;
819 }
820
821 /* The stub does not support the 'P' request. Use 'G' instead,
822 and don't try using 'P' in the future (it will just waste our
823 time). */
824 stub_supports_P = 0;
825 }
826
827 buf[0] = 'G';
828
829 /* Command describes registers byte by byte,
830 each byte encoded as two hex characters. */
831
832 p = buf + 1;
833 /* remote_prepare_to_store insures that register_bytes_found gets set. */
834 for (i = 0; i < register_bytes_found; i++)
835 {
836 *p++ = tohex ((registers[i] >> 4) & 0xf);
837 *p++ = tohex (registers[i] & 0xf);
838 }
839 *p = '\0';
840
841 remote_send (buf);
842 }
843
844 #if 0
845
846 /* Use of the data cache is disabled because it loses for looking at
847 and changing hardware I/O ports and the like. Accepting `volatile'
848 would perhaps be one way to fix it, but a better way which would
849 win for more cases would be to use the executable file for the text
850 segment, like the `icache' code below but done cleanly (in some
851 target-independent place, perhaps in target_xfer_memory, perhaps
852 based on assigning each target a speed or perhaps by some simpler
853 mechanism). */
854
855 /* Read a word from remote address ADDR and return it.
856 This goes through the data cache. */
857
858 static int
859 remote_fetch_word (addr)
860 CORE_ADDR addr;
861 {
862 #if 0
863 if (icache)
864 {
865 extern CORE_ADDR text_start, text_end;
866
867 if (addr >= text_start && addr < text_end)
868 {
869 int buffer;
870 xfer_core_file (addr, &buffer, sizeof (int));
871 return buffer;
872 }
873 }
874 #endif
875 return dcache_fetch (remote_dcache, addr);
876 }
877
878 /* Write a word WORD into remote address ADDR.
879 This goes through the data cache. */
880
881 static void
882 remote_store_word (addr, word)
883 CORE_ADDR addr;
884 int word;
885 {
886 dcache_poke (remote_dcache, addr, word);
887 }
888 #endif /* 0 */
889 \f
890 /* Write memory data directly to the remote machine.
891 This does not inform the data cache; the data cache uses this.
892 MEMADDR is the address in the remote memory space.
893 MYADDR is the address of the buffer in our space.
894 LEN is the number of bytes.
895
896 Returns number of bytes transferred, or 0 for error. */
897
898 static int
899 remote_write_bytes (memaddr, myaddr, len)
900 CORE_ADDR memaddr;
901 unsigned char *myaddr;
902 int len;
903 {
904 char buf[PBUFSIZ];
905 int i;
906 char *p;
907
908 /* FIXME-32x64: Need a version of print_address_numeric which puts the
909 result in a buffer like sprintf. */
910 sprintf (buf, "M%lx,%x:", (unsigned long) memaddr, len);
911
912 /* We send target system values byte by byte, in increasing byte addresses,
913 each byte encoded as two hex characters. */
914
915 p = buf + strlen (buf);
916 for (i = 0; i < len; i++)
917 {
918 *p++ = tohex ((myaddr[i] >> 4) & 0xf);
919 *p++ = tohex (myaddr[i] & 0xf);
920 }
921 *p = '\0';
922
923 putpkt (buf);
924 getpkt (buf, 0);
925
926 if (buf[0] == 'E')
927 {
928 /* There is no correspondance between what the remote protocol uses
929 for errors and errno codes. We would like a cleaner way of
930 representing errors (big enough to include errno codes, bfd_error
931 codes, and others). But for now just return EIO. */
932 errno = EIO;
933 return 0;
934 }
935 return len;
936 }
937
938 /* Read memory data directly from the remote machine.
939 This does not use the data cache; the data cache uses this.
940 MEMADDR is the address in the remote memory space.
941 MYADDR is the address of the buffer in our space.
942 LEN is the number of bytes.
943
944 Returns number of bytes transferred, or 0 for error. */
945
946 static int
947 remote_read_bytes (memaddr, myaddr, len)
948 CORE_ADDR memaddr;
949 unsigned char *myaddr;
950 int len;
951 {
952 char buf[PBUFSIZ];
953 int i;
954 char *p;
955
956 if (len > PBUFSIZ / 2 - 1)
957 abort ();
958
959 /* FIXME-32x64: Need a version of print_address_numeric which puts the
960 result in a buffer like sprintf. */
961 sprintf (buf, "m%lx,%x", (unsigned long) memaddr, len);
962 putpkt (buf);
963 getpkt (buf, 0);
964
965 if (buf[0] == 'E')
966 {
967 /* There is no correspondance between what the remote protocol uses
968 for errors and errno codes. We would like a cleaner way of
969 representing errors (big enough to include errno codes, bfd_error
970 codes, and others). But for now just return EIO. */
971 errno = EIO;
972 return 0;
973 }
974
975 /* Reply describes memory byte by byte,
976 each byte encoded as two hex characters. */
977
978 p = buf;
979 for (i = 0; i < len; i++)
980 {
981 if (p[0] == 0 || p[1] == 0)
982 /* Reply is short. This means that we were able to read only part
983 of what we wanted to. */
984 break;
985 myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
986 p += 2;
987 }
988 return i;
989 }
990 \f
991 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
992 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
993 nonzero. Returns length of data written or read; 0 for error. */
994
995 /* ARGSUSED */
996 static int
997 remote_xfer_memory(memaddr, myaddr, len, should_write, target)
998 CORE_ADDR memaddr;
999 char *myaddr;
1000 int len;
1001 int should_write;
1002 struct target_ops *target; /* ignored */
1003 {
1004 int xfersize;
1005 int bytes_xferred;
1006 int total_xferred = 0;
1007
1008 while (len > 0)
1009 {
1010 if (len > MAXBUFBYTES)
1011 xfersize = MAXBUFBYTES;
1012 else
1013 xfersize = len;
1014
1015 if (should_write)
1016 bytes_xferred = remote_write_bytes (memaddr,
1017 (unsigned char *)myaddr, xfersize);
1018 else
1019 bytes_xferred = remote_read_bytes (memaddr,
1020 (unsigned char *)myaddr, xfersize);
1021
1022 /* If we get an error, we are done xferring. */
1023 if (bytes_xferred == 0)
1024 break;
1025
1026 memaddr += bytes_xferred;
1027 myaddr += bytes_xferred;
1028 len -= bytes_xferred;
1029 total_xferred += bytes_xferred;
1030 }
1031 return total_xferred;
1032 }
1033
1034 #if 0
1035 /* Enable after 4.12. */
1036
1037 void
1038 remote_search (len, data, mask, startaddr, increment, lorange, hirange
1039 addr_found, data_found)
1040 int len;
1041 char *data;
1042 char *mask;
1043 CORE_ADDR startaddr;
1044 int increment;
1045 CORE_ADDR lorange;
1046 CORE_ADDR hirange;
1047 CORE_ADDR *addr_found;
1048 char *data_found;
1049 {
1050 if (increment == -4 && len == 4)
1051 {
1052 long mask_long, data_long;
1053 long data_found_long;
1054 CORE_ADDR addr_we_found;
1055 char buf[PBUFSIZ];
1056 long returned_long[2];
1057 char *p;
1058
1059 mask_long = extract_unsigned_integer (mask, len);
1060 data_long = extract_unsigned_integer (data, len);
1061 sprintf (buf, "t%x:%x,%x", startaddr, data_long, mask_long);
1062 putpkt (buf);
1063 getpkt (buf, 0);
1064 if (buf[0] == '\0')
1065 {
1066 /* The stub doesn't support the 't' request. We might want to
1067 remember this fact, but on the other hand the stub could be
1068 switched on us. Maybe we should remember it only until
1069 the next "target remote". */
1070 generic_search (len, data, mask, startaddr, increment, lorange,
1071 hirange, addr_found, data_found);
1072 return;
1073 }
1074
1075 if (buf[0] == 'E')
1076 /* There is no correspondance between what the remote protocol uses
1077 for errors and errno codes. We would like a cleaner way of
1078 representing errors (big enough to include errno codes, bfd_error
1079 codes, and others). But for now just use EIO. */
1080 memory_error (EIO, startaddr);
1081 p = buf;
1082 addr_we_found = 0;
1083 while (*p != '\0' && *p != ',')
1084 addr_we_found = (addr_we_found << 4) + fromhex (*p++);
1085 if (*p == '\0')
1086 error ("Protocol error: short return for search");
1087
1088 data_found_long = 0;
1089 while (*p != '\0' && *p != ',')
1090 data_found_long = (data_found_long << 4) + fromhex (*p++);
1091 /* Ignore anything after this comma, for future extensions. */
1092
1093 if (addr_we_found < lorange || addr_we_found >= hirange)
1094 {
1095 *addr_found = 0;
1096 return;
1097 }
1098
1099 *addr_found = addr_we_found;
1100 *data_found = store_unsigned_integer (data_we_found, len);
1101 return;
1102 }
1103 generic_search (len, data, mask, startaddr, increment, lorange,
1104 hirange, addr_found, data_found);
1105 }
1106 #endif /* 0 */
1107 \f
1108 static void
1109 remote_files_info (ignore)
1110 struct target_ops *ignore;
1111 {
1112 puts_filtered ("Debugging a target over a serial line.\n");
1113 }
1114 \f
1115 /* Stuff for dealing with the packets which are part of this protocol.
1116 See comment at top of file for details. */
1117
1118 /* Read a single character from the remote end, masking it down to 7 bits. */
1119
1120 static int
1121 readchar (timeout)
1122 int timeout;
1123 {
1124 int ch;
1125
1126 ch = SERIAL_READCHAR (remote_desc, timeout);
1127
1128 switch (ch)
1129 {
1130 case SERIAL_EOF:
1131 error ("Remote connection closed");
1132 case SERIAL_ERROR:
1133 perror_with_name ("Remote communication error");
1134 case SERIAL_TIMEOUT:
1135 return ch;
1136 default:
1137 return ch & 0x7f;
1138 }
1139 }
1140
1141 /* Send the command in BUF to the remote machine,
1142 and read the reply into BUF.
1143 Report an error if we get an error reply. */
1144
1145 static void
1146 remote_send (buf)
1147 char *buf;
1148 {
1149
1150 putpkt (buf);
1151 getpkt (buf, 0);
1152
1153 if (buf[0] == 'E')
1154 error ("Remote failure reply: %s", buf);
1155 }
1156
1157 /* Send a packet to the remote machine, with error checking.
1158 The data of the packet is in BUF. */
1159 static void
1160 putpkt (buf)
1161 char *buf;
1162 {
1163 int i;
1164 unsigned char csum = 0;
1165 char buf2[PBUFSIZ];
1166 int cnt = strlen (buf);
1167 int ch;
1168 char *p;
1169
1170 /* Copy the packet into buffer BUF2, encapsulating it
1171 and giving it a checksum. */
1172
1173 if (cnt > sizeof(buf2) - 5) /* Prosanity check */
1174 abort();
1175
1176 p = buf2;
1177 *p++ = '$';
1178
1179 for (i = 0; i < cnt; i++)
1180 {
1181 csum += buf[i];
1182 *p++ = buf[i];
1183 }
1184 *p++ = '#';
1185 *p++ = tohex ((csum >> 4) & 0xf);
1186 *p++ = tohex (csum & 0xf);
1187
1188 /* Send it over and over until we get a positive ack. */
1189
1190 while (1)
1191 {
1192 int started_error_output = 0;
1193
1194 if (remote_debug)
1195 {
1196 *p = '\0';
1197 printf_unfiltered ("Sending packet: %s...", buf2);
1198 gdb_flush(gdb_stdout);
1199 }
1200 if (SERIAL_WRITE (remote_desc, buf2, p - buf2))
1201 perror_with_name ("putpkt: write failed");
1202
1203 /* read until either a timeout occurs (-2) or '+' is read */
1204 while (1)
1205 {
1206 ch = readchar (remote_timeout);
1207
1208 if (remote_debug)
1209 {
1210 switch (ch)
1211 {
1212 case '+':
1213 case SERIAL_TIMEOUT:
1214 case '$':
1215 if (started_error_output)
1216 {
1217 putc_unfiltered ('\n');
1218 started_error_output = 0;
1219 }
1220 }
1221 }
1222
1223 switch (ch)
1224 {
1225 case '+':
1226 if (remote_debug)
1227 printf_unfiltered("Got Ack\n");
1228 return;
1229 case SERIAL_TIMEOUT:
1230 break; /* Retransmit buffer */
1231 case '$':
1232 {
1233 unsigned char junkbuf[PBUFSIZ];
1234
1235 /* It's probably an old response, and we're out of sync. Just
1236 gobble up the packet and ignore it. */
1237 getpkt (junkbuf, 0);
1238 continue; /* Now, go look for + */
1239 }
1240 default:
1241 if (remote_debug)
1242 {
1243 if (!started_error_output)
1244 {
1245 started_error_output = 1;
1246 printf_unfiltered ("putpkt: Junk: ");
1247 }
1248 putc_unfiltered (ch & 0177);
1249 }
1250 continue;
1251 }
1252 break; /* Here to retransmit */
1253 }
1254
1255 #if 0
1256 /* This is wrong. If doing a long backtrace, the user should be
1257 able to get out next time we call QUIT, without anything as violent
1258 as interrupt_query. If we want to provide a way out of here
1259 without getting to the next QUIT, it should be based on hitting
1260 ^C twice as in remote_wait. */
1261 if (quit_flag)
1262 {
1263 quit_flag = 0;
1264 interrupt_query ();
1265 }
1266 #endif
1267 }
1268 }
1269
1270 /* Come here after finding the start of the frame. Collect the rest into BUF,
1271 verifying the checksum, length, and handling run-length compression.
1272 Returns 0 on any error, 1 on success. */
1273
1274 static int
1275 read_frame (buf)
1276 char *buf;
1277 {
1278 unsigned char csum;
1279 char *bp;
1280 int c;
1281
1282 csum = 0;
1283 bp = buf;
1284
1285 while (1)
1286 {
1287 c = readchar (remote_timeout);
1288
1289 switch (c)
1290 {
1291 case SERIAL_TIMEOUT:
1292 if (remote_debug)
1293 puts_filtered ("Timeout in mid-packet, retrying\n");
1294 return 0;
1295 case '$':
1296 if (remote_debug)
1297 puts_filtered ("Saw new packet start in middle of old one\n");
1298 return 0; /* Start a new packet, count retries */
1299 case '#':
1300 {
1301 unsigned char pktcsum;
1302
1303 *bp = '\000';
1304
1305 pktcsum = fromhex (readchar (remote_timeout)) << 4;
1306 pktcsum |= fromhex (readchar (remote_timeout));
1307
1308 if (csum == pktcsum)
1309 return 1;
1310
1311 printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
1312 pktcsum, csum);
1313 puts_filtered (buf);
1314 puts_filtered ("\n");
1315
1316 return 0;
1317 }
1318 case '*': /* Run length encoding */
1319 csum += c;
1320 c = readchar (remote_timeout);
1321 csum += c;
1322 c = c - ' ' + 3; /* Compute repeat count */
1323
1324 if (bp + c - 1 < buf + PBUFSIZ - 1)
1325 {
1326 memset (bp, *(bp - 1), c);
1327 bp += c;
1328 continue;
1329 }
1330
1331 *bp = '\0';
1332 printf_filtered ("Repeat count %d too large for buffer: ", c);
1333 puts_filtered (buf);
1334 puts_filtered ("\n");
1335 return 0;
1336
1337 default:
1338 if (bp < buf + PBUFSIZ - 1)
1339 {
1340 *bp++ = c;
1341 csum += c;
1342 continue;
1343 }
1344
1345 *bp = '\0';
1346 puts_filtered ("Remote packet too long: ");
1347 puts_filtered (buf);
1348 puts_filtered ("\n");
1349
1350 return 0;
1351 }
1352 }
1353 }
1354
1355 /* Read a packet from the remote machine, with error checking,
1356 and store it in BUF. BUF is expected to be of size PBUFSIZ.
1357 If FOREVER, wait forever rather than timing out; this is used
1358 while the target is executing user code. */
1359
1360 static void
1361 getpkt (buf, forever)
1362 char *buf;
1363 int forever;
1364 {
1365 char *bp;
1366 int c;
1367 int tries;
1368 int timeout;
1369 int val;
1370
1371 if (forever)
1372 timeout = -1;
1373 else
1374 timeout = remote_timeout;
1375
1376 #define MAX_TRIES 10
1377
1378 for (tries = 1; tries <= MAX_TRIES; tries++)
1379 {
1380 /* This can loop forever if the remote side sends us characters
1381 continuously, but if it pauses, we'll get a zero from readchar
1382 because of timeout. Then we'll count that as a retry. */
1383
1384 /* Note that we will only wait forever prior to the start of a packet.
1385 After that, we expect characters to arrive at a brisk pace. They
1386 should show up within remote_timeout intervals. */
1387
1388 do
1389 {
1390 c = readchar (timeout);
1391
1392 if (c == SERIAL_TIMEOUT)
1393 {
1394 if (remote_debug)
1395 puts_filtered ("Timed out.\n");
1396 goto retry;
1397 }
1398 }
1399 while (c != '$');
1400
1401 /* We've found the start of a packet, now collect the data. */
1402
1403 val = read_frame (buf);
1404
1405 if (val == 1)
1406 {
1407 if (remote_debug)
1408 fprintf_unfiltered (gdb_stderr, "Packet received: %s\n", buf);
1409 SERIAL_WRITE (remote_desc, "+", 1);
1410 return;
1411 }
1412
1413 /* Try the whole thing again. */
1414 retry:
1415 SERIAL_WRITE (remote_desc, "-", 1);
1416 }
1417
1418 /* We have tried hard enough, and just can't receive the packet. Give up. */
1419
1420 printf_unfiltered ("Ignoring packet error, continuing...\n");
1421 SERIAL_WRITE (remote_desc, "+", 1);
1422 }
1423 \f
1424 static void
1425 remote_kill ()
1426 {
1427 putpkt ("k");
1428 /* Don't wait for it to die. I'm not really sure it matters whether
1429 we do or not. For the existing stubs, kill is a noop. */
1430 target_mourn_inferior ();
1431 }
1432
1433 static void
1434 remote_mourn ()
1435 {
1436 unpush_target (&remote_ops);
1437 generic_mourn_inferior ();
1438 }
1439 \f
1440 #ifdef REMOTE_BREAKPOINT
1441
1442 /* On some machines, e.g. 68k, we may use a different breakpoint instruction
1443 than other targets. */
1444 static unsigned char break_insn[] = REMOTE_BREAKPOINT;
1445
1446 /* Check that it fits in BREAKPOINT_MAX bytes. */
1447 static unsigned char check_break_insn_size[BREAKPOINT_MAX] = REMOTE_BREAKPOINT;
1448
1449 #else /* No REMOTE_BREAKPOINT. */
1450
1451 /* Same old breakpoint instruction. This code does nothing different
1452 than mem-break.c. */
1453 static unsigned char break_insn[] = BREAKPOINT;
1454
1455 #endif /* No REMOTE_BREAKPOINT. */
1456
1457 /* Insert a breakpoint on targets that don't have any better breakpoint
1458 support. We read the contents of the target location and stash it,
1459 then overwrite it with a breakpoint instruction. ADDR is the target
1460 location in the target machine. CONTENTS_CACHE is a pointer to
1461 memory allocated for saving the target contents. It is guaranteed
1462 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
1463 is accomplished via BREAKPOINT_MAX). */
1464
1465 static int
1466 remote_insert_breakpoint (addr, contents_cache)
1467 CORE_ADDR addr;
1468 char *contents_cache;
1469 {
1470 int val;
1471
1472 val = target_read_memory (addr, contents_cache, sizeof break_insn);
1473
1474 if (val == 0)
1475 val = target_write_memory (addr, (char *)break_insn, sizeof break_insn);
1476
1477 return val;
1478 }
1479
1480 static int
1481 remote_remove_breakpoint (addr, contents_cache)
1482 CORE_ADDR addr;
1483 char *contents_cache;
1484 {
1485 return target_write_memory (addr, contents_cache, sizeof break_insn);
1486 }
1487 \f
1488 /* Define the target subroutine names */
1489
1490 struct target_ops remote_hppro_ops = {
1491 "hppro", /* to_shortname */
1492 "Remote serial target for HP-PRO targets", /* to_longname */
1493 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
1494 This is for targets that supports the HP-PRO standard.\n\
1495 Specify the serial device it is connected to (e.g. /dev/ttya) or telnet port.", /* to_doc */
1496 remote_open, /* to_open */
1497 remote_close, /* to_close */
1498 NULL, /* to_attach */
1499 remote_detach, /* to_detach */
1500 remote_resume, /* to_resume */
1501 remote_wait, /* to_wait */
1502 remote_fetch_registers, /* to_fetch_registers */
1503 remote_store_registers, /* to_store_registers */
1504 remote_prepare_to_store, /* to_prepare_to_store */
1505 remote_xfer_memory, /* to_xfer_memory */
1506 remote_files_info, /* to_files_info */
1507
1508 remote_insert_breakpoint, /* to_insert_breakpoint */
1509 remote_remove_breakpoint, /* to_remove_breakpoint */
1510
1511 NULL, /* to_terminal_init */
1512 NULL, /* to_terminal_inferior */
1513 NULL, /* to_terminal_ours_for_output */
1514 NULL, /* to_terminal_ours */
1515 NULL, /* to_terminal_info */
1516 remote_kill, /* to_kill */
1517 hppro_load, /* to_load */
1518 NULL, /* to_lookup_symbol */
1519 NULL, /* to_create_inferior */
1520 remote_mourn, /* to_mourn_inferior */
1521 0, /* to_can_run */
1522 0, /* to_notice_signals */
1523 0, /* to_stop */
1524 process_stratum, /* to_stratum */
1525 NULL, /* to_next */
1526 1, /* to_has_all_memory */
1527 1, /* to_has_memory */
1528 1, /* to_has_stack */
1529 1, /* to_has_registers */
1530 1, /* to_has_execution */
1531 NULL, /* sections */
1532 NULL, /* sections_end */
1533 OPS_MAGIC /* to_magic */
1534 };
1535
1536 void
1537 _initialize_remote_hppro ()
1538 {
1539 struct cmd_list_element *c;
1540 add_target (&remote_hppro_ops);
1541
1542 /* this sets the type of download protocol */
1543 c = add_set_cmd ("loadtype", no_class, var_string, (char *)&loadtype_str,
1544 "Set the type of the remote load protocol.\n", &setlist);
1545 c->function.sfunc = set_loadtype_command;
1546 add_show_from_set (c, &showlist);
1547 loadtype_str = savestring ("generic", 8);
1548
1549 /* this adds a command to boot the board */
1550 add_com ("boot", class_support, boot_board,
1551 "Boot the damn target board.\n");
1552 }
1553
1554 static void
1555 set_loadtype_command (ignore, from_tty, c)
1556 char *ignore;
1557 int from_tty;
1558 struct cmd_list_element *c;
1559 {
1560 loadtype_str = savestring (*(char **) c->var, strlen (*(char **) c->var));
1561 }
1562
This page took 0.089181 seconds and 4 git commands to generate.