Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 1 | /* |
| 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 Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 21 | #include <sys/ptrace.h> |
| 22 | #include <sys/wait.h> |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 23 | #include <unistd.h> |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 24 | #endif |
| 25 | |
| 26 | #include "jni.h" |
| 27 | |
Andreas Gampe | 43e7243 | 2019-05-14 16:15:24 -0700 | [diff] [blame] | 28 | #include <android-base/file.h> |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 29 | #include <android-base/logging.h> |
| 30 | #include <android-base/stringprintf.h> |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 31 | #include <backtrace/Backtrace.h> |
| 32 | |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 33 | #include "base/file_utils.h" |
Andreas Gampe | 85f1c57 | 2018-11-21 13:52:48 -0800 | [diff] [blame] | 34 | #include "base/logging.h" |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 35 | #include "base/macros.h" |
David Srbecky | 1ed4515 | 2019-04-09 18:10:26 +0100 | [diff] [blame] | 36 | #include "base/mutex.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 37 | #include "base/utils.h" |
Andreas Gampe | 88da3b0 | 2015-06-12 20:38:49 -0700 | [diff] [blame] | 38 | #include "gc/heap.h" |
| 39 | #include "gc/space/image_space.h" |
David Srbecky | 1ed4515 | 2019-04-09 18:10:26 +0100 | [diff] [blame] | 40 | #include "jit/debugger_interface.h" |
Andreas Gampe | 88da3b0 | 2015-06-12 20:38:49 -0700 | [diff] [blame] | 41 | #include "oat_file.h" |
Andreas Gampe | d482e73 | 2017-04-24 17:59:09 -0700 | [diff] [blame] | 42 | #include "runtime.h" |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 43 | |
| 44 | namespace 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. |
| 48 | static constexpr bool kCauseSegfault = false; |
| 49 | char* go_away_compiler_cfi = nullptr; |
| 50 | |
| 51 | static 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 Srbecky | cd56c6c | 2018-06-03 12:00:18 +0100 | [diff] [blame] | 61 | extern "C" JNIEXPORT jint JNICALL Java_Main_startSecondaryProcess(JNIEnv*, jclass) { |
David Srbecky | 3923987 | 2019-04-29 17:05:43 +0100 | [diff] [blame] | 62 | printf("Java_Main_startSecondaryProcess\n"); |
David Srbecky | cd56c6c | 2018-06-03 12:00:18 +0100 | [diff] [blame] | 63 | #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 Gampe | 43e7243 | 2019-05-14 16:15:24 -0700 | [diff] [blame] | 66 | android::base::ReadFileToString("/proc/self/cmdline", &cmdline); |
David Srbecky | cd56c6c | 2018-06-03 12:00:18 +0100 | [diff] [blame] | 67 | 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 Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 73 | } |
David Srbecky | cd56c6c | 2018-06-03 12:00:18 +0100 | [diff] [blame] | 74 | 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 | |
| 89 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_sigstop(JNIEnv*, jclass) { |
David Srbecky | 3923987 | 2019-04-29 17:05:43 +0100 | [diff] [blame] | 90 | printf("Java_Main_sigstop\n"); |
David Srbecky | cd56c6c | 2018-06-03 12:00:18 +0100 | [diff] [blame] | 91 | #if __linux__ |
David Srbecky | 1ed4515 | 2019-04-09 18:10:26 +0100 | [diff] [blame] | 92 | MutexLock mu(Thread::Current(), *GetNativeDebugInfoLock()); // Avoid races with the JIT thread. |
David Srbecky | cd56c6c | 2018-06-03 12:00:18 +0100 | [diff] [blame] | 93 | raise(SIGSTOP); |
| 94 | #endif |
| 95 | return true; // Prevent the compiler from tail-call optimizing this method away. |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | // Helper to look for a sequence in the stack trace. |
| 99 | #if __linux__ |
| 100 | static 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 Srbecky | 08a9af0 | 2018-01-18 13:05:01 +0000 | [diff] [blame] | 107 | if (it->func_name.find(seq[cur_search_index]) != std::string::npos) { |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 108 | cur_search_index++; |
| 109 | if (cur_search_index == seq.size()) { |
| 110 | return true; |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
Roland Levillain | 91d65e0 | 2016-01-19 15:59:16 +0000 | [diff] [blame] | 116 | printf("Cannot find %s in backtrace:\n", seq[cur_search_index].c_str()); |
David Srbecky | 020c543 | 2015-06-10 22:43:11 +0100 | [diff] [blame] | 117 | for (Backtrace::const_iterator it = bt->begin(); it != bt->end(); ++it) { |
| 118 | if (BacktraceMap::IsValid(it->map)) { |
David Srbecky | f38572d | 2018-02-05 15:45:14 +0000 | [diff] [blame] | 119 | printf(" %s\n", Backtrace::FormatFrameData(&*it).c_str()); |
David Srbecky | 020c543 | 2015-06-10 22:43:11 +0100 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 123 | return false; |
| 124 | } |
Andreas Gampe | 78da0d7 | 2016-09-05 12:30:49 -0700 | [diff] [blame] | 125 | |
| 126 | static void MoreErrorInfo(pid_t pid, bool sig_quit_on_fail) { |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 127 | PrintFileToLog(android::base::StringPrintf("/proc/%d/maps", pid), ::android::base::ERROR); |
Andreas Gampe | 78da0d7 | 2016-09-05 12:30:49 -0700 | [diff] [blame] | 128 | |
| 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 Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 136 | #endif |
| 137 | |
David Srbecky | cd56c6c | 2018-06-03 12:00:18 +0100 | [diff] [blame] | 138 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_unwindInProcess(JNIEnv*, jclass) { |
David Srbecky | 3923987 | 2019-04-29 17:05:43 +0100 | [diff] [blame] | 139 | printf("Java_Main_unwindInProcess\n"); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 140 | #if __linux__ |
David Srbecky | 1ed4515 | 2019-04-09 18:10:26 +0100 | [diff] [blame] | 141 | MutexLock mu(Thread::Current(), *GetNativeDebugInfoLock()); // Avoid races with the JIT thread. |
| 142 | |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 143 | std::unique_ptr<Backtrace> bt(Backtrace::Create(BACKTRACE_CURRENT_PROCESS, GetTid())); |
| 144 | if (!bt->Unwind(0, nullptr)) { |
Roland Levillain | 91d65e0 | 2016-01-19 15:59:16 +0000 | [diff] [blame] | 145 | printf("Cannot unwind in process.\n"); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 146 | return JNI_FALSE; |
| 147 | } else if (bt->NumFrames() == 0) { |
David Srbecky | 020c543 | 2015-06-10 22:43:11 +0100 | [diff] [blame] | 148 | printf("No frames for unwind in process.\n"); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 149 | 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 Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 155 | // "mini-debug-info" does not include parameters to save space. |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 156 | std::vector<std::string> seq = { |
David Srbecky | cd56c6c | 2018-06-03 12:00:18 +0100 | [diff] [blame] | 157 | "Java_Main_unwindInProcess", // This function. |
| 158 | "java.util.Arrays.binarySearch0", // Framework method. |
Mathieu Chartier | 7cb707f | 2019-03-08 12:58:06 -0800 | [diff] [blame] | 159 | "Base.$noinline$runTest", // Method in other dex file. |
David Srbecky | cd56c6c | 2018-06-03 12:00:18 +0100 | [diff] [blame] | 160 | "Main.main" // The Java entry method. |
David Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 161 | }; |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 162 | |
David Srbecky | 08a9af0 | 2018-01-18 13:05:01 +0000 | [diff] [blame] | 163 | bool result = CheckStack(bt.get(), seq); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 164 | 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 Srbecky | cd56c6c | 2018-06-03 12:00:18 +0100 | [diff] [blame] | 179 | static constexpr int kSleepTimeMicroseconds = 50000; // 0.05 seconds |
| 180 | static constexpr int kMaxTotalSleepTimeMicroseconds = 10000000; // 10 seconds |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 181 | |
| 182 | // Wait for a sigstop. This code is copied from libbacktrace. |
| 183 | int 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 Srbecky | cd56c6c | 2018-06-03 12:00:18 +0100 | [diff] [blame] | 212 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_unwindOtherProcess(JNIEnv*, jclass, jint pid_int) { |
David Srbecky | 3923987 | 2019-04-29 17:05:43 +0100 | [diff] [blame] | 213 | printf("Java_Main_unwindOtherProcess\n"); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 214 | #if __linux__ |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 215 | pid_t pid = static_cast<pid_t>(pid_int); |
| 216 | |
David Srbecky | cd56c6c | 2018-06-03 12:00:18 +0100 | [diff] [blame] | 217 | // 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 Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 219 | // Were not able to attach, bad. |
David Srbecky | 020c543 | 2015-06-10 22:43:11 +0100 | [diff] [blame] | 220 | printf("Failed to attach to other process.\n"); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 221 | PLOG(ERROR) << "Failed to attach."; |
David Srbecky | a70e5b9 | 2015-06-17 03:52:54 +0100 | [diff] [blame] | 222 | kill(pid, SIGKILL); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 223 | return JNI_FALSE; |
| 224 | } |
| 225 | |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 226 | 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 Srbecky | cd56c6c | 2018-06-03 12:00:18 +0100 | [diff] [blame] | 229 | if (signal != SIGSTOP) { |
David Srbecky | 3923987 | 2019-04-29 17:05:43 +0100 | [diff] [blame] | 230 | printf("wait_for_sigstop failed.\n"); |
David Srbecky | cd56c6c | 2018-06-03 12:00:18 +0100 | [diff] [blame] | 231 | return JNI_FALSE; |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | std::unique_ptr<Backtrace> bt(Backtrace::Create(pid, BACKTRACE_CURRENT_THREAD)); |
| 235 | bool result = true; |
| 236 | if (!bt->Unwind(0, nullptr)) { |
Roland Levillain | 91d65e0 | 2016-01-19 15:59:16 +0000 | [diff] [blame] | 237 | printf("Cannot unwind other process.\n"); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 238 | result = false; |
| 239 | } else if (bt->NumFrames() == 0) { |
David Srbecky | 020c543 | 2015-06-10 22:43:11 +0100 | [diff] [blame] | 240 | printf("No frames for unwind of other process.\n"); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 241 | result = false; |
| 242 | } |
| 243 | |
| 244 | if (result) { |
| 245 | // See comment in unwindInProcess for non-exact stack matching. |
David Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 246 | // "mini-debug-info" does not include parameters to save space. |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 247 | std::vector<std::string> seq = { |
David Srbecky | cd56c6c | 2018-06-03 12:00:18 +0100 | [diff] [blame] | 248 | "Java_Main_sigstop", // The stop function in the other process. |
| 249 | "java.util.Arrays.binarySearch0", // Framework method. |
Mathieu Chartier | 7cb707f | 2019-03-08 12:58:06 -0800 | [diff] [blame] | 250 | "Base.$noinline$runTest", // Method in other dex file. |
David Srbecky | cd56c6c | 2018-06-03 12:00:18 +0100 | [diff] [blame] | 251 | "Main.main" // The Java entry method. |
David Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 252 | }; |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 253 | |
David Srbecky | 08a9af0 | 2018-01-18 13:05:01 +0000 | [diff] [blame] | 254 | result = CheckStack(bt.get(), seq); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Andreas Gampe | 78da0d7 | 2016-09-05 12:30:49 -0700 | [diff] [blame] | 257 | constexpr bool kSigQuitOnFail = true; |
| 258 | if (!result) { |
David Srbecky | 3923987 | 2019-04-29 17:05:43 +0100 | [diff] [blame] | 259 | printf("Failed to unwind secondary with pid %d\n", pid); |
Andreas Gampe | 78da0d7 | 2016-09-05 12:30:49 -0700 | [diff] [blame] | 260 | MoreErrorInfo(pid, kSigQuitOnFail); |
| 261 | } |
| 262 | |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 263 | if (ptrace(PTRACE_DETACH, pid, 0, 0) != 0) { |
David Srbecky | 3923987 | 2019-04-29 17:05:43 +0100 | [diff] [blame] | 264 | printf("Detach failed\n"); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 265 | PLOG(ERROR) << "Detach failed"; |
| 266 | } |
| 267 | |
Andreas Gampe | 78da0d7 | 2016-09-05 12:30:49 -0700 | [diff] [blame] | 268 | // 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 Srbecky | a70e5b9 | 2015-06-17 03:52:54 +0100 | [diff] [blame] | 273 | // Kill the other process once we are done with it. |
| 274 | kill(pid, SIGKILL); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 275 | |
| 276 | return result ? JNI_TRUE : JNI_FALSE; |
| 277 | #else |
David Srbecky | 3923987 | 2019-04-29 17:05:43 +0100 | [diff] [blame] | 278 | printf("Remote unwind supported only on linux\n"); |
Andreas Gampe | 3faa581 | 2015-09-14 13:59:51 -0700 | [diff] [blame] | 279 | UNUSED(pid_int); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 280 | return JNI_FALSE; |
| 281 | #endif |
| 282 | } |
| 283 | |
| 284 | } // namespace art |