* exec.c (xfer_memory): Add attrib argument.
[deliverable/binutils-gdb.git] / gdb / remote-sds.c
CommitLineData
c906108c 1/* Remote target communications for serial-line targets using SDS' protocol.
29e57380 2 Copyright 1997, 2001 Free Software Foundation, Inc.
c906108c 3
c5aa993b 4 This file is part of GDB.
c906108c 5
c5aa993b
JM
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
c906108c 10
c5aa993b
JM
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
c906108c 15
c5aa993b
JM
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
c906108c
SS
20
21/* This interface was written by studying the behavior of the SDS
22 monitor on an ADS 821/860 board, and by consulting the
23 documentation of the monitor that is available on Motorola's web
24 site. -sts 8/13/97 */
25
26#include "defs.h"
27#include "gdb_string.h"
28#include <fcntl.h>
29#include "frame.h"
30#include "inferior.h"
31#include "bfd.h"
32#include "symfile.h"
33#include "target.h"
03f2053f 34#include "gdb_wait.h"
c906108c
SS
35#include "gdbcmd.h"
36#include "objfiles.h"
37#include "gdb-stabs.h"
38#include "gdbthread.h"
39#include "gdbcore.h"
c906108c
SS
40
41#ifdef USG
42#include <sys/types.h>
43#endif
44
45#include <signal.h>
46#include "serial.h"
47
a14ed312 48extern void _initialize_remote_sds (void);
c906108c
SS
49
50/* Declarations of local functions. */
51
a14ed312 52static int sds_write_bytes (CORE_ADDR, char *, int);
c906108c 53
a14ed312 54static int sds_read_bytes (CORE_ADDR, char *, int);
c906108c 55
a14ed312 56static void sds_files_info (struct target_ops *ignore);
c906108c 57
29e57380
C
58static int sds_xfer_memory (CORE_ADDR, char *, int, int,
59 struct mem_attrib *, struct target_ops *);
c906108c 60
a14ed312 61static void sds_prepare_to_store (void);
c906108c 62
a14ed312 63static void sds_fetch_registers (int);
c906108c 64
a14ed312 65static void sds_resume (int, int, enum target_signal);
c906108c 66
a14ed312 67static int sds_start_remote (PTR);
c906108c 68
a14ed312 69static void sds_open (char *, int);
c906108c 70
a14ed312 71static void sds_close (int);
c906108c 72
a14ed312 73static void sds_store_registers (int);
c906108c 74
a14ed312 75static void sds_mourn (void);
c906108c 76
a14ed312 77static void sds_create_inferior (char *, char *, char **);
c906108c 78
a14ed312 79static void sds_load (char *, int);
c906108c 80
a14ed312 81static int getmessage (unsigned char *, int);
c906108c 82
a14ed312 83static int putmessage (unsigned char *, int);
c906108c 84
a14ed312 85static int sds_send (unsigned char *, int);
c906108c 86
a14ed312 87static int readchar (int);
c906108c 88
a14ed312 89static int sds_wait (int, struct target_waitstatus *);
c906108c 90
a14ed312 91static void sds_kill (void);
c906108c 92
a14ed312 93static int tohex (int);
c906108c 94
a14ed312 95static int fromhex (int);
c906108c 96
a14ed312 97static void sds_detach (char *, int);
c906108c 98
a14ed312 99static void sds_interrupt (int);
c906108c 100
a14ed312 101static void sds_interrupt_twice (int);
c906108c 102
a14ed312 103static void interrupt_query (void);
c906108c 104
a14ed312 105static int read_frame (char *);
c906108c 106
a14ed312 107static int sds_insert_breakpoint (CORE_ADDR, char *);
c906108c 108
a14ed312 109static int sds_remove_breakpoint (CORE_ADDR, char *);
c906108c 110
a14ed312 111static void init_sds_ops (void);
c906108c 112
a14ed312 113static void sds_command (char *args, int from_tty);
c906108c
SS
114
115/* Define the target operations vector. */
116
117static struct target_ops sds_ops;
118
119/* This was 5 seconds, which is a long time to sit and wait.
120 Unless this is going though some terminal server or multiplexer or
121 other form of hairy serial connection, I would think 2 seconds would
122 be plenty. */
123
124static int sds_timeout = 2;
125
126/* Descriptor for I/O to remote machine. Initialize it to NULL so
127 that sds_open knows that we don't have a file open when the program
128 starts. */
129
130static serial_t sds_desc = NULL;
131
132/* This limit comes from the monitor. */
133
134#define PBUFSIZ 250
135
136/* Maximum number of bytes to read/write at once. The value here
137 is chosen to fill up a packet (the headers account for the 32). */
138#define MAXBUFBYTES ((PBUFSIZ-32)/2)
139
140static int next_msg_id;
141
142static int just_started;
143
144static int message_pending;
c906108c 145\f
c5aa993b 146
c906108c
SS
147/* Clean up connection to a remote debugger. */
148
149/* ARGSUSED */
150static void
fba45db2 151sds_close (int quitting)
c906108c
SS
152{
153 if (sds_desc)
154 SERIAL_CLOSE (sds_desc);
155 sds_desc = NULL;
156}
157
158/* Stub for catch_errors. */
159
160static int
fba45db2 161sds_start_remote (PTR dummy)
c906108c
SS
162{
163 char c;
164 unsigned char buf[200];
165
8edbea78 166 immediate_quit++; /* Allow user to interrupt it */
c906108c
SS
167
168 /* Ack any packet which the remote side has already sent. */
169 SERIAL_WRITE (sds_desc, "{#*\r\n", 5);
170 SERIAL_WRITE (sds_desc, "{#}\r\n", 5);
171
172 while ((c = readchar (1)) >= 0)
173 printf_unfiltered ("%c", c);
174 printf_unfiltered ("\n");
175
176 next_msg_id = 251;
177
178 buf[0] = 26;
179 sds_send (buf, 1);
180
181 buf[0] = 0;
182 sds_send (buf, 1);
183
8edbea78 184 immediate_quit--;
c906108c
SS
185
186 start_remote (); /* Initialize gdb process mechanisms */
187 return 1;
188}
189
190/* Open a connection to a remote debugger.
191 NAME is the filename used for communication. */
192
c906108c 193static void
fba45db2 194sds_open (char *name, int from_tty)
c906108c
SS
195{
196 if (name == 0)
197 error ("To open a remote debug connection, you need to specify what serial\n\
198device is attached to the remote system (e.g. /dev/ttya).");
199
200 target_preopen (from_tty);
201
202 unpush_target (&sds_ops);
203
c906108c
SS
204 sds_desc = SERIAL_OPEN (name);
205 if (!sds_desc)
206 perror_with_name (name);
207
208 if (baud_rate != -1)
209 {
210 if (SERIAL_SETBAUDRATE (sds_desc, baud_rate))
211 {
212 SERIAL_CLOSE (sds_desc);
213 perror_with_name (name);
214 }
215 }
216
217
218 SERIAL_RAW (sds_desc);
219
220 /* If there is something sitting in the buffer we might take it as a
221 response to a command, which would be bad. */
222 SERIAL_FLUSH_INPUT (sds_desc);
223
224 if (from_tty)
225 {
226 puts_filtered ("Remote debugging using ");
227 puts_filtered (name);
228 puts_filtered ("\n");
229 }
230 push_target (&sds_ops); /* Switch to using remote target now */
231
232 just_started = 1;
233
234 /* Start the remote connection; if error (0), discard this target.
235 In particular, if the user quits, be sure to discard it (we'd be
236 in an inconsistent state otherwise). */
c5aa993b 237 if (!catch_errors (sds_start_remote, NULL,
c906108c
SS
238 "Couldn't establish connection to remote target\n",
239 RETURN_MASK_ALL))
240 pop_target ();
241}
242
243/* This takes a program previously attached to and detaches it. After
244 this is done, GDB can be used to debug some other program. We
245 better not have left any breakpoints in the target program or it'll
246 die when it hits one. */
247
248static void
fba45db2 249sds_detach (char *args, int from_tty)
c906108c
SS
250{
251 char buf[PBUFSIZ];
252
253 if (args)
254 error ("Argument given to \"detach\" when remotely debugging.");
255
256#if 0
257 /* Tell the remote target to detach. */
258 strcpy (buf, "D");
259 sds_send (buf, 1);
260#endif
261
262 pop_target ();
263 if (from_tty)
264 puts_filtered ("Ending remote debugging.\n");
265}
266
267/* Convert hex digit A to a number. */
268
269static int
fba45db2 270fromhex (int a)
c906108c
SS
271{
272 if (a >= '0' && a <= '9')
273 return a - '0';
274 else if (a >= 'a' && a <= 'f')
275 return a - 'a' + 10;
c5aa993b 276 else
c906108c
SS
277 error ("Reply contains invalid hex digit %d", a);
278}
279
280/* Convert number NIB to a hex digit. */
281
282static int
fba45db2 283tohex (int nib)
c906108c
SS
284{
285 if (nib < 10)
c5aa993b 286 return '0' + nib;
c906108c 287 else
c5aa993b 288 return 'a' + nib - 10;
c906108c
SS
289}
290
291static int
fba45db2 292tob64 (unsigned char *inbuf, char *outbuf, int len)
c906108c
SS
293{
294 int i, sum;
295 char *p;
296
297 if (len % 3 != 0)
298 error ("bad length");
299
300 p = outbuf;
301 for (i = 0; i < len; i += 3)
302 {
303 /* Collect the next three bytes into a number. */
304 sum = ((long) *inbuf++) << 16;
c5aa993b 305 sum |= ((long) *inbuf++) << 8;
c906108c
SS
306 sum |= ((long) *inbuf++);
307
308 /* Spit out 4 6-bit encodings. */
309 *p++ = ((sum >> 18) & 0x3f) + '0';
310 *p++ = ((sum >> 12) & 0x3f) + '0';
c5aa993b 311 *p++ = ((sum >> 6) & 0x3f) + '0';
c906108c
SS
312 *p++ = (sum & 0x3f) + '0';
313 }
314 return (p - outbuf);
315}
316
317static int
fba45db2 318fromb64 (char *inbuf, char *outbuf, int len)
c906108c
SS
319{
320 int i, sum;
321
322 if (len % 4 != 0)
323 error ("bad length");
324
325 for (i = 0; i < len; i += 4)
326 {
327 /* Collect 4 6-bit digits. */
c5aa993b 328 sum = (*inbuf++ - '0') << 18;
c906108c 329 sum |= (*inbuf++ - '0') << 12;
c5aa993b 330 sum |= (*inbuf++ - '0') << 6;
c906108c
SS
331 sum |= (*inbuf++ - '0');
332
333 /* Now take the resulting 24-bit number and get three bytes out
334 of it. */
335 *outbuf++ = (sum >> 16) & 0xff;
c5aa993b 336 *outbuf++ = (sum >> 8) & 0xff;
c906108c
SS
337 *outbuf++ = sum & 0xff;
338 }
339
340 return (len / 4) * 3;
341}
c906108c 342\f
c5aa993b 343
c906108c
SS
344/* Tell the remote machine to resume. */
345
346static enum target_signal last_sent_signal = TARGET_SIGNAL_0;
347int last_sent_step;
348
349static void
fba45db2 350sds_resume (int pid, int step, enum target_signal siggnal)
c906108c
SS
351{
352 unsigned char buf[PBUFSIZ];
353
c906108c
SS
354 last_sent_signal = siggnal;
355 last_sent_step = step;
356
357 buf[0] = (step ? 21 : 20);
c5aa993b 358 buf[1] = 0; /* (should be signal?) */
c906108c
SS
359
360 sds_send (buf, 2);
361}
362\f
363/* Send a message to target to halt it. Target will respond, and send
364 us a message pending notice. */
365
366static void
fba45db2 367sds_interrupt (int signo)
c906108c
SS
368{
369 unsigned char buf[PBUFSIZ];
370
371 /* If this doesn't work, try more severe steps. */
372 signal (signo, sds_interrupt_twice);
c5aa993b 373
c906108c 374 if (remote_debug)
9846de1b 375 fprintf_unfiltered (gdb_stdlog, "sds_interrupt called\n");
c906108c
SS
376
377 buf[0] = 25;
378 sds_send (buf, 1);
379}
380
c5aa993b 381static void (*ofunc) ();
c906108c
SS
382
383/* The user typed ^C twice. */
384
385static void
fba45db2 386sds_interrupt_twice (int signo)
c906108c
SS
387{
388 signal (signo, ofunc);
c5aa993b 389
c906108c
SS
390 interrupt_query ();
391
392 signal (signo, sds_interrupt);
393}
394
395/* Ask the user what to do when an interrupt is received. */
396
397static void
fba45db2 398interrupt_query (void)
c906108c
SS
399{
400 target_terminal_ours ();
401
402 if (query ("Interrupted while waiting for the program.\n\
403Give up (and stop debugging it)? "))
404 {
405 target_mourn_inferior ();
406 return_to_top_level (RETURN_QUIT);
407 }
408
409 target_terminal_inferior ();
410}
411
412/* If nonzero, ignore the next kill. */
413int kill_kludge;
414
415/* Wait until the remote machine stops, then return, storing status in
416 STATUS just as `wait' would. Returns "pid" (though it's not clear
417 what, if anything, that means in the case of this target). */
418
419static int
fba45db2 420sds_wait (int pid, struct target_waitstatus *status)
c906108c
SS
421{
422 unsigned char buf[PBUFSIZ];
423 int retlen;
424
425 status->kind = TARGET_WAITKIND_EXITED;
426 status->value.integer = 0;
427
428 ofunc = (void (*)()) signal (SIGINT, sds_interrupt);
429
430 signal (SIGINT, ofunc);
431
432 if (just_started)
433 {
434 just_started = 0;
435 status->kind = TARGET_WAITKIND_STOPPED;
436 return inferior_pid;
437 }
438
439 while (1)
440 {
441 getmessage (buf, 1);
442
443 if (message_pending)
444 {
445 buf[0] = 26;
446 retlen = sds_send (buf, 1);
447 if (remote_debug)
448 {
9846de1b 449 fprintf_unfiltered (gdb_stdlog, "Signals: %02x%02x %02x %02x\n",
7a292a7a 450 buf[0], buf[1],
c906108c
SS
451 buf[2], buf[3]);
452 }
453 message_pending = 0;
454 status->kind = TARGET_WAITKIND_STOPPED;
455 status->value.sig = TARGET_SIGNAL_TRAP;
456 goto got_status;
457 }
458 }
c5aa993b 459got_status:
c906108c
SS
460 return inferior_pid;
461}
462
463static unsigned char sprs[16];
464
465/* Read the remote registers into the block REGS. */
466/* Currently we just read all the registers, so we don't use regno. */
467
468/* ARGSUSED */
469static void
fba45db2 470sds_fetch_registers (int regno)
c906108c
SS
471{
472 unsigned char buf[PBUFSIZ];
473 int i, retlen;
c906108c
SS
474 char regs[REGISTER_BYTES];
475
476 /* Unimplemented registers read as all bits zero. */
477 memset (regs, 0, REGISTER_BYTES);
478
479 buf[0] = 18;
480 buf[1] = 1;
481 buf[2] = 0;
482 retlen = sds_send (buf, 3);
483
484 for (i = 0; i < 4 * 6; ++i)
485 regs[i + 4 * 32 + 8 * 32] = buf[i];
486 for (i = 0; i < 4 * 4; ++i)
487 sprs[i] = buf[i + 4 * 7];
488
489 buf[0] = 18;
490 buf[1] = 2;
491 buf[2] = 0;
492 retlen = sds_send (buf, 3);
493
494 for (i = 0; i < retlen; i++)
495 regs[i] = buf[i];
496
497 /* (should warn about reply too short) */
498
499 for (i = 0; i < NUM_REGS; i++)
c5aa993b 500 supply_register (i, &regs[REGISTER_BYTE (i)]);
c906108c
SS
501}
502
503/* Prepare to store registers. Since we may send them all, we have to
504 read out the ones we don't want to change first. */
505
c5aa993b 506static void
fba45db2 507sds_prepare_to_store (void)
c906108c
SS
508{
509 /* Make sure the entire registers array is valid. */
c5aa993b 510 read_register_bytes (0, (char *) NULL, REGISTER_BYTES);
c906108c
SS
511}
512
513/* Store register REGNO, or all registers if REGNO == -1, from the contents
514 of REGISTERS. FIXME: ignores errors. */
515
516static void
fba45db2 517sds_store_registers (int regno)
c906108c
SS
518{
519 unsigned char *p, buf[PBUFSIZ];
520 int i;
521
522 /* Store all the special-purpose registers. */
523 p = buf;
524 *p++ = 19;
525 *p++ = 1;
526 *p++ = 0;
527 *p++ = 0;
528 for (i = 0; i < 4 * 6; i++)
529 *p++ = registers[i + 4 * 32 + 8 * 32];
530 for (i = 0; i < 4 * 1; i++)
531 *p++ = 0;
532 for (i = 0; i < 4 * 4; i++)
533 *p++ = sprs[i];
534
535 sds_send (buf, p - buf);
536
537 /* Store all the general-purpose registers. */
538 p = buf;
539 *p++ = 19;
540 *p++ = 2;
541 *p++ = 0;
542 *p++ = 0;
543 for (i = 0; i < 4 * 32; i++)
544 *p++ = registers[i];
545
546 sds_send (buf, p - buf);
547
548}
549\f
550/* Write memory data directly to the remote machine. This does not
551 inform the data cache; the data cache uses this. MEMADDR is the
552 address in the remote memory space. MYADDR is the address of the
553 buffer in our space. LEN is the number of bytes.
554
555 Returns number of bytes transferred, or 0 for error. */
556
557static int
fba45db2 558sds_write_bytes (CORE_ADDR memaddr, char *myaddr, int len)
c906108c
SS
559{
560 int max_buf_size; /* Max size of packet output buffer */
561 int origlen;
562 unsigned char buf[PBUFSIZ];
563 int todo;
564 int i;
565
566 /* Chop the transfer down if necessary */
567
568 max_buf_size = 150;
569
570 origlen = len;
571 while (len > 0)
572 {
573 todo = min (len, max_buf_size);
574
575 buf[0] = 13;
576 buf[1] = 0;
577 buf[2] = (int) (memaddr >> 24) & 0xff;
578 buf[3] = (int) (memaddr >> 16) & 0xff;
c5aa993b
JM
579 buf[4] = (int) (memaddr >> 8) & 0xff;
580 buf[5] = (int) (memaddr) & 0xff;
c906108c
SS
581 buf[6] = 1;
582 buf[7] = 0;
583
584 for (i = 0; i < todo; i++)
585 buf[i + 8] = myaddr[i];
586
587 sds_send (buf, 8 + todo);
588
589 /* (should look at result) */
590
591 myaddr += todo;
592 memaddr += todo;
593 len -= todo;
594 }
595 return origlen;
596}
597
598/* Read memory data directly from the remote machine. This does not
599 use the data cache; the data cache uses this. MEMADDR is the
600 address in the remote memory space. MYADDR is the address of the
601 buffer in our space. LEN is the number of bytes.
602
603 Returns number of bytes transferred, or 0 for error. */
604
605static int
fba45db2 606sds_read_bytes (CORE_ADDR memaddr, char *myaddr, int len)
c906108c
SS
607{
608 int max_buf_size; /* Max size of packet output buffer */
609 int origlen, retlen;
610 unsigned char buf[PBUFSIZ];
611 int todo;
612 int i;
613
614 /* Chop the transfer down if necessary */
615
616 max_buf_size = 150;
617
618 origlen = len;
619 while (len > 0)
620 {
621 todo = min (len, max_buf_size);
622
623 buf[0] = 12;
624 buf[1] = 0;
625 buf[2] = (int) (memaddr >> 24) & 0xff;
626 buf[3] = (int) (memaddr >> 16) & 0xff;
c5aa993b
JM
627 buf[4] = (int) (memaddr >> 8) & 0xff;
628 buf[5] = (int) (memaddr) & 0xff;
c906108c 629 buf[6] = (int) (todo >> 8) & 0xff;
c5aa993b 630 buf[7] = (int) (todo) & 0xff;
c906108c
SS
631 buf[8] = 1;
632
633 retlen = sds_send (buf, 9);
634
635 if (retlen - 2 != todo)
636 {
637 return 0;
638 }
639
640 /* Reply describes memory byte by byte. */
641
642 for (i = 0; i < todo; i++)
643 myaddr[i] = buf[i + 2];
644
645 myaddr += todo;
646 memaddr += todo;
647 len -= todo;
648 }
649
650 return origlen;
651}
652\f
653/* Read or write LEN bytes from inferior memory at MEMADDR,
654 transferring to or from debugger address MYADDR. Write to inferior
655 if SHOULD_WRITE is nonzero. Returns length of data written or
d93bce06 656 read; 0 for error. TARGET is unused. */
c906108c
SS
657
658/* ARGSUSED */
659static int
d93bce06 660sds_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int should_write,
29e57380
C
661 struct mem_attrib *attrib ATTRIBUTE_UNUSED,
662 struct target_ops *target ATTRIBUTE_UNUSED)
c906108c 663{
4930751a
C
664 int res;
665
666 if (should_write)
667 res = sds_write_bytes (memaddr, myaddr, len);
668 else
669 res = sds_read_bytes (memaddr, myaddr, len);
670
671 return res;
c906108c 672}
c906108c 673\f
c5aa993b 674
c906108c 675static void
fba45db2 676sds_files_info (struct target_ops *ignore)
c906108c
SS
677{
678 puts_filtered ("Debugging over a serial connection, using SDS protocol.\n");
679}
680\f
681/* Stuff for dealing with the packets which are part of this protocol.
682 See comment at top of file for details. */
683
684/* Read a single character from the remote end, masking it down to 7 bits. */
685
686static int
fba45db2 687readchar (int timeout)
c906108c
SS
688{
689 int ch;
690
691 ch = SERIAL_READCHAR (sds_desc, timeout);
692
693 if (remote_debug > 1 && ch >= 0)
9846de1b 694 fprintf_unfiltered (gdb_stdlog, "%c(%x)", ch, ch);
c906108c
SS
695
696 switch (ch)
697 {
698 case SERIAL_EOF:
699 error ("Remote connection closed");
700 case SERIAL_ERROR:
701 perror_with_name ("Remote communication error");
702 case SERIAL_TIMEOUT:
703 return ch;
704 default:
705 return ch & 0x7f;
706 }
707}
708
709/* An SDS-style checksum is a sum of the bytes modulo 253. (Presumably
710 because 253, 254, and 255 are special flags in the protocol.) */
711
712static int
fba45db2 713compute_checksum (int csum, char *buf, int len)
c906108c
SS
714{
715 int i;
716
717 for (i = 0; i < len; ++i)
718 csum += (unsigned char) buf[i];
719
720 csum %= 253;
721 return csum;
722}
723
724/* Send the command in BUF to the remote machine, and read the reply
725 into BUF also. */
726
727static int
fba45db2 728sds_send (unsigned char *buf, int len)
c906108c
SS
729{
730 putmessage (buf, len);
731
732 return getmessage (buf, 0);
733}
734
735/* Send a message to the remote machine. */
736
737static int
fba45db2 738putmessage (unsigned char *buf, int len)
c906108c
SS
739{
740 int i, enclen;
741 unsigned char csum = 0;
742 char buf2[PBUFSIZ], buf3[PBUFSIZ];
743 unsigned char header[3];
c906108c
SS
744 char *p;
745
746 /* Copy the packet into buffer BUF2, encapsulating it
747 and giving it a checksum. */
748
749 if (len > 170) /* Prosanity check */
c5aa993b 750 abort ();
c906108c
SS
751
752 if (remote_debug)
753 {
9846de1b 754 fprintf_unfiltered (gdb_stdlog, "Message to send: \"");
c906108c 755 for (i = 0; i < len; ++i)
9846de1b
JM
756 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
757 fprintf_unfiltered (gdb_stdlog, "\"\n");
c906108c
SS
758 }
759
760 p = buf2;
761 *p++ = '$';
762
763 if (len % 3 != 0)
764 {
765 buf[len] = '\0';
c5aa993b 766 buf[len + 1] = '\0';
c906108c
SS
767 }
768
769 header[1] = next_msg_id;
770
771 header[2] = len;
772
773 csum = compute_checksum (csum, buf, len);
774 csum = compute_checksum (csum, header + 1, 2);
775
776 header[0] = csum;
777
778 tob64 (header, p, 3);
779 p += 4;
780 enclen = tob64 (buf, buf3, ((len + 2) / 3) * 3);
781
782 for (i = 0; i < enclen; ++i)
783 *p++ = buf3[i];
784 *p++ = '\r';
785 *p++ = '\n';
786
787 next_msg_id = (next_msg_id + 3) % 245;
788
789 /* Send it over and over until we get a positive ack. */
790
791 while (1)
792 {
c906108c
SS
793 if (remote_debug)
794 {
795 *p = '\0';
9846de1b
JM
796 fprintf_unfiltered (gdb_stdlog, "Sending encoded: \"%s\"", buf2);
797 fprintf_unfiltered (gdb_stdlog,
798 " (Checksum %d, id %d, length %d)\n",
799 header[0], header[1], header[2]);
800 gdb_flush (gdb_stdlog);
c906108c
SS
801 }
802 if (SERIAL_WRITE (sds_desc, buf2, p - buf2))
803 perror_with_name ("putmessage: write failed");
804
805 return 1;
c906108c 806 }
c906108c
SS
807}
808
809/* Come here after finding the start of the frame. Collect the rest
810 into BUF. Returns 0 on any error, 1 on success. */
811
812static int
fba45db2 813read_frame (char *buf)
c906108c
SS
814{
815 char *bp;
816 int c;
817
818 bp = buf;
819
820 while (1)
821 {
822 c = readchar (sds_timeout);
823
824 switch (c)
825 {
826 case SERIAL_TIMEOUT:
827 if (remote_debug)
9846de1b 828 fputs_filtered ("Timeout in mid-message, retrying\n", gdb_stdlog);
c906108c
SS
829 return 0;
830 case '$':
831 if (remote_debug)
9846de1b
JM
832 fputs_filtered ("Saw new packet start in middle of old one\n",
833 gdb_stdlog);
c906108c
SS
834 return 0; /* Start a new packet, count retries */
835 case '\r':
836 break;
837
838 case '\n':
839 {
840 *bp = '\000';
841 if (remote_debug)
9846de1b 842 fprintf_unfiltered (gdb_stdlog, "Received encoded: \"%s\"\n",
c906108c
SS
843 buf);
844 return 1;
845 }
846
847 default:
848 if (bp < buf + PBUFSIZ - 1)
849 {
850 *bp++ = c;
851 continue;
852 }
853
854 *bp = '\0';
855 puts_filtered ("Message too long: ");
856 puts_filtered (buf);
857 puts_filtered ("\n");
858
859 return 0;
860 }
861 }
862}
863
864/* Read a packet from the remote machine, with error checking,
865 and store it in BUF. BUF is expected to be of size PBUFSIZ.
866 If FOREVER, wait forever rather than timing out; this is used
867 while the target is executing user code. */
868
869static int
fba45db2 870getmessage (unsigned char *buf, int forever)
c906108c
SS
871{
872 int c, c2, c3;
873 int tries;
874 int timeout;
875 int val, i, len, csum;
876 unsigned char header[3];
877 unsigned char inbuf[500];
878
879 strcpy (buf, "timeout");
880
881 if (forever)
882 {
c906108c 883 timeout = watchdog > 0 ? watchdog : -1;
c906108c
SS
884 }
885
886 else
887 timeout = sds_timeout;
888
889#define MAX_TRIES 3
890
891 for (tries = 1; tries <= MAX_TRIES; tries++)
892 {
893 /* This can loop forever if the remote side sends us characters
c5aa993b
JM
894 continuously, but if it pauses, we'll get a zero from readchar
895 because of timeout. Then we'll count that as a retry. */
c906108c
SS
896
897 /* Note that we will only wait forever prior to the start of a packet.
c5aa993b
JM
898 After that, we expect characters to arrive at a brisk pace. They
899 should show up within sds_timeout intervals. */
c906108c
SS
900
901 do
902 {
903 c = readchar (timeout);
904
905 if (c == SERIAL_TIMEOUT)
906 {
c906108c
SS
907 if (forever) /* Watchdog went off. Kill the target. */
908 {
909 target_mourn_inferior ();
910 error ("Watchdog has expired. Target detached.\n");
911 }
c906108c 912 if (remote_debug)
9846de1b 913 fputs_filtered ("Timed out.\n", gdb_stdlog);
c906108c
SS
914 goto retry;
915 }
916 }
917 while (c != '$' && c != '{');
918
919 /* We might have seen a "trigraph", a sequence of three characters
c5aa993b 920 that indicate various sorts of communication state. */
c906108c
SS
921
922 if (c == '{')
923 {
924 /* Read the other two chars of the trigraph. */
925 c2 = readchar (timeout);
926 c3 = readchar (timeout);
927 if (remote_debug)
9846de1b 928 fprintf_unfiltered (gdb_stdlog, "Trigraph %c%c%c received\n",
c906108c
SS
929 c, c2, c3);
930 if (c3 == '+')
931 {
932 message_pending = 1;
c5aa993b 933 return 0; /*???? */
c906108c
SS
934 }
935 continue;
936 }
937
938 val = read_frame (inbuf);
939
940 if (val == 1)
941 {
942 fromb64 (inbuf, header, 4);
943 /* (should check out other bits) */
944 fromb64 (inbuf + 4, buf, strlen (inbuf) - 4);
945
946 len = header[2];
947
948 csum = 0;
949 csum = compute_checksum (csum, buf, len);
950 csum = compute_checksum (csum, header + 1, 2);
951
952 if (csum != header[0])
953 fprintf_unfiltered (gdb_stderr,
c5aa993b 954 "Checksum mismatch: computed %d, received %d\n",
c906108c
SS
955 csum, header[0]);
956
957 if (header[2] == 0xff)
958 fprintf_unfiltered (gdb_stderr, "Requesting resend...\n");
959
960 if (remote_debug)
961 {
9846de1b 962 fprintf_unfiltered (gdb_stdlog,
c5aa993b 963 "... (Got checksum %d, id %d, length %d)\n",
c906108c 964 header[0], header[1], header[2]);
9846de1b 965 fprintf_unfiltered (gdb_stdlog, "Message received: \"");
c906108c
SS
966 for (i = 0; i < len; ++i)
967 {
9846de1b 968 fprintf_unfiltered (gdb_stdlog, "%02x", (unsigned char) buf[i]);
c906108c 969 }
9846de1b 970 fprintf_unfiltered (gdb_stdlog, "\"\n");
c906108c
SS
971 }
972
973 /* no ack required? */
974 return len;
975 }
976
977 /* Try the whole thing again. */
978 retry:
979 /* need to do something here */
980 }
981
982 /* We have tried hard enough, and just can't receive the packet. Give up. */
983
984 printf_unfiltered ("Ignoring packet error, continuing...\n");
985 return 0;
986}
987\f
988static void
fba45db2 989sds_kill (void)
c906108c
SS
990{
991 /* Don't try to do anything to the target. */
992}
993
994static void
fba45db2 995sds_mourn (void)
c906108c
SS
996{
997 unpush_target (&sds_ops);
998 generic_mourn_inferior ();
999}
1000
1001static void
fba45db2 1002sds_create_inferior (char *exec_file, char *args, char **env)
c906108c
SS
1003{
1004 inferior_pid = 42000;
1005
1006 /* Clean up from the last time we were running. */
1007 clear_proceed_status ();
1008
1009 /* Let the remote process run. */
1010 proceed (bfd_get_start_address (exec_bfd), TARGET_SIGNAL_0, 0);
1011}
1012
1013static void
fba45db2 1014sds_load (char *filename, int from_tty)
c906108c
SS
1015{
1016 generic_load (filename, from_tty);
1017
1018 inferior_pid = 0;
1019}
1020\f
1021/* The SDS monitor has commands for breakpoint insertion, although it
1022 it doesn't actually manage the breakpoints, it just returns the
1023 replaced instruction back to the debugger. */
1024
1025static int
fba45db2 1026sds_insert_breakpoint (CORE_ADDR addr, char *contents_cache)
c906108c
SS
1027{
1028 int i, retlen;
1029 unsigned char *p, buf[PBUFSIZ];
1030
1031 p = buf;
1032 *p++ = 16;
1033 *p++ = 0;
1034 *p++ = (int) (addr >> 24) & 0xff;
1035 *p++ = (int) (addr >> 16) & 0xff;
c5aa993b
JM
1036 *p++ = (int) (addr >> 8) & 0xff;
1037 *p++ = (int) (addr) & 0xff;
1038
c906108c
SS
1039 retlen = sds_send (buf, p - buf);
1040
1041 for (i = 0; i < 4; ++i)
1042 contents_cache[i] = buf[i + 2];
1043
1044 return 0;
1045}
1046
1047static int
fba45db2 1048sds_remove_breakpoint (CORE_ADDR addr, char *contents_cache)
c906108c
SS
1049{
1050 int i, retlen;
1051 unsigned char *p, buf[PBUFSIZ];
1052
1053 p = buf;
1054 *p++ = 17;
1055 *p++ = 0;
1056 *p++ = (int) (addr >> 24) & 0xff;
1057 *p++ = (int) (addr >> 16) & 0xff;
c5aa993b
JM
1058 *p++ = (int) (addr >> 8) & 0xff;
1059 *p++ = (int) (addr) & 0xff;
c906108c
SS
1060 for (i = 0; i < 4; ++i)
1061 *p++ = contents_cache[i];
1062
1063 retlen = sds_send (buf, p - buf);
1064
1065 return 0;
1066}
1067\f
c5aa993b 1068static void
fba45db2 1069init_sds_ops (void)
c906108c
SS
1070{
1071 sds_ops.to_shortname = "sds";
1072 sds_ops.to_longname = "Remote serial target with SDS protocol";
1073 sds_ops.to_doc = "Use a remote computer via a serial line; using the SDS protocol.\n\
1074Specify the serial device it is connected to (e.g. /dev/ttya).";
1075 sds_ops.to_open = sds_open;
1076 sds_ops.to_close = sds_close;
1077 sds_ops.to_detach = sds_detach;
1078 sds_ops.to_resume = sds_resume;
1079 sds_ops.to_wait = sds_wait;
1080 sds_ops.to_fetch_registers = sds_fetch_registers;
1081 sds_ops.to_store_registers = sds_store_registers;
1082 sds_ops.to_prepare_to_store = sds_prepare_to_store;
1083 sds_ops.to_xfer_memory = sds_xfer_memory;
1084 sds_ops.to_files_info = sds_files_info;
1085 sds_ops.to_insert_breakpoint = sds_insert_breakpoint;
1086 sds_ops.to_remove_breakpoint = sds_remove_breakpoint;
1087 sds_ops.to_kill = sds_kill;
1088 sds_ops.to_load = sds_load;
1089 sds_ops.to_create_inferior = sds_create_inferior;
1090 sds_ops.to_mourn_inferior = sds_mourn;
1091 sds_ops.to_stratum = process_stratum;
1092 sds_ops.to_has_all_memory = 1;
1093 sds_ops.to_has_memory = 1;
1094 sds_ops.to_has_stack = 1;
1095 sds_ops.to_has_registers = 1;
1096 sds_ops.to_has_execution = 1;
1097 sds_ops.to_magic = OPS_MAGIC;
1098}
1099
1100/* Put a command string, in args, out to the monitor and display the
1101 reply message. */
1102
1103static void
fba45db2 1104sds_command (char *args, int from_tty)
c906108c
SS
1105{
1106 char *p;
1107 int i, len, retlen;
1108 unsigned char buf[1000];
1109
1110 /* Convert hexadecimal chars into a byte buffer. */
1111 p = args;
1112 len = 0;
1113 while (*p != '\0')
1114 {
1115 buf[len++] = fromhex (p[0]) * 16 + fromhex (p[1]);
1116 if (p[1] == '\0')
1117 break;
1118 p += 2;
1119 }
1120
1121 retlen = sds_send (buf, len);
1122
1123 printf_filtered ("Reply is ");
1124 for (i = 0; i < retlen; ++i)
1125 {
1126 printf_filtered ("%02x", buf[i]);
c5aa993b 1127 }
c906108c
SS
1128 printf_filtered ("\n");
1129}
1130
1131void
fba45db2 1132_initialize_remote_sds (void)
c906108c
SS
1133{
1134 init_sds_ops ();
1135 add_target (&sds_ops);
1136
1137 add_show_from_set (add_set_cmd ("sdstimeout", no_class,
c5aa993b
JM
1138 var_integer, (char *) &sds_timeout,
1139 "Set timeout value for sds read.\n", &setlist),
c906108c
SS
1140 &showlist);
1141
1142 add_com ("sds", class_obscure, sds_command,
c5aa993b 1143 "Send a command to the SDS monitor.");
c906108c 1144}
This page took 0.140774 seconds and 4 git commands to generate.