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

Subversion Repositories or1k

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

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

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

powered by: WebSVN 2.1.0

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