Go to the source code of this file.
Data Structures | |
struct | bt_func_t |
Description of a function for the purpose of backtracing (filled by __bt_analyze_func) More... | |
struct | backtrace_cb_ctx |
Macros | |
#define | BACKTRACE_DEBUG 0 |
Enable to debug why a backtrace is wrong. | |
#define | FUNCTION_ALIGNMENT 32 |
Function alignment enfored by the compiler (-falign-functions). | |
#define | true 1 |
#define | false 0 |
#define | MIPS_OP_ADDIU_SP(op) (((op) & 0xFFFF0000) == 0x27BD0000) |
Matches: addiu $sp, $sp, imm. | |
#define | MIPS_OP_DADDIU_SP(op) (((op) & 0xFFFF0000) == 0x67BD0000) |
Matches: daddiu $sp, $sp, imm. | |
#define | MIPS_OP_JR_RA(op) (((op) & 0xFFFFFFFF) == 0x03E00008) |
Matches: jr $ra. | |
#define | MIPS_OP_SD_RA_SP(op) (((op) & 0xFFFF0000) == 0xFFBF0000) |
Matches: sd $ra, imm($sp) | |
#define | MIPS_OP_SW_RA_SP(op) (((op) & 0xFFFF0000) == 0xAFBF0000) |
Matches: sw $ra, imm($sp) | |
#define | MIPS_OP_SD_FP_SP(op) (((op) & 0xFFFF0000) == 0xFFBE0000) |
Matches: sd $fp, imm($sp) | |
#define | MIPS_OP_SW_FP_SP(op) (((op) & 0xFFFF0000) == 0xAFBE0000) |
Matches: sw $fp, imm($sp) | |
#define | MIPS_OP_LUI_GP(op) (((op) & 0xFFFF0000) == 0x3C1C0000) |
Matches: lui $gp, imm. | |
#define | MIPS_OP_NOP(op) ((op) == 0x00000000) |
Matches: nop. | |
#define | MIPS_OP_MOVE_FP_SP(op) ((op) == 0x03A0F025) |
Matches: move $fp, $sp. | |
#define | debugf osSyncPrintf |
#define | symbolsPerChunk 0x1000 |
#define | chunkSize ((sizeof(Symbol) * symbolsPerChunk)) |
#define | inthandler ((uint32_t*)0x8006A9F0) |
#define | inthandler_end ((uint32_t*)0x8006B35C) |
Typedefs | |
typedef s64 | int64_t |
typedef s32 | int32_t |
typedef s16 | int16_t |
typedef s8 | int8_t |
typedef u64 | uint64_t |
typedef u32 | uint32_t |
typedef u16 | uint16_t |
typedef u8 | uint8_t |
typedef s32 | bool |
Enumerations | |
enum | bt_func_type { BT_FUNCTION , BT_FUNCTION_FRAMEPOINTER , BT_EXCEPTION , BT_LEAF } |
The "type" of funciton as categorized by the backtrace heuristic (__bt_analyze_func) More... | |
Functions | |
bool | __bt_analyze_func (bt_func_t *func, uint32_t *ptr, uint32_t func_start, bool from_exception) |
Analyze a function to find out its stack frame layout and properties (useful for backtracing). | |
int | backtrace (void **buffer, int size) |
Walk the stack and return the current call stack. | |
int | backtrace_thread (void **buffer, int size, OSThread *thread) |
s32 | address2symbol (u32 address, Symbol *out) |
Uses the symbol table to look up the symbol corresponding to the given address. | |
char * | load_symbol_string (char *dest, u32 addr, int n) |
void | backtrace_address_to_string (u32 address, char *dest) |
Converts a function address to a string representation using its name, offset, and file. | |
void | debug_backtrace (void) |
Print a backtrace. | |
struct bt_func_t |
Data Fields | ||
---|---|---|
bt_func_type | type | Type of the function. |
int | stack_size | Size of the stack frame. |
int | ra_offset | Offset of the return address from the top of the stack frame. |
int | fp_offset | Offset of the saved fp from the top of the stack frame; this is != 0 only if the function modifies fp (maybe as a frame pointer, but not necessarily) |
#define BACKTRACE_DEBUG 0 |
Enable to debug why a backtrace is wrong.
Definition at line 11 of file backtrace.c.
#define FUNCTION_ALIGNMENT 32 |
Function alignment enfored by the compiler (-falign-functions).
Definition at line 14 of file backtrace.c.
Referenced by __bt_analyze_func().
#define true 1 |
Definition at line 26 of file backtrace.c.
#define false 0 |
Definition at line 27 of file backtrace.c.
#define MIPS_OP_ADDIU_SP | ( | op | ) | (((op) & 0xFFFF0000) == 0x27BD0000) |
Matches: addiu $sp, $sp, imm.
Definition at line 45 of file backtrace.c.
Referenced by __bt_analyze_func().
#define MIPS_OP_DADDIU_SP | ( | op | ) | (((op) & 0xFFFF0000) == 0x67BD0000) |
Matches: daddiu $sp, $sp, imm.
Definition at line 46 of file backtrace.c.
Referenced by __bt_analyze_func().
#define MIPS_OP_JR_RA | ( | op | ) | (((op) & 0xFFFFFFFF) == 0x03E00008) |
Matches: jr $ra.
Definition at line 47 of file backtrace.c.
#define MIPS_OP_SD_RA_SP | ( | op | ) | (((op) & 0xFFFF0000) == 0xFFBF0000) |
Matches: sd $ra, imm($sp)
Definition at line 48 of file backtrace.c.
Referenced by __bt_analyze_func().
#define MIPS_OP_SW_RA_SP | ( | op | ) | (((op) & 0xFFFF0000) == 0xAFBF0000) |
Matches: sw $ra, imm($sp)
Definition at line 49 of file backtrace.c.
Referenced by __bt_analyze_func().
#define MIPS_OP_SD_FP_SP | ( | op | ) | (((op) & 0xFFFF0000) == 0xFFBE0000) |
Matches: sd $fp, imm($sp)
Definition at line 50 of file backtrace.c.
Referenced by __bt_analyze_func().
#define MIPS_OP_SW_FP_SP | ( | op | ) | (((op) & 0xFFFF0000) == 0xAFBE0000) |
Matches: sw $fp, imm($sp)
Definition at line 51 of file backtrace.c.
Referenced by __bt_analyze_func().
#define MIPS_OP_LUI_GP | ( | op | ) | (((op) & 0xFFFF0000) == 0x3C1C0000) |
Matches: lui $gp, imm.
Definition at line 52 of file backtrace.c.
Referenced by __bt_analyze_func().
#define MIPS_OP_NOP | ( | op | ) | ((op) == 0x00000000) |
#define MIPS_OP_MOVE_FP_SP | ( | op | ) | ((op) == 0x03A0F025) |
Matches: move $fp, $sp.
Definition at line 54 of file backtrace.c.
Referenced by __bt_analyze_func().
#define debugf osSyncPrintf |
Definition at line 56 of file backtrace.c.
#define symbolsPerChunk 0x1000 |
#define chunkSize ((sizeof(Symbol) * symbolsPerChunk)) |
#define inthandler ((uint32_t*)0x8006A9F0) |
#define inthandler_end ((uint32_t*)0x8006B35C) |
typedef s64 int64_t |
Definition at line 16 of file backtrace.c.
typedef s32 int32_t |
Definition at line 17 of file backtrace.c.
typedef s16 int16_t |
Definition at line 18 of file backtrace.c.
typedef s8 int8_t |
Definition at line 19 of file backtrace.c.
typedef u64 uint64_t |
Definition at line 20 of file backtrace.c.
typedef u32 uint32_t |
Definition at line 21 of file backtrace.c.
typedef u16 uint16_t |
Definition at line 22 of file backtrace.c.
typedef u8 uint8_t |
Definition at line 23 of file backtrace.c.
typedef s32 bool |
Definition at line 25 of file backtrace.c.
enum bt_func_type |
The "type" of funciton as categorized by the backtrace heuristic (__bt_analyze_func)
Definition at line 30 of file backtrace.c.
bool __bt_analyze_func | ( | bt_func_t * | func, |
uint32_t * | ptr, | ||
uint32_t | func_start, | ||
bool | from_exception ) |
Analyze a function to find out its stack frame layout and properties (useful for backtracing).
This function implements the core heuristic used by the backtrace engine. It analyzes the actual code of a function in memory instruction by instruction, trying to find out whether the function uses a stack frame or not, whether it uses a frame pointer, and where the return address is stored.
Since we do not have DWARF informations or similar metadata, we can just do educated guesses. A mistake in the heuristic will result probably in a wrong backtrace from this point on.
The heuristic works as follows:
sd $ra, nn($sp)
), and an instruction creating the stack frame (eg: addiu $sp, $sp, -nn
). Once both are found, the heuristic knows how to fill in .stack_size
and .ra_offset
fields of the function description structure, and it can stop.move $fp, $sp
, it knows that the function uses $fp as frame pointer, and will mark the function as BT_FUNCTION_FRAMEPOINTER. In any case, the field .fp_offset
will be filled in with the offset in the stack where $fp is stored, so that the backtrace engine can track the current value of the register in any case.func | Output function description structure |
ptr | Pointer to the function code at the point where the backtrace starts. This is normally the point where a JAL opcode is found, as we are walking up the call stack. |
func_start | Start of the function being analyzed. This is optional: the heuristic can work without this hint, but it is useful in certain situations (eg: to better walk up after an exception). |
from_exception | If true, this function was interrupted by an exception. This is a hint that the function might even be a leaf function without a stack frame, and that we must use special heuristics for it. |
Definition at line 490 of file backtrace.c.
int backtrace | ( | void ** | buffer, |
int | size ) |
Walk the stack and return the current call stack.
This function will analyze the current execution context, walking the stack and returning informations on the active call frames.
This function adheres to POSIX specification. It does not allocate memory so it is safe to be called even in the context of low memory conditions or possibly corrupted heap.
If called within an interrupt or exception handler, the function is able to correctly walk backward the interrupt handler and show the context even before the exception was triggered.
buffer | Empty array of pointers. This will be populated with pointers to the return addresses for each call frame. |
size | Size of the buffer, that is, maximum number of call frames that will be walked by the function. |
Definition at line 298 of file backtrace.c.
Referenced by debug_backtrace().
int backtrace_thread | ( | void ** | buffer, |
int | size, | ||
OSThread * | thread ) |
Definition at line 308 of file backtrace.c.
Referenced by crash_screen_draw().
s32 address2symbol | ( | u32 | address, |
Symbol * | out ) |
Uses the symbol table to look up the symbol corresponding to the given address.
The address should be inside some function, otherwise an incorrect symbol will be returned.
address | Address to look up |
out | Output symbol |
Definition at line 331 of file backtrace.c.
Referenced by backtrace_address_to_string().
char * load_symbol_string | ( | char * | dest, |
u32 | addr, | ||
int | n ) |
Definition at line 383 of file backtrace.c.
Referenced by backtrace_address_to_string().
void backtrace_address_to_string | ( | u32 | address, |
char * | dest ) |
Converts a function address to a string representation using its name, offset, and file.
Definition at line 396 of file backtrace.c.
Referenced by crash_screen_draw(), and debug_backtrace().
void debug_backtrace | ( | void | ) |
Print a backtrace.
Definition at line 423 of file backtrace.c.