/*
* DebugSymbols.cpp
* Rocket_Man
*
* Created by John Ryland on 24/06/09.
* Copyright 2009 InvertedLogic. All rights reserved.
*
*/
#include <dlfcn.h>
#include "DebugSymbols.h"
#include <execinfo.h>
#if 0
int
backtrace(void** array, int size);
char**
backtrace_symbols(void* const* array, int size);
int
dladdr(const void* addr, Dl_info* info);
DESCRIPTION
The dladdr() function queries dyld (the dynamic linker) for information about the image con-
taining the address addr. The information is returned in the structure specified by info.
The structure contains at least the following members:
const char* dli_fname The pathname of the shared object containing the address.
void* dli_fbase The base address (mach_header) at which the image is mapped into
the address space of the calling process.
const char* dli_sname The name of the nearest run-time symbol with a value less than or
equal to addr.
void* dli_saddr The value of the symbol returned in dli_sname.
#include <execinfo.h>
#include <stdio.h>
...
void* callstack[128];
int i, frames = backtrace(callstack, 128);
char** strs = backtrace_symbols(callstack, frames);
for (i = 0; i < frames; ++i) {
printf("%s\n", strs[i]);
}
free(strs);
#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>
/* Obtain a backtrace and print it to stdout. */
void
print_trace (void)
{
void *array[10];
size_t size;
char **strings;
size_t i;
size = backtrace (array, 10);
strings = backtrace_symbols (array, size);
printf ("Obtained %zd stack frames.\n", size);
for (i = 0; i < size; i++)
printf ("%s\n", strings[i]);
free (strings);
}
/* A dummy function to make the backtrace more interesting. */
void
dummy_function (void)
{
print_trace ();
}
int
main (void)
{
dummy_function ();
return 0;
}
#endif