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