Skip to content

Commit 711d51a

Browse files
committed
fix(ftrace): sort the symbol table & target func name
1 parent 48b24c0 commit 711d51a

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

sim/src/cpu.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <decode.h>
33
#include <memory.h>
44
#include <disasm.h>
5+
#include <macro.h>
56
#include "ftrace.h"
67

78
extern int itrace_enabled;
@@ -31,6 +32,28 @@ static int is_call(uint32_t inst) {
3132
return 0;
3233
}
3334

35+
uint64_t get_call_target_addr(Decode *s) {
36+
uint32_t inst = s->inst;
37+
uint32_t opcode = inst & 0x7f;
38+
39+
// J-type (jal)
40+
if (opcode == 0b1101111) {
41+
int32_t imm_j = SEXT(BITS(inst, 31, 31) << 20 | \
42+
BITS(inst, 19, 12) << 12 | \
43+
BITS(inst, 20, 20) << 11 | \
44+
BITS(inst, 30, 21) << 1, 21);
45+
return s->pc + imm_j;
46+
}
47+
// I-type (jalr)
48+
else if (opcode == 0b1100111) {
49+
int rs1_idx = (inst >> 15) & 0x1f;
50+
int32_t imm_i = (int32_t)inst >> 20;
51+
return cpu.reg[rs1_idx] + imm_i;
52+
}
53+
54+
return 0;
55+
}
56+
3457
static int is_ret(uint32_t inst) {
3558
uint32_t opcode = inst & 0x7f;
3659
uint32_t rs1 = (inst >> 15) & 0x1f;
@@ -59,7 +82,9 @@ void exec_once(){
5982
}
6083
if (ftrace_enabled) {
6184
if (is_call(s.inst)) {
62-
const FuncSymbol *func_symbol = find_func(sym_table, s.pc);
85+
uint64_t target_addr = get_call_target_addr(&s);
86+
printf("Call target address: 0x%lx\n", target_addr);
87+
const FuncSymbol *func_symbol = find_func(sym_table, target_addr);
6388
const char *func_name = func_symbol ? func_symbol->name : "unknown_function";
6489
const uint64_t func_addr = func_symbol ? func_symbol->address : 0;
6590
printf("\33[1;34m0x%08lx\33[1;0m: ", s.pc);

sim/src/main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ int main(int argc, char *argv[]){
2828
sprintf(elf_file, "test/build/%s.elf", argv[1]);
2929
sym_table = malloc(sizeof(SymbolTable));
3030
load_elf_symbols(elf_file);
31+
sort_symbols_by_address(sym_table);
3132

3233
init_cpu();
3334
if (argc > 2 && strcmp(argv[2], "--debug") == 0) {

0 commit comments

Comments
 (0)