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" |
| 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 Kamath | ad4b0d2 | 2014-04-02 12:06:02 +0100 | [diff] [blame] | 28 | #include <sys/resource.h> |
| 29 | |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 30 | namespace art { |
| 31 | |
| 32 | static 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 | |
| 47 | static 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) { |
Brian Carlstrom | 966ce11 | 2014-05-12 17:30:36 -0700 | [diff] [blame^] | 61 | LOG(INFO) << "Late-enabling -Xcheck:jni"; |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 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 { |
Brian Carlstrom | 966ce11 | 2014-05-12 17:30:36 -0700 | [diff] [blame^] | 66 | LOG(INFO) << "Not late-enabling -Xcheck:jni (already on)"; |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 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 | |
| 91 | static jlong ZygoteHooks_nativePreFork(JNIEnv* env, jclass) { |
| 92 | Runtime* runtime = Runtime::Current(); |
| 93 | CHECK(runtime->IsZygote()) << "runtime instance not started with -Xzygote"; |
Narayan Kamath | 3de95a7 | 2014-04-02 12:54:23 +0100 | [diff] [blame] | 94 | |
| 95 | runtime->PreZygoteFork(); |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 96 | |
| 97 | // Grab thread before fork potentially makes Thread::pthread_key_self_ unusable. |
| 98 | Thread* self = Thread::Current(); |
| 99 | return reinterpret_cast<jlong>(self); |
| 100 | } |
| 101 | |
| 102 | static void ZygoteHooks_nativePostForkChild(JNIEnv* env, jclass, jlong token, jint debug_flags) { |
| 103 | Thread* thread = reinterpret_cast<Thread*>(token); |
| 104 | // Our system thread ID, etc, has changed so reset Thread state. |
| 105 | thread->InitAfterFork(); |
| 106 | EnableDebugFeatures(debug_flags); |
| 107 | Runtime::Current()->DidForkFromZygote(); |
| 108 | } |
| 109 | |
| 110 | static JNINativeMethod gMethods[] = { |
| 111 | NATIVE_METHOD(ZygoteHooks, nativePreFork, "()J"), |
| 112 | NATIVE_METHOD(ZygoteHooks, nativePostForkChild, "(JI)V"), |
| 113 | }; |
| 114 | |
| 115 | void register_dalvik_system_ZygoteHooks(JNIEnv* env) { |
| 116 | REGISTER_NATIVE_METHODS("dalvik/system/ZygoteHooks"); |
| 117 | } |
| 118 | |
| 119 | } // namespace art |