* paread.c, coffread.c, elfread.c: Include <time.h> before libbfd.h.
[deliverable/binutils-gdb.git] / gdb / remote.c
CommitLineData
b543979c 1/* Remote target communications for serial-line targets in custom GDB protocol
7c622b41 2 Copyright 1988, 1991, 1992, 1993 Free Software Foundation, Inc.
bd5635a1
RP
3
4This file is part of GDB.
5
b543979c 6This program is free software; you can redistribute it and/or modify
bd5635a1 7it under the terms of the GNU General Public License as published by
b543979c
JG
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
bd5635a1 10
b543979c 11This program is distributed in the hope that it will be useful,
bd5635a1
RP
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
b543979c
JG
17along with this program; if not, write to the Free Software
18Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
bd5635a1
RP
19
20/* Remote communication protocol.
e50ebec8
JK
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 '#'
29
30 CSUM1 and CSUM2 are ascii hex representation of an 8-bit
31 checksum of <data>, the most significant nibble is sent first.
32 the hex digits 0-9,a-f are used.
33
34 Receiver responds with:
35
36 + - if CSUM is correct and ready for next packet
37 - - if CSUM is incorrect
38
39 <data> is as follows:
bd5635a1
RP
40 All values are encoded in ascii hex digits.
41
42 Request Packet
43
44 read registers g
45 reply XX....X Each byte of register data
46 is described by two hex digits.
47 Registers are in the internal order
48 for GDB, and the bytes in a register
49 are in the same order the machine uses.
50 or ENN for an error.
51
52 write regs GXX..XX Each byte of register data
53 is described by two hex digits.
54 reply OK for success
55 ENN for an error
56
57 read mem mAA..AA,LLLL AA..AA is address, LLLL is length.
58 reply XX..XX XX..XX is mem contents
d538b510
RP
59 Can be fewer bytes than requested
60 if able to read only part of the data.
bd5635a1
RP
61 or ENN NN is errno
62
63 write mem MAA..AA,LLLL:XX..XX
64 AA..AA is address,
65 LLLL is number of bytes,
66 XX..XX is data
67 reply OK for success
d538b510
RP
68 ENN for an error (this includes the case
69 where only part of the data was
70 written).
bd5635a1
RP
71
72 cont cAA..AA AA..AA is address to resume
73 If AA..AA is omitted,
74 resume at same address.
75
76 step sAA..AA AA..AA is address to resume
77 If AA..AA is omitted,
78 resume at same address.
79
80 last signal ? Reply the current reason for stopping.
81 This is the same reply as is generated
82 for step or cont : SAA where AA is the
83 signal number.
84
85 There is no immediate reply to step or cont.
86 The reply comes when the machine stops.
87 It is SAA AA is the "signal number"
88
e50ebec8
JK
89 or... TAAn...:r...;n:r...;n...:r...;
90 AA = signal number
91 n... = register number
92 r... = register contents
758aeb93
ILT
93 or... WAA The process extited, and AA is
94 the exit status. This is only
95 applicable for certains sorts of
96 targets.
97 or... NAATT;DD;BB Relocate the object file.
98 AA = signal number
99 TT = text address
100 DD = data address
101 BB = bss address
102 This is used by the NLM stub,
103 which is why it only has three
104 addresses rather than one per
105 section: the NLM stub always
106 sees only three sections, even
107 though gdb may see more.
8f86a4e4 108
d538b510
RP
109 kill request k
110
111 toggle debug d toggle debug flag (see 386 & 68k stubs)
112 reset r reset -- see sparc stub.
113 reserved <other> On other requests, the stub should
114 ignore the request and send an empty
115 response ($#<checksum>). This way
116 we can extend the protocol and GDB
117 can tell whether the stub it is
118 talking to uses the old or the new.
bd5635a1
RP
119*/
120
d747e0af 121#include "defs.h"
bd5635a1
RP
122#include <string.h>
123#include <fcntl.h>
bd5635a1
RP
124#include "frame.h"
125#include "inferior.h"
e50ebec8 126#include "bfd.h"
6b27ebe8 127#include "symfile.h"
bd5635a1
RP
128#include "target.h"
129#include "wait.h"
130#include "terminal.h"
8f86a4e4 131#include "gdbcmd.h"
758aeb93
ILT
132#include "objfiles.h"
133#include "gdb-stabs.h"
bd5635a1 134
d538b510
RP
135#include "dcache.h"
136
8f86a4e4 137#if !defined(DONT_USE_REMOTE)
bd5635a1
RP
138#ifdef USG
139#include <sys/types.h>
140#endif
141
142#include <signal.h>
ebdb9ade 143#include "serial.h"
bd5635a1 144
b543979c
JG
145/* Prototypes for local functions */
146
d538b510
RP
147static int
148remote_write_bytes PARAMS ((CORE_ADDR memaddr, unsigned char *myaddr, int len));
b543979c 149
d538b510
RP
150static int
151remote_read_bytes PARAMS ((CORE_ADDR memaddr, unsigned char *myaddr, int len));
b543979c
JG
152
153static void
5af4f5f6 154remote_files_info PARAMS ((struct target_ops *ignore));
b543979c
JG
155
156static int
5af4f5f6
JK
157remote_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr, int len,
158 int should_write, struct target_ops *target));
b543979c
JG
159
160static void
161remote_prepare_to_store PARAMS ((void));
162
163static void
5af4f5f6 164remote_fetch_registers PARAMS ((int regno));
b543979c
JG
165
166static void
d538b510 167remote_resume PARAMS ((int pid, int step, int siggnal));
b543979c 168
7c622b41 169static int
5af4f5f6 170remote_start_remote PARAMS ((char *dummy));
7c622b41 171
b543979c 172static void
5af4f5f6 173remote_open PARAMS ((char *name, int from_tty));
b543979c
JG
174
175static void
5af4f5f6 176remote_close PARAMS ((int quitting));
b543979c
JG
177
178static void
5af4f5f6 179remote_store_registers PARAMS ((int regno));
b543979c
JG
180
181static void
5af4f5f6 182getpkt PARAMS ((char *buf, int forever));
b543979c
JG
183
184static void
5af4f5f6 185putpkt PARAMS ((char *buf));
b543979c
JG
186
187static void
5af4f5f6 188remote_send PARAMS ((char *buf));
b543979c
JG
189
190static int
191readchar PARAMS ((void));
192
193static int
5af4f5f6 194remote_wait PARAMS ((WAITTYPE *status));
b543979c
JG
195
196static int
5af4f5f6 197tohex PARAMS ((int nib));
b543979c
JG
198
199static int
5af4f5f6 200fromhex PARAMS ((int a));
b543979c
JG
201
202static void
5af4f5f6
JK
203remote_detach PARAMS ((char *args, int from_tty));
204
205static void
206remote_interrupt PARAMS ((int signo));
207
208static void
209remote_interrupt_twice PARAMS ((int signo));
b543979c 210
bd5635a1
RP
211extern struct target_ops remote_ops; /* Forward decl */
212
ebdb9ade
JK
213/* This was 5 seconds, which is a long time to sit and wait.
214 Unless this is going though some terminal server or multiplexer or
215 other form of hairy serial connection, I would think 2 seconds would
216 be plenty. */
217static int timeout = 2;
bd5635a1
RP
218
219#if 0
220int icache;
221#endif
222
55fea07b
JK
223/* FIXME: This is a hack which lets this file compile. It should be getting
224 this setting from remote-utils.c. */
225#define remote_debug (0)
226
16e1d1d3 227/* Descriptor for I/O to remote machine. Initialize it to NULL so that
bd5635a1
RP
228 remote_open knows that we don't have a file open when the program
229 starts. */
ebdb9ade 230serial_t remote_desc = NULL;
bd5635a1 231
b543979c 232#define PBUFSIZ 1024
bd5635a1
RP
233
234/* Maximum number of bytes to read/write at once. The value here
235 is chosen to fill up a packet (the headers account for the 32). */
236#define MAXBUFBYTES ((PBUFSIZ-32)/2)
237
b543979c
JG
238/* Round up PBUFSIZ to hold all the registers, at least. */
239#if REGISTER_BYTES > MAXBUFBYTES
240#undef PBUFSIZ
241#define PBUFSIZ (REGISTER_BYTES * 2 + 32)
bd5635a1 242#endif
bd5635a1 243\f
bd5635a1
RP
244/* Clean up connection to a remote debugger. */
245
e1ce8aa5 246/* ARGSUSED */
b543979c 247static void
bd5635a1
RP
248remote_close (quitting)
249 int quitting;
250{
ebdb9ade
JK
251 if (remote_desc)
252 SERIAL_CLOSE (remote_desc);
253 remote_desc = NULL;
b543979c
JG
254}
255
7c622b41
JG
256/* Stub for catch_errors. */
257
258static int
259remote_start_remote (dummy)
260 char *dummy;
261{
ac7a377f
JK
262 immediate_quit = 1; /* Allow user to interrupt it */
263
7c622b41 264 /* Ack any packet which the remote side has already sent. */
a4cb75b8
JK
265 /* I'm not sure this \r is needed; we don't use it any other time we
266 send an ack. */
ebdb9ade 267 SERIAL_WRITE (remote_desc, "+\r", 2);
7c622b41 268 putpkt ("?"); /* initiate a query from remote machine */
ac7a377f 269 immediate_quit = 0;
7c622b41
JG
270
271 start_remote (); /* Initialize gdb process mechanisms */
272 return 1;
273}
274
bd5635a1
RP
275/* Open a connection to a remote debugger.
276 NAME is the filename used for communication. */
277
d538b510
RP
278static DCACHE *remote_dcache;
279
b543979c 280static void
bd5635a1
RP
281remote_open (name, from_tty)
282 char *name;
283 int from_tty;
284{
bd5635a1
RP
285 if (name == 0)
286 error (
287"To open a remote debug connection, you need to specify what serial\n\
288device is attached to the remote system (e.g. /dev/ttya).");
289
f2fc6e7a
JK
290 target_preopen (from_tty);
291
ebdb9ade 292 unpush_target (&remote_ops);
bd5635a1 293
d538b510 294 remote_dcache = dcache_init (remote_read_bytes, remote_write_bytes);
bd5635a1 295
ebdb9ade
JK
296 remote_desc = SERIAL_OPEN (name);
297 if (!remote_desc)
bd5635a1
RP
298 perror_with_name (name);
299
55fea07b
JK
300#if 0
301 /* FIXME: This should be using remote-utils.c. */
b543979c
JG
302 if (baud_rate)
303 {
ebdb9ade 304 int rate;
b543979c 305
ebdb9ade
JK
306 if (sscanf (baud_rate, "%d", &rate) == 1)
307 if (SERIAL_SETBAUDRATE (remote_desc, rate))
308 {
309 SERIAL_CLOSE (remote_desc);
310 perror_with_name (name);
311 }
b543979c 312 }
55fea07b 313#endif
ebdb9ade
JK
314
315 SERIAL_RAW (remote_desc);
bd5635a1
RP
316
317 if (from_tty)
7c622b41
JG
318 {
319 puts_filtered ("Remote debugging using ");
320 puts_filtered (name);
321 puts_filtered ("\n");
322 }
bd5635a1 323 push_target (&remote_ops); /* Switch to using remote target now */
bd5635a1 324
ac7a377f
JK
325 /* Start the remote connection; if error (0), discard this target.
326 In particular, if the user quits, be sure to discard it
327 (we'd be in an inconsistent state otherwise). */
7c622b41 328 if (!catch_errors (remote_start_remote, (char *)0,
e50ebec8 329 "Couldn't establish connection to remote target\n", RETURN_MASK_ALL))
7c622b41 330 pop_target();
bd5635a1
RP
331}
332
333/* remote_detach()
334 takes a program previously attached to and detaches it.
335 We better not have left any breakpoints
336 in the program or it'll die when it hits one.
337 Close the open connection to the remote debugger.
338 Use this when you want to detach and do something else
339 with your gdb. */
340
341static void
342remote_detach (args, from_tty)
343 char *args;
344 int from_tty;
345{
346 if (args)
347 error ("Argument given to \"detach\" when remotely debugging.");
348
349 pop_target ();
350 if (from_tty)
7c622b41 351 puts_filtered ("Ending remote debugging.\n");
bd5635a1
RP
352}
353
354/* Convert hex digit A to a number. */
355
356static int
357fromhex (a)
358 int a;
359{
360 if (a >= '0' && a <= '9')
361 return a - '0';
362 else if (a >= 'a' && a <= 'f')
363 return a - 'a' + 10;
364 else
365 error ("Reply contains invalid hex digit");
366 return -1;
367}
368
369/* Convert number NIB to a hex digit. */
370
371static int
372tohex (nib)
373 int nib;
374{
375 if (nib < 10)
376 return '0'+nib;
377 else
378 return 'a'+nib-10;
379}
380\f
381/* Tell the remote machine to resume. */
382
b543979c 383static void
d538b510
RP
384remote_resume (pid, step, siggnal)
385 int pid, step, siggnal;
bd5635a1
RP
386{
387 char buf[PBUFSIZ];
388
389 if (siggnal)
ebdb9ade
JK
390 {
391 char *name;
392 target_terminal_ours_for_output ();
393 printf_filtered ("Can't send signals to a remote system. ");
394 name = strsigno (siggnal);
395 if (name)
396 printf_filtered (name);
397 else
398 printf_filtered ("Signal %d", siggnal);
399 printf_filtered (" not sent.\n");
400 target_terminal_inferior ();
401 }
bd5635a1 402
d538b510 403 dcache_flush (remote_dcache);
bd5635a1
RP
404
405 strcpy (buf, step ? "s": "c");
406
407 putpkt (buf);
408}
ebdb9ade 409\f
b543979c
JG
410/* Send ^C to target to halt it. Target will respond, and send us a
411 packet. */
412
5af4f5f6
JK
413static void
414remote_interrupt (signo)
e676a15f 415 int signo;
b543979c 416{
ebdb9ade
JK
417 /* If this doesn't work, try more severe steps. */
418 signal (signo, remote_interrupt_twice);
8f86a4e4 419
d538b510 420 if (remote_debug)
8f86a4e4
JG
421 printf ("remote_interrupt called\n");
422
ebdb9ade 423 SERIAL_WRITE (remote_desc, "\003", 1); /* Send a ^C */
b543979c
JG
424}
425
5af4f5f6
JK
426static void (*ofunc)();
427
ebdb9ade
JK
428/* The user typed ^C twice. */
429static void
430remote_interrupt_twice (signo)
431 int signo;
432{
433 signal (signo, ofunc);
434
435 target_terminal_ours ();
6b27ebe8 436 if (query ("Interrupted while waiting for the program.\n\
ebdb9ade
JK
437Give up (and stop debugging it)? "))
438 {
439 target_mourn_inferior ();
e50ebec8 440 return_to_top_level (RETURN_QUIT);
ebdb9ade
JK
441 }
442 else
443 {
444 signal (signo, remote_interrupt);
445 target_terminal_inferior ();
446 }
447}
b543979c 448
bd5635a1 449/* Wait until the remote machine stops, then return,
e1ce8aa5
JK
450 storing status in STATUS just as `wait' would.
451 Returns "pid" (though it's not clear what, if anything, that
452 means in the case of this target). */
bd5635a1 453
b543979c 454static int
bd5635a1
RP
455remote_wait (status)
456 WAITTYPE *status;
457{
458 unsigned char buf[PBUFSIZ];
8f86a4e4 459
bd5635a1 460 WSETEXIT ((*status), 0);
b543979c 461
4f8a48e5 462 while (1)
8f86a4e4 463 {
4f8a48e5 464 unsigned char *p;
a03d4f8e 465
4f8a48e5
ILT
466 ofunc = (void (*)()) signal (SIGINT, remote_interrupt);
467 getpkt ((char *) buf, 1);
468 signal (SIGINT, ofunc);
4ecee2f9 469
4f8a48e5
ILT
470 if (buf[0] == 'E')
471 warning ("Remote failure reply: %s", buf);
472 else if (buf[0] == 'T')
8f86a4e4 473 {
4f8a48e5
ILT
474 int i;
475 long regno;
476 char regs[MAX_REGISTER_RAW_SIZE];
a03d4f8e 477
4f8a48e5
ILT
478 /* Expedited reply, containing Signal, {regno, reg} repeat */
479 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
480 ss = signal number
481 n... = register number
482 r... = register contents
483 */
5af4f5f6 484
4f8a48e5 485 p = &buf[3]; /* after Txx */
5af4f5f6 486
4f8a48e5
ILT
487 while (*p)
488 {
489 unsigned char *p1;
5af4f5f6 490
4f8a48e5 491 regno = strtol (p, &p1, 16); /* Read the register number */
5af4f5f6 492
4f8a48e5
ILT
493 if (p1 == p)
494 warning ("Remote sent badly formed register number: %s\nPacket: '%s'\n",
495 p1, buf);
4ecee2f9 496
4f8a48e5 497 p = p1;
4ecee2f9 498
4f8a48e5
ILT
499 if (*p++ != ':')
500 warning ("Malformed packet (missing colon): %s\nPacket: '%s'\n",
501 p, buf);
a03d4f8e 502
4f8a48e5
ILT
503 if (regno >= NUM_REGS)
504 warning ("Remote sent bad register number %d: %s\nPacket: '%s'\n",
505 regno, p, buf);
506
507 for (i = 0; i < REGISTER_RAW_SIZE (regno); i++)
508 {
509 if (p[0] == 0 || p[1] == 0)
510 warning ("Remote reply is too short: %s", buf);
511 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
512 p += 2;
513 }
514
515 if (*p++ != ';')
516 warning ("Remote register badly formatted: %s", buf);
517
518 supply_register (regno, regs);
519 }
520 break;
8f86a4e4 521 }
4f8a48e5 522 else if (buf[0] == 'N')
758aeb93 523 {
4f8a48e5
ILT
524 unsigned char *p1;
525 bfd_vma text_addr, data_addr, bss_addr;
526
527 /* Relocate object file. Format is NAATT;DD;BB where AA is
528 the signal number, TT is the new text address, DD is the
529 new data address, and BB is the new bss address. This is
530 used by the NLM stub; gdb may see more sections. */
531 p = &buf[3];
532 text_addr = strtol (p, &p1, 16);
533 if (p1 == p || *p1 != ';')
534 warning ("Malformed relocation packet: Packet '%s'", buf);
535 p = p1 + 1;
536 data_addr = strtol (p, &p1, 16);
537 if (p1 == p || *p1 != ';')
538 warning ("Malformed relocation packet: Packet '%s'", buf);
539 p = p1 + 1;
540 bss_addr = strtol (p, &p1, 16);
541 if (p1 == p)
542 warning ("Malformed relocation packet: Packet '%s'", buf);
543
544 if (symfile_objfile != NULL)
545 {
546 struct section_offsets *offs;
547
548 /* FIXME: Why don't the various symfile_offsets routines
549 in the sym_fns vectors set this? */
550 if (symfile_objfile->num_sections == 0)
551 symfile_objfile->num_sections = SECT_OFF_MAX;
552
553 offs = ((struct section_offsets *)
554 alloca (sizeof (struct section_offsets)
555 + (symfile_objfile->num_sections
556 * sizeof (offs->offsets))));
557 memcpy (offs, symfile_objfile->section_offsets,
558 (sizeof (struct section_offsets)
559 + (symfile_objfile->num_sections
560 * sizeof (offs->offsets))));
561 ANOFFSET (offs, SECT_OFF_TEXT) = text_addr;
562 ANOFFSET (offs, SECT_OFF_DATA) = data_addr;
563 ANOFFSET (offs, SECT_OFF_BSS) = bss_addr;
564
565 objfile_relocate (symfile_objfile, offs);
566 }
567 break;
758aeb93 568 }
4f8a48e5
ILT
569 else if (buf[0] == 'W')
570 {
571 /* The remote process exited. */
572 WSETEXIT (*status, (fromhex (buf[1]) << 4) + fromhex (buf[2]));
573 return 0;
574 }
575 else if (buf[0] == 'S')
576 break;
577 else
578 warning ("Invalid remote reply: %s", buf);
758aeb93 579 }
8f86a4e4 580
bd5635a1 581 WSETSTOP ((*status), (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))));
8f86a4e4 582
e1ce8aa5 583 return 0;
bd5635a1
RP
584}
585
55fea07b
JK
586/* Number of bytes of registers this stub implements. */
587static int register_bytes_found;
588
bd5635a1 589/* Read the remote registers into the block REGS. */
e1ce8aa5
JK
590/* Currently we just read all the registers, so we don't use regno. */
591/* ARGSUSED */
b543979c 592static void
bd5635a1
RP
593remote_fetch_registers (regno)
594 int regno;
595{
596 char buf[PBUFSIZ];
597 int i;
598 char *p;
599 char regs[REGISTER_BYTES];
600
601 sprintf (buf, "g");
602 remote_send (buf);
603
55fea07b
JK
604 /* Unimplemented registers read as all bits zero. */
605 memset (regs, 0, REGISTER_BYTES);
606
bd5635a1
RP
607 /* Reply describes registers byte by byte, each byte encoded as two
608 hex characters. Suck them all up, then supply them to the
609 register cacheing/storage mechanism. */
610
611 p = buf;
612 for (i = 0; i < REGISTER_BYTES; i++)
613 {
55fea07b
JK
614 if (p[0] == 0)
615 break;
616 if (p[1] == 0)
617 {
618 warning ("Remote reply is of odd length: %s", buf);
619 /* Don't change register_bytes_found in this case, and don't
620 print a second warning. */
621 goto supply_them;
622 }
bd5635a1
RP
623 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
624 p += 2;
625 }
55fea07b
JK
626
627 if (i != register_bytes_found)
628 {
629 register_bytes_found = i;
630#ifdef REGISTER_BYTES_OK
631 if (!REGISTER_BYTES_OK (i))
632 warning ("Remote reply is too short: %s", buf);
633#endif
634 }
635
636 supply_them:
bd5635a1
RP
637 for (i = 0; i < NUM_REGS; i++)
638 supply_register (i, &regs[REGISTER_BYTE(i)]);
bd5635a1
RP
639}
640
641/* Prepare to store registers. Since we send them all, we have to
642 read out the ones we don't want to change first. */
643
b543979c 644static void
bd5635a1
RP
645remote_prepare_to_store ()
646{
34517ebc
JG
647 /* Make sure the entire registers array is valid. */
648 read_register_bytes (0, (char *)NULL, REGISTER_BYTES);
bd5635a1
RP
649}
650
651/* Store the remote registers from the contents of the block REGISTERS.
652 FIXME, eventually just store one register if that's all that is needed. */
653
e1ce8aa5 654/* ARGSUSED */
b543979c 655static void
bd5635a1
RP
656remote_store_registers (regno)
657 int regno;
658{
659 char buf[PBUFSIZ];
660 int i;
661 char *p;
662
663 buf[0] = 'G';
664
665 /* Command describes registers byte by byte,
666 each byte encoded as two hex characters. */
667
668 p = buf + 1;
55fea07b
JK
669 /* remote_prepare_to_store insures that register_bytes_found gets set. */
670 for (i = 0; i < register_bytes_found; i++)
bd5635a1
RP
671 {
672 *p++ = tohex ((registers[i] >> 4) & 0xf);
673 *p++ = tohex (registers[i] & 0xf);
674 }
675 *p = '\0';
676
677 remote_send (buf);
bd5635a1
RP
678}
679
b43e0347
JK
680#if 0
681
682/* Use of the data cache is disabled because it loses for looking at
683 and changing hardware I/O ports and the like. Accepting `volatile'
684 would perhaps be one way to fix it, but a better way which would
685 win for more cases would be to use the executable file for the text
686 segment, like the `icache' code below but done cleanly (in some
687 target-independent place, perhaps in target_xfer_memory, perhaps
688 based on assigning each target a speed or perhaps by some simpler
689 mechanism). */
690
bd5635a1
RP
691/* Read a word from remote address ADDR and return it.
692 This goes through the data cache. */
693
b43e0347 694static int
bd5635a1
RP
695remote_fetch_word (addr)
696 CORE_ADDR addr;
697{
d538b510 698#if 0
bd5635a1
RP
699 if (icache)
700 {
701 extern CORE_ADDR text_start, text_end;
702
703 if (addr >= text_start && addr < text_end)
704 {
705 int buffer;
706 xfer_core_file (addr, &buffer, sizeof (int));
707 return buffer;
708 }
709 }
d538b510
RP
710#endif
711 return dcache_fetch (remote_dcache, addr);
bd5635a1
RP
712}
713
714/* Write a word WORD into remote address ADDR.
715 This goes through the data cache. */
716
b43e0347 717static void
bd5635a1
RP
718remote_store_word (addr, word)
719 CORE_ADDR addr;
720 int word;
721{
d538b510 722 dcache_poke (remote_dcache, addr, word);
bd5635a1 723}
b43e0347 724#endif /* 0 */
bd5635a1
RP
725\f
726/* Write memory data directly to the remote machine.
727 This does not inform the data cache; the data cache uses this.
728 MEMADDR is the address in the remote memory space.
729 MYADDR is the address of the buffer in our space.
d538b510 730 LEN is the number of bytes.
bd5635a1 731
d538b510
RP
732 Returns number of bytes transferred, or 0 for error. */
733
734static int
bd5635a1
RP
735remote_write_bytes (memaddr, myaddr, len)
736 CORE_ADDR memaddr;
d538b510 737 unsigned char *myaddr;
bd5635a1
RP
738 int len;
739{
740 char buf[PBUFSIZ];
741 int i;
742 char *p;
743
744 if (len > PBUFSIZ / 2 - 20)
745 abort ();
746
747 sprintf (buf, "M%x,%x:", memaddr, len);
748
b543979c 749 /* We send target system values byte by byte, in increasing byte addresses,
bd5635a1
RP
750 each byte encoded as two hex characters. */
751
752 p = buf + strlen (buf);
753 for (i = 0; i < len; i++)
754 {
755 *p++ = tohex ((myaddr[i] >> 4) & 0xf);
756 *p++ = tohex (myaddr[i] & 0xf);
757 }
758 *p = '\0';
759
d538b510
RP
760 putpkt (buf);
761 getpkt (buf, 0);
762
763 if (buf[0] == 'E')
764 {
765 /* There is no correspondance between what the remote protocol uses
766 for errors and errno codes. We would like a cleaner way of
767 representing errors (big enough to include errno codes, bfd_error
768 codes, and others). But for now just return EIO. */
769 errno = EIO;
770 return 0;
771 }
772 return len;
bd5635a1
RP
773}
774
775/* Read memory data directly from the remote machine.
776 This does not use the data cache; the data cache uses this.
777 MEMADDR is the address in the remote memory space.
778 MYADDR is the address of the buffer in our space.
d538b510 779 LEN is the number of bytes.
bd5635a1 780
d538b510
RP
781 Returns number of bytes transferred, or 0 for error. */
782
783static int
bd5635a1
RP
784remote_read_bytes (memaddr, myaddr, len)
785 CORE_ADDR memaddr;
d538b510 786 unsigned char *myaddr;
bd5635a1
RP
787 int len;
788{
789 char buf[PBUFSIZ];
790 int i;
791 char *p;
792
793 if (len > PBUFSIZ / 2 - 1)
794 abort ();
795
796 sprintf (buf, "m%x,%x", memaddr, len);
d538b510
RP
797 putpkt (buf);
798 getpkt (buf, 0);
799
800 if (buf[0] == 'E')
801 {
802 /* There is no correspondance between what the remote protocol uses
803 for errors and errno codes. We would like a cleaner way of
804 representing errors (big enough to include errno codes, bfd_error
805 codes, and others). But for now just return EIO. */
806 errno = EIO;
807 return 0;
808 }
bd5635a1 809
b543979c 810 /* Reply describes memory byte by byte,
bd5635a1
RP
811 each byte encoded as two hex characters. */
812
813 p = buf;
814 for (i = 0; i < len; i++)
815 {
816 if (p[0] == 0 || p[1] == 0)
d538b510
RP
817 /* Reply is short. This means that we were able to read only part
818 of what we wanted to. */
819 break;
bd5635a1
RP
820 myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
821 p += 2;
822 }
d538b510 823 return i;
bd5635a1
RP
824}
825\f
826/* Read or write LEN bytes from inferior memory at MEMADDR, transferring
e1ce8aa5 827 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
bd5635a1
RP
828 nonzero. Returns length of data written or read; 0 for error. */
829
b543979c
JG
830/* ARGSUSED */
831static int
832remote_xfer_memory(memaddr, myaddr, len, should_write, target)
bd5635a1
RP
833 CORE_ADDR memaddr;
834 char *myaddr;
835 int len;
e1ce8aa5 836 int should_write;
b543979c 837 struct target_ops *target; /* ignored */
bd5635a1 838{
bd5635a1 839 int xfersize;
d538b510
RP
840 int bytes_xferred;
841 int total_xferred = 0;
842
bd5635a1
RP
843 while (len > 0)
844 {
845 if (len > MAXBUFBYTES)
846 xfersize = MAXBUFBYTES;
847 else
848 xfersize = len;
849
e1ce8aa5 850 if (should_write)
d538b510 851 bytes_xferred = remote_write_bytes (memaddr, myaddr, xfersize);
bd5635a1 852 else
d538b510
RP
853 bytes_xferred = remote_read_bytes (memaddr, myaddr, xfersize);
854
855 /* If we get an error, we are done xferring. */
856 if (bytes_xferred == 0)
857 break;
858
859 memaddr += bytes_xferred;
860 myaddr += bytes_xferred;
861 len -= bytes_xferred;
862 total_xferred += bytes_xferred;
bd5635a1 863 }
d538b510 864 return total_xferred;
bd5635a1
RP
865}
866
b543979c 867static void
8f86a4e4 868remote_files_info (ignore)
5af4f5f6 869 struct target_ops *ignore;
bd5635a1 870{
7c622b41 871 puts_filtered ("Debugging a target over a serial line.\n");
bd5635a1
RP
872}
873\f
e50ebec8
JK
874/* Stuff for dealing with the packets which are part of this protocol.
875 See comment at top of file for details. */
bd5635a1 876
ebdb9ade 877/* Read a single character from the remote end, masking it down to 7 bits. */
b543979c 878
bd5635a1
RP
879static int
880readchar ()
881{
ebdb9ade 882 int ch;
bd5635a1 883
ebdb9ade 884 ch = SERIAL_READCHAR (remote_desc, timeout);
fce7f2d9 885
ebdb9ade
JK
886 if (ch < 0)
887 return ch;
bd5635a1 888
ebdb9ade 889 return ch & 0x7f;
bd5635a1
RP
890}
891
892/* Send the command in BUF to the remote machine,
893 and read the reply into BUF.
894 Report an error if we get an error reply. */
895
896static void
897remote_send (buf)
898 char *buf;
899{
900
901 putpkt (buf);
7c622b41 902 getpkt (buf, 0);
bd5635a1
RP
903
904 if (buf[0] == 'E')
905 error ("Remote failure reply: %s", buf);
906}
907
908/* Send a packet to the remote machine, with error checking.
909 The data of the packet is in BUF. */
910
911static void
912putpkt (buf)
913 char *buf;
914{
915 int i;
916 unsigned char csum = 0;
b543979c 917 char buf2[PBUFSIZ];
bd5635a1 918 int cnt = strlen (buf);
ebdb9ade 919 int ch;
bd5635a1
RP
920 char *p;
921
922 /* Copy the packet into buffer BUF2, encapsulating it
923 and giving it a checksum. */
924
b543979c
JG
925 if (cnt > sizeof(buf2) - 5) /* Prosanity check */
926 abort();
927
bd5635a1
RP
928 p = buf2;
929 *p++ = '$';
930
931 for (i = 0; i < cnt; i++)
932 {
933 csum += buf[i];
934 *p++ = buf[i];
935 }
936 *p++ = '#';
937 *p++ = tohex ((csum >> 4) & 0xf);
938 *p++ = tohex (csum & 0xf);
939
940 /* Send it over and over until we get a positive ack. */
941
6b27ebe8
JK
942 while (1)
943 {
d538b510 944 if (remote_debug)
6b27ebe8
JK
945 {
946 *p = '\0';
947 printf ("Sending packet: %s...", buf2); fflush(stdout);
948 }
949 if (SERIAL_WRITE (remote_desc, buf2, p - buf2))
950 perror_with_name ("putpkt: write failed");
951
952 /* read until either a timeout occurs (-2) or '+' is read */
953 while (1)
954 {
955 ch = readchar ();
956
957 switch (ch)
958 {
959 case '+':
d538b510 960 if (remote_debug)
6b27ebe8
JK
961 printf("Ack\n");
962 return;
963 case SERIAL_TIMEOUT:
964 break; /* Retransmit buffer */
965 case SERIAL_ERROR:
966 perror_with_name ("putpkt: couldn't read ACK");
967 case SERIAL_EOF:
968 error ("putpkt: EOF while trying to read ACK");
969 default:
d538b510 970 if (remote_debug)
6b27ebe8
JK
971 printf ("%02X %c ", ch&0xFF, ch);
972 continue;
973 }
974 break; /* Here to retransmit */
975 }
976 }
bd5635a1
RP
977}
978
979/* Read a packet from the remote machine, with error checking,
7c622b41
JG
980 and store it in BUF. BUF is expected to be of size PBUFSIZ.
981 If FOREVER, wait forever rather than timing out; this is used
982 while the target is executing user code. */
bd5635a1
RP
983
984static void
7c622b41 985getpkt (buf, forever)
bd5635a1 986 char *buf;
ebdb9ade 987 int forever;
bd5635a1
RP
988{
989 char *bp;
990 unsigned char csum;
7c622b41 991 int c = 0;
bd5635a1 992 unsigned char c1, c2;
38094c60
JG
993 int retries = 0;
994#define MAX_RETRIES 10
bd5635a1 995
bd5635a1
RP
996 while (1)
997 {
7c622b41
JG
998 /* This can loop forever if the remote side sends us characters
999 continuously, but if it pauses, we'll get a zero from readchar
1000 because of timeout. Then we'll count that as a retry. */
6b27ebe8
JK
1001
1002 c = readchar();
1003 if (c > 0 && c != '$')
1004 continue;
1005
1006 if (c == SERIAL_TIMEOUT)
1007 {
1008 if (forever)
1009 continue;
1010 if (++retries >= MAX_RETRIES)
d538b510 1011 if (remote_debug) puts_filtered ("Timed out.\n");
6b27ebe8
JK
1012 goto out;
1013 }
1014
1015 if (c == SERIAL_EOF)
1016 error ("Remote connection closed");
1017 if (c == SERIAL_ERROR)
1018 perror_with_name ("Remote communication error");
7c622b41 1019
bd5635a1
RP
1020 /* Force csum to be zero here because of possible error retry. */
1021 csum = 0;
bd5635a1 1022 bp = buf;
7c622b41 1023
bd5635a1
RP
1024 while (1)
1025 {
1026 c = readchar ();
ebdb9ade 1027 if (c == SERIAL_TIMEOUT)
7c622b41 1028 {
d538b510 1029 if (remote_debug)
7c622b41
JG
1030 puts_filtered ("Timeout in mid-packet, retrying\n");
1031 goto whole; /* Start a new packet, count retries */
1032 }
1033 if (c == '$')
1034 {
d538b510 1035 if (remote_debug)
7c622b41
JG
1036 puts_filtered ("Saw new packet start in middle of old one\n");
1037 goto whole; /* Start a new packet, count retries */
1038 }
bd5635a1
RP
1039 if (c == '#')
1040 break;
8f86a4e4
JG
1041 if (bp >= buf+PBUFSIZ-1)
1042 {
1043 *bp = '\0';
7c622b41
JG
1044 puts_filtered ("Remote packet too long: ");
1045 puts_filtered (buf);
1046 puts_filtered ("\n");
8f86a4e4
JG
1047 goto whole;
1048 }
bd5635a1
RP
1049 *bp++ = c;
1050 csum += c;
1051 }
1052 *bp = 0;
1053
1054 c1 = fromhex (readchar ());
1055 c2 = fromhex (readchar ());
1056 if ((csum & 0xff) == (c1 << 4) + c2)
1057 break;
7c622b41
JG
1058 printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
1059 (c1 << 4) + c2, csum & 0xff);
1060 puts_filtered (buf);
1061 puts_filtered ("\n");
38094c60 1062
8f86a4e4
JG
1063 /* Try the whole thing again. */
1064whole:
38094c60
JG
1065 if (++retries < MAX_RETRIES)
1066 {
ebdb9ade 1067 SERIAL_WRITE (remote_desc, "-", 1);
38094c60
JG
1068 }
1069 else
1070 {
1071 printf ("Ignoring packet error, continuing...\n");
1072 break;
1073 }
bd5635a1
RP
1074 }
1075
7c622b41
JG
1076out:
1077
ebdb9ade 1078 SERIAL_WRITE (remote_desc, "+", 1);
bd5635a1 1079
d538b510 1080 if (remote_debug)
8f86a4e4 1081 fprintf (stderr,"Packet received: %s\n", buf);
bd5635a1
RP
1082}
1083\f
ebdb9ade
JK
1084static void
1085remote_kill ()
1086{
1087 putpkt ("k");
1088 /* Don't wait for it to die. I'm not really sure it matters whether
1089 we do or not. For the existing stubs, kill is a noop. */
1090 target_mourn_inferior ();
1091}
bd5635a1 1092
ebdb9ade
JK
1093static void
1094remote_mourn ()
1095{
1096 unpush_target (&remote_ops);
1097 generic_mourn_inferior ();
1098}
1099\f
5af4f5f6
JK
1100#ifdef REMOTE_BREAKPOINT
1101
1102/* On some machines, e.g. 68k, we may use a different breakpoint instruction
1103 than other targets. */
1104static unsigned char break_insn[] = REMOTE_BREAKPOINT;
1105
1106/* Check that it fits in BREAKPOINT_MAX bytes. */
1107static unsigned char check_break_insn_size[BREAKPOINT_MAX] = REMOTE_BREAKPOINT;
1108
1109#else /* No REMOTE_BREAKPOINT. */
1110
1111/* Same old breakpoint instruction. This code does nothing different
1112 than mem-break.c. */
1113static unsigned char break_insn[] = BREAKPOINT;
1114
1115#endif /* No REMOTE_BREAKPOINT. */
1116
1117/* Insert a breakpoint on targets that don't have any better breakpoint
1118 support. We read the contents of the target location and stash it,
1119 then overwrite it with a breakpoint instruction. ADDR is the target
1120 location in the target machine. CONTENTS_CACHE is a pointer to
1121 memory allocated for saving the target contents. It is guaranteed
1122 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
1123 is accomplished via BREAKPOINT_MAX). */
1124
d538b510 1125static int
5af4f5f6
JK
1126remote_insert_breakpoint (addr, contents_cache)
1127 CORE_ADDR addr;
1128 char *contents_cache;
1129{
1130 int val;
1131
1132 val = target_read_memory (addr, contents_cache, sizeof break_insn);
1133
1134 if (val == 0)
1135 val = target_write_memory (addr, (char *)break_insn, sizeof break_insn);
1136
1137 return val;
1138}
1139
d538b510 1140static int
5af4f5f6
JK
1141remote_remove_breakpoint (addr, contents_cache)
1142 CORE_ADDR addr;
1143 char *contents_cache;
1144{
1145 return target_write_memory (addr, contents_cache, sizeof break_insn);
1146}
1147\f
bd5635a1
RP
1148/* Define the target subroutine names */
1149
1150struct target_ops remote_ops = {
b543979c
JG
1151 "remote", /* to_shortname */
1152 "Remote serial target in gdb-specific protocol", /* to_longname */
1153 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
1154Specify the serial device it is connected to (e.g. /dev/ttya).", /* to_doc */
1155 remote_open, /* to_open */
1156 remote_close, /* to_close */
1157 NULL, /* to_attach */
1158 remote_detach, /* to_detach */
1159 remote_resume, /* to_resume */
1160 remote_wait, /* to_wait */
1161 remote_fetch_registers, /* to_fetch_registers */
1162 remote_store_registers, /* to_store_registers */
1163 remote_prepare_to_store, /* to_prepare_to_store */
b543979c
JG
1164 remote_xfer_memory, /* to_xfer_memory */
1165 remote_files_info, /* to_files_info */
5af4f5f6
JK
1166
1167 remote_insert_breakpoint, /* to_insert_breakpoint */
1168 remote_remove_breakpoint, /* to_remove_breakpoint */
1169
b543979c
JG
1170 NULL, /* to_terminal_init */
1171 NULL, /* to_terminal_inferior */
1172 NULL, /* to_terminal_ours_for_output */
1173 NULL, /* to_terminal_ours */
1174 NULL, /* to_terminal_info */
ebdb9ade 1175 remote_kill, /* to_kill */
6b27ebe8 1176 generic_load, /* to_load */
b543979c
JG
1177 NULL, /* to_lookup_symbol */
1178 NULL, /* to_create_inferior */
ebdb9ade 1179 remote_mourn, /* to_mourn_inferior */
34517ebc 1180 0, /* to_can_run */
7c622b41 1181 0, /* to_notice_signals */
b543979c
JG
1182 process_stratum, /* to_stratum */
1183 NULL, /* to_next */
1184 1, /* to_has_all_memory */
1185 1, /* to_has_memory */
1186 1, /* to_has_stack */
1187 1, /* to_has_registers */
1188 1, /* to_has_execution */
1189 NULL, /* sections */
1190 NULL, /* sections_end */
1191 OPS_MAGIC /* to_magic */
bd5635a1
RP
1192};
1193
1194void
1195_initialize_remote ()
1196{
1197 add_target (&remote_ops);
1198}
8f86a4e4 1199#endif
This page took 0.19118 seconds and 4 git commands to generate.