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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [gdb-5.0/] [gdb/] [gdbserver/] [remote-utils.c] - Blame information for rev 1775

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 106 markom
/* Remote utility routines for the remote server for GDB.
2
   Copyright (C) 1986, 1989, 1993 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
#include "server.h"
22
#include "terminal.h"
23
#include <stdio.h>
24
#include <string.h>
25
#include <sys/ioctl.h>
26
#include <sys/file.h>
27
#include <netinet/in.h>
28
#include <sys/socket.h>
29
#include <netdb.h>
30
#include <netinet/tcp.h>
31
#include <sys/ioctl.h>
32
#include <signal.h>
33
#include <fcntl.h>
34
 
35
int remote_debug = 0;
36
 
37
static int remote_desc;
38
 
39
/* Open a connection to a remote debugger.
40
   NAME is the filename used for communication.  */
41
 
42
void
43
remote_open (name)
44
     char *name;
45
{
46
  int save_fcntl_flags;
47
 
48
  if (!strchr (name, ':'))
49
    {
50
      remote_desc = open (name, O_RDWR);
51
      if (remote_desc < 0)
52
        perror_with_name ("Could not open remote device");
53
 
54
#ifdef HAVE_TERMIOS
55
      {
56
        struct termios termios;
57
        tcgetattr (remote_desc, &termios);
58
 
59
        termios.c_iflag = 0;
60
        termios.c_oflag = 0;
61
        termios.c_lflag = 0;
62
        termios.c_cflag &= ~(CSIZE | PARENB);
63
        termios.c_cflag |= CLOCAL | CS8;
64
        termios.c_cc[VMIN] = 0;
65
        termios.c_cc[VTIME] = 0;
66
 
67
        tcsetattr (remote_desc, TCSANOW, &termios);
68
      }
69
#endif
70
 
71
#ifdef HAVE_TERMIO
72
      {
73
        struct termio termio;
74
        ioctl (remote_desc, TCGETA, &termio);
75
 
76
        termio.c_iflag = 0;
77
        termio.c_oflag = 0;
78
        termio.c_lflag = 0;
79
        termio.c_cflag &= ~(CSIZE | PARENB);
80
        termio.c_cflag |= CLOCAL | CS8;
81
        termio.c_cc[VMIN] = 0;
82
        termio.c_cc[VTIME] = 0;
83
 
84
        ioctl (remote_desc, TCSETA, &termio);
85
      }
86
#endif
87
 
88
#ifdef HAVE_SGTTY
89
      {
90
        struct sgttyb sg;
91
 
92
        ioctl (remote_desc, TIOCGETP, &sg);
93
        sg.sg_flags = RAW;
94
        ioctl (remote_desc, TIOCSETP, &sg);
95
      }
96
#endif
97
 
98
 
99
    }
100
  else
101
    {
102
      char *port_str;
103
      int port;
104
      struct sockaddr_in sockaddr;
105
      int tmp;
106
      struct protoent *protoent;
107
      int tmp_desc;
108
 
109
      port_str = strchr (name, ':');
110
 
111
      port = atoi (port_str + 1);
112
 
113
      tmp_desc = socket (PF_INET, SOCK_STREAM, 0);
114
      if (tmp_desc < 0)
115
        perror_with_name ("Can't open socket");
116
 
117
      /* Allow rapid reuse of this port. */
118
      tmp = 1;
119
      setsockopt (tmp_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp,
120
                  sizeof (tmp));
121
 
122
      sockaddr.sin_family = PF_INET;
123
      sockaddr.sin_port = htons (port);
124
      sockaddr.sin_addr.s_addr = INADDR_ANY;
125
 
126
      if (bind (tmp_desc, (struct sockaddr *) &sockaddr, sizeof (sockaddr))
127
          || listen (tmp_desc, 1))
128
        perror_with_name ("Can't bind address");
129
 
130
      tmp = sizeof (sockaddr);
131
      remote_desc = accept (tmp_desc, (struct sockaddr *) &sockaddr, &tmp);
132
      if (remote_desc == -1)
133
        perror_with_name ("Accept failed");
134
 
135
      protoent = getprotobyname ("tcp");
136
      if (!protoent)
137
        perror_with_name ("getprotobyname");
138
 
139
      /* Enable TCP keep alive process. */
140
      tmp = 1;
141
      setsockopt (tmp_desc, SOL_SOCKET, SO_KEEPALIVE, (char *) &tmp, sizeof (tmp));
142
 
143
      /* Tell TCP not to delay small packets.  This greatly speeds up
144
         interactive response. */
145
      tmp = 1;
146
      setsockopt (remote_desc, protoent->p_proto, TCP_NODELAY,
147
                  (char *) &tmp, sizeof (tmp));
148
 
149
      close (tmp_desc);         /* No longer need this */
150
 
151
      signal (SIGPIPE, SIG_IGN);        /* If we don't do this, then gdbserver simply
152
                                           exits when the remote side dies.  */
153
    }
154
 
155
#if defined(F_SETFL) && defined (FASYNC)
156
  save_fcntl_flags = fcntl (remote_desc, F_GETFL, 0);
157
  fcntl (remote_desc, F_SETFL, save_fcntl_flags | FASYNC);
158
  disable_async_io ();
159
#endif /* FASYNC */
160
  fprintf (stderr, "Remote debugging using %s\n", name);
161
}
162
 
