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 | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 28 | #include <android-base/logging.h> |
| 29 | #include <android-base/stringprintf.h> |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 30 | #include <backtrace/Backtrace.h> |
| 31 | |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 32 | #include "base/file_utils.h" |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 33 | #include "base/macros.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 34 | #include "base/utils.h" |
Andreas Gampe | 88da3b0 | 2015-06-12 20:38:49 -0700 | [diff] [blame] | 35 | #include "gc/heap.h" |
| 36 | #include "gc/space/image_space.h" |
| 37 | #include "oat_file.h" |
Andreas Gampe | d482e73 | 2017-04-24 17:59:09 -0700 | [diff] [blame] | 38 | #include "runtime.h" |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 39 | |
| 40 | namespace 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. |
| 44 | static constexpr bool kCauseSegfault = false; |
| 45 | char* go_away_compiler_cfi = nullptr; |
| 46 | |
| 47 | static 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 | |
| 57 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_sleep(JNIEnv*, jobject, jint, jboolean, jdouble) { |
| 58 | // Keep pausing. |
David Srbecky | 03bf174 | 2016-02-15 16:54:34 +0000 | [diff] [blame] | 59 | printf("Going to sleep\n"); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 60 | for (;;) { |
Andreas Gampe | 78da0d7 | 2016-09-05 12:30:49 -0700 | [diff] [blame] | 61 | sleep(1); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
| 65 | // Helper to look for a sequence in the stack trace. |
| 66 | #if __linux__ |
| 67 | static 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 Srbecky | 08a9af0 | 2018-01-18 13:05:01 +0000 | [diff] [blame] | 74 | if (it->func_name.find(seq[cur_search_index]) != std::string::npos) { |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 75 | cur_search_index++; |
| 76 | if (cur_search_index == seq.size()) { |
| 77 | return true; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
Roland Levillain | 91d65e0 | 2016-01-19 15:59:16 +0000 | [diff] [blame] | 83 | 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] | 84 | for (Backtrace::const_iterator it = bt->begin(); it != bt->end(); ++it) { |
| 85 | if (BacktraceMap::IsValid(it->map)) { |
David Srbecky | f38572d | 2018-02-05 15:45:14 +0000 | [diff] [blame] | 86 | printf(" %s\n", Backtrace::FormatFrameData(&*it).c_str()); |
David Srbecky | 020c543 | 2015-06-10 22:43:11 +0100 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 90 | return false; |
| 91 | } |
Andreas Gampe | 78da0d7 | 2016-09-05 12:30:49 -0700 | [diff] [blame] | 92 | |
| 93 | static void MoreErrorInfo(pid_t pid, bool sig_quit_on_fail) { |
| 94 | printf("Secondary pid is %d\n", pid); |
| 95 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 96 | PrintFileToLog(android::base::StringPrintf("/proc/%d/maps", pid), ::android::base::ERROR); |
Andreas Gampe | 78da0d7 | 2016-09-05 12:30:49 -0700 | [diff] [blame] | 97 | |
| 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 Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 105 | #endif |
| 106 | |
David Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 107 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_unwindInProcess( |
| 108 | JNIEnv*, |
| 109 | jobject, |
David Srbecky | 08a9af0 | 2018-01-18 13:05:01 +0000 | [diff] [blame] | 110 | jboolean, |
David Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 111 | jint, |
| 112 | jboolean) { |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 113 | #if __linux__ |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 114 | std::unique_ptr<Backtrace> bt(Backtrace::Create(BACKTRACE_CURRENT_PROCESS, GetTid())); |
| 115 | if (!bt->Unwind(0, nullptr)) { |
Roland Levillain | 91d65e0 | 2016-01-19 15:59:16 +0000 | [diff] [blame] | 116 | printf("Cannot unwind in process.\n"); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 117 | return JNI_FALSE; |
| 118 | } else if (bt->NumFrames() == 0) { |
David Srbecky | 020c543 | 2015-06-10 22:43:11 +0100 | [diff] [blame] | 119 | printf("No frames for unwind in process.\n"); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 120 | 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 Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 126 | // "mini-debug-info" does not include parameters to save space. |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 127 | std::vector<std::string> seq = { |
| 128 | "Java_Main_unwindInProcess", // This function. |
David Srbecky | 08a9af0 | 2018-01-18 13:05:01 +0000 | [diff] [blame] | 129 | "java.util.Arrays.binarySearch0", // Framework method. |
David Srbecky | 872be3d | 2018-02-05 17:30:01 +0000 | [diff] [blame] | 130 | "Base.runBase", // Method in other dex file. |
David Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 131 | "Main.main" // The Java entry method. |
| 132 | }; |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 133 | |
David Srbecky | 08a9af0 | 2018-01-18 13:05:01 +0000 | [diff] [blame] | 134 | bool result = CheckStack(bt.get(), seq); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 135 | 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__ |
| 150 | static constexpr int kSleepTimeMicroseconds = 50000; // 0.05 seconds |
| 151 | static constexpr int kMaxTotalSleepTimeMicroseconds = 1000000; // 1 second |
| 152 | |
| 153 | // Wait for a sigstop. This code is copied from libbacktrace. |
| 154 | int 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 Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 183 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_unwindOtherProcess( |
| 184 | JNIEnv*, |
| 185 | jobject, |
David Srbecky | 08a9af0 | 2018-01-18 13:05:01 +0000 | [diff] [blame] | 186 | jboolean, |
David Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 187 | jint pid_int) { |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 188 | #if __linux__ |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 189 | 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 Srbecky | 020c543 | 2015-06-10 22:43:11 +0100 | [diff] [blame] | 195 | printf("Failed to attach to other process.\n"); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 196 | PLOG(ERROR) << "Failed to attach."; |
David Srbecky | a70e5b9 | 2015-06-17 03:52:54 +0100 | [diff] [blame] | 197 | kill(pid, SIGKILL); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 198 | 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 Levillain | 91d65e0 | 2016-01-19 15:59:16 +0000 | [diff] [blame] | 213 | printf("Cannot unwind other process.\n"); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 214 | result = false; |
| 215 | } else if (bt->NumFrames() == 0) { |
David Srbecky | 020c543 | 2015-06-10 22:43:11 +0100 | [diff] [blame] | 216 | printf("No frames for unwind of other process.\n"); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 217 | result = false; |
| 218 | } |
| 219 | |
| 220 | if (result) { |
| 221 | // See comment in unwindInProcess for non-exact stack matching. |
David Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 222 | // "mini-debug-info" does not include parameters to save space. |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 223 | std::vector<std::string> seq = { |
David Srbecky | 946bb09 | 2018-03-09 17:23:01 +0000 | [diff] [blame] | 224 | "Java_Main_sleep", // The sleep function in the other process. |
David Srbecky | 08a9af0 | 2018-01-18 13:05:01 +0000 | [diff] [blame] | 225 | "java.util.Arrays.binarySearch0", // Framework method. |
David Srbecky | 872be3d | 2018-02-05 17:30:01 +0000 | [diff] [blame] | 226 | "Base.runBase", // Method in other dex file. |
David Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 227 | "Main.main" // The Java entry method. |
| 228 | }; |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 229 | |
David Srbecky | 08a9af0 | 2018-01-18 13:05:01 +0000 | [diff] [blame] | 230 | result = CheckStack(bt.get(), seq); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Andreas Gampe | 78da0d7 | 2016-09-05 12:30:49 -0700 | [diff] [blame] | 233 | constexpr bool kSigQuitOnFail = true; |
| 234 | if (!result) { |
| 235 | MoreErrorInfo(pid, kSigQuitOnFail); |
| 236 | } |
| 237 | |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 238 | if (ptrace(PTRACE_DETACH, pid, 0, 0) != 0) { |
| 239 | PLOG(ERROR) << "Detach failed"; |
| 240 | } |
| 241 | |
Andreas Gampe | 78da0d7 | 2016-09-05 12:30:49 -0700 | [diff] [blame] | 242 | // 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 Srbecky | a70e5b9 | 2015-06-17 03:52:54 +0100 | [diff] [blame] | 247 | // Kill the other process once we are done with it. |
| 248 | kill(pid, SIGKILL); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 249 | |
| 250 | return result ? JNI_TRUE : JNI_FALSE; |
| 251 | #else |
Andreas Gampe | 3faa581 | 2015-09-14 13:59:51 -0700 | [diff] [blame] | 252 | UNUSED(pid_int); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 253 | return JNI_FALSE; |
| 254 | #endif |
| 255 | } |
| 256 | |
| 257 | } // namespace art |