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