[usbdpi] Keep output of monitor_usb() in usb0.log file
Previsouly, the output of monitor_usb() went to a FIFO pipe and
consequently was lost after closing the USB DPI module. This commit
makes sure the output is kept just like other logs, as this is
useful for debugging (e.g. in CI).
Signed-off-by: Pirmin Vogel <vogelpi@lowrisc.org>
diff --git a/hw/dv/dpi/usbdpi/monitor_usb.c b/hw/dv/dpi/usbdpi/monitor_usb.c
index 8f8df7c..778936c 100644
--- a/hw/dv/dpi/usbdpi/monitor_usb.c
+++ b/hw/dv/dpi/usbdpi/monitor_usb.c
@@ -88,7 +88,7 @@
return dr;
}
-void monitor_usb(void *mon_void, int fifo_fd, int loglevel, int tick,
+void monitor_usb(void *mon_void, FILE *mon_file, int loglevel, int tick,
int hdrive, int p2d, int d2p, int *lastpid) {
struct mon_ctx *mon = (struct mon_ctx *)mon_void;
assert(mon);
@@ -99,7 +99,7 @@
if ((d2p & D2P_DP_EN) || (d2p & D2P_DN_EN)) {
if (hdrive) {
- dprintf(fifo_fd, "mon: %8d: Bus clash\n", tick);
+ fprintf(mon_file, "mon: %8d: Bus clash\n", tick);
}
dp = ((d2p & D2P_DP_EN) && (d2p & D2P_DP)) ? 1 : 0;
dn = ((d2p & D2P_DN_EN) && (d2p & D2P_DN)) ? 1 : 0;
@@ -112,9 +112,9 @@
if ((mon->driver != M_NONE) || (mon->pu != (d2p & D2P_PU))) {
if (log) {
if (d2p & D2P_PU) {
- dprintf(fifo_fd, "mon: %8d: Idle, FS resistor\n", tick);
+ fprintf(mon_file, "mon: %8d: Idle, FS resistor\n", tick);
} else {
- dprintf(fifo_fd, "mon: %8d: Idle, SE0\n", tick);
+ fprintf(mon_file, "mon: %8d: Idle, SE0\n", tick);
}
}
mon->driver = M_NONE;
@@ -129,7 +129,7 @@
if ((mon->line & 0xfff) == ((DK << 10) | (DJ << 8) | (DK << 6) | (DJ << 4) |
(DK << 2) | (DK << 0))) {
if (log) {
- dprintf(fifo_fd, "mon: %8d: (%c) SOP\n", tick,
+ fprintf(mon_file, "mon: %8d: (%c) SOP\n", tick,
mon->driver == M_HOST ? 'H' : 'D');
}
mon->sopAt = tick;
@@ -145,25 +145,25 @@
uint32_t pkt_crc16, comp_crc16;
if (compact && mon->byte == 2) {
- dprintf(fifo_fd, "mon: %8d -- %8d: (%c) SOP, PID %s, EOP\n", mon->sopAt,
- tick, mon->driver == M_HOST ? 'H' : 'D',
+ fprintf(mon_file, "mon: %8d -- %8d: (%c) SOP, PID %s, EOP\n",
+ mon->sopAt, tick, mon->driver == M_HOST ? 'H' : 'D',
pid_2data(mon->lastpid, mon->bytes[0], mon->bytes[1]));
} else if (compact && mon->byte == 1) {
- dprintf(fifo_fd, "mon: %8d -- %8d: (%c) SOP, PID %s %02x EOP\n",
+ fprintf(mon_file, "mon: %8d -- %8d: (%c) SOP, PID %s %02x EOP\n",
mon->sopAt, tick, mon->driver == M_HOST ? 'H' : 'D',
decode_pid[mon->lastpid & 0xf], mon->bytes[0]);
} else {
if (compact) {
- dprintf(fifo_fd, "mon: %8d -- %8d: (%c) SOP, PID %s, EOP\n",
+ fprintf(mon_file, "mon: %8d -- %8d: (%c) SOP, PID %s, EOP\n",
mon->sopAt, tick, mon->driver == M_HOST ? 'H' : 'D',
decode_pid[mon->lastpid & 0xf]);
}
- dprintf(fifo_fd,
- "mon: %s: ", mon->driver == M_HOST ? "h->d" : "d->h");
+ fprintf(mon_file, "mon: %s: ",
+ mon->driver == M_HOST ? "h->d" : "d->h");
comp_crc16 = CRC16(mon->bytes, mon->byte - 2);
pkt_crc16 = mon->bytes[mon->byte - 2] | mon->bytes[mon->byte - 1] << 8;
for (i = 0; i < mon->byte; i++) {
- dprintf(fifo_fd, "%02x%s", mon->bytes[i],
+ fprintf(mon_file, "%02x%s", mon->bytes[i],
((i & 0xf) == 0xf) ? "\nmon: "
: ((i + 1) == mon->byte) ? "" : ", ");
if ((mon->bytes[i] == 0x0d) || (mon->bytes[i] == 0x0a)) {
@@ -179,24 +179,24 @@
}
}
if (comp_crc16 == pkt_crc16) {
- dprintf(fifo_fd, "%s CRCOK\n",
+ fprintf(mon_file, "%s CRCOK\n",
(mon->byte == MON_BYTES_SIZE) ? "..." : "");
} else {
- dprintf(fifo_fd, "%s\nmon: CRC16 %04x BAD expected %04x\n",
+ fprintf(mon_file, "%s\nmon: CRC16 %04x BAD expected %04x\n",
(mon->byte == MON_BYTES_SIZE) ? "..." : "", pkt_crc16,
comp_crc16);
}
if (text && mon->byte > 2) {
- dprintf(fifo_fd, "mon: %s\n", mon->bytes);
+ fprintf(mon_file, "mon: %s\n", mon->bytes);
}
}
} else if (compact) {
- dprintf(fifo_fd, "mon: %8d -- %8d: (%c) SOP, PID %s EOP\n", mon->sopAt,
+ fprintf(mon_file, "mon: %8d -- %8d: (%c) SOP, PID %s EOP\n", mon->sopAt,
tick, mon->driver == M_HOST ? 'H' : 'D',
decode_pid[mon->lastpid & 0xf]);
}
if (log) {
- dprintf(fifo_fd, "mon: %8d: (%c) EOP\n", tick,
+ fprintf(mon_file, "mon: %8d: (%c) EOP\n", tick,
mon->driver == M_HOST ? 'H' : 'D');
}
mon->state = MS_IDLE;
@@ -206,7 +206,7 @@
mon->rawbits = (mon->rawbits << 1) | newbit;
if ((mon->rawbits & 0x7e) == 0x7e) {
if (newbit == 1) {
- dprintf(fifo_fd, "mon: %8d: (%c) Bitstuff error, got 1 after 0x%x\n",
+ fprintf(mon_file, "mon: %8d: (%c) Bitstuff error, got 1 after 0x%x\n",
tick, mon->driver == M_HOST ? 'H' : 'D', mon->rawbits);
}
/* Ignore bit stuff bit */
@@ -223,12 +223,12 @@
*lastpid = mon->bits;
mon->lastpid = mon->bits;
if (log) {
- dprintf(fifo_fd, "mon: %8d: (%c) PID %s (0x%x)\n", tick,
+ fprintf(mon_file, "mon: %8d: (%c) PID %s (0x%x)\n", tick,
mon->driver == M_HOST ? 'H' : 'D',
decode_pid[mon->bits & 0xf], mon->bits);
}
} else if (log) {
- dprintf(fifo_fd, "mon: %8d: (%c) BAD PID 0x%x\n", tick,
+ fprintf(mon_file, "mon: %8d: (%c) BAD PID 0x%x\n", tick,
mon->driver == M_HOST ? 'H' : 'D', mon->bits);
}
mon->state = MS_GET_BYTES;
diff --git a/hw/dv/dpi/usbdpi/usbdpi.c b/hw/dv/dpi/usbdpi/usbdpi.c
index 32dd722..5348dd7 100644
--- a/hw/dv/dpi/usbdpi/usbdpi.c
+++ b/hw/dv/dpi/usbdpi/usbdpi.c
@@ -53,32 +53,23 @@
assert(cwd_rv != NULL);
int rv;
- rv = snprintf(ctx->fifo_pathname, PATH_MAX, "%s/%s", cwd, name);
+
+ // Monitor log file
+ rv = snprintf(ctx->mon_pathname, PATH_MAX, "%s/%s.log", cwd, name);
assert(rv <= PATH_MAX && rv > 0);
-
- // Delete the file if it still exists (simulation crashed)
- rv = unlink(ctx->fifo_pathname);
-
- rv = mkfifo(ctx->fifo_pathname, 0644); // writes are not supported currently
- if (rv != 0) {
- fprintf(stderr, "USB: Unable to create FIFO at %s: %s\n",
- ctx->fifo_pathname, strerror(errno));
+ ctx->mon_file = fopen(ctx->mon_pathname, "w");
+ if (ctx->mon_file == NULL) {
+ fprintf(stderr, "USB: Unable to open monitor file at %s: %s\n",
+ ctx->mon_pathname, strerror(errno));
return NULL;
}
-
- ctx->fifo_fd = open(ctx->fifo_pathname, O_RDWR);
- if (ctx->fifo_fd < 0) {
- fprintf(stderr, "USB: Unable to open FIFO at %s: %s\n", ctx->fifo_pathname,
- strerror(errno));
- return NULL;
- }
-
+ // more useful for tail -f
+ setlinebuf(ctx->mon_file);
printf(
- "\n"
- "USB: FIFO pipe created at %s. Run\n"
- "$ cat %s\n"
- "to observe the output.\n",
- ctx->fifo_pathname, ctx->fifo_pathname);
+ "\nUSB: Monitor output file created at %s. Works well with tail:\n"
+ "$ tail -f %s\n",
+ ctx->mon_pathname, ctx->mon_pathname);
+
return (void *)ctx;
}
@@ -125,7 +116,7 @@
n = snprintf(obuf, MAX_OBUF, "%4x %8d %s %s %s\n", ctx->frame, ctx->tick,
raw_str, (d2p & D2P_PU) ? "PU" : " ",
(ctx->state == ST_GET) ? decode_usb[dp << 1 | dn] : "ZZ ");
- ssize_t written = write(ctx->fifo_fd, obuf, n);
+ ssize_t written = fwrite(obuf, sizeof(char), (size_t)n, ctx->mon_file);
assert(written == n);
}
}
@@ -635,7 +626,7 @@
return ctx->driving;
}
- monitor_usb(ctx->mon, ctx->fifo_fd, ctx->loglevel, ctx->tick,
+ monitor_usb(ctx->mon, ctx->mon_file, ctx->loglevel, ctx->tick,
(ctx->state != ST_IDLE) && (ctx->state != ST_GET), ctx->driving,
d2p, &(ctx->lastrxpid));
@@ -809,7 +800,7 @@
obuf, MAX_OBUF, "%4x %8d %s %s\n", ctx->frame, ctx->tick,
ctx->driving & P2D_SENSE ? "VBUS" : " ",
(ctx->state != ST_IDLE) ? decode_usb[(ctx->driving >> 1) & 3] : "ZZ ");
- ssize_t written = write(ctx->fifo_fd, obuf, n);
+ ssize_t written = fwrite(obuf, sizeof(char), (size_t)n, ctx->mon_file);
assert(written == n);
}
return ctx->driving;
@@ -820,15 +811,6 @@
if (!ctx) {
return;
}
- int rv;
- rv = close(ctx->fifo_fd);
- if (rv != 0) {
- printf("USB: Failed to close FIFO: %s\n", strerror(errno));
- }
- rv = unlink(ctx->fifo_pathname);
- if (rv != 0) {
- printf("USB: Failed to unlink FIFO file at %s: %s\n", ctx->fifo_pathname,
- strerror(errno));
- }
+ fclose(ctx->mon_file);
free(ctx);
}
diff --git a/hw/dv/dpi/usbdpi/usbdpi.h b/hw/dv/dpi/usbdpi/usbdpi.h
index 17ce921..0aaa587 100644
--- a/hw/dv/dpi/usbdpi/usbdpi.h
+++ b/hw/dv/dpi/usbdpi/usbdpi.h
@@ -9,6 +9,7 @@
#define TOOL_INCISIVE 0
#include <limits.h>
+#include <stdio.h>
#include <svdpi.h>
// How many bits in our frame (1ms on real hardware)
@@ -90,9 +91,9 @@
#endif
struct usbdpi_ctx {
- int fifo_fd;
int loglevel;
- char fifo_pathname[PATH_MAX];
+ FILE *mon_file;
+ char mon_pathname[PATH_MAX];
void *mon;
int lastrxpid;
int tick;
@@ -123,8 +124,8 @@
uint32_t CRC16(uint8_t *data, int bytes);
void *monitor_usb_init(void);
-void monitor_usb(void *mon, int fifo_fd, int log, int tick, int hdrive, int p2d,
- int d2p, int *lastpid);
+void monitor_usb(void *mon, FILE *mon_file, int log, int tick, int hdrive,
+ int p2d, int d2p, int *lastpid);
#ifdef __cplusplus
}