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

298 {
299 struct backtrace_cb_ctx ctx = {
300 buffer,
301 size,
302 -1, // skip backtrace itself
303 };
304 backtrace_foreach(backtrace_cb, &ctx);
305 return ctx.i;
306}

Referenced by debug_backtrace().

◆ backtrace_thread()

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

Definition at line 308 of file backtrace.c.

308 {
309 struct backtrace_cb_ctx ctx = {
310 buffer,
311 size,
312 0,
313 };
314 u32 sp = (u32)thread->context.sp;
315 u32 pc = (u32)thread->context.pc;
316 u32 fp = (u32)thread->context.s8;
317 backtrace_cb(&ctx, (void*)pc);
318 backtrace_foreach_foreign(backtrace_cb, &ctx, (uint32_t*)sp, (uint32_t*)pc, (uint32_t*)fp);
319 return ctx.i;
320}
u32 uint32_t
Definition backtrace.c:21

Referenced by crash_screen_draw().

◆ debug_backtrace()

void debug_backtrace ( void )

Print a backtrace.

Definition at line 423 of file backtrace.c.

423 {
424 s32 bt[32];
425 s32 max = backtrace((void**)bt, ARRAY_COUNT(bt));
426 s32 i;
427 char buf[128];
428
429 osSyncPrintf("Backtrace:\n");
430 for (i = 0; i < max; i++) {
431 backtrace_address_to_string(bt[i], buf);
432 osSyncPrintf(" %s\n", buf);
433 }
434}
int backtrace(void **buffer, int size)
Walk the stack and return the current call stack.
Definition backtrace.c:298
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:396
void osSyncPrintf(const char *fmt,...)
Definition is_debug.c:32
#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 396 of file backtrace.c.

396 {
397 Symbol sym;
398 s32 offset = address2symbol(address, &sym);
399
400 if (offset >= 0 && offset < 0x1000) { // 0x1000 = arbitrary func size limit
401 char name[0x40];
402 char file[0x40];
403 char* namep = load_symbol_string(name, sym.nameOffset, ARRAY_COUNT(name));
404 char* filep = load_symbol_string(file, sym.fileOffset, ARRAY_COUNT(file));
405
406 offset = 0; // Don't show offsets
407
408 if (filep == NULL)
409 if (offset == 0)
410 sprintf(dest, "%s", namep);
411 else
412 sprintf(dest, "%s+0x%X", namep, offset);
413 else
414 if (offset == 0)
415 sprintf(dest, "%s (%s)", namep, filep);
416 else
417 sprintf(dest, "%s (%s+0x%X)", namep, filep, offset);
418 } else {
419 sprintf(dest, "%p", address);
420 }
421}
char * load_symbol_string(char *dest, u32 addr, int n)
Definition backtrace.c:383
s32 address2symbol(u32 address, Symbol *out)
Uses the symbol table to look up the symbol corresponding to the given address.
Definition backtrace.c:331
u32 fileOffset
Offset of the file name and line string.
Definition backtrace.h:17
u32 nameOffset
Offset of the symbol name string.
Definition backtrace.h:16

Referenced by crash_screen_draw(), and debug_backtrace().