blob: 67d825efaa2d3cb83fe32f2448564d8002f0e1bd [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,
Nicolas Geoffray0f042e02015-11-05 11:32:24 +000067 DEBUG_GENERATE_DEBUG_INFO = 1 << 5,
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010068 };
69
Mathieu Chartier6eff38d2015-03-17 09:52:22 -070070 Runtime* const runtime = Runtime::Current();
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010071 if ((debug_flags & DEBUG_ENABLE_CHECKJNI) != 0) {
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010072 JavaVMExt* vm = runtime->GetJavaVM();
Ian Rogers68d8b422014-07-17 11:09:10 -070073 if (!vm->IsCheckJniEnabled()) {
Brian Carlstrom966ce112014-05-12 17:30:36 -070074 LOG(INFO) << "Late-enabling -Xcheck:jni";
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010075 vm->SetCheckJniEnabled(true);
76 // There's only one thread running at this point, so only one JNIEnv to fix up.
77 Thread::Current()->GetJniEnv()->SetCheckJniEnabled(true);
78 } else {
Brian Carlstrom966ce112014-05-12 17:30:36 -070079 LOG(INFO) << "Not late-enabling -Xcheck:jni (already on)";
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010080 }
81 debug_flags &= ~DEBUG_ENABLE_CHECKJNI;
82 }
83
84 if ((debug_flags & DEBUG_ENABLE_JNI_LOGGING) != 0) {
85 gLogVerbosity.third_party_jni = true;
86 debug_flags &= ~DEBUG_ENABLE_JNI_LOGGING;
87 }
88
89 Dbg::SetJdwpAllowed((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0);
90 if ((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0) {
91 EnableDebugger();
92 }
93 debug_flags &= ~DEBUG_ENABLE_DEBUGGER;
94
Mathieu Chartier6eff38d2015-03-17 09:52:22 -070095 const bool safe_mode = (debug_flags & DEBUG_ENABLE_SAFEMODE) != 0;
96 if (safe_mode) {
Andreas Gamped2abbc92014-12-19 09:53:27 -080097 // Ensure that any (secondary) oat files will be interpreted.
Andreas Gamped2abbc92014-12-19 09:53:27 -080098 runtime->AddCompilerOption("--compiler-filter=interpret-only");
Nicolas Geoffray0f042e02015-11-05 11:32:24 +000099 runtime->SetSafeMode(true);
Andreas Gamped2abbc92014-12-19 09:53:27 -0800100 debug_flags &= ~DEBUG_ENABLE_SAFEMODE;
101 }
102
David Srbecky8363c772015-05-28 16:12:43 +0100103 const bool generate_debug_info = (debug_flags & DEBUG_GENERATE_DEBUG_INFO) != 0;
104 if (generate_debug_info) {
105 runtime->AddCompilerOption("--generate-debug-info");
106 debug_flags &= ~DEBUG_GENERATE_DEBUG_INFO;
Andreas Gampe00bb8782015-04-24 16:33:43 -0700107 }
108
Andreas Gamped2abbc92014-12-19 09:53:27 -0800109 // This is for backwards compatibility with Dalvik.
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100110 debug_flags &= ~DEBUG_ENABLE_ASSERT;
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100111
112 if (debug_flags != 0) {
113 LOG(ERROR) << StringPrintf("Unknown bits set in debug_flags: %#x", debug_flags);
114 }
115}
116
117static jlong ZygoteHooks_nativePreFork(JNIEnv* env, jclass) {
118 Runtime* runtime = Runtime::Current();
119 CHECK(runtime->IsZygote()) << "runtime instance not started with -Xzygote";
Narayan Kamath3de95a72014-04-02 12:54:23 +0100120
121 runtime->PreZygoteFork();
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100122
Andreas Gampe40da2862015-02-27 12:49:04 -0800123 if (Trace::GetMethodTracingMode() != TracingMode::kTracingInactive) {
124 // Tracing active, pause it.
125 Trace::Pause();
126 }
127
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100128 // Grab thread before fork potentially makes Thread::pthread_key_self_ unusable.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700129 return reinterpret_cast<jlong>(ThreadForEnv(env));
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100130}
131
Nicolas Geoffrayd66c8622015-12-11 14:59:16 +0000132static void ZygoteHooks_nativePostForkChild(JNIEnv* env,
133 jclass,
134 jlong token,
135 jint debug_flags,
136 jboolean is_system_server,
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700137 jstring instruction_set) {
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100138 Thread* thread = reinterpret_cast<Thread*>(token);
139 // Our system thread ID, etc, has changed so reset Thread state.
140 thread->InitAfterFork();
141 EnableDebugFeatures(debug_flags);
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700142
Andreas Gampe40da2862015-02-27 12:49:04 -0800143 // Update tracing.
144 if (Trace::GetMethodTracingMode() != TracingMode::kTracingInactive) {
145 Trace::TraceOutputMode output_mode = Trace::GetOutputMode();
146 Trace::TraceMode trace_mode = Trace::GetMode();
Andreas Gampee34a42c2015-04-25 14:44:29 -0700147 size_t buffer_size = Trace::GetBufferSize();
Andreas Gampe40da2862015-02-27 12:49:04 -0800148
149 // Just drop it.
150 Trace::Abort();
151
152 // Only restart if it was streaming mode.
153 // TODO: Expose buffer size, so we can also do file mode.
154 if (output_mode == Trace::TraceOutputMode::kStreaming) {
155 const char* proc_name_cutils = get_process_name();
156 std::string proc_name;
157 if (proc_name_cutils != nullptr) {
158 proc_name = proc_name_cutils;
159 }
160 if (proc_name_cutils == nullptr || proc_name == "zygote" || proc_name == "zygote64") {
161 // Either no process name, or the name hasn't been changed, yet. Just use pid.
162 pid_t pid = getpid();
163 proc_name = StringPrintf("%u", static_cast<uint32_t>(pid));
164 }
165
Calin Juravlef83e7332015-11-04 16:16:47 +0000166 std::string trace_file = StringPrintf("/data/misc/trace/%s.trace.bin", proc_name.c_str());
167 Trace::Start(trace_file.c_str(),
168 -1,
169 buffer_size,
170 0, // TODO: Expose flags.
171 output_mode,
172 trace_mode,
173 0); // TODO: Expose interval.
174 if (thread->IsExceptionPending()) {
175 ScopedObjectAccess soa(env);
176 thread->ClearException();
Andreas Gampe40da2862015-02-27 12:49:04 -0800177 }
178 }
179 }
180
Nicolas Geoffrayd66c8622015-12-11 14:59:16 +0000181 if (instruction_set != nullptr && !is_system_server) {
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700182 ScopedUtfChars isa_string(env, instruction_set);
183 InstructionSet isa = GetInstructionSetFromString(isa_string.c_str());
jgu21a6da74e2014-09-10 06:57:17 -0400184 Runtime::NativeBridgeAction action = Runtime::NativeBridgeAction::kUnload;
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700185 if (isa != kNone && isa != kRuntimeISA) {
186 action = Runtime::NativeBridgeAction::kInitialize;
187 }
Nicolas Geoffrayd66c8622015-12-11 14:59:16 +0000188 Runtime::Current()->InitNonZygoteOrPostFork(
189 env, is_system_server, action, isa_string.c_str());
jgu21a6da74e2014-09-10 06:57:17 -0400190 } else {
Nicolas Geoffrayd66c8622015-12-11 14:59:16 +0000191 Runtime::Current()->InitNonZygoteOrPostFork(
192 env, is_system_server, Runtime::NativeBridgeAction::kUnload, nullptr);
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700193 }
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100194}
195
196static JNINativeMethod gMethods[] = {
197 NATIVE_METHOD(ZygoteHooks, nativePreFork, "()J"),
Nicolas Geoffrayd66c8622015-12-11 14:59:16 +0000198 NATIVE_METHOD(ZygoteHooks, nativePostForkChild, "(JIZLjava/lang/String;)V"),
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100199};
200
201void register_dalvik_system_ZygoteHooks(JNIEnv* env) {
202 REGISTER_NATIVE_METHODS("dalvik/system/ZygoteHooks");
203}
204
205} // namespace art