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