blob: 820bd0420fcb18e24ae0f21e58185ae92d148f88 [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.
Ian Rogersc5f17732014-06-05 20:48:42 -070035#if defined(HAVE_PRCTL)
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010036 if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) {
37 PLOG(ERROR) << "prctl(PR_SET_DUMPABLE) failed for pid " << getpid();
38 }
Ian Rogersc5f17732014-06-05 20:48:42 -070039#endif
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010040 // We don't want core dumps, though, so set the core dump size to 0.
41 rlimit rl;
42 rl.rlim_cur = 0;
43 rl.rlim_max = RLIM_INFINITY;
44 if (setrlimit(RLIMIT_CORE, &rl) == -1) {
45 PLOG(ERROR) << "setrlimit(RLIMIT_CORE) failed for pid " << getpid();
46 }
47}
48
49static void EnableDebugFeatures(uint32_t debug_flags) {
50 // Must match values in dalvik.system.Zygote.
51 enum {
52 DEBUG_ENABLE_DEBUGGER = 1,
53 DEBUG_ENABLE_CHECKJNI = 1 << 1,
54 DEBUG_ENABLE_ASSERT = 1 << 2,
55 DEBUG_ENABLE_SAFEMODE = 1 << 3,
56 DEBUG_ENABLE_JNI_LOGGING = 1 << 4,
57 };
58
59 if ((debug_flags & DEBUG_ENABLE_CHECKJNI) != 0) {
60 Runtime* runtime = Runtime::Current();
61 JavaVMExt* vm = runtime->GetJavaVM();
62 if (!vm->check_jni) {
Brian Carlstrom966ce112014-05-12 17:30:36 -070063 LOG(INFO) << "Late-enabling -Xcheck:jni";
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010064 vm->SetCheckJniEnabled(true);
65 // There's only one thread running at this point, so only one JNIEnv to fix up.
66 Thread::Current()->GetJniEnv()->SetCheckJniEnabled(true);
67 } else {
Brian Carlstrom966ce112014-05-12 17:30:36 -070068 LOG(INFO) << "Not late-enabling -Xcheck:jni (already on)";
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010069 }
70 debug_flags &= ~DEBUG_ENABLE_CHECKJNI;
71 }
72
73 if ((debug_flags & DEBUG_ENABLE_JNI_LOGGING) != 0) {
74 gLogVerbosity.third_party_jni = true;
75 debug_flags &= ~DEBUG_ENABLE_JNI_LOGGING;
76 }
77
78 Dbg::SetJdwpAllowed((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0);
79 if ((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0) {
80 EnableDebugger();
81 }
82 debug_flags &= ~DEBUG_ENABLE_DEBUGGER;
83
84 // These two are for backwards compatibility with Dalvik.
85 debug_flags &= ~DEBUG_ENABLE_ASSERT;
86 debug_flags &= ~DEBUG_ENABLE_SAFEMODE;
87
88 if (debug_flags != 0) {
89 LOG(ERROR) << StringPrintf("Unknown bits set in debug_flags: %#x", debug_flags);
90 }
91}
92
93static jlong ZygoteHooks_nativePreFork(JNIEnv* env, jclass) {
94 Runtime* runtime = Runtime::Current();
95 CHECK(runtime->IsZygote()) << "runtime instance not started with -Xzygote";
Narayan Kamath3de95a72014-04-02 12:54:23 +010096
97 runtime->PreZygoteFork();
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010098
99 // Grab thread before fork potentially makes Thread::pthread_key_self_ unusable.
100 Thread* self = Thread::Current();
101 return reinterpret_cast<jlong>(self);
102}
103
104static void ZygoteHooks_nativePostForkChild(JNIEnv* env, jclass, jlong token, jint debug_flags) {
105 Thread* thread = reinterpret_cast<Thread*>(token);
106 // Our system thread ID, etc, has changed so reset Thread state.
107 thread->InitAfterFork();
108 EnableDebugFeatures(debug_flags);
109 Runtime::Current()->DidForkFromZygote();
110}
111
112static JNINativeMethod gMethods[] = {
113 NATIVE_METHOD(ZygoteHooks, nativePreFork, "()J"),
114 NATIVE_METHOD(ZygoteHooks, nativePostForkChild, "(JI)V"),
115};
116
117void register_dalvik_system_ZygoteHooks(JNIEnv* env) {
118 REGISTER_NATIVE_METHODS("dalvik/system/ZygoteHooks");
119}
120
121} // namespace art