OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [gdb-5.0/] [gdb/] [remote-sds.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.