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