Skip to content

Commit

Permalink
Added support for SIGTERM (#472)
Browse files Browse the repository at this point in the history
* Added support for SIGTERM

* Update KSCrashDoctor_Tests.m

* created KSTestModuleConfig

Used for sharing the defines for SWIFTPM_MODULE_BUNDLE.

* Update KSTestModuleConfig.h

* Removed include folder in tests
  • Loading branch information
naftaly committed May 9, 2024
1 parent a3c5059 commit 61e396d
Show file tree
Hide file tree
Showing 7 changed files with 2,641 additions and 6 deletions.
9 changes: 9 additions & 0 deletions Sources/KSCrashRecording/KSCrashDoctor.m
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,11 @@ - (NSString*) appendOriginatingCall:(NSString*) string callName:(NSString*) call
return string;
}

- (BOOL) isGracefulTerminationRequest:(NSDictionary *)report
{
return [report[@KSCrashField_Signal][@KSCrashField_Signal] integerValue] == SIGTERM;
}

- (NSString*) diagnoseCrash:(NSDictionary*) report
{
@try
Expand Down Expand Up @@ -558,6 +563,10 @@ - (NSString*) diagnoseCrash:(NSDictionary*) report
callName:lastFunctionName];
}

if([self isGracefulTerminationRequest:errorReport]) {
return @"The OS request the app be gracefully terminated.";
}

return nil;
}
@catch (NSException* e)
Expand Down
2 changes: 2 additions & 0 deletions Sources/KSCrashRecordingCore/KSSignalInfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ static const KSSignalInfo g_fatalSignalData[] =
SIGNAL_INFO(SIGSEGV, g_sigSegVCodes),
SIGNAL_INFO_NOCODES(SIGSYS),
SIGNAL_INFO(SIGTRAP, g_sigTrapCodes),
SIGNAL_INFO_NOCODES(SIGTERM),
};
static const int g_fatalSignalsCount = sizeof(g_fatalSignalData) / sizeof(*g_fatalSignalData);

Expand All @@ -131,6 +132,7 @@ static const int g_fatalSignals[] =
SIGSEGV,
SIGSYS,
SIGTRAP,
SIGTERM,
};

const char* kssignal_signalName(const int sigNum)
Expand Down
24 changes: 24 additions & 0 deletions Tests/KSCrashRecordingTests/KSCrashDoctor_Tests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#import <XCTest/XCTest.h>

#import "KSCrashDoctor.h"
#import "KSTestModuleConfig.h"

@interface KSCrashDoctor_Tests : XCTestCase @end

@implementation KSCrashDoctor_Tests

- (NSDictionary<NSString *, id> *)_crashReportAsJSON:(NSString *)filename
{
NSURL *url = [KS_TEST_MODULE_BUNDLE URLForResource:filename withExtension:@"json"];
NSData *data = [NSData dataWithContentsOfURL:url];
return [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
}

- (void) testGracefulTermination
{
id report = [self _crashReportAsJSON:@"sigterm"];
NSString *diagnostic = [[[KSCrashDoctor alloc] init] diagnoseCrash:report];
XCTAssertEqual(diagnostic, @"The OS request the app be gracefully terminated.");
}

@end
7 changes: 1 addition & 6 deletions Tests/KSCrashRecordingTests/KSCrashReportFixer_Tests.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@

#import <XCTest/XCTest.h>
#import "KSCrashReportFixer.h"

#ifdef SWIFTPM_MODULE_BUNDLE
#define KS_TEST_MODULE_BUNDLE SWIFTPM_MODULE_BUNDLE
#else
#define KS_TEST_MODULE_BUNDLE ([NSBundle bundleForClass:[self class]])
#endif
#import "KSTestModuleConfig.h"

@interface KSCrashReportFixer_Tests : XCTestCase

Expand Down
21 changes: 21 additions & 0 deletions Tests/KSCrashRecordingTests/KSTestModuleConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// KSTestModuleConfig.h
//
//
// Created by Alexander Cohen on 5/9/24.
//

#ifndef KSTestModuleConfig_h
#define KSTestModuleConfig_h

#import <XCTest/XCTest.h>

#if !defined(KS_TEST_MODULE_BUNDLE)
#ifdef SWIFTPM_MODULE_BUNDLE
#define KS_TEST_MODULE_BUNDLE SWIFTPM_MODULE_BUNDLE
#else
#define KS_TEST_MODULE_BUNDLE ([NSBundle bundleForClass:[self class]])
#endif
#endif

#endif // KSTestModuleConfig_h
Loading

0 comments on commit 61e396d

Please sign in to comment.