blob: b7190e2560e7834321dc79160f1a6b621915ab96 [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
17#include <errno.h>
18#include <stdint.h>
19#include <string.h>
20#include <sys/param.h>
21#include <sys/ptrace.h>
22#include <sys/types.h>
23#include <ucontext.h>
24#include <unistd.h>
25
26#include <string>
27
28#include <backtrace/Backtrace.h>
29#include <backtrace/BacktraceMap.h>
30
31#include "BacktraceCurrent.h"
32#include "BacktraceLog.h"
33#include "ThreadEntry.h"
34#include "thread_utils.h"
35
36bool BacktraceCurrent::ReadWord(uintptr_t ptr, word_t* out_value) {
37 if (!VerifyReadWordArgs(ptr, out_value)) {
38 return false;
39 }
40
41 backtrace_map_t map;
42 FillInMap(ptr, &map);
43 if (BacktraceMap::IsValid(map) && map.flags & PROT_READ) {
44 *out_value = *reinterpret_cast<word_t*>(ptr);
45 return true;
46 } else {
47 BACK_LOGW("pointer %p not in a readable map", reinterpret_cast<void*>(ptr));
48 *out_value = static_cast<word_t>(-1);
49 return false;
50 }
51}
52
53size_t BacktraceCurrent::Read(uintptr_t addr, uint8_t* buffer, size_t bytes) {
54 backtrace_map_t map;
55 FillInMap(addr, &map);
56 if (!BacktraceMap::IsValid(map) || !(map.flags & PROT_READ)) {
57 return 0;
58 }
59 bytes = MIN(map.end - addr, bytes);
60 memcpy(buffer, reinterpret_cast<uint8_t*>(addr), bytes);
61 return bytes;
62}
63
64bool BacktraceCurrent::Unwind(size_t num_ignore_frames, ucontext_t* ucontext) {
65 if (ucontext) {
66 return UnwindFromContext(num_ignore_frames, ucontext);
67 }
68
69 if (Tid() != gettid()) {
70 return UnwindThread(num_ignore_frames);
71 }
72
73 return UnwindFromContext(num_ignore_frames, nullptr);
74}
75
76static pthread_mutex_t g_sigaction_mutex = PTHREAD_MUTEX_INITIALIZER;
77
78static void SignalHandler(int, siginfo_t*, void* sigcontext) {
79 ThreadEntry* entry = ThreadEntry::Get(getpid(), gettid(), false);
80 if (!entry) {
81 BACK_LOGW("Unable to find pid %d tid %d information", getpid(), gettid());
82 return;
83 }
84
85 entry->CopyUcontextFromSigcontext(sigcontext);
86
87 // Indicate the ucontext is now valid.
88 entry->Wake();
89
90 // Pause the thread until the unwind is complete. This avoids having
91 // the thread run ahead causing problems.
92 // The number indicates that we are waiting for the second Wake() call
93 // overall which is made by the thread requesting an unwind.
94 entry->Wait(2);
95
96 ThreadEntry::Remove(entry);
97}
98
99bool BacktraceCurrent::UnwindThread(size_t num_ignore_frames) {
100 // Prevent multiple threads trying to set the trigger action on different
101 // threads at the same time.
102 pthread_mutex_lock(&g_sigaction_mutex);
103
104 ThreadEntry* entry = ThreadEntry::Get(Pid(), Tid());
105 entry->Lock();
106
107 struct sigaction act, oldact;
108 memset(&act, 0, sizeof(act));
109 act.sa_sigaction = SignalHandler;
110 act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
111 sigemptyset(&act.sa_mask);
112 if (sigaction(THREAD_SIGNAL, &act, &oldact) != 0) {
113 BACK_LOGW("sigaction failed %s", strerror(errno));
114 entry->Unlock();
115 ThreadEntry::Remove(entry);
116 pthread_mutex_unlock(&g_sigaction_mutex);
117 return false;
118 }
119
120 if (tgkill(Pid(), Tid(), THREAD_SIGNAL) != 0) {
121 BACK_LOGW("tgkill %d failed: %s", Tid(), strerror(errno));
122 sigaction(THREAD_SIGNAL, &oldact, nullptr);
123 entry->Unlock();
124 ThreadEntry::Remove(entry);
125 pthread_mutex_unlock(&g_sigaction_mutex);
126 return false;
127 }
128
129 // Wait for the thread to get the ucontext. The number indicates
130 // that we are waiting for the first Wake() call made by the thread.
131 entry->Wait(1);
132
133 // After the thread has received the signal, allow other unwinders to
134 // continue.
135 sigaction(THREAD_SIGNAL, &oldact, nullptr);
136 pthread_mutex_unlock(&g_sigaction_mutex);
137
138 bool unwind_done = UnwindFromContext(num_ignore_frames, entry->GetUcontext());
139
140 // Tell the signal handler to exit and release the entry.
141 entry->Wake();
142
143 return unwind_done;
144}