| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 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 Ferris | ca09ce9 | 2015-03-31 17:28:22 -0700 | [diff] [blame] | 17 | #define _GNU_SOURCE 1 | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 18 | #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 Ferris | c463ba4 | 2016-03-09 14:35:54 -0800 | [diff] [blame] | 27 | #include <stdlib.h> | 
|  | 28 |  | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 29 | #include <string> | 
|  | 30 |  | 
| Elliott Hughes | 3848890 | 2018-07-11 11:13:16 -0700 | [diff] [blame] | 31 | #include <android-base/threads.h> | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 32 | #include <backtrace/Backtrace.h> | 
|  | 33 | #include <backtrace/BacktraceMap.h> | 
|  | 34 |  | 
| Christopher Ferris | f30a810 | 2017-05-03 17:22:24 -0700 | [diff] [blame] | 35 | #include "BacktraceAsyncSafeLog.h" | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 36 | #include "BacktraceCurrent.h" | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 37 | #include "ThreadEntry.h" | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 38 |  | 
| Christopher Ferris | 7937a36 | 2018-01-18 11:15:49 -0800 | [diff] [blame] | 39 | bool BacktraceCurrent::ReadWord(uint64_t ptr, word_t* out_value) { | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 40 | 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 Ferris | f30a810 | 2017-05-03 17:22:24 -0700 | [diff] [blame] | 50 | BACK_ASYNC_SAFE_LOGW("pointer %p not in a readable map", reinterpret_cast<void*>(ptr)); | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 51 | *out_value = static_cast<word_t>(-1); | 
|  | 52 | return false; | 
|  | 53 | } | 
|  | 54 | } | 
|  | 55 |  | 
| Christopher Ferris | 7937a36 | 2018-01-18 11:15:49 -0800 | [diff] [blame] | 56 | size_t BacktraceCurrent::Read(uint64_t addr, uint8_t* buffer, size_t bytes) { | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 57 | 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 Ferris | c8bec5a | 2017-12-11 17:44:33 -0800 | [diff] [blame] | 67 | bool BacktraceCurrent::Unwind(size_t num_ignore_frames, void* ucontext) { | 
| Christopher Ferris | 30c942c | 2015-05-14 15:39:52 -0700 | [diff] [blame] | 68 | if (GetMap() == nullptr) { | 
|  | 69 | // Without a map object, we can't do anything. | 
| Yabin Cui | f880828 | 2017-12-12 18:04:10 -0800 | [diff] [blame] | 70 | error_.error_code = BACKTRACE_UNWIND_ERROR_MAP_MISSING; | 
| Christopher Ferris | 30c942c | 2015-05-14 15:39:52 -0700 | [diff] [blame] | 71 | return false; | 
|  | 72 | } | 
|  | 73 |  | 
| Yabin Cui | f880828 | 2017-12-12 18:04:10 -0800 | [diff] [blame] | 74 | error_.error_code = BACKTRACE_UNWIND_NO_ERROR; | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 75 | if (ucontext) { | 
|  | 76 | return UnwindFromContext(num_ignore_frames, ucontext); | 
|  | 77 | } | 
|  | 78 |  | 
| Yi Kong | c91e6f9 | 2018-12-17 12:51:36 -0800 | [diff] [blame] | 79 | if (Tid() != static_cast<pid_t>(android::base::GetThreadId())) { | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 80 | return UnwindThread(num_ignore_frames); | 
|  | 81 | } | 
|  | 82 |  | 
|  | 83 | return UnwindFromContext(num_ignore_frames, nullptr); | 
|  | 84 | } | 
|  | 85 |  | 
| Christopher Ferris | ca09ce9 | 2015-03-31 17:28:22 -0700 | [diff] [blame] | 86 | bool 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 Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 96 | static pthread_mutex_t g_sigaction_mutex = PTHREAD_MUTEX_INITIALIZER; | 
|  | 97 |  | 
| Christopher Ferris | 10ab87f | 2017-03-09 14:04:43 -0800 | [diff] [blame] | 98 | // 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. | 
|  | 103 | class 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 Ferris | d7226f9 | 2015-09-03 11:25:55 -0700 | [diff] [blame] | 114 | static void SignalLogOnly(int, siginfo_t*, void*) { | 
| Christopher Ferris | 10ab87f | 2017-03-09 14:04:43 -0800 | [diff] [blame] | 115 | ErrnoRestorer restore; | 
|  | 116 |  | 
| Elliott Hughes | 3848890 | 2018-07-11 11:13:16 -0700 | [diff] [blame] | 117 | BACK_ASYNC_SAFE_LOGE("pid %d, tid %d: Received a spurious signal %d\n", getpid(), | 
|  | 118 | static_cast<int>(android::base::GetThreadId()), THREAD_SIGNAL); | 
| Christopher Ferris | d7226f9 | 2015-09-03 11:25:55 -0700 | [diff] [blame] | 119 | } | 
|  | 120 |  | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 121 | static void SignalHandler(int, siginfo_t*, void* sigcontext) { | 
| Christopher Ferris | 10ab87f | 2017-03-09 14:04:43 -0800 | [diff] [blame] | 122 | ErrnoRestorer restore; | 
|  | 123 |  | 
| Elliott Hughes | 3848890 | 2018-07-11 11:13:16 -0700 | [diff] [blame] | 124 | ThreadEntry* entry = ThreadEntry::Get(getpid(), android::base::GetThreadId(), false); | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 125 | if (!entry) { | 
| Elliott Hughes | 3848890 | 2018-07-11 11:13:16 -0700 | [diff] [blame] | 126 | BACK_ASYNC_SAFE_LOGE("pid %d, tid %d entry not found", getpid(), | 
|  | 127 | static_cast<int>(android::base::GetThreadId())); | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 128 | return; | 
|  | 129 | } | 
|  | 130 |  | 
|  | 131 | entry->CopyUcontextFromSigcontext(sigcontext); | 
|  | 132 |  | 
|  | 133 | // Indicate the ucontext is now valid. | 
|  | 134 | entry->Wake(); | 
|  | 135 |  | 
|  | 136 | // Pause the thread until the unwind is complete. This avoids having | 
|  | 137 | // the thread run ahead causing problems. | 
|  | 138 | // The number indicates that we are waiting for the second Wake() call | 
|  | 139 | // overall which is made by the thread requesting an unwind. | 
| Christopher Ferris | 2d09171 | 2015-05-28 16:10:33 -0700 | [diff] [blame] | 140 | if (entry->Wait(2)) { | 
|  | 141 | // Do not remove the entry here because that can result in a deadlock | 
|  | 142 | // if the code cannot properly send a signal to the thread under test. | 
|  | 143 | entry->Wake(); | 
|  | 144 | } else { | 
|  | 145 | // At this point, it is possible that entry has been freed, so just exit. | 
| Christopher Ferris | f30a810 | 2017-05-03 17:22:24 -0700 | [diff] [blame] | 146 | BACK_ASYNC_SAFE_LOGE("Timed out waiting for unwind thread to indicate it completed."); | 
| Christopher Ferris | 2d09171 | 2015-05-28 16:10:33 -0700 | [diff] [blame] | 147 | } | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 148 | } | 
|  | 149 |  | 
|  | 150 | bool BacktraceCurrent::UnwindThread(size_t num_ignore_frames) { | 
|  | 151 | // Prevent multiple threads trying to set the trigger action on different | 
|  | 152 | // threads at the same time. | 
|  | 153 | pthread_mutex_lock(&g_sigaction_mutex); | 
|  | 154 |  | 
|  | 155 | ThreadEntry* entry = ThreadEntry::Get(Pid(), Tid()); | 
|  | 156 | entry->Lock(); | 
|  | 157 |  | 
|  | 158 | struct sigaction act, oldact; | 
|  | 159 | memset(&act, 0, sizeof(act)); | 
|  | 160 | act.sa_sigaction = SignalHandler; | 
|  | 161 | act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK; | 
|  | 162 | sigemptyset(&act.sa_mask); | 
|  | 163 | if (sigaction(THREAD_SIGNAL, &act, &oldact) != 0) { | 
| Christopher Ferris | f30a810 | 2017-05-03 17:22:24 -0700 | [diff] [blame] | 164 | BACK_ASYNC_SAFE_LOGE("sigaction failed: %s", strerror(errno)); | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 165 | ThreadEntry::Remove(entry); | 
|  | 166 | pthread_mutex_unlock(&g_sigaction_mutex); | 
| Yabin Cui | f880828 | 2017-12-12 18:04:10 -0800 | [diff] [blame] | 167 | error_.error_code = BACKTRACE_UNWIND_ERROR_INTERNAL; | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 168 | return false; | 
|  | 169 | } | 
|  | 170 |  | 
|  | 171 | if (tgkill(Pid(), Tid(), THREAD_SIGNAL) != 0) { | 
| Christopher Ferris | c463ba4 | 2016-03-09 14:35:54 -0800 | [diff] [blame] | 172 | // Do not emit an error message, this might be expected. Set the | 
|  | 173 | // error and let the caller decide. | 
|  | 174 | if (errno == ESRCH) { | 
| Yabin Cui | f880828 | 2017-12-12 18:04:10 -0800 | [diff] [blame] | 175 | error_.error_code = BACKTRACE_UNWIND_ERROR_THREAD_DOESNT_EXIST; | 
| Christopher Ferris | c463ba4 | 2016-03-09 14:35:54 -0800 | [diff] [blame] | 176 | } else { | 
| Yabin Cui | f880828 | 2017-12-12 18:04:10 -0800 | [diff] [blame] | 177 | error_.error_code = BACKTRACE_UNWIND_ERROR_INTERNAL; | 
| Christopher Ferris | c463ba4 | 2016-03-09 14:35:54 -0800 | [diff] [blame] | 178 | } | 
|  | 179 |  | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 180 | sigaction(THREAD_SIGNAL, &oldact, nullptr); | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 181 | ThreadEntry::Remove(entry); | 
|  | 182 | pthread_mutex_unlock(&g_sigaction_mutex); | 
|  | 183 | return false; | 
|  | 184 | } | 
|  | 185 |  | 
|  | 186 | // Wait for the thread to get the ucontext. The number indicates | 
|  | 187 | // that we are waiting for the first Wake() call made by the thread. | 
| Christopher Ferris | 2d09171 | 2015-05-28 16:10:33 -0700 | [diff] [blame] | 188 | bool wait_completed = entry->Wait(1); | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 189 |  | 
| Christopher Ferris | d7226f9 | 2015-09-03 11:25:55 -0700 | [diff] [blame] | 190 | if (!wait_completed && oldact.sa_sigaction == nullptr) { | 
|  | 191 | // If the wait failed, it could be that the signal could not be delivered | 
|  | 192 | // within the timeout. Add a signal handler that's simply going to log | 
|  | 193 | // something so that we don't crash if the signal eventually gets | 
|  | 194 | // delivered. Only do this if there isn't already an action set up. | 
|  | 195 | memset(&act, 0, sizeof(act)); | 
|  | 196 | act.sa_sigaction = SignalLogOnly; | 
|  | 197 | act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK; | 
|  | 198 | sigemptyset(&act.sa_mask); | 
|  | 199 | sigaction(THREAD_SIGNAL, &act, nullptr); | 
|  | 200 | } else { | 
|  | 201 | sigaction(THREAD_SIGNAL, &oldact, nullptr); | 
|  | 202 | } | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 203 | // After the thread has received the signal, allow other unwinders to | 
|  | 204 | // continue. | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 205 | pthread_mutex_unlock(&g_sigaction_mutex); | 
|  | 206 |  | 
| Christopher Ferris | 2d09171 | 2015-05-28 16:10:33 -0700 | [diff] [blame] | 207 | bool unwind_done = false; | 
|  | 208 | if (wait_completed) { | 
|  | 209 | unwind_done = UnwindFromContext(num_ignore_frames, entry->GetUcontext()); | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 210 |  | 
| Christopher Ferris | 2d09171 | 2015-05-28 16:10:33 -0700 | [diff] [blame] | 211 | // Tell the signal handler to exit and release the entry. | 
|  | 212 | entry->Wake(); | 
|  | 213 |  | 
|  | 214 | // Wait for the thread to indicate it is done with the ThreadEntry. | 
|  | 215 | if (!entry->Wait(3)) { | 
|  | 216 | // Send a warning, but do not mark as a failure to unwind. | 
| Christopher Ferris | f30a810 | 2017-05-03 17:22:24 -0700 | [diff] [blame] | 217 | BACK_ASYNC_SAFE_LOGW("Timed out waiting for signal handler to indicate it finished."); | 
| Christopher Ferris | 2d09171 | 2015-05-28 16:10:33 -0700 | [diff] [blame] | 218 | } | 
|  | 219 | } else { | 
| Christopher Ferris | c463ba4 | 2016-03-09 14:35:54 -0800 | [diff] [blame] | 220 | // Check to see if the thread has disappeared. | 
|  | 221 | if (tgkill(Pid(), Tid(), 0) == -1 && errno == ESRCH) { | 
| Yabin Cui | f880828 | 2017-12-12 18:04:10 -0800 | [diff] [blame] | 222 | error_.error_code = BACKTRACE_UNWIND_ERROR_THREAD_DOESNT_EXIST; | 
| Christopher Ferris | c463ba4 | 2016-03-09 14:35:54 -0800 | [diff] [blame] | 223 | } else { | 
| Yabin Cui | f880828 | 2017-12-12 18:04:10 -0800 | [diff] [blame] | 224 | error_.error_code = BACKTRACE_UNWIND_ERROR_THREAD_TIMEOUT; | 
| Christopher Ferris | f30a810 | 2017-05-03 17:22:24 -0700 | [diff] [blame] | 225 | BACK_ASYNC_SAFE_LOGE("Timed out waiting for signal handler to get ucontext data."); | 
| Christopher Ferris | c463ba4 | 2016-03-09 14:35:54 -0800 | [diff] [blame] | 226 | } | 
| Christopher Ferris | 2d09171 | 2015-05-28 16:10:33 -0700 | [diff] [blame] | 227 | } | 
|  | 228 |  | 
|  | 229 | ThreadEntry::Remove(entry); | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 230 |  | 
|  | 231 | return unwind_done; | 
|  | 232 | } |