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 | |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 17 | #include "dalvik_system_ZygoteHooks.h" |
| 18 | |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 19 | #include <stdlib.h> |
| 20 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame^] | 21 | #include <android-base/logging.h> |
| 22 | #include <android-base/stringprintf.h> |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 23 | |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 24 | #include "arch/instruction_set.h" |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 25 | #include "art_method-inl.h" |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame^] | 26 | #include "base/macros.h" |
| 27 | #include "base/mutex.h" |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 28 | #include "debugger.h" |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 29 | #include "java_vm_ext.h" |
Mathieu Chartier | 455f67c | 2015-03-17 13:48:29 -0700 | [diff] [blame] | 30 | #include "jit/jit.h" |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 31 | #include "jni_internal.h" |
Andreas Gampe | 87583b3 | 2017-05-25 11:22:18 -0700 | [diff] [blame] | 32 | #include "native_util.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 33 | #include "nativehelper/jni_macros.h" |
Andreas Gampe | 373a9b5 | 2017-10-18 09:01:57 -0700 | [diff] [blame] | 34 | #include "nativehelper/scoped_utf_chars.h" |
Alex Light | e77b48b | 2017-02-22 11:08:06 -0800 | [diff] [blame] | 35 | #include "non_debuggable_classes.h" |
Nicolas Geoffray | 68bf390 | 2017-09-07 14:40:48 +0100 | [diff] [blame] | 36 | #include "oat_file.h" |
| 37 | #include "oat_file_manager.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 38 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | 513061a | 2017-06-01 09:17:34 -0700 | [diff] [blame] | 39 | #include "stack.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 40 | #include "thread-current-inl.h" |
Alex Light | e77b48b | 2017-02-22 11:08:06 -0800 | [diff] [blame] | 41 | #include "thread_list.h" |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 42 | #include "trace.h" |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 43 | |
Elliott Hughes | 0a18df8 | 2015-01-09 15:16:16 -0800 | [diff] [blame] | 44 | #if defined(__linux__) |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 45 | #include <sys/prctl.h> |
| 46 | #endif |
| 47 | |
Narayan Kamath | ad4b0d2 | 2014-04-02 12:06:02 +0100 | [diff] [blame] | 48 | #include <sys/resource.h> |
| 49 | |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 50 | namespace art { |
| 51 | |
Alex Light | e77b48b | 2017-02-22 11:08:06 -0800 | [diff] [blame] | 52 | // Set to true to always determine the non-debuggable classes even if we would not allow a debugger |
| 53 | // to actually attach. |
Andreas Gampe | dbe5491 | 2017-10-19 13:02:03 -0700 | [diff] [blame] | 54 | static bool kAlwaysCollectNonDebuggableClasses = |
| 55 | RegisterRuntimeDebugFlag(&kAlwaysCollectNonDebuggableClasses); |
Alex Light | e77b48b | 2017-02-22 11:08:06 -0800 | [diff] [blame] | 56 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 57 | using android::base::StringPrintf; |
| 58 | |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 59 | static void EnableDebugger() { |
Elliott Hughes | 0a18df8 | 2015-01-09 15:16:16 -0800 | [diff] [blame] | 60 | #if defined(__linux__) |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 61 | // To let a non-privileged gdbserver attach to this |
| 62 | // process, we must set our dumpable flag. |
| 63 | if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) { |
| 64 | PLOG(ERROR) << "prctl(PR_SET_DUMPABLE) failed for pid " << getpid(); |
| 65 | } |
Oleksiy Vyalov | de9007f | 2016-06-21 16:21:37 -0700 | [diff] [blame] | 66 | |
| 67 | // Even if Yama is on a non-privileged native debugger should |
| 68 | // be able to attach to the debuggable app. |
| 69 | if (prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY, 0, 0, 0) == -1) { |
| 70 | // if Yama is off prctl(PR_SET_PTRACER) returns EINVAL - don't log in this |
| 71 | // case since it's expected behaviour. |
| 72 | if (errno != EINVAL) { |
| 73 | PLOG(ERROR) << "prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY) failed for pid " << getpid(); |
| 74 | } |
| 75 | } |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame] | 76 | #endif |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 77 | // We don't want core dumps, though, so set the core dump size to 0. |
| 78 | rlimit rl; |
| 79 | rl.rlim_cur = 0; |
| 80 | rl.rlim_max = RLIM_INFINITY; |
| 81 | if (setrlimit(RLIMIT_CORE, &rl) == -1) { |
| 82 | PLOG(ERROR) << "setrlimit(RLIMIT_CORE) failed for pid " << getpid(); |
| 83 | } |
| 84 | } |
| 85 | |
Alex Light | 861af89 | 2017-02-28 12:45:36 -0800 | [diff] [blame] | 86 | class ClassSet { |
| 87 | public: |
Alex Light | 8474026 | 2017-03-01 09:00:28 -0800 | [diff] [blame] | 88 | // The number of classes we reasonably expect to have to look at. Realistically the number is more |
| 89 | // ~10 but there is little harm in having some extra. |
| 90 | static constexpr int kClassSetCapacity = 100; |
Alex Light | 861af89 | 2017-02-28 12:45:36 -0800 | [diff] [blame] | 91 | |
Alex Light | 8474026 | 2017-03-01 09:00:28 -0800 | [diff] [blame] | 92 | explicit ClassSet(Thread* const self) : self_(self) { |
| 93 | self_->GetJniEnv()->PushFrame(kClassSetCapacity); |
Alex Light | 861af89 | 2017-02-28 12:45:36 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Alex Light | 8474026 | 2017-03-01 09:00:28 -0800 | [diff] [blame] | 96 | ~ClassSet() { |
| 97 | self_->GetJniEnv()->PopFrame(); |
| 98 | } |
| 99 | |
| 100 | void AddClass(ObjPtr<mirror::Class> klass) REQUIRES(Locks::mutator_lock_) { |
| 101 | class_set_.insert(self_->GetJniEnv()->AddLocalReference<jclass>(klass.Ptr())); |
| 102 | } |
| 103 | |
| 104 | const std::unordered_set<jclass>& GetClasses() const { |
Alex Light | 861af89 | 2017-02-28 12:45:36 -0800 | [diff] [blame] | 105 | return class_set_; |
| 106 | } |
| 107 | |
| 108 | private: |
Alex Light | 8474026 | 2017-03-01 09:00:28 -0800 | [diff] [blame] | 109 | Thread* const self_; |
| 110 | std::unordered_set<jclass> class_set_; |
Alex Light | 861af89 | 2017-02-28 12:45:36 -0800 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | static void DoCollectNonDebuggableCallback(Thread* thread, void* data) |
Alex Light | e77b48b | 2017-02-22 11:08:06 -0800 | [diff] [blame] | 114 | REQUIRES(Locks::mutator_lock_) { |
| 115 | class NonDebuggableStacksVisitor : public StackVisitor { |
| 116 | public: |
Alex Light | 861af89 | 2017-02-28 12:45:36 -0800 | [diff] [blame] | 117 | NonDebuggableStacksVisitor(Thread* t, ClassSet* class_set) |
| 118 | : StackVisitor(t, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 119 | class_set_(class_set) {} |
Alex Light | e77b48b | 2017-02-22 11:08:06 -0800 | [diff] [blame] | 120 | |
| 121 | ~NonDebuggableStacksVisitor() OVERRIDE {} |
| 122 | |
| 123 | bool VisitFrame() OVERRIDE REQUIRES(Locks::mutator_lock_) { |
| 124 | if (GetMethod()->IsRuntimeMethod()) { |
| 125 | return true; |
| 126 | } |
Alex Light | 861af89 | 2017-02-28 12:45:36 -0800 | [diff] [blame] | 127 | class_set_->AddClass(GetMethod()->GetDeclaringClass()); |
Alex Light | e77b48b | 2017-02-22 11:08:06 -0800 | [diff] [blame] | 128 | if (kIsDebugBuild) { |
| 129 | LOG(INFO) << GetMethod()->GetDeclaringClass()->PrettyClass() |
| 130 | << " might not be fully debuggable/deoptimizable due to " |
| 131 | << GetMethod()->PrettyMethod() << " appearing on the stack during zygote fork."; |
| 132 | } |
| 133 | return true; |
| 134 | } |
Alex Light | 861af89 | 2017-02-28 12:45:36 -0800 | [diff] [blame] | 135 | |
| 136 | private: |
| 137 | ClassSet* class_set_; |
Alex Light | e77b48b | 2017-02-22 11:08:06 -0800 | [diff] [blame] | 138 | }; |
Alex Light | 861af89 | 2017-02-28 12:45:36 -0800 | [diff] [blame] | 139 | NonDebuggableStacksVisitor visitor(thread, reinterpret_cast<ClassSet*>(data)); |
Alex Light | e77b48b | 2017-02-22 11:08:06 -0800 | [diff] [blame] | 140 | visitor.WalkStack(); |
| 141 | } |
| 142 | |
Alex Light | 861af89 | 2017-02-28 12:45:36 -0800 | [diff] [blame] | 143 | static void CollectNonDebuggableClasses() REQUIRES(!Locks::mutator_lock_) { |
Alex Light | e77b48b | 2017-02-22 11:08:06 -0800 | [diff] [blame] | 144 | Runtime* const runtime = Runtime::Current(); |
Alex Light | 861af89 | 2017-02-28 12:45:36 -0800 | [diff] [blame] | 145 | Thread* const self = Thread::Current(); |
| 146 | // Get the mutator lock. |
| 147 | ScopedObjectAccess soa(self); |
| 148 | ClassSet classes(self); |
| 149 | { |
Alex Light | 8474026 | 2017-03-01 09:00:28 -0800 | [diff] [blame] | 150 | // Drop the shared mutator lock. |
| 151 | ScopedThreadSuspension sts(self, art::ThreadState::kNative); |
| 152 | // Get exclusive mutator lock with suspend all. |
| 153 | ScopedSuspendAll suspend("Checking stacks for non-obsoletable methods!", /*long_suspend*/false); |
| 154 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
| 155 | runtime->GetThreadList()->ForEach(DoCollectNonDebuggableCallback, &classes); |
Alex Light | 861af89 | 2017-02-28 12:45:36 -0800 | [diff] [blame] | 156 | } |
Alex Light | 8474026 | 2017-03-01 09:00:28 -0800 | [diff] [blame] | 157 | for (jclass klass : classes.GetClasses()) { |
| 158 | NonDebuggableClasses::AddNonDebuggableClass(klass); |
Alex Light | 861af89 | 2017-02-28 12:45:36 -0800 | [diff] [blame] | 159 | } |
Alex Light | e77b48b | 2017-02-22 11:08:06 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Nicolas Geoffray | 68bf390 | 2017-09-07 14:40:48 +0100 | [diff] [blame] | 162 | // Must match values in com.android.internal.os.Zygote. |
| 163 | enum { |
| 164 | DEBUG_ENABLE_JDWP = 1, |
| 165 | DEBUG_ENABLE_CHECKJNI = 1 << 1, |
| 166 | DEBUG_ENABLE_ASSERT = 1 << 2, |
| 167 | DEBUG_ENABLE_SAFEMODE = 1 << 3, |
| 168 | DEBUG_ENABLE_JNI_LOGGING = 1 << 4, |
| 169 | DEBUG_GENERATE_DEBUG_INFO = 1 << 5, |
| 170 | DEBUG_ALWAYS_JIT = 1 << 6, |
| 171 | DEBUG_NATIVE_DEBUGGABLE = 1 << 7, |
| 172 | DEBUG_JAVA_DEBUGGABLE = 1 << 8, |
| 173 | DISABLE_VERIFIER = 1 << 9, |
| 174 | ONLY_USE_SYSTEM_OAT_FILES = 1 << 10, |
| 175 | }; |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 176 | |
Nicolas Geoffray | 68bf390 | 2017-09-07 14:40:48 +0100 | [diff] [blame] | 177 | static uint32_t EnableDebugFeatures(uint32_t runtime_flags) { |
Mathieu Chartier | 6eff38d | 2015-03-17 09:52:22 -0700 | [diff] [blame] | 178 | Runtime* const runtime = Runtime::Current(); |
Nicolas Geoffray | 5510c0a | 2017-09-12 15:11:37 +0100 | [diff] [blame] | 179 | if ((runtime_flags & DEBUG_ENABLE_CHECKJNI) != 0) { |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 180 | JavaVMExt* vm = runtime->GetJavaVM(); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 181 | if (!vm->IsCheckJniEnabled()) { |
Brian Carlstrom | 966ce11 | 2014-05-12 17:30:36 -0700 | [diff] [blame] | 182 | LOG(INFO) << "Late-enabling -Xcheck:jni"; |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 183 | vm->SetCheckJniEnabled(true); |
| 184 | // There's only one thread running at this point, so only one JNIEnv to fix up. |
| 185 | Thread::Current()->GetJniEnv()->SetCheckJniEnabled(true); |
| 186 | } else { |
Brian Carlstrom | 966ce11 | 2014-05-12 17:30:36 -0700 | [diff] [blame] | 187 | LOG(INFO) << "Not late-enabling -Xcheck:jni (already on)"; |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 188 | } |
Nicolas Geoffray | 5510c0a | 2017-09-12 15:11:37 +0100 | [diff] [blame] | 189 | runtime_flags &= ~DEBUG_ENABLE_CHECKJNI; |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 190 | } |
| 191 | |
Nicolas Geoffray | 5510c0a | 2017-09-12 15:11:37 +0100 | [diff] [blame] | 192 | if ((runtime_flags & DEBUG_ENABLE_JNI_LOGGING) != 0) { |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 193 | gLogVerbosity.third_party_jni = true; |
Nicolas Geoffray | 5510c0a | 2017-09-12 15:11:37 +0100 | [diff] [blame] | 194 | runtime_flags &= ~DEBUG_ENABLE_JNI_LOGGING; |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 195 | } |
| 196 | |
Nicolas Geoffray | 5510c0a | 2017-09-12 15:11:37 +0100 | [diff] [blame] | 197 | Dbg::SetJdwpAllowed((runtime_flags & DEBUG_ENABLE_JDWP) != 0); |
| 198 | if ((runtime_flags & DEBUG_ENABLE_JDWP) != 0) { |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 199 | EnableDebugger(); |
| 200 | } |
Nicolas Geoffray | 5510c0a | 2017-09-12 15:11:37 +0100 | [diff] [blame] | 201 | runtime_flags &= ~DEBUG_ENABLE_JDWP; |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 202 | |
Nicolas Geoffray | 5510c0a | 2017-09-12 15:11:37 +0100 | [diff] [blame] | 203 | const bool safe_mode = (runtime_flags & DEBUG_ENABLE_SAFEMODE) != 0; |
Mathieu Chartier | 6eff38d | 2015-03-17 09:52:22 -0700 | [diff] [blame] | 204 | if (safe_mode) { |
Nicolas Geoffray | 49cda06 | 2017-04-21 13:08:25 +0100 | [diff] [blame] | 205 | // Only quicken oat files. |
| 206 | runtime->AddCompilerOption("--compiler-filter=quicken"); |
Nicolas Geoffray | 0f042e0 | 2015-11-05 11:32:24 +0000 | [diff] [blame] | 207 | runtime->SetSafeMode(true); |
Nicolas Geoffray | 5510c0a | 2017-09-12 15:11:37 +0100 | [diff] [blame] | 208 | runtime_flags &= ~DEBUG_ENABLE_SAFEMODE; |
Andreas Gampe | d2abbc9 | 2014-12-19 09:53:27 -0800 | [diff] [blame] | 209 | } |
| 210 | |
Nicolas Geoffray | 5510c0a | 2017-09-12 15:11:37 +0100 | [diff] [blame] | 211 | const bool generate_debug_info = (runtime_flags & DEBUG_GENERATE_DEBUG_INFO) != 0; |
David Srbecky | 8363c77 | 2015-05-28 16:12:43 +0100 | [diff] [blame] | 212 | if (generate_debug_info) { |
| 213 | runtime->AddCompilerOption("--generate-debug-info"); |
Nicolas Geoffray | 5510c0a | 2017-09-12 15:11:37 +0100 | [diff] [blame] | 214 | runtime_flags &= ~DEBUG_GENERATE_DEBUG_INFO; |
Andreas Gampe | 00bb878 | 2015-04-24 16:33:43 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Andreas Gampe | d2abbc9 | 2014-12-19 09:53:27 -0800 | [diff] [blame] | 217 | // This is for backwards compatibility with Dalvik. |
Nicolas Geoffray | 5510c0a | 2017-09-12 15:11:37 +0100 | [diff] [blame] | 218 | runtime_flags &= ~DEBUG_ENABLE_ASSERT; |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 219 | |
Nicolas Geoffray | 5510c0a | 2017-09-12 15:11:37 +0100 | [diff] [blame] | 220 | if ((runtime_flags & DEBUG_ALWAYS_JIT) != 0) { |
Siva Chandra | 05d2415 | 2016-01-05 17:43:17 -0800 | [diff] [blame] | 221 | jit::JitOptions* jit_options = runtime->GetJITOptions(); |
| 222 | CHECK(jit_options != nullptr); |
| 223 | jit_options->SetJitAtFirstUse(); |
Nicolas Geoffray | 5510c0a | 2017-09-12 15:11:37 +0100 | [diff] [blame] | 224 | runtime_flags &= ~DEBUG_ALWAYS_JIT; |
Siva Chandra | 05d2415 | 2016-01-05 17:43:17 -0800 | [diff] [blame] | 225 | } |
| 226 | |
Alex Light | e77b48b | 2017-02-22 11:08:06 -0800 | [diff] [blame] | 227 | bool needs_non_debuggable_classes = false; |
Nicolas Geoffray | 5510c0a | 2017-09-12 15:11:37 +0100 | [diff] [blame] | 228 | if ((runtime_flags & DEBUG_JAVA_DEBUGGABLE) != 0) { |
Nicolas Geoffray | 433b79a | 2017-01-30 20:54:45 +0000 | [diff] [blame] | 229 | runtime->AddCompilerOption("--debuggable"); |
| 230 | runtime->SetJavaDebuggable(true); |
| 231 | // Deoptimize the boot image as it may be non-debuggable. |
| 232 | runtime->DeoptimizeBootImage(); |
Nicolas Geoffray | 5510c0a | 2017-09-12 15:11:37 +0100 | [diff] [blame] | 233 | runtime_flags &= ~DEBUG_JAVA_DEBUGGABLE; |
Alex Light | e77b48b | 2017-02-22 11:08:06 -0800 | [diff] [blame] | 234 | needs_non_debuggable_classes = true; |
| 235 | } |
| 236 | if (needs_non_debuggable_classes || kAlwaysCollectNonDebuggableClasses) { |
| 237 | CollectNonDebuggableClasses(); |
Nicolas Geoffray | 433b79a | 2017-01-30 20:54:45 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Nicolas Geoffray | 5510c0a | 2017-09-12 15:11:37 +0100 | [diff] [blame] | 240 | if ((runtime_flags & DEBUG_NATIVE_DEBUGGABLE) != 0) { |
David Srbecky | 346dc99 | 2016-03-13 22:00:07 +0000 | [diff] [blame] | 241 | runtime->AddCompilerOption("--debuggable"); |
| 242 | runtime->AddCompilerOption("--generate-debug-info"); |
David Srbecky | f448016 | 2016-03-16 00:06:24 +0000 | [diff] [blame] | 243 | runtime->SetNativeDebuggable(true); |
Nicolas Geoffray | 5510c0a | 2017-09-12 15:11:37 +0100 | [diff] [blame] | 244 | runtime_flags &= ~DEBUG_NATIVE_DEBUGGABLE; |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Nicolas Geoffray | 68bf390 | 2017-09-07 14:40:48 +0100 | [diff] [blame] | 247 | return runtime_flags; |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | static jlong ZygoteHooks_nativePreFork(JNIEnv* env, jclass) { |
| 251 | Runtime* runtime = Runtime::Current(); |
| 252 | CHECK(runtime->IsZygote()) << "runtime instance not started with -Xzygote"; |
Narayan Kamath | 3de95a7 | 2014-04-02 12:54:23 +0100 | [diff] [blame] | 253 | |
| 254 | runtime->PreZygoteFork(); |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 255 | |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 256 | if (Trace::GetMethodTracingMode() != TracingMode::kTracingInactive) { |
| 257 | // Tracing active, pause it. |
| 258 | Trace::Pause(); |
| 259 | } |
| 260 | |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 261 | // Grab thread before fork potentially makes Thread::pthread_key_self_ unusable. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 262 | return reinterpret_cast<jlong>(ThreadForEnv(env)); |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 263 | } |
| 264 | |
Nicolas Geoffray | d66c862 | 2015-12-11 14:59:16 +0000 | [diff] [blame] | 265 | static void ZygoteHooks_nativePostForkChild(JNIEnv* env, |
| 266 | jclass, |
| 267 | jlong token, |
Nicolas Geoffray | 5510c0a | 2017-09-12 15:11:37 +0100 | [diff] [blame] | 268 | jint runtime_flags, |
Nicolas Geoffray | d66c862 | 2015-12-11 14:59:16 +0000 | [diff] [blame] | 269 | jboolean is_system_server, |
Andreas Gampe | 6be67ee | 2014-09-02 21:22:18 -0700 | [diff] [blame] | 270 | jstring instruction_set) { |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 271 | Thread* thread = reinterpret_cast<Thread*>(token); |
| 272 | // Our system thread ID, etc, has changed so reset Thread state. |
| 273 | thread->InitAfterFork(); |
Nicolas Geoffray | 68bf390 | 2017-09-07 14:40:48 +0100 | [diff] [blame] | 274 | runtime_flags = EnableDebugFeatures(runtime_flags); |
| 275 | |
| 276 | if ((runtime_flags & DISABLE_VERIFIER) != 0) { |
| 277 | Runtime::Current()->DisableVerifier(); |
| 278 | runtime_flags &= ~DISABLE_VERIFIER; |
| 279 | } |
| 280 | |
| 281 | if ((runtime_flags & ONLY_USE_SYSTEM_OAT_FILES) != 0) { |
| 282 | Runtime::Current()->GetOatFileManager().SetOnlyUseSystemOatFiles(); |
| 283 | runtime_flags &= ~ONLY_USE_SYSTEM_OAT_FILES; |
| 284 | } |
| 285 | |
| 286 | if (runtime_flags != 0) { |
| 287 | LOG(ERROR) << StringPrintf("Unknown bits set in runtime_flags: %#x", runtime_flags); |
| 288 | } |
Andreas Gampe | 6be67ee | 2014-09-02 21:22:18 -0700 | [diff] [blame] | 289 | |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 290 | // Update tracing. |
| 291 | if (Trace::GetMethodTracingMode() != TracingMode::kTracingInactive) { |
| 292 | Trace::TraceOutputMode output_mode = Trace::GetOutputMode(); |
| 293 | Trace::TraceMode trace_mode = Trace::GetMode(); |
Andreas Gampe | e34a42c | 2015-04-25 14:44:29 -0700 | [diff] [blame] | 294 | size_t buffer_size = Trace::GetBufferSize(); |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 295 | |
| 296 | // Just drop it. |
| 297 | Trace::Abort(); |
| 298 | |
| 299 | // Only restart if it was streaming mode. |
| 300 | // TODO: Expose buffer size, so we can also do file mode. |
| 301 | if (output_mode == Trace::TraceOutputMode::kStreaming) { |
Dmitriy Filchenko | 03c0134 | 2016-07-11 17:41:28 -0700 | [diff] [blame] | 302 | static constexpr size_t kMaxProcessNameLength = 100; |
| 303 | char name_buf[kMaxProcessNameLength] = {}; |
| 304 | int rc = pthread_getname_np(pthread_self(), name_buf, kMaxProcessNameLength); |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 305 | std::string proc_name; |
Dmitriy Filchenko | 03c0134 | 2016-07-11 17:41:28 -0700 | [diff] [blame] | 306 | |
| 307 | if (rc == 0) { |
| 308 | // On success use the pthread name. |
| 309 | proc_name = name_buf; |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 310 | } |
Dmitriy Filchenko | 03c0134 | 2016-07-11 17:41:28 -0700 | [diff] [blame] | 311 | |
| 312 | if (proc_name.empty() || proc_name == "zygote" || proc_name == "zygote64") { |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 313 | // Either no process name, or the name hasn't been changed, yet. Just use pid. |
| 314 | pid_t pid = getpid(); |
| 315 | proc_name = StringPrintf("%u", static_cast<uint32_t>(pid)); |
| 316 | } |
| 317 | |
Calin Juravle | f83e733 | 2015-11-04 16:16:47 +0000 | [diff] [blame] | 318 | std::string trace_file = StringPrintf("/data/misc/trace/%s.trace.bin", proc_name.c_str()); |
| 319 | Trace::Start(trace_file.c_str(), |
| 320 | -1, |
| 321 | buffer_size, |
| 322 | 0, // TODO: Expose flags. |
| 323 | output_mode, |
| 324 | trace_mode, |
| 325 | 0); // TODO: Expose interval. |
| 326 | if (thread->IsExceptionPending()) { |
| 327 | ScopedObjectAccess soa(env); |
| 328 | thread->ClearException(); |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
Nicolas Geoffray | d66c862 | 2015-12-11 14:59:16 +0000 | [diff] [blame] | 333 | if (instruction_set != nullptr && !is_system_server) { |
Andreas Gampe | 6be67ee | 2014-09-02 21:22:18 -0700 | [diff] [blame] | 334 | ScopedUtfChars isa_string(env, instruction_set); |
| 335 | InstructionSet isa = GetInstructionSetFromString(isa_string.c_str()); |
jgu21 | a6da74e | 2014-09-10 06:57:17 -0400 | [diff] [blame] | 336 | Runtime::NativeBridgeAction action = Runtime::NativeBridgeAction::kUnload; |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 337 | if (isa != InstructionSet::kNone && isa != kRuntimeISA) { |
Andreas Gampe | 6be67ee | 2014-09-02 21:22:18 -0700 | [diff] [blame] | 338 | action = Runtime::NativeBridgeAction::kInitialize; |
| 339 | } |
Nicolas Geoffray | d66c862 | 2015-12-11 14:59:16 +0000 | [diff] [blame] | 340 | Runtime::Current()->InitNonZygoteOrPostFork( |
| 341 | env, is_system_server, action, isa_string.c_str()); |
jgu21 | a6da74e | 2014-09-10 06:57:17 -0400 | [diff] [blame] | 342 | } else { |
Nicolas Geoffray | d66c862 | 2015-12-11 14:59:16 +0000 | [diff] [blame] | 343 | Runtime::Current()->InitNonZygoteOrPostFork( |
| 344 | env, is_system_server, Runtime::NativeBridgeAction::kUnload, nullptr); |
Andreas Gampe | 6be67ee | 2014-09-02 21:22:18 -0700 | [diff] [blame] | 345 | } |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 346 | } |
| 347 | |
Andreas Gampe | f38a661 | 2016-04-11 08:42:26 -0700 | [diff] [blame] | 348 | static void ZygoteHooks_startZygoteNoThreadCreation(JNIEnv* env ATTRIBUTE_UNUSED, |
| 349 | jclass klass ATTRIBUTE_UNUSED) { |
| 350 | Runtime::Current()->SetZygoteNoThreadSection(true); |
| 351 | } |
| 352 | |
| 353 | static void ZygoteHooks_stopZygoteNoThreadCreation(JNIEnv* env ATTRIBUTE_UNUSED, |
| 354 | jclass klass ATTRIBUTE_UNUSED) { |
| 355 | Runtime::Current()->SetZygoteNoThreadSection(false); |
| 356 | } |
| 357 | |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 358 | static JNINativeMethod gMethods[] = { |
| 359 | NATIVE_METHOD(ZygoteHooks, nativePreFork, "()J"), |
Nicolas Geoffray | d66c862 | 2015-12-11 14:59:16 +0000 | [diff] [blame] | 360 | NATIVE_METHOD(ZygoteHooks, nativePostForkChild, "(JIZLjava/lang/String;)V"), |
Andreas Gampe | f38a661 | 2016-04-11 08:42:26 -0700 | [diff] [blame] | 361 | NATIVE_METHOD(ZygoteHooks, startZygoteNoThreadCreation, "()V"), |
| 362 | NATIVE_METHOD(ZygoteHooks, stopZygoteNoThreadCreation, "()V"), |
Narayan Kamath | 8b2c8b9 | 2014-03-31 16:44:54 +0100 | [diff] [blame] | 363 | }; |
| 364 | |
| 365 | void register_dalvik_system_ZygoteHooks(JNIEnv* env) { |
| 366 | REGISTER_NATIVE_METHODS("dalvik/system/ZygoteHooks"); |
| 367 | } |
| 368 | |
| 369 | } // namespace art |