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