xref: /aosp_15_r20/system/unwinding/libunwindstack/ThreadEntry.cpp (revision eb293b8f56ee8303637c5595cfcdeef8039e85c6)
1*eb293b8fSAndroid Build Coastguard Worker /*
2*eb293b8fSAndroid Build Coastguard Worker  * Copyright (C) 2015 The Android Open Source Project
3*eb293b8fSAndroid Build Coastguard Worker  *
4*eb293b8fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*eb293b8fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*eb293b8fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*eb293b8fSAndroid Build Coastguard Worker  *
8*eb293b8fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*eb293b8fSAndroid Build Coastguard Worker  *
10*eb293b8fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*eb293b8fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*eb293b8fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*eb293b8fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*eb293b8fSAndroid Build Coastguard Worker  * limitations under the License.
15*eb293b8fSAndroid Build Coastguard Worker  */
16*eb293b8fSAndroid Build Coastguard Worker 
17*eb293b8fSAndroid Build Coastguard Worker #include <pthread.h>
18*eb293b8fSAndroid Build Coastguard Worker #include <stdint.h>
19*eb293b8fSAndroid Build Coastguard Worker #include <string.h>
20*eb293b8fSAndroid Build Coastguard Worker #include <sys/types.h>
21*eb293b8fSAndroid Build Coastguard Worker #include <time.h>
22*eb293b8fSAndroid Build Coastguard Worker #include <ucontext.h>
23*eb293b8fSAndroid Build Coastguard Worker 
24*eb293b8fSAndroid Build Coastguard Worker #include <chrono>
25*eb293b8fSAndroid Build Coastguard Worker 
26*eb293b8fSAndroid Build Coastguard Worker #include <unwindstack/Log.h>
27*eb293b8fSAndroid Build Coastguard Worker 
28*eb293b8fSAndroid Build Coastguard Worker #include "ThreadEntry.h"
29*eb293b8fSAndroid Build Coastguard Worker 
30*eb293b8fSAndroid Build Coastguard Worker namespace unwindstack {
31*eb293b8fSAndroid Build Coastguard Worker 
32*eb293b8fSAndroid Build Coastguard Worker std::mutex ThreadEntry::entries_mutex_;
33*eb293b8fSAndroid Build Coastguard Worker std::map<pid_t, ThreadEntry*> ThreadEntry::entries_;
34*eb293b8fSAndroid Build Coastguard Worker 
35*eb293b8fSAndroid Build Coastguard Worker // Assumes that ThreadEntry::entries_mutex_ has already been locked before
36*eb293b8fSAndroid Build Coastguard Worker // creating a ThreadEntry object.
ThreadEntry(pid_t tid)37*eb293b8fSAndroid Build Coastguard Worker ThreadEntry::ThreadEntry(pid_t tid) : tid_(tid), ref_count_(1), wait_value_(0) {
38*eb293b8fSAndroid Build Coastguard Worker   // Add ourselves to the global list.
39*eb293b8fSAndroid Build Coastguard Worker   entries_[tid_] = this;
40*eb293b8fSAndroid Build Coastguard Worker }
41*eb293b8fSAndroid Build Coastguard Worker 
Get(pid_t tid,bool create)42*eb293b8fSAndroid Build Coastguard Worker ThreadEntry* ThreadEntry::Get(pid_t tid, bool create) {
43*eb293b8fSAndroid Build Coastguard Worker   ThreadEntry* entry = nullptr;
44*eb293b8fSAndroid Build Coastguard Worker 
45*eb293b8fSAndroid Build Coastguard Worker   std::lock_guard<std::mutex> guard(entries_mutex_);
46*eb293b8fSAndroid Build Coastguard Worker   auto iter = entries_.find(tid);
47*eb293b8fSAndroid Build Coastguard Worker   if (iter == entries_.end()) {
48*eb293b8fSAndroid Build Coastguard Worker     if (create) {
49*eb293b8fSAndroid Build Coastguard Worker       entry = new ThreadEntry(tid);
50*eb293b8fSAndroid Build Coastguard Worker     }
51*eb293b8fSAndroid Build Coastguard Worker   } else {
52*eb293b8fSAndroid Build Coastguard Worker     entry = iter->second;
53*eb293b8fSAndroid Build Coastguard Worker     entry->ref_count_++;
54*eb293b8fSAndroid Build Coastguard Worker   }
55*eb293b8fSAndroid Build Coastguard Worker 
56*eb293b8fSAndroid Build Coastguard Worker   return entry;
57*eb293b8fSAndroid Build Coastguard Worker }
58*eb293b8fSAndroid Build Coastguard Worker 
Remove(ThreadEntry * entry)59*eb293b8fSAndroid Build Coastguard Worker void ThreadEntry::Remove(ThreadEntry* entry) {
60*eb293b8fSAndroid Build Coastguard Worker   entry->Unlock();
61*eb293b8fSAndroid Build Coastguard Worker 
62*eb293b8fSAndroid Build Coastguard Worker   std::lock_guard<std::mutex> guard(entries_mutex_);
63*eb293b8fSAndroid Build Coastguard Worker   if (--entry->ref_count_ == 0) {
64*eb293b8fSAndroid Build Coastguard Worker     delete entry;
65*eb293b8fSAndroid Build Coastguard Worker   }
66*eb293b8fSAndroid Build Coastguard Worker }
67*eb293b8fSAndroid Build Coastguard Worker 
68*eb293b8fSAndroid Build Coastguard Worker // Assumes that ThreadEntry::entries_mutex_ has already been locked before
69*eb293b8fSAndroid Build Coastguard Worker // deleting a ThreadEntry object.
~ThreadEntry()70*eb293b8fSAndroid Build Coastguard Worker ThreadEntry::~ThreadEntry() {
71*eb293b8fSAndroid Build Coastguard Worker   auto iter = entries_.find(tid_);
72*eb293b8fSAndroid Build Coastguard Worker   if (iter != entries_.end()) {
73*eb293b8fSAndroid Build Coastguard Worker     entries_.erase(iter);
74*eb293b8fSAndroid Build Coastguard Worker   }
75*eb293b8fSAndroid Build Coastguard Worker }
76*eb293b8fSAndroid Build Coastguard Worker 
GetWaitTypeName(WaitType type)77*eb293b8fSAndroid Build Coastguard Worker const char* ThreadEntry::GetWaitTypeName(WaitType type) {
78*eb293b8fSAndroid Build Coastguard Worker   switch (type) {
79*eb293b8fSAndroid Build Coastguard Worker     case WAIT_FOR_UCONTEXT:
80*eb293b8fSAndroid Build Coastguard Worker       return "ucontext";
81*eb293b8fSAndroid Build Coastguard Worker     case WAIT_FOR_UNWIND_TO_COMPLETE:
82*eb293b8fSAndroid Build Coastguard Worker       return "unwind to complete";
83*eb293b8fSAndroid Build Coastguard Worker     case WAIT_FOR_THREAD_TO_RESTART:
84*eb293b8fSAndroid Build Coastguard Worker       return "thread to restart";
85*eb293b8fSAndroid Build Coastguard Worker   }
86*eb293b8fSAndroid Build Coastguard Worker }
87*eb293b8fSAndroid Build Coastguard Worker 
Wait(WaitType type,pid_t tid)88*eb293b8fSAndroid Build Coastguard Worker bool ThreadEntry::Wait(WaitType type, pid_t tid) {
89*eb293b8fSAndroid Build Coastguard Worker   static const std::chrono::duration wait_time(std::chrono::seconds(10));
90*eb293b8fSAndroid Build Coastguard Worker   std::unique_lock<std::mutex> lock(wait_mutex_);
91*eb293b8fSAndroid Build Coastguard Worker   if (wait_cond_.wait_for(lock, wait_time, [this, type] { return wait_value_ == type; })) {
92*eb293b8fSAndroid Build Coastguard Worker     return true;
93*eb293b8fSAndroid Build Coastguard Worker   } else {
94*eb293b8fSAndroid Build Coastguard Worker     if (tid == 0) {
95*eb293b8fSAndroid Build Coastguard Worker       Log::AsyncSafe("In thread being unwound: Timeout waiting for %s", GetWaitTypeName(type));
96*eb293b8fSAndroid Build Coastguard Worker     } else {
97*eb293b8fSAndroid Build Coastguard Worker       Log::AsyncSafe("Unwinding thread %d: Timeout waiting for %s", tid, GetWaitTypeName(type));
98*eb293b8fSAndroid Build Coastguard Worker     }
99*eb293b8fSAndroid Build Coastguard Worker     return false;
100*eb293b8fSAndroid Build Coastguard Worker   }
101*eb293b8fSAndroid Build Coastguard Worker }
102*eb293b8fSAndroid Build Coastguard Worker 
Wake()103*eb293b8fSAndroid Build Coastguard Worker void ThreadEntry::Wake() {
104*eb293b8fSAndroid Build Coastguard Worker   wait_mutex_.lock();
105*eb293b8fSAndroid Build Coastguard Worker   wait_value_++;
106*eb293b8fSAndroid Build Coastguard Worker   wait_mutex_.unlock();
107*eb293b8fSAndroid Build Coastguard Worker 
108*eb293b8fSAndroid Build Coastguard Worker   wait_cond_.notify_one();
109*eb293b8fSAndroid Build Coastguard Worker }
110*eb293b8fSAndroid Build Coastguard Worker 
CopyUcontextFromSigcontext(void * sigcontext)111*eb293b8fSAndroid Build Coastguard Worker void ThreadEntry::CopyUcontextFromSigcontext(void* sigcontext) {
112*eb293b8fSAndroid Build Coastguard Worker   ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(sigcontext);
113*eb293b8fSAndroid Build Coastguard Worker   // The only thing the unwinder cares about is the mcontext data.
114*eb293b8fSAndroid Build Coastguard Worker   memcpy(&ucontext_.uc_mcontext, &ucontext->uc_mcontext, sizeof(ucontext->uc_mcontext));
115*eb293b8fSAndroid Build Coastguard Worker }
116*eb293b8fSAndroid Build Coastguard Worker 
117*eb293b8fSAndroid Build Coastguard Worker }  // namespace unwindstack
118