blob: a4d0d0cbe4c4a86379e98f3f5c1e83d0e9b61483 [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>
Andreas Gampe73810102015-04-22 18:57:06 -070021#include <sys/ptrace.h>
22#include <sys/wait.h>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070023#include <unistd.h>
Andreas Gampe73810102015-04-22 18:57:06 -070024#endif
25
26#include "jni.h"
27
Andreas Gampe57943812017-12-06 21:39:13 -080028#include <android-base/logging.h>
29#include <android-base/stringprintf.h>
Andreas Gampe73810102015-04-22 18:57:06 -070030#include <backtrace/Backtrace.h>
31
David Sehr891a50e2017-10-27 17:01:07 -070032#include "base/file_utils.h"
Andreas Gampe73810102015-04-22 18:57:06 -070033#include "base/macros.h"
David Sehrc431b9d2018-03-02 12:01:51 -080034#include "base/utils.h"
Andreas Gampe88da3b02015-06-12 20:38:49 -070035#include "gc/heap.h"
36#include "gc/space/image_space.h"
37#include "oat_file.h"
Andreas Gamped482e732017-04-24 17:59:09 -070038#include "runtime.h"
Andreas Gampe73810102015-04-22 18:57:06 -070039
40namespace art {
41
42// For testing debuggerd. We do not have expected-death tests, so can't test this by default.
43// Code for this is copied from SignalTest.
44static constexpr bool kCauseSegfault = false;
45char* go_away_compiler_cfi = nullptr;
46
47static void CauseSegfault() {
48#if defined(__arm__) || defined(__i386__) || defined(__x86_64__) || defined(__aarch64__)
49 // On supported architectures we cause a real SEGV.
50 *go_away_compiler_cfi = 'a';
51#else
52 // On other architectures we simulate SEGV.
53 kill(getpid(), SIGSEGV);
54#endif
55}
56
57extern "C" JNIEXPORT jboolean JNICALL Java_Main_sleep(JNIEnv*, jobject, jint, jboolean, jdouble) {
58 // Keep pausing.
David Srbecky03bf1742016-02-15 16:54:34 +000059 printf("Going to sleep\n");
Andreas Gampe73810102015-04-22 18:57:06 -070060 for (;;) {
Andreas Gampe78da0d72016-09-05 12:30:49 -070061 sleep(1);
Andreas Gampe73810102015-04-22 18:57:06 -070062 }
63}
64
65// Helper to look for a sequence in the stack trace.
66#if __linux__
67static bool CheckStack(Backtrace* bt, const std::vector<std::string>& seq) {
68 size_t cur_search_index = 0; // The currently active index in seq.
69 CHECK_GT(seq.size(), 0U);
70
71 for (Backtrace::const_iterator it = bt->begin(); it != bt->end(); ++it) {
72 if (BacktraceMap::IsValid(it->map)) {
73 LOG(INFO) << "Got " << it->func_name << ", looking for " << seq[cur_search_index];
David Srbecky08a9af02018-01-18 13:05:01 +000074 if (it->func_name.find(seq[cur_search_index]) != std::string::npos) {
Andreas Gampe73810102015-04-22 18:57:06 -070075 cur_search_index++;
76 if (cur_search_index == seq.size()) {
77 return true;
78 }
79 }
80 }
81 }
82
Roland Levillain91d65e02016-01-19 15:59:16 +000083 printf("Cannot find %s in backtrace:\n", seq[cur_search_index].c_str());
David Srbecky020c5432015-06-10 22:43:11 +010084 for (Backtrace::const_iterator it = bt->begin(); it != bt->end(); ++it) {
85 if (BacktraceMap::IsValid(it->map)) {
David Srbeckyf38572d2018-02-05 15:45:14 +000086 printf(" %s\n", Backtrace::FormatFrameData(&*it).c_str());
David Srbecky020c5432015-06-10 22:43:11 +010087 }
88 }
89
Andreas Gampe73810102015-04-22 18:57:06 -070090 return false;
91}
Andreas Gampe78da0d72016-09-05 12:30:49 -070092
93static void MoreErrorInfo(pid_t pid, bool sig_quit_on_fail) {
94 printf("Secondary pid is %d\n", pid);
95
Andreas Gampe46ee31b2016-12-14 10:11:49 -080096 PrintFileToLog(android::base::StringPrintf("/proc/%d/maps", pid), ::android::base::ERROR);
Andreas Gampe78da0d72016-09-05 12:30:49 -070097
98 if (sig_quit_on_fail) {
99 int res = kill(pid, SIGQUIT);
100 if (res != 0) {
101 PLOG(ERROR) << "Failed to send signal";
102 }
103 }
104}
Andreas Gampe73810102015-04-22 18:57:06 -0700105#endif
106
David Srbecky52886112016-01-22 13:56:47 +0000107extern "C" JNIEXPORT jboolean JNICALL Java_Main_unwindInProcess(
108 JNIEnv*,
109 jobject,
David Srbecky08a9af02018-01-18 13:05:01 +0000110 jboolean,
David Srbecky52886112016-01-22 13:56:47 +0000111 jint,
112 jboolean) {
Andreas Gampe73810102015-04-22 18:57:06 -0700113#if __linux__
Andreas Gampe73810102015-04-22 18:57:06 -0700114 std::unique_ptr<Backtrace> bt(Backtrace::Create(BACKTRACE_CURRENT_PROCESS, GetTid()));
115 if (!bt->Unwind(0, nullptr)) {
Roland Levillain91d65e02016-01-19 15:59:16 +0000116 printf("Cannot unwind in process.\n");
Andreas Gampe73810102015-04-22 18:57:06 -0700117 return JNI_FALSE;
118 } else if (bt->NumFrames() == 0) {
David Srbecky020c5432015-06-10 22:43:11 +0100119 printf("No frames for unwind in process.\n");
Andreas Gampe73810102015-04-22 18:57:06 -0700120 return JNI_FALSE;
121 }
122
123 // We cannot really parse an exact stack, as the optimizing compiler may inline some functions.
124 // This is also risky, as deduping might play a trick on us, so the test needs to make sure that
125 // only unique functions are being expected.
David Srbecky52886112016-01-22 13:56:47 +0000126 // "mini-debug-info" does not include parameters to save space.
Andreas Gampe73810102015-04-22 18:57:06 -0700127 std::vector<std::string> seq = {
128 "Java_Main_unwindInProcess", // This function.
David Srbecky08a9af02018-01-18 13:05:01 +0000129 "java.util.Arrays.binarySearch0", // Framework method.
David Srbecky872be3d2018-02-05 17:30:01 +0000130 "Base.runBase", // Method in other dex file.
David Srbecky52886112016-01-22 13:56:47 +0000131 "Main.main" // The Java entry method.
132 };
Andreas Gampe73810102015-04-22 18:57:06 -0700133
David Srbecky08a9af02018-01-18 13:05:01 +0000134 bool result = CheckStack(bt.get(), seq);
Andreas Gampe73810102015-04-22 18:57:06 -0700135 if (!kCauseSegfault) {
136 return result ? JNI_TRUE : JNI_FALSE;
137 } else {
138 LOG(INFO) << "Result of check-stack: " << result;
139 }
140#endif
141
142 if (kCauseSegfault) {
143 CauseSegfault();
144 }
145
146 return JNI_FALSE;
147}
148
149#if __linux__
150static constexpr int kSleepTimeMicroseconds = 50000; // 0.05 seconds
151static constexpr int kMaxTotalSleepTimeMicroseconds = 1000000; // 1 second
152
153// Wait for a sigstop. This code is copied from libbacktrace.
154int wait_for_sigstop(pid_t tid, int* total_sleep_time_usec, bool* detach_failed ATTRIBUTE_UNUSED) {
155 for (;;) {
156 int status;
157 pid_t n = TEMP_FAILURE_RETRY(waitpid(tid, &status, __WALL | WNOHANG));
158 if (n == -1) {
159 PLOG(WARNING) << "waitpid failed: tid " << tid;
160 break;
161 } else if (n == tid) {
162 if (WIFSTOPPED(status)) {
163 return WSTOPSIG(status);
164 } else {
165 PLOG(ERROR) << "unexpected waitpid response: n=" << n << ", status=" << std::hex << status;
166 break;
167 }
168 }
169
170 if (*total_sleep_time_usec > kMaxTotalSleepTimeMicroseconds) {
171 PLOG(WARNING) << "timed out waiting for stop signal: tid=" << tid;
172 break;
173 }
174
175 usleep(kSleepTimeMicroseconds);
176 *total_sleep_time_usec += kSleepTimeMicroseconds;
177 }
178
179 return -1;
180}
181#endif
182
David Srbecky52886112016-01-22 13:56:47 +0000183extern "C" JNIEXPORT jboolean JNICALL Java_Main_unwindOtherProcess(
184 JNIEnv*,
185 jobject,
David Srbecky08a9af02018-01-18 13:05:01 +0000186 jboolean,
David Srbecky52886112016-01-22 13:56:47 +0000187 jint pid_int) {
Andreas Gampe73810102015-04-22 18:57:06 -0700188#if __linux__
Andreas Gampe73810102015-04-22 18:57:06 -0700189 pid_t pid = static_cast<pid_t>(pid_int);
190
191 // OK, this is painful. debuggerd uses ptrace to unwind other processes.
192
193 if (ptrace(PTRACE_ATTACH, pid, 0, 0)) {
194 // Were not able to attach, bad.
David Srbecky020c5432015-06-10 22:43:11 +0100195 printf("Failed to attach to other process.\n");
Andreas Gampe73810102015-04-22 18:57:06 -0700196 PLOG(ERROR) << "Failed to attach.";
David Srbeckya70e5b92015-06-17 03:52:54 +0100197 kill(pid, SIGKILL);
Andreas Gampe73810102015-04-22 18:57:06 -0700198 return JNI_FALSE;
199 }
200
201 kill(pid, SIGSTOP);
202
203 bool detach_failed = false;
204 int total_sleep_time_usec = 0;
205 int signal = wait_for_sigstop(pid, &total_sleep_time_usec, &detach_failed);
206 if (signal == -1) {
207 LOG(WARNING) << "wait_for_sigstop failed.";
208 }
209
210 std::unique_ptr<Backtrace> bt(Backtrace::Create(pid, BACKTRACE_CURRENT_THREAD));
211 bool result = true;
212 if (!bt->Unwind(0, nullptr)) {
Roland Levillain91d65e02016-01-19 15:59:16 +0000213 printf("Cannot unwind other process.\n");
Andreas Gampe73810102015-04-22 18:57:06 -0700214 result = false;
215 } else if (bt->NumFrames() == 0) {
David Srbecky020c5432015-06-10 22:43:11 +0100216 printf("No frames for unwind of other process.\n");
Andreas Gampe73810102015-04-22 18:57:06 -0700217 result = false;
218 }
219
220 if (result) {
221 // See comment in unwindInProcess for non-exact stack matching.
David Srbecky52886112016-01-22 13:56:47 +0000222 // "mini-debug-info" does not include parameters to save space.
Andreas Gampe73810102015-04-22 18:57:06 -0700223 std::vector<std::string> seq = {
David Srbecky946bb092018-03-09 17:23:01 +0000224 "Java_Main_sleep", // The sleep function in the other process.
David Srbecky08a9af02018-01-18 13:05:01 +0000225 "java.util.Arrays.binarySearch0", // Framework method.
David Srbecky872be3d2018-02-05 17:30:01 +0000226 "Base.runBase", // Method in other dex file.
David Srbecky52886112016-01-22 13:56:47 +0000227 "Main.main" // The Java entry method.
228 };
Andreas Gampe73810102015-04-22 18:57:06 -0700229
David Srbecky08a9af02018-01-18 13:05:01 +0000230 result = CheckStack(bt.get(), seq);
Andreas Gampe73810102015-04-22 18:57:06 -0700231 }
232
Andreas Gampe78da0d72016-09-05 12:30:49 -0700233 constexpr bool kSigQuitOnFail = true;
234 if (!result) {
235 MoreErrorInfo(pid, kSigQuitOnFail);
236 }
237
Andreas Gampe73810102015-04-22 18:57:06 -0700238 if (ptrace(PTRACE_DETACH, pid, 0, 0) != 0) {
239 PLOG(ERROR) << "Detach failed";
240 }
241
Andreas Gampe78da0d72016-09-05 12:30:49 -0700242 // If we failed to unwind and induced an ANR dump, give the child some time (20s).
243 if (!result && kSigQuitOnFail) {
244 sleep(20);
245 }
246
David Srbeckya70e5b92015-06-17 03:52:54 +0100247 // Kill the other process once we are done with it.
248 kill(pid, SIGKILL);
Andreas Gampe73810102015-04-22 18:57:06 -0700249
250 return result ? JNI_TRUE : JNI_FALSE;
251#else
Andreas Gampe3faa5812015-09-14 13:59:51 -0700252 UNUSED(pid_int);
Andreas Gampe73810102015-04-22 18:57:06 -0700253 return JNI_FALSE;
254#endif
255}
256
257} // namespace art