* stabsread.c (get_substring): Declare second arg as int.
[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., 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 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 "gdb_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_remove_breakpoint PARAMS ((CORE_ADDR, char *));
164
165 static int
166 remote_insert_breakpoint PARAMS ((CORE_ADDR, char *));
167
168 static void
169 remote_mourn PARAMS ((void));
170
171 static void
172 remote_kill PARAMS ((void));
173
174 static int
175 read_frame PARAMS ((char *));
176
177 static void
178 boot_board PARAMS ((void));
179
180 static int
181 remote_write_bytes PARAMS ((CORE_ADDR memaddr, char *myaddr, int len));
182
183 static int
184 remote_read_bytes PARAMS ((CORE_ADDR memaddr, char *myaddr, int len));
185
186 static void
187 remote_files_info PARAMS ((struct target_ops *ignore));
188
189 static int
190 remote_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr, int len,
191 int should_write, struct target_ops *target));
192
193 static void
194 remote_prepare_to_store PARAMS ((void));
195
196 static void
197 remote_fetch_registers PARAMS ((int regno));
198
199 static void
200 remote_resume PARAMS ((int pid, int step, enum target_signal siggnal));
201
202 static int
203 remote_start_remote PARAMS ((char *dummy));
204
205 static void
206 remote_open PARAMS ((char *name, int from_tty));
207
208 static void
209 remote_close PARAMS ((int quitting));
210
211 static void
212 remote_store_registers PARAMS ((int regno));
213
214 static void
215 getpkt PARAMS ((char *buf, int forever));
216
217 static void
218 putpkt PARAMS ((char *buf));
219
220 static void
221 remote_send PARAMS ((char *buf));
222
223 static int
224 readchar PARAMS ((int timeout));
225
226 static int
227 remote_wait PARAMS ((int pid, struct target_waitstatus *status));
228
229 static int
230 tohex PARAMS ((int nib));
231
232 static int
233 fromhex PARAMS ((int a));
234
235 static void
236 remote_detach PARAMS ((char *args, int from_tty));
237
238 static void
239 remote_interrupt PARAMS ((int signo));
240
241 static void
242 remote_interrupt_twice PARAMS ((int signo));
243
244 static void
245 interrupt_query PARAMS ((void));
246
247 static void
248 hppro_load PARAMS ((char *name, int from_tty));
249
250 extern struct target_ops remote_ops; /* Forward decl */
251
252 /* This was 5 seconds, which is a long time to sit and wait.
253 Unless this is going though some terminal server or multiplexer or
254 other form of hairy serial connection, I would think 2 seconds would
255 be plenty. */
256 static int remote_timeout = 2;
257
258 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
259 remote_open knows that we don't have a file open when the program
260 starts. */
261 extern serial_t remote_desc;
262
263 /* Having this larger than 400 causes us to be incompatible with m68k-stub.c
264 and i386-stub.c. Normally, no one would notice because it only matters
265 for writing large chunks of memory (e.g. in downloads). Also, this needs
266 to be more than 400 if required to hold the registers (see below, where
267 we round it up based on REGISTER_BYTES). */
268 #define PBUFSIZ 400
269
270 /* Maximum number of bytes to read/write at once. The value here
271 is chosen to fill up a packet (the headers account for the 32). */
272 #define MAXBUFBYTES ((PBUFSIZ-32)/2)
273
274 /* Round up PBUFSIZ to hold all the registers, at least. */
275 /* The blank line after the #if seems to be required to work around a
276 bug in HP's PA compiler. */
277 #if REGISTER_BYTES > MAXBUFBYTES
278
279 #undef PBUFSIZ
280 #define PBUFSIZ (REGISTER_BYTES * 2 + 32)
281 #endif
282
283 /* Should we try the 'P' request? If this is set to one when the stub
284 doesn't support 'P', the only consequence is some unnecessary traffic. */
285 static int stub_supports_P = 1;
286
287 /* sets the download protocol, choices are srec, generic, boot */
288 char *loadtype;
289 static char *loadtype_str;
290 static void set_loadtype_command
291 PARAMS ((char *, int, struct cmd_list_element *));
292
293 static void
294 hppro_load (file, from_tty)
295 char *file;
296 int from_tty;
297 {
298 puts ("Loading... HA!");
299 }
300
301 \f
302 /* Clean up connection to a remote debugger. */
303
304 /* ARGSUSED */
305 static void
306 remote_close (quitting)
307 int quitting;
308 {
309 if (remote_desc)
310 SERIAL_CLOSE (remote_desc);
311 remote_desc = NULL;
312 }
313
314 /* Query the remote side for the text, data and bss offsets. */
315
316 #if 0
317
318 static void
319 get_offsets ()
320 {
321 unsigned char buf[PBUFSIZ];
322 int nvals;
323 CORE_ADDR text_addr, data_addr, bss_addr;
324 struct section_offsets *offs;
325
326 putpkt ("qOffsets");
327
328 getpkt (buf, 0);
329
330 if (buf[0] == '\000')
331 return; /* Return silently. Stub doesn't support this
332 command. */
333 if (buf[0] == 'E')
334 {
335 warning ("Remote failure reply: %s", buf);
336 return;
337 }
338
339 nvals = sscanf (buf, "Text=%lx;Data=%lx;Bss=%lx", &text_addr, &data_addr,
340 &bss_addr);
341 if (nvals != 3)
342 error ("Malformed response to offset query, %s", buf);
343
344 if (symfile_objfile == NULL)
345 return;
346
347 offs = (struct section_offsets *) alloca (sizeof (struct section_offsets)
348 + symfile_objfile->num_sections
349 * sizeof (offs->offsets));
350 memcpy (offs, symfile_objfile->section_offsets,
351 sizeof (struct section_offsets)
352 + symfile_objfile->num_sections
353 * sizeof (offs->offsets));
354
355 /* FIXME: This code assumes gdb-stabs.h is being used; it's broken
356 for xcoff, dwarf, sdb-coff, etc. But there is no simple
357 canonical representation for this stuff. (Just what does "text"
358 as seen by the stub mean, anyway? I think it means all sections
359 with SEC_CODE set, but we currently have no way to deal with that). */
360
361 ANOFFSET (offs, SECT_OFF_TEXT) = text_addr;
362
363 /* This is a temporary kludge to force data and bss to use the same offsets
364 because that's what nlmconv does now. The real solution requires changes
365 to the stub and remote.c that I don't have time to do right now. */
366
367 ANOFFSET (offs, SECT_OFF_DATA) = data_addr;
368 ANOFFSET (offs, SECT_OFF_BSS) = data_addr;
369
370 objfile_relocate (symfile_objfile, offs);
371 }
372
373 #endif /* unused */
374
375 #define INBUFSIZE 10
376
377 void
378 boot_board()
379 {
380 char c;
381 char buf[INBUFSIZE];
382 char *ptr;
383
384 /* See if we can connect to the boot ROM command line */
385 ptr = buf;
386 while (1) {
387 SERIAL_WRITE (remote_desc, "\r\n", 2);
388 c = readchar (2);
389 if ((sr_get_debug() > 2) && (isascii(c)))
390 putchar (c);
391 if (c == SERIAL_TIMEOUT) {
392 if (sr_get_debug())
393 puts_filtered ("Timed out.\n");
394 break;
395 }
396 if (c == '&') {
397 if (sr_get_debug() > 2)
398 puts ("Got ACK from stub");
399 break;
400 }
401 if (c == '>') {
402 if (sr_get_debug() > 2)
403 puts ("Got prompt from ROM monitor");
404 break;
405 }
406 }
407
408 }
409
410 /* Stub for catch_errors. */
411 static int
412 remote_start_remote (dummy)
413 char *dummy;
414 {
415 immediate_quit = 1; /* Allow user to interrupt it */
416
417 /* Ack any packet which the remote side has already sent. */
418
419 if (sr_get_debug())
420 puts ("Trying a '+' to ACK the target.");
421
422 SERIAL_WRITE (remote_desc, "+", 1);
423
424 #if 0
425 boot_board();
426
427 get_offsets (); /* Get text, data & bss offsets */
428 #endif
429
430 putpkt ("?"); /* initiate a query from remote machine */
431 immediate_quit = 0;
432
433 start_remote (); /* Initialize gdb process mechanisms */
434
435 return 1;
436 }
437
438 /* Open a connection to a remote debugger.
439 NAME is the filename used for communication. */
440
441 static DCACHE *remote_dcache;
442
443 static void
444 remote_open (name, from_tty)
445 char *name;
446 int from_tty;
447 {
448 if (name == 0)
449 error (
450 "To open a remote debug connection, you need to specify what serial\n\
451 device is attached to the remote system (e.g. /dev/ttya).");
452
453 target_preopen (from_tty);
454
455 unpush_target (&remote_ops);
456
457 remote_dcache = dcache_init (remote_read_bytes, remote_write_bytes);
458
459 remote_desc = SERIAL_OPEN (name);
460 if (!remote_desc)
461 perror_with_name (name);
462
463 if (baud_rate != -1)
464 {
465 if (SERIAL_SETBAUDRATE (remote_desc, baud_rate))
466 {
467 SERIAL_CLOSE (remote_desc);
468 perror_with_name (name);
469 }
470 }
471
472 SERIAL_RAW (remote_desc);
473
474 /* If there is something sitting in the buffer we might take it as a
475 response to a command, which would be bad. */
476 SERIAL_FLUSH_INPUT (remote_desc);
477
478 if (from_tty)
479 {
480 puts_filtered ("Remote debugging using ");
481 puts_filtered (name);
482 puts_filtered ("\n");
483 }
484 push_target (&remote_ops); /* Switch to using remote target now */
485
486 /* Start out by trying the 'P' request to set registers. We set this each
487 time that we open a new target so that if the user switches from one
488 stub to another, we can (if the target is closed and reopened) cope. */
489 stub_supports_P = 1;
490
491 /* Without this, some commands which require an active target (such as kill)
492 won't work. This variable serves (at least) double duty as both the pid
493 of the target process (if it has such), and as a flag indicating that a
494 target is active. These functions should be split out into seperate
495 variables, especially since GDB will someday have a notion of debugging
496 several processes. */
497
498 inferior_pid = 42000;
499
500 /* Start the remote connection; if error (0), discard this target.
501 In particular, if the user quits, be sure to discard it
502 (we'd be in an inconsistent state otherwise). */
503 if (!catch_errors (remote_start_remote, (char *)0,
504 "Couldn't establish connection to remote target\n", RETURN_MASK_ALL))
505 pop_target();
506 }
507
508 /* remote_detach()
509 takes a program previously attached to and detaches it.
510 We better not have left any breakpoints
511 in the program or it'll die when it hits one.
512 Close the open connection to the remote debugger.
513 Use this when you want to detach and do something else
514 with your gdb. */
515
516 static void
517 remote_detach (args, from_tty)
518 char *args;
519 int from_tty;
520 {
521 if (args)
522 error ("Argument given to \"detach\" when remotely debugging.");
523
524 pop_target ();
525 if (from_tty)
526 puts_filtered ("Ending remote debugging.\n");
527 }
528
529 /* Convert hex digit A to a number. */
530
531 static int
532 fromhex (a)
533 int a;
534 {
535 if (a >= '0' && a <= '9')
536 return a - '0';
537 else if (a >= 'a' && a <= 'f')
538 return a - 'a' + 10;
539 else
540 error ("Reply contains invalid hex digit");
541 }
542
543 /* Convert number NIB to a hex digit. */
544
545 static int
546 tohex (nib)
547 int nib;
548 {
549 if (nib < 10)
550 return '0'+nib;
551 else
552 return 'a'+nib-10;
553 }
554 \f
555 /* Tell the remote machine to resume. */
556
557 static void
558 remote_resume (pid, step, siggnal)
559 int pid, step;
560 enum target_signal siggnal;
561 {
562 char buf[PBUFSIZ];
563
564 if (siggnal)
565 {
566 target_terminal_ours_for_output ();
567 printf_filtered
568 ("Can't send signals to a remote system. %s not sent.\n",
569 target_signal_to_name (siggnal)); target_terminal_inferior ();
570 }
571
572 dcache_flush (remote_dcache);
573
574 strcpy (buf, step ? "s": "c");
575
576 putpkt (buf);
577 }
578 \f
579 /* Send ^C to target to halt it. Target will respond, and send us a
580 packet. */
581
582 static void
583 remote_interrupt (signo)
584 int signo;
585 {
586 /* If this doesn't work, try more severe steps. */
587 signal (signo, remote_interrupt_twice);
588
589 if (remote_debug)
590 printf_unfiltered ("remote_interrupt called\n");
591
592 SERIAL_WRITE (remote_desc, "\003", 1); /* Send a ^C */
593 }
594
595 static void (*ofunc)();
596
597 /* The user typed ^C twice. */
598 static void
599 remote_interrupt_twice (signo)
600 int signo;
601 {
602 signal (signo, ofunc);
603
604 interrupt_query ();
605
606 signal (signo, remote_interrupt);
607 }
608
609 /* Ask the user what to do when an interrupt is received. */
610
611 static void
612 interrupt_query ()
613 {
614 target_terminal_ours ();
615
616 if (query ("Interrupted while waiting for the program.\n\
617 Give up (and stop debugging it)? "))
618 {
619 target_mourn_inferior ();
620 return_to_top_level (RETURN_QUIT);
621 }
622
623 target_terminal_inferior ();
624 }
625
626 /* Wait until the remote machine stops, then return,
627 storing status in STATUS just as `wait' would.
628 Returns "pid" (though it's not clear what, if anything, that
629 means in the case of this target). */
630
631 static int
632 remote_wait (pid, status)
633 int pid;
634 struct target_waitstatus *status;
635 {
636 unsigned char buf[PBUFSIZ];
637
638 status->kind = TARGET_WAITKIND_EXITED;
639 status->value.integer = 0;
640
641 while (1)
642 {
643 unsigned char *p;
644
645 ofunc = (void (*)()) signal (SIGINT, remote_interrupt);
646 getpkt ((char *) buf, 1);
647 signal (SIGINT, ofunc);
648
649 switch (buf[0])
650 {
651 case 'E': /* Error of some sort */
652 warning ("Remote failure reply: %s", buf);
653 continue;
654 case 'T': /* Status with PC, SP, FP, ... */
655 {
656 int i;
657 long regno;
658 char regs[MAX_REGISTER_RAW_SIZE];
659
660 /* Expedited reply, containing Signal, {regno, reg} repeat */
661 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
662 ss = signal number
663 n... = register number
664 r... = register contents
665 */
666
667 p = &buf[3]; /* after Txx */
668
669 while (*p)
670 {
671 unsigned char *p1;
672
673 regno = strtol (p, (char **) &p1, 16); /* Read the register number */
674
675 if (p1 == p)
676 warning ("Remote sent badly formed register number: %s\nPacket: '%s'\n",
677 p1, buf);
678
679 p = p1;
680
681 if (*p++ != ':')
682 warning ("Malformed packet (missing colon): %s\nPacket: '%s'\n",
683 p, buf);
684
685 if (regno >= NUM_REGS)
686 warning ("Remote sent bad register number %d: %s\nPacket: '%s'\n",
687 regno, p, buf);
688
689 for (i = 0; i < REGISTER_RAW_SIZE (regno); i++)
690 {
691 if (p[0] == 0 || p[1] == 0)
692 warning ("Remote reply is too short: %s", buf);
693 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
694 p += 2;
695 }
696
697 if (*p++ != ';')
698 warning ("Remote register badly formatted: %s", buf);
699
700 supply_register (regno, regs);
701 }
702 }
703 /* fall through */
704 case 'S': /* Old style status, just signal only */
705 status->kind = TARGET_WAITKIND_STOPPED;
706 status->value.sig = (enum target_signal)
707 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
708
709 return inferior_pid;
710 case 'W': /* Target exited */
711 {
712 /* The remote process exited. */
713 status->kind = TARGET_WAITKIND_EXITED;
714 status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
715 return inferior_pid;
716 }
717 case 'O': /* Console output */
718 fputs_filtered (buf + 1, gdb_stdout);
719 continue;
720 default:
721 warning ("Invalid remote reply: %s", buf);
722 continue;
723 }
724 }
725 return inferior_pid;
726 }
727
728 /* Number of bytes of registers this stub implements. */
729 static int register_bytes_found;
730
731 /* Read the remote registers into the block REGS. */
732 /* Currently we just read all the registers, so we don't use regno. */
733 /* ARGSUSED */
734 static void
735 remote_fetch_registers (regno)
736 int regno;
737 {
738 char buf[PBUFSIZ];
739 int i;
740 char *p;
741 char regs[REGISTER_BYTES];
742
743 sprintf (buf, "g");
744 remote_send (buf);
745
746 /* Unimplemented registers read as all bits zero. */
747 memset (regs, 0, REGISTER_BYTES);
748
749 /* We can get out of synch in various cases. If the first character
750 in the buffer is not a hex character, assume that has happened
751 and try to fetch another packet to read. */
752 while ((buf[0] < '0' || buf[0] > '9')
753 && (buf[0] < 'a' || buf[0] > 'f'))
754 {
755 if (remote_debug)
756 printf_unfiltered ("Bad register packet; fetching a new packet\n");
757 getpkt (buf, 0);
758 }
759
760 /* Reply describes registers byte by byte, each byte encoded as two
761 hex characters. Suck them all up, then supply them to the
762 register cacheing/storage mechanism. */
763
764 p = buf;
765 for (i = 0; i < REGISTER_BYTES; i++)
766 {
767 if (p[0] == 0)
768 break;
769 if (p[1] == 0)
770 {
771 warning ("Remote reply is of odd length: %s", buf);
772 /* Don't change register_bytes_found in this case, and don't
773 print a second warning. */
774 goto supply_them;
775 }
776 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
777 p += 2;
778 }
779
780 if (i != register_bytes_found)
781 {
782 register_bytes_found = i;
783 #ifdef REGISTER_BYTES_OK
784 if (!REGISTER_BYTES_OK (i))
785 warning ("Remote reply is too short: %s", buf);
786 #endif
787 }
788
789 supply_them:
790 for (i = 0; i < NUM_REGS; i++)
791 supply_register (i, &regs[REGISTER_BYTE(i)]);
792 }
793
794 /* Prepare to store registers. Since we may send them all (using a
795 'G' request), we have to read out the ones we don't want to change
796 first. */
797
798 static void
799 remote_prepare_to_store ()
800 {
801 /* Make sure the entire registers array is valid. */
802 read_register_bytes (0, (char *)NULL, REGISTER_BYTES);
803 }
804
805 /* Store register REGNO, or all registers if REGNO == -1, from the contents
806 of REGISTERS. FIXME: ignores errors. */
807
808 static void
809 remote_store_registers (regno)
810 int regno;
811 {
812 char buf[PBUFSIZ];
813 int i;
814 char *p;
815
816 if (regno >= 0 && stub_supports_P)
817 {
818 /* Try storing a single register. */
819 char *regp;
820
821 sprintf (buf, "P%x=", regno);
822 p = buf + strlen (buf);
823 regp = &registers[REGISTER_BYTE (regno)];
824 for (i = 0; i < REGISTER_RAW_SIZE (regno); ++i)
825 {
826 *p++ = tohex ((regp[i] >> 4) & 0xf);
827 *p++ = tohex (regp[i] & 0xf);
828 }
829 *p = '\0';
830 remote_send (buf);
831 if (buf[0] != '\0')
832 {
833 /* The stub understands the 'P' request. We are done. */
834 return;
835 }
836
837 /* The stub does not support the 'P' request. Use 'G' instead,
838 and don't try using 'P' in the future (it will just waste our
839 time). */
840 stub_supports_P = 0;
841 }
842
843 buf[0] = 'G';
844
845 /* Command describes registers byte by byte,
846 each byte encoded as two hex characters. */
847
848 p = buf + 1;
849 /* remote_prepare_to_store insures that register_bytes_found gets set. */
850 for (i = 0; i < register_bytes_found; i++)
851 {
852 *p++ = tohex ((registers[i] >> 4) & 0xf);
853 *p++ = tohex (registers[i] & 0xf);
854 }
855 *p = '\0';
856
857 remote_send (buf);
858 }
859
860 #if 0
861
862 /* Use of the data cache is disabled because it loses for looking at
863 and changing hardware I/O ports and the like. Accepting `volatile'
864 would perhaps be one way to fix it. Another idea would be to use the
865 executable file for the text segment (for all SEC_CODE sections?
866 For all SEC_READONLY sections?). This has problems if you want to
867 actually see what the memory contains (e.g. self-modifying code,
868 clobbered memory, user downloaded the wrong thing). */
869
870 /* Read a word from remote address ADDR and return it.
871 This goes through the data cache. */
872
873 static int
874 remote_fetch_word (addr)
875 CORE_ADDR addr;
876 {
877 return dcache_fetch (remote_dcache, addr);
878 }
879
880 /* Write a word WORD into remote address ADDR.
881 This goes through the data cache. */
882
883 static void
884 remote_store_word (addr, word)
885 CORE_ADDR addr;
886 int word;
887 {
888 dcache_poke (remote_dcache, addr, word);
889 }
890 #endif /* 0 */
891 \f
892 /* Write memory data directly to the remote machine.
893 This does not inform the data cache; the data cache uses this.
894 MEMADDR is the address in the remote memory space.
895 MYADDR is the address of the buffer in our space.
896 LEN is the number of bytes.
897
898 Returns number of bytes transferred, or 0 for error. */
899
900 static int
901 remote_write_bytes (memaddr, myaddr, len)
902 CORE_ADDR memaddr;
903 char *myaddr;
904 int len;
905 {
906 char buf[PBUFSIZ];
907 int i;
908 char *p;
909
910 /* FIXME-32x64: Need a version of print_address_numeric which puts the
911 result in a buffer like sprintf. */
912 sprintf (buf, "M%lx,%x:", (unsigned long) memaddr, len);
913
914 /* We send target system values byte by byte, in increasing byte addresses,
915 each byte encoded as two hex characters. */
916
917 p = buf + strlen (buf);
918 for (i = 0; i < len; i++)
919 {
920 *p++ = tohex ((myaddr[i] >> 4) & 0xf);
921 *p++ = tohex (myaddr[i] & 0xf);
922 }
923 *p = '\0';
924
925 putpkt (buf);
926 getpkt (buf, 0);
927
928 if (buf[0] == 'E')
929 {
930 /* There is no correspondance between what the remote protocol uses
931 for errors and errno codes. We would like a cleaner way of
932 representing errors (big enough to include errno codes, bfd_error
933 codes, and others). But for now just return EIO. */
934 errno = EIO;
935 return 0;
936 }
937 return len;
938 }
939
940 /* Read memory data directly from the remote machine.
941 This does not use the data cache; the data cache uses this.
942 MEMADDR is the address in the remote memory space.
943 MYADDR is the address of the buffer in our space.
944 LEN is the number of bytes.
945
946 Returns number of bytes transferred, or 0 for error. */
947
948 static int
949 remote_read_bytes (memaddr, myaddr, len)
950 CORE_ADDR memaddr;
951 char *myaddr;
952 int len;
953 {
954 char buf[PBUFSIZ];
955 int i;
956 char *p;
957
958 if (len > PBUFSIZ / 2 - 1)
959 abort ();
960
961 /* FIXME-32x64: Need a version of print_address_numeric which puts the
962 result in a buffer like sprintf. */
963 sprintf (buf, "m%lx,%x", (unsigned long) memaddr, len);
964 putpkt (buf);
965 getpkt (buf, 0);
966
967 if (buf[0] == 'E')
968 {
969 /* There is no correspondance between what the remote protocol uses
970 for errors and errno codes. We would like a cleaner way of
971 representing errors (big enough to include errno codes, bfd_error
972 codes, and others). But for now just return EIO. */
973 errno = EIO;
974 return 0;
975 }
976
977 /* Reply describes memory byte by byte,
978 each byte encoded as two hex characters. */
979
980 p = buf;
981 for (i = 0; i < len; i++)
982 {
983 if (p[0] == 0 || p[1] == 0)
984 /* Reply is short. This means that we were able to read only part
985 of what we wanted to. */
986 break;
987 myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
988 p += 2;
989 }
990 return i;
991 }
992 \f
993 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
994 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
995 nonzero. Returns length of data written or read; 0 for error. */
996
997 /* ARGSUSED */
998 static int
999 remote_xfer_memory(memaddr, myaddr, len, should_write, target)
1000 CORE_ADDR memaddr;
1001 char *myaddr;
1002 int len;
1003 int should_write;
1004 struct target_ops *target; /* ignored */
1005 {
1006 int xfersize;
1007 int bytes_xferred;
1008 int total_xferred = 0;
1009
1010 while (len > 0)
1011 {
1012 if (len > MAXBUFBYTES)
1013 xfersize = MAXBUFBYTES;
1014 else
1015 xfersize = len;
1016
1017 if (should_write)
1018 bytes_xferred = remote_write_bytes (memaddr, myaddr, xfersize);
1019 else
1020 bytes_xferred = remote_read_bytes (memaddr, 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 putchar_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 putchar_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 int c;
1366 int tries;
1367 int timeout;
1368 int val;
1369
1370 if (forever)
1371 timeout = -1;
1372 else
1373 timeout = remote_timeout;
1374
1375 #define MAX_TRIES 10
1376
1377 for (tries = 1; tries <= MAX_TRIES; tries++)
1378 {
1379 /* This can loop forever if the remote side sends us characters
1380 continuously, but if it pauses, we'll get a zero from readchar
1381 because of timeout. Then we'll count that as a retry. */
1382
1383 /* Note that we will only wait forever prior to the start of a packet.
1384 After that, we expect characters to arrive at a brisk pace. They
1385 should show up within remote_timeout intervals. */
1386
1387 do
1388 {
1389 c = readchar (timeout);
1390
1391 if (c == SERIAL_TIMEOUT)
1392 {
1393 if (remote_debug)
1394 puts_filtered ("Timed out.\n");
1395 goto retry;
1396 }
1397 }
1398 while (c != '$');
1399
1400 /* We've found the start of a packet, now collect the data. */
1401
1402 val = read_frame (buf);
1403
1404 if (val == 1)
1405 {
1406 if (remote_debug)
1407 fprintf_unfiltered (gdb_stderr, "Packet received: %s\n", buf);
1408 SERIAL_WRITE (remote_desc, "+", 1);
1409 return;
1410 }
1411
1412 /* Try the whole thing again. */
1413 retry:
1414 SERIAL_WRITE (remote_desc, "-", 1);
1415 }
1416
1417 /* We have tried hard enough, and just can't receive the packet. Give up. */
1418
1419 printf_unfiltered ("Ignoring packet error, continuing...\n");
1420 SERIAL_WRITE (remote_desc, "+", 1);
1421 }
1422 \f
1423 static void
1424 remote_kill ()
1425 {
1426 putpkt ("k");
1427 /* Don't wait for it to die. I'm not really sure it matters whether
1428 we do or not. For the existing stubs, kill is a noop. */
1429 target_mourn_inferior ();
1430 }
1431
1432 static void
1433 remote_mourn ()
1434 {
1435 unpush_target (&remote_ops);
1436 generic_mourn_inferior ();
1437 }
1438 \f
1439 #ifdef REMOTE_BREAKPOINT
1440
1441 /* On some machines, e.g. 68k, we may use a different breakpoint instruction
1442 than other targets. */
1443 static unsigned char break_insn[] = REMOTE_BREAKPOINT;
1444
1445 #else /* No REMOTE_BREAKPOINT. */
1446
1447 /* Same old breakpoint instruction. This code does nothing different
1448 than mem-break.c. */
1449 static unsigned char break_insn[] = BREAKPOINT;
1450
1451 #endif /* No REMOTE_BREAKPOINT. */
1452
1453 /* Insert a breakpoint on targets that don't have any better breakpoint
1454 support. We read the contents of the target location and stash it,
1455 then overwrite it with a breakpoint instruction. ADDR is the target
1456 location in the target machine. CONTENTS_CACHE is a pointer to
1457 memory allocated for saving the target contents. It is guaranteed
1458 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
1459 is accomplished via BREAKPOINT_MAX). */
1460
1461 static int
1462 remote_insert_breakpoint (addr, contents_cache)
1463 CORE_ADDR addr;
1464 char *contents_cache;
1465 {
1466 int val;
1467
1468 val = target_read_memory (addr, contents_cache, sizeof break_insn);
1469
1470 if (val == 0)
1471 val = target_write_memory (addr, (char *)break_insn, sizeof break_insn);
1472
1473 return val;
1474 }
1475
1476 static int
1477 remote_remove_breakpoint (addr, contents_cache)
1478 CORE_ADDR addr;
1479 char *contents_cache;
1480 {
1481 return target_write_memory (addr, contents_cache, sizeof break_insn);
1482 }
1483 \f
1484 /* Define the target subroutine names */
1485
1486 struct target_ops remote_hppro_ops = {
1487 "hppro", /* to_shortname */
1488 "Remote serial target for HP-PRO targets", /* to_longname */
1489 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
1490 This is for targets that supports the HP-PRO standard.\n\
1491 Specify the serial device it is connected to (e.g. /dev/ttya) or telnet port.", /* to_doc */
1492 remote_open, /* to_open */
1493 remote_close, /* to_close */
1494 NULL, /* to_attach */
1495 remote_detach, /* to_detach */
1496 remote_resume, /* to_resume */
1497 remote_wait, /* to_wait */
1498 remote_fetch_registers, /* to_fetch_registers */
1499 remote_store_registers, /* to_store_registers */
1500 remote_prepare_to_store, /* to_prepare_to_store */
1501 remote_xfer_memory, /* to_xfer_memory */
1502 remote_files_info, /* to_files_info */
1503
1504 remote_insert_breakpoint, /* to_insert_breakpoint */
1505 remote_remove_breakpoint, /* to_remove_breakpoint */
1506
1507 NULL, /* to_terminal_init */
1508 NULL, /* to_terminal_inferior */
1509 NULL, /* to_terminal_ours_for_output */
1510 NULL, /* to_terminal_ours */
1511 NULL, /* to_terminal_info */
1512 remote_kill, /* to_kill */
1513 hppro_load, /* to_load */
1514 NULL, /* to_lookup_symbol */
1515 NULL, /* to_create_inferior */
1516 remote_mourn, /* to_mourn_inferior */
1517 0, /* to_can_run */
1518 0, /* to_notice_signals */
1519 0, /* to_thread_alive */
1520 0, /* to_stop */
1521 process_stratum, /* to_stratum */
1522 NULL, /* to_next */
1523 1, /* to_has_all_memory */
1524 1, /* to_has_memory */
1525 1, /* to_has_stack */
1526 1, /* to_has_registers */
1527 1, /* to_has_execution */
1528 NULL, /* sections */
1529 NULL, /* sections_end */
1530 OPS_MAGIC /* to_magic */
1531 };
1532
1533 void
1534 _initialize_remote_hppro ()
1535 {
1536 struct cmd_list_element *c;
1537 add_target (&remote_hppro_ops);
1538
1539 /* this sets the type of download protocol */
1540 c = add_set_cmd ("loadtype", no_class, var_string, (char *)&loadtype_str,
1541 "Set the type of the remote load protocol.\n", &setlist);
1542 c->function.sfunc = set_loadtype_command;
1543 add_show_from_set (c, &showlist);
1544 loadtype_str = savestring ("generic", 8);
1545
1546 /* this adds a command to boot the board */
1547 add_com ("boot", class_support, boot_board,
1548 "Boot the damn target board.\n");
1549 }
1550
1551 static void
1552 set_loadtype_command (ignore, from_tty, c)
1553 char *ignore;
1554 int from_tty;
1555 struct cmd_list_element *c;
1556 {
1557 loadtype_str = savestring (*(char **) c->var, strlen (*(char **) c->var));
1558 }
1559
This page took 0.111867 seconds and 4 git commands to generate.