Unwind_Backtrace.c [plain text]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
#include "unwind.h"
extern int foo();
static const char* expected[] = { "bar", "foo", "main" };
static int step = 0;
static _Unwind_Reason_Code handler(struct _Unwind_Context* context, void* ref)
{
if ( step > 2 )
return _URC_NORMAL_STOP;
struct dl_info dyldInfo;
if ( dladdr((void*)_Unwind_GetIP(context), &dyldInfo) ) {
if ( strcmp(dyldInfo.dli_sname, expected[step]) != 0 ) {
fprintf(stderr, "unexpected frame %s\n", dyldInfo.dli_sname);
exit(1);
}
++step;
}
else {
exit(1);
}
return _URC_NO_REASON;
}
int bar()
{
_Unwind_Backtrace(&handler, NULL);
return (step == 2);
}
int main()
{
return foo();
}