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> |
| 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" |
Andreas Gampe | 78da0d7 | 2016-09-05 12:30:49 -0700 | [diff] [blame] | 32 | #include "base/stringprintf.h" |
Andreas Gampe | 88da3b0 | 2015-06-12 20:38:49 -0700 | [diff] [blame] | 33 | #include "gc/heap.h" |
| 34 | #include "gc/space/image_space.h" |
| 35 | #include "oat_file.h" |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 36 | #include "utils.h" |
| 37 | |
| 38 | namespace art { |
| 39 | |
| 40 | // For testing debuggerd. We do not have expected-death tests, so can't test this by default. |
| 41 | // Code for this is copied from SignalTest. |
| 42 | static constexpr bool kCauseSegfault = false; |
| 43 | char* go_away_compiler_cfi = nullptr; |
| 44 | |
| 45 | static void CauseSegfault() { |
| 46 | #if defined(__arm__) || defined(__i386__) || defined(__x86_64__) || defined(__aarch64__) |
| 47 | // On supported architectures we cause a real SEGV. |
| 48 | *go_away_compiler_cfi = 'a'; |
| 49 | #else |
| 50 | // On other architectures we simulate SEGV. |
| 51 | kill(getpid(), SIGSEGV); |
| 52 | #endif |
| 53 | } |
| 54 | |
| 55 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_sleep(JNIEnv*, jobject, jint, jboolean, jdouble) { |
| 56 | // Keep pausing. |
David Srbecky | 03bf174 | 2016-02-15 16:54:34 +0000 | [diff] [blame] | 57 | printf("Going to sleep\n"); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 58 | for (;;) { |
Andreas Gampe | 78da0d7 | 2016-09-05 12:30:49 -0700 | [diff] [blame] | 59 | sleep(1); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
| 63 | // Helper to look for a sequence in the stack trace. |
| 64 | #if __linux__ |
| 65 | static bool CheckStack(Backtrace* bt, const std::vector<std::string>& seq) { |
| 66 | size_t cur_search_index = 0; // The currently active index in seq. |
| 67 | CHECK_GT(seq.size(), 0U); |
| 68 | |
| 69 | for (Backtrace::const_iterator it = bt->begin(); it != bt->end(); ++it) { |
| 70 | if (BacktraceMap::IsValid(it->map)) { |
| 71 | LOG(INFO) << "Got " << it->func_name << ", looking for " << seq[cur_search_index]; |
| 72 | if (it->func_name == seq[cur_search_index]) { |
| 73 | cur_search_index++; |
| 74 | if (cur_search_index == seq.size()) { |
| 75 | return true; |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
Roland Levillain | 91d65e0 | 2016-01-19 15:59:16 +0000 | [diff] [blame] | 81 | 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] | 82 | for (Backtrace::const_iterator it = bt->begin(); it != bt->end(); ++it) { |
| 83 | if (BacktraceMap::IsValid(it->map)) { |
| 84 | printf(" %s\n", it->func_name.c_str()); |
| 85 | } |
| 86 | } |
| 87 | |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 88 | return false; |
| 89 | } |
Andreas Gampe | 78da0d7 | 2016-09-05 12:30:49 -0700 | [diff] [blame] | 90 | |
| 91 | static void MoreErrorInfo(pid_t pid, bool sig_quit_on_fail) { |
| 92 | printf("Secondary pid is %d\n", pid); |
| 93 | |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 94 | PrintFileToLog(StringPrintf("/proc/%d/maps", pid), ::android::base::ERROR); |
Andreas Gampe | 78da0d7 | 2016-09-05 12:30:49 -0700 | [diff] [blame] | 95 | |
| 96 | if (sig_quit_on_fail) { |
| 97 | int res = kill(pid, SIGQUIT); |
| 98 | if (res != 0) { |
| 99 | PLOG(ERROR) << "Failed to send signal"; |
| 100 | } |
| 101 | } |
| 102 | } |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 103 | #endif |
| 104 | |
Andreas Gampe | 88da3b0 | 2015-06-12 20:38:49 -0700 | [diff] [blame] | 105 | // Currently we have to fall back to our own loader for the boot image when it's compiled PIC |
| 106 | // because its base is zero. Thus in-process unwinding through it won't work. This is a helper |
| 107 | // detecting this. |
| 108 | #if __linux__ |
| 109 | static bool IsPicImage() { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 110 | std::vector<gc::space::ImageSpace*> image_spaces = |
| 111 | Runtime::Current()->GetHeap()->GetBootImageSpaces(); |
| 112 | CHECK(!image_spaces.empty()); // We should be running with an image. |
| 113 | const OatFile* oat_file = image_spaces[0]->GetOatFile(); |
Andreas Gampe | 88da3b0 | 2015-06-12 20:38:49 -0700 | [diff] [blame] | 114 | CHECK(oat_file != nullptr); // We should have an oat file to go with the image. |
| 115 | return oat_file->IsPic(); |
| 116 | } |
| 117 | #endif |
| 118 | |
David Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 119 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_unwindInProcess( |
| 120 | JNIEnv*, |
| 121 | jobject, |
| 122 | jboolean full_signatrues, |
| 123 | jint, |
| 124 | jboolean) { |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 125 | #if __linux__ |
Andreas Gampe | 88da3b0 | 2015-06-12 20:38:49 -0700 | [diff] [blame] | 126 | if (IsPicImage()) { |
| 127 | LOG(INFO) << "Image is pic, in-process unwinding check bypassed."; |
| 128 | return JNI_TRUE; |
| 129 | } |
| 130 | |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 131 | // TODO: What to do on Valgrind? |
| 132 | |
| 133 | std::unique_ptr<Backtrace> bt(Backtrace::Create(BACKTRACE_CURRENT_PROCESS, GetTid())); |
| 134 | if (!bt->Unwind(0, nullptr)) { |
Roland Levillain | 91d65e0 | 2016-01-19 15:59:16 +0000 | [diff] [blame] | 135 | printf("Cannot unwind in process.\n"); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 136 | return JNI_FALSE; |
| 137 | } else if (bt->NumFrames() == 0) { |
David Srbecky | 020c543 | 2015-06-10 22:43:11 +0100 | [diff] [blame] | 138 | printf("No frames for unwind in process.\n"); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 139 | return JNI_FALSE; |
| 140 | } |
| 141 | |
| 142 | // We cannot really parse an exact stack, as the optimizing compiler may inline some functions. |
| 143 | // This is also risky, as deduping might play a trick on us, so the test needs to make sure that |
| 144 | // only unique functions are being expected. |
David Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 145 | // "mini-debug-info" does not include parameters to save space. |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 146 | std::vector<std::string> seq = { |
| 147 | "Java_Main_unwindInProcess", // This function. |
David Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 148 | "Main.unwindInProcess", // The corresponding Java native method frame. |
| 149 | "int java.util.Arrays.binarySearch(java.lang.Object[], int, int, java.lang.Object, java.util.Comparator)", // Framework method. |
| 150 | "Main.main" // The Java entry method. |
| 151 | }; |
| 152 | std::vector<std::string> full_seq = { |
| 153 | "Java_Main_unwindInProcess", // This function. |
| 154 | "boolean Main.unwindInProcess(boolean, int, boolean)", // The corresponding Java native method frame. |
David Srbecky | 3da7608 | 2015-06-10 21:52:06 +0000 | [diff] [blame] | 155 | "int java.util.Arrays.binarySearch(java.lang.Object[], int, int, java.lang.Object, java.util.Comparator)", // Framework method. |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 156 | "void Main.main(java.lang.String[])" // The Java entry method. |
| 157 | }; |
| 158 | |
David Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 159 | bool result = CheckStack(bt.get(), full_signatrues ? full_seq : seq); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 160 | if (!kCauseSegfault) { |
| 161 | return result ? JNI_TRUE : JNI_FALSE; |
| 162 | } else { |
| 163 | LOG(INFO) << "Result of check-stack: " << result; |
| 164 | } |
| 165 | #endif |
| 166 | |
| 167 | if (kCauseSegfault) { |
| 168 | CauseSegfault(); |
| 169 | } |
| 170 | |
| 171 | return JNI_FALSE; |
| 172 | } |
| 173 | |
| 174 | #if __linux__ |
| 175 | static constexpr int kSleepTimeMicroseconds = 50000; // 0.05 seconds |
| 176 | static constexpr int kMaxTotalSleepTimeMicroseconds = 1000000; // 1 second |
| 177 | |
| 178 | // Wait for a sigstop. This code is copied from libbacktrace. |
| 179 | int wait_for_sigstop(pid_t tid, int* total_sleep_time_usec, bool* detach_failed ATTRIBUTE_UNUSED) { |
| 180 | for (;;) { |
| 181 | int status; |
| 182 | pid_t n = TEMP_FAILURE_RETRY(waitpid(tid, &status, __WALL | WNOHANG)); |
| 183 | if (n == -1) { |
| 184 | PLOG(WARNING) << "waitpid failed: tid " << tid; |
| 185 | break; |
| 186 | } else if (n == tid) { |
| 187 | if (WIFSTOPPED(status)) { |
| 188 | return WSTOPSIG(status); |
| 189 | } else { |
| 190 | PLOG(ERROR) << "unexpected waitpid response: n=" << n << ", status=" << std::hex << status; |
| 191 | break; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | if (*total_sleep_time_usec > kMaxTotalSleepTimeMicroseconds) { |
| 196 | PLOG(WARNING) << "timed out waiting for stop signal: tid=" << tid; |
| 197 | break; |
| 198 | } |
| 199 | |
| 200 | usleep(kSleepTimeMicroseconds); |
| 201 | *total_sleep_time_usec += kSleepTimeMicroseconds; |
| 202 | } |
| 203 | |
| 204 | return -1; |
| 205 | } |
| 206 | #endif |
| 207 | |
David Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 208 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_unwindOtherProcess( |
| 209 | JNIEnv*, |
| 210 | jobject, |
| 211 | jboolean full_signatrues, |
| 212 | jint pid_int) { |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 213 | #if __linux__ |
| 214 | // TODO: What to do on Valgrind? |
| 215 | pid_t pid = static_cast<pid_t>(pid_int); |
| 216 | |
| 217 | // OK, this is painful. debuggerd uses ptrace to unwind other processes. |
| 218 | |
| 219 | if (ptrace(PTRACE_ATTACH, pid, 0, 0)) { |
| 220 | // Were not able to attach, bad. |
David Srbecky | 020c543 | 2015-06-10 22:43:11 +0100 | [diff] [blame] | 221 | printf("Failed to attach to other process.\n"); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 222 | PLOG(ERROR) << "Failed to attach."; |
David Srbecky | a70e5b9 | 2015-06-17 03:52:54 +0100 | [diff] [blame] | 223 | kill(pid, SIGKILL); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 224 | return JNI_FALSE; |
| 225 | } |
| 226 | |
| 227 | kill(pid, SIGSTOP); |
| 228 | |
| 229 | bool detach_failed = false; |
| 230 | int total_sleep_time_usec = 0; |
| 231 | int signal = wait_for_sigstop(pid, &total_sleep_time_usec, &detach_failed); |
| 232 | if (signal == -1) { |
| 233 | LOG(WARNING) << "wait_for_sigstop failed."; |
| 234 | } |
| 235 | |
| 236 | std::unique_ptr<Backtrace> bt(Backtrace::Create(pid, BACKTRACE_CURRENT_THREAD)); |
| 237 | bool result = true; |
| 238 | if (!bt->Unwind(0, nullptr)) { |
Roland Levillain | 91d65e0 | 2016-01-19 15:59:16 +0000 | [diff] [blame] | 239 | printf("Cannot unwind other process.\n"); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 240 | result = false; |
| 241 | } else if (bt->NumFrames() == 0) { |
David Srbecky | 020c543 | 2015-06-10 22:43:11 +0100 | [diff] [blame] | 242 | printf("No frames for unwind of other process.\n"); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 243 | result = false; |
| 244 | } |
| 245 | |
| 246 | if (result) { |
| 247 | // See comment in unwindInProcess for non-exact stack matching. |
David Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 248 | // "mini-debug-info" does not include parameters to save space. |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 249 | std::vector<std::string> seq = { |
| 250 | // "Java_Main_sleep", // The sleep function being executed in the |
| 251 | // other runtime. |
| 252 | // Note: For some reason, the name isn't |
| 253 | // resolved, so don't look for it right now. |
David Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 254 | "Main.sleep", // The corresponding Java native method frame. |
| 255 | "int java.util.Arrays.binarySearch(java.lang.Object[], int, int, java.lang.Object, java.util.Comparator)", // Framework method. |
| 256 | "Main.main" // The Java entry method. |
| 257 | }; |
| 258 | std::vector<std::string> full_seq = { |
| 259 | // "Java_Main_sleep", // The sleep function being executed in the |
| 260 | // other runtime. |
| 261 | // Note: For some reason, the name isn't |
| 262 | // resolved, so don't look for it right now. |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 263 | "boolean Main.sleep(int, boolean, double)", // The corresponding Java native method frame. |
David Srbecky | 3da7608 | 2015-06-10 21:52:06 +0000 | [diff] [blame] | 264 | "int java.util.Arrays.binarySearch(java.lang.Object[], int, int, java.lang.Object, java.util.Comparator)", // Framework method. |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 265 | "void Main.main(java.lang.String[])" // The Java entry method. |
| 266 | }; |
| 267 | |
David Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 268 | result = CheckStack(bt.get(), full_signatrues ? full_seq : seq); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Andreas Gampe | 78da0d7 | 2016-09-05 12:30:49 -0700 | [diff] [blame] | 271 | constexpr bool kSigQuitOnFail = true; |
| 272 | if (!result) { |
| 273 | MoreErrorInfo(pid, kSigQuitOnFail); |
| 274 | } |
| 275 | |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 276 | if (ptrace(PTRACE_DETACH, pid, 0, 0) != 0) { |
| 277 | PLOG(ERROR) << "Detach failed"; |
| 278 | } |
| 279 | |
Andreas Gampe | 78da0d7 | 2016-09-05 12:30:49 -0700 | [diff] [blame] | 280 | // If we failed to unwind and induced an ANR dump, give the child some time (20s). |
| 281 | if (!result && kSigQuitOnFail) { |
| 282 | sleep(20); |
| 283 | } |
| 284 | |
David Srbecky | a70e5b9 | 2015-06-17 03:52:54 +0100 | [diff] [blame] | 285 | // Kill the other process once we are done with it. |
| 286 | kill(pid, SIGKILL); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 287 | |
| 288 | return result ? JNI_TRUE : JNI_FALSE; |
| 289 | #else |
Andreas Gampe | 3faa581 | 2015-09-14 13:59:51 -0700 | [diff] [blame] | 290 | UNUSED(pid_int); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 291 | return JNI_FALSE; |
| 292 | #endif |
| 293 | } |
| 294 | |
| 295 | } // namespace art |