-
-
Save ArnCarveris/09982554d454f9460790029e79e7e7ce to your computer and use it in GitHub Desktop.
Test of backtrace and dladdr with C++ lambdas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// c++ -framework Foundation -std=c++14 -o dladdr-lambda-test dladdr-lambda-test.mm | |
#import <Foundation/Foundation.h> | |
#include <cxxabi.h> | |
#include <dlfcn.h> | |
#include <execinfo.h> | |
@interface MyObject : NSObject | |
@end | |
@implementation MyObject | |
+ (void)doStuff | |
{ | |
[] { | |
void* callstack[128]; | |
int i, frames = backtrace(callstack, 128); | |
for (i = 0; i < frames; ++i) { | |
if ((intptr_t)callstack[i] == 1) | |
continue; | |
const char* mangledName; | |
const char* demangledName; | |
Dl_info info; | |
if (dladdr(callstack[i], &info) && info.dli_sname) | |
mangledName = info.dli_sname; | |
if (mangledName) | |
demangledName = abi::__cxa_demangle(mangledName, nullptr, nullptr, nullptr); | |
if (mangledName || demangledName) | |
printf("0x%016lx %s + %ld\n", (intptr_t)callstack[i], demangledName ? demangledName : mangledName, (intptr_t)callstack[i] - (intptr_t)info.dli_saddr); | |
else | |
printf("?\n"); | |
} | |
}(); | |
} | |
@end | |
int main(int argc, char** argv) | |
{ | |
[MyObject doStuff]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment