]> Git Repo - binutils.git/blob - gdb/remote-pa.c
* v850-tdep.c (v850_push_arguments): Use symbolic names for arg
[binutils.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 ((char *, int));
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 static void
378 boot_board (dummy1, dummy2)
379      char *dummy1;
380      int dummy2;
381 {
382   char c;
383   char buf[INBUFSIZE];
384   char *ptr;
385
386   /* See if we can connect to the boot ROM command line */
387   ptr = buf;
388   while (1) {
389       SERIAL_WRITE (remote_desc, "\r\n", 2);
390       c = readchar (2);
391       if ((sr_get_debug() > 2) && (isascii(c)))
392         putchar (c);
393       if (c == SERIAL_TIMEOUT) {
394         if (sr_get_debug())
395           puts_filtered ("Timed out.\n");
396         break;
397       } 
398       if (c == '&') {
399         if (sr_get_debug() > 2)
400           puts ("Got ACK from stub");
401         break;
402       }
403       if (c == '>') {
404         if (sr_get_debug() > 2)
405           puts ("Got prompt from ROM monitor");
406         break;
407       }
408     }
409   
410 }
411
412 /* Stub for catch_errors.  */
413 static int
414 remote_start_remote (dummy)
415      char *dummy;
416 {
417   immediate_quit = 1;           /* Allow user to interrupt it */
418
419   /* Ack any packet which the remote side has already sent.  */
420
421   if (sr_get_debug())
422     puts ("Trying a '+' to ACK the target.");
423
424   SERIAL_WRITE (remote_desc, "+", 1);
425   
426 #if 0
427   boot_board();
428
429   get_offsets ();               /* Get text, data & bss offsets */
430 #endif
431
432   putpkt ("?");                 /* initiate a query from remote machine */
433   immediate_quit = 0;
434
435   start_remote ();              /* Initialize gdb process mechanisms */
436
437   return 1;
438 }
439
440 /* Open a connection to a remote debugger.
441    NAME is the filename used for communication.  */
442
443 static DCACHE *remote_dcache;
444
445 static void
446 remote_open (name, from_tty)
447      char *name;
448      int from_tty;
449 {
450   if (name == 0)
451     error (
452 "To open a remote debug connection, you need to specify what serial\n\
453 device is attached to the remote system (e.g. /dev/ttya).");
454
455   target_preopen (from_tty);
456
457   unpush_target (&remote_ops);
458
459   remote_dcache = dcache_init (remote_read_bytes, remote_write_bytes);
460
461   remote_desc = SERIAL_OPEN (name);
462   if (!remote_desc)
463     perror_with_name (name);
464
465   if (baud_rate != -1)
466     {
467       if (SERIAL_SETBAUDRATE (remote_desc, baud_rate))
468         {
469           SERIAL_CLOSE (remote_desc);
470           perror_with_name (name);
471         }
472     }
473
474   SERIAL_RAW (remote_desc);
475
476   /* If there is something sitting in the buffer we might take it as a
477      response to a command, which would be bad.  */
478   SERIAL_FLUSH_INPUT (remote_desc);
479
480   if (from_tty)
481     {
482       puts_filtered ("Remote debugging using ");
483       puts_filtered (name);
484       puts_filtered ("\n");
485     }
486   push_target (&remote_ops);    /* Switch to using remote target now */
487
488   /* Start out by trying the 'P' request to set registers.  We set this each
489      time that we open a new target so that if the user switches from one
490      stub to another, we can (if the target is closed and reopened) cope.  */
491   stub_supports_P = 1;
492
493   /* Without this, some commands which require an active target (such as kill)
494      won't work.  This variable serves (at least) double duty as both the pid
495      of the target process (if it has such), and as a flag indicating that a
496      target is active.  These functions should be split out into seperate
497      variables, especially since GDB will someday have a notion of debugging
498      several processes.  */
499
500   inferior_pid = 42000;
501
502   /* Start the remote connection; if error (0), discard this target.
503      In particular, if the user quits, be sure to discard it
504      (we'd be in an inconsistent state otherwise).  */
505   if (!catch_errors (remote_start_remote, (char *)0, 
506         "Couldn't establish connection to remote target\n", RETURN_MASK_ALL))
507     pop_target();
508 }
509
510 /* remote_detach()
511    takes a program previously attached to and detaches it.
512    We better not have left any breakpoints
513    in the program or it'll die when it hits one.
514    Close the open connection to the remote debugger.
515    Use this when you want to detach and do something else
516    with your gdb.  */
517
518 static void
519 remote_detach (args, from_tty)
520      char *args;
521      int from_tty;
522 {
523   if (args)
524     error ("Argument given to \"detach\" when remotely debugging.");
525   
526   pop_target ();
527   if (from_tty)
528     puts_filtered ("Ending remote debugging.\n");
529 }
530
531 /* Convert hex digit A to a number.  */
532
533 static int
534 fromhex (a)
535      int a;
536 {
537   if (a >= '0' && a <= '9')
538     return a - '0';
539   else if (a >= 'a' && a <= 'f')
540     return a - 'a' + 10;
541   else
542     error ("Reply contains invalid hex digit");
543 }
544
545 /* Convert number NIB to a hex digit.  */
546
547 static int
548 tohex (nib)
549      int nib;
550 {
551   if (nib < 10)
552     return '0'+nib;
553   else
554     return 'a'+nib-10;
555 }
556 \f
557 /* Tell the remote machine to resume.  */
558
559 static void
560 remote_resume (pid, step, siggnal)
561      int pid, step;
562      enum target_signal siggnal;
563 {
564   char buf[PBUFSIZ];
565
566   if (siggnal)
567     {
568       target_terminal_ours_for_output ();
569       printf_filtered
570         ("Can't send signals to a remote system.  %s not sent.\n",
571          target_signal_to_name (siggnal));      target_terminal_inferior ();
572     }
573
574   dcache_flush (remote_dcache);
575
576   strcpy (buf, step ? "s": "c");
577
578   putpkt (buf);
579 }
580 \f
581 /* Send ^C to target to halt it.  Target will respond, and send us a
582    packet.  */
583
584 static void
585 remote_interrupt (signo)
586      int signo;
587 {
588   /* If this doesn't work, try more severe steps.  */
589   signal (signo, remote_interrupt_twice);
590   
591   if (remote_debug)
592     printf_unfiltered ("remote_interrupt called\n");
593
594   SERIAL_WRITE (remote_desc, "\003", 1); /* Send a ^C */
595 }
596
597 static void (*ofunc)();
598
599 /* The user typed ^C twice.  */
600 static void
601 remote_interrupt_twice (signo)
602      int signo;
603 {
604   signal (signo, ofunc);
605   
606   interrupt_query ();
607
608   signal (signo, remote_interrupt);
609 }
610
611 /* Ask the user what to do when an interrupt is received.  */
612
613 static void
614 interrupt_query ()
615 {
616   target_terminal_ours ();
617
618   if (query ("Interrupted while waiting for the program.\n\
619 Give up (and stop debugging it)? "))
620     {
621       target_mourn_inferior ();
622       return_to_top_level (RETURN_QUIT);
623     }
624
625   target_terminal_inferior ();
626 }
627
628 /* Wait until the remote machine stops, then return,
629    storing status in STATUS just as `wait' would.
630    Returns "pid" (though it's not clear what, if anything, that
631    means in the case of this target).  */
632
633 static int
634 remote_wait (pid, status)
635      int pid;
636      struct target_waitstatus *status;
637 {
638   unsigned char buf[PBUFSIZ];
639
640   status->kind = TARGET_WAITKIND_EXITED;
641   status->value.integer = 0;
642
643   while (1)
644     {
645       unsigned char *p;
646
647       ofunc = (void (*)()) signal (SIGINT, remote_interrupt);
648       getpkt ((char *) buf, 1);
649       signal (SIGINT, ofunc);
650
651       switch (buf[0])
652         {
653         case 'E':               /* Error of some sort */
654           warning ("Remote failure reply: %s", buf);
655           continue;
656         case 'T':               /* Status with PC, SP, FP, ... */
657           {
658             int i;
659             long regno;
660             char regs[MAX_REGISTER_RAW_SIZE];
661
662             /* Expedited reply, containing Signal, {regno, reg} repeat */
663             /*  format is:  'Tssn...:r...;n...:r...;n...:r...;#cc', where
664                 ss = signal number
665                 n... = register number
666                 r... = register contents
667                 */
668
669             p = &buf[3];        /* after Txx */
670
671             while (*p)
672               {
673                 unsigned char *p1;
674
675                 regno = strtol (p, (char **) &p1, 16); /* Read the register number */
676
677                 if (p1 == p)
678                   warning ("Remote sent badly formed register number: %s\nPacket: '%s'\n",
679                            p1, buf);
680
681                 p = p1;
682
683                 if (*p++ != ':')
684                   warning ("Malformed packet (missing colon): %s\nPacket: '%s'\n",
685                            p, buf);
686
687                 if (regno >= NUM_REGS)
688                   warning ("Remote sent bad register number %d: %s\nPacket: '%s'\n",
689                            regno, p, buf);
690
691                 for (i = 0; i < REGISTER_RAW_SIZE (regno); i++)
692                   {
693                     if (p[0] == 0 || p[1] == 0)
694                       warning ("Remote reply is too short: %s", buf);
695                     regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
696                     p += 2;
697                   }
698
699                 if (*p++ != ';')
700                   warning ("Remote register badly formatted: %s", buf);
701
702                 supply_register (regno, regs);
703               }
704           }
705           /* fall through */
706         case 'S':               /* Old style status, just signal only */
707           status->kind = TARGET_WAITKIND_STOPPED;
708           status->value.sig = (enum target_signal)
709             (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
710
711           return inferior_pid;
712         case 'W':               /* Target exited */
713           {
714             /* The remote process exited.  */
715             status->kind = TARGET_WAITKIND_EXITED;
716             status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
717             return inferior_pid;
718           }
719         case 'O':               /* Console output */
720           fputs_filtered (buf + 1, gdb_stdout);
721           continue;
722         default:
723           warning ("Invalid remote reply: %s", buf);
724           continue;
725         }
726     }
727   return inferior_pid;
728 }
729
730 /* Number of bytes of registers this stub implements.  */
731 static int register_bytes_found;
732
733 /* Read the remote registers into the block REGS.  */
734 /* Currently we just read all the registers, so we don't use regno.  */
735 /* ARGSUSED */
736 static void
737 remote_fetch_registers (regno)
738      int regno;
739 {
740   char buf[PBUFSIZ];
741   int i;
742   char *p;
743   char regs[REGISTER_BYTES];
744
745   sprintf (buf, "g");
746   remote_send (buf);
747
748   /* Unimplemented registers read as all bits zero.  */
749   memset (regs, 0, REGISTER_BYTES);
750
751   /* We can get out of synch in various cases.  If the first character
752      in the buffer is not a hex character, assume that has happened
753      and try to fetch another packet to read.  */
754   while ((buf[0] < '0' || buf[0] > '9')
755          && (buf[0] < 'a' || buf[0] > 'f'))
756     {
757       if (remote_debug)
758         printf_unfiltered ("Bad register packet; fetching a new packet\n");
759       getpkt (buf, 0);
760     }
761
762   /* Reply describes registers byte by byte, each byte encoded as two
763      hex characters.  Suck them all up, then supply them to the
764      register cacheing/storage mechanism.  */
765
766   p = buf;
767   for (i = 0; i < REGISTER_BYTES; i++)
768     {
769       if (p[0] == 0)
770         break;
771       if (p[1] == 0)
772         {
773           warning ("Remote reply is of odd length: %s", buf);
774           /* Don't change register_bytes_found in this case, and don't
775              print a second warning.  */
776           goto supply_them;
777         }
778       regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
779       p += 2;
780     }
781
782   if (i != register_bytes_found)
783     {
784       register_bytes_found = i;
785 #ifdef REGISTER_BYTES_OK
786       if (!REGISTER_BYTES_OK (i))
787         warning ("Remote reply is too short: %s", buf);
788 #endif
789     }
790
791  supply_them:
792   for (i = 0; i < NUM_REGS; i++)
793     supply_register (i, &regs[REGISTER_BYTE(i)]);
794 }
795
796 /* Prepare to store registers.  Since we may send them all (using a
797    'G' request), we have to read out the ones we don't want to change
798    first.  */
799
800 static void 
801 remote_prepare_to_store ()
802 {
803   /* Make sure the entire registers array is valid.  */
804   read_register_bytes (0, (char *)NULL, REGISTER_BYTES);
805 }
806
807 /* Store register REGNO, or all registers if REGNO == -1, from the contents
808    of REGISTERS.  FIXME: ignores errors.  */
809
810 static void
811 remote_store_registers (regno)
812      int regno;
813 {
814   char buf[PBUFSIZ];
815   int i;
816   char *p;
817
818   if (regno >= 0 && stub_supports_P)
819     {
820       /* Try storing a single register.  */
821       char *regp;
822
823       sprintf (buf, "P%x=", regno);
824       p = buf + strlen (buf);
825       regp = &registers[REGISTER_BYTE (regno)];
826       for (i = 0; i < REGISTER_RAW_SIZE (regno); ++i)
827         {
828           *p++ = tohex ((regp[i] >> 4) & 0xf);
829           *p++ = tohex (regp[i] & 0xf);
830         }
831       *p = '\0';
832       remote_send (buf);
833       if (buf[0] != '\0')
834         {
835           /* The stub understands the 'P' request.  We are done.  */
836           return;
837         }
838
839       /* The stub does not support the 'P' request.  Use 'G' instead,
840          and don't try using 'P' in the future (it will just waste our
841          time).  */
842       stub_supports_P = 0;
843     }
844
845   buf[0] = 'G';
846
847   /* Command describes registers byte by byte,
848      each byte encoded as two hex characters.  */
849
850   p = buf + 1;
851   /* remote_prepare_to_store insures that register_bytes_found gets set.  */
852   for (i = 0; i < register_bytes_found; i++)
853     {
854       *p++ = tohex ((registers[i] >> 4) & 0xf);
855       *p++ = tohex (registers[i] & 0xf);
856     }
857   *p = '\0';
858
859   remote_send (buf);
860 }
861
862 #if 0
863
864 /* Use of the data cache is disabled because it loses for looking at
865    and changing hardware I/O ports and the like.  Accepting `volatile'
866    would perhaps be one way to fix it.  Another idea would be to use the
867    executable file for the text segment (for all SEC_CODE sections?
868    For all SEC_READONLY sections?).  This has problems if you want to
869    actually see what the memory contains (e.g. self-modifying code,
870    clobbered memory, user downloaded the wrong thing).  */
871
872 /* Read a word from remote address ADDR and return it.
873    This goes through the data cache.  */
874
875 static int
876 remote_fetch_word (addr)
877      CORE_ADDR addr;
878 {
879   return dcache_fetch (remote_dcache, addr);
880 }
881
882 /* Write a word WORD into remote address ADDR.
883    This goes through the data cache.  */
884
885 static void
886 remote_store_word (addr, word)
887      CORE_ADDR addr;
888      int word;
889 {
890   dcache_poke (remote_dcache, addr, word);
891 }
892 #endif /* 0 */
893 \f
894 /* Write memory data directly to the remote machine.
895    This does not inform the data cache; the data cache uses this.
896    MEMADDR is the address in the remote memory space.
897    MYADDR is the address of the buffer in our space.
898    LEN is the number of bytes.
899
900    Returns number of bytes transferred, or 0 for error.  */
901
902 static int
903 remote_write_bytes (memaddr, myaddr, len)
904      CORE_ADDR memaddr;
905      char *myaddr;
906      int len;
907 {
908   char buf[PBUFSIZ];
909   int i;
910   char *p;
911
912   /* FIXME-32x64: Need a version of print_address_numeric which puts the
913      result in a buffer like sprintf.  */
914   sprintf (buf, "M%lx,%x:", (unsigned long) memaddr, len);
915
916   /* We send target system values byte by byte, in increasing byte addresses,
917      each byte encoded as two hex characters.  */
918
919   p = buf + strlen (buf);
920   for (i = 0; i < len; i++)
921     {
922       *p++ = tohex ((myaddr[i] >> 4) & 0xf);
923       *p++ = tohex (myaddr[i] & 0xf);
924     }
925   *p = '\0';
926
927   putpkt (buf);
928   getpkt (buf, 0);
929
930   if (buf[0] == 'E')
931     {
932       /* There is no correspondance between what the remote protocol uses
933          for errors and errno codes.  We would like a cleaner way of
934          representing errors (big enough to include errno codes, bfd_error
935          codes, and others).  But for now just return EIO.  */
936       errno = EIO;
937       return 0;
938     }
939   return len;
940 }
941
942 /* Read memory data directly from the remote machine.
943    This does not use the data cache; the data cache uses this.
944    MEMADDR is the address in the remote memory space.
945    MYADDR is the address of the buffer in our space.
946    LEN is the number of bytes.
947
948    Returns number of bytes transferred, or 0 for error.  */
949
950 static int
951 remote_read_bytes (memaddr, myaddr, len)
952      CORE_ADDR memaddr;
953      char *myaddr;
954      int len;
955 {
956   char buf[PBUFSIZ];
957   int i;
958   char *p;
959
960   if (len > PBUFSIZ / 2 - 1)
961     abort ();
962
963   /* FIXME-32x64: Need a version of print_address_numeric which puts the
964      result in a buffer like sprintf.  */
965   sprintf (buf, "m%lx,%x", (unsigned long) memaddr, len);
966   putpkt (buf);
967   getpkt (buf, 0);
968
969   if (buf[0] == 'E')
970     {
971       /* There is no correspondance between what the remote protocol uses
972          for errors and errno codes.  We would like a cleaner way of
973          representing errors (big enough to include errno codes, bfd_error
974          codes, and others).  But for now just return EIO.  */
975       errno = EIO;
976       return 0;
977     }
978
979   /* Reply describes memory byte by byte,
980      each byte encoded as two hex characters.  */
981
982   p = buf;
983   for (i = 0; i < len; i++)
984     {
985       if (p[0] == 0 || p[1] == 0)
986         /* Reply is short.  This means that we were able to read only part
987            of what we wanted to.  */
988         break;
989       myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
990       p += 2;
991     }
992   return i;
993 }
994 \f
995 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
996    to or from debugger address MYADDR.  Write to inferior if SHOULD_WRITE is
997    nonzero.  Returns length of data written or read; 0 for error.  */
998
999 /* ARGSUSED */
1000 static int
1001 remote_xfer_memory(memaddr, myaddr, len, should_write, target)
1002      CORE_ADDR memaddr;
1003      char *myaddr;
1004      int len;
1005      int should_write;
1006      struct target_ops *target;                 /* ignored */
1007 {
1008   int xfersize;
1009   int bytes_xferred;
1010   int total_xferred = 0;
1011
1012   while (len > 0)
1013     {
1014       if (len > MAXBUFBYTES)
1015         xfersize = MAXBUFBYTES;
1016       else
1017         xfersize = len;
1018
1019       if (should_write)
1020         bytes_xferred = remote_write_bytes (memaddr, myaddr, xfersize);
1021       else
1022         bytes_xferred = remote_read_bytes (memaddr, myaddr, xfersize);
1023
1024       /* If we get an error, we are done xferring.  */
1025       if (bytes_xferred == 0)
1026         break;
1027
1028       memaddr += bytes_xferred;
1029       myaddr  += bytes_xferred;
1030       len     -= bytes_xferred;
1031       total_xferred += bytes_xferred;
1032     }
1033   return total_xferred;
1034 }
1035
1036 #if 0
1037 /* Enable after 4.12.  */
1038
1039 void
1040 remote_search (len, data, mask, startaddr, increment, lorange, hirange
1041                addr_found, data_found)
1042      int len;
1043      char *data;
1044      char *mask;
1045      CORE_ADDR startaddr;
1046      int increment;
1047      CORE_ADDR lorange;
1048      CORE_ADDR hirange;
1049      CORE_ADDR *addr_found;
1050      char *data_found;
1051 {
1052   if (increment == -4 && len == 4)
1053     {
1054       long mask_long, data_long;
1055       long data_found_long;
1056       CORE_ADDR addr_we_found;
1057       char buf[PBUFSIZ];
1058       long returned_long[2];
1059       char *p;
1060
1061       mask_long = extract_unsigned_integer (mask, len);
1062       data_long = extract_unsigned_integer (data, len);
1063       sprintf (buf, "t%x:%x,%x", startaddr, data_long, mask_long);
1064       putpkt (buf);
1065       getpkt (buf, 0);
1066       if (buf[0] == '\0')
1067         {
1068           /* The stub doesn't support the 't' request.  We might want to
1069              remember this fact, but on the other hand the stub could be
1070              switched on us.  Maybe we should remember it only until
1071              the next "target remote".  */
1072           generic_search (len, data, mask, startaddr, increment, lorange,
1073                           hirange, addr_found, data_found);
1074           return;
1075         }
1076
1077       if (buf[0] == 'E')
1078         /* There is no correspondance between what the remote protocol uses
1079            for errors and errno codes.  We would like a cleaner way of
1080            representing errors (big enough to include errno codes, bfd_error
1081            codes, and others).  But for now just use EIO.  */
1082         memory_error (EIO, startaddr);
1083       p = buf;
1084       addr_we_found = 0;
1085       while (*p != '\0' && *p != ',')
1086         addr_we_found = (addr_we_found << 4) + fromhex (*p++);
1087       if (*p == '\0')
1088         error ("Protocol error: short return for search");
1089
1090       data_found_long = 0;
1091       while (*p != '\0' && *p != ',')
1092         data_found_long = (data_found_long << 4) + fromhex (*p++);
1093       /* Ignore anything after this comma, for future extensions.  */
1094
1095       if (addr_we_found < lorange || addr_we_found >= hirange)
1096         {
1097           *addr_found = 0;
1098           return;
1099         }
1100
1101       *addr_found = addr_we_found;
1102       *data_found = store_unsigned_integer (data_we_found, len);
1103       return;
1104     }
1105   generic_search (len, data, mask, startaddr, increment, lorange,
1106                   hirange, addr_found, data_found);
1107 }
1108 #endif /* 0 */
1109 \f
1110 static void
1111 remote_files_info (ignore)
1112      struct target_ops *ignore;
1113 {
1114   puts_filtered ("Debugging a target over a serial line.\n");
1115 }
1116 \f
1117 /* Stuff for dealing with the packets which are part of this protocol.
1118    See comment at top of file for details.  */
1119
1120 /* Read a single character from the remote end, masking it down to 7 bits. */
1121
1122 static int
1123 readchar (timeout)
1124      int timeout;
1125 {
1126   int ch;
1127
1128   ch = SERIAL_READCHAR (remote_desc, timeout);
1129
1130   switch (ch)
1131     {
1132     case SERIAL_EOF:
1133       error ("Remote connection closed");
1134     case SERIAL_ERROR:
1135       perror_with_name ("Remote communication error");
1136     case SERIAL_TIMEOUT:
1137       return ch;
1138     default:
1139       return ch & 0x7f;
1140     }
1141 }
1142
1143 /* Send the command in BUF to the remote machine,
1144    and read the reply into BUF.
1145    Report an error if we get an error reply.  */
1146
1147 static void
1148 remote_send (buf)
1149      char *buf;
1150 {
1151
1152   putpkt (buf);
1153   getpkt (buf, 0);
1154
1155   if (buf[0] == 'E')
1156     error ("Remote failure reply: %s", buf);
1157 }
1158
1159 /* Send a packet to the remote machine, with error checking.
1160    The data of the packet is in BUF.  */
1161 static void
1162 putpkt (buf)
1163      char *buf;
1164 {
1165   int i;
1166   unsigned char csum = 0;
1167   char buf2[PBUFSIZ];
1168   int cnt = strlen (buf);
1169   int ch;
1170   char *p;
1171
1172   /* Copy the packet into buffer BUF2, encapsulating it
1173      and giving it a checksum.  */
1174
1175   if (cnt > sizeof(buf2) - 5)           /* Prosanity check */
1176     abort();
1177
1178   p = buf2;
1179   *p++ = '$';
1180
1181   for (i = 0; i < cnt; i++)
1182     {
1183       csum += buf[i];
1184       *p++ = buf[i];
1185     }
1186   *p++ = '#';
1187   *p++ = tohex ((csum >> 4) & 0xf);
1188   *p++ = tohex (csum & 0xf);
1189
1190   /* Send it over and over until we get a positive ack.  */
1191
1192   while (1)
1193     {
1194       int started_error_output = 0;
1195
1196       if (remote_debug)
1197         {
1198           *p = '\0';
1199           printf_unfiltered ("Sending packet: %s...", buf2);
1200           gdb_flush(gdb_stdout);
1201         }
1202       if (SERIAL_WRITE (remote_desc, buf2, p - buf2))
1203         perror_with_name ("putpkt: write failed");
1204
1205       /* read until either a timeout occurs (-2) or '+' is read */
1206       while (1)
1207         {
1208           ch = readchar (remote_timeout);
1209
1210           if (remote_debug)
1211             {
1212               switch (ch)
1213                 {
1214                 case '+':
1215                 case SERIAL_TIMEOUT:
1216                 case '$':
1217                   if (started_error_output)
1218                     {
1219                       putchar_unfiltered ('\n');
1220                       started_error_output = 0;
1221                     }
1222                 }
1223             }
1224
1225           switch (ch)
1226             {
1227             case '+':
1228               if (remote_debug)
1229                 printf_unfiltered("Got Ack\n");
1230               return;
1231             case SERIAL_TIMEOUT:
1232               break;            /* Retransmit buffer */
1233             case '$':
1234               {
1235                 unsigned char junkbuf[PBUFSIZ];
1236
1237               /* It's probably an old response, and we're out of sync.  Just
1238                  gobble up the packet and ignore it.  */
1239                 getpkt (junkbuf, 0);
1240                 continue;               /* Now, go look for + */
1241               }
1242             default:
1243               if (remote_debug)
1244                 {
1245                   if (!started_error_output)
1246                     {
1247                       started_error_output = 1;
1248                       printf_unfiltered ("putpkt: Junk: ");
1249                     }
1250                   putchar_unfiltered (ch & 0177);
1251                 }
1252               continue;
1253             }
1254           break;                /* Here to retransmit */
1255         }
1256
1257 #if 0
1258       /* This is wrong.  If doing a long backtrace, the user should be
1259          able to get out next time we call QUIT, without anything as violent
1260          as interrupt_query.  If we want to provide a way out of here
1261          without getting to the next QUIT, it should be based on hitting
1262          ^C twice as in remote_wait.  */
1263       if (quit_flag)
1264         {
1265           quit_flag = 0;
1266           interrupt_query ();
1267         }
1268 #endif
1269     }
1270 }
1271
1272 /* Come here after finding the start of the frame.  Collect the rest into BUF,
1273    verifying the checksum, length, and handling run-length compression.
1274    Returns 0 on any error, 1 on success.  */
1275
1276 static int
1277 read_frame (buf)
1278      char *buf;
1279 {
1280   unsigned char csum;
1281   char *bp;
1282   int c;
1283
1284   csum = 0;
1285   bp = buf;
1286
1287   while (1)
1288     {
1289       c = readchar (remote_timeout);
1290
1291       switch (c)
1292         {
1293         case SERIAL_TIMEOUT:
1294           if (remote_debug)
1295             puts_filtered ("Timeout in mid-packet, retrying\n");
1296           return 0;
1297         case '$':
1298           if (remote_debug)
1299             puts_filtered ("Saw new packet start in middle of old one\n");
1300           return 0;             /* Start a new packet, count retries */
1301         case '#':
1302           {
1303             unsigned char pktcsum;
1304
1305             *bp = '\000';
1306
1307             pktcsum = fromhex (readchar (remote_timeout)) << 4;
1308             pktcsum |= fromhex (readchar (remote_timeout));
1309
1310             if (csum == pktcsum)
1311               return 1;
1312
1313             printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
1314                              pktcsum, csum);
1315             puts_filtered (buf);
1316             puts_filtered ("\n");
1317
1318             return 0;
1319           }
1320         case '*':               /* Run length encoding */
1321           csum += c;
1322           c = readchar (remote_timeout);
1323           csum += c;
1324           c = c - ' ' + 3;      /* Compute repeat count */
1325
1326           if (bp + c - 1 < buf + PBUFSIZ - 1)
1327             {
1328               memset (bp, *(bp - 1), c);
1329               bp += c;
1330               continue;
1331             }
1332
1333           *bp = '\0';
1334           printf_filtered ("Repeat count %d too large for buffer: ", c);
1335           puts_filtered (buf);
1336           puts_filtered ("\n");
1337           return 0;
1338
1339         default:
1340           if (bp < buf + PBUFSIZ - 1)
1341             {
1342               *bp++ = c;
1343               csum += c;
1344               continue;
1345             }
1346
1347           *bp = '\0';
1348           puts_filtered ("Remote packet too long: ");
1349           puts_filtered (buf);
1350           puts_filtered ("\n");
1351
1352           return 0;
1353         }
1354     }
1355 }
1356
1357 /* Read a packet from the remote machine, with error checking,
1358    and store it in BUF.  BUF is expected to be of size PBUFSIZ.
1359    If FOREVER, wait forever rather than timing out; this is used
1360    while the target is executing user code.  */
1361
1362 static void
1363 getpkt (buf, forever)
1364      char *buf;
1365      int forever;
1366 {
1367   int c;
1368   int tries;
1369   int timeout;
1370   int val;
1371
1372   if (forever)
1373     timeout = -1;
1374   else
1375     timeout = remote_timeout;
1376
1377 #define MAX_TRIES 10
1378
1379   for (tries = 1; tries <= MAX_TRIES; tries++)
1380     {
1381       /* This can loop forever if the remote side sends us characters
1382          continuously, but if it pauses, we'll get a zero from readchar
1383          because of timeout.  Then we'll count that as a retry.  */
1384
1385       /* Note that we will only wait forever prior to the start of a packet.
1386          After that, we expect characters to arrive at a brisk pace.  They
1387          should show up within remote_timeout intervals.  */
1388
1389       do
1390         {
1391           c = readchar (timeout);
1392
1393           if (c == SERIAL_TIMEOUT)
1394             {
1395               if (remote_debug)
1396                 puts_filtered ("Timed out.\n");
1397               goto retry;
1398             }
1399         }
1400       while (c != '$');
1401
1402       /* We've found the start of a packet, now collect the data.  */
1403
1404       val = read_frame (buf);
1405
1406       if (val == 1)
1407         {
1408           if (remote_debug)
1409             fprintf_unfiltered (gdb_stderr, "Packet received: %s\n", buf);
1410           SERIAL_WRITE (remote_desc, "+", 1);
1411           return;
1412         }
1413
1414       /* Try the whole thing again.  */
1415 retry:
1416       SERIAL_WRITE (remote_desc, "-", 1);
1417     }
1418
1419   /* We have tried hard enough, and just can't receive the packet.  Give up. */
1420
1421   printf_unfiltered ("Ignoring packet error, continuing...\n");
1422   SERIAL_WRITE (remote_desc, "+", 1);
1423 }
1424 \f
1425 static void
1426 remote_kill ()
1427 {
1428   putpkt ("k");
1429   /* Don't wait for it to die.  I'm not really sure it matters whether
1430      we do or not.  For the existing stubs, kill is a noop.  */
1431   target_mourn_inferior ();
1432 }
1433
1434 static void
1435 remote_mourn ()
1436 {
1437   unpush_target (&remote_ops);
1438   generic_mourn_inferior ();
1439 }
1440 \f
1441 #ifdef REMOTE_BREAKPOINT
1442
1443 /* On some machines, e.g. 68k, we may use a different breakpoint instruction
1444    than other targets.  */
1445 static unsigned char break_insn[] = REMOTE_BREAKPOINT;
1446
1447 #else /* No REMOTE_BREAKPOINT.  */
1448
1449 /* Same old breakpoint instruction.  This code does nothing different
1450    than mem-break.c.  */
1451 static unsigned char break_insn[] = BREAKPOINT;
1452
1453 #endif /* No REMOTE_BREAKPOINT.  */
1454
1455 /* Insert a breakpoint on targets that don't have any better breakpoint
1456    support.  We read the contents of the target location and stash it,
1457    then overwrite it with a breakpoint instruction.  ADDR is the target
1458    location in the target machine.  CONTENTS_CACHE is a pointer to 
1459    memory allocated for saving the target contents.  It is guaranteed
1460    by the caller to be long enough to save sizeof BREAKPOINT bytes (this
1461    is accomplished via BREAKPOINT_MAX).  */
1462
1463 static int
1464 remote_insert_breakpoint (addr, contents_cache)
1465      CORE_ADDR addr;
1466      char *contents_cache;
1467 {
1468   int val;
1469
1470   val = target_read_memory (addr, contents_cache, sizeof break_insn);
1471
1472   if (val == 0)
1473     val = target_write_memory (addr, (char *)break_insn, sizeof break_insn);
1474
1475   return val;
1476 }
1477
1478 static int
1479 remote_remove_breakpoint (addr, contents_cache)
1480      CORE_ADDR addr;
1481      char *contents_cache;
1482 {
1483   return target_write_memory (addr, contents_cache, sizeof break_insn);
1484 }
1485 \f
1486 /* Define the target subroutine names */
1487
1488 struct target_ops remote_hppro_ops = {
1489   "hppro",                      /* to_shortname */
1490   "Remote serial target for HP-PRO targets",    /* to_longname */
1491   "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
1492 This is for targets that supports the HP-PRO standard.\n\
1493 Specify the serial device it is connected to (e.g. /dev/ttya) or telnet port.",  /* to_doc */
1494   remote_open,                  /* to_open */
1495   remote_close,                 /* to_close */
1496   NULL,                         /* to_attach */
1497   remote_detach,                /* to_detach */
1498   remote_resume,                /* to_resume */
1499   remote_wait,                  /* to_wait */
1500   remote_fetch_registers,       /* to_fetch_registers */
1501   remote_store_registers,       /* to_store_registers */
1502   remote_prepare_to_store,      /* to_prepare_to_store */
1503   remote_xfer_memory,           /* to_xfer_memory */
1504   remote_files_info,            /* to_files_info */
1505
1506   remote_insert_breakpoint,     /* to_insert_breakpoint */
1507   remote_remove_breakpoint,     /* to_remove_breakpoint */
1508
1509   NULL,                         /* to_terminal_init */
1510   NULL,                         /* to_terminal_inferior */
1511   NULL,                         /* to_terminal_ours_for_output */
1512   NULL,                         /* to_terminal_ours */
1513   NULL,                         /* to_terminal_info */
1514   remote_kill,                  /* to_kill */
1515   hppro_load,                   /* to_load */
1516   NULL,                         /* to_lookup_symbol */
1517   NULL,                         /* to_create_inferior */
1518   remote_mourn,                 /* to_mourn_inferior */
1519   0,                            /* to_can_run */
1520   0,                            /* to_notice_signals */
1521   0,                            /* to_thread_alive */
1522   0,                            /* to_stop */
1523   process_stratum,              /* to_stratum */
1524   NULL,                         /* to_next */
1525   1,                            /* to_has_all_memory */
1526   1,                            /* to_has_memory */
1527   1,                            /* to_has_stack */
1528   1,                            /* to_has_registers */
1529   1,                            /* to_has_execution */
1530   NULL,                         /* sections */
1531   NULL,                         /* sections_end */
1532   OPS_MAGIC                     /* to_magic */
1533 };
1534
1535 void
1536 _initialize_remote_hppro ()
1537 {
1538   struct cmd_list_element *c;
1539   add_target (&remote_hppro_ops);
1540
1541   /* this sets the type of download protocol */
1542   c = add_set_cmd ("loadtype", no_class, var_string, (char *)&loadtype_str,
1543        "Set the type of the remote load protocol.\n", &setlist);
1544   c->function.sfunc =  set_loadtype_command;
1545   add_show_from_set (c, &showlist);
1546   loadtype_str = savestring ("generic", 8);
1547
1548   /* this adds a command to boot the board */
1549     add_com ("boot", class_support, boot_board,
1550            "Boot the damn target board.\n");
1551 }
1552
1553 static void
1554 set_loadtype_command (ignore, from_tty, c)
1555      char *ignore;
1556      int from_tty;
1557      struct cmd_list_element *c;
1558 {
1559   loadtype_str = savestring (*(char **) c->var, strlen (*(char **) c->var));
1560 }
1561
This page took 0.11348 seconds and 4 git commands to generate.