1 /* Test-driver for the remote-virtual-component simulator framework
2 for GDB, the GNU Debugger.
4 Copyright 2006-2015 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 /* Avoid any problems whatsoever building this program if we're not
22 also building hardware support. */
26 main (int argc
, char *argv
[])
37 #include "libiberty.h"
52 #ifdef HAVE_SYS_TYPES_H
53 #include <sys/types.h>
56 #ifdef HAVE_SYS_TIME_H
60 #ifdef HAVE_SYS_SELECT_H
61 #include <sys/select.h>
68 /* Not guarded in dv-sockser.c, so why here. */
69 #include <netinet/in.h>
70 #include <arpa/inet.h>
72 #include <sys/socket.h>
80 RV_MBOX_HANDLE_CMD
= 5,
85 enum opts
{ OPT_PORT
= 1, OPT_TIMEOUT
, OPT_VERBOSE
};
87 struct option longopts
[] =
89 {"port", required_argument
, NULL
, OPT_PORT
},
90 {"timeout", required_argument
, NULL
, OPT_TIMEOUT
},
91 {"verbose", no_argument
, NULL
, OPT_VERBOSE
},
96 time_t timeout
= 30000;
97 char *progname
= "(unknown)";
100 /* Required forward-declarations. */
101 static void handle_input_file (int, char *);
103 /* Set up a "server" listening to the port in PORT for a raw TCP
104 connection. Return a file descriptor for the connection or -1 on
107 int setupsocket (void)
112 struct sockaddr_in sa_in
;
113 struct sockaddr_in from
;
116 memset (&from
, 0, len
);
117 memset (&sa_in
, 0, sizeof (sa_in
));
119 s
= socket (AF_INET
, SOCK_STREAM
, 0);
123 if (setsockopt (s
, SOL_SOCKET
, SO_REUSEADDR
, &reuse
, sizeof reuse
) != 0)
126 sa_in
.sin_port
= htons (port
);
127 sa_in
.sin_family
= AF_INET
;
129 if (bind (s
, (struct sockaddr
*) & sa_in
, sizeof sa_in
) < 0)
132 if (listen (s
, 1) < 0)
135 return accept (s
, (struct sockaddr
*) &from
, &len
);
138 /* Basic host-to-little-endian 32-bit value. Could use the BFD
139 machinery, but let's avoid it for this only dependency. */
142 h2le32 (unsigned char *dest
, unsigned int val
)
145 dest
[1] = (val
>> 8) & 255;
146 dest
[2] = (val
>> 16) & 255;
147 dest
[3] = (val
>> 24) & 255;
150 /* Send a blob of data. */
153 send_output (int fd
, unsigned char *buf
, int nbytes
)
157 ssize_t written
= write (fd
, buf
, nbytes
);
160 fprintf (stderr
, "%s: write to socket failed: %s\n",
161 progname
, strerror (errno
));
168 /* Receive a blob of data, NBYTES large. Compare to the first NCOMP
169 bytes of BUF; if not a match, write error message to stderr and
170 exit (2). Else put it in buf. */
173 expect_input (int fd
, unsigned char *buf
, int nbytes
, int ncomp
)
178 for (i
= 0; i
< nbytes
; i
++)
185 r
= read (fd
, &byt
, 1);
187 while (r
<= 0 && (r
== 0 || errno
== EAGAIN
));
191 fprintf (stderr
, "%s: read from socket failed: %s",
192 progname
, strerror (errno
));
196 if (i
< ncomp
&& byt
!= buf
[i
])
199 fprintf (stderr
, "%s: unexpected input,\n ", progname
);
201 fprintf (stderr
, "nothing,");
203 for (j
= 0; j
< i
; j
++)
204 fprintf (stderr
, "%02x", buf
[j
]);
205 fprintf (stderr
, "\nthen %02x instead of %02x\n", byt
, buf
[i
]);
213 /* Handle everything about a nil-terminated line of input.
214 Call exit (2) on error with error text on stderr. */
217 handle_input (int fd
, char *buf
, char *fname
, int lineno
)
223 static unsigned char bytes
[1024];
226 memset (bytes
, 0, sizeof bytes
);
234 /* Comment characters and empty lines. */
235 case 0: case '!': case '#':
238 /* Include another file. */
240 handle_input_file (fd
, s
);
243 /* Raw input (to be expected). */
248 sscanf (s
, "%02x%n", &data
, &n
);
251 bytes
[nbytes
++] = data
;
254 expect_input (fd
, bytes
, nbytes
, nbytes
);
258 for (i
= 0; i
< nbytes
; i
++)
259 printf ("%02x", bytes
[i
]);
264 /* Raw output (to be written). */
269 sscanf (s
, "%02x%n", &data
, &n
);
273 bytes
[nbytes
++] = data
;
279 send_output (fd
, bytes
, nbytes
);
283 for (i
= 0; i
< nbytes
; i
++)
284 printf ("%02x", bytes
[i
]);
289 /* Read a register. */
293 sscanf (s
, "%x,%x%n", &addr
, &data
, &n
);
294 if (n
< 0 || s
[n
] != 0)
298 bytes
[2] = RV_READ_CMD
;
299 h2le32 (bytes
+ 3, addr
);
300 expect_input (fd
, bytes
, 11, 7);
301 h2le32 (bytes
+ 7, data
);
302 send_output (fd
, bytes
, 11);
304 printf ("r,%x,%x\n", addr
, data
);
308 /* Write a register. */
312 sscanf (s
, "%x,%x%n", &addr
, &data
, &n
);
313 if (n
< 0 || s
[n
] != 0)
317 bytes
[2] = RV_WRITE_CMD
;
318 h2le32 (bytes
+ 3, addr
);
319 h2le32 (bytes
+ 7, data
);
320 expect_input (fd
, bytes
, 11, 11);
321 send_output (fd
, bytes
, 11);
323 printf ("w,%x,%x\n", addr
, data
);
327 /* Wait for some milliseconds. */
332 sscanf (s
, "%d%n", &del
, &n
);
333 if (n
< 0 || s
[n
] != 0 || del
== 0)
336 to
.tv_sec
= del
/ 1000;
337 to
.tv_usec
= (del
% 1000) * 1000;
339 if (select (0, NULL
, NULL
, NULL
, &to
) != 0)
341 fprintf (stderr
, "%s: problem waiting for %d ms:\n %s\n",
342 progname
, del
, strerror (errno
));
346 printf ("t,%d\n", del
);
350 /* Expect a watchdog command. */
356 bytes
[2] = RV_WATCHDOG_CMD
;
357 expect_input (fd
, bytes
, 3, 3);
362 /* Send an IRQ notification. */
364 sscanf (s
, "%x%n", &data
, &n
);
365 if (n
< 0 || s
[n
] != 0)
369 bytes
[2] = RV_IRQ_CMD
;
370 h2le32 (bytes
+ 3, data
);
371 send_output (fd
, bytes
, 7);
373 printf ("I,%x\n", data
);
376 /* DMA store (to CPU). */
380 sscanf (s
, "%x,%n", &addr
, &n
);
382 if (n
< 0 || s
[n
] == 0)
388 sscanf (s
, "%02x%n", &data
, &n
);
392 bytes
[11 + nbytes
++] = data
;
399 h2le32 (bytes
, nbytes
+ 11);
400 bytes
[2] = RV_MEM_WR_CMD
;
401 h2le32 (bytes
+ 3, addr
);
402 h2le32 (bytes
+ 7, nbytes
);
403 send_output (fd
, bytes
, nbytes
+ 11);
406 printf ("s,%x,", addr
);
407 for (i
= 0; i
< nbytes
; i
++)
408 printf ("%02x", bytes
[i
]);
414 /* DMA load (from CPU). */
418 sscanf (s
, "%x,%n", &addr
, &n
);
420 if (n
< 0 || s
[n
] == 0)
426 sscanf (s
, "%02x%n", &data
, &n
);
430 bytes
[11 + nbytes
++] = data
;
437 h2le32 (bytes
, nbytes
+ 11);
440 bytes
[2] = RV_MEM_RD_CMD
;
441 h2le32 (bytes
+ 3, addr
);
442 h2le32 (bytes
+ 7, nbytes
);
443 send_output (fd
, bytes
, 11);
444 bytes
[0] = (nbytes
+ 11) & 255;
445 bytes
[1] = ((nbytes
+ 11) >> 8) & 255;
446 expect_input (fd
, bytes
, nbytes
+ 11, nbytes
+ 11);
449 printf ("l,%x,", addr
);
450 for (i
= 0; i
< nbytes
; i
++)
451 printf ("%02x", bytes
[i
]);
459 fprintf (stderr
, "%s: invalid command line in %s:%d:\n %s",
460 progname
, fname
, lineno
, strerror (errno
));
465 /* Loop over the contents of FNAME, using handle_input to parse each line.
466 Errors to stderr, exit (2). */
469 handle_input_file (int fd
, char *fname
)
471 static char buf
[2048] = {0};
473 FILE *f
= fopen (fname
, "r");
477 fprintf (stderr
, "%s: problem opening %s: %s\n",
478 progname
, fname
, strerror (errno
));
482 /* Let's cut the buffer short, so we always get a newline. */
483 while (fgets (buf
, sizeof (buf
) - 1, f
) != NULL
)
485 buf
[strlen (buf
) - 1] = 0;
487 handle_input (fd
, buf
, fname
, lineno
);
494 main (int argc
, char *argv
[])
502 while ((optc
= getopt_long (argc
, argv
, "", longopts
, NULL
)) != -1)
506 port
= atoi (optarg
);
510 timeout
= (time_t) atoi (optarg
);
521 fprintf (stderr
, "%s: problem setting up the connection: %s\n",
522 progname
, strerror (errno
));
526 for (i
= optind
; i
< argc
; i
++)
527 handle_input_file (fd
, argv
[i
]);
529 /* FIXME: option-controlled test for remaining input? */