Paper Mario DX
Paper Mario (N64) modding
 
Loading...
Searching...
No Matches
backtrace.h File Reference

Go to the source code of this file.

Data Structures

struct  Symbol
 
struct  SymbolTable
 

Macros

#define SYMBOL_TABLE_PTR_ROM_ADDR   0x18
 ROM address of the pointer to the symbol table.
 

Functions

int backtrace (void **buffer, int size)
 Walk the stack and return the current call stack.
 
int backtrace_thread (void **buffer, int size, OSThread *thread)
 
void debug_backtrace (void)
 Print a backtrace.
 
void backtrace_address_to_string (u32 address, char *dest)
 Converts a function address to a string representation using its name, offset, and file.
 

Data Structure Documentation

◆ Symbol

struct Symbol
Data Fields
u32 address RAM address.
u32 nameOffset Offset of the symbol name string.
u32 fileOffset Offset of the file name and line string.

◆ SymbolTable

struct SymbolTable
Data Fields
char magic[4]
u32 symbolCount
struct Symbol symbols[0]

Macro Definition Documentation

◆ SYMBOL_TABLE_PTR_ROM_ADDR

#define SYMBOL_TABLE_PTR_ROM_ADDR   0x18

ROM address of the pointer to the symbol table.

This particular location is an unused part of the header lol. Don't modify this without also updating append_symbol_table.py.

Definition at line 12 of file backtrace.h.

Referenced by address2symbol().

Function Documentation

◆ backtrace()

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.

Parameters
bufferEmpty array of pointers. This will be populated with pointers to the return addresses for each call frame.
sizeSize of the buffer, that is, maximum number of call frames that will be walked by the function.
Returns
Number of call frames walked (at most, size).

Definition at line 287 of file backtrace.c.

287 {
288 struct backtrace_cb_ctx ctx = {
289 buffer,
290 size,
291 -1, // skip backtrace itself
292 };
293 backtrace_foreach(backtrace_cb, &ctx);
294 return ctx.i;
295}
BSS s32 PopupMenu_SelectedIndex

Referenced by debug_backtrace().

◆ backtrace_thread()

int backtrace_thread ( void ** buffer,
int size,
OSThread * thread )

Definition at line 297 of file backtrace.c.

297 {
298 struct backtrace_cb_ctx ctx = {
299 buffer,
300 size,
301 0,
302 };
303 u32 sp = (u32)thread->context.sp;
304 u32 pc = (u32)thread->context.pc;
305 u32 fp = (u32)thread->context.s8;
306 backtrace_cb(&ctx, (void*)pc);
307 backtrace_foreach_foreign(backtrace_cb, &ctx, (uint32_t*)sp, (uint32_t*)pc, (uint32_t*)fp);
308 return ctx.i;
309}

Referenced by crash_screen_draw().

◆ debug_backtrace()

void debug_backtrace ( void )

Print a backtrace.

Definition at line 412 of file backtrace.c.

412 {
413 s32 bt[32];
414 s32 max = backtrace((void**)bt, ARRAY_COUNT(bt));
415 s32 i;
416 char buf[128];
417
418 debugf("Backtrace:\n");
419 for (i = 0; i < max; i++) {
421 debugf(" %s\n", buf);
422 }
423}
int backtrace(void **buffer, int size)
Walk the stack and return the current call stack.
Definition backtrace.c:287
#define debugf
Definition backtrace.c:45
void backtrace_address_to_string(u32 address, char *dest)
Converts a function address to a string representation using its name, offset, and file.
Definition backtrace.c:385
#define ARRAY_COUNT(arr)
Definition macros.h:40

◆ 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 385 of file backtrace.c.

385 {
386 Symbol sym;
387 s32 offset = address2symbol(address, &sym);
388
389 if (offset >= 0 && offset < 0x1000) { // 0x1000 = arbitrary func size limit
390 char name[0x40];
391 char file[0x40];
392 char* namep = load_symbol_string(name, sym.nameOffset, ARRAY_COUNT(name));
393 char* filep = load_symbol_string(file, sym.fileOffset, ARRAY_COUNT(file));
394
395 offset = 0; // Don't show offsets
396
397 if (filep == NULL)
398 if (offset == 0)
399 sprintf(dest, "%s", namep);
400 else
401 sprintf(dest, "%s+0x%X", namep, offset);
402 else
403 if (offset == 0)
404 sprintf(dest, "%s (%s)", namep, filep);
405 else
406 sprintf(dest, "%s (%s+0x%X)", namep, filep, offset);
407 } else {
408 sprintf(dest, "%p", address);
409 }
410}
char * load_symbol_string(char *dest, u32 addr, int n)
Definition backtrace.c:372
s32 address2symbol(u32 address, Symbol *out)
Uses the symbol table to look up the symbol corresponding to the given address.
Definition backtrace.c:320

Referenced by crash_screen_draw(), and debug_backtrace().