aboutsummaryrefslogtreecommitdiff
path: root/c_emulator/riscv_sim.c
diff options
context:
space:
mode:
authorAlex Richardson <alexrichardson@google.com>2023-05-26 07:17:39 -0700
committerBill McSpadden <bill@riscv.org>2023-08-28 09:33:39 -0500
commit5725f3fe08a2c1b8588b9a4416d91c297e1e5c00 (patch)
treeac7a0c83a847bc1b30f4bca2b9a4602069590169 /c_emulator/riscv_sim.c
parent58cac61d9ddde591902c933a9dfa5d8ba3fca6da (diff)
downloadsail-riscv-5725f3fe08a2c1b8588b9a4416d91c297e1e5c00.zip
sail-riscv-5725f3fe08a2c1b8588b9a4416d91c297e1e5c00.tar.gz
sail-riscv-5725f3fe08a2c1b8588b9a4416d91c297e1e5c00.tar.bz2
csim: Allow redirecting trace output to a file using command line flag
This is useful when booting an operating system with tracing enabled as it allows showing the printed messages on the terminal and storing the trace log to a separate file. All categorized trace messages are now redirected to the --trace-output file when passed. If this option is not gived, the current behaviour of using stdout is retained. Example usage: `./c_emulator/riscv_sim_RV64 -b os-boot/rv64-64mb.dtb --trace-output /dev/stderr os-boot/linux-rv64-64mb.bbl --trace-output /tmp/linux.log`
Diffstat (limited to 'c_emulator/riscv_sim.c')
-rw-r--r--c_emulator/riscv_sim.c70
1 files changed, 45 insertions, 25 deletions
diff --git a/c_emulator/riscv_sim.c b/c_emulator/riscv_sim.c
index fdb9ce0..5a73896 100644
--- a/c_emulator/riscv_sim.c
+++ b/c_emulator/riscv_sim.c
@@ -48,10 +48,14 @@ const char *RV32ISA = "RV32IMAC";
#define CSR_MTVAL 0x343
#define CSR_MIP 0x344
+#define OPT_TRACE_OUTPUT 1000
+
static bool do_dump_dts = false;
static bool do_show_times = false;
struct tv_spike_t *s = NULL;
char *term_log = NULL;
+static const char *trace_log_path = NULL;
+FILE *trace_log = NULL;
char *dtb_file = NULL;
unsigned char *dtb = NULL;
size_t dtb_len = 0;
@@ -111,33 +115,34 @@ char *sailcov_file = NULL;
#endif
static struct option options[] = {
- {"enable-dirty-update", no_argument, 0, 'd'},
- {"enable-misaligned", no_argument, 0, 'm'},
- {"enable-pmp", no_argument, 0, 'P'},
- {"enable-next", no_argument, 0, 'N'},
- {"ram-size", required_argument, 0, 'z'},
- {"disable-compressed", no_argument, 0, 'C'},
- {"disable-writable-misa", no_argument, 0, 'I'},
- {"disable-fdext", no_argument, 0, 'F'},
- {"mtval-has-illegal-inst-bits", no_argument, 0, 'i'},
- {"device-tree-blob", required_argument, 0, 'b'},
- {"terminal-log", required_argument, 0, 't'},
- {"show-times", required_argument, 0, 'p'},
- {"report-arch", no_argument, 0, 'a'},
- {"test-signature", required_argument, 0, 'T'},
- {"signature-granularity", required_argument, 0, 'g'},
+ {"enable-dirty-update", no_argument, 0, 'd' },
+ {"enable-misaligned", no_argument, 0, 'm' },
+ {"enable-pmp", no_argument, 0, 'P' },
+ {"enable-next", no_argument, 0, 'N' },
+ {"ram-size", required_argument, 0, 'z' },
+ {"disable-compressed", no_argument, 0, 'C' },
+ {"disable-writable-misa", no_argument, 0, 'I' },
+ {"disable-fdext", no_argument, 0, 'F' },
+ {"mtval-has-illegal-inst-bits", no_argument, 0, 'i' },
+ {"device-tree-blob", required_argument, 0, 'b' },
+ {"terminal-log", required_argument, 0, 't' },
+ {"show-times", required_argument, 0, 'p' },
+ {"report-arch", no_argument, 0, 'a' },
+ {"test-signature", required_argument, 0, 'T' },
+ {"signature-granularity", required_argument, 0, 'g' },
#ifdef RVFI_DII
- {"rvfi-dii", required_argument, 0, 'r'},
+ {"rvfi-dii", required_argument, 0, 'r' },
#endif
- {"help", no_argument, 0, 'h'},
- {"trace", optional_argument, 0, 'v'},
- {"no-trace", optional_argument, 0, 'V'},
- {"inst-limit", required_argument, 0, 'l'},
- {"enable-zfinx", no_argument, 0, 'x'},
+ {"help", no_argument, 0, 'h' },
+ {"trace", optional_argument, 0, 'v' },
+ {"no-trace", optional_argument, 0, 'V' },
+ {"trace-output", required_argument, 0, OPT_TRACE_OUTPUT},
+ {"inst-limit", required_argument, 0, 'l' },
+ {"enable-zfinx", no_argument, 0, 'x' },
#ifdef SAILCOV
- {"sailcov-file", required_argument, 0, 'c'},
+ {"sailcov-file", required_argument, 0, 'c' },
#endif
- {0, 0, 0, 0 }
+ {0, 0, 0, 0 }
};
static void print_usage(const char *argv0, int ec)
@@ -352,6 +357,10 @@ char *process_args(int argc, char **argv)
sailcov_file = strdup(optarg);
break;
#endif
+ case OPT_TRACE_OUTPUT:
+ trace_log_path = optarg;
+ fprintf(stderr, "using %s for trace output.\n", trace_log_path);
+ break;
case '?':
print_usage(argv[0], 1);
break;
@@ -626,6 +635,9 @@ void close_logs(void)
exit(EXIT_FAILURE);
}
#endif
+ if (trace_log != stdout) {
+ fclose(trace_log);
+ }
}
void finish(int ec)
@@ -726,8 +738,8 @@ void flush_logs(void)
if (config_print_instr) {
fprintf(stderr, "\n");
fflush(stderr);
- fprintf(stdout, "\n");
- fflush(stdout);
+ fprintf(trace_log, "\n");
+ fflush(trace_log);
}
}
@@ -1010,6 +1022,14 @@ void init_logs()
exit(1);
}
+ if (trace_log_path == NULL) {
+ trace_log = stdout;
+ } else if ((trace_log = fopen(trace_log_path, "w+")) < 0) {
+ fprintf(stderr, "Cannot create trace log '%s': %s\n", trace_log,
+ strerror(errno));
+ exit(1);
+ }
+
#ifdef SAILCOV
if (sailcov_file != NULL) {
sail_set_coverage_file(sailcov_file);