blob: 14f04de5eecd4734312e15d47169c171d893e06b [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
Dan Albert23f750b2015-04-30 12:52:21 -070032#include <cutils/threads.h>
33
Christopher Ferris2c43cff2015-03-26 19:18:36 -070034#include "BacktraceCurrent.h"
35#include "BacktraceLog.h"
36#include "ThreadEntry.h"
37#include "thread_utils.h"
38
39bool BacktraceCurrent::ReadWord(uintptr_t ptr, word_t* out_value) {
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 {
50 BACK_LOGW("pointer %p not in a readable map", reinterpret_cast<void*>(ptr));
51 *out_value = static_cast<word_t>(-1);
52 return false;
53 }
54}
55
56size_t BacktraceCurrent::Read(uintptr_t addr, uint8_t* buffer, size_t bytes) {
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
67bool BacktraceCurrent::Unwind(size_t num_ignore_frames, ucontext_t* ucontext) {
68 if (ucontext) {
69 return UnwindFromContext(num_ignore_frames, ucontext);
70 }
71
72 if (Tid() != gettid()) {
73 return UnwindThread(num_ignore_frames);
74 }
75
76 return UnwindFromContext(num_ignore_frames, nullptr);
77}
78
Christopher Ferrisca09ce92015-03-31 17:28:22 -070079bool BacktraceCurrent::DiscardFrame(const backtrace_frame_data_t& frame) {
80 if (BacktraceMap::IsValid(frame.map)) {
81 const std::string library = basename(frame.map.name.c_str());
82 if (library == "libunwind.so" || library == "libbacktrace.so") {
83 return true;
84 }
85 }
86 return false;
87}
88
Christopher Ferris2c43cff2015-03-26 19:18:36 -070089static pthread_mutex_t g_sigaction_mutex = PTHREAD_MUTEX_INITIALIZER;
90
91static void SignalHandler(int, siginfo_t*, void* sigcontext) {
92 ThreadEntry* entry = ThreadEntry::Get(getpid(), gettid(), false);
93 if (!entry) {
94 BACK_LOGW("Unable to find pid %d tid %d information", getpid(), gettid());
95 return;
96 }
97
98 entry->CopyUcontextFromSigcontext(sigcontext);
99
100 // Indicate the ucontext is now valid.
101 entry->Wake();
102
103 // Pause the thread until the unwind is complete. This avoids having
104 // the thread run ahead causing problems.
105 // The number indicates that we are waiting for the second Wake() call
106 // overall which is made by the thread requesting an unwind.
107 entry->Wait(2);
108
109 ThreadEntry::Remove(entry);
110}
111
112bool BacktraceCurrent::UnwindThread(size_t num_ignore_frames) {
113 // Prevent multiple threads trying to set the trigger action on different
114 // threads at the same time.
115 pthread_mutex_lock(&g_sigaction_mutex);
116
117 ThreadEntry* entry = ThreadEntry::Get(Pid(), Tid());
118 entry->Lock();
119
120 struct sigaction act, oldact;
121 memset(&act, 0, sizeof(act));
122 act.sa_sigaction = SignalHandler;
123 act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
124 sigemptyset(&act.sa_mask);
125 if (sigaction(THREAD_SIGNAL, &act, &oldact) != 0) {
126 BACK_LOGW("sigaction failed %s", strerror(errno));
127 entry->Unlock();
128 ThreadEntry::Remove(entry);
129 pthread_mutex_unlock(&g_sigaction_mutex);
130 return false;
131 }
132
133 if (tgkill(Pid(), Tid(), THREAD_SIGNAL) != 0) {
134 BACK_LOGW("tgkill %d failed: %s", Tid(), strerror(errno));
135 sigaction(THREAD_SIGNAL, &oldact, nullptr);
136 entry->Unlock();
137 ThreadEntry::Remove(entry);
138 pthread_mutex_unlock(&g_sigaction_mutex);
139 return false;
140 }
141
142 // Wait for the thread to get the ucontext. The number indicates
143 // that we are waiting for the first Wake() call made by the thread.
144 entry->Wait(1);
145
146 // After the thread has received the signal, allow other unwinders to
147 // continue.
148 sigaction(THREAD_SIGNAL, &oldact, nullptr);
149 pthread_mutex_unlock(&g_sigaction_mutex);
150
151 bool unwind_done = UnwindFromContext(num_ignore_frames, entry->GetUcontext());
152
153 // Tell the signal handler to exit and release the entry.
154 entry->Wake();
155
156 return unwind_done;
157}