Skip to content

Commit

Permalink
feat: Dump Native and JS callstacks on signal
Browse files Browse the repository at this point in the history
Dump the native and JavaScript callstacks when any
of SIGABRT SIGILL, SIGSEGV, SIGFPE or SIGBUS is received.
  • Loading branch information
mbektchiev committed May 20, 2019
1 parent 949b6bb commit 6ec5d5c
Showing 1 changed file with 37 additions and 12 deletions.
49 changes: 37 additions & 12 deletions src/NativeScript/TNSRuntime.mm
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ - (double)memoryCheckInterval;

- (double)freeMemoryRatio;

+ (double)readDoubleFromPackageJsonIos: (NSDictionary*)packageJson withKey: (NSString*)key;
+ (double)readDoubleFromPackageJsonIos:(NSDictionary*)packageJson withKey:(NSString*)key;

+ (NSString*)readStringFromPackageJsonIos: (NSDictionary*)packageJson withKey: (NSString*)key;
+ (NSString*)readStringFromPackageJsonIos:(NSDictionary*)packageJson withKey:(NSString*)key;

+ (NSDictionary*)readAppPackageJson:(NSString*)applicationPath;

Expand Down Expand Up @@ -102,20 +102,45 @@ + (TNSRuntime*)runtimeForVM:(JSC::VM*)vm {
return nil;
}

void (*oldHandlers[__DARWIN_NSIG])(int) = {};
void sig_handler(int sig) {
NSLog(@"NativeScript caught signal %d.", sig);
NSLog(@"Native Stack: ");
WTFReportBacktrace();
NSLog(@"JS Stack: ");
dumpExecJsCallStack([TNSRuntime current] -> _globalObject -> globalExec());

if (oldHandlers[sig]) {
oldHandlers[sig](sig);
}

exit(-sig);
}

void install_handler(int sig) {
oldHandlers[sig] = signal(sig, sig_handler);
}

+ (void)initialize {
TNSPERF();
if (self == [TNSRuntime self]) {
WTF::initializeMainThread();
initializeThreading();

JSC::Options::useJIT() = false;
NSDictionary* packageJson = [TNSRuntime readAppPackageJson:[NSBundle mainBundle].bundlePath];
NSString *jscFlags = [TNSRuntime readStringFromPackageJsonIos:packageJson withKey:@"jscFlags"];
NSString* jscFlags = [TNSRuntime readStringFromPackageJsonIos:packageJson withKey:@"jscFlags"];
if (jscFlags != nil) {
JSC::Options::setOptions([jscFlags cStringUsingEncoding:NSUTF8StringEncoding]);
}

_runtimes = [[NSPointerArray alloc] initWithOptions:NSPointerFunctionsOpaquePersonality | NSPointerFunctionsOpaqueMemory];

install_handler(SIGABRT);
install_handler(SIGILL);
install_handler(SIGSEGV);
install_handler(SIGFPE);
install_handler(SIGBUS);
}
}

Expand Down Expand Up @@ -226,7 +251,7 @@ - (JSValueRef)convertObject:(id)object {
+ (NSDictionary*)readAppPackageJson:(NSString*)applicationPath {
NSString* packageJsonPath = [applicationPath stringByAppendingPathComponent:@"app/package.json"];
NSData* data = [NSData dataWithContentsOfFile:packageJsonPath];
NSDictionary *res = @{};
NSDictionary* res = @{};
if (data) {
NSError* error = nil;
res = [[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error] retain];
Expand All @@ -240,7 +265,7 @@ - (NSDictionary*)appPackageJson {
return self->appPackageJsonData;
}

self->appPackageJsonData = [TNSRuntime readAppPackageJson:self->_applicationPath] ;
self->appPackageJsonData = [TNSRuntime readAppPackageJson:self->_applicationPath];
return self->appPackageJsonData;
}

Expand Down Expand Up @@ -277,9 +302,9 @@ - (double)freeMemoryRatio {
return *self->freeMemoryRatioValue;
}

+ (double)readDoubleFromPackageJsonIos: (NSDictionary*)packageJson withKey: (NSString*)key {
+ (double)readDoubleFromPackageJsonIos:(NSDictionary*)packageJson withKey:(NSString*)key {
double res = 0;
if (packageJson){
if (packageJson) {
if (NSDictionary* ios = packageJson[@"ios"]) {
if (id value = ios[key]) {
if ([value respondsToSelector:@selector(doubleValue)]) {
Expand All @@ -294,16 +319,16 @@ + (double)readDoubleFromPackageJsonIos: (NSDictionary*)packageJson withKey: (NSS
return res;
}

+ (NSString*)readStringFromPackageJsonIos:(NSDictionary*)packageJson withKey: (NSString*)key {
NSString *res = nil;
+ (NSString*)readStringFromPackageJsonIos:(NSDictionary*)packageJson withKey:(NSString*)key {
NSString* res = nil;
if (packageJson) {
if (NSDictionary* ios = packageJson[@"ios"]) {
if (id value = ios[key]) {
res = value;
}
}
}

return res;
}

Expand Down

0 comments on commit 6ec5d5c

Please sign in to comment.