macOS: Add logging for unhandled NSExceptions

This commit is contained in:
UnknownShadow200 2023-06-12 21:44:04 +10:00
parent cca8ff64a3
commit 0498a2a748
2 changed files with 40 additions and 2 deletions

View File

@ -994,8 +994,20 @@ void Logger_Hook(void) {
}
#elif defined CC_BUILD_POSIX
static const char* SignalDescribe(int type) {
switch (type) {
case SIGSEGV: return "SIGSEGV";
case SIGBUS: return "SIGBUS";
case SIGILL: return "SIGILL";
case SIGABRT: return "SIGABRT";
case SIGFPE: return "SIGFPE";
}
return NULL;
}
static void SignalHandler(int sig, siginfo_t* info, void* ctx) {
cc_string msg; char msgBuffer[128 + 1];
const char* desc;
int type, code;
cc_uintptr addr;
@ -1009,9 +1021,14 @@ static void SignalHandler(int sig, siginfo_t* info, void* ctx) {
type = info->si_signo;
code = info->si_code;
addr = (cc_uintptr)info->si_addr;
desc = SignalDescribe(type);
String_InitArray_NT(msg, msgBuffer);
String_Format3(&msg, "Unhandled signal %i (code %i) at %x", &type, &code, &addr);
if (desc) {
String_Format3(&msg, "Unhandled signal %c (code %i) at %x", desc, &code, &addr);
} else {
String_Format3(&msg, "Unhandled signal %i (code %i) at %x", &type, &code, &addr);
}
msg.buffer[msg.length] = '\0';
#if defined CC_BUILD_ANDROID

View File

@ -19,7 +19,7 @@ static cc_bool scroll_debugging;
*---------------------------------------------------Shared with Carbon----------------------------------------------------*
*#########################################################################################################################*/
extern size_t CGDisplayBitsPerPixel(CGDirectDisplayID display);
// TODO: Try NSBitsPerPixelFromDepth([NSScreen mainScreen].depth) instead
// TODO: Try replacing with NSBitsPerPixelFromDepth([NSScreen mainScreen].depth) instead
// NOTE: If code here is changed, don't forget to update corresponding code in Window_Carbon.c
static void Window_CommonInit(void) {
@ -128,8 +128,29 @@ void Clipboard_SetText(const cc_string* value) {
[pasteboard setString:str forType:NSStringPboardType];
}
static void LogUnhandled(NSString* str) {
if (!str) return;
const char* src = [str UTF8String];
if (!src) return;
cc_string msg = String_FromReadonly(src);
Platform_Log(msg.buffer, msg.length);
Logger_Log(&msg);
}
// TODO: Should really be handled elsewhere, in Logger or ErrorHandler
static void LogUnhandledNSErrors(NSException* ex) {
// last chance to log exception details before process dies
LogUnhandled(@"About to die from unhandled NSException..");
LogUnhandled([ex name]);
LogUnhandled([ex reason]);
}
static NSAutoreleasePool* pool;
void Window_Init(void) {
NSSetUncaughtExceptionHandler(LogUnhandledNSErrors);
// https://www.cocoawithlove.com/2009/01/demystifying-nsapplication-by.html
pool = [[NSAutoreleasePool alloc] init];
appHandle = [NSApplication sharedApplication];