163
void
164
remote_close ()
165
{
166
  close (remote_desc);
167
}
168
 
169
/* Convert hex digit A to a number.  */
170
 
171
static int
172
fromhex (a)
173
     int a;
174
{
175
  if (a >= '0' && a <= '9')
176
    return a - '0';
177
  else if (a >= 'a' && a <= 'f')
178
    return a - 'a' + 10;
179
  else
180
    error ("Reply contains invalid hex digit");
181
}
182
 
183
/* Convert number NIB to a hex digit.  */
184
 
185
static int
186
tohex (nib)
187
     int nib;
188
{
189
  if (nib < 10)
190
    return '0' + nib;
191
  else
192
    return 'a' + nib - 10;
193
}
194
 
195
/* Send a packet to the remote machine, with error checking.
196
   The data of the packet is in BUF.  Returns >= 0 on success, -1 otherwise. */
197
 
198
int
199
putpkt (buf)
200
     char *buf;
201
{
202
  int i;
203
  unsigned char csum = 0;
204
  char buf2[PBUFSIZ];
205
  char buf3[1];
206
  int cnt = strlen (buf);
207
  char *p;
208
 
209
  /* Copy the packet into buffer BUF2, encapsulating it
210
     and giving it a checksum.  */
211
 
212
  p = buf2;
213
  *p++ = '$';
214
 
215
  for (i = 0; i < cnt; i++)
216
    {
217
      csum += buf[i];
218
      *p++ = buf[i];
219
    }
220
  *p++ = '#';
221
  *p++ = tohex ((csum >> 4) & 0xf);
222
  *p++ = tohex (csum & 0xf);
223
 
224
  *p = '\0';
225
 
226
  /* Send it over and over until we get a positive ack.  */
227
 
228
  do
229
    {
230
      int cc;
231
 
232
      if (write (remote_desc, buf2, p - buf2) != p - buf2)
233
        {
234
          perror ("putpkt(write)");
235
          return -1;
236
        }
237
 
238
      if (remote_debug)
239
        printf ("putpkt (\"%s\"); [looking for ack]\n", buf2);
240
      cc = read (remote_desc, buf3, 1);
241
      if (remote_debug)
242
        printf ("[received '%c' (0x%x)]\n", buf3[0], buf3[0]);
243
      if (cc <= 0)
244
        {
245
          if (cc == 0)
246
            fprintf (stderr, "putpkt(read): Got EOF\n");
247
          else
248
            perror ("putpkt(read)");
249
 
250
          return -1;
251
        }
252
    }
253
  while (buf3[0] != '+');
254
 
255
  return 1;                     /* Success! */
256
}
257
 
258
/* Come here when we get an input interrupt from the remote side.  This
259
   interrupt should only be active while we are waiting for the child to do
260
   something.  About the only thing that should come through is a ^C, which
261
   will cause us to send a SIGINT to the child.  */
262
 
263
static void
264
input_interrupt ()
265
{
266
  int cc;
267
  char c;
268
 
269
  cc = read (remote_desc, &c, 1);
270
 
271
  if (cc != 1 || c != '\003')
272
    {
273
      fprintf (stderr, "input_interrupt, cc = %d c = %d\n", cc, c);
274
      return;
275
    }
276
 
277
  kill (inferior_pid, SIGINT);
278
}
279
 
