1 /* Test-driver for the remote-virtual-component simulator framework
2 for GDB, the GNU Debugger.
4 Copyright 2006-2013 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[])
38 #include "libiberty.h"
53 #ifdef HAVE_SYS_TYPES_H
54 #include <sys/types.h>
57 #ifdef HAVE_SYS_TIME_H
61 #ifdef HAVE_SYS_SELECT_H
62 #include <sys/select.h>
69 /* Not guarded in dv-sockser.c, so why here. */
70 #include <netinet/in.h>
71 #include <arpa/inet.h>
73 #include <sys/socket.h>
81 RV_MBOX_HANDLE_CMD = 5,
86 enum opts { OPT_PORT = 1, OPT_TIMEOUT, OPT_VERBOSE };
88 struct option longopts[] =
90 {"port", required_argument, NULL, OPT_PORT},
91 {"timeout", required_argument, NULL, OPT_TIMEOUT},
92 {"verbose", no_argument, NULL, OPT_VERBOSE},
97 time_t timeout = 30000;
98 char *progname = "(unknown)";
101 /* Required forward-declarations. */
102 static void handle_input_file (int, char *);
104 /* Set up a "server" listening to the port in PORT for a raw TCP
105 connection. Return a file descriptor for the connection or -1 on
108 int setupsocket (void)
113 struct sockaddr_in sa_in;
114 struct sockaddr_in from;
117 memset (&from, 0, len);
118 memset (&sa_in, 0, sizeof (sa_in));
120 s = socket (AF_INET, SOCK_STREAM, 0);
124 if (setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof reuse) != 0)
127 sa_in.sin_port = htons (port);
128 sa_in.sin_family = AF_INET;
130 if (bind (s, (struct sockaddr *) & sa_in, sizeof sa_in) < 0)
133 if (listen (s, 1) < 0)
136 return accept (s, (struct sockaddr *) &from, &len);
139 /* Basic host-to-little-endian 32-bit value. Could use the BFD
140 machinery, but let's avoid it for this only dependency. */
143 h2le32 (unsigned char *dest, unsigned int val)
146 dest[1] = (val >> 8) & 255;
147 dest[2] = (val >> 16) & 255;
148 dest[3] = (val >> 24) & 255;
151 /* Send a blob of data. */
154 send_output (int fd, unsigned char *buf, int nbytes)
158 ssize_t written = write (fd, buf, nbytes);
161 fprintf (stderr, "%s: write to socket failed: %s\n",
162 progname, strerror (errno));
169 /* Receive a blob of data, NBYTES large. Compare to the first NCOMP
170 bytes of BUF; if not a match, write error message to stderr and
171 exit (2). Else put it in buf. */
174 expect_input (int fd, unsigned char *buf, int nbytes, int ncomp)
179 for (i = 0; i < nbytes; i++)
186 r = read (fd, &byt, 1);
188 while (r <= 0 && (r == 0 || errno == EAGAIN));
192 fprintf (stderr, "%s: read from socket failed: %s",
193 progname, strerror (errno));
197 if (i < ncomp && byt != buf[i])
200 fprintf (stderr, "%s: unexpected input,\n ", progname);
202 fprintf (stderr, "nothing,");
204 for (j = 0; j < i; j++)
205 fprintf (stderr, "%02x", buf[j]);
206 fprintf (stderr, "\nthen %02x instead of %02x\n", byt, buf[i]);
214 /* Handle everything about a nil-terminated line of input.
215 Call exit (2) on error with error text on stderr. */
218 handle_input (int fd, char *buf, char *fname, int lineno)
224 static unsigned char bytes[1024];
227 memset (bytes, 0, sizeof bytes);
235 /* Comment characters and empty lines. */
236 case 0: case '!': case '#':
239 /* Include another file. */
241 handle_input_file (fd, s);
244 /* Raw input (to be expected). */
249 sscanf (s, "%02x%n", &data, &n);
252 bytes[nbytes++] = data;
255 expect_input (fd, bytes, nbytes, nbytes);
259 for (i = 0; i < nbytes; i++)
260 printf ("%02x", bytes[i]);
265 /* Raw output (to be written). */
270 sscanf (s, "%02x%n", &data, &n);
274 bytes[nbytes++] = data;
280 send_output (fd, bytes, nbytes);
284 for (i = 0; i < nbytes; i++)
285 printf ("%02x", bytes[i]);
290 /* Read a register. */
294 sscanf (s, "%x,%x%n", &addr, &data, &n);
295 if (n < 0 || s[n] != 0)
299 bytes[2] = RV_READ_CMD;
300 h2le32 (bytes + 3, addr);
301 expect_input (fd, bytes, 11, 7);
302 h2le32 (bytes + 7, data);
303 send_output (fd, bytes, 11);
305 printf ("r,%x,%x\n", addr, data);
309 /* Write a register. */
313 sscanf (s, "%x,%x%n", &addr, &data, &n);
314 if (n < 0 || s[n] != 0)
318 bytes[2] = RV_WRITE_CMD;
319 h2le32 (bytes + 3, addr);
320 h2le32 (bytes + 7, data);
321 expect_input (fd, bytes, 11, 11);
322 send_output (fd, bytes, 11);
324 printf ("w,%x,%x\n", addr, data);
328 /* Wait for some milliseconds. */
333 sscanf (s, "%d%n", &del, &n);
334 if (n < 0 || s[n] != 0 || del == 0)
337 to.tv_sec = del / 1000;
338 to.tv_usec = (del % 1000) * 1000;
340 if (select (0, NULL, NULL, NULL, &to) != 0)
342 fprintf (stderr, "%s: problem waiting for %d ms:\n %s\n",
343 progname, del, strerror (errno));
347 printf ("t,%d\n", del);
351 /* Expect a watchdog command. */
357 bytes[2] = RV_WATCHDOG_CMD;
358 expect_input (fd, bytes, 3, 3);
363 /* Send an IRQ notification. */
365 sscanf (s, "%x%n", &data, &n);
366 if (n < 0 || s[n] != 0)
370 bytes[2] = RV_IRQ_CMD;
371 h2le32 (bytes + 3, data);
372 send_output (fd, bytes, 7);
374 printf ("I,%x\n", data);
377 /* DMA store (to CPU). */
381 sscanf (s, "%x,%n", &addr, &n);
383 if (n < 0 || s[n] == 0)
389 sscanf (s, "%02x%n", &data, &n);
393 bytes[11 + nbytes++] = data;
400 h2le32 (bytes, nbytes + 11);
401 bytes[2] = RV_MEM_WR_CMD;
402 h2le32 (bytes + 3, addr);
403 h2le32 (bytes + 7, nbytes);
404 send_output (fd, bytes, nbytes + 11);
407 printf ("s,%x,", addr);
408 for (i = 0; i < nbytes; i++)
409 printf ("%02x", bytes[i]);
415 /* DMA load (from CPU). */
419 sscanf (s, "%x,%n", &addr, &n);
421 if (n < 0 || s[n] == 0)
427 sscanf (s, "%02x%n", &data, &n);
431 bytes[11 + nbytes++] = data;
438 h2le32 (bytes, nbytes + 11);
441 bytes[2] = RV_MEM_RD_CMD;
442 h2le32 (bytes + 3, addr);
443 h2le32 (bytes + 7, nbytes);
444 send_output (fd, bytes, 11);
445 bytes[0] = (nbytes + 11) & 255;
446 bytes[1] = ((nbytes + 11) >> 8) & 255;
447 expect_input (fd, bytes, nbytes + 11, nbytes + 11);
450 printf ("l,%x,", addr);
451 for (i = 0; i < nbytes; i++)
452 printf ("%02x", bytes[i]);
460 fprintf (stderr, "%s: invalid command line in %s:%d:\n %s",
461 progname, fname, lineno, strerror (errno));
466 /* Loop over the contents of FNAME, using handle_input to parse each line.
467 Errors to stderr, exit (2). */
470 handle_input_file (int fd, char *fname)
472 static char buf[2048] = {0};
474 FILE *f = fopen (fname, "r");
478 fprintf (stderr, "%s: problem opening %s: %s\n",
479 progname, fname, strerror (errno));
483 /* Let's cut the buffer short, so we always get a newline. */
484 while (fgets (buf, sizeof (buf) - 1, f) != NULL)
486 buf[strlen (buf) - 1] = 0;
488 handle_input (fd, buf, fname, lineno);
495 main (int argc, char *argv[])
503 while ((optc = getopt_long (argc, argv, "", longopts, NULL)) != -1)
507 port = atoi (optarg);
511 timeout = (time_t) atoi (optarg);
522 fprintf (stderr, "%s: problem setting up the connection: %s\n",
523 progname, strerror (errno));
527 for (i = optind; i < argc; i++)
528 handle_input_file (fd, argv[i]);
530 /* FIXME: option-controlled test for remaining input? */