blob: c3b84e3b6c2a6bb0ccaefb4a031f6c7468f54738 [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"
Andreas Gampe13e3c6d2014-09-02 21:22:18 -070020#include "instruction_set.h"
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010021#include "jni_internal.h"
22#include "JNIHelp.h"
Andreas Gampe13e3c6d2014-09-02 21:22:18 -070023#include "ScopedUtfChars.h"
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010024#include "thread-inl.h"
25
26#if defined(HAVE_PRCTL)
27#include <sys/prctl.h>
28#endif
29
Narayan Kamathad4b0d22014-04-02 12:06:02 +010030#include <sys/resource.h>
31
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010032namespace art {
33
34static void EnableDebugger() {
35 // To let a non-privileged gdbserver attach to this
36 // process, we must set our dumpable flag.
Ian Rogersc5f17732014-06-05 20:48:42 -070037#if defined(HAVE_PRCTL)
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010038 if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) {
39 PLOG(ERROR) << "prctl(PR_SET_DUMPABLE) failed for pid " << getpid();
40 }
Ian Rogersc5f17732014-06-05 20:48:42 -070041#endif
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010042 // We don't want core dumps, though, so set the core dump size to 0.
43 rlimit rl;
44 rl.rlim_cur = 0;
45 rl.rlim_max = RLIM_INFINITY;
46 if (setrlimit(RLIMIT_CORE, &rl) == -1) {
47 PLOG(ERROR) << "setrlimit(RLIMIT_CORE) failed for pid " << getpid();
48 }
49}
50
51static void EnableDebugFeatures(uint32_t debug_flags) {
52 // Must match values in dalvik.system.Zygote.
53 enum {
54 DEBUG_ENABLE_DEBUGGER = 1,
55 DEBUG_ENABLE_CHECKJNI = 1 << 1,
56 DEBUG_ENABLE_ASSERT = 1 << 2,
57 DEBUG_ENABLE_SAFEMODE = 1 << 3,
58 DEBUG_ENABLE_JNI_LOGGING = 1 << 4,
59 };
60
61 if ((debug_flags & DEBUG_ENABLE_CHECKJNI) != 0) {
62 Runtime* runtime = Runtime::Current();
63 JavaVMExt* vm = runtime->GetJavaVM();
64 if (!vm->check_jni) {
Brian Carlstrom966ce112014-05-12 17:30:36 -070065 LOG(INFO) << "Late-enabling -Xcheck:jni";
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010066 vm->SetCheckJniEnabled(true);
67 // There's only one thread running at this point, so only one JNIEnv to fix up.
68 Thread::Current()->GetJniEnv()->SetCheckJniEnabled(true);
69 } else {
Brian Carlstrom966ce112014-05-12 17:30:36 -070070 LOG(INFO) << "Not late-enabling -Xcheck:jni (already on)";
Narayan Kamath8b2c8b92014-03-31 16:44:54 +010071 }
72 debug_flags &= ~DEBUG_ENABLE_CHECKJNI;
73 }
74
75 if ((debug_flags & DEBUG_ENABLE_JNI_LOGGING) != 0) {
76 gLogVerbosity.third_party_jni = true;
77 debug_flags &= ~DEBUG_ENABLE_JNI_LOGGING;
78 }
79
80 Dbg::SetJdwpAllowed((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0);
81 if ((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0) {
82 EnableDebugger();
83 }
84 debug_flags &= ~DEBUG_ENABLE_DEBUGGER;
85
86 // These two are for backwards compatibility with Dalvik.
87 debug_flags &= ~DEBUG_ENABLE_ASSERT;
88 debug_flags &= ~DEBUG_ENABLE_SAFEMODE;
89
90 if (debug_flags != 0) {
91 LOG(ERROR) << StringPrintf("Unknown bits set in debug_flags: %#x", debug_flags);
92 }
93}
94
95static jlong ZygoteHooks_nativePreFork(JNIEnv* env, jclass) {
96 Runtime* runtime = Runtime::Current();
97 CHECK(runtime->IsZygote()) << "runtime instance not started with -Xzygote";
Narayan Kamath3de95a72014-04-02 12:54:23 +010098
99 runtime->PreZygoteFork();
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100100
101 // Grab thread before fork potentially makes Thread::pthread_key_self_ unusable.
102 Thread* self = Thread::Current();
103 return reinterpret_cast<jlong>(self);
104}
105
Andreas Gampe13e3c6d2014-09-02 21:22:18 -0700106static void ZygoteHooks_nativePostForkChild(JNIEnv* env, jclass, jlong token, jint debug_flags,
107 jstring instruction_set) {
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100108 Thread* thread = reinterpret_cast<Thread*>(token);
109 // Our system thread ID, etc, has changed so reset Thread state.
110 thread->InitAfterFork();
111 EnableDebugFeatures(debug_flags);
Andreas Gampe13e3c6d2014-09-02 21:22:18 -0700112
113 Runtime::NativeBridgeAction action = Runtime::NativeBridgeAction::kUnload;
114 if (instruction_set != nullptr) {
115 ScopedUtfChars isa_string(env, instruction_set);
116 InstructionSet isa = GetInstructionSetFromString(isa_string.c_str());
117 if (isa != kNone && isa != kRuntimeISA) {
118 action = Runtime::NativeBridgeAction::kInitialize;
119 }
120 }
121 Runtime::Current()->DidForkFromZygote(action);
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100122}
123
124static JNINativeMethod gMethods[] = {
125 NATIVE_METHOD(ZygoteHooks, nativePreFork, "()J"),
Andreas Gampe13e3c6d2014-09-02 21:22:18 -0700126 NATIVE_METHOD(ZygoteHooks, nativePostForkChild, "(JILjava/lang/String;)V"),
Narayan Kamath8b2c8b92014-03-31 16:44:54 +0100127};
128
129void register_dalvik_system_ZygoteHooks(JNIEnv* env) {
130 REGISTER_NATIVE_METHODS("dalvik/system/ZygoteHooks");
131}
132
133} // namespace art