From 5e0d8a1824b56e8c593baf273b8dc341cf1075cf Mon Sep 17 00:00:00 2001 From: tracer-ics2023 Date: Wed, 11 Sep 2024 10:01:53 +0800 Subject: [PATCH] =?UTF-8?q?>=20=20compile=20NEMU=20221220000=20=E5=BC=A0?= =?UTF-8?q?=E4=B8=89=20Linux=20zzy=205.15.146.1-microsoft-standard-WSL2=20?= =?UTF-8?q?#1=20SMP=20Thu=20Jan=2011=2004:09:03=20UTC=202024=20x86=5F64=20?= =?UTF-8?q?x86=5F64=20x86=5F64=20GNU/Linux=20=2010:01:53=20up=2041=20min,?= =?UTF-8?q?=20=201=20user,=20=20load=20average:=200.19,=200.20,=200.24?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nemu/Kconfig | 5 +++++ nemu/src/cpu/cpu-exec.c | 11 ++++++----- nemu/src/monitor/elf.c | 21 +++++++++++++++------ 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/nemu/Kconfig b/nemu/Kconfig index 5d0cd47..d6ba87d 100644 --- a/nemu/Kconfig +++ b/nemu/Kconfig @@ -221,6 +221,11 @@ config FTRACE_DISPLAY bool "Enable to ftrace log in 'statistic'(must end of log file)" default n +config FTRACE_INST + depends on FTRACE + bool "Enable ftrace in itrace means in every instruction to show what function name" + default n + config DIFFTEST depends on TARGET_NATIVE_ELF diff --git a/nemu/src/cpu/cpu-exec.c b/nemu/src/cpu/cpu-exec.c index d92badd..79c7aac 100644 --- a/nemu/src/cpu/cpu-exec.c +++ b/nemu/src/cpu/cpu-exec.c @@ -45,18 +45,14 @@ static void iringbuf_append(const char* log) { } static void iringbuf_display(void) { - log_write("--- START IRINGBUF ---\n"); for (int i = 0; i < CONFIG_IRINGBUF_LINENUM; i ++) { if (iringbuf[i][0] != '\0') - log_write("%-4s%s\n", i == irb_pos ? "-->" : "", iringbuf[i]); + log_write("iringbuf %3d %-4s%s\n", i, i == irb_pos ? "-->" : "", iringbuf[i]); } - log_write("--- END IRINGBUF ---\n"); } #endif #ifdef CONFIG_FTRACE -void ftrace_display(); - #endif static void trace_and_difftest(Decode *_this, vaddr_t dnpc) { @@ -100,6 +96,10 @@ static void exec_once(Decode *s, vaddr_t pc) { memset(p, ' ', space_len); p += space_len; +#ifdef CONFIG_FTRACE_INST + int ftrace_inst(char* p, size_t size, vaddr_t pc); + p += ftrace_inst(p, s->logbuf + sizeof(s->logbuf) - p, s->pc); +#endif #ifndef CONFIG_ISA_loongarch32r void disassemble(char *str, int size, uint64_t pc, uint8_t *code, int nbyte); disassemble(p, s->logbuf + sizeof(s->logbuf) - p, @@ -126,6 +126,7 @@ static void statistic() { iringbuf_display(); #endif #ifdef CONFIG_FTRACE_DISPLAY + void ftrace_display(); ftrace_display(); #endif IFNDEF(CONFIG_TARGET_AM, setlocale(LC_NUMERIC, "")); diff --git a/nemu/src/monitor/elf.c b/nemu/src/monitor/elf.c index 8f904a6..96ef526 100644 --- a/nemu/src/monitor/elf.c +++ b/nemu/src/monitor/elf.c @@ -14,18 +14,27 @@ typedef struct { word_t size; char fname[32]; } Elf_Ftrace; -// [start, start + size) == [start, end] +// [start, start + size) == [start, end) static int elf_ftrace_size = 0; static Elf_Ftrace elf_ftrace[128]; #ifdef CONFIG_FTRACE_DISPLAY void ftrace_display() { for (int i = 0; i < elf_ftrace_size; i ++) { - log_write("ftrace %d: ["FMT_WORD", "FMT_WORD"] %s\n", i, elf_ftrace[i].start, elf_ftrace[i].end, elf_ftrace[i].fname); + log_write("ftrace %3d: ["FMT_WORD", "FMT_WORD") %s\n", i, elf_ftrace[i].start, elf_ftrace[i].end, elf_ftrace[i].fname); } } -#else -void ftrace_display() {} +#endif + +#ifdef CONFIG_FTRACE_INST +int ftrace_inst(char* log, size_t size, vaddr_t pc) { + for (int i = 0; i < elf_ftrace_size; i ++) { + if (pc >= elf_ftrace[i].start && pc < elf_ftrace[i].end) { + return snprintf(log, size, "%s", elf_ftrace[i].fname); + } + } + return 0; +} #endif void init_elf(const char* elf_file) { @@ -103,7 +112,7 @@ void init_elf(const char* elf_file) { // filling ftrace for (int i = 0; i < symt_len; i ++) { - Log("%3d: %08x "FMT_WORD" "FMT_WORD"", i, symt[i].st_value, symt[i].st_size, symt[i].st_name); + // Log("%3d: %08x "FMT_WORD" "FMT_WORD"", i, symt[i].st_value, symt[i].st_size, symt[i].st_name); if (ELF_ST_TYPE(symt[i].st_info) == STT_FUNC) { // Warning due to elf_ftrace is global variable so you don't need to set '\0' in the end strncpy(elf_ftrace[elf_ftrace_size].fname, buffer + symt[i].st_name, @@ -111,7 +120,7 @@ void init_elf(const char* elf_file) { elf_ftrace[elf_ftrace_size].start = symt[i].st_value; elf_ftrace[elf_ftrace_size].size = symt[i].st_size; elf_ftrace[elf_ftrace_size].end = elf_ftrace[elf_ftrace_size].start + \ - elf_ftrace[elf_ftrace_size].size - 1; + elf_ftrace[elf_ftrace_size].size; elf_ftrace_size ++; } }