blob: e61857f83d719845b73064c25aeaf8e258af13ee [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
David Srbecky020c5432015-06-10 22:43:11 +010076 printf("Can not find %s in backtrace:\n", seq[cur_search_index].c_str());
77 for (Backtrace::const_iterator it = bt->begin(); it != bt->end(); ++it) {
78 if (BacktraceMap::IsValid(it->map)) {
79 printf(" %s\n", it->func_name.c_str());
80 }
81 }
82
Andreas Gampe73810102015-04-22 18:57:06 -070083 return false;
84}
85#endif
86
87extern "C" JNIEXPORT jboolean JNICALL Java_Main_unwindInProcess(JNIEnv*, jobject, jint, jboolean) {
88#if __linux__
89 // TODO: What to do on Valgrind?
90
91 std::unique_ptr<Backtrace> bt(Backtrace::Create(BACKTRACE_CURRENT_PROCESS, GetTid()));
92 if (!bt->Unwind(0, nullptr)) {
David Srbecky020c5432015-06-10 22:43:11 +010093 printf("Can not unwind in process.\n");
Andreas Gampe73810102015-04-22 18:57:06 -070094 return JNI_FALSE;
95 } else if (bt->NumFrames() == 0) {
David Srbecky020c5432015-06-10 22:43:11 +010096 printf("No frames for unwind in process.\n");
Andreas Gampe73810102015-04-22 18:57:06 -070097 return JNI_FALSE;
98 }
99
100 // We cannot really parse an exact stack, as the optimizing compiler may inline some functions.
101 // This is also risky, as deduping might play a trick on us, so the test needs to make sure that
102 // only unique functions are being expected.
103 std::vector<std::string> seq = {
104 "Java_Main_unwindInProcess", // This function.
105 "boolean Main.unwindInProcess(int, boolean)", // The corresponding Java native method frame.
106 "void Main.main(java.lang.String[])" // The Java entry method.
107 };
108
109 bool result = CheckStack(bt.get(), seq);
110 if (!kCauseSegfault) {
111 return result ? JNI_TRUE : JNI_FALSE;
112 } else {
113 LOG(INFO) << "Result of check-stack: " << result;
114 }
115#endif
116
117 if (kCauseSegfault) {
118 CauseSegfault();
119 }
120
121 return JNI_FALSE;
122}
123
124#if __linux__
125static constexpr int kSleepTimeMicroseconds = 50000; // 0.05 seconds
126static constexpr int kMaxTotalSleepTimeMicroseconds = 1000000; // 1 second
127
128// Wait for a sigstop. This code is copied from libbacktrace.
129int wait_for_sigstop(pid_t tid, int* total_sleep_time_usec, bool* detach_failed ATTRIBUTE_UNUSED) {
130 for (;;) {
131 int status;
132 pid_t n = TEMP_FAILURE_RETRY(waitpid(tid, &status, __WALL | WNOHANG));
133 if (n == -1) {
134 PLOG(WARNING) << "waitpid failed: tid " << tid;
135 break;
136 } else if (n == tid) {
137 if (WIFSTOPPED(status)) {
138 return WSTOPSIG(status);
139 } else {
140 PLOG(ERROR) << "unexpected waitpid response: n=" << n << ", status=" << std::hex << status;
141 break;
142 }
143 }
144
145 if (*total_sleep_time_usec > kMaxTotalSleepTimeMicroseconds) {
146 PLOG(WARNING) << "timed out waiting for stop signal: tid=" << tid;
147 break;
148 }
149
150 usleep(kSleepTimeMicroseconds);
151 *total_sleep_time_usec += kSleepTimeMicroseconds;
152 }
153
154 return -1;
155}
156#endif
157
158extern "C" JNIEXPORT jboolean JNICALL Java_Main_unwindOtherProcess(JNIEnv*, jobject, jint pid_int) {
159#if __linux__
160 // TODO: What to do on Valgrind?
161 pid_t pid = static_cast<pid_t>(pid_int);
162
163 // OK, this is painful. debuggerd uses ptrace to unwind other processes.
164
165 if (ptrace(PTRACE_ATTACH, pid, 0, 0)) {
166 // Were not able to attach, bad.
David Srbecky020c5432015-06-10 22:43:11 +0100167 printf("Failed to attach to other process.\n");
Andreas Gampe73810102015-04-22 18:57:06 -0700168 PLOG(ERROR) << "Failed to attach.";
169 kill(pid, SIGCONT);
170 return JNI_FALSE;
171 }
172
173 kill(pid, SIGSTOP);
174
175 bool detach_failed = false;
176 int total_sleep_time_usec = 0;
177 int signal = wait_for_sigstop(pid, &total_sleep_time_usec, &detach_failed);
178 if (signal == -1) {
179 LOG(WARNING) << "wait_for_sigstop failed.";
180 }
181
182 std::unique_ptr<Backtrace> bt(Backtrace::Create(pid, BACKTRACE_CURRENT_THREAD));
183 bool result = true;
184 if (!bt->Unwind(0, nullptr)) {
David Srbecky020c5432015-06-10 22:43:11 +0100185 printf("Can not unwind other process.\n");
Andreas Gampe73810102015-04-22 18:57:06 -0700186 result = false;
187 } else if (bt->NumFrames() == 0) {
David Srbecky020c5432015-06-10 22:43:11 +0100188 printf("No frames for unwind of other process.\n");
Andreas Gampe73810102015-04-22 18:57:06 -0700189 result = false;
190 }
191
192 if (result) {
193 // See comment in unwindInProcess for non-exact stack matching.
194 std::vector<std::string> seq = {
195 // "Java_Main_sleep", // The sleep function being executed in the
196 // other runtime.
197 // Note: For some reason, the name isn't
198 // resolved, so don't look for it right now.
199 "boolean Main.sleep(int, boolean, double)", // The corresponding Java native method frame.
Andreas Gampe73810102015-04-22 18:57:06 -0700200 "void Main.main(java.lang.String[])" // The Java entry method.
201 };
202
203 result = CheckStack(bt.get(), seq);
204 }
205
206 if (ptrace(PTRACE_DETACH, pid, 0, 0) != 0) {
207 PLOG(ERROR) << "Detach failed";
208 }
209
210 // Continue the process so we can kill it on the Java side.
211 kill(pid, SIGCONT);
212
213 return result ? JNI_TRUE : JNI_FALSE;
214#else
215 return JNI_FALSE;
216#endif
217}
218
219} // namespace art