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