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