blob: 2b3d10c585b7ca131477d77802b51f2bec273da5 [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
Maunik Shahd99a5432014-09-04 01:37:44 +053037#ifdef __ANDROID__
38#include <cutils/properties.h>
39#endif
Narayan Kamathad4b0d22014-04-02 12:06:02 +010040#include <sys/resource.h>
41
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010042namespace art {
43
44static void EnableDebugger() {
Elliott Hughes0a18df82015-01-09 15:16:16 -080045#if defined(__linux__)
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010046 // To let a non-privileged gdbserver attach to this
47 // process, we must set our dumpable flag.
48 if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) {
49 PLOG(ERROR) << "prctl(PR_SET_DUMPABLE) failed for pid " << getpid();
50 }
Oleksiy Vyalove21145f2016-06-21 16:21:37 -070051
52 // Even if Yama is on a non-privileged native debugger should
53 // be able to attach to the debuggable app.
54 if (prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY, 0, 0, 0) == -1) {
55 // if Yama is off prctl(PR_SET_PTRACER) returns EINVAL - don't log in this
56 // case since it's expected behaviour.
57 if (errno != EINVAL) {
58 PLOG(ERROR) << "prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY) failed for pid " << getpid();
59 }
60 }
Ian Rogersc5f17732014-06-05 20:48:42 -070061#endif
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010062 // We don't want core dumps, though, so set the core dump size to 0.
63 rlimit rl;
Maunik Shahd99a5432014-09-04 01:37:44 +053064#ifdef __ANDROID__
65 char prop_value[PROPERTY_VALUE_MAX];
66 property_get("persist.debug.trace", prop_value, "0");
67 if (prop_value[0] == '1') {
68 LOG(INFO) << "setting RLIM to infinity for process " << getpid();
69 rl.rlim_cur = RLIM_INFINITY;
70 } else {
71 rl.rlim_cur = 0;
72 }
73#else
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010074 rl.rlim_cur = 0;
Maunik Shahd99a5432014-09-04 01:37:44 +053075#endif
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010076 rl.rlim_max = RLIM_INFINITY;
77 if (setrlimit(RLIMIT_CORE, &rl) == -1) {
78 PLOG(ERROR) << "setrlimit(RLIMIT_CORE) failed for pid " << getpid();
79 }
80}
81
82static void EnableDebugFeatures(uint32_t debug_flags) {
Ian Rogers68d8b422014-07-17 11:09:10 -070083 // Must match values in com.android.internal.os.Zygote.
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010084 enum {
85 DEBUG_ENABLE_DEBUGGER = 1,
86 DEBUG_ENABLE_CHECKJNI = 1 << 1,
87 DEBUG_ENABLE_ASSERT = 1 << 2,
88 DEBUG_ENABLE_SAFEMODE = 1 << 3,
89 DEBUG_ENABLE_JNI_LOGGING = 1 << 4,
Nicolas Geoffray0f042e02015-11-05 11:32:24 +000090 DEBUG_GENERATE_DEBUG_INFO = 1 << 5,
Siva Chandra05d24152016-01-05 17:43:17 -080091 DEBUG_ALWAYS_JIT = 1 << 6,
Tamas Berghammerdd5e5e92016-02-12 16:29:00 +000092 DEBUG_NATIVE_DEBUGGABLE = 1 << 7,
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010093 };
94
Mathieu Chartier6eff38d2015-03-17 09:52:22 -070095 Runtime* const runtime = Runtime::Current();
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010096 if ((debug_flags & DEBUG_ENABLE_CHECKJNI) != 0) {
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010097 JavaVMExt* vm = runtime->GetJavaVM();
Ian Rogers68d8b422014-07-17 11:09:10 -070098 if (!vm->IsCheckJniEnabled()) {
Brian Carlstrom966ce112014-05-12 17:30:36 -070099 LOG(INFO) << "Late-enabling -Xcheck:jni";
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100100 vm->SetCheckJniEnabled(true);
101 // There's only one thread running at this point, so only one JNIEnv to fix up.
102 Thread::Current()->GetJniEnv()->SetCheckJniEnabled(true);
103 } else {
Brian Carlstrom966ce112014-05-12 17:30:36 -0700104 LOG(INFO) << "Not late-enabling -Xcheck:jni (already on)";
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100105 }
106 debug_flags &= ~DEBUG_ENABLE_CHECKJNI;
107 }
108
109 if ((debug_flags & DEBUG_ENABLE_JNI_LOGGING) != 0) {
110 gLogVerbosity.third_party_jni = true;
111 debug_flags &= ~DEBUG_ENABLE_JNI_LOGGING;
112 }
113
114 Dbg::SetJdwpAllowed((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0);
115 if ((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0) {
116 EnableDebugger();
117 }
118 debug_flags &= ~DEBUG_ENABLE_DEBUGGER;
119
Mathieu Chartier6eff38d2015-03-17 09:52:22 -0700120 const bool safe_mode = (debug_flags & DEBUG_ENABLE_SAFEMODE) != 0;
121 if (safe_mode) {
Andreas Gamped2abbc92014-12-19 09:53:27 -0800122 // Ensure that any (secondary) oat files will be interpreted.
Andreas Gamped2abbc92014-12-19 09:53:27 -0800123 runtime->AddCompilerOption("--compiler-filter=interpret-only");
Nicolas Geoffray0f042e02015-11-05 11:32:24 +0000124 runtime->SetSafeMode(true);
Andreas Gamped2abbc92014-12-19 09:53:27 -0800125 debug_flags &= ~DEBUG_ENABLE_SAFEMODE;
126 }
127
David Srbecky8363c772015-05-28 16:12:43 +0100128 const bool generate_debug_info = (debug_flags & DEBUG_GENERATE_DEBUG_INFO) != 0;
129 if (generate_debug_info) {
130 runtime->AddCompilerOption("--generate-debug-info");
131 debug_flags &= ~DEBUG_GENERATE_DEBUG_INFO;
Andreas Gampe00bb8782015-04-24 16:33:43 -0700132 }
133
Andreas Gamped2abbc92014-12-19 09:53:27 -0800134 // This is for backwards compatibility with Dalvik.
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100135 debug_flags &= ~DEBUG_ENABLE_ASSERT;
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100136
Siva Chandra05d24152016-01-05 17:43:17 -0800137 if ((debug_flags & DEBUG_ALWAYS_JIT) != 0) {
138 jit::JitOptions* jit_options = runtime->GetJITOptions();
139 CHECK(jit_options != nullptr);
140 jit_options->SetJitAtFirstUse();
141 debug_flags &= ~DEBUG_ALWAYS_JIT;
142 }
143
Tamas Berghammerdd5e5e92016-02-12 16:29:00 +0000144 if ((debug_flags & DEBUG_NATIVE_DEBUGGABLE) != 0) {
David Srbecky346dc992016-03-13 22:00:07 +0000145 runtime->AddCompilerOption("--debuggable");
146 runtime->AddCompilerOption("--generate-debug-info");
David Srbeckyf4480162016-03-16 00:06:24 +0000147 runtime->SetNativeDebuggable(true);
Tamas Berghammerdd5e5e92016-02-12 16:29:00 +0000148 debug_flags &= ~DEBUG_NATIVE_DEBUGGABLE;
149 }
150
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100151 if (debug_flags != 0) {
152 LOG(ERROR) << StringPrintf("Unknown bits set in debug_flags: %#x", debug_flags);
153 }
154}
155
156static jlong ZygoteHooks_nativePreFork(JNIEnv* env, jclass) {
157 Runtime* runtime = Runtime::Current();
158 CHECK(runtime->IsZygote()) << "runtime instance not started with -Xzygote";
Narayan Kamath3de95a72014-04-02 12:54:23 +0100159
160 runtime->PreZygoteFork();
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100161
Andreas Gampe40da2862015-02-27 12:49:04 -0800162 if (Trace::GetMethodTracingMode() != TracingMode::kTracingInactive) {
163 // Tracing active, pause it.
164 Trace::Pause();
165 }
166
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100167 // Grab thread before fork potentially makes Thread::pthread_key_self_ unusable.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700168 return reinterpret_cast<jlong>(ThreadForEnv(env));
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100169}
170
Nicolas Geoffrayd66c8622015-12-11 14:59:16 +0000171static void ZygoteHooks_nativePostForkChild(JNIEnv* env,
172 jclass,
173 jlong token,
174 jint debug_flags,
175 jboolean is_system_server,
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700176 jstring instruction_set) {
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100177 Thread* thread = reinterpret_cast<Thread*>(token);
178 // Our system thread ID, etc, has changed so reset Thread state.
179 thread->InitAfterFork();
180 EnableDebugFeatures(debug_flags);
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700181
Andreas Gampe40da2862015-02-27 12:49:04 -0800182 // Update tracing.
183 if (Trace::GetMethodTracingMode() != TracingMode::kTracingInactive) {
184 Trace::TraceOutputMode output_mode = Trace::GetOutputMode();
185 Trace::TraceMode trace_mode = Trace::GetMode();
Andreas Gampee34a42c2015-04-25 14:44:29 -0700186 size_t buffer_size = Trace::GetBufferSize();
Andreas Gampe40da2862015-02-27 12:49:04 -0800187
188 // Just drop it.
189 Trace::Abort();
190
191 // Only restart if it was streaming mode.
192 // TODO: Expose buffer size, so we can also do file mode.
193 if (output_mode == Trace::TraceOutputMode::kStreaming) {
194 const char* proc_name_cutils = get_process_name();
195 std::string proc_name;
196 if (proc_name_cutils != nullptr) {
197 proc_name = proc_name_cutils;
198 }
199 if (proc_name_cutils == nullptr || proc_name == "zygote" || proc_name == "zygote64") {
200 // Either no process name, or the name hasn't been changed, yet. Just use pid.
201 pid_t pid = getpid();
202 proc_name = StringPrintf("%u", static_cast<uint32_t>(pid));
203 }
204
Calin Juravlef83e7332015-11-04 16:16:47 +0000205 std::string trace_file = StringPrintf("/data/misc/trace/%s.trace.bin", proc_name.c_str());
206 Trace::Start(trace_file.c_str(),
207 -1,
208 buffer_size,
209 0, // TODO: Expose flags.
210 output_mode,
211 trace_mode,
212 0); // TODO: Expose interval.
213 if (thread->IsExceptionPending()) {
214 ScopedObjectAccess soa(env);
215 thread->ClearException();
Andreas Gampe40da2862015-02-27 12:49:04 -0800216 }
217 }
218 }
219
Nicolas Geoffrayd66c8622015-12-11 14:59:16 +0000220 if (instruction_set != nullptr && !is_system_server) {
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700221 ScopedUtfChars isa_string(env, instruction_set);
222 InstructionSet isa = GetInstructionSetFromString(isa_string.c_str());
jgu21a6da74e2014-09-10 06:57:17 -0400223 Runtime::NativeBridgeAction action = Runtime::NativeBridgeAction::kUnload;
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700224 if (isa != kNone && isa != kRuntimeISA) {
225 action = Runtime::NativeBridgeAction::kInitialize;
226 }
Nicolas Geoffrayd66c8622015-12-11 14:59:16 +0000227 Runtime::Current()->InitNonZygoteOrPostFork(
228 env, is_system_server, action, isa_string.c_str());
jgu21a6da74e2014-09-10 06:57:17 -0400229 } else {
Nicolas Geoffrayd66c8622015-12-11 14:59:16 +0000230 Runtime::Current()->InitNonZygoteOrPostFork(
231 env, is_system_server, Runtime::NativeBridgeAction::kUnload, nullptr);
Andreas Gampe6be67ee2014-09-02 21:22:18 -0700232 }
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100233}
234
Andreas Gampe415d8072016-04-11 08:42:26 -0700235static void ZygoteHooks_startZygoteNoThreadCreation(JNIEnv* env ATTRIBUTE_UNUSED,
236 jclass klass ATTRIBUTE_UNUSED) {
237 Runtime::Current()->SetZygoteNoThreadSection(true);
238}
239
240static void ZygoteHooks_stopZygoteNoThreadCreation(JNIEnv* env ATTRIBUTE_UNUSED,
241 jclass klass ATTRIBUTE_UNUSED) {
242 Runtime::Current()->SetZygoteNoThreadSection(false);
243}
244
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100245static JNINativeMethod gMethods[] = {
246 NATIVE_METHOD(ZygoteHooks, nativePreFork, "()J"),
Nicolas Geoffrayd66c8622015-12-11 14:59:16 +0000247 NATIVE_METHOD(ZygoteHooks, nativePostForkChild, "(JIZLjava/lang/String;)V"),
Andreas Gampe415d8072016-04-11 08:42:26 -0700248 NATIVE_METHOD(ZygoteHooks, startZygoteNoThreadCreation, "()V"),
249 NATIVE_METHOD(ZygoteHooks, stopZygoteNoThreadCreation, "()V"),
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100250};
251
252void register_dalvik_system_ZygoteHooks(JNIEnv* env) {
253 REGISTER_NATIVE_METHODS("dalvik/system/ZygoteHooks");
254}
255
256} // namespace art