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" |
| 32 | #include "utils.h" |
| 33 | |
| 34 | namespace art { |
| 35 | |
| 36 | // For testing debuggerd. We do not have expected-death tests, so can't test this by default. |
| 37 | // Code for this is copied from SignalTest. |
| 38 | static constexpr bool kCauseSegfault = false; |
| 39 | char* go_away_compiler_cfi = nullptr; |
| 40 | |
| 41 | static void CauseSegfault() { |
| 42 | #if defined(__arm__) || defined(__i386__) || defined(__x86_64__) || defined(__aarch64__) |
| 43 | // On supported architectures we cause a real SEGV. |
| 44 | *go_away_compiler_cfi = 'a'; |
| 45 | #else |
| 46 | // On other architectures we simulate SEGV. |
| 47 | kill(getpid(), SIGSEGV); |
| 48 | #endif |
| 49 | } |
| 50 | |
| 51 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_sleep(JNIEnv*, jobject, jint, jboolean, jdouble) { |
| 52 | // Keep pausing. |
| 53 | for (;;) { |
| 54 | pause(); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // Helper to look for a sequence in the stack trace. |
| 59 | #if __linux__ |
| 60 | static bool CheckStack(Backtrace* bt, const std::vector<std::string>& seq) { |
| 61 | size_t cur_search_index = 0; // The currently active index in seq. |
| 62 | CHECK_GT(seq.size(), 0U); |
| 63 | |
| 64 | for (Backtrace::const_iterator it = bt->begin(); it != bt->end(); ++it) { |
| 65 | if (BacktraceMap::IsValid(it->map)) { |
| 66 | LOG(INFO) << "Got " << it->func_name << ", looking for " << seq[cur_search_index]; |
| 67 | if (it->func_name == seq[cur_search_index]) { |
| 68 | cur_search_index++; |
| 69 | if (cur_search_index == seq.size()) { |
| 70 | return true; |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
David Srbecky | 020c543 | 2015-06-10 22:43:11 +0100 | [diff] [blame] | 76 | printf("Can not find %s in backtrace:\n", seq[cur_search_index].c_str()); |
| 77 | for (Backtrace::const_iterator it = bt->begin(); it != bt->end(); ++it) { |
| 78 | if (BacktraceMap::IsValid(it->map)) { |
| 79 | printf(" %s\n", it->func_name.c_str()); |
| 80 | } |
| 81 | } |
| 82 | |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 83 | return false; |
| 84 | } |
| 85 | #endif |
| 86 | |
| 87 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_unwindInProcess(JNIEnv*, jobject, jint, jboolean) { |
| 88 | #if __linux__ |
| 89 | // TODO: What to do on Valgrind? |
| 90 | |
| 91 | std::unique_ptr<Backtrace> bt(Backtrace::Create(BACKTRACE_CURRENT_PROCESS, GetTid())); |
| 92 | if (!bt->Unwind(0, nullptr)) { |
David Srbecky | 020c543 | 2015-06-10 22:43:11 +0100 | [diff] [blame] | 93 | printf("Can not unwind in process.\n"); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 94 | return JNI_FALSE; |
| 95 | } else if (bt->NumFrames() == 0) { |
David Srbecky | 020c543 | 2015-06-10 22:43:11 +0100 | [diff] [blame] | 96 | printf("No frames for unwind in process.\n"); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 97 | return JNI_FALSE; |
| 98 | } |
| 99 | |
| 100 | // We cannot really parse an exact stack, as the optimizing compiler may inline some functions. |
| 101 | // This is also risky, as deduping might play a trick on us, so the test needs to make sure that |
| 102 | // only unique functions are being expected. |
| 103 | std::vector<std::string> seq = { |
| 104 | "Java_Main_unwindInProcess", // This function. |
| 105 | "boolean Main.unwindInProcess(int, boolean)", // The corresponding Java native method frame. |
David Srbecky | 3da7608 | 2015-06-10 21:52:06 +0000 | [diff] [blame^] | 106 | "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] | 107 | "void Main.main(java.lang.String[])" // The Java entry method. |
| 108 | }; |
| 109 | |
| 110 | bool result = CheckStack(bt.get(), seq); |
| 111 | if (!kCauseSegfault) { |
| 112 | return result ? JNI_TRUE : JNI_FALSE; |
| 113 | } else { |
| 114 | LOG(INFO) << "Result of check-stack: " << result; |
| 115 | } |
| 116 | #endif |
| 117 | |
| 118 | if (kCauseSegfault) { |
| 119 | CauseSegfault(); |
| 120 | } |
| 121 | |
| 122 | return JNI_FALSE; |
| 123 | } |
| 124 | |
| 125 | #if __linux__ |
| 126 | static constexpr int kSleepTimeMicroseconds = 50000; // 0.05 seconds |
| 127 | static constexpr int kMaxTotalSleepTimeMicroseconds = 1000000; // 1 second |
| 128 | |
| 129 | // Wait for a sigstop. This code is copied from libbacktrace. |
| 130 | int wait_for_sigstop(pid_t tid, int* total_sleep_time_usec, bool* detach_failed ATTRIBUTE_UNUSED) { |
| 131 | for (;;) { |
| 132 | int status; |
| 133 | pid_t n = TEMP_FAILURE_RETRY(waitpid(tid, &status, __WALL | WNOHANG)); |
| 134 | if (n == -1) { |
| 135 | PLOG(WARNING) << "waitpid failed: tid " << tid; |
| 136 | break; |
| 137 | } else if (n == tid) { |
| 138 | if (WIFSTOPPED(status)) { |
| 139 | return WSTOPSIG(status); |
| 140 | } else { |
| 141 | PLOG(ERROR) << "unexpected waitpid response: n=" << n << ", status=" << std::hex << status; |
| 142 | break; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | if (*total_sleep_time_usec > kMaxTotalSleepTimeMicroseconds) { |
| 147 | PLOG(WARNING) << "timed out waiting for stop signal: tid=" << tid; |
| 148 | break; |
| 149 | } |
| 150 | |
| 151 | usleep(kSleepTimeMicroseconds); |
| 152 | *total_sleep_time_usec += kSleepTimeMicroseconds; |
| 153 | } |
| 154 | |
| 155 | return -1; |
| 156 | } |
| 157 | #endif |
| 158 | |
| 159 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_unwindOtherProcess(JNIEnv*, jobject, jint pid_int) { |
| 160 | #if __linux__ |
| 161 | // TODO: What to do on Valgrind? |
| 162 | pid_t pid = static_cast<pid_t>(pid_int); |
| 163 | |
| 164 | // OK, this is painful. debuggerd uses ptrace to unwind other processes. |
| 165 | |
| 166 | if (ptrace(PTRACE_ATTACH, pid, 0, 0)) { |
| 167 | // Were not able to attach, bad. |
David Srbecky | 020c543 | 2015-06-10 22:43:11 +0100 | [diff] [blame] | 168 | printf("Failed to attach to other process.\n"); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 169 | PLOG(ERROR) << "Failed to attach."; |
| 170 | kill(pid, SIGCONT); |
| 171 | return JNI_FALSE; |
| 172 | } |
| 173 | |
| 174 | kill(pid, SIGSTOP); |
| 175 | |
| 176 | bool detach_failed = false; |
| 177 | int total_sleep_time_usec = 0; |
| 178 | int signal = wait_for_sigstop(pid, &total_sleep_time_usec, &detach_failed); |
| 179 | if (signal == -1) { |
| 180 | LOG(WARNING) << "wait_for_sigstop failed."; |
| 181 | } |
| 182 | |
| 183 | std::unique_ptr<Backtrace> bt(Backtrace::Create(pid, BACKTRACE_CURRENT_THREAD)); |
| 184 | bool result = true; |
| 185 | if (!bt->Unwind(0, nullptr)) { |
David Srbecky | 020c543 | 2015-06-10 22:43:11 +0100 | [diff] [blame] | 186 | printf("Can not unwind other process.\n"); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 187 | result = false; |
| 188 | } else if (bt->NumFrames() == 0) { |
David Srbecky | 020c543 | 2015-06-10 22:43:11 +0100 | [diff] [blame] | 189 | printf("No frames for unwind of other process.\n"); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 190 | result = false; |
| 191 | } |
| 192 | |
| 193 | if (result) { |
| 194 | // See comment in unwindInProcess for non-exact stack matching. |
| 195 | std::vector<std::string> seq = { |
| 196 | // "Java_Main_sleep", // The sleep function being executed in the |
| 197 | // other runtime. |
| 198 | // Note: For some reason, the name isn't |
| 199 | // resolved, so don't look for it right now. |
| 200 | "boolean Main.sleep(int, boolean, double)", // The corresponding Java native method frame. |
David Srbecky | 3da7608 | 2015-06-10 21:52:06 +0000 | [diff] [blame^] | 201 | "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] | 202 | "void Main.main(java.lang.String[])" // The Java entry method. |
| 203 | }; |
| 204 | |
| 205 | result = CheckStack(bt.get(), seq); |
| 206 | } |
| 207 | |
| 208 | if (ptrace(PTRACE_DETACH, pid, 0, 0) != 0) { |
| 209 | PLOG(ERROR) << "Detach failed"; |
| 210 | } |
| 211 | |
| 212 | // Continue the process so we can kill it on the Java side. |
| 213 | kill(pid, SIGCONT); |
| 214 | |
| 215 | return result ? JNI_TRUE : JNI_FALSE; |
| 216 | #else |
| 217 | return JNI_FALSE; |
| 218 | #endif |
| 219 | } |
| 220 | |
| 221 | } // namespace art |