blob: b2d7e55214325a3c95c5f225882e33621ceaa0a7 [file] [log] [blame]
Andreas Gampe73810102015-04-22 18:57:06 -07001/*
2 * Copyright (C) 2015 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#if __linux__
18#include <errno.h>
19#include <signal.h>
20#include <string.h>
21#include <unistd.h>
22#include <sys/ptrace.h>
23#include <sys/wait.h>
24#endif
25
26#include "jni.h"
27
28#include <backtrace/Backtrace.h>
29
30#include "base/logging.h"
31#include "base/macros.h"
32#include "utils.h"
33
34namespace art {
35
36// For testing debuggerd. We do not have expected-death tests, so can't test this by default.
37// Code for this is copied from SignalTest.
38static constexpr bool kCauseSegfault = false;
39char* go_away_compiler_cfi = nullptr;
40
41static void CauseSegfault() {
42#if defined(__arm__) || defined(__i386__) || defined(__x86_64__) || defined(__aarch64__)
43 // On supported architectures we cause a real SEGV.
44 *go_away_compiler_cfi = 'a';
45#else
46 // On other architectures we simulate SEGV.
47 kill(getpid(), SIGSEGV);
48#endif
49}
50
51extern "C" JNIEXPORT jboolean JNICALL Java_Main_sleep(JNIEnv*, jobject, jint, jboolean, jdouble) {
52 // Keep pausing.
53 for (;;) {
54 pause();
55 }
56}
57
58// Helper to look for a sequence in the stack trace.
59#if __linux__
60static bool CheckStack(Backtrace* bt, const std::vector<std::string>& seq) {
61 size_t cur_search_index = 0; // The currently active index in seq.
62 CHECK_GT(seq.size(), 0U);
63
64 for (Backtrace::const_iterator it = bt->begin(); it != bt->end(); ++it) {
65 if (BacktraceMap::IsValid(it->map)) {
66 LOG(INFO) << "Got " << it->func_name << ", looking for " << seq[cur_search_index];
67 if (it->func_name == seq[cur_search_index]) {
68 cur_search_index++;
69 if (cur_search_index == seq.size()) {
70 return true;
71 }
72 }
73 }
74 }
75
76 return false;
77}
78#endif
79
80extern "C" JNIEXPORT jboolean JNICALL Java_Main_unwindInProcess(JNIEnv*, jobject, jint, jboolean) {
81#if __linux__
82 // TODO: What to do on Valgrind?
83
84 std::unique_ptr<Backtrace> bt(Backtrace::Create(BACKTRACE_CURRENT_PROCESS, GetTid()));
85 if (!bt->Unwind(0, nullptr)) {
86 return JNI_FALSE;
87 } else if (bt->NumFrames() == 0) {
88 return JNI_FALSE;
89 }
90
91 // We cannot really parse an exact stack, as the optimizing compiler may inline some functions.
92 // This is also risky, as deduping might play a trick on us, so the test needs to make sure that
93 // only unique functions are being expected.
94 std::vector<std::string> seq = {
95 "Java_Main_unwindInProcess", // This function.
96 "boolean Main.unwindInProcess(int, boolean)", // The corresponding Java native method frame.
97 "void Main.main(java.lang.String[])" // The Java entry method.
98 };
99
100 bool result = CheckStack(bt.get(), seq);
101 if (!kCauseSegfault) {
102 return result ? JNI_TRUE : JNI_FALSE;
103 } else {
104 LOG(INFO) << "Result of check-stack: " << result;
105 }
106#endif
107
108 if (kCauseSegfault) {
109 CauseSegfault();
110 }
111
112 return JNI_FALSE;
113}
114
115#if __linux__
116static constexpr int kSleepTimeMicroseconds = 50000; // 0.05 seconds
117static constexpr int kMaxTotalSleepTimeMicroseconds = 1000000; // 1 second
118
119// Wait for a sigstop. This code is copied from libbacktrace.
120int wait_for_sigstop(pid_t tid, int* total_sleep_time_usec, bool* detach_failed ATTRIBUTE_UNUSED) {
121 for (;;) {
122 int status;
123 pid_t n = TEMP_FAILURE_RETRY(waitpid(tid, &status, __WALL | WNOHANG));
124 if (n == -1) {
125 PLOG(WARNING) << "waitpid failed: tid " << tid;
126 break;
127 } else if (n == tid) {
128 if (WIFSTOPPED(status)) {
129 return WSTOPSIG(status);
130 } else {
131 PLOG(ERROR) << "unexpected waitpid response: n=" << n << ", status=" << std::hex << status;
132 break;
133 }
134 }
135
136 if (*total_sleep_time_usec > kMaxTotalSleepTimeMicroseconds) {
137 PLOG(WARNING) << "timed out waiting for stop signal: tid=" << tid;
138 break;
139 }
140
141 usleep(kSleepTimeMicroseconds);
142 *total_sleep_time_usec += kSleepTimeMicroseconds;
143 }
144
145 return -1;
146}
147#endif
148
149extern "C" JNIEXPORT jboolean JNICALL Java_Main_unwindOtherProcess(JNIEnv*, jobject, jint pid_int) {
150#if __linux__
151 // TODO: What to do on Valgrind?
152 pid_t pid = static_cast<pid_t>(pid_int);
153
154 // OK, this is painful. debuggerd uses ptrace to unwind other processes.
155
156 if (ptrace(PTRACE_ATTACH, pid, 0, 0)) {
157 // Were not able to attach, bad.
158 PLOG(ERROR) << "Failed to attach.";
159 kill(pid, SIGCONT);
160 return JNI_FALSE;
161 }
162
163 kill(pid, SIGSTOP);
164
165 bool detach_failed = false;
166 int total_sleep_time_usec = 0;
167 int signal = wait_for_sigstop(pid, &total_sleep_time_usec, &detach_failed);
168 if (signal == -1) {
169 LOG(WARNING) << "wait_for_sigstop failed.";
170 }
171
172 std::unique_ptr<Backtrace> bt(Backtrace::Create(pid, BACKTRACE_CURRENT_THREAD));
173 bool result = true;
174 if (!bt->Unwind(0, nullptr)) {
175 result = false;
176 } else if (bt->NumFrames() == 0) {
177 result = false;
178 }
179
180 if (result) {
181 // See comment in unwindInProcess for non-exact stack matching.
182 std::vector<std::string> seq = {
183 // "Java_Main_sleep", // The sleep function being executed in the
184 // other runtime.
185 // Note: For some reason, the name isn't
186 // resolved, so don't look for it right now.
187 "boolean Main.sleep(int, boolean, double)", // The corresponding Java native method frame.
188 "void Main.main(java.lang.String[])" // The Java entry method.
189 };
190
191 result = CheckStack(bt.get(), seq);
192 }
193
194 if (ptrace(PTRACE_DETACH, pid, 0, 0) != 0) {
195 PLOG(ERROR) << "Detach failed";
196 }
197
198 // Continue the process so we can kill it on the Java side.
199 kill(pid, SIGCONT);
200
201 return result ? JNI_TRUE : JNI_FALSE;
202#else
203 return JNI_FALSE;
204#endif
205}
206
207} // namespace art