Skip to content

Commit 7212a60

Browse files
committed
feat(ftrace): implement ftrace
1 parent 3579dcd commit 7212a60

File tree

6 files changed

+71
-7
lines changed

6 files changed

+71
-7
lines changed

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ itrace:
2525
@echo "-------Start Debugging-------"
2626
@$(SIM) $(T) --itrace
2727

28+
ftrace:
29+
@echo "-------Build Simulator-------"
30+
@$(MAKE) -C sim
31+
@echo "-------Build Test-------"
32+
@$(MAKE) -C test T=$(T)
33+
@echo "-------Start Debugging-------"
34+
@$(SIM) $(T) --ftrace
35+
2836
unit_test:
2937
@echo "------- Building and Running Simulator Unit Tests -------"
3038
@$(MAKE) -C sim unit_test

sim/include/ftrace.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ void add_func_symbol(SymbolTable *table, uint64_t addr, const char *name);
2020

2121
void sort_symbols_by_address(SymbolTable *table);
2222

23-
const char* find_func_name(SymbolTable *table, uint64_t addr);
23+
const FuncSymbol* find_func(SymbolTable *table, uint64_t addr);
2424

2525
void free_symbol_table(SymbolTable *table);
2626

sim/src/cpu.c

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

67
extern int itrace_enabled;
8+
extern int ftrace_enabled;
9+
extern SymbolTable *sym_table;
710
extern LLVMDisasmContextRef disasm_ctx;
11+
12+
int indentaton_level = 0;
813
CPU_state cpu = {};
914
static int running = 1;
1015

@@ -14,7 +19,29 @@ void init_cpu(){
1419
memset(cpu.csr, 0, sizeof(cpu.csr));
1520
}
1621

17-
22+
static int is_call(uint32_t inst) {
23+
uint32_t opcode = inst & 0x7f;
24+
uint32_t rd = (inst >> 7) & 0x1f;
25+
if (opcode == 0x6f && rd == 1) { // JAL && rd == x1 (ra)
26+
return 1;
27+
}
28+
if (opcode == 0x67 && rd == 1) { // JALR && rd == x1 (ra)
29+
return 1;
30+
}
31+
return 0;
32+
}
33+
34+
static int is_ret(uint32_t inst) {
35+
uint32_t opcode = inst & 0x7f;
36+
uint32_t rs1 = (inst >> 15) & 0x1f;
37+
uint32_t rd = (inst >> 7) & 0x1f;
38+
uint32_t imm = (inst >> 20) & 0xfff;
39+
if (opcode == 0x67 && rs1 == 1 && rd == 0 && imm == 0) { // JALR x0, 0(x1)
40+
return 1;
41+
}
42+
return 0;
43+
}
44+
1845
void exec_once(){
1946
Decode s;
2047
s.pc = cpu.pc;
@@ -30,6 +57,29 @@ void exec_once(){
3057
disassemble_inst(disasm_ctx, bytes, 4, s.pc, asm_buf, sizeof(asm_buf));
3158
printf("\33[1;34m0x%016lx\33[1;0m: %08x %s\n", s.pc, s.inst, asm_buf);
3259
}
60+
if (ftrace_enabled) {
61+
if (is_call(s.inst)) {
62+
const FuncSymbol *func_symbol = find_func(sym_table, s.pc);
63+
const char *func_name = func_symbol ? func_symbol->name : "unknown_function";
64+
const uint64_t func_addr = func_symbol ? func_symbol->address : 0;
65+
printf("\33[1;34m0x%08lx\33[1;0m: ", s.pc);
66+
for (int i = 0; i < indentaton_level; i++) {
67+
printf(" ");
68+
}
69+
printf("call [%s@%08lx]\n", func_name, func_addr);
70+
indentaton_level++;
71+
} else if (is_ret(s.inst)) {
72+
indentaton_level--;
73+
if (indentaton_level < 0) indentaton_level = 0;
74+
const FuncSymbol *func_symbol = find_func(sym_table, s.pc);
75+
const char *func_name = func_symbol ? func_symbol->name : "unknown_function";
76+
printf("\33[1;34m0x%08lx\33[1;0m: ", s.pc);
77+
for (int i = 0; i < indentaton_level; i++) {
78+
printf(" ");
79+
}
80+
printf("ret [%s]\n", func_name);
81+
}
82+
}
3383
decode_exec(&s);
3484
cpu.pc = s.dnpc;
3585
}

sim/src/ftrace.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ void sort_symbols_by_address(SymbolTable *table) {
4848
qsort(table->symbols, table->count, sizeof(FuncSymbol), compare_symbols);
4949
}
5050

51-
const char* find_func_name(SymbolTable *table, uint64_t addr) {
52-
if (!table || table->count == 0) return "unknown_function";
51+
const FuncSymbol* find_func(SymbolTable *table, uint64_t addr) {
52+
if (!table || table->count == 0) return NULL;
5353

5454
int low = 0, high = table->count - 1;
5555
int best_match_idx = -1;
@@ -65,10 +65,10 @@ const char* find_func_name(SymbolTable *table, uint64_t addr) {
6565
}
6666

6767
if (best_match_idx != -1) {
68-
return table->symbols[best_match_idx].name;
68+
return &(table->symbols[best_match_idx]);
6969
}
7070

71-
return "unknown_function";
71+
return NULL;
7272
}
7373

7474

sim/src/main.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
uint8_t *mem = NULL;
88
int itrace_enabled = 0;
9-
LLVMDisasmContextRef disasm_ctx;
9+
int ftrace_enabled = 0;
1010
SymbolTable *sym_table = NULL;
11+
LLVMDisasmContextRef disasm_ctx;
1112

1213
const char *help_string = "Usage: Simulator <img_file> [-d|-b]\n"
1314
"Options:\n"
@@ -40,6 +41,10 @@ int main(int argc, char *argv[]){
4041
init_llvm_disassembler();
4142
cpu_exec();
4243
}
44+
else if (argc > 2 && strcmp(argv[2], "--ftrace") == 0) {
45+
ftrace_enabled = 1;
46+
cpu_exec();
47+
}
4348
else {
4449
printf("%s", help_string);
4550
exit(0);

sim/src/memory.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <common.h>
22
#include <gelf.h>
33
#include <fcntl.h>
4+
#include <unistd.h>
45
#include "ftrace.h"
56

67
extern uint8_t* mem;

0 commit comments

Comments
 (0)