* printcmd.c (print_scalar_formatted): When truncating value we are
[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
536 /* FIXME: Why don't the various symfile_offsets routines
537 in the sym_fns vectors set this? */
538 if (symfile_objfile->num_sections == 0)
539 symfile_objfile->num_sections = SECT_OFF_MAX;
540
541 offs = ((struct section_offsets *)
542 alloca (sizeof (struct section_offsets)
543 + (symfile_objfile->num_sections
544 * sizeof (offs->offsets))));
545 memcpy (offs, symfile_objfile->section_offsets,
546 (sizeof (struct section_offsets)
547 + (symfile_objfile->num_sections
548 * sizeof (offs->offsets))));
549 ANOFFSET (offs, SECT_OFF_TEXT) = text_addr;
550 ANOFFSET (offs, SECT_OFF_DATA) = data_addr;
551 ANOFFSET (offs, SECT_OFF_BSS) = bss_addr;
552
553 objfile_relocate (symfile_objfile, offs);
554 }
555 break;
758aeb93 556 }
4f8a48e5
ILT
557 else if (buf[0] == 'W')
558 {
559 /* The remote process exited. */
560 WSETEXIT (*status, (fromhex (buf[1]) << 4) + fromhex (buf[2]));
561 return 0;
562 }
563 else if (buf[0] == 'S')
564 break;
565 else
566 warning ("Invalid remote reply: %s", buf);
758aeb93 567 }
8f86a4e4 568
bd5635a1 569 WSETSTOP ((*status), (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))));
8f86a4e4 570
e1ce8aa5 571 return 0;
bd5635a1
RP
572}
573
55fea07b
JK
574/* Number of bytes of registers this stub implements. */
575static int register_bytes_found;
576
bd5635a1 577/* Read the remote registers into the block REGS. */
e1ce8aa5
JK
578/* Currently we just read all the registers, so we don't use regno. */
579/* ARGSUSED */
b543979c 580static void
bd5635a1
RP
581remote_fetch_registers (regno)
582 int regno;
583{
584 char buf[PBUFSIZ];
585 int i;
586 char *p;
587 char regs[REGISTER_BYTES];
588
589 sprintf (buf, "g");
590 remote_send (buf);
591
55fea07b
JK
592 /* Unimplemented registers read as all bits zero. */
593 memset (regs, 0, REGISTER_BYTES);
594
bd5635a1
RP
595 /* Reply describes registers byte by byte, each byte encoded as two
596 hex characters. Suck them all up, then supply them to the
597 register cacheing/storage mechanism. */
598
599 p = buf;
600 for (i = 0; i < REGISTER_BYTES; i++)
601 {
55fea07b
JK
602 if (p[0] == 0)
603 break;
604 if (p[1] == 0)
605 {
606 warning ("Remote reply is of odd length: %s", buf);
607 /* Don't change register_bytes_found in this case, and don't
608 print a second warning. */
609 goto supply_them;
610 }
bd5635a1
RP
611 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
612 p += 2;
613 }
55fea07b
JK
614
615 if (i != register_bytes_found)
616 {
617 register_bytes_found = i;
618#ifdef REGISTER_BYTES_OK
619 if (!REGISTER_BYTES_OK (i))
620 warning ("Remote reply is too short: %s", buf);
621#endif
622 }
623
624 supply_them:
bd5635a1
RP
625 for (i = 0; i < NUM_REGS; i++)
626 supply_register (i, &regs[REGISTER_BYTE(i)]);
bd5635a1
RP
627}
628
629/* Prepare to store registers. Since we send them all, we have to
630 read out the ones we don't want to change first. */
631
b543979c 632static void
bd5635a1
RP
633remote_prepare_to_store ()
634{
34517ebc
JG
635 /* Make sure the entire registers array is valid. */
636 read_register_bytes (0, (char *)NULL, REGISTER_BYTES);
bd5635a1
RP
637}
638
639/* Store the remote registers from the contents of the block REGISTERS.
640 FIXME, eventually just store one register if that's all that is needed. */
641
e1ce8aa5 642/* ARGSUSED */
b543979c 643static void
bd5635a1
RP
644remote_store_registers (regno)
645 int regno;
646{
647 char buf[PBUFSIZ];
648 int i;
649 char *p;
650
651 buf[0] = 'G';
652
653 /* Command describes registers byte by byte,
654 each byte encoded as two hex characters. */
655
656 p = buf + 1;
55fea07b
JK
657 /* remote_prepare_to_store insures that register_bytes_found gets set. */
658 for (i = 0; i < register_bytes_found; i++)
bd5635a1
RP
659 {
660 *p++ = tohex ((registers[i] >> 4) & 0xf);
661 *p++ = tohex (registers[i] & 0xf);
662 }
663 *p = '\0';
664
665 remote_send (buf);
bd5635a1
RP
666}
667
b43e0347
JK
668#if 0
669
670/* Use of the data cache is disabled because it loses for looking at
671 and changing hardware I/O ports and the like. Accepting `volatile'
672 would perhaps be one way to fix it, but a better way which would
673 win for more cases would be to use the executable file for the text
674 segment, like the `icache' code below but done cleanly (in some
675 target-independent place, perhaps in target_xfer_memory, perhaps
676 based on assigning each target a speed or perhaps by some simpler
677 mechanism). */
678
bd5635a1
RP
679/* Read a word from remote address ADDR and return it.
680 This goes through the data cache. */
681
b43e0347 682static int
bd5635a1
RP
683remote_fetch_word (addr)
684 CORE_ADDR addr;
685{
d538b510 686#if 0
bd5635a1
RP
687 if (icache)
688 {
689 extern CORE_ADDR text_start, text_end;
690
691 if (addr >= text_start && addr < text_end)
692 {
693 int buffer;
694 xfer_core_file (addr, &buffer, sizeof (int));
695 return buffer;
696 }
697 }
d538b510
RP
698#endif
699 return dcache_fetch (remote_dcache, addr);
bd5635a1
RP
700}
701
702/* Write a word WORD into remote address ADDR.
703 This goes through the data cache. */
704
b43e0347 705static void
bd5635a1
RP
706remote_store_word (addr, word)
707 CORE_ADDR addr;
708 int word;
709{
d538b510 710 dcache_poke (remote_dcache, addr, word);
bd5635a1 711}
b43e0347 712#endif /* 0 */
bd5635a1
RP
713\f
714/* Write memory data directly to the remote machine.
715 This does not inform the data cache; the data cache uses this.
716 MEMADDR is the address in the remote memory space.
717 MYADDR is the address of the buffer in our space.
d538b510 718 LEN is the number of bytes.
bd5635a1 719
d538b510
RP
720 Returns number of bytes transferred, or 0 for error. */
721
722static int
bd5635a1
RP
723remote_write_bytes (memaddr, myaddr, len)
724 CORE_ADDR memaddr;
d538b510 725 unsigned char *myaddr;
bd5635a1
RP
726 int len;
727{
728 char buf[PBUFSIZ];
729 int i;
730 char *p;
731
732 if (len > PBUFSIZ / 2 - 20)
733 abort ();
734
735 sprintf (buf, "M%x,%x:", memaddr, len);
736
b543979c 737 /* We send target system values byte by byte, in increasing byte addresses,
bd5635a1
RP
738 each byte encoded as two hex characters. */
739
740 p = buf + strlen (buf);
741 for (i = 0; i < len; i++)
742 {
743 *p++ = tohex ((myaddr[i] >> 4) & 0xf);
744 *p++ = tohex (myaddr[i] & 0xf);
745 }
746 *p = '\0';
747
d538b510
RP
748 putpkt (buf);
749 getpkt (buf, 0);
750
751 if (buf[0] == 'E')
752 {
753 /* There is no correspondance between what the remote protocol uses
754 for errors and errno codes. We would like a cleaner way of
755 representing errors (big enough to include errno codes, bfd_error
756 codes, and others). But for now just return EIO. */
757 errno = EIO;
758 return 0;
759 }
760 return len;
bd5635a1
RP
761}
762
763/* Read memory data directly from the remote machine.
764 This does not use the data cache; the data cache uses this.
765 MEMADDR is the address in the remote memory space.
766 MYADDR is the address of the buffer in our space.
d538b510 767 LEN is the number of bytes.
bd5635a1 768
d538b510
RP
769 Returns number of bytes transferred, or 0 for error. */
770
771static int
bd5635a1
RP
772remote_read_bytes (memaddr, myaddr, len)
773 CORE_ADDR memaddr;
d538b510 774 unsigned char *myaddr;
bd5635a1
RP
775 int len;
776{
777 char buf[PBUFSIZ];
778 int i;
779 char *p;
780
781 if (len > PBUFSIZ / 2 - 1)
782 abort ();
783
784 sprintf (buf, "m%x,%x", memaddr, len);
d538b510
RP
785 putpkt (buf);
786 getpkt (buf, 0);
787
788 if (buf[0] == 'E')
789 {
790 /* There is no correspondance between what the remote protocol uses
791 for errors and errno codes. We would like a cleaner way of
792 representing errors (big enough to include errno codes, bfd_error
793 codes, and others). But for now just return EIO. */
794 errno = EIO;
795 return 0;
796 }
bd5635a1 797
b543979c 798 /* Reply describes memory byte by byte,
bd5635a1
RP
799 each byte encoded as two hex characters. */
800
801 p = buf;
802 for (i = 0; i < len; i++)
803 {
804 if (p[0] == 0 || p[1] == 0)
d538b510
RP
805 /* Reply is short. This means that we were able to read only part
806 of what we wanted to. */
807 break;
bd5635a1
RP
808 myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
809 p += 2;
810 }
d538b510 811 return i;
bd5635a1
RP
812}
813\f
814/* Read or write LEN bytes from inferior memory at MEMADDR, transferring
e1ce8aa5 815 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
bd5635a1
RP
816 nonzero. Returns length of data written or read; 0 for error. */
817
b543979c
JG
818/* ARGSUSED */
819static int
820remote_xfer_memory(memaddr, myaddr, len, should_write, target)
bd5635a1
RP
821 CORE_ADDR memaddr;
822 char *myaddr;
823 int len;
e1ce8aa5 824 int should_write;
b543979c 825 struct target_ops *target; /* ignored */
bd5635a1 826{
bd5635a1 827 int xfersize;
d538b510
RP
828 int bytes_xferred;
829 int total_xferred = 0;
830
bd5635a1
RP
831 while (len > 0)
832 {
833 if (len > MAXBUFBYTES)
834 xfersize = MAXBUFBYTES;
835 else
836 xfersize = len;
837
e1ce8aa5 838 if (should_write)
d538b510 839 bytes_xferred = remote_write_bytes (memaddr, myaddr, xfersize);
bd5635a1 840 else
d538b510
RP
841 bytes_xferred = remote_read_bytes (memaddr, myaddr, xfersize);
842
843 /* If we get an error, we are done xferring. */
844 if (bytes_xferred == 0)
845 break;
846
847 memaddr += bytes_xferred;
848 myaddr += bytes_xferred;
849 len -= bytes_xferred;
850 total_xferred += bytes_xferred;
bd5635a1 851 }
d538b510 852 return total_xferred;
bd5635a1
RP
853}
854
b543979c 855static void
8f86a4e4 856remote_files_info (ignore)
5af4f5f6 857 struct target_ops *ignore;
bd5635a1 858{
7c622b41 859 puts_filtered ("Debugging a target over a serial line.\n");
bd5635a1
RP
860}
861\f
e50ebec8
JK
862/* Stuff for dealing with the packets which are part of this protocol.
863 See comment at top of file for details. */
bd5635a1 864
ebdb9ade 865/* Read a single character from the remote end, masking it down to 7 bits. */
b543979c 866
bd5635a1
RP
867static int
868readchar ()
869{
ebdb9ade 870 int ch;
bd5635a1 871
ebdb9ade 872 ch = SERIAL_READCHAR (remote_desc, timeout);
fce7f2d9 873
ebdb9ade
JK
874 if (ch < 0)
875 return ch;
bd5635a1 876
ebdb9ade 877 return ch & 0x7f;
bd5635a1
RP
878}
879
880/* Send the command in BUF to the remote machine,
881 and read the reply into BUF.
882 Report an error if we get an error reply. */
883
884static void
885remote_send (buf)
886 char *buf;
887{
888
889 putpkt (buf);
7c622b41 890 getpkt (buf, 0);
bd5635a1
RP
891
892 if (buf[0] == 'E')
893 error ("Remote failure reply: %s", buf);
894}
895
896/* Send a packet to the remote machine, with error checking.
897 The data of the packet is in BUF. */
898
899static void
900putpkt (buf)
901 char *buf;
902{
903 int i;
904 unsigned char csum = 0;
b543979c 905 char buf2[PBUFSIZ];
bd5635a1 906 int cnt = strlen (buf);
ebdb9ade 907 int ch;
bd5635a1
RP
908 char *p;
909
910 /* Copy the packet into buffer BUF2, encapsulating it
911 and giving it a checksum. */
912
b543979c
JG
913 if (cnt > sizeof(buf2) - 5) /* Prosanity check */
914 abort();
915
bd5635a1
RP
916 p = buf2;
917 *p++ = '$';
918
919 for (i = 0; i < cnt; i++)
920 {
921 csum += buf[i];
922 *p++ = buf[i];
923 }
924 *p++ = '#';
925 *p++ = tohex ((csum >> 4) & 0xf);
926 *p++ = tohex (csum & 0xf);
927
928 /* Send it over and over until we get a positive ack. */
929
6b27ebe8
JK
930 while (1)
931 {
66a48870 932 if (sr_get_debug ())
6b27ebe8
JK
933 {
934 *p = '\0';
935 printf ("Sending packet: %s...", buf2); fflush(stdout);
936 }
937 if (SERIAL_WRITE (remote_desc, buf2, p - buf2))
938 perror_with_name ("putpkt: write failed");
939
940 /* read until either a timeout occurs (-2) or '+' is read */
941 while (1)
942 {
943 ch = readchar ();
944
945 switch (ch)
946 {
947 case '+':
66a48870 948 if (sr_get_debug ())
6b27ebe8
JK
949 printf("Ack\n");
950 return;
951 case SERIAL_TIMEOUT:
952 break; /* Retransmit buffer */
953 case SERIAL_ERROR:
954 perror_with_name ("putpkt: couldn't read ACK");
955 case SERIAL_EOF:
956 error ("putpkt: EOF while trying to read ACK");
957 default:
66a48870 958 if (sr_get_debug ())
6b27ebe8
JK
959 printf ("%02X %c ", ch&0xFF, ch);
960 continue;
961 }
962 break; /* Here to retransmit */
963 }
964 }
bd5635a1
RP
965}
966
967/* Read a packet from the remote machine, with error checking,
7c622b41
JG
968 and store it in BUF. BUF is expected to be of size PBUFSIZ.
969 If FOREVER, wait forever rather than timing out; this is used
970 while the target is executing user code. */
bd5635a1
RP
971
972static void
7c622b41 973getpkt (buf, forever)
bd5635a1 974 char *buf;
ebdb9ade 975 int forever;
bd5635a1
RP
976{
977 char *bp;
978 unsigned char csum;
7c622b41 979 int c = 0;
bd5635a1 980 unsigned char c1, c2;
38094c60
JG
981 int retries = 0;
982#define MAX_RETRIES 10
bd5635a1 983
bd5635a1
RP
984 while (1)
985 {
7c622b41
JG
986 /* This can loop forever if the remote side sends us characters
987 continuously, but if it pauses, we'll get a zero from readchar
988 because of timeout. Then we'll count that as a retry. */
6b27ebe8
JK
989
990 c = readchar();
991 if (c > 0 && c != '$')
992 continue;
993
994 if (c == SERIAL_TIMEOUT)
995 {
996 if (forever)
997 continue;
998 if (++retries >= MAX_RETRIES)
66a48870 999 if (sr_get_debug ()) puts_filtered ("Timed out.\n");
6b27ebe8
JK
1000 goto out;
1001 }
1002
1003 if (c == SERIAL_EOF)
1004 error ("Remote connection closed");
1005 if (c == SERIAL_ERROR)
1006 perror_with_name ("Remote communication error");
7c622b41 1007
bd5635a1
RP
1008 /* Force csum to be zero here because of possible error retry. */
1009 csum = 0;
bd5635a1 1010 bp = buf;
7c622b41 1011
bd5635a1
RP
1012 while (1)
1013 {
1014 c = readchar ();
ebdb9ade 1015 if (c == SERIAL_TIMEOUT)
7c622b41 1016 {
66a48870 1017 if (sr_get_debug ())
7c622b41
JG
1018 puts_filtered ("Timeout in mid-packet, retrying\n");
1019 goto whole; /* Start a new packet, count retries */
1020 }
1021 if (c == '$')
1022 {
66a48870 1023 if (sr_get_debug ())
7c622b41
JG
1024 puts_filtered ("Saw new packet start in middle of old one\n");
1025 goto whole; /* Start a new packet, count retries */
1026 }
bd5635a1
RP
1027 if (c == '#')
1028 break;
8f86a4e4
JG
1029 if (bp >= buf+PBUFSIZ-1)
1030 {
1031 *bp = '\0';
7c622b41
JG
1032 puts_filtered ("Remote packet too long: ");
1033 puts_filtered (buf);
1034 puts_filtered ("\n");
8f86a4e4
JG
1035 goto whole;
1036 }
bd5635a1
RP
1037 *bp++ = c;
1038 csum += c;
1039 }
1040 *bp = 0;
1041
1042 c1 = fromhex (readchar ());
1043 c2 = fromhex (readchar ());
1044 if ((csum & 0xff) == (c1 << 4) + c2)
1045 break;
7c622b41
JG
1046 printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
1047 (c1 << 4) + c2, csum & 0xff);
1048 puts_filtered (buf);
1049 puts_filtered ("\n");
38094c60 1050
8f86a4e4
JG
1051 /* Try the whole thing again. */
1052whole:
38094c60
JG
1053 if (++retries < MAX_RETRIES)
1054 {
ebdb9ade 1055 SERIAL_WRITE (remote_desc, "-", 1);
38094c60
JG
1056 }
1057 else
1058 {
1059 printf ("Ignoring packet error, continuing...\n");
1060 break;
1061 }
bd5635a1
RP
1062 }
1063
7c622b41
JG
1064out:
1065
ebdb9ade 1066 SERIAL_WRITE (remote_desc, "+", 1);
bd5635a1 1067
66a48870 1068 if (sr_get_debug ())
8f86a4e4 1069 fprintf (stderr,"Packet received: %s\n", buf);
bd5635a1
RP
1070}
1071\f
ebdb9ade
JK
1072static void
1073remote_kill ()
1074{
1075 putpkt ("k");
1076 /* Don't wait for it to die. I'm not really sure it matters whether
1077 we do or not. For the existing stubs, kill is a noop. */
1078 target_mourn_inferior ();
1079}
bd5635a1 1080
ebdb9ade
JK
1081static void
1082remote_mourn ()
1083{
1084 unpush_target (&remote_ops);
1085 generic_mourn_inferior ();
1086}
1087\f
5af4f5f6
JK
1088#ifdef REMOTE_BREAKPOINT
1089
1090/* On some machines, e.g. 68k, we may use a different breakpoint instruction
1091 than other targets. */
1092static unsigned char break_insn[] = REMOTE_BREAKPOINT;
1093
1094/* Check that it fits in BREAKPOINT_MAX bytes. */
1095static unsigned char check_break_insn_size[BREAKPOINT_MAX] = REMOTE_BREAKPOINT;
1096
1097#else /* No REMOTE_BREAKPOINT. */
1098
1099/* Same old breakpoint instruction. This code does nothing different
1100 than mem-break.c. */
1101static unsigned char break_insn[] = BREAKPOINT;
1102
1103#endif /* No REMOTE_BREAKPOINT. */
1104
1105/* Insert a breakpoint on targets that don't have any better breakpoint
1106 support. We read the contents of the target location and stash it,
1107 then overwrite it with a breakpoint instruction. ADDR is the target
1108 location in the target machine. CONTENTS_CACHE is a pointer to
1109 memory allocated for saving the target contents. It is guaranteed
1110 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
1111 is accomplished via BREAKPOINT_MAX). */
1112
d538b510 1113static int
5af4f5f6
JK
1114remote_insert_breakpoint (addr, contents_cache)
1115 CORE_ADDR addr;
1116 char *contents_cache;
1117{
1118 int val;
1119
1120 val = target_read_memory (addr, contents_cache, sizeof break_insn);
1121
1122 if (val == 0)
1123 val = target_write_memory (addr, (char *)break_insn, sizeof break_insn);
1124
1125 return val;
1126}
1127
d538b510 1128static int
5af4f5f6
JK
1129remote_remove_breakpoint (addr, contents_cache)
1130 CORE_ADDR addr;
1131 char *contents_cache;
1132{
1133 return target_write_memory (addr, contents_cache, sizeof break_insn);
1134}
1135\f
bd5635a1
RP
1136/* Define the target subroutine names */
1137
1138struct target_ops remote_ops = {
b543979c
JG
1139 "remote", /* to_shortname */
1140 "Remote serial target in gdb-specific protocol", /* to_longname */
1141 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
1142Specify the serial device it is connected to (e.g. /dev/ttya).", /* to_doc */
1143 remote_open, /* to_open */
1144 remote_close, /* to_close */
1145 NULL, /* to_attach */
1146 remote_detach, /* to_detach */
1147 remote_resume, /* to_resume */
1148 remote_wait, /* to_wait */
1149 remote_fetch_registers, /* to_fetch_registers */
1150 remote_store_registers, /* to_store_registers */
1151 remote_prepare_to_store, /* to_prepare_to_store */
b543979c
JG
1152 remote_xfer_memory, /* to_xfer_memory */
1153 remote_files_info, /* to_files_info */
5af4f5f6
JK
1154
1155 remote_insert_breakpoint, /* to_insert_breakpoint */
1156 remote_remove_breakpoint, /* to_remove_breakpoint */
1157
b543979c
JG
1158 NULL, /* to_terminal_init */
1159 NULL, /* to_terminal_inferior */
1160 NULL, /* to_terminal_ours_for_output */
1161 NULL, /* to_terminal_ours */
1162 NULL, /* to_terminal_info */
ebdb9ade 1163 remote_kill, /* to_kill */
6b27ebe8 1164 generic_load, /* to_load */
b543979c
JG
1165 NULL, /* to_lookup_symbol */
1166 NULL, /* to_create_inferior */
ebdb9ade 1167 remote_mourn, /* to_mourn_inferior */
34517ebc 1168 0, /* to_can_run */
7c622b41 1169 0, /* to_notice_signals */
b543979c
JG
1170 process_stratum, /* to_stratum */
1171 NULL, /* to_next */
1172 1, /* to_has_all_memory */
1173 1, /* to_has_memory */
1174 1, /* to_has_stack */
1175 1, /* to_has_registers */
1176 1, /* to_has_execution */
1177 NULL, /* sections */
1178 NULL, /* sections_end */
1179 OPS_MAGIC /* to_magic */
bd5635a1
RP
1180};
1181
1182void
1183_initialize_remote ()
1184{
1185 add_target (&remote_ops);
1186}
8f86a4e4 1187#endif
This page took 0.180058 seconds and 4 git commands to generate.