TEST(libbacktrace, thread_multiple_dump) { // Dump NUM_THREADS simultaneously. std::vector<thread_t> runners(NUM_THREADS); std::vector<dump_thread_t> dumpers(NUM_THREADS); pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); for (size_t i = 0; i < NUM_THREADS; i++) { // Launch the runners, they will spin in hard loops doing nothing. runners[i].tid = 0; runners[i].state = 0; ASSERT_TRUE(pthread_create(&runners[i].threadId, &attr, ThreadMaxRun, &runners[i]) == 0); } // Wait for tids to be set. for (std::vector<thread_t>::iterator it = runners.begin(); it != runners.end(); ++it) { ASSERT_TRUE(WaitForNonZero(&it->state, 10)); } // Start all of the dumpers at once, they will spin until they are signalled // to begin their dump run. int32_t dump_now = 0; for (size_t i = 0; i < NUM_THREADS; i++) { dumpers[i].thread.tid = runners[i].tid; dumpers[i].thread.state = 0; dumpers[i].done = 0; dumpers[i].now = &dump_now; ASSERT_TRUE(pthread_create(&dumpers[i].thread.threadId, &attr, ThreadDump, &dumpers[i]) == 0); } // Start all of the dumpers going at once. android_atomic_acquire_store(1, &dump_now); for (size_t i = 0; i < NUM_THREADS; i++) { ASSERT_TRUE(WaitForNonZero(&dumpers[i].done, 10)); // Tell the runner thread to exit its infinite loop. android_atomic_acquire_store(0, &runners[i].state); ASSERT_TRUE(dumpers[i].backtrace != NULL); VerifyMaxDump(dumpers[i].backtrace); delete dumpers[i].backtrace; dumpers[i].backtrace = NULL; } }
TEST(libbacktrace, thread_ignore_frames) { pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); thread_t thread_data = { 0, 0, 0 }; pthread_t thread; ASSERT_TRUE(pthread_create(&thread, &attr, ThreadLevelRun, &thread_data) == 0); // Wait up to 2 seconds for the tid to be set. ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2)); UniquePtr<Backtrace> all(Backtrace::Create(getpid(), thread_data.tid)); ASSERT_TRUE(all.get() != NULL); ASSERT_TRUE(all->Unwind(0)); UniquePtr<Backtrace> ign1(Backtrace::Create(getpid(), thread_data.tid)); ASSERT_TRUE(ign1.get() != NULL); ASSERT_TRUE(ign1->Unwind(1)); UniquePtr<Backtrace> ign2(Backtrace::Create(getpid(), thread_data.tid)); ASSERT_TRUE(ign2.get() != NULL); ASSERT_TRUE(ign2->Unwind(2)); VerifyIgnoreFrames(all.get(), ign1.get(), ign2.get(), NULL); // Tell the thread to exit its infinite loop. android_atomic_acquire_store(0, &thread_data.state); }
TEST(libbacktrace, thread_level_trace) { pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); thread_t thread_data = { 0, 0, 0 }; pthread_t thread; ASSERT_TRUE(pthread_create(&thread, &attr, ThreadLevelRun, &thread_data) == 0); // Wait up to 2 seconds for the tid to be set. ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2)); // Save the current signal action and make sure it is restored afterwards. struct sigaction cur_action; ASSERT_TRUE(sigaction(SIGURG, NULL, &cur_action) == 0); UniquePtr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid)); ASSERT_TRUE(backtrace.get() != NULL); ASSERT_TRUE(backtrace->Unwind(0)); VerifyLevelDump(backtrace.get()); // Tell the thread to exit its infinite loop. android_atomic_acquire_store(0, &thread_data.state); // Verify that the old action was restored. struct sigaction new_action; ASSERT_TRUE(sigaction(SIGURG, NULL, &new_action) == 0); EXPECT_EQ(cur_action.sa_sigaction, new_action.sa_sigaction); EXPECT_EQ(cur_action.sa_flags, new_action.sa_flags); }
TEST(libbacktrace, thread_ignore_frames) { pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); thread_t thread_data = { 0, 0, 0 }; pthread_t thread; ASSERT_TRUE(pthread_create(&thread, &attr, ThreadLevelRun, &thread_data) == 0); // Wait up to 2 seconds for the tid to be set. ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2)); backtrace_context_t all; ASSERT_TRUE(backtrace_create_context(&all, getpid(), thread_data.tid, 0)); backtrace_context_t ign1; ASSERT_TRUE(backtrace_create_context(&ign1, getpid(), thread_data.tid, 1)); backtrace_context_t ign2; ASSERT_TRUE(backtrace_create_context(&ign2, getpid(), thread_data.tid, 2)); VerifyIgnoreFrames(all.backtrace, ign1.backtrace, ign2.backtrace, NULL); backtrace_destroy_context(&all); backtrace_destroy_context(&ign1); backtrace_destroy_context(&ign2); // Tell the thread to exit its infinite loop. android_atomic_acquire_store(0, &thread_data.state); }
TEST(libbacktrace, thread_max_trace) { pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); thread_t thread_data = { 0, 0, 0 }; pthread_t thread; ASSERT_TRUE(pthread_create(&thread, &attr, ThreadMaxRun, &thread_data) == 0); // Wait for the tid to be set. ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2)); UniquePtr<Backtrace> backtrace(Backtrace::Create(getpid(), thread_data.tid)); ASSERT_TRUE(backtrace.get() != NULL); ASSERT_TRUE(backtrace->Unwind(0)); VerifyMaxDump(backtrace.get()); // Tell the thread to exit its infinite loop. android_atomic_acquire_store(0, &thread_data.state); }
TEST(libbacktrace, thread_max_trace) { pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); thread_t thread_data = { 0, 0, 0 }; pthread_t thread; ASSERT_TRUE(pthread_create(&thread, &attr, ThreadMaxRun, &thread_data) == 0); // Wait for the tid to be set. ASSERT_TRUE(WaitForNonZero(&thread_data.state, 2)); backtrace_context_t context; ASSERT_TRUE(backtrace_create_context(&context, getpid(), thread_data.tid, 0)); VerifyMaxDump(context.backtrace); backtrace_destroy_context(&context); // Tell the thread to exit its infinite loop. android_atomic_acquire_store(0, &thread_data.state); }