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.
5 This file is part of GDB.
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.
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.
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, Boston, MA 02111-1307, USA. */
23 #include <netinet/in.h>
24 #include <sys/socket.h>
26 #include <netinet/tcp.h>
31 /* Sort of a hack... */
34 static int remote_desc
;
36 /* Print the system error message for errno, and also mention STRING
37 as the file name for which the error was encountered.
38 Then return to command level. */
41 perror_with_name (string
)
45 extern char *sys_errlist
[];
50 err
= (errno
< sys_nerr
) ? sys_errlist
[errno
] : "unknown error";
51 combined
= (char *) alloca (strlen (err
) + strlen (string
) + 3);
52 strcpy (combined
, string
);
53 strcat (combined
, ": ");
54 strcat (combined
, err
);
55 fprintf (stderr
, "\n%s.\n", combined
);
61 sync_error (fp
, desc
, expect
, got
)
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
);
80 /* Open a connection to a remote debugger.
81 NAME is the filename used for communication. */
87 extern char *strchr ();
89 if (!strchr (name
, ':'))
91 fprintf (stderr
, "%s: Must specify tcp connection as host:addr\n", name
);
99 struct sockaddr_in sockaddr
;
101 struct protoent
*protoent
;
104 port_str
= strchr (name
, ':');
106 port
= atoi (port_str
+ 1);
108 tmp_desc
= socket (PF_INET
, SOCK_STREAM
, 0);
110 perror_with_name ("Can't open socket");
112 /* Allow rapid reuse of this port. */
114 setsockopt (tmp_desc
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&tmp
,
117 sockaddr
.sin_family
= PF_INET
;
118 sockaddr
.sin_port
= htons(port
);
119 sockaddr
.sin_addr
.s_addr
= INADDR_ANY
;
121 if (bind (tmp_desc
, (struct sockaddr
*)&sockaddr
, sizeof (sockaddr
))
122 || listen (tmp_desc
, 1))
123 perror_with_name ("Can't bind address");
125 tmp
= sizeof (sockaddr
);
126 remote_desc
= accept (tmp_desc
, (struct sockaddr
*)&sockaddr
, &tmp
);
127 if (remote_desc
== -1)
128 perror_with_name ("Accept failed");
130 protoent
= getprotobyname ("tcp");
132 perror_with_name ("getprotobyname");
134 /* Enable TCP keep alive process. */
136 setsockopt (tmp_desc
, SOL_SOCKET
, SO_KEEPALIVE
, (char *)&tmp
, sizeof(tmp
));
138 /* Tell TCP not to delay small packets. This greatly speeds up
139 interactive response. */
141 setsockopt (remote_desc
, protoent
->p_proto
, TCP_NODELAY
,
142 (char *)&tmp
, sizeof(tmp
));
144 close (tmp_desc
); /* No longer need this */
146 signal (SIGPIPE
, SIG_IGN
); /* If we don't do this, then gdbreplay simply
147 exits when the remote side dies. */
150 fcntl (remote_desc
, F_SETFL
, FASYNC
);
152 fprintf (stderr
, "Replay logfile using %s\n", name
);
156 static int tohex (ch
)
159 if (ch
>= '0' && ch
<= '9')
163 if (ch
>= 'A' && ch
<= 'F')
165 return (ch
- 'A' + 10);
167 if (ch
>= 'a' && ch
<= 'f')
169 return (ch
- 'a' + 10);
171 fprintf (stderr
, "\nInvalid hex digit '%c'\n", ch
);
198 case 'b': ch
= '\b'; break;
199 case 'f': ch
= '\f'; break;
200 case 'n': ch
= '\n'; break;
201 case 'r': ch
= '\r'; break;
202 case 't': ch
= '\t'; break;
203 case 'v': ch
= '\v'; break;
208 ch
= tohex (ch2
) << 4;
215 /* Treat any other char as just itself */
224 /* Accept input from gdb and match with chars from fp (after skipping one
225 blank) up until a \n is read from fp (which is not matched) */
232 unsigned char fromgdb
;
234 if ((fromlog
= logchar (fp
)) != ' ')
236 sync_error (fp
, "Sync error during gdb read of leading blank", ' ',
241 fromlog
= logchar (fp
);
246 read (remote_desc
, &fromgdb
, 1);
247 } while (fromlog
== fromgdb
);
250 sync_error (fp
, "Sync error during read of gdb packet", fromlog
,
255 /* Play data back to gdb from fp (after skipping leading blank) up until a
256 \n is read from fp (which is discarded and not sent to gdb). */
265 if ((fromlog
= logchar (fp
)) != ' ')
267 sync_error (fp
, "Sync error skipping blank during write to gdb", ' ',
270 while ((fromlog
= logchar (fp
)) != EOL
)
273 write (remote_desc
, &ch
, 1);
287 fprintf (stderr
, "Usage: gdbreplay <logfile> <host:port>\n");
291 fp
= fopen (argv
[1], "r");
294 perror_with_name (argv
[1]);
296 remote_open (argv
[2]);
297 while ((ch
= logchar (fp
)) != EOF
)
302 /* data sent from gdb to gdbreplay, accept and match it */
306 /* data sent from gdbreplay to gdb, play it */
310 /* Command executed by gdb */
311 while ((ch
= logchar (fp
)) != EOL
);