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