// For testing debuggerd. We do not have expected-death tests, so can't test this by default. // Code for this is copied from SignalTest. static constexpr bool kCauseSegfault = false; char* go_away_compiler_cfi = nullptr;
staticvoid CauseSegfault() { #ifdefined(__arm__) || defined(__i386__) || defined(__x86_64__) || defined(__aarch64__) // On supported architectures we cause a real SEGV.
*go_away_compiler_cfi = 'a'; #else // On other architectures we simulate SEGV.
kill(getpid(), SIGSEGV); #endif
}
extern"C" JNIEXPORT jint JNICALL Java_Main_startSecondaryProcess(JNIEnv*, jclass) {
printf("Java_Main_startSecondaryProcess\n"); #if __linux__ // Get our command line so that we can use it to start identical process.
std::string cmdline; // null-separated and null-terminated arguments. if (!android::base::ReadFileToString("/proc/self/cmdline", &cmdline)) {
LOG(FATAL) << "Failed to read /proc/self/cmdline.";
} if (cmdline.empty()) {
LOG(FATAL) << "No data was read from /proc/self/cmdline.";
} // Workaround for b/150189787. if (cmdline.back() != '\0') {
cmdline += '\0';
}
cmdline = cmdline + "--secondary" + '\0'; // Let the child know it is a helper.
// Split the string into individual arguments suitable for execv.
std::vector<char*> argv; for (size_t i = 0; i < cmdline.size(); i += strlen(&cmdline[i]) + 1) {
argv.push_back(&cmdline[i]);
}
argv.push_back(nullptr); // Terminate the list.
extern"C" JNIEXPORT jboolean JNICALL Java_Main_sigstop(JNIEnv*, jclass) {
printf("Java_Main_sigstop\n"); #if __linux__
MutexLock mu(Thread::Current(), *GetNativeDebugInfoLock()); // Avoid races with the JIT thread.
raise(SIGSTOP); #endif returntrue; // Prevent the compiler from tail-call optimizing this method away.
}
// Helper to look for a sequence in the stack trace. #if __linux__ staticbool CheckStack(unwindstack::AndroidUnwinder& unwinder,
unwindstack::AndroidUnwinderData& data, const std::vector<std::string>& seq) {
size_t cur_search_index = 0; // The currently active index in seq.
CHECK_GT(seq.size(), 0U);
bool ok = true; for (const unwindstack::FrameData& frame : data.frames) { if (frame.map_info == nullptr) {
printf("Error: No map_info for frame #%02zu\n", frame.num);
ok = false; continue;
} const std::string& function_name = frame.function_name; if (cur_search_index < seq.size()) {
LOG(INFO) << "Got " << function_name << ", looking for " << seq[cur_search_index]; if (function_name.find(seq[cur_search_index]) != std::string::npos) {
cur_search_index++;
}
} if (function_name == "main") { break;
} #if !defined(__ANDROID__) && defined(__BIONIC__ ) // host-bionic // TODO(b/182810709): Unwinding is broken on host-bionic so we expect some empty frames. #else const std::string& lib_name = frame.map_info->name(); if (!kIsTargetBuild && lib_name.find("libc.so") != std::string::npos) { // TODO(b/254626913): Unwinding can fail for libc on host.
} elseif (function_name.empty()) {
printf("Error: No function name for frame #%02zu\n", frame.num);
ok = false;
} #endif
}
if (cur_search_index < seq.size()) {
printf("Error: Cannot find %s\n", seq[cur_search_index].c_str());
ok = false;
}
if (!ok) {
printf("Backtrace:\n"); for (const unwindstack::FrameData& frame : data.frames) {
printf(" %s\n", unwinder.FormatFrame(frame).c_str());
}
}
if (sig_quit_on_fail) { int res = kill(pid, SIGQUIT); if (res != 0) {
PLOG(ERROR) << "Failed to send signal";
}
}
} #endif
extern"C" JNIEXPORT jboolean JNICALL Java_Main_unwindInProcess(JNIEnv*, jclass) {
printf("Java_Main_unwindInProcess\n"); #if __linux__
MutexLock mu(Thread::Current(), *GetNativeDebugInfoLock()); // Avoid races with the JIT thread.
unwindstack::AndroidLocalUnwinder unwinder;
unwindstack::AndroidUnwinderData data; if (!unwinder.Unwind(data)) {
printf("Cannot unwind in process.\n"); return JNI_FALSE;
}
// We cannot really parse an exact stack, as the optimizing compiler may inline some functions. // This is also risky, as deduping might play a trick on us, so the test needs to make sure that // only unique functions are being expected. // "mini-debug-info" does not include parameters to save space.
std::vector<std::string> seq = { "Java_Main_unwindInProcess", // This function. "java.util.Arrays.binarySearch0", // Framework method. "Base.$noinline$runTest", // Method in other dex file. "Main.main"// The Java entry method.
};
bool result = CheckStack(unwinder, data, seq); if (!kCauseSegfault) { return result ? JNI_TRUE : JNI_FALSE;
} else {
LOG(INFO) << "Result of check-stack: " << result;
} #endif
if (kCauseSegfault) {
CauseSegfault();
}
return JNI_FALSE;
}
#if __linux__ static constexpr int kSleepTimeMicroseconds = 50000; // 0.05 seconds static constexpr int kMaxTotalSleepTimeMicroseconds = 10000000; // 10 seconds
// Wait for a sigstop. This code is copied from libbacktrace. int wait_for_sigstop(pid_t tid, int* total_sleep_time_usec, [[maybe_unused]] bool* detach_failed) { for (;;) { int status;
pid_t n = TEMP_FAILURE_RETRY(waitpid(tid, &status, __WALL | WNOHANG | WUNTRACED)); if (n == -1) {
PLOG(WARNING) << "waitpid failed: tid " << tid; break;
} elseif (n == tid) { if (WIFSTOPPED(status)) { return WSTOPSIG(status);
} else {
PLOG(ERROR) << "unexpected waitpid response: n=" << n << ", status=" << std::hex << status; break;
}
}
if (*total_sleep_time_usec > kMaxTotalSleepTimeMicroseconds) {
PLOG(WARNING) << "timed out waiting for stop signal: tid=" << tid; break;
}
// We wait for the SIGSTOP while the child process is untraced (using // `WUNTRACED` in `wait_for_sigstop()`) to avoid a SIGSEGV for implicit // suspend check stopping the process because it's being traced. bool detach_failed = false; int total_sleep_time_usec = 0; int signal = wait_for_sigstop(pid, &total_sleep_time_usec, &detach_failed); if (signal != SIGSTOP) {
printf("wait_for_sigstop failed.\n"); return JNI_FALSE;
}
// SEIZE is like ATTACH, but it does not stop the process (it has already stopped itself). if (ptrace(PTRACE_SEIZE, pid, 0, 0)) { // Were not able to attach, bad.
printf("Failed to attach to other process.\n");
PLOG(ERROR) << "Failed to attach.";
kill(pid, SIGKILL); return JNI_FALSE;
}
bool result = true;
unwindstack::AndroidRemoteUnwinder unwinder(pid);
unwindstack::AndroidUnwinderData data; if (!unwinder.Unwind(data)) {
printf("Cannot unwind other process.\n");
result = false;
} else { // See comment in unwindInProcess for non-exact stack matching. // "mini-debug-info" does not include parameters to save space.
std::vector<std::string> seq = { "Java_Main_sigstop", // The stop function in the other process. "java.util.Arrays.binarySearch0", // Framework method. "Base.$noinline$runTest", // Method in other dex file. "Main.main"// The Java entry method.
};
result = CheckStack(unwinder, data, seq);
}
constexpr bool kSigQuitOnFail = true; if (!result) {
printf("Failed to unwind secondary with pid %d\n", pid);
MoreErrorInfo(pid, kSigQuitOnFail);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.