recover from exception on iOS and macOS using signals and log the stacktrace

This commit is contained in:
2018-10-04 15:26:18 +02:00
parent f45eefe433
commit 55dbab498f
7 changed files with 53 additions and 64 deletions

View File

@@ -1,12 +1,12 @@
#include "pch.h"
#include "objc_utils.h"
#include "log.h"
#include "app.h"
#include <atomic>
#ifdef __OBJC__
@implementation PathWithModDate
@end
@implementation ObjcUtils
+ (NSArray*)getFilesAtPathSortedByModificationDate:(NSString*)folderPath
{
@@ -34,3 +34,42 @@
}
@end
#endif
std::string base_address;
std::atomic_flag handler_executed;
void exception_handler(NSException *exception)
{
if (handler_executed.test_and_set())
exit(0);
LOG("exception %s\n", [[exception name] cStringUsingEncoding:NSUTF8StringEncoding]);
NSString* callstack = [[NSThread callStackSymbols] componentsJoinedByString:@"\n"];
LOG("base:\n%s\ncallstack:\n%s", base_address.c_str(), [callstack cStringUsingEncoding:NSUTF8StringEncoding]);
if (App::I.canvas && App::I.canvas->m_canvas)
App::I.canvas->m_canvas->project_save_thread(App::I.data_path + "/recovery.ppi");
exit(0);
}
static void signal_handler (int signo) {
if (handler_executed.test_and_set())
exit(0);
LOG("signal %d\n", signo);
NSString* callstack = [[NSThread callStackSymbols] componentsJoinedByString:@"\n"];
LOG("base:\n%s\ncallstack:\n%s", base_address.c_str(), [callstack cStringUsingEncoding:NSUTF8StringEncoding]);
if (App::I.canvas && App::I.canvas->m_canvas)
App::I.canvas->m_canvas->project_save_thread(App::I.data_path + "/recovery.ppi");
exit(0);
}
void install_global_handlers()
{
NSString* callstack = [[NSThread callStackSymbols] componentsJoinedByString:@"\n"];
base_address = [callstack cStringUsingEncoding:NSUTF8StringEncoding];
NSSetUncaughtExceptionHandler(exception_handler);
signal(SIGABRT, signal_handler);
signal(SIGILL, signal_handler);
signal(SIGSEGV, signal_handler);
signal(SIGFPE, signal_handler);
signal(SIGBUS, signal_handler);
signal(SIGPIPE, signal_handler);
}

View File

@@ -1,8 +1,6 @@
#pragma once
#ifdef __OBJC__
@interface PathWithModDate : NSObject
@property (strong) NSString *path;
@property (strong) NSDate *modDate;
@@ -10,6 +8,6 @@
@interface ObjcUtils : NSObject
@end
#endif
void install_global_handlers();

View File

@@ -17,7 +17,6 @@
#import <AVFoundation/AVFoundation.h>
#import <UIKit/UIKit.h>
#import <GLKit/GLKit.h>
#import <ZipArchive/ZipArchive.h>
#endif
#include <OpenGLES/ES3/gl.h>
#include <OpenGLES/ES3/glext.h>
@@ -31,7 +30,6 @@
#import <AVFoundation/AVFoundation.h>
#import <UIKit/UIKit.h>
#import <GLKit/GLKit.h>
#import <ZipArchive/ZipArchive.h>
#endif
#include <OpenGLES/ES3/gl.h>
#include <OpenGLES/ES3/glext.h>
@@ -42,7 +40,6 @@
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
#import <ZipArchive/ZipArchive.h>
#endif
#include <OpenGL/gl3.h>
#include <OpenGL/gl3ext.h>