280
void
281
enable_async_io ()
282
{
283
  signal (SIGIO, input_interrupt);
284
}
285
 
286
void
287
disable_async_io ()
288
{
289
  signal (SIGIO, SIG_IGN);
290
}
291
 
292
/* Returns next char from remote GDB.  -1 if error.  */
293
 
294
static int
295
readchar ()
296
{
297
  static char buf[BUFSIZ];
298
  static int bufcnt = 0;
299
  static char *bufp;
300
 
301
  if (bufcnt-- > 0)
302
    return *bufp++ & 0x7f;
303
 
304
  bufcnt = read (remote_desc, buf, sizeof (buf));
305
 
306
  if (bufcnt <= 0)
307
    {
308
      if (bufcnt == 0)
309
        fprintf (stderr, "readchar: Got EOF\n");
310
      else
311
        perror ("readchar");
312
 
313
      return -1;
314
    }
315
 
316
  bufp = buf;
317
  bufcnt--;
318
  return *bufp++ & 0x7f;
319
}
320
 
321
/* Read a packet from the remote machine, with error checking,
322
   and store it in BUF.  Returns length of packet, or negative if error. */
323
 
324
int
325
getpkt (buf)
326
     char *buf;
327
{
328
  char *bp;
329
  unsigned char csum, c1, c2;
330
  int c;
331
 
332
  while (1)
333
    {
334
      csum = 0;
335
 
336
      while (1)
337
        {
338
          c = readchar ();
339
          if (c == '$')
340
            break;
341
          if (remote_debug)
342
            printf ("[getpkt: discarding char '%c']\n", c);
343
          if (c < 0)
344
            return -1;
345
        }
346
 
347
      bp = buf;
348
      while (1)
349
        {
350
          c = readchar ();
351
          if (c < 0)
352
            return -1;
353
          if (c == '#')
354
            break;
355
          *bp++ = c;
356
          csum += c;
357
        }
358
      *bp = 0;
359
 
360
      c1 = fromhex (readchar ());
361
      c2 = fromhex (readchar ());
362
 
363
      if (csum == (c1 << 4) + c2)
364
        break;
365
 
366
      fprintf (stderr, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
367
               (c1 << 4) + c2, csum, buf);
368
      write (remote_desc, "-", 1);
369
    }
370
 
371
  if (remote_debug)
372
    printf ("getpkt (\"%s\");  [sending ack] \n", buf);
373
 
374
  write (remote_desc, "+", 1);
375
 
376
  if (remote_debug)
377
    printf ("[sent ack]\n");
378
  return bp - buf;
379
}
380
 
381
void
382
write_ok (buf)
383
     char *buf;
384
{
385
  buf[0] = 'O';
386
  buf[1] = 'K';
387
  buf[2] = '\0';
388
}
389
 
390
void
391
write_enn (buf)
392
     char *buf;
393
{
394
  buf[0] = 'E';
395
  buf[1] = 'N';
396
  buf[2] = 'N';
397
  buf[3] = '\0';
398
}
399
 
400
void
401
convert_int_to_ascii (from, to, n)
402
     char *from, *to;
403
     int n;
404
{
405
  int nib;
406
  char ch;
407
  while (n--)
408
    {
409
      ch = *from++;
410
      nib = ((ch & 0xf0) >> 4) & 0x0f;
411
      *to++ = tohex (nib);
412
      nib = ch & 0x0f;
413
      *to++ = tohex (nib);
414
    }
415
  *to++ = 0;
416
}
417
 
418
 
419
void
420
convert_ascii_to_int (from, to, n)
421
     char *from, *to;
422
     int n;
423
{
424
  int nib1, nib2;
425
  while (n--)
426
    {
427
      nib1 = fromhex (*from++);
428
      nib2 = fromhex (*from++);
429
      *to++ = (((nib1 & 0x0f) << 4) & 0xf0) | (nib2 & 0x0f);
430
    }
431
}
432
 
433
static char *
434
outreg (regno, buf)
435
     int regno;
436
     char *buf;
437
{
438
  int regsize = REGISTER_RAW_SIZE (regno);
439
 
440
  if ((regno >> 12) != 0)
441
    *buf++ = tohex ((regno >> 12) & 0xf);
442
  if ((regno >> 8) != 0)
443
    *buf++ = tohex ((regno >> 8) & 0xf);
444
  *buf++ = tohex ((regno >> 4) & 0xf);
445
  *buf++ = tohex (regno & 0xf);
446
  *buf++ = ':';
447
  convert_int_to_ascii (&registers[REGISTER_BYTE (regno)], buf, regsize);
448
  buf += 2 * regsize;
449
  *buf++ = ';';
450
 
451
  return buf;
452
}
453
 
