Skip to content

Instantly share code, notes, and snippets.

@saagarjha
Created February 7, 2023 08:34
Show Gist options
  • Save saagarjha/13f04d732ad7f8d3f8b1699632ac197d to your computer and use it in GitHub Desktop.
Save saagarjha/13f04d732ad7f8d3f8b1699632ac197d to your computer and use it in GitHub Desktop.
Poor man's replacement for abort_report_np (https://twitter.com/mraleph/status/1622598001855287300)
// Usage should be fairly self-explanatory, just paste this in a header and use
// CRASH_WITH_MESSAGE("foobar") in your function.
// Example backtrace:
// Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
// 0 ??? 0x1022d8000 CRASHING IN test.c:20 (foobar) + 0
// 1 a.out 0x1022d7f60 main + 24
// 2 dyld 0x195f07e50 start + 2544
#define STRINGIFY(a) #a
#define CRASH_FUNCTION_NAME(file, line, message) "CRASHING IN " file ":" STRINGIFY(line) " (" message ")"
#define _CRASH_VARIABLE_NAME(message, line) message##line
#define CRASH_VARIABLE_NAME(line) _CRASH_VARIABLE_NAME(__crash_variable, line)
#define CRASH_WITH_MESSAGE(message) do { \
static char CRASH_VARIABLE_NAME(__LINE__) __asm__(CRASH_FUNCTION_NAME(__FILE__, __LINE__, message)); \
((void (*)(void))&CRASH_VARIABLE_NAME(__LINE__))(); \
} while (0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment