blob: f6f4423a16763f9633de234f4142d03d4ced5c7e [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
Christopher Ferrisc463ba42016-03-09 14:35:54 -080027#include <stdlib.h>
28
Christopher Ferris2c43cff2015-03-26 19:18:36 -070029#include <string>
30
31#include <backtrace/Backtrace.h>
32#include <backtrace/BacktraceMap.h>
33
Christopher Ferrisf30a8102017-05-03 17:22:24 -070034#include "BacktraceAsyncSafeLog.h"
Christopher Ferris2c43cff2015-03-26 19:18:36 -070035#include "BacktraceCurrent.h"
Christopher Ferris2c43cff2015-03-26 19:18:36 -070036#include "ThreadEntry.h"
37#include "thread_utils.h"
38
Christopher Ferris7937a362018-01-18 11:15:49 -080039bool BacktraceCurrent::ReadWord(uint64_t ptr, word_t* out_value) {
Christopher Ferris2c43cff2015-03-26 19:18:36 -070040 if (!VerifyReadWordArgs(ptr, out_value)) {
41 return false;
42 }
43
44 backtrace_map_t map;
45 FillInMap(ptr, &map);
46 if (BacktraceMap::IsValid(map) && map.flags & PROT_READ) {
47 *out_value = *reinterpret_cast<word_t*>(ptr);
48 return true;
49 } else {
Christopher Ferrisf30a8102017-05-03 17:22:24 -070050 BACK_ASYNC_SAFE_LOGW("pointer %p not in a readable map", reinterpret_cast<void*>(ptr));
Christopher Ferris2c43cff2015-03-26 19:18:36 -070051 *out_value = static_cast<word_t>(-1);
52 return false;
53 }
54}
55
Christopher Ferris7937a362018-01-18 11:15:49 -080056size_t BacktraceCurrent::Read(uint64_t addr, uint8_t* buffer, size_t bytes) {
Christopher Ferris2c43cff2015-03-26 19:18:36 -070057 backtrace_map_t map;
58 FillInMap(addr, &map);
59 if (!BacktraceMap::IsValid(map) || !(map.flags & PROT_READ)) {
60 return 0;
61 }
62 bytes = MIN(map.end - addr, bytes);
63 memcpy(buffer, reinterpret_cast<uint8_t*>(addr), bytes);
64 return bytes;
65}
66
Christopher Ferrisc8bec5a2017-12-11 17:44:33 -080067bool BacktraceCurrent::Unwind(size_t num_ignore_frames, void* ucontext) {
Christopher Ferris30c942c2015-05-14 15:39:52 -070068 if (GetMap() == nullptr) {
69 // Without a map object, we can't do anything.
Yabin Cuif8808282017-12-12 18:04:10 -080070 error_.error_code = BACKTRACE_UNWIND_ERROR_MAP_MISSING;
Christopher Ferris30c942c2015-05-14 15:39:52 -070071 return false;
72 }
73
Yabin Cuif8808282017-12-12 18:04:10 -080074 error_.error_code = BACKTRACE_UNWIND_NO_ERROR;
Christopher Ferris2c43cff2015-03-26 19:18:36 -070075 if (ucontext) {
76 return UnwindFromContext(num_ignore_frames, ucontext);
77 }
78
79 if (Tid() != gettid()) {
80 return UnwindThread(num_ignore_frames);
81 }
82
83 return UnwindFromContext(num_ignore_frames, nullptr);
84}
85
Christopher Ferrisca09ce92015-03-31 17:28:22 -070086bool BacktraceCurrent::DiscardFrame(const backtrace_frame_data_t& frame) {
87 if (BacktraceMap::IsValid(frame.map)) {
88 const std::string library = basename(frame.map.name.c_str());
89 if (library == "libunwind.so" || library == "libbacktrace.so") {
90 return true;
91 }
92 }
93 return false;
94}
95
Christopher Ferris2c43cff2015-03-26 19:18:36 -070096static pthread_mutex_t g_sigaction_mutex = PTHREAD_MUTEX_INITIALIZER;
97
Christopher Ferris10ab87f2017-03-09 14:04:43 -080098// Since errno is stored per thread, changing it in the signal handler
99// modifies the value on the thread in which the signal handler executes.
100// If a signal occurs between a call and an errno check, it's possible
101// to get the errno set here. Always save and restore it just in case
102// code would modify it.
103class ErrnoRestorer {
104 public:
105 ErrnoRestorer() : saved_errno_(errno) {}
106 ~ErrnoRestorer() {
107 errno = saved_errno_;
108 }
109
110 private:
111 int saved_errno_;
112};
113
Christopher Ferrisd7226f92015-09-03 11:25:55 -0700114static void SignalLogOnly(int, siginfo_t*, void*) {
Christopher Ferris10ab87f2017-03-09 14:04:43 -0800115 ErrnoRestorer restore;
116
Christopher Ferrisf30a8102017-05-03 17:22:24 -0700117 BACK_ASYNC_SAFE_LOGE("pid %d, tid %d: Received a spurious signal %d\n", getpid(), gettid(),
118 THREAD_SIGNAL);
Christopher Ferrisd7226f92015-09-03 11:25:55 -0700119}
120
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700121static void SignalHandler(int, siginfo_t*, void* sigcontext) {
Christopher Ferris10ab87f2017-03-09 14:04:43 -0800122 ErrnoRestorer restore;
123
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700124 ThreadEntry* entry = ThreadEntry::Get(getpid(), gettid(), false);
125 if (!entry) {
Christopher Ferrisf30a8102017-05-03 17:22:24 -0700126 BACK_ASYNC_SAFE_LOGE("pid %d, tid %d entry not found", getpid(), gettid());
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700127 return;
128 }
129
130 entry->CopyUcontextFromSigcontext(sigcontext);
131
132 // Indicate the ucontext is now valid.
133 entry->Wake();
134
135 // Pause the thread until the unwind is complete. This avoids having
136 // the thread run ahead causing problems.
137 // The number indicates that we are waiting for the second Wake() call
138 // overall which is made by the thread requesting an unwind.
Christopher Ferris2d091712015-05-28 16:10:33 -0700139 if (entry->Wait(2)) {
140 // Do not remove the entry here because that can result in a deadlock
141 // if the code cannot properly send a signal to the thread under test.
142 entry->Wake();
143 } else {
144 // At this point, it is possible that entry has been freed, so just exit.
Christopher Ferrisf30a8102017-05-03 17:22:24 -0700145 BACK_ASYNC_SAFE_LOGE("Timed out waiting for unwind thread to indicate it completed.");
Christopher Ferris2d091712015-05-28 16:10:33 -0700146 }
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700147}
148
149bool BacktraceCurrent::UnwindThread(size_t num_ignore_frames) {
150 // Prevent multiple threads trying to set the trigger action on different
151 // threads at the same time.
152 pthread_mutex_lock(&g_sigaction_mutex);
153
154 ThreadEntry* entry = ThreadEntry::Get(Pid(), Tid());
155 entry->Lock();
156
157 struct sigaction act, oldact;
158 memset(&act, 0, sizeof(act));
159 act.sa_sigaction = SignalHandler;
160 act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
161 sigemptyset(&act.sa_mask);
162 if (sigaction(THREAD_SIGNAL, &act, &oldact) != 0) {
Christopher Ferrisf30a8102017-05-03 17:22:24 -0700163 BACK_ASYNC_SAFE_LOGE("sigaction failed: %s", strerror(errno));
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700164 ThreadEntry::Remove(entry);
165 pthread_mutex_unlock(&g_sigaction_mutex);
Yabin Cuif8808282017-12-12 18:04:10 -0800166 error_.error_code = BACKTRACE_UNWIND_ERROR_INTERNAL;
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700167 return false;
168 }
169
170 if (tgkill(Pid(), Tid(), THREAD_SIGNAL) != 0) {
Christopher Ferrisc463ba42016-03-09 14:35:54 -0800171 // Do not emit an error message, this might be expected. Set the
172 // error and let the caller decide.
173 if (errno == ESRCH) {
Yabin Cuif8808282017-12-12 18:04:10 -0800174 error_.error_code = BACKTRACE_UNWIND_ERROR_THREAD_DOESNT_EXIST;
Christopher Ferrisc463ba42016-03-09 14:35:54 -0800175 } else {
Yabin Cuif8808282017-12-12 18:04:10 -0800176 error_.error_code = BACKTRACE_UNWIND_ERROR_INTERNAL;
Christopher Ferrisc463ba42016-03-09 14:35:54 -0800177 }
178
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700179 sigaction(THREAD_SIGNAL, &oldact, nullptr);
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700180 ThreadEntry::Remove(entry);
181 pthread_mutex_unlock(&g_sigaction_mutex);
182 return false;
183 }
184
185 // Wait for the thread to get the ucontext. The number indicates
186 // that we are waiting for the first Wake() call made by the thread.
Christopher Ferris2d091712015-05-28 16:10:33 -0700187 bool wait_completed = entry->Wait(1);
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700188
Christopher Ferrisd7226f92015-09-03 11:25:55 -0700189 if (!wait_completed && oldact.sa_sigaction == nullptr) {
190 // If the wait failed, it could be that the signal could not be delivered
191 // within the timeout. Add a signal handler that's simply going to log
192 // something so that we don't crash if the signal eventually gets
193 // delivered. Only do this if there isn't already an action set up.
194 memset(&act, 0, sizeof(act));
195 act.sa_sigaction = SignalLogOnly;
196 act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
197 sigemptyset(&act.sa_mask);
198 sigaction(THREAD_SIGNAL, &act, nullptr);
199 } else {
200 sigaction(THREAD_SIGNAL, &oldact, nullptr);
201 }
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700202 // After the thread has received the signal, allow other unwinders to
203 // continue.
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700204 pthread_mutex_unlock(&g_sigaction_mutex);
205
Christopher Ferris2d091712015-05-28 16:10:33 -0700206 bool unwind_done = false;
207 if (wait_completed) {
208 unwind_done = UnwindFromContext(num_ignore_frames, entry->GetUcontext());
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700209
Christopher Ferris2d091712015-05-28 16:10:33 -0700210 // Tell the signal handler to exit and release the entry.
211 entry->Wake();
212
213 // Wait for the thread to indicate it is done with the ThreadEntry.
214 if (!entry->Wait(3)) {
215 // Send a warning, but do not mark as a failure to unwind.
Christopher Ferrisf30a8102017-05-03 17:22:24 -0700216 BACK_ASYNC_SAFE_LOGW("Timed out waiting for signal handler to indicate it finished.");
Christopher Ferris2d091712015-05-28 16:10:33 -0700217 }
218 } else {
Christopher Ferrisc463ba42016-03-09 14:35:54 -0800219 // Check to see if the thread has disappeared.
220 if (tgkill(Pid(), Tid(), 0) == -1 && errno == ESRCH) {
Yabin Cuif8808282017-12-12 18:04:10 -0800221 error_.error_code = BACKTRACE_UNWIND_ERROR_THREAD_DOESNT_EXIST;
Christopher Ferrisc463ba42016-03-09 14:35:54 -0800222 } else {
Yabin Cuif8808282017-12-12 18:04:10 -0800223 error_.error_code = BACKTRACE_UNWIND_ERROR_THREAD_TIMEOUT;
Christopher Ferrisf30a8102017-05-03 17:22:24 -0700224 BACK_ASYNC_SAFE_LOGE("Timed out waiting for signal handler to get ucontext data.");
Christopher Ferrisc463ba42016-03-09 14:35:54 -0800225 }
Christopher Ferris2d091712015-05-28 16:10:33 -0700226 }
227
228 ThreadEntry::Remove(entry);
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700229
230 return unwind_done;
231}