1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
|
/* Test-driver for the remote-virtual-component simulator framework
for GDB, the GNU Debugger.
Copyright 2006-2014 Free Software Foundation, Inc.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* Avoid any problems whatsoever building this program if we're not
also building hardware support. */
#if !WITH_HW
int
main (int argc, char *argv[])
{
return 2;
}
#else
#ifdef HAVE_CONFIG_H
#include "cconfig.h"
#include "tconfig.h"
#endif
#include "getopt.h"
#include "libiberty.h"
#define _GNU_SOURCE
#include <stdio.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
/* Not guarded in dv-sockser.c, so why here. */
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/socket.h>
enum rv_command {
RV_READ_CMD = 0,
RV_WRITE_CMD = 1,
RV_IRQ_CMD = 2,
RV_MEM_RD_CMD = 3,
RV_MEM_WR_CMD = 4,
RV_MBOX_HANDLE_CMD = 5,
RV_MBOX_PUT_CMD = 6,
RV_WATCHDOG_CMD = 7
};
enum opts { OPT_PORT = 1, OPT_TIMEOUT, OPT_VERBOSE };
struct option longopts[] =
{
{"port", required_argument, NULL, OPT_PORT},
{"timeout", required_argument, NULL, OPT_TIMEOUT},
{"verbose", no_argument, NULL, OPT_VERBOSE},
{NULL, 0, NULL, 0}
};
int port = 10000;
time_t timeout = 30000;
char *progname = "(unknown)";
int verbose = 0;
/* Required forward-declarations. */
static void handle_input_file (int, char *);
/* Set up a "server" listening to the port in PORT for a raw TCP
connection. Return a file descriptor for the connection or -1 on
error. */
int setupsocket (void)
{
int s;
socklen_t len;
int reuse = 1;
struct sockaddr_in sa_in;
struct sockaddr_in from;
len = sizeof (from);
memset (&from, 0, len);
memset (&sa_in, 0, sizeof (sa_in));
s = socket (AF_INET, SOCK_STREAM, 0);
if (s == -1)
return -1;
if (setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof reuse) != 0)
return -1;
sa_in.sin_port = htons (port);
sa_in.sin_family = AF_INET;
if (bind (s, (struct sockaddr *) & sa_in, sizeof sa_in) < 0)
return -1;
if (listen (s, 1) < 0)
return -1;
return accept (s, (struct sockaddr *) &from, &len);
}
/* Basic host-to-little-endian 32-bit value. Could use the BFD
machinery, but let's avoid it for this only dependency. */
static void
h2le32 (unsigned char *dest, unsigned int val)
{
dest[0] = val & 255;
dest[1] = (val >> 8) & 255;
dest[2] = (val >> 16) & 255;
dest[3] = (val >> 24) & 255;
}
/* Send a blob of data. */
static void
send_output (int fd, unsigned char *buf, int nbytes)
{
while (nbytes > 0)
{
ssize_t written = write (fd, buf, nbytes);
if (written < 0)
{
fprintf (stderr, "%s: write to socket failed: %s\n",
progname, strerror (errno));
exit (2);
}
nbytes -= written;
}
}
/* Receive a blob of data, NBYTES large. Compare to the first NCOMP
bytes of BUF; if not a match, write error message to stderr and
exit (2). Else put it in buf. */
static void
expect_input (int fd, unsigned char *buf, int nbytes, int ncomp)
{
unsigned char byt;
int i;
for (i = 0; i < nbytes; i++)
{
int r;
do
{
errno = 0;
r = read (fd, &byt, 1);
}
while (r <= 0 && (r == 0 || errno == EAGAIN));
if (r != 1)
{
fprintf (stderr, "%s: read from socket failed: %s",
progname, strerror (errno));
exit (2);
}
if (i < ncomp && byt != buf[i])
{
int j;
fprintf (stderr, "%s: unexpected input,\n ", progname);
if (i == 0)
fprintf (stderr, "nothing,");
else
for (j = 0; j < i; j++)
fprintf (stderr, "%02x", buf[j]);
fprintf (stderr, "\nthen %02x instead of %02x\n", byt, buf[i]);
exit (2);
}
else
buf[i] = byt;
}
}
/* Handle everything about a nil-terminated line of input.
Call exit (2) on error with error text on stderr. */
static void
handle_input (int fd, char *buf, char *fname, int lineno)
{
int nbytes = 0;
int n = -1;
char *s = buf + 2;
unsigned int data;
static unsigned char bytes[1024];
int i;
memset (bytes, 0, sizeof bytes);
lineno++;
if (buf[1] != ',')
goto syntax_error;
switch (buf[0])
{
/* Comment characters and empty lines. */
case 0: case '!': case '#':
break;
/* Include another file. */
case '@':
handle_input_file (fd, s);
break;
/* Raw input (to be expected). */
case 'i':
do
{
n = -1;
sscanf (s, "%02x%n", &data, &n);
s += n;
if (n > 0)
bytes[nbytes++] = data;
}
while (n > 0);
expect_input (fd, bytes, nbytes, nbytes);
if (verbose)
{
printf ("i,");
for (i = 0; i < nbytes; i++)
printf ("%02x", bytes[i]);
printf ("\n");
}
break;
/* Raw output (to be written). */
case 'o':
do
{
n = -1;
sscanf (s, "%02x%n", &data, &n);
if (n > 0)
{
s += n;
bytes[nbytes++] = data;
}
}
while (n > 0);
if (*s != 0)
goto syntax_error;
send_output (fd, bytes, nbytes);
if (verbose)
{
printf ("o,");
for (i = 0; i < nbytes; i++)
printf ("%02x", bytes[i]);
printf ("\n");
}
break;
/* Read a register. */
case 'r':
{
unsigned int addr;
sscanf (s, "%x,%x%n", &addr, &data, &n);
if (n < 0 || s[n] != 0)
goto syntax_error;
bytes[0] = 11;
bytes[1] = 0;
bytes[2] = RV_READ_CMD;
h2le32 (bytes + 3, addr);
expect_input (fd, bytes, 11, 7);
h2le32 (bytes + 7, data);
send_output (fd, bytes, 11);
if (verbose)
printf ("r,%x,%x\n", addr, data);
}
break;
/* Write a register. */
case 'w':
{
unsigned int addr;
sscanf (s, "%x,%x%n", &addr, &data, &n);
if (n < 0 || s[n] != 0)
goto syntax_error;
bytes[0] = 11;
bytes[1] = 0;
bytes[2] = RV_WRITE_CMD;
h2le32 (bytes + 3, addr);
h2le32 (bytes + 7, data);
expect_input (fd, bytes, 11, 11);
send_output (fd, bytes, 11);
if (verbose)
printf ("w,%x,%x\n", addr, data);
}
break;
/* Wait for some milliseconds. */
case 't':
{
int del = 0;
struct timeval to;
sscanf (s, "%d%n", &del, &n);
if (n < 0 || s[n] != 0 || del == 0)
goto syntax_error;
to.tv_sec = del / 1000;
to.tv_usec = (del % 1000) * 1000;
if (select (0, NULL, NULL, NULL, &to) != 0)
{
fprintf (stderr, "%s: problem waiting for %d ms:\n %s\n",
progname, del, strerror (errno));
exit (2);
}
if (verbose)
printf ("t,%d\n", del);
}
break;
/* Expect a watchdog command. */
case 'W':
if (*s != 0)
goto syntax_error;
bytes[0] = 3;
bytes[1] = 0;
bytes[2] = RV_WATCHDOG_CMD;
expect_input (fd, bytes, 3, 3);
if (verbose)
printf ("W\n");
break;
/* Send an IRQ notification. */
case 'I':
sscanf (s, "%x%n", &data, &n);
if (n < 0 || s[n] != 0)
goto syntax_error;
bytes[0] = 7;
bytes[1] = 0;
bytes[2] = RV_IRQ_CMD;
h2le32 (bytes + 3, data);
send_output (fd, bytes, 7);
if (verbose)
printf ("I,%x\n", data);
break;
/* DMA store (to CPU). */
case 's':
{
unsigned int addr;
sscanf (s, "%x,%n", &addr, &n);
if (n < 0 || s[n] == 0)
goto syntax_error;
s += n;
do
{
n = -1;
sscanf (s, "%02x%n", &data, &n);
if (n > 0)
{
s += n;
bytes[11 + nbytes++] = data;
}
}
while (n > 0);
if (*s != 0)
goto syntax_error;
h2le32 (bytes, nbytes + 11);
bytes[2] = RV_MEM_WR_CMD;
h2le32 (bytes + 3, addr);
h2le32 (bytes + 7, nbytes);
send_output (fd, bytes, nbytes + 11);
if (verbose)
{
printf ("s,%x,", addr);
for (i = 0; i < nbytes; i++)
printf ("%02x", bytes[i]);
printf ("\n");
}
}
break;
/* DMA load (from CPU). */
case 'l':
{
unsigned int addr;
sscanf (s, "%x,%n", &addr, &n);
if (n < 0 || s[n] == 0)
goto syntax_error;
s += n;
do
{
n = -1;
sscanf (s, "%02x%n", &data, &n);
if (n > 0)
{
s += n;
bytes[11 + nbytes++] = data;
}
}
while (n > 0);
if (*s != 0)
goto syntax_error;
h2le32 (bytes, nbytes + 11);
bytes[0] = 11;
bytes[1] = 0;
bytes[2] = RV_MEM_RD_CMD;
h2le32 (bytes + 3, addr);
h2le32 (bytes + 7, nbytes);
send_output (fd, bytes, 11);
bytes[0] = (nbytes + 11) & 255;
bytes[1] = ((nbytes + 11) >> 8) & 255;
expect_input (fd, bytes, nbytes + 11, nbytes + 11);
if (verbose)
{
printf ("l,%x,", addr);
for (i = 0; i < nbytes; i++)
printf ("%02x", bytes[i]);
printf ("\n");
}
}
break;
syntax_error:
default:
fprintf (stderr, "%s: invalid command line in %s:%d:\n %s",
progname, fname, lineno, strerror (errno));
exit (2);
}
}
/* Loop over the contents of FNAME, using handle_input to parse each line.
Errors to stderr, exit (2). */
static void
handle_input_file (int fd, char *fname)
{
static char buf[2048] = {0};
int lineno = 0;
FILE *f = fopen (fname, "r");
if (f == NULL)
{
fprintf (stderr, "%s: problem opening %s: %s\n",
progname, fname, strerror (errno));
exit (2);
}
/* Let's cut the buffer short, so we always get a newline. */
while (fgets (buf, sizeof (buf) - 1, f) != NULL)
{
buf[strlen (buf) - 1] = 0;
lineno++;
handle_input (fd, buf, fname, lineno);
}
fclose (f);
}
int
main (int argc, char *argv[])
{
int optc;
int fd;
FILE *f;
int i;
progname = argv[0];
while ((optc = getopt_long (argc, argv, "", longopts, NULL)) != -1)
switch (optc)
{
case OPT_PORT:
port = atoi (optarg);
break;
case OPT_TIMEOUT:
timeout = (time_t) atoi (optarg);
break;
case OPT_VERBOSE:
verbose = 1;
break;
}
fd = setupsocket ();
if (fd == -1)
{
fprintf (stderr, "%s: problem setting up the connection: %s\n",
progname, strerror (errno));
exit (2);
}
for (i = optind; i < argc; i++)
handle_input_file (fd, argv[i]);
/* FIXME: option-controlled test for remaining input? */
close (fd);
return 1;
}
#endif
|