123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- #define _GNU_SOURCE 1
- #include <errno.h>
- #include <stdint.h>
- #include <string.h>
- #include <sys/param.h>
- #include <sys/ptrace.h>
- #include <sys/types.h>
- #include <ucontext.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string>
- #include <android-base/threads.h>
- #include <backtrace/Backtrace.h>
- #include <backtrace/BacktraceMap.h>
- #include "BacktraceAsyncSafeLog.h"
- #include "BacktraceCurrent.h"
- #include "ThreadEntry.h"
- bool BacktraceCurrent::ReadWord(uint64_t ptr, word_t* out_value) {
- if (!VerifyReadWordArgs(ptr, out_value)) {
- return false;
- }
- backtrace_map_t map;
- FillInMap(ptr, &map);
- if (BacktraceMap::IsValid(map) && map.flags & PROT_READ) {
- *out_value = *reinterpret_cast<word_t*>(ptr);
- return true;
- } else {
- BACK_ASYNC_SAFE_LOGW("pointer %p not in a readable map", reinterpret_cast<void*>(ptr));
- *out_value = static_cast<word_t>(-1);
- return false;
- }
- }
- size_t BacktraceCurrent::Read(uint64_t addr, uint8_t* buffer, size_t bytes) {
- backtrace_map_t map;
- FillInMap(addr, &map);
- if (!BacktraceMap::IsValid(map) || !(map.flags & PROT_READ)) {
- return 0;
- }
- bytes = MIN(map.end - addr, bytes);
- memcpy(buffer, reinterpret_cast<uint8_t*>(addr), bytes);
- return bytes;
- }
- bool BacktraceCurrent::Unwind(size_t num_ignore_frames, void* ucontext) {
- if (GetMap() == nullptr) {
-
- error_.error_code = BACKTRACE_UNWIND_ERROR_MAP_MISSING;
- return false;
- }
- error_.error_code = BACKTRACE_UNWIND_NO_ERROR;
- if (ucontext) {
- return UnwindFromContext(num_ignore_frames, ucontext);
- }
- if (Tid() != static_cast<pid_t>(android::base::GetThreadId())) {
- return UnwindThread(num_ignore_frames);
- }
- return UnwindFromContext(num_ignore_frames, nullptr);
- }
- bool BacktraceCurrent::DiscardFrame(const backtrace_frame_data_t& frame) {
- if (BacktraceMap::IsValid(frame.map)) {
- const std::string library = basename(frame.map.name.c_str());
- if (library == "libunwind.so" || library == "libbacktrace.so") {
- return true;
- }
- }
- return false;
- }
- static pthread_mutex_t g_sigaction_mutex = PTHREAD_MUTEX_INITIALIZER;
- class ErrnoRestorer {
- public:
- ErrnoRestorer() : saved_errno_(errno) {}
- ~ErrnoRestorer() {
- errno = saved_errno_;
- }
- private:
- int saved_errno_;
- };
- static void SignalLogOnly(int, siginfo_t*, void*) {
- ErrnoRestorer restore;
- BACK_ASYNC_SAFE_LOGE("pid %d, tid %d: Received a spurious signal %d\n", getpid(),
- static_cast<int>(android::base::GetThreadId()), THREAD_SIGNAL);
- }
- static void SignalHandler(int, siginfo_t*, void* sigcontext) {
- ErrnoRestorer restore;
- ThreadEntry* entry = ThreadEntry::Get(getpid(), android::base::GetThreadId(), false);
- if (!entry) {
- BACK_ASYNC_SAFE_LOGE("pid %d, tid %d entry not found", getpid(),
- static_cast<int>(android::base::GetThreadId()));
- return;
- }
- entry->CopyUcontextFromSigcontext(sigcontext);
-
- entry->Wake();
-
-
-
-
- if (entry->Wait(2)) {
-
-
- entry->Wake();
- } else {
-
- BACK_ASYNC_SAFE_LOGE("Timed out waiting for unwind thread to indicate it completed.");
- }
- }
- bool BacktraceCurrent::UnwindThread(size_t num_ignore_frames) {
-
-
- pthread_mutex_lock(&g_sigaction_mutex);
- ThreadEntry* entry = ThreadEntry::Get(Pid(), Tid());
- entry->Lock();
- struct sigaction act, oldact;
- memset(&act, 0, sizeof(act));
- act.sa_sigaction = SignalHandler;
- act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
- sigemptyset(&act.sa_mask);
- if (sigaction(THREAD_SIGNAL, &act, &oldact) != 0) {
- BACK_ASYNC_SAFE_LOGE("sigaction failed: %s", strerror(errno));
- ThreadEntry::Remove(entry);
- pthread_mutex_unlock(&g_sigaction_mutex);
- error_.error_code = BACKTRACE_UNWIND_ERROR_INTERNAL;
- return false;
- }
- if (tgkill(Pid(), Tid(), THREAD_SIGNAL) != 0) {
-
-
- if (errno == ESRCH) {
- error_.error_code = BACKTRACE_UNWIND_ERROR_THREAD_DOESNT_EXIST;
- } else {
- error_.error_code = BACKTRACE_UNWIND_ERROR_INTERNAL;
- }
- sigaction(THREAD_SIGNAL, &oldact, nullptr);
- ThreadEntry::Remove(entry);
- pthread_mutex_unlock(&g_sigaction_mutex);
- return false;
- }
-
-
- bool wait_completed = entry->Wait(1);
- if (!wait_completed && oldact.sa_sigaction == nullptr) {
-
-
-
-
- memset(&act, 0, sizeof(act));
- act.sa_sigaction = SignalLogOnly;
- act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
- sigemptyset(&act.sa_mask);
- sigaction(THREAD_SIGNAL, &act, nullptr);
- } else {
- sigaction(THREAD_SIGNAL, &oldact, nullptr);
- }
-
-
- pthread_mutex_unlock(&g_sigaction_mutex);
- bool unwind_done = false;
- if (wait_completed) {
- unwind_done = UnwindFromContext(num_ignore_frames, entry->GetUcontext());
-
- entry->Wake();
-
- if (!entry->Wait(3)) {
-
- BACK_ASYNC_SAFE_LOGW("Timed out waiting for signal handler to indicate it finished.");
- }
- } else {
-
- if (tgkill(Pid(), Tid(), 0) == -1 && errno == ESRCH) {
- error_.error_code = BACKTRACE_UNWIND_ERROR_THREAD_DOESNT_EXIST;
- } else {
- error_.error_code = BACKTRACE_UNWIND_ERROR_THREAD_TIMEOUT;
- BACK_ASYNC_SAFE_LOGE("Timed out waiting for signal handler to get ucontext data.");
- }
- }
- ThreadEntry::Remove(entry);
- return unwind_done;
- }
|