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