From cabaf67aeaf6dfad0ba153dfcacc76500a33778e Mon Sep 17 00:00:00 2001 From: noahsmartin Date: Fri, 27 Oct 2023 17:27:27 -0400 Subject: [PATCH] Lock after resume (#64) --- ETTrace/Tracer/EMGTracer.mm | 51 ++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/ETTrace/Tracer/EMGTracer.mm b/ETTrace/Tracer/EMGTracer.mm index 2791bea..2100f06 100644 --- a/ETTrace/Tracer/EMGTracer.mm +++ b/ETTrace/Tracer/EMGTracer.mm @@ -137,7 +137,7 @@ + (NSString *)deviceName { return [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding]; } -+ (Thread *) createThread:(thread_t) threadId +Thread* createThread(thread_t threadId) { Thread *thread = new Thread; @@ -174,6 +174,16 @@ + (void)recordStackForAllThreads thread_count = 1; } + std::map stackMap; + for (mach_msg_type_number_t i = 0; i < thread_count; i++) { + if (threads[i] == sETTraceThread) { + continue; + } + + Stack *stack = new Stack; + stackMap.insert(std::pair(threads[i], stack)); + } + // Suspend all threads but ETTrace's for (mach_msg_type_number_t i = 0; i < thread_count; i++) { if (threads[i] != sETTraceThread) { @@ -187,36 +197,41 @@ + (void)recordStackForAllThreads continue; } - Stack stack; - stack.time = time; - FIRCLSWriteThreadStack(threads[i], stack.frames, kMaxFramesPerStack, &(stack.frameCount)); + Stack *stack = stackMap.at(threads[i]); + stack->time = time; + FIRCLSWriteThreadStack(threads[i], stack->frames, kMaxFramesPerStack, &(stack->frameCount)); + } - std::vector *threadStack; - sThreadsLock.lock(); - if (sThreadsMap->find(threads[i]) == sThreadsMap->end()) { - Thread *thread = [self createThread:threads[i]]; + for (mach_msg_type_number_t i = 0; i < thread_count; i++) { + if (threads[i] != sETTraceThread) + thread_resume(threads[i]); + } + + std::vector *threadStack; + std::map::iterator it; + sThreadsLock.lock(); + for (it = stackMap.begin(); it != stackMap.end(); it++) { + thread_t t_id = it->first; + if (sThreadsMap->find(t_id) == sThreadsMap->end()) { + Thread *thread = createThread(t_id); // Add to hash map - sThreadsMap->insert(std::pair(threads[i], thread)); + sThreadsMap->insert(std::pair(t_id, thread)); threadStack = thread->stacks; } else { - threadStack = sThreadsMap->at(threads[i])->stacks; + threadStack = sThreadsMap->at(t_id)->stacks; } - + Stack *stack = it->second; try { - threadStack->emplace_back(stack); + threadStack->emplace_back(*stack); } catch (const std::length_error& le) { fflush(stdout); fflush(stderr); throw le; } - sThreadsLock.unlock(); - } - - for (mach_msg_type_number_t i = 0; i < thread_count; i++) { - if (threads[i] != sETTraceThread) - thread_resume(threads[i]); + delete stack; } + sThreadsLock.unlock(); } + (void)setup {