Skip to content

Commit 7d9e784

Browse files
Raise max captured thread count to 1000 (kstenerud#664)
* Raise max captured thread count to 1000 * Use MAX_CAPTURED_THREADS in unit tests
1 parent 3a380ea commit 7d9e784

File tree

6 files changed

+28
-10
lines changed

6 files changed

+28
-10
lines changed

Samples/Common/Sources/CrashTriggers/KSCrashTriggersList.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ + (void)trigger_signal_abort
9595

9696
+ (void)trigger_other_manyThreads
9797
{
98-
NSUInteger const threadsCount = 200;
98+
NSUInteger const threadsCount = 1005;
9999
static NSMutableArray *allThreads = [NSMutableArray arrayWithCapacity:threadsCount + 1];
100100

101101
for (NSUInteger idx = 0; idx < threadsCount; ++idx) {

Sources/KSCrashRecording/KSCrashReportStoreC.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ int kscrs_getReportIDs(int64_t *reportIDs, int count, const KSCrashReportStoreCC
209209
static char *readReportAtPath(const char *path)
210210
{
211211
char *rawReport;
212-
ksfu_readEntireFile(path, &rawReport, NULL, 2000000);
212+
ksfu_readEntireFile(path, &rawReport, NULL, 20000000);
213213
if (rawReport == NULL) {
214214
KSLOG_ERROR("Failed to load report at path: %s", path);
215215
return NULL;

Sources/KSCrashRecordingCore/KSJSONCodecObjC.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ + (NSData *)encode:(id)object options:(KSJSONEncodeOption)encodeOptions error:(N
411411
+ (id)decode:(NSData *)JSONData options:(KSJSONDecodeOption)decodeOptions error:(NSError *__autoreleasing *)error
412412
{
413413
KSJSONCodec *codec = [self codecWithEncodeOptions:0 decodeOptions:decodeOptions];
414-
NSMutableData *stringData = [NSMutableData dataWithLength:10001];
414+
NSMutableData *stringData = [NSMutableData dataWithLength:20000000];
415415
int errorOffset;
416416
int result = ksjson_decode(JSONData.bytes, (int)JSONData.length, stringData.mutableBytes, (int)stringData.length,
417417
codec.callbacks, (__bridge void *)codec, &errorOffset);

Sources/KSCrashRecordingCore/KSMachineContext.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,16 @@ static inline bool getThreadList(KSMachineContext *context)
8383
}
8484
KSLOG_TRACE("Got %d threads", context->threadCount);
8585
mach_msg_type_number_t threadCount = actualThreadCount;
86-
mach_msg_type_number_t maxThreadCount = sizeof(context->allThreads) / sizeof(context->allThreads[0]);
87-
if (threadCount > maxThreadCount) {
88-
KSLOG_ERROR("Thread count %d is higher than maximum of %d", threadCount, maxThreadCount);
89-
for (mach_msg_type_number_t idx = maxThreadCount; idx < threadCount; ++idx) {
86+
if (threadCount > MAX_CAPTURED_THREADS) {
87+
KSLOG_ERROR("Thread count %d is higher than maximum of %d", threadCount, MAX_CAPTURED_THREADS);
88+
for (mach_msg_type_number_t idx = MAX_CAPTURED_THREADS; idx < threadCount; ++idx) {
9089
if (threads[idx] == context->thisThread) {
9190
// If crashed thread is outside of threads limit we place it at the end of the list
92-
threads[maxThreadCount - 1] = threads[idx];
91+
threads[MAX_CAPTURED_THREADS - 1] = threads[idx];
9392
break;
9493
}
9594
}
96-
threadCount = maxThreadCount;
95+
threadCount = MAX_CAPTURED_THREADS;
9796
}
9897
for (mach_msg_type_number_t i = 0; i < threadCount; i++) {
9998
context->allThreads[i] = threads[i];

Sources/KSCrashRecordingCore/include/KSMachineContext_Apple.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
extern "C" {
3838
#endif
3939

40+
#define MAX_CAPTURED_THREADS 1000
41+
4042
#ifdef __arm64__
4143
#define STRUCT_MCONTEXT_L _STRUCT_MCONTEXT64
4244
#else
@@ -45,7 +47,7 @@ extern "C" {
4547

4648
typedef struct KSMachineContext {
4749
thread_t thisThread;
48-
thread_t allThreads[100];
50+
thread_t allThreads[MAX_CAPTURED_THREADS];
4951
int threadCount;
5052
bool isCrashedContext;
5153
bool isCurrentThread;

Tests/KSCrashRecordingCoreTests/KSMachineContext_Tests.m

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,21 @@ - (void)testSuspendResumeThreads
4747
ksmc_resumeEnvironment(threads1, numThreads1);
4848
}
4949

50+
- (void)startTheBackgroundJob
51+
{
52+
sleep(5);
53+
}
54+
55+
- (void)testMaxThreadsInContext
56+
{
57+
KSMachineContext machineContext = { 0 };
58+
int threadsToCreate = MAX_CAPTURED_THREADS + 5;
59+
for (int i = 0; i < threadsToCreate; ++i) {
60+
[NSThread detachNewThreadSelector:@selector(startTheBackgroundJob) toTarget:self withObject:nil];
61+
}
62+
63+
ksmc_getContextForThread(ksthread_self(), &machineContext, true);
64+
XCTAssertEqual(machineContext.threadCount, MAX_CAPTURED_THREADS);
65+
}
66+
5067
@end

0 commit comments

Comments
 (0)