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