diff --git a/nemu/Kconfig b/nemu/Kconfig index e4916c1..adc8183 100644 --- a/nemu/Kconfig +++ b/nemu/Kconfig @@ -159,7 +159,20 @@ config ITRACE_BUFFER config MTRACE depends on TRACE && TARGET_NATIVE_ELF && ENGINE_INTERPRETER bool "Enable memory tracer" - default n + + +config MTRACE_RANGE + depends on MTRACE + string "Memory trace active range" + default "0-fffffff" + help + Memory tracer will only print memory access in these ranges. + Use comma to seperate between ranges. + +config MTRACE_RANGE_MAX + depends on MTRACE + int "Max range count in MTRACE_RANGE" + default 10 config DIFFTEST depends on TARGET_NATIVE_ELF diff --git a/nemu/src/memory/paddr.c b/nemu/src/memory/paddr.c index a8bbf0b..52fddee 100644 --- a/nemu/src/memory/paddr.c +++ b/nemu/src/memory/paddr.c @@ -13,6 +13,7 @@ * See the Mulan PSL v2 for more details. ***************************************************************************************/ +#include "common.h" #include #include #include @@ -23,6 +24,11 @@ static uint8_t *pmem = NULL; #else // CONFIG_PMEM_GARRAY static uint8_t pmem[CONFIG_MSIZE] PG_ALIGN = {}; #endif +#ifdef CONFIG_MTRACE +static word_t mtrace_start[CONFIG_MTRACE_RANGE_MAX] = {0}; +static word_t mtrace_end[CONFIG_MTRACE_RANGE_MAX] = {0}; +static int range_count = 0; +#endif uint8_t* guest_to_host(paddr_t paddr) { return pmem + paddr - CONFIG_MBASE; } paddr_t host_to_guest(uint8_t *haddr) { return haddr - pmem + CONFIG_MBASE; } @@ -41,10 +47,34 @@ static void out_of_bound(paddr_t addr) { addr, PMEM_LEFT, PMEM_RIGHT, cpu.pc); } +#ifdef CONFIG_MTRACE +static void mtrace_print(char type, word_t addr, int len, word_t data) { + for (int i = 0; i < range_count; i++) + if (addr <= mtrace_end[i] && addr >= mtrace_start[i] ) { + printf("[TRACE] Mem %c " FMT_WORD "%d D " FMT_WORD "\n", type, addr, len, data); + break; + } +} +#endif + void init_mem() { #if defined(CONFIG_PMEM_MALLOC) pmem = malloc(CONFIG_MSIZE); assert(pmem); +#endif +#ifdef CONFIG_MTRACE + char range[sizeof(CONFIG_MTRACE_RANGE)] = CONFIG_MTRACE_RANGE; + char *saveptr, *ptr; + ptr = strtok_r(range, ",", &saveptr); + for (range_count = 0; range_count < CONFIG_MTRACE_RANGE_MAX; range_count++) { + word_t start, end; + Assert(sscanf(ptr, "%x-%x", &start, &end) == 2, "Config option MTRACE_RANGE has wrong format"); + mtrace_start[range_count] = start; + mtrace_end[range_count] = end; + + ptr = strtok_r(NULL, ",", &saveptr); + if (!ptr) break; + } #endif IFDEF(CONFIG_MEM_RANDOM, memset(pmem, rand(), CONFIG_MSIZE)); Log("physical memory area [" FMT_PADDR ", " FMT_PADDR "]", PMEM_LEFT, PMEM_RIGHT); @@ -57,13 +87,13 @@ word_t paddr_read(paddr_t addr, int len) { out_of_bound(addr); mtrace: - IFDEF(CONFIG_MTRACE, printf("R " FMT_WORD "%d D " FMT_WORD "\n", addr, len, result)); + IFDEF(CONFIG_MTRACE, mtrace_print('R', addr, len, result)); return result; } void paddr_write(paddr_t addr, int len, word_t data) { - IFDEF(CONFIG_MTRACE, printf("W " FMT_WORD "%d D " FMT_WORD "\n", addr, len, data)); + IFDEF(CONFIG_MTRACE, mtrace_print('W', addr, len, data)); if (likely(in_pmem(addr))) { pmem_write(addr, len, data); return; } IFDEF(CONFIG_DEVICE, mmio_write(addr, len, data); return); out_of_bound(addr);