1 /* Test-driver for the remote-virtual-component simulator framework
2 for GDB, the GNU Debugger.
4 Copyright 2006-2021 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"
46 #ifdef HAVE_SYS_TYPES_H
47 #include <sys/types.h>
52 #ifdef HAVE_SYS_SELECT_H
53 #include <sys/select.h>
58 /* Not guarded in dv-sockser.c, so why here. */
59 #include <netinet/in.h>
60 #include <arpa/inet.h>
62 #include <sys/socket.h>
70 RV_MBOX_HANDLE_CMD
= 5,
75 enum opts
{ OPT_PORT
= 1, OPT_TIMEOUT
, OPT_VERBOSE
};
77 struct option longopts
[] =
79 {"port", required_argument
, NULL
, OPT_PORT
},
80 {"timeout", required_argument
, NULL
, OPT_TIMEOUT
},
81 {"verbose", no_argument
, NULL
, OPT_VERBOSE
},
86 time_t timeout
= 30000;
87 char *progname
= "(unknown)";
90 /* Required forward-declarations. */
91 static void handle_input_file (int, char *);
93 /* Set up a "server" listening to the port in PORT for a raw TCP
94 connection. Return a file descriptor for the connection or -1 on
97 static int setupsocket (void)
102 struct sockaddr_in sa_in
;
103 struct sockaddr_in from
;
106 memset (&from
, 0, len
);
107 memset (&sa_in
, 0, sizeof (sa_in
));
109 s
= socket (AF_INET
, SOCK_STREAM
, 0);
113 if (setsockopt (s
, SOL_SOCKET
, SO_REUSEADDR
, &reuse
, sizeof reuse
) != 0)
116 sa_in
.sin_port
= htons (port
);
117 sa_in
.sin_family
= AF_INET
;
119 if (bind (s
, (struct sockaddr
*) & sa_in
, sizeof sa_in
) < 0)
122 if (listen (s
, 1) < 0)
125 return accept (s
, (struct sockaddr
*) &from
, &len
);
128 /* Basic host-to-little-endian 32-bit value. Could use the BFD
129 machinery, but let's avoid it for this only dependency. */
132 h2le32 (unsigned char *dest
, unsigned int val
)
135 dest
[1] = (val
>> 8) & 255;
136 dest
[2] = (val
>> 16) & 255;
137 dest
[3] = (val
>> 24) & 255;
140 /* Send a blob of data. */
143 send_output (int fd
, unsigned char *buf
, int nbytes
)
147 ssize_t written
= write (fd
, buf
, nbytes
);
150 fprintf (stderr
, "%s: write to socket failed: %s\n",
151 progname
, strerror (errno
));
158 /* Receive a blob of data, NBYTES large. Compare to the first NCOMP
159 bytes of BUF; if not a match, write error message to stderr and
160 exit (2). Else put it in buf. */
163 expect_input (int fd
, unsigned char *buf
, int nbytes
, int ncomp
)
168 for (i
= 0; i
< nbytes
; i
++)
175 r
= read (fd
, &byt
, 1);
177 while (r
<= 0 && (r
== 0 || errno
== EAGAIN
));
181 fprintf (stderr
, "%s: read from socket failed: %s",
182 progname
, strerror (errno
));
186 if (i
< ncomp
&& byt
!= buf
[i
])
189 fprintf (stderr
, "%s: unexpected input,\n ", progname
);
191 fprintf (stderr
, "nothing,");
193 for (j
= 0; j
< i
; j
++)
194 fprintf (stderr
, "%02x", buf
[j
]);
195 fprintf (stderr
, "\nthen %02x instead of %02x\n", byt
, buf
[i
]);
203 /* Handle everything about a nil-terminated line of input.
204 Call exit (2) on error with error text on stderr. */
207 handle_input (int fd
, char *buf
, char *fname
, int lineno
)
213 static unsigned char bytes
[1024];
216 memset (bytes
, 0, sizeof bytes
);
224 /* Comment characters and empty lines. */
225 case 0: case '!': case '#':
228 /* Include another file. */
230 handle_input_file (fd
, s
);
233 /* Raw input (to be expected). */
238 sscanf (s
, "%02x%n", &data
, &n
);
241 bytes
[nbytes
++] = data
;
244 expect_input (fd
, bytes
, nbytes
, nbytes
);
248 for (i
= 0; i
< nbytes
; i
++)
249 printf ("%02x", bytes
[i
]);
254 /* Raw output (to be written). */
259 sscanf (s
, "%02x%n", &data
, &n
);
263 bytes
[nbytes
++] = data
;
269 send_output (fd
, bytes
, nbytes
);
273 for (i
= 0; i
< nbytes
; i
++)
274 printf ("%02x", bytes
[i
]);
279 /* Read a register. */
283 sscanf (s
, "%x,%x%n", &addr
, &data
, &n
);
284 if (n
< 0 || s
[n
] != 0)
288 bytes
[2] = RV_READ_CMD
;
289 h2le32 (bytes
+ 3, addr
);
290 expect_input (fd
, bytes
, 11, 7);
291 h2le32 (bytes
+ 7, data
);
292 send_output (fd
, bytes
, 11);
294 printf ("r,%x,%x\n", addr
, data
);
298 /* Write a register. */
302 sscanf (s
, "%x,%x%n", &addr
, &data
, &n
);
303 if (n
< 0 || s
[n
] != 0)
307 bytes
[2] = RV_WRITE_CMD
;
308 h2le32 (bytes
+ 3, addr
);
309 h2le32 (bytes
+ 7, data
);
310 expect_input (fd
, bytes
, 11, 11);
311 send_output (fd
, bytes
, 11);
313 printf ("w,%x,%x\n", addr
, data
);
317 /* Wait for some milliseconds. */
322 sscanf (s
, "%d%n", &del
, &n
);
323 if (n
< 0 || s
[n
] != 0 || del
== 0)
326 to
.tv_sec
= del
/ 1000;
327 to
.tv_usec
= (del
% 1000) * 1000;
329 if (select (0, NULL
, NULL
, NULL
, &to
) != 0)
331 fprintf (stderr
, "%s: problem waiting for %d ms:\n %s\n",
332 progname
, del
, strerror (errno
));
336 printf ("t,%d\n", del
);
340 /* Expect a watchdog command. */
346 bytes
[2] = RV_WATCHDOG_CMD
;
347 expect_input (fd
, bytes
, 3, 3);
352 /* Send an IRQ notification. */
354 sscanf (s
, "%x%n", &data
, &n
);
355 if (n
< 0 || s
[n
] != 0)
359 bytes
[2] = RV_IRQ_CMD
;
360 h2le32 (bytes
+ 3, data
);
361 send_output (fd
, bytes
, 7);
363 printf ("I,%x\n", data
);
366 /* DMA store (to CPU). */
370 sscanf (s
, "%x,%n", &addr
, &n
);
372 if (n
< 0 || s
[n
] == 0)
378 sscanf (s
, "%02x%n", &data
, &n
);
382 bytes
[11 + nbytes
++] = data
;
389 h2le32 (bytes
, nbytes
+ 11);
390 bytes
[2] = RV_MEM_WR_CMD
;
391 h2le32 (bytes
+ 3, addr
);
392 h2le32 (bytes
+ 7, nbytes
);
393 send_output (fd
, bytes
, nbytes
+ 11);
396 printf ("s,%x,", addr
);
397 for (i
= 0; i
< nbytes
; i
++)
398 printf ("%02x", bytes
[i
]);
404 /* DMA load (from CPU). */
408 sscanf (s
, "%x,%n", &addr
, &n
);
410 if (n
< 0 || s
[n
] == 0)
416 sscanf (s
, "%02x%n", &data
, &n
);
420 bytes
[11 + nbytes
++] = data
;
427 h2le32 (bytes
, nbytes
+ 11);
430 bytes
[2] = RV_MEM_RD_CMD
;
431 h2le32 (bytes
+ 3, addr
);
432 h2le32 (bytes
+ 7, nbytes
);
433 send_output (fd
, bytes
, 11);
434 bytes
[0] = (nbytes
+ 11) & 255;
435 bytes
[1] = ((nbytes
+ 11) >> 8) & 255;
436 expect_input (fd
, bytes
, nbytes
+ 11, nbytes
+ 11);
439 printf ("l,%x,", addr
);
440 for (i
= 0; i
< nbytes
; i
++)
441 printf ("%02x", bytes
[i
]);
449 fprintf (stderr
, "%s: invalid command line in %s:%d:\n %s",
450 progname
, fname
, lineno
, strerror (errno
));
455 /* Loop over the contents of FNAME, using handle_input to parse each line.
456 Errors to stderr, exit (2). */
459 handle_input_file (int fd
, char *fname
)
461 static char buf
[2048] = {0};
463 FILE *f
= fopen (fname
, "r");
467 fprintf (stderr
, "%s: problem opening %s: %s\n",
468 progname
, fname
, strerror (errno
));
472 /* Let's cut the buffer short, so we always get a newline. */
473 while (fgets (buf
, sizeof (buf
) - 1, f
) != NULL
)
475 buf
[strlen (buf
) - 1] = 0;
477 handle_input (fd
, buf
, fname
, lineno
);
484 main (int argc
, char *argv
[])
492 while ((optc
= getopt_long (argc
, argv
, "", longopts
, NULL
)) != -1)
496 port
= atoi (optarg
);
500 timeout
= (time_t) atoi (optarg
);
511 fprintf (stderr
, "%s: problem setting up the connection: %s\n",
512 progname
, strerror (errno
));
516 for (i
= optind
; i
< argc
; i
++)
517 handle_input_file (fd
, argv
[i
]);
519 /* FIXME: option-controlled test for remaining input? */
This page took 0.14615 seconds and 5 git commands to generate.