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