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 17#include "dalvik_system_ZygoteHooks.h" 18 19#include <stdlib.h> 20 21#include <cutils/process_name.h> 22 23#include "arch/instruction_set.h" 24#include "debugger.h" 25#include "java_vm_ext.h" 26#include "jit/jit.h" 27#include "jni_internal.h" 28#include "JNIHelp.h" 29#include "scoped_thread_state_change.h" 30#include "ScopedUtfChars.h" 31#include "thread-inl.h" 32#include "trace.h" 33 34#if defined(__linux__) 35#include <sys/prctl.h> 36#endif 37 38#include <sys/resource.h> 39 40namespace art { 41 42static void EnableDebugger() { 43#if defined(__linux__) 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 } 49#endif 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 59static void EnableDebugFeatures(uint32_t debug_flags) { 60 // Must match values in com.android.internal.os.Zygote. 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, 67 DEBUG_ENABLE_JIT = 1 << 5, 68 DEBUG_GENERATE_DEBUG_INFO = 1 << 6, 69 }; 70 71 Runtime* const runtime = Runtime::Current(); 72 if ((debug_flags & DEBUG_ENABLE_CHECKJNI) != 0) { 73 JavaVMExt* vm = runtime->GetJavaVM(); 74 if (!vm->IsCheckJniEnabled()) { 75 LOG(INFO) << "Late-enabling -Xcheck:jni"; 76 vm->SetCheckJniEnabled(true); 77 // There's only one thread running at this point, so only one JNIEnv to fix up. 78 Thread::Current()->GetJniEnv()->SetCheckJniEnabled(true); 79 } else { 80 LOG(INFO) << "Not late-enabling -Xcheck:jni (already on)"; 81 } 82 debug_flags &= ~DEBUG_ENABLE_CHECKJNI; 83 } 84 85 if ((debug_flags & DEBUG_ENABLE_JNI_LOGGING) != 0) { 86 gLogVerbosity.third_party_jni = true; 87 debug_flags &= ~DEBUG_ENABLE_JNI_LOGGING; 88 } 89 90 Dbg::SetJdwpAllowed((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0); 91 if ((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0) { 92 EnableDebugger(); 93 } 94 debug_flags &= ~DEBUG_ENABLE_DEBUGGER; 95 96 const bool safe_mode = (debug_flags & DEBUG_ENABLE_SAFEMODE) != 0; 97 if (safe_mode) { 98 // Ensure that any (secondary) oat files will be interpreted. 99 runtime->AddCompilerOption("--compiler-filter=interpret-only"); 100 debug_flags &= ~DEBUG_ENABLE_SAFEMODE; 101 } 102 103 bool use_jit = false; 104 if ((debug_flags & DEBUG_ENABLE_JIT) != 0) { 105 if (safe_mode) { 106 LOG(INFO) << "Not enabling JIT due to safe mode"; 107 } else { 108 use_jit = true; 109 LOG(INFO) << "Late-enabling JIT"; 110 } 111 debug_flags &= ~DEBUG_ENABLE_JIT; 112 } 113 runtime->GetJITOptions()->SetUseJIT(use_jit); 114 115 const bool generate_debug_info = (debug_flags & DEBUG_GENERATE_DEBUG_INFO) != 0; 116 if (generate_debug_info) { 117 runtime->AddCompilerOption("--generate-debug-info"); 118 debug_flags &= ~DEBUG_GENERATE_DEBUG_INFO; 119 } 120 121 // This is for backwards compatibility with Dalvik. 122 debug_flags &= ~DEBUG_ENABLE_ASSERT; 123 124 if (debug_flags != 0) { 125 LOG(ERROR) << StringPrintf("Unknown bits set in debug_flags: %#x", debug_flags); 126 } 127} 128 129static jlong ZygoteHooks_nativePreFork(JNIEnv* env, jclass) { 130 Runtime* runtime = Runtime::Current(); 131 CHECK(runtime->IsZygote()) << "runtime instance not started with -Xzygote"; 132 133 runtime->PreZygoteFork(); 134 135 if (Trace::GetMethodTracingMode() != TracingMode::kTracingInactive) { 136 // Tracing active, pause it. 137 Trace::Pause(); 138 } 139 140 // Grab thread before fork potentially makes Thread::pthread_key_self_ unusable. 141 return reinterpret_cast<jlong>(ThreadForEnv(env)); 142} 143 144static void ZygoteHooks_nativePostForkChild(JNIEnv* env, jclass, jlong token, jint debug_flags, 145 jstring instruction_set) { 146 Thread* thread = reinterpret_cast<Thread*>(token); 147 // Our system thread ID, etc, has changed so reset Thread state. 148 thread->InitAfterFork(); 149 EnableDebugFeatures(debug_flags); 150 151 // Update tracing. 152 if (Trace::GetMethodTracingMode() != TracingMode::kTracingInactive) { 153 Trace::TraceOutputMode output_mode = Trace::GetOutputMode(); 154 Trace::TraceMode trace_mode = Trace::GetMode(); 155 size_t buffer_size = Trace::GetBufferSize(); 156 157 // Just drop it. 158 Trace::Abort(); 159 160 // Only restart if it was streaming mode. 161 // TODO: Expose buffer size, so we can also do file mode. 162 if (output_mode == Trace::TraceOutputMode::kStreaming) { 163 const char* proc_name_cutils = get_process_name(); 164 std::string proc_name; 165 if (proc_name_cutils != nullptr) { 166 proc_name = proc_name_cutils; 167 } 168 if (proc_name_cutils == nullptr || proc_name == "zygote" || proc_name == "zygote64") { 169 // Either no process name, or the name hasn't been changed, yet. Just use pid. 170 pid_t pid = getpid(); 171 proc_name = StringPrintf("%u", static_cast<uint32_t>(pid)); 172 } 173 174 std::string profiles_dir(GetDalvikCache("profiles", false /* create_if_absent */)); 175 if (!profiles_dir.empty()) { 176 std::string trace_file = StringPrintf("%s/%s.trace.bin", profiles_dir.c_str(), 177 proc_name.c_str()); 178 Trace::Start(trace_file.c_str(), 179 -1, 180 buffer_size, 181 0, // TODO: Expose flags. 182 output_mode, 183 trace_mode, 184 0); // TODO: Expose interval. 185 if (thread->IsExceptionPending()) { 186 ScopedObjectAccess soa(env); 187 thread->ClearException(); 188 } 189 } else { 190 LOG(ERROR) << "Profiles dir is empty?!?!"; 191 } 192 } 193 } 194 195 if (instruction_set != nullptr) { 196 ScopedUtfChars isa_string(env, instruction_set); 197 InstructionSet isa = GetInstructionSetFromString(isa_string.c_str()); 198 Runtime::NativeBridgeAction action = Runtime::NativeBridgeAction::kUnload; 199 if (isa != kNone && isa != kRuntimeISA) { 200 action = Runtime::NativeBridgeAction::kInitialize; 201 } 202 Runtime::Current()->DidForkFromZygote(env, action, isa_string.c_str()); 203 } else { 204 Runtime::Current()->DidForkFromZygote(env, Runtime::NativeBridgeAction::kUnload, nullptr); 205 } 206} 207 208static JNINativeMethod gMethods[] = { 209 NATIVE_METHOD(ZygoteHooks, nativePreFork, "()J"), 210 NATIVE_METHOD(ZygoteHooks, nativePostForkChild, "(JILjava/lang/String;)V"), 211}; 212 213void register_dalvik_system_ZygoteHooks(JNIEnv* env) { 214 REGISTER_NATIVE_METHODS("dalvik/system/ZygoteHooks"); 215} 216 217} // namespace art 218