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