blob: 8524348164a1379eafdbe83dcc885688353036c1 [file] [log] [blame]
Narayan Kamath8b2c8b92014-03-31 16:44:54 +01001/*
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 Gampe277ccbd2014-11-03 21:36:10 -080017#include "dalvik_system_ZygoteHooks.h"
18
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010019#include <stdlib.h>
20
Andreas Gampe40da2862015-02-27 12:49:04 -080021#include <cutils/process_name.h>
22
Ian Rogersd582fa42014-11-05 23:46:43 -080023#include "arch/instruction_set.h"
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010024#include "debugger.h"
Ian Rogers68d8b422014-07-17 11:09:10 -070025#include "java_vm_ext.h"
Mathieu Chartier455f67c2015-03-17 13:48:29 -070026#include "jit/jit.h"
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010027#include "jni_internal.h"
28#include "JNIHelp.h"
Andreas Gampe40da2862015-02-27 12:49:04 -080029#include "scoped_thread_state_change.h"
Andreas Gampe6be67ee2014-09-02 21:22:18 -070030#include "ScopedUtfChars.h"
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010031#include "thread-inl.h"
Andreas Gampe40da2862015-02-27 12:49:04 -080032#include "trace.h"
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010033
Elliott Hughes0a18df82015-01-09 15:16:16 -080034#if defined(__linux__)
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010035#include <sys/prctl.h>
36#endif
37
Narayan Kamathad4b0d22014-04-02 12:06:02 +010038#include <sys/resource.h>
39
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010040namespace art {
41
42static void EnableDebugger() {
Elliott Hughes0a18df82015-01-09 15:16:16 -080043#if defined(__linux__)
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010044 // 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 Rogersc5f17732014-06-05 20:48:42 -070049#endif
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010050 // 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) {
Ian Rogers68d8b422014-07-17 11:09:10 -070060 // Must match values in com.android.internal.os.Zygote.
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010061 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,
Mathieu Chartier6eff38d2015-03-17 09:52:22 -070067 DEBUG_ENABLE_JIT = 1 << 5,
Andreas Gampe00bb8782015-04-24 16:33:43 -070068 DEBUG_GENERATE_CFI = 1 << 6,
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010069 };
70
Mathieu Chartier6eff38d2015-03-17 09:52:22 -070071 Runtime* const runtime = Runtime::Current();
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010072 if ((debug_flags & DEBUG_ENABLE_CHECKJNI) != 0) {
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010073 JavaVMExt* vm = runtime->GetJavaVM();
Ian Rogers68d8b422014-07-17 11:09:10 -070074 if (!vm->IsCheckJniEnabled()) {
Brian Carlstrom966ce112014-05-12 17:30:36 -070075 LOG(INFO) << "Late-enabling -Xcheck:jni";
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010076 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 {
Brian Carlstrom966ce112014-05-12 17:30:36 -070080 LOG(INFO) << "Not late-enabling -Xcheck:jni (already on)";
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010081 }
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
Mathieu Chartier6eff38d2015-03-17 09:52:22 -070096 const bool safe_mode = (debug_flags & DEBUG_ENABLE_SAFEMODE) != 0;
97 if (safe_mode) {
Andreas Gamped2abbc92014-12-19 09:53:27 -080098 // Ensure that any (secondary) oat files will be interpreted.
Andreas Gamped2abbc92014-12-19 09:53:27 -080099 runtime->AddCompilerOption("--compiler-filter=interpret-only");
100 debug_flags &= ~DEBUG_ENABLE_SAFEMODE;
101 }
102
Mathieu Chartier455f67c2015-03-17 13:48:29 -0700103 bool use_jit = false;
Mathieu Chartier6eff38d2015-03-17 09:52:22 -0700104 if ((debug_flags & DEBUG_ENABLE_JIT) != 0) {
105 if (safe_mode) {
Mathieu Chartier455f67c2015-03-17 13:48:29 -0700106 LOG(INFO) << "Not enabling JIT due to safe mode";
Mathieu Chartier6eff38d2015-03-17 09:52:22 -0700107 } else {
Mathieu Chartier455f67c2015-03-17 13:48:29 -0700108 use_jit = true;
109 LOG(INFO) << "Late-enabling JIT";
Mathieu Chartier6eff38d2015-03-17 09:52:22 -0700110 }
111 debug_flags &= ~DEBUG_ENABLE_JIT;
112 }
Mathieu Chartier455f67c2015-03-17 13:48:29 -0700113 runtime->GetJITOptions()->SetUseJIT(use_jit);
Mathieu Chartier6eff38d2015-03-17 09:52:22 -0700114
Andreas Gampe00bb8782015-04-24 16:33:43 -0700115 const bool generate_cfi = (debug_flags & DEBUG_GENERATE_CFI) != 0;
116 if (generate_cfi) {
117 runtime->AddCompilerOption("--include-cfi");
118 debug_flags &= ~DEBUG_GENERATE_CFI;
119 }
120
Andreas Gamped2abbc92014-12-19 09:53:27 -0800121 // This is for backwards compatibility with Dalvik.
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100122 debug_flags &= ~DEBUG_ENABLE_ASSERT;
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100123
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";
Narayan Kamath3de95a72014-04-02 12:54:23 +0100132
133 runtime->PreZygoteFork();
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100134
Andreas Gampe40da2862015-02-27 12:49:04 -0800135 if (Trace::GetMethodTracingMode() != TracingMode::kTracingInactive) {
136 // Tracing active, pause it.
137 Trace::Pause();
138 }
139
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100140 // Grab thread before fork potentially makes Thread::pthread_key_self_ unusable.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700141 return reinterpret_cast<jlong>(ThreadForEnv(env));
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100142}
143
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700144static void ZygoteHooks_nativePostForkChild(JNIEnv* env, jclass, jlong token, jint debug_flags,
145 jstring instruction_set) {
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100146 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);
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700150
Andreas Gampe40da2862015-02-27 12:49:04 -0800151 // Update tracing.
152 if (Trace::GetMethodTracingMode() != TracingMode::kTracingInactive) {
153 Trace::TraceOutputMode output_mode = Trace::GetOutputMode();
154 Trace::TraceMode trace_mode = Trace::GetMode();
155
156 // Just drop it.
157 Trace::Abort();
158
159 // Only restart if it was streaming mode.
160 // TODO: Expose buffer size, so we can also do file mode.
161 if (output_mode == Trace::TraceOutputMode::kStreaming) {
162 const char* proc_name_cutils = get_process_name();
163 std::string proc_name;
164 if (proc_name_cutils != nullptr) {
165 proc_name = proc_name_cutils;
166 }
167 if (proc_name_cutils == nullptr || proc_name == "zygote" || proc_name == "zygote64") {
168 // Either no process name, or the name hasn't been changed, yet. Just use pid.
169 pid_t pid = getpid();
170 proc_name = StringPrintf("%u", static_cast<uint32_t>(pid));
171 }
172
173 std::string profiles_dir(GetDalvikCache("profiles", false /* create_if_absent */));
174 if (!profiles_dir.empty()) {
175 std::string trace_file = StringPrintf("%s/%s.trace.bin", profiles_dir.c_str(),
176 proc_name.c_str());
177 Trace::Start(trace_file.c_str(),
178 -1,
179 -1, // TODO: Expose buffer size.
180 0, // TODO: Expose flags.
181 output_mode,
182 trace_mode,
183 0); // TODO: Expose interval.
184 if (thread->IsExceptionPending()) {
185 ScopedObjectAccess soa(env);
186 thread->ClearException();
187 }
188 } else {
189 LOG(ERROR) << "Profiles dir is empty?!?!";
190 }
191 }
192 }
193
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700194 if (instruction_set != nullptr) {
195 ScopedUtfChars isa_string(env, instruction_set);
196 InstructionSet isa = GetInstructionSetFromString(isa_string.c_str());
jgu21a6da74e2014-09-10 06:57:17 -0400197 Runtime::NativeBridgeAction action = Runtime::NativeBridgeAction::kUnload;
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700198 if (isa != kNone && isa != kRuntimeISA) {
199 action = Runtime::NativeBridgeAction::kInitialize;
200 }
jgu21a6da74e2014-09-10 06:57:17 -0400201 Runtime::Current()->DidForkFromZygote(env, action, isa_string.c_str());
202 } else {
203 Runtime::Current()->DidForkFromZygote(env, Runtime::NativeBridgeAction::kUnload, nullptr);
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700204 }
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100205}
206
207static JNINativeMethod gMethods[] = {
208 NATIVE_METHOD(ZygoteHooks, nativePreFork, "()J"),
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700209 NATIVE_METHOD(ZygoteHooks, nativePostForkChild, "(JILjava/lang/String;)V"),
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100210};
211
212void register_dalvik_system_ZygoteHooks(JNIEnv* env) {
213 REGISTER_NATIVE_METHODS("dalvik/system/ZygoteHooks");
214}
215
216} // namespace art