Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 1 | /* |
| 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 Gampe | 6be67ee | 2014-09-02 21:22:18 -0700 | [diff] [blame] | 20 | #include "instruction_set.h" |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 21 | #include "java_vm_ext.h" |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 22 | #include "jni_internal.h" |
| 23 | #include "JNIHelp.h" |
Andreas Gampe | 6be67ee | 2014-09-02 21:22:18 -0700 | [diff] [blame] | 24 | #include "ScopedUtfChars.h" |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 25 | #include "thread-inl.h" |
| 26 | |
| 27 | #if defined(HAVE_PRCTL) |
| 28 | #include <sys/prctl.h> |
| 29 | #endif |
| 30 | |
Narayan Kamath | ad4b0d2 | 2014-04-02 12:06:02 +0100 | [diff] [blame] | 31 | #include <sys/resource.h> |
| 32 | |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 33 | namespace art { |
| 34 | |
| 35 | static void EnableDebugger() { |
| 36 | // To let a non-privileged gdbserver attach to this |
| 37 | // process, we must set our dumpable flag. |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame] | 38 | #if defined(HAVE_PRCTL) |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 39 | if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) { |
| 40 | PLOG(ERROR) << "prctl(PR_SET_DUMPABLE) failed for pid " << getpid(); |
| 41 | } |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame] | 42 | #endif |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 43 | // We don't want core dumps, though, so set the core dump size to 0. |
| 44 | rlimit rl; |
| 45 | rl.rlim_cur = 0; |
| 46 | rl.rlim_max = RLIM_INFINITY; |
| 47 | if (setrlimit(RLIMIT_CORE, &rl) == -1) { |
| 48 | PLOG(ERROR) << "setrlimit(RLIMIT_CORE) failed for pid " << getpid(); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | static void EnableDebugFeatures(uint32_t debug_flags) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 53 | // Must match values in com.android.internal.os.Zygote. |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 54 | enum { |
| 55 | DEBUG_ENABLE_DEBUGGER = 1, |
| 56 | DEBUG_ENABLE_CHECKJNI = 1 << 1, |
| 57 | DEBUG_ENABLE_ASSERT = 1 << 2, |
| 58 | DEBUG_ENABLE_SAFEMODE = 1 << 3, |
| 59 | DEBUG_ENABLE_JNI_LOGGING = 1 << 4, |
| 60 | }; |
| 61 | |
| 62 | if ((debug_flags & DEBUG_ENABLE_CHECKJNI) != 0) { |
| 63 | Runtime* runtime = Runtime::Current(); |
| 64 | JavaVMExt* vm = runtime->GetJavaVM(); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 65 | if (!vm->IsCheckJniEnabled()) { |
Brian Carlstrom | 966ce11 | 2014-05-12 17:30:36 -0700 | [diff] [blame] | 66 | LOG(INFO) << "Late-enabling -Xcheck:jni"; |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 67 | vm->SetCheckJniEnabled(true); |
| 68 | // There's only one thread running at this point, so only one JNIEnv to fix up. |
| 69 | Thread::Current()->GetJniEnv()->SetCheckJniEnabled(true); |
| 70 | } else { |
Brian Carlstrom | 966ce11 | 2014-05-12 17:30:36 -0700 | [diff] [blame] | 71 | LOG(INFO) << "Not late-enabling -Xcheck:jni (already on)"; |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 72 | } |
| 73 | debug_flags &= ~DEBUG_ENABLE_CHECKJNI; |
| 74 | } |
| 75 | |
| 76 | if ((debug_flags & DEBUG_ENABLE_JNI_LOGGING) != 0) { |
| 77 | gLogVerbosity.third_party_jni = true; |
| 78 | debug_flags &= ~DEBUG_ENABLE_JNI_LOGGING; |
| 79 | } |
| 80 | |
| 81 | Dbg::SetJdwpAllowed((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0); |
| 82 | if ((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0) { |
| 83 | EnableDebugger(); |
| 84 | } |
| 85 | debug_flags &= ~DEBUG_ENABLE_DEBUGGER; |
| 86 | |
| 87 | // These two are for backwards compatibility with Dalvik. |
| 88 | debug_flags &= ~DEBUG_ENABLE_ASSERT; |
| 89 | debug_flags &= ~DEBUG_ENABLE_SAFEMODE; |
| 90 | |
| 91 | if (debug_flags != 0) { |
| 92 | LOG(ERROR) << StringPrintf("Unknown bits set in debug_flags: %#x", debug_flags); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | static jlong ZygoteHooks_nativePreFork(JNIEnv* env, jclass) { |
| 97 | Runtime* runtime = Runtime::Current(); |
| 98 | CHECK(runtime->IsZygote()) << "runtime instance not started with -Xzygote"; |
Narayan Kamath | 3de95a7 | 2014-04-02 12:54:23 +0100 | [diff] [blame] | 99 | |
| 100 | runtime->PreZygoteFork(); |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 101 | |
| 102 | // Grab thread before fork potentially makes Thread::pthread_key_self_ unusable. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 103 | return reinterpret_cast<jlong>(ThreadForEnv(env)); |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 104 | } |
| 105 | |
Andreas Gampe | 6be67ee | 2014-09-02 21:22:18 -0700 | [diff] [blame] | 106 | static void ZygoteHooks_nativePostForkChild(JNIEnv* env, jclass, jlong token, jint debug_flags, |
| 107 | jstring instruction_set) { |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 108 | 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 Gampe | 6be67ee | 2014-09-02 21:22:18 -0700 | [diff] [blame] | 112 | |
Andreas Gampe | 6be67ee | 2014-09-02 21:22:18 -0700 | [diff] [blame] | 113 | if (instruction_set != nullptr) { |
| 114 | ScopedUtfChars isa_string(env, instruction_set); |
| 115 | InstructionSet isa = GetInstructionSetFromString(isa_string.c_str()); |
jgu21 | a6da74e | 2014-09-10 06:57:17 -0400 | [diff] [blame] | 116 | Runtime::NativeBridgeAction action = Runtime::NativeBridgeAction::kUnload; |
Andreas Gampe | 6be67ee | 2014-09-02 21:22:18 -0700 | [diff] [blame] | 117 | if (isa != kNone && isa != kRuntimeISA) { |
| 118 | action = Runtime::NativeBridgeAction::kInitialize; |
| 119 | } |
jgu21 | a6da74e | 2014-09-10 06:57:17 -0400 | [diff] [blame] | 120 | Runtime::Current()->DidForkFromZygote(env, action, isa_string.c_str()); |
| 121 | } else { |
| 122 | Runtime::Current()->DidForkFromZygote(env, Runtime::NativeBridgeAction::kUnload, nullptr); |
Andreas Gampe | 6be67ee | 2014-09-02 21:22:18 -0700 | [diff] [blame] | 123 | } |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static JNINativeMethod gMethods[] = { |
| 127 | NATIVE_METHOD(ZygoteHooks, nativePreFork, "()J"), |
Andreas Gampe | 6be67ee | 2014-09-02 21:22:18 -0700 | [diff] [blame] | 128 | NATIVE_METHOD(ZygoteHooks, nativePostForkChild, "(JILjava/lang/String;)V"), |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | void register_dalvik_system_ZygoteHooks(JNIEnv* env) { |
| 132 | REGISTER_NATIVE_METHODS("dalvik/system/ZygoteHooks"); |
| 133 | } |
| 134 | |
| 135 | } // namespace art |