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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [gdb/] [gdbserver/] [gdbreplay.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 578 markom
/* Replay a remote debug session logfile for GDB.
2
   Copyright 1996, 1998, 1999, 2000 Free Software Foundation, Inc.
3
   Written by Fred Fish (fnf@cygnus.com) from pieces of gdbserver.
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 "config.h"
23
#include <stdio.h>
24
#include <sys/file.h>
25
#include <netinet/in.h>
26
#include <sys/socket.h>
27
#include <netdb.h>
28
#include <netinet/tcp.h>
29
#include <signal.h>
30
#include <ctype.h>
31
#include <fcntl.h>
32
#include <errno.h>
33
 
34
/* Sort of a hack... */
35
#define EOL (EOF - 1)
36
 
37
static int remote_desc;
38
 
39
/* Print the system error message for errno, and also mention STRING
40
   as the file name for which the error was encountered.
41
   Then return to command level.  */
42
 
43
void
44
perror_with_name (char *string)
45
{
46
#ifndef STDC_HEADERS
47
  extern int sys_nerr;
48
  extern char *sys_errlist[];
49
  extern int errno;
50
#endif
51
  const char *err;
52
  char *combined;
53
 
54
  err = (errno < sys_nerr) ? sys_errlist[errno] : "unknown error";
55
  combined = (char *) alloca (strlen (err) + strlen (string) + 3);
56
  strcpy (combined, string);
57
  strcat (combined, ": ");
58
  strcat (combined, err);
59
  fprintf (stderr, "\n%s.\n", combined);
60
  fflush (stderr);
61
  exit (1);
62
}
63
 
64
static void
65
sync_error (FILE *fp, char *desc, int expect, int got)
66
{
67
  fprintf (stderr, "\n%s\n", desc);
68
  fprintf (stderr, "At logfile offset %ld, expected '0x%x' got '0x%x'\n",
69
           ftell (fp), expect, got);
70
  fflush (stderr);
71
  exit (1);
72
}
73
 
74
void
75
remote_close (void)
76
{
77
  close (remote_desc);
78
}
79
 
80
/* Open a connection to a remote debugger.
81
   NAME is the filename used for communication.  */
82
 
83
void
84
remote_open (char *name)
85
{
86
  extern char *strchr ();
87
 
88
  if (!strchr (name, ':'))
89
    {
90
      fprintf (stderr, "%s: Must specify tcp connection as host:addr\n", name);
91
      fflush (stderr);
92
      exit (1);
93
    }
94
  else
95
    {
96
      char *port_str;
97
      int port;
98
      struct sockaddr_in sockaddr;
99
      int tmp;
100
      struct protoent *protoent;
101
      int tmp_desc;
102
 
103
      port_str = strchr (name, ':');
104
 
105
      port = atoi (port_str + 1);
106
 
107
      tmp_desc = socket (PF_INET, SOCK_STREAM, 0);
108
      if (tmp_desc < 0)
109
        perror_with_name ("Can't open socket");
110
 
111
      /* Allow rapid reuse of this port. */
112
      tmp = 1;
113
      setsockopt (tmp_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp,
114
                  sizeof (tmp));
115
 
116
      sockaddr.sin_family = PF_INET;
117
      sockaddr.sin_port = htons (port);
118
      sockaddr.sin_addr.s_addr = INADDR_ANY;
119
 
120
      if (bind (tmp_desc, (struct sockaddr *) &sockaddr, sizeof (sockaddr))
121
          || listen (tmp_desc, 1))
122
        perror_with_name ("Can't bind address");
123
 
124
      tmp = sizeof (sockaddr);
125
      remote_desc = accept (tmp_desc, (struct sockaddr *) &sockaddr, &tmp);
126
      if (remote_desc == -1)
127
        perror_with_name ("Accept failed");
128
 
129
      protoent = getprotobyname ("tcp");
130
      if (!protoent)
131
        perror_with_name ("getprotobyname");
132
 
133
      /* Enable TCP keep alive process. */
134
      tmp = 1;
135
      setsockopt (tmp_desc, SOL_SOCKET, SO_KEEPALIVE, (char *) &tmp, sizeof (tmp));
136
 
137
      /* Tell TCP not to delay small packets.  This greatly speeds up
138
         interactive response. */
139
      tmp = 1;
140
      setsockopt (remote_desc, protoent->p_proto, TCP_NODELAY,
141
                  (char *) &tmp, sizeof (tmp));
142
 
143
      close (tmp_desc);         /* No longer need this */
144
 
145
      signal (SIGPIPE, SIG_IGN);        /* If we don't do this, then gdbreplay simply
146
                                           exits when the remote side dies.  */
147
    }
148
 
149
  fcntl (remote_desc, F_SETFL, FASYNC);
150
 
151
  fprintf (stderr, "Replay logfile using %s\n", name);
152
  fflush (stderr);
153
}
154
 
155
static int
156
tohex (int ch)
157
{
158
  if (ch >= '0' && ch <= '9')
159
    {
160
      return (ch - '0');
161
    }
162
  if (ch >= 'A' && ch <= 'F')
163
    {
164
      return (ch - 'A' + 10);
165
    }
166
  if (ch >= 'a' && ch <= 'f')
167
    {
168
      return (ch - 'a' + 10);
169
    }
170
  fprintf (stderr, "\nInvalid hex digit '%c'\n", ch);
171
  fflush (stderr);
172
  exit (1);
173
}
174
 
175
static int
176
logchar (FILE *fp)
177
{
178
  int ch;
179
  int ch2;
180
 
181
  ch = fgetc (fp);
182
  fputc (ch, stdout);
183
  fflush (stdout);
184
  switch (ch)
185
    {
186
    case '\n':
187
      ch = EOL;
188
      break;
189
    case '\\':
190
      ch = fgetc (fp);
191
      fputc (ch, stdout);
192
      fflush (stdout);
193
      switch (ch)
194
        {
195
        case '\\':
196
          break;
197
        case 'b':
198
          ch = '\b';
199
          break;
200
        case 'f':
201
          ch = '\f';
202
          break;
203
        case 'n':
204
          ch = '\n';
205
          break;
206
        case 'r':
207
          ch = '\r';
208
          break;
209
        case 't':
210
          ch = '\t';
211
          break;
212
        case 'v':
213
          ch = '\v';
214
          break;
215
        case 'x':
216
          ch2 = fgetc (fp);
217
          fputc (ch2, stdout);
218
          fflush (stdout);
219
          ch = tohex (ch2) << 4;
220
          ch2 = fgetc (fp);
221
          fputc (ch2, stdout);
222
          fflush (stdout);
223
          ch |= tohex (ch2);
224
          break;
225
        default:
226
          /* Treat any other char as just itself */
227
          break;
228
        }
229
    default:
230
      break;
231
    }
232
  return (ch);
233
}
234
 
235
/* Accept input from gdb and match with chars from fp (after skipping one
236
   blank) up until a \n is read from fp (which is not matched) */
237
 
238
void
239
expect (FILE *fp)
240
{
241
  int fromlog;
242
  unsigned char fromgdb;
243
 
244
  if ((fromlog = logchar (fp)) != ' ')
245
    {
246
      sync_error (fp, "Sync error during gdb read of leading blank", ' ',
247
                  fromlog);
248
    }
249
  do
250
    {
251
      fromlog = logchar (fp);
252
      if (fromlog == EOL)
253
        {
254
          break;
255
        }
256
      read (remote_desc, &fromgdb, 1);
257
    }
258
  while (fromlog == fromgdb);
259
  if (fromlog != EOL)
260
    {
261
      sync_error (fp, "Sync error during read of gdb packet", fromlog,
262
                  fromgdb);
263
    }
264
}
265
 
266
/* Play data back to gdb from fp (after skipping leading blank) up until a
267
   \n is read from fp (which is discarded and not sent to gdb). */
268
 
269
void
270
play (FILE *fp)
271
{
272
  int fromlog;
273
  char ch;
274
 
275
  if ((fromlog = logchar (fp)) != ' ')
276
    {
277
      sync_error (fp, "Sync error skipping blank during write to gdb", ' ',
278
                  fromlog);
279
    }
280
  while ((fromlog = logchar (fp)) != EOL)
281
    {
282
      ch = fromlog;
283
      write (remote_desc, &ch, 1);
284
    }
285
}
286
 
287
int
288
main (int argc, char *argv[])
289
{
290
  FILE *fp;
291
  int ch;
292
 
293
  if (argc < 3)
294
    {
295
      fprintf (stderr, "Usage: gdbreplay <logfile> <host:port>\n");
296
      fflush (stderr);
297
      exit (1);
298
    }
299
  fp = fopen (argv[1], "r");
300
  if (fp == NULL)
301
    {
302
      perror_with_name (argv[1]);
303
    }
304
  remote_open (argv[2]);
305
  while ((ch = logchar (fp)) != EOF)
306
    {
307
      switch (ch)
308
        {
309
        case 'w':
310
          /* data sent from gdb to gdbreplay, accept and match it */
311
          expect (fp);
312
          break;
313
        case 'r':
314
          /* data sent from gdbreplay to gdb, play it */
315
          play (fp);
316
          break;
317
        case 'c':
318
          /* Command executed by gdb */
319
          while ((ch = logchar (fp)) != EOL);
320
          break;
321
        }
322
    }
323
  remote_close ();
324
  exit (0);
325
}

powered by: WebSVN 2.1.0

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