Skip to content

Commit

Permalink
Lock after resume (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
noahsmartin authored Oct 27, 2023
1 parent 8d1061e commit cabaf67
Showing 1 changed file with 33 additions and 18 deletions.
51 changes: 33 additions & 18 deletions ETTrace/Tracer/EMGTracer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -174,6 +174,16 @@ + (void)recordStackForAllThreads
thread_count = 1;
}

std::map<thread_t, Stack *> 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<unsigned int, Stack *>(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) {
Expand All @@ -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<Stack> *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<Stack> *threadStack;
std::map<thread_t, Stack *>::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<unsigned int, Thread *>(threads[i], thread));
sThreadsMap->insert(std::pair<thread_t, Thread *>(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 {
Expand Down

0 comments on commit cabaf67

Please sign in to comment.