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