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