Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 17 | #include "dalvik_system_ZygoteHooks.h" |
| 18 | |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 19 | #include <stdlib.h> |
| 20 | |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 21 | #include <cutils/process_name.h> |
| 22 | |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 23 | #include "arch/instruction_set.h" |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 24 | #include "debugger.h" |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 25 | #include "java_vm_ext.h" |
Mathieu Chartier | 455f67c | 2015-03-17 13:48:29 -0700 | [diff] [blame] | 26 | #include "jit/jit.h" |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 27 | #include "jni_internal.h" |
| 28 | #include "JNIHelp.h" |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 29 | #include "scoped_thread_state_change.h" |
Andreas Gampe | 6be67ee | 2014-09-02 21:22:18 -0700 | [diff] [blame] | 30 | #include "ScopedUtfChars.h" |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 31 | #include "thread-inl.h" |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 32 | #include "trace.h" |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 33 | |
Elliott Hughes | 0a18df8 | 2015-01-09 15:16:16 -0800 | [diff] [blame] | 34 | #if defined(__linux__) |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 35 | #include <sys/prctl.h> |
| 36 | #endif |
| 37 | |
Narayan Kamath | ad4b0d2 | 2014-04-02 12:06:02 +0100 | [diff] [blame] | 38 | #include <sys/resource.h> |
| 39 | |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 40 | namespace art { |
| 41 | |
| 42 | static void EnableDebugger() { |
Elliott Hughes | 0a18df8 | 2015-01-09 15:16:16 -0800 | [diff] [blame] | 43 | #if defined(__linux__) |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 44 | // To let a non-privileged gdbserver attach to this |
| 45 | // process, we must set our dumpable flag. |
| 46 | if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) { |
| 47 | PLOG(ERROR) << "prctl(PR_SET_DUMPABLE) failed for pid " << getpid(); |
| 48 | } |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame] | 49 | #endif |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 50 | // We don't want core dumps, though, so set the core dump size to 0. |
| 51 | rlimit rl; |
| 52 | rl.rlim_cur = 0; |
| 53 | rl.rlim_max = RLIM_INFINITY; |
| 54 | if (setrlimit(RLIMIT_CORE, &rl) == -1) { |
| 55 | PLOG(ERROR) << "setrlimit(RLIMIT_CORE) failed for pid " << getpid(); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | static void EnableDebugFeatures(uint32_t debug_flags) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 60 | // Must match values in com.android.internal.os.Zygote. |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 61 | enum { |
| 62 | DEBUG_ENABLE_DEBUGGER = 1, |
| 63 | DEBUG_ENABLE_CHECKJNI = 1 << 1, |
| 64 | DEBUG_ENABLE_ASSERT = 1 << 2, |
| 65 | DEBUG_ENABLE_SAFEMODE = 1 << 3, |
| 66 | DEBUG_ENABLE_JNI_LOGGING = 1 << 4, |
Nicolas Geoffray | 0f042e0 | 2015-11-05 11:32:24 +0000 | [diff] [blame] | 67 | DEBUG_GENERATE_DEBUG_INFO = 1 << 5, |
Siva Chandra | 05d2415 | 2016-01-05 17:43:17 -0800 | [diff] [blame] | 68 | DEBUG_ALWAYS_JIT = 1 << 6, |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 69 | DEBUG_NATIVE_DEBUGGABLE = 1 << 7, |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 70 | }; |
| 71 | |
Mathieu Chartier | 6eff38d | 2015-03-17 09:52:22 -0700 | [diff] [blame] | 72 | Runtime* const runtime = Runtime::Current(); |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 73 | if ((debug_flags & DEBUG_ENABLE_CHECKJNI) != 0) { |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 74 | JavaVMExt* vm = runtime->GetJavaVM(); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 75 | if (!vm->IsCheckJniEnabled()) { |
Brian Carlstrom | 966ce11 | 2014-05-12 17:30:36 -0700 | [diff] [blame] | 76 | LOG(INFO) << "Late-enabling -Xcheck:jni"; |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 77 | vm->SetCheckJniEnabled(true); |
| 78 | // There's only one thread running at this point, so only one JNIEnv to fix up. |
| 79 | Thread::Current()->GetJniEnv()->SetCheckJniEnabled(true); |
| 80 | } else { |
Brian Carlstrom | 966ce11 | 2014-05-12 17:30:36 -0700 | [diff] [blame] | 81 | LOG(INFO) << "Not late-enabling -Xcheck:jni (already on)"; |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 82 | } |
| 83 | debug_flags &= ~DEBUG_ENABLE_CHECKJNI; |
| 84 | } |
| 85 | |
| 86 | if ((debug_flags & DEBUG_ENABLE_JNI_LOGGING) != 0) { |
| 87 | gLogVerbosity.third_party_jni = true; |
| 88 | debug_flags &= ~DEBUG_ENABLE_JNI_LOGGING; |
| 89 | } |
| 90 | |
| 91 | Dbg::SetJdwpAllowed((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0); |
| 92 | if ((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0) { |
| 93 | EnableDebugger(); |
| 94 | } |
| 95 | debug_flags &= ~DEBUG_ENABLE_DEBUGGER; |
| 96 | |
Mathieu Chartier | 6eff38d | 2015-03-17 09:52:22 -0700 | [diff] [blame] | 97 | const bool safe_mode = (debug_flags & DEBUG_ENABLE_SAFEMODE) != 0; |
| 98 | if (safe_mode) { |
Andreas Gampe | d2abbc9 | 2014-12-19 09:53:27 -0800 | [diff] [blame] | 99 | // Ensure that any (secondary) oat files will be interpreted. |
Andreas Gampe | d2abbc9 | 2014-12-19 09:53:27 -0800 | [diff] [blame] | 100 | runtime->AddCompilerOption("--compiler-filter=interpret-only"); |
Nicolas Geoffray | 0f042e0 | 2015-11-05 11:32:24 +0000 | [diff] [blame] | 101 | runtime->SetSafeMode(true); |
Andreas Gampe | d2abbc9 | 2014-12-19 09:53:27 -0800 | [diff] [blame] | 102 | debug_flags &= ~DEBUG_ENABLE_SAFEMODE; |
| 103 | } |
| 104 | |
David Srbecky | 8363c77 | 2015-05-28 16:12:43 +0100 | [diff] [blame] | 105 | const bool generate_debug_info = (debug_flags & DEBUG_GENERATE_DEBUG_INFO) != 0; |
| 106 | if (generate_debug_info) { |
| 107 | runtime->AddCompilerOption("--generate-debug-info"); |
| 108 | debug_flags &= ~DEBUG_GENERATE_DEBUG_INFO; |
Andreas Gampe | 00bb878 | 2015-04-24 16:33:43 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Andreas Gampe | d2abbc9 | 2014-12-19 09:53:27 -0800 | [diff] [blame] | 111 | // This is for backwards compatibility with Dalvik. |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 112 | debug_flags &= ~DEBUG_ENABLE_ASSERT; |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 113 | |
Siva Chandra | 05d2415 | 2016-01-05 17:43:17 -0800 | [diff] [blame] | 114 | if ((debug_flags & DEBUG_ALWAYS_JIT) != 0) { |
| 115 | jit::JitOptions* jit_options = runtime->GetJITOptions(); |
| 116 | CHECK(jit_options != nullptr); |
| 117 | jit_options->SetJitAtFirstUse(); |
| 118 | debug_flags &= ~DEBUG_ALWAYS_JIT; |
| 119 | } |
| 120 | |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 121 | if ((debug_flags & DEBUG_NATIVE_DEBUGGABLE) != 0) { |
| 122 | runtime->AddCompilerOption("--native-debuggable"); |
| 123 | debug_flags &= ~DEBUG_NATIVE_DEBUGGABLE; |
| 124 | } |
| 125 | |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 126 | if (debug_flags != 0) { |
| 127 | LOG(ERROR) << StringPrintf("Unknown bits set in debug_flags: %#x", debug_flags); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | static jlong ZygoteHooks_nativePreFork(JNIEnv* env, jclass) { |
| 132 | Runtime* runtime = Runtime::Current(); |
| 133 | CHECK(runtime->IsZygote()) << "runtime instance not started with -Xzygote"; |
Narayan Kamath | 3de95a7 | 2014-04-02 12:54:23 +0100 | [diff] [blame] | 134 | |
| 135 | runtime->PreZygoteFork(); |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 136 | |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 137 | if (Trace::GetMethodTracingMode() != TracingMode::kTracingInactive) { |
| 138 | // Tracing active, pause it. |
| 139 | Trace::Pause(); |
| 140 | } |
| 141 | |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 142 | // Grab thread before fork potentially makes Thread::pthread_key_self_ unusable. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 143 | return reinterpret_cast<jlong>(ThreadForEnv(env)); |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 144 | } |
| 145 | |
Nicolas Geoffray | d66c862 | 2015-12-11 14:59:16 +0000 | [diff] [blame] | 146 | static void ZygoteHooks_nativePostForkChild(JNIEnv* env, |
| 147 | jclass, |
| 148 | jlong token, |
| 149 | jint debug_flags, |
| 150 | jboolean is_system_server, |
Andreas Gampe | 6be67ee | 2014-09-02 21:22:18 -0700 | [diff] [blame] | 151 | jstring instruction_set) { |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 152 | Thread* thread = reinterpret_cast<Thread*>(token); |
| 153 | // Our system thread ID, etc, has changed so reset Thread state. |
| 154 | thread->InitAfterFork(); |
| 155 | EnableDebugFeatures(debug_flags); |
Andreas Gampe | 6be67ee | 2014-09-02 21:22:18 -0700 | [diff] [blame] | 156 | |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 157 | // Update tracing. |
| 158 | if (Trace::GetMethodTracingMode() != TracingMode::kTracingInactive) { |
| 159 | Trace::TraceOutputMode output_mode = Trace::GetOutputMode(); |
| 160 | Trace::TraceMode trace_mode = Trace::GetMode(); |
Andreas Gampe | e34a42c | 2015-04-25 14:44:29 -0700 | [diff] [blame] | 161 | size_t buffer_size = Trace::GetBufferSize(); |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 162 | |
| 163 | // Just drop it. |
| 164 | Trace::Abort(); |
| 165 | |
| 166 | // Only restart if it was streaming mode. |
| 167 | // TODO: Expose buffer size, so we can also do file mode. |
| 168 | if (output_mode == Trace::TraceOutputMode::kStreaming) { |
| 169 | const char* proc_name_cutils = get_process_name(); |
| 170 | std::string proc_name; |
| 171 | if (proc_name_cutils != nullptr) { |
| 172 | proc_name = proc_name_cutils; |
| 173 | } |
| 174 | if (proc_name_cutils == nullptr || proc_name == "zygote" || proc_name == "zygote64") { |
| 175 | // Either no process name, or the name hasn't been changed, yet. Just use pid. |
| 176 | pid_t pid = getpid(); |
| 177 | proc_name = StringPrintf("%u", static_cast<uint32_t>(pid)); |
| 178 | } |
| 179 | |
Calin Juravle | f83e733 | 2015-11-04 16:16:47 +0000 | [diff] [blame] | 180 | std::string trace_file = StringPrintf("/data/misc/trace/%s.trace.bin", proc_name.c_str()); |
| 181 | Trace::Start(trace_file.c_str(), |
| 182 | -1, |
| 183 | buffer_size, |
| 184 | 0, // TODO: Expose flags. |
| 185 | output_mode, |
| 186 | trace_mode, |
| 187 | 0); // TODO: Expose interval. |
| 188 | if (thread->IsExceptionPending()) { |
| 189 | ScopedObjectAccess soa(env); |
| 190 | thread->ClearException(); |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
Nicolas Geoffray | d66c862 | 2015-12-11 14:59:16 +0000 | [diff] [blame] | 195 | if (instruction_set != nullptr && !is_system_server) { |
Andreas Gampe | 6be67ee | 2014-09-02 21:22:18 -0700 | [diff] [blame] | 196 | ScopedUtfChars isa_string(env, instruction_set); |
| 197 | InstructionSet isa = GetInstructionSetFromString(isa_string.c_str()); |
jgu21 | a6da74e | 2014-09-10 06:57:17 -0400 | [diff] [blame] | 198 | Runtime::NativeBridgeAction action = Runtime::NativeBridgeAction::kUnload; |
Andreas Gampe | 6be67ee | 2014-09-02 21:22:18 -0700 | [diff] [blame] | 199 | if (isa != kNone && isa != kRuntimeISA) { |
| 200 | action = Runtime::NativeBridgeAction::kInitialize; |
| 201 | } |
Nicolas Geoffray | d66c862 | 2015-12-11 14:59:16 +0000 | [diff] [blame] | 202 | Runtime::Current()->InitNonZygoteOrPostFork( |
| 203 | env, is_system_server, action, isa_string.c_str()); |
jgu21 | a6da74e | 2014-09-10 06:57:17 -0400 | [diff] [blame] | 204 | } else { |
Nicolas Geoffray | d66c862 | 2015-12-11 14:59:16 +0000 | [diff] [blame] | 205 | Runtime::Current()->InitNonZygoteOrPostFork( |
| 206 | env, is_system_server, Runtime::NativeBridgeAction::kUnload, nullptr); |
Andreas Gampe | 6be67ee | 2014-09-02 21:22:18 -0700 | [diff] [blame] | 207 | } |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | static JNINativeMethod gMethods[] = { |
| 211 | NATIVE_METHOD(ZygoteHooks, nativePreFork, "()J"), |
Nicolas Geoffray | d66c862 | 2015-12-11 14:59:16 +0000 | [diff] [blame] | 212 | NATIVE_METHOD(ZygoteHooks, nativePostForkChild, "(JIZLjava/lang/String;)V"), |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 213 | }; |
| 214 | |
| 215 | void register_dalvik_system_ZygoteHooks(JNIEnv* env) { |
| 216 | REGISTER_NATIVE_METHODS("dalvik/system/ZygoteHooks"); |
| 217 | } |
| 218 | |
| 219 | } // namespace art |