blob: df6055dac3791e313edb3213078407decca084a8 [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
17#include <stdlib.h>
18
19#include "debugger.h"
Ian Rogers68d8b422014-07-17 11:09:10 -070020#include "java_vm_ext.h"
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010021#include "jni_internal.h"
22#include "JNIHelp.h"
23#include "thread-inl.h"
24
25#if defined(HAVE_PRCTL)
26#include <sys/prctl.h>
27#endif
28
Narayan Kamathad4b0d22014-04-02 12:06:02 +010029#include <sys/resource.h>
30
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010031namespace art {
32
33static void EnableDebugger() {
34 // To let a non-privileged gdbserver attach to this
35 // process, we must set our dumpable flag.
Ian Rogersc5f17732014-06-05 20:48:42 -070036#if defined(HAVE_PRCTL)
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010037 if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) {
38 PLOG(ERROR) << "prctl(PR_SET_DUMPABLE) failed for pid " << getpid();
39 }
Ian Rogersc5f17732014-06-05 20:48:42 -070040#endif
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010041 // We don't want core dumps, though, so set the core dump size to 0.
42 rlimit rl;
43 rl.rlim_cur = 0;
44 rl.rlim_max = RLIM_INFINITY;
45 if (setrlimit(RLIMIT_CORE, &rl) == -1) {
46 PLOG(ERROR) << "setrlimit(RLIMIT_CORE) failed for pid " << getpid();
47 }
48}
49
50static void EnableDebugFeatures(uint32_t debug_flags) {
Ian Rogers68d8b422014-07-17 11:09:10 -070051 // Must match values in com.android.internal.os.Zygote.
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010052 enum {
53 DEBUG_ENABLE_DEBUGGER = 1,
54 DEBUG_ENABLE_CHECKJNI = 1 << 1,
55 DEBUG_ENABLE_ASSERT = 1 << 2,
56 DEBUG_ENABLE_SAFEMODE = 1 << 3,
57 DEBUG_ENABLE_JNI_LOGGING = 1 << 4,
58 };
59
60 if ((debug_flags & DEBUG_ENABLE_CHECKJNI) != 0) {
61 Runtime* runtime = Runtime::Current();
62 JavaVMExt* vm = runtime->GetJavaVM();
Ian Rogers68d8b422014-07-17 11:09:10 -070063 if (!vm->IsCheckJniEnabled()) {
Brian Carlstrom966ce112014-05-12 17:30:36 -070064 LOG(INFO) << "Late-enabling -Xcheck:jni";
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010065 vm->SetCheckJniEnabled(true);
66 // There's only one thread running at this point, so only one JNIEnv to fix up.
67 Thread::Current()->GetJniEnv()->SetCheckJniEnabled(true);
68 } else {
Brian Carlstrom966ce112014-05-12 17:30:36 -070069 LOG(INFO) << "Not late-enabling -Xcheck:jni (already on)";
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010070 }
71 debug_flags &= ~DEBUG_ENABLE_CHECKJNI;
72 }
73
74 if ((debug_flags & DEBUG_ENABLE_JNI_LOGGING) != 0) {
75 gLogVerbosity.third_party_jni = true;
76 debug_flags &= ~DEBUG_ENABLE_JNI_LOGGING;
77 }
78
79 Dbg::SetJdwpAllowed((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0);
80 if ((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0) {
81 EnableDebugger();
82 }
83 debug_flags &= ~DEBUG_ENABLE_DEBUGGER;
84
85 // These two are for backwards compatibility with Dalvik.
86 debug_flags &= ~DEBUG_ENABLE_ASSERT;
87 debug_flags &= ~DEBUG_ENABLE_SAFEMODE;
88
89 if (debug_flags != 0) {
90 LOG(ERROR) << StringPrintf("Unknown bits set in debug_flags: %#x", debug_flags);
91 }
92}
93
94static jlong ZygoteHooks_nativePreFork(JNIEnv* env, jclass) {
95 Runtime* runtime = Runtime::Current();
96 CHECK(runtime->IsZygote()) << "runtime instance not started with -Xzygote";
Narayan Kamath3de95a72014-04-02 12:54:23 +010097
98 runtime->PreZygoteFork();
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010099
100 // Grab thread before fork potentially makes Thread::pthread_key_self_ unusable.
101 Thread* self = Thread::Current();
102 return reinterpret_cast<jlong>(self);
103}
104
105static void ZygoteHooks_nativePostForkChild(JNIEnv* env, jclass, jlong token, jint debug_flags) {
106 Thread* thread = reinterpret_cast<Thread*>(token);
107 // Our system thread ID, etc, has changed so reset Thread state.
108 thread->InitAfterFork();
109 EnableDebugFeatures(debug_flags);
110 Runtime::Current()->DidForkFromZygote();
111}
112
113static JNINativeMethod gMethods[] = {
114 NATIVE_METHOD(ZygoteHooks, nativePreFork, "()J"),
115 NATIVE_METHOD(ZygoteHooks, nativePostForkChild, "(JI)V"),
116};
117
118void register_dalvik_system_ZygoteHooks(JNIEnv* env) {
119 REGISTER_NATIVE_METHODS("dalvik/system/ZygoteHooks");
120}
121
122} // namespace art