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