Created
March 31, 2021 22:31
-
-
Save GeorgeLyon/1cc0242c003bb948233bc912f998820c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#include <execinfo.h> | |
#include <inttypes.h> | |
#include <mach-o/dyld.h> | |
#include <mach-o/getsect.h> | |
#include <stdint.h> | |
#include <stdio.h> | |
#include <string.h> | |
#define POINTER "0x%016" PRIxPTR | |
intptr_t base_address() { | |
const struct segment_command_64* text_segment = getsegbyname("__TEXT"); | |
return (uintptr_t)text_segment->vmaddr; | |
} | |
uintptr_t image_slide() { | |
char path[1024]; | |
uint32_t size = sizeof(path); | |
if (_NSGetExecutablePath(path, &size) != 0) return -1; | |
uintptr_t result = 0; | |
for (uint32_t i = 0; i < _dyld_image_count(); i++) { | |
const char * name = _dyld_get_image_name(i); | |
uintptr_t slide = _dyld_get_image_vmaddr_slide(i); | |
if (strcmp(name, path) == 0) result = slide; | |
// printf(POINTER ": %s\n", slide, name); | |
} | |
return result; | |
} | |
void print_backtrace() { | |
int buffer_size = 10; | |
void *bt[buffer_size]; | |
int bt_count = backtrace(bt, buffer_size); | |
for (int i = 0; i < bt_count; i++) { | |
uintptr_t return_address = (uintptr_t)bt[i]; | |
uintptr_t symbol_address = return_address - image_slide(); | |
printf("%d: " POINTER " (" POINTER ") - ", i, return_address, symbol_address); | |
fflush(stdout); | |
backtrace_symbols_fd(bt + i, 1, fileno(stdout)); | |
} | |
} | |
int main() { | |
printf("__TEXT: " POINTER "\n", base_address()); | |
print_backtrace(); | |
return 0; | |
} | |
// Debug info can be accessed using a dSYM like so: dwarfdump <path-to-dSYM> --lookup=<symbol_address> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment