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