用etrace记录异常处理的踪迹,就在异常响应机制下手即可。在实现异常响应之后,记录异常状态:
- 异常号
- 程序计数器
- 寄存器值
- 异常处理入口地址
// etrace
/* 记录异常处理的踪迹 */
void etrace(const char *inst, vaddr_t epc, word_t mcause, word_t gpr, word_t mtvec){
etrace_write("etrace: %s epc = " FMT_WORD ", mcause = "FMT_WORD", gpr(a7) = " FMT_WORD", mtvec = "FMT_WORD "\n",
inst, epc, mcause, gpr, mtvec);
}
#define ETRACE(inst) do { bool success = true; word_t a7 = isa_reg_str2val("a7",&success); assert(success); etrace(inst, read_csrs(CSR_MEPC), read_csrs(CSR_MCAUSE), a7, read_csrs(CSR_MTVEC));} while(0)
#define ECALL(dnpc) do { dnpc = isa_raise_intr(ENVIRONMENT_CALL_FROM_U_MODE, s->pc); ETRACE("ecall");} while(0)
但是此时查看日志发现并没有关于etrace的记录。在etrace
函数中,添加printf函数,发现etrace
调用printf函数输出正确信息,但是并没有执行log_write
的指令。
#define log_write(...) IFDEF(CONFIG_TARGET_NATIVE_ELF, \
do { \
extern FILE* log_fp; \
extern bool log_enable(); \
if (log_enable()) { \
fprintf(log_fp, __VA_ARGS__); \
fflush(log_fp); \
} \
} while (0) \
)
最终调试发现是log_enable()
是错误的。继续查看log_enable()
的源码:
bool log_enable() {
return MUXDEF(CONFIG_TRACE, (g_nr_guest_inst >= CONFIG_TRACE_START) &&
(g_nr_guest_inst <= CONFIG_TRACE_END), false);
}
那我们先不管g_nr_guest_inst
具体含义直接自定义一个etrace_write()
#define etrace_write(...) IFDEF(CONFIG_TARGET_NATIVE_ELF, \
do { \
extern FILE* log_fp; \
fprintf(log_fp, __VA_ARGS__); \
fflush(log_fp); \
} while (0) \
)
调用此即可实现etrace功能。输出例子如下:
etrace: ecall epc = 0x80001408, mcause = 0x00000008, gpr(a7) = 0xffffffff, mtvec = 0x800014
etrace: mret epc = 0x8000140c, mcause = 0x00000008, gpr(a7) = 0xffffffff, mtvec = 0x8000141
标签:fp,log,mcause,etrace,inst,a7,PA3.1
From: https://www.cnblogs.com/shangshankandashu/p/18543484