blob: 8e223668e0ee70ea56a02fb88489e9176323660f [file] [log] [blame]
Christopher Ferris2c43cff2015-03-26 19:18:36 -07001/*
2 * Copyright (C) 2013 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
Christopher Ferrisca09ce92015-03-31 17:28:22 -070017#define _GNU_SOURCE 1
Christopher Ferris2c43cff2015-03-26 19:18:36 -070018#include <errno.h>
19#include <stdint.h>
20#include <string.h>
21#include <sys/param.h>
22#include <sys/ptrace.h>
23#include <sys/types.h>
24#include <ucontext.h>
25#include <unistd.h>
26
27#include <string>
28
29#include <backtrace/Backtrace.h>
30#include <backtrace/BacktraceMap.h>
31
32#include "BacktraceCurrent.h"
33#include "BacktraceLog.h"
34#include "ThreadEntry.h"
35#include "thread_utils.h"
36
37bool BacktraceCurrent::ReadWord(uintptr_t ptr, word_t* out_value) {
38 if (!VerifyReadWordArgs(ptr, out_value)) {
39 return false;
40 }
41
42 backtrace_map_t map;
43 FillInMap(ptr, &map);
44 if (BacktraceMap::IsValid(map) && map.flags & PROT_READ) {
45 *out_value = *reinterpret_cast<word_t*>(ptr);
46 return true;
47 } else {
48 BACK_LOGW("pointer %p not in a readable map", reinterpret_cast<void*>(ptr));
49 *out_value = static_cast<word_t>(-1);
50 return false;
51 }
52}
53
54size_t BacktraceCurrent::Read(uintptr_t addr, uint8_t* buffer, size_t bytes) {
55 backtrace_map_t map;
56 FillInMap(addr, &map);
57 if (!BacktraceMap::IsValid(map) || !(map.flags & PROT_READ)) {
58 return 0;
59 }
60 bytes = MIN(map.end - addr, bytes);
61 memcpy(buffer, reinterpret_cast<uint8_t*>(addr), bytes);
62 return bytes;
63}
64
65bool BacktraceCurrent::Unwind(size_t num_ignore_frames, ucontext_t* ucontext) {
Christopher Ferris30c942c2015-05-14 15:39:52 -070066 if (GetMap() == nullptr) {
67 // Without a map object, we can't do anything.
68 return false;
69 }
70
Christopher Ferris2c43cff2015-03-26 19:18:36 -070071 if (ucontext) {
72 return UnwindFromContext(num_ignore_frames, ucontext);
73 }
74
75 if (Tid() != gettid()) {
76 return UnwindThread(num_ignore_frames);
77 }
78
79 return UnwindFromContext(num_ignore_frames, nullptr);
80}
81
Christopher Ferrisca09ce92015-03-31 17:28:22 -070082bool BacktraceCurrent::DiscardFrame(const backtrace_frame_data_t& frame) {
83 if (BacktraceMap::IsValid(frame.map)) {
84 const std::string library = basename(frame.map.name.c_str());
85 if (library == "libunwind.so" || library == "libbacktrace.so") {
86 return true;
87 }
88 }
89 return false;
90}
91
Christopher Ferris2c43cff2015-03-26 19:18:36 -070092static pthread_mutex_t g_sigaction_mutex = PTHREAD_MUTEX_INITIALIZER;
93
Christopher Ferrisd7226f92015-09-03 11:25:55 -070094static void SignalLogOnly(int, siginfo_t*, void*) {
95 BACK_LOGE("pid %d, tid %d: Received a spurious signal %d\n", getpid(), gettid(), THREAD_SIGNAL);
96}
97
Christopher Ferris2c43cff2015-03-26 19:18:36 -070098static void SignalHandler(int, siginfo_t*, void* sigcontext) {
99 ThreadEntry* entry = ThreadEntry::Get(getpid(), gettid(), false);
100 if (!entry) {
Christopher Ferris2d091712015-05-28 16:10:33 -0700101 BACK_LOGE("pid %d, tid %d entry not found", getpid(), gettid());
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700102 return;
103 }
104
105 entry->CopyUcontextFromSigcontext(sigcontext);
106
107 // Indicate the ucontext is now valid.
108 entry->Wake();
109
110 // Pause the thread until the unwind is complete. This avoids having
111 // the thread run ahead causing problems.
112 // The number indicates that we are waiting for the second Wake() call
113 // overall which is made by the thread requesting an unwind.
Christopher Ferris2d091712015-05-28 16:10:33 -0700114 if (entry->Wait(2)) {
115 // Do not remove the entry here because that can result in a deadlock
116 // if the code cannot properly send a signal to the thread under test.
117 entry->Wake();
118 } else {
119 // At this point, it is possible that entry has been freed, so just exit.
120 BACK_LOGE("Timed out waiting for unwind thread to indicate it completed.");
121 }
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700122}
123
124bool BacktraceCurrent::UnwindThread(size_t num_ignore_frames) {
125 // Prevent multiple threads trying to set the trigger action on different
126 // threads at the same time.
127 pthread_mutex_lock(&g_sigaction_mutex);
128
129 ThreadEntry* entry = ThreadEntry::Get(Pid(), Tid());
130 entry->Lock();
131
132 struct sigaction act, oldact;
133 memset(&act, 0, sizeof(act));
134 act.sa_sigaction = SignalHandler;
135 act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
136 sigemptyset(&act.sa_mask);
137 if (sigaction(THREAD_SIGNAL, &act, &oldact) != 0) {
Christopher Ferris2d091712015-05-28 16:10:33 -0700138 BACK_LOGE("sigaction failed: %s", strerror(errno));
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700139 ThreadEntry::Remove(entry);
140 pthread_mutex_unlock(&g_sigaction_mutex);
141 return false;
142 }
143
144 if (tgkill(Pid(), Tid(), THREAD_SIGNAL) != 0) {
Christopher Ferris2d091712015-05-28 16:10:33 -0700145 BACK_LOGE("tgkill %d failed: %s", Tid(), strerror(errno));
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700146 sigaction(THREAD_SIGNAL, &oldact, nullptr);
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700147 ThreadEntry::Remove(entry);
148 pthread_mutex_unlock(&g_sigaction_mutex);
149 return false;
150 }
151
152 // Wait for the thread to get the ucontext. The number indicates
153 // that we are waiting for the first Wake() call made by the thread.
Christopher Ferris2d091712015-05-28 16:10:33 -0700154 bool wait_completed = entry->Wait(1);
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700155
Christopher Ferrisd7226f92015-09-03 11:25:55 -0700156 if (!wait_completed && oldact.sa_sigaction == nullptr) {
157 // If the wait failed, it could be that the signal could not be delivered
158 // within the timeout. Add a signal handler that's simply going to log
159 // something so that we don't crash if the signal eventually gets
160 // delivered. Only do this if there isn't already an action set up.
161 memset(&act, 0, sizeof(act));
162 act.sa_sigaction = SignalLogOnly;
163 act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
164 sigemptyset(&act.sa_mask);
165 sigaction(THREAD_SIGNAL, &act, nullptr);
166 } else {
167 sigaction(THREAD_SIGNAL, &oldact, nullptr);
168 }
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700169 // After the thread has received the signal, allow other unwinders to
170 // continue.
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700171 pthread_mutex_unlock(&g_sigaction_mutex);
172
Christopher Ferris2d091712015-05-28 16:10:33 -0700173 bool unwind_done = false;
174 if (wait_completed) {
175 unwind_done = UnwindFromContext(num_ignore_frames, entry->GetUcontext());
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700176
Christopher Ferris2d091712015-05-28 16:10:33 -0700177 // Tell the signal handler to exit and release the entry.
178 entry->Wake();
179
180 // Wait for the thread to indicate it is done with the ThreadEntry.
181 if (!entry->Wait(3)) {
182 // Send a warning, but do not mark as a failure to unwind.
183 BACK_LOGW("Timed out waiting for signal handler to indicate it finished.");
184 }
185 } else {
186 BACK_LOGE("Timed out waiting for signal handler to get ucontext data.");
187 }
188
189 ThreadEntry::Remove(entry);
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700190
191 return unwind_done;
192}