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