|
2 | 2 | #include <decode.h> |
3 | 3 | #include <memory.h> |
4 | 4 | #include <disasm.h> |
| 5 | +#include <macro.h> |
5 | 6 | #include "ftrace.h" |
6 | 7 |
|
7 | 8 | extern int itrace_enabled; |
@@ -31,6 +32,28 @@ static int is_call(uint32_t inst) { |
31 | 32 | return 0; |
32 | 33 | } |
33 | 34 |
|
| 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 | + |
34 | 57 | static int is_ret(uint32_t inst) { |
35 | 58 | uint32_t opcode = inst & 0x7f; |
36 | 59 | uint32_t rs1 = (inst >> 15) & 0x1f; |
@@ -59,7 +82,9 @@ void exec_once(){ |
59 | 82 | } |
60 | 83 | if (ftrace_enabled) { |
61 | 84 | 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); |
63 | 88 | const char *func_name = func_symbol ? func_symbol->name : "unknown_function"; |
64 | 89 | const uint64_t func_addr = func_symbol ? func_symbol->address : 0; |
65 | 90 | printf("\33[1;34m0x%08lx\33[1;0m: ", s.pc); |
|
0 commit comments