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

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

Referenced by debug_backtrace().

◆ backtrace_thread()

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

Definition at line 301 of file backtrace.c.

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

Referenced by crash_screen_draw().

◆ debug_backtrace()

void debug_backtrace ( void )

Print a backtrace.

Definition at line 416 of file backtrace.c.

416 {
417 s32 bt[32];
418 s32 max = backtrace((void**)bt, ARRAY_COUNT(bt));
419 s32 i;
420 char buf[128];
421
422 debugf("Backtrace:\n");
423 for (i = 0; i < max; i++) {
425 debugf(" %s\n", buf);
426 }
427}
int backtrace(void **buffer, int size)
Walk the stack and return the current call stack.
Definition backtrace.c:291
#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:389
#define ARRAY_COUNT(arr)
Definition macros.h:39

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

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

Referenced by crash_screen_draw(), debug_backtrace(), and evt_execute_next_command().