From c7a5741847aefe2760c331754465102304a57e8f Mon Sep 17 00:00:00 2001 From: Allen Date: Tue, 22 Dec 2020 16:35:23 +0800 Subject: [PATCH] emu: catch sigint signal and save coverage before exit. This works for linux-hello which never stops. Now, we can stop emu with sigint and still get a valid coverage result. --- src/test/csrc/emu.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/test/csrc/emu.cpp b/src/test/csrc/emu.cpp index c570f8b0b..e7d07ee10 100644 --- a/src/test/csrc/emu.cpp +++ b/src/test/csrc/emu.cpp @@ -2,6 +2,8 @@ #include "sdcard.h" #include "difftest.h" #include +#include +#include #include "ram.h" #include "zlib.h" @@ -228,6 +230,16 @@ inline void Emulator::single_cycle() { cycles ++; } +#if VM_COVERAGE == 1 +uint64_t *max_cycle_ptr = NULL; +// when interrupted, we set max_cycle to zero +// so that the emulator will stop gracefully +void sig_handler(int signo) { + if (signo == SIGINT) + *max_cycle_ptr = 0; +} +#endif + uint64_t Emulator::execute(uint64_t max_cycle, uint64_t max_instr) { extern void poll_event(void); extern uint32_t uptime(void); @@ -252,6 +264,9 @@ uint64_t Emulator::execute(uint64_t max_cycle, uint64_t max_instr) { // since we are not sure when an emu will stop // we distinguish multiple dat files by emu start time time_t start_time = time(NULL); + max_cycle_ptr = &max_cycle; + if (signal(SIGINT, sig_handler) == SIG_ERR) + printf("\ncan't catch SIGINT\n"); #endif while (!Verilated::gotFinish() && trapCode == STATE_RUNNING) { -- GitLab