blob: aeb996c68d1c3336943831c71515f177e7cf8f0c [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 Gampe43e72432019-05-14 16:15:24 -070028#include <android-base/file.h>
Andreas Gampe57943812017-12-06 21:39:13 -080029#include <android-base/logging.h>
30#include <android-base/stringprintf.h>
Andreas Gampe73810102015-04-22 18:57:06 -070031#include <backtrace/Backtrace.h>
32
David Sehr891a50e2017-10-27 17:01:07 -070033#include "base/file_utils.h"
Andreas Gampe85f1c572018-11-21 13:52:48 -080034#include "base/logging.h"
Andreas Gampe73810102015-04-22 18:57:06 -070035#include "base/macros.h"
David Srbecky1ed45152019-04-09 18:10:26 +010036#include "base/mutex.h"
David Sehrc431b9d2018-03-02 12:01:51 -080037#include "base/utils.h"
Andreas Gampe88da3b02015-06-12 20:38:49 -070038#include "gc/heap.h"
39#include "gc/space/image_space.h"
David Srbecky1ed45152019-04-09 18:10:26 +010040#include "jit/debugger_interface.h"
Andreas Gampe88da3b02015-06-12 20:38:49 -070041#include "oat_file.h"
Andreas Gamped482e732017-04-24 17:59:09 -070042#include "runtime.h"
Andreas Gampe73810102015-04-22 18:57:06 -070043
44namespace art {
45
46// For testing debuggerd. We do not have expected-death tests, so can't test this by default.
47// Code for this is copied from SignalTest.
48static constexpr bool kCauseSegfault = false;
49char* go_away_compiler_cfi = nullptr;
50
51static void CauseSegfault() {
52#if defined(__arm__) || defined(__i386__) || defined(__x86_64__) || defined(__aarch64__)
53 // On supported architectures we cause a real SEGV.
54 *go_away_compiler_cfi = 'a';
55#else
56 // On other architectures we simulate SEGV.
57 kill(getpid(), SIGSEGV);
58#endif
59}
60
David Srbeckycd56c6c2018-06-03 12:00:18 +010061extern "C" JNIEXPORT jint JNICALL Java_Main_startSecondaryProcess(JNIEnv*, jclass) {
David Srbecky39239872019-04-29 17:05:43 +010062 printf("Java_Main_startSecondaryProcess\n");
David Srbeckycd56c6c2018-06-03 12:00:18 +010063#if __linux__
64 // Get our command line so that we can use it to start identical process.
65 std::string cmdline; // null-separated and null-terminated arguments.
Andreas Gampe43e72432019-05-14 16:15:24 -070066 android::base::ReadFileToString("/proc/self/cmdline", &cmdline);
David Srbeckycd56c6c2018-06-03 12:00:18 +010067 cmdline = cmdline + "--secondary" + '\0'; // Let the child know it is a helper.
68
69 // Split the string into individual arguments suitable for execv.
70 std::vector<char*> argv;
71 for (size_t i = 0; i < cmdline.size(); i += strlen(&cmdline[i]) + 1) {
72 argv.push_back(&cmdline[i]);
Andreas Gampe73810102015-04-22 18:57:06 -070073 }
David Srbeckycd56c6c2018-06-03 12:00:18 +010074 argv.push_back(nullptr); // Terminate the list.
75
76 pid_t pid = fork();
77 if (pid < 0) {
78 LOG(FATAL) << "Fork failed";
79 } else if (pid == 0) {
80 execv(argv[0], argv.data());
81 exit(1);
82 }
83 return pid;
84#else
85 return 0;
86#endif
87}
88
89extern "C" JNIEXPORT jboolean JNICALL Java_Main_sigstop(JNIEnv*, jclass) {
David Srbecky39239872019-04-29 17:05:43 +010090 printf("Java_Main_sigstop\n");
David Srbeckycd56c6c2018-06-03 12:00:18 +010091#if __linux__
David Srbecky1ed45152019-04-09 18:10:26 +010092 MutexLock mu(Thread::Current(), *GetNativeDebugInfoLock()); // Avoid races with the JIT thread.
David Srbeckycd56c6c2018-06-03 12:00:18 +010093 raise(SIGSTOP);
94#endif
95 return true; // Prevent the compiler from tail-call optimizing this method away.
Andreas Gampe73810102015-04-22 18:57:06 -070096}
97
98// Helper to look for a sequence in the stack trace.
99#if __linux__
100static bool CheckStack(Backtrace* bt, const std::vector<std::string>& seq) {
101 size_t cur_search_index = 0; // The currently active index in seq.
102 CHECK_GT(seq.size(), 0U);
103
104 for (Backtrace::const_iterator it = bt->begin(); it != bt->end(); ++it) {
105 if (BacktraceMap::IsValid(it->map)) {
106 LOG(INFO) << "Got " << it->func_name << ", looking for " << seq[cur_search_index];
David Srbecky08a9af02018-01-18 13:05:01 +0000107 if (it->func_name.find(seq[cur_search_index]) != std::string::npos) {
Andreas Gampe73810102015-04-22 18:57:06 -0700108 cur_search_index++;
109 if (cur_search_index == seq.size()) {
110 return true;
111 }
112 }
113 }
114 }
115
Roland Levillain91d65e02016-01-19 15:59:16 +0000116 printf("Cannot find %s in backtrace:\n", seq[cur_search_index].c_str());
David Srbecky020c5432015-06-10 22:43:11 +0100117 for (Backtrace::const_iterator it = bt->begin(); it != bt->end(); ++it) {
118 if (BacktraceMap::IsValid(it->map)) {
David Srbeckyf38572d2018-02-05 15:45:14 +0000119 printf(" %s\n", Backtrace::FormatFrameData(&*it).c_str());
David Srbecky020c5432015-06-10 22:43:11 +0100120 }
121 }
122
Andreas Gampe73810102015-04-22 18:57:06 -0700123 return false;
124}
Andreas Gampe78da0d72016-09-05 12:30:49 -0700125
126static void MoreErrorInfo(pid_t pid, bool sig_quit_on_fail) {
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800127 PrintFileToLog(android::base::StringPrintf("/proc/%d/maps", pid), ::android::base::ERROR);
Andreas Gampe78da0d72016-09-05 12:30:49 -0700128
129 if (sig_quit_on_fail) {
130 int res = kill(pid, SIGQUIT);
131 if (res != 0) {
132 PLOG(ERROR) << "Failed to send signal";
133 }
134 }
135}
Andreas Gampe73810102015-04-22 18:57:06 -0700136#endif
137
David Srbeckycd56c6c2018-06-03 12:00:18 +0100138extern "C" JNIEXPORT jboolean JNICALL Java_Main_unwindInProcess(JNIEnv*, jclass) {
David Srbecky39239872019-04-29 17:05:43 +0100139 printf("Java_Main_unwindInProcess\n");
Andreas Gampe73810102015-04-22 18:57:06 -0700140#if __linux__
David Srbecky1ed45152019-04-09 18:10:26 +0100141 MutexLock mu(Thread::Current(), *GetNativeDebugInfoLock()); // Avoid races with the JIT thread.
142
Andreas Gampe73810102015-04-22 18:57:06 -0700143 std::unique_ptr<Backtrace> bt(Backtrace::Create(BACKTRACE_CURRENT_PROCESS, GetTid()));
144 if (!bt->Unwind(0, nullptr)) {
Roland Levillain91d65e02016-01-19 15:59:16 +0000145 printf("Cannot unwind in process.\n");
Andreas Gampe73810102015-04-22 18:57:06 -0700146 return JNI_FALSE;
147 } else if (bt->NumFrames() == 0) {
David Srbecky020c5432015-06-10 22:43:11 +0100148 printf("No frames for unwind in process.\n");
Andreas Gampe73810102015-04-22 18:57:06 -0700149 return JNI_FALSE;
150 }
151
152 // We cannot really parse an exact stack, as the optimizing compiler may inline some functions.
153 // This is also risky, as deduping might play a trick on us, so the test needs to make sure that
154 // only unique functions are being expected.
David Srbecky52886112016-01-22 13:56:47 +0000155 // "mini-debug-info" does not include parameters to save space.
Andreas Gampe73810102015-04-22 18:57:06 -0700156 std::vector<std::string> seq = {
David Srbeckycd56c6c2018-06-03 12:00:18 +0100157 "Java_Main_unwindInProcess", // This function.
158 "java.util.Arrays.binarySearch0", // Framework method.
Mathieu Chartier7cb707f2019-03-08 12:58:06 -0800159 "Base.$noinline$runTest", // Method in other dex file.
David Srbeckycd56c6c2018-06-03 12:00:18 +0100160 "Main.main" // The Java entry method.
David Srbecky52886112016-01-22 13:56:47 +0000161 };
Andreas Gampe73810102015-04-22 18:57:06 -0700162
David Srbecky08a9af02018-01-18 13:05:01 +0000163 bool result = CheckStack(bt.get(), seq);
Andreas Gampe73810102015-04-22 18:57:06 -0700164 if (!kCauseSegfault) {
165 return result ? JNI_TRUE : JNI_FALSE;
166 } else {
167 LOG(INFO) << "Result of check-stack: " << result;
168 }
169#endif
170
171 if (kCauseSegfault) {
172 CauseSegfault();
173 }
174
175 return JNI_FALSE;
176}
177
178#if __linux__
David Srbeckycd56c6c2018-06-03 12:00:18 +0100179static constexpr int kSleepTimeMicroseconds = 50000; // 0.05 seconds
180static constexpr int kMaxTotalSleepTimeMicroseconds = 10000000; // 10 seconds
Andreas Gampe73810102015-04-22 18:57:06 -0700181
182// Wait for a sigstop. This code is copied from libbacktrace.
183int wait_for_sigstop(pid_t tid, int* total_sleep_time_usec, bool* detach_failed ATTRIBUTE_UNUSED) {
184 for (;;) {
185 int status;
186 pid_t n = TEMP_FAILURE_RETRY(waitpid(tid, &status, __WALL | WNOHANG));
187 if (n == -1) {
188 PLOG(WARNING) << "waitpid failed: tid " << tid;
189 break;
190 } else if (n == tid) {
191 if (WIFSTOPPED(status)) {
192 return WSTOPSIG(status);
193 } else {
194 PLOG(ERROR) << "unexpected waitpid response: n=" << n << ", status=" << std::hex << status;
195 break;
196 }
197 }
198
199 if (*total_sleep_time_usec > kMaxTotalSleepTimeMicroseconds) {
200 PLOG(WARNING) << "timed out waiting for stop signal: tid=" << tid;
201 break;
202 }
203
204 usleep(kSleepTimeMicroseconds);
205 *total_sleep_time_usec += kSleepTimeMicroseconds;
206 }
207
208 return -1;
209}
210#endif
211
David Srbeckycd56c6c2018-06-03 12:00:18 +0100212extern "C" JNIEXPORT jboolean JNICALL Java_Main_unwindOtherProcess(JNIEnv*, jclass, jint pid_int) {
David Srbecky39239872019-04-29 17:05:43 +0100213 printf("Java_Main_unwindOtherProcess\n");
Andreas Gampe73810102015-04-22 18:57:06 -0700214#if __linux__
Andreas Gampe73810102015-04-22 18:57:06 -0700215 pid_t pid = static_cast<pid_t>(pid_int);
216
David Srbeckycd56c6c2018-06-03 12:00:18 +0100217 // SEIZE is like ATTACH, but it does not stop the process (we let it stop itself).
218 if (ptrace(PTRACE_SEIZE, pid, 0, 0)) {
Andreas Gampe73810102015-04-22 18:57:06 -0700219 // Were not able to attach, bad.
David Srbecky020c5432015-06-10 22:43:11 +0100220 printf("Failed to attach to other process.\n");
Andreas Gampe73810102015-04-22 18:57:06 -0700221 PLOG(ERROR) << "Failed to attach.";
David Srbeckya70e5b92015-06-17 03:52:54 +0100222 kill(pid, SIGKILL);
Andreas Gampe73810102015-04-22 18:57:06 -0700223 return JNI_FALSE;
224 }
225
Andreas Gampe73810102015-04-22 18:57:06 -0700226 bool detach_failed = false;
227 int total_sleep_time_usec = 0;
228 int signal = wait_for_sigstop(pid, &total_sleep_time_usec, &detach_failed);
David Srbeckycd56c6c2018-06-03 12:00:18 +0100229 if (signal != SIGSTOP) {
David Srbecky39239872019-04-29 17:05:43 +0100230 printf("wait_for_sigstop failed.\n");
David Srbeckycd56c6c2018-06-03 12:00:18 +0100231 return JNI_FALSE;
Andreas Gampe73810102015-04-22 18:57:06 -0700232 }
233
234 std::unique_ptr<Backtrace> bt(Backtrace::Create(pid, BACKTRACE_CURRENT_THREAD));
235 bool result = true;
236 if (!bt->Unwind(0, nullptr)) {
Roland Levillain91d65e02016-01-19 15:59:16 +0000237 printf("Cannot unwind other process.\n");
Andreas Gampe73810102015-04-22 18:57:06 -0700238 result = false;
239 } else if (bt->NumFrames() == 0) {
David Srbecky020c5432015-06-10 22:43:11 +0100240 printf("No frames for unwind of other process.\n");
Andreas Gampe73810102015-04-22 18:57:06 -0700241 result = false;
242 }
243
244 if (result) {
245 // See comment in unwindInProcess for non-exact stack matching.
David Srbecky52886112016-01-22 13:56:47 +0000246 // "mini-debug-info" does not include parameters to save space.
Andreas Gampe73810102015-04-22 18:57:06 -0700247 std::vector<std::string> seq = {
David Srbeckycd56c6c2018-06-03 12:00:18 +0100248 "Java_Main_sigstop", // The stop function in the other process.
249 "java.util.Arrays.binarySearch0", // Framework method.
Mathieu Chartier7cb707f2019-03-08 12:58:06 -0800250 "Base.$noinline$runTest", // Method in other dex file.
David Srbeckycd56c6c2018-06-03 12:00:18 +0100251 "Main.main" // The Java entry method.
David Srbecky52886112016-01-22 13:56:47 +0000252 };
Andreas Gampe73810102015-04-22 18:57:06 -0700253
David Srbecky08a9af02018-01-18 13:05:01 +0000254 result = CheckStack(bt.get(), seq);
Andreas Gampe73810102015-04-22 18:57:06 -0700255 }
256
Andreas Gampe78da0d72016-09-05 12:30:49 -0700257 constexpr bool kSigQuitOnFail = true;
258 if (!result) {
David Srbecky39239872019-04-29 17:05:43 +0100259 printf("Failed to unwind secondary with pid %d\n", pid);
Andreas Gampe78da0d72016-09-05 12:30:49 -0700260 MoreErrorInfo(pid, kSigQuitOnFail);
261 }
262
Andreas Gampe73810102015-04-22 18:57:06 -0700263 if (ptrace(PTRACE_DETACH, pid, 0, 0) != 0) {
David Srbecky39239872019-04-29 17:05:43 +0100264 printf("Detach failed\n");
Andreas Gampe73810102015-04-22 18:57:06 -0700265 PLOG(ERROR) << "Detach failed";
266 }
267
Andreas Gampe78da0d72016-09-05 12:30:49 -0700268 // If we failed to unwind and induced an ANR dump, give the child some time (20s).
269 if (!result && kSigQuitOnFail) {
270 sleep(20);
271 }
272
David Srbeckya70e5b92015-06-17 03:52:54 +0100273 // Kill the other process once we are done with it.
274 kill(pid, SIGKILL);
Andreas Gampe73810102015-04-22 18:57:06 -0700275
276 return result ? JNI_TRUE : JNI_FALSE;
277#else
David Srbecky39239872019-04-29 17:05:43 +0100278 printf("Remote unwind supported only on linux\n");
Andreas Gampe3faa5812015-09-14 13:59:51 -0700279 UNUSED(pid_int);
Andreas Gampe73810102015-04-22 18:57:06 -0700280 return JNI_FALSE;
281#endif
282}
283
284} // namespace art