454
void
455
prepare_resume_reply (buf, status, signo)
456
     char *buf;
457
     char status;
458
     unsigned char signo;
459
{
460
  int nib;
461
 
462
  *buf++ = status;
463
 
464
  /* FIXME!  Should be converting this signal number (numbered
465
     according to the signal numbering of the system we are running on)
466
     to the signal numbers used by the gdb protocol (see enum target_signal
467
     in gdb/target.h).  */
468
  nib = ((signo & 0xf0) >> 4);
469
  *buf++ = tohex (nib);
470
  nib = signo & 0x0f;
471
  *buf++ = tohex (nib);
472
 
473
  if (status == 'T')
474
    {
475
#ifdef GDBSERVER_RESUME_REGS
476
      static int gdbserver_resume_regs[] = GDBSERVER_RESUME_REGS ;
477
      int i;
478
      for (i = 0;
479
           i < sizeof (gdbserver_resume_regs)
480
                / sizeof (gdbserver_resume_regs[0]);
481
           i++)
482
        {
483
          int regnum = gdbserver_resume_regs[i];
484
          buf = outreg (regnum, buf);
485
        }
486
#else /* !defined(GDBSERVER_RESUME_REGS) */
487
      buf = outreg (PC_REGNUM, buf);
488
      buf = outreg (FP_REGNUM, buf);
489
      buf = outreg (SP_REGNUM, buf);
490
#ifdef NPC_REGNUM
491
      buf = outreg (NPC_REGNUM, buf);
492
#endif
493
#ifdef O7_REGNUM
494
      buf = outreg (O7_REGNUM, buf);
495
#endif
496
#endif /* GDBSERVER_RESUME_REGS */
497
 
498
      /* If the debugger hasn't used any thread features, don't burden it with
499
         threads.  If we didn't check this, GDB 4.13 and older would choke.  */
500
      if (cont_thread != 0)
501
        {
502
          if (old_thread_from_wait != thread_from_wait)
503
            {
504
              sprintf (buf, "thread:%x;", thread_from_wait);
505
              buf += strlen (buf);
506
              old_thread_from_wait = thread_from_wait;
507
            }
508
        }
509
    }
510
  /* For W and X, we're done.  */
511
  *buf++ = 0;
512
}
513
 
514
void
515
decode_m_packet (from, mem_addr_ptr, len_ptr)
516
     char *from;
517
     CORE_ADDR *mem_addr_ptr;
518
     unsigned int *len_ptr;
519
{
520
  int i = 0, j = 0;
521
  char ch;
522
  *mem_addr_ptr = *len_ptr = 0;
523
 
524
  while ((ch = from[i++]) != ',')
525
    {
526
      *mem_addr_ptr = *mem_addr_ptr << 4;
527
      *mem_addr_ptr |= fromhex (ch) & 0x0f;
528
    }
529
 
530
  for (j = 0; j < 4; j++)
531
    {
532
      if ((ch = from[i++]) == 0)
533
        break;
534
      *len_ptr = *len_ptr << 4;
535
      *len_ptr |= fromhex (ch) & 0x0f;
536
    }
537
}
538
 
539
void
540
decode_M_packet (from, mem_addr_ptr, len_ptr, to)
541
     char *from, *to;
542
     CORE_ADDR *mem_addr_ptr;
543
     unsigned int *len_ptr;
544
{
545
  int i = 0;
546
  char ch;
547
  *mem_addr_ptr = *len_ptr = 0;
548
 
549
  while ((ch = from[i++]) != ',')
550
    {
551
      *mem_addr_ptr = *mem_addr_ptr << 4;
552
      *mem_addr_ptr |= fromhex (ch) & 0x0f;
553
    }
554
 
555
  while ((ch = from[i++]) != ':')
556
    {
557
      *len_ptr = *len_ptr << 4;
558
      *len_ptr |= fromhex (ch) & 0x0f;
559
    }
560
 
561
  convert_ascii_to_int (&from[i++], to, *len_ptr);
562
}

powered by: WebSVN 2.1.0

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