Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 1 | /* Copyright (C) 2016 The Android Open Source Project |
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 3 | * |
| 4 | * This file implements interfaces from the file jvmti.h. This implementation |
| 5 | * is licensed under the same terms as the file jvmti.h. The |
| 6 | * copyright and license information for the file jvmti.h follows. |
| 7 | * |
| 8 | * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. |
| 9 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 10 | * |
| 11 | * This code is free software; you can redistribute it and/or modify it |
| 12 | * under the terms of the GNU General Public License version 2 only, as |
| 13 | * published by the Free Software Foundation. Oracle designates this |
| 14 | * particular file as subject to the "Classpath" exception as provided |
| 15 | * by Oracle in the LICENSE file that accompanied this code. |
| 16 | * |
| 17 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 18 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 20 | * version 2 for more details (a copy is included in the LICENSE file that |
| 21 | * accompanied this code). |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License version |
| 24 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 25 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 26 | * |
| 27 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 28 | * or visit www.oracle.com if you need additional information or have any |
| 29 | * questions. |
| 30 | */ |
| 31 | |
| 32 | #include "ti_stack.h" |
| 33 | |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 34 | #include <algorithm> |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 35 | #include <initializer_list> |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 36 | #include <list> |
| 37 | #include <unordered_map> |
| 38 | #include <vector> |
| 39 | |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 40 | #include "android-base/macros.h" |
| 41 | #include "android-base/thread_annotations.h" |
Andreas Gampe | e5d2398 | 2019-01-08 10:34:26 -0800 | [diff] [blame] | 42 | #include "arch/context.h" |
Andreas Gampe | a1d2f95 | 2017-04-20 22:53:58 -0700 | [diff] [blame] | 43 | #include "art_field-inl.h" |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 44 | #include "art_method-inl.h" |
Andreas Gampe | a1d2f95 | 2017-04-20 22:53:58 -0700 | [diff] [blame] | 45 | #include "art_jvmti.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 46 | #include "art_method-inl.h" |
Andreas Gampe | 6237cd3 | 2017-06-22 22:17:38 -0700 | [diff] [blame] | 47 | #include "barrier.h" |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 48 | #include "base/bit_utils.h" |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 49 | #include "base/enums.h" |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 50 | #include "base/locks.h" |
| 51 | #include "base/macros.h" |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 52 | #include "base/mutex.h" |
Alex Light | a4cdd36 | 2019-04-18 09:17:10 -0700 | [diff] [blame] | 53 | #include "deopt_manager.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 54 | #include "dex/code_item_accessors-inl.h" |
| 55 | #include "dex/dex_file.h" |
| 56 | #include "dex/dex_file_annotations.h" |
| 57 | #include "dex/dex_file_types.h" |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 58 | #include "dex/dex_instruction-inl.h" |
| 59 | #include "dex/primitive.h" |
| 60 | #include "events.h" |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 61 | #include "gc_root.h" |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 62 | #include "handle_scope-inl.h" |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 63 | #include "instrumentation.h" |
| 64 | #include "interpreter/shadow_frame-inl.h" |
| 65 | #include "interpreter/shadow_frame.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 66 | #include "jni/jni_env_ext.h" |
| 67 | #include "jni/jni_internal.h" |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 68 | #include "jvalue-inl.h" |
| 69 | #include "jvalue.h" |
| 70 | #include "jvmti.h" |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 71 | #include "mirror/class.h" |
| 72 | #include "mirror/dex_cache.h" |
Andreas Gampe | 373a9b5 | 2017-10-18 09:01:57 -0700 | [diff] [blame] | 73 | #include "nativehelper/scoped_local_ref.h" |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 74 | #include "scoped_thread_state_change-inl.h" |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 75 | #include "scoped_thread_state_change.h" |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 76 | #include "stack.h" |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 77 | #include "thread.h" |
| 78 | #include "thread_state.h" |
Alex Light | ae45cbb | 2018-10-18 15:49:56 -0700 | [diff] [blame] | 79 | #include "ti_logging.h" |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 80 | #include "ti_thread.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 81 | #include "thread-current-inl.h" |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 82 | #include "thread_list.h" |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 83 | #include "thread_pool.h" |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 84 | #include "ti_thread.h" |
Andreas Gampe | a1d2f95 | 2017-04-20 22:53:58 -0700 | [diff] [blame] | 85 | #include "well_known_classes.h" |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 86 | |
| 87 | namespace openjdkjvmti { |
| 88 | |
Andreas Gampe | 6db6b4d | 2017-06-12 16:36:33 -0700 | [diff] [blame] | 89 | template <typename FrameFn> |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 90 | struct GetStackTraceVisitor : public art::StackVisitor { |
| 91 | GetStackTraceVisitor(art::Thread* thread_in, |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 92 | size_t start_, |
Andreas Gampe | 6db6b4d | 2017-06-12 16:36:33 -0700 | [diff] [blame] | 93 | size_t stop_, |
| 94 | FrameFn fn_) |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 95 | : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
Andreas Gampe | 6db6b4d | 2017-06-12 16:36:33 -0700 | [diff] [blame] | 96 | fn(fn_), |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 97 | start(start_), |
| 98 | stop(stop_) {} |
Andreas Gampe | 6db6b4d | 2017-06-12 16:36:33 -0700 | [diff] [blame] | 99 | GetStackTraceVisitor(const GetStackTraceVisitor&) = default; |
Andreas Gampe | 44b3174 | 2018-10-01 19:30:57 -0700 | [diff] [blame] | 100 | GetStackTraceVisitor(GetStackTraceVisitor&&) noexcept = default; |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 101 | |
Andreas Gampe | fa6a1b0 | 2018-09-07 08:11:55 -0700 | [diff] [blame] | 102 | bool VisitFrame() override REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 103 | art::ArtMethod* m = GetMethod(); |
| 104 | if (m->IsRuntimeMethod()) { |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | if (start == 0) { |
| 109 | m = m->GetInterfaceMethodIfProxy(art::kRuntimePointerSize); |
Andreas Gampe | 13b2784 | 2016-11-07 16:48:23 -0800 | [diff] [blame] | 110 | jmethodID id = art::jni::EncodeArtMethod(m); |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 111 | |
Andreas Gampe | 2340e3f | 2016-12-12 19:37:19 -0800 | [diff] [blame] | 112 | uint32_t dex_pc = GetDexPc(false); |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 113 | jlong dex_location = (dex_pc == art::dex::kDexNoIndex) ? -1 : static_cast<jlong>(dex_pc); |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 114 | |
Andreas Gampe | 2340e3f | 2016-12-12 19:37:19 -0800 | [diff] [blame] | 115 | jvmtiFrameInfo info = { id, dex_location }; |
Andreas Gampe | 6db6b4d | 2017-06-12 16:36:33 -0700 | [diff] [blame] | 116 | fn(info); |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 117 | |
| 118 | if (stop == 1) { |
| 119 | return false; // We're done. |
| 120 | } else if (stop > 0) { |
| 121 | stop--; |
| 122 | } |
| 123 | } else { |
| 124 | start--; |
| 125 | } |
| 126 | |
| 127 | return true; |
| 128 | } |
| 129 | |
Andreas Gampe | 6db6b4d | 2017-06-12 16:36:33 -0700 | [diff] [blame] | 130 | FrameFn fn; |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 131 | size_t start; |
| 132 | size_t stop; |
| 133 | }; |
| 134 | |
Alex Light | 0aa7a5a | 2018-10-10 15:58:14 +0000 | [diff] [blame] | 135 | art::ShadowFrame* FindFrameAtDepthVisitor::GetOrCreateShadowFrame(bool* created_frame) { |
| 136 | art::ShadowFrame* cur = GetCurrentShadowFrame(); |
| 137 | if (cur == nullptr) { |
| 138 | *created_frame = true; |
| 139 | art::ArtMethod* method = GetMethod(); |
| 140 | const uint16_t num_regs = method->DexInstructionData().RegistersSize(); |
| 141 | cur = GetThread()->FindOrCreateDebuggerShadowFrame(GetFrameId(), |
| 142 | num_regs, |
| 143 | method, |
| 144 | GetDexPc()); |
| 145 | DCHECK(cur != nullptr); |
| 146 | } else { |
| 147 | *created_frame = false; |
| 148 | } |
| 149 | return cur; |
| 150 | } |
| 151 | |
Andreas Gampe | 6db6b4d | 2017-06-12 16:36:33 -0700 | [diff] [blame] | 152 | template <typename FrameFn> |
| 153 | GetStackTraceVisitor<FrameFn> MakeStackTraceVisitor(art::Thread* thread_in, |
| 154 | size_t start, |
| 155 | size_t stop, |
| 156 | FrameFn fn) { |
| 157 | return GetStackTraceVisitor<FrameFn>(thread_in, start, stop, fn); |
| 158 | } |
| 159 | |
| 160 | struct GetStackTraceVectorClosure : public art::Closure { |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 161 | public: |
Andreas Gampe | 6db6b4d | 2017-06-12 16:36:33 -0700 | [diff] [blame] | 162 | GetStackTraceVectorClosure(size_t start, size_t stop) |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 163 | : start_input(start), |
| 164 | stop_input(stop), |
| 165 | start_result(0), |
| 166 | stop_result(0) {} |
| 167 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 168 | void Run(art::Thread* self) override REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Andreas Gampe | 6db6b4d | 2017-06-12 16:36:33 -0700 | [diff] [blame] | 169 | auto frames_fn = [&](jvmtiFrameInfo info) { |
| 170 | frames.push_back(info); |
| 171 | }; |
| 172 | auto visitor = MakeStackTraceVisitor(self, start_input, stop_input, frames_fn); |
Andreas Gampe | 6e89776 | 2018-10-16 13:09:32 -0700 | [diff] [blame] | 173 | visitor.WalkStack(/* include_transitions= */ false); |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 174 | |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 175 | start_result = visitor.start; |
| 176 | stop_result = visitor.stop; |
| 177 | } |
| 178 | |
| 179 | const size_t start_input; |
| 180 | const size_t stop_input; |
| 181 | |
| 182 | std::vector<jvmtiFrameInfo> frames; |
| 183 | size_t start_result; |
| 184 | size_t stop_result; |
| 185 | }; |
| 186 | |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 187 | static jvmtiError TranslateFrameVector(const std::vector<jvmtiFrameInfo>& frames, |
| 188 | jint start_depth, |
| 189 | size_t start_result, |
| 190 | jint max_frame_count, |
| 191 | jvmtiFrameInfo* frame_buffer, |
| 192 | jint* count_ptr) { |
| 193 | size_t collected_frames = frames.size(); |
| 194 | |
| 195 | // Assume we're here having collected something. |
| 196 | DCHECK_GT(max_frame_count, 0); |
| 197 | |
| 198 | // Frames from the top. |
| 199 | if (start_depth >= 0) { |
| 200 | if (start_result != 0) { |
| 201 | // Not enough frames. |
| 202 | return ERR(ILLEGAL_ARGUMENT); |
| 203 | } |
| 204 | DCHECK_LE(collected_frames, static_cast<size_t>(max_frame_count)); |
| 205 | if (frames.size() > 0) { |
| 206 | memcpy(frame_buffer, frames.data(), collected_frames * sizeof(jvmtiFrameInfo)); |
| 207 | } |
| 208 | *count_ptr = static_cast<jint>(frames.size()); |
| 209 | return ERR(NONE); |
| 210 | } |
| 211 | |
| 212 | // Frames from the bottom. |
| 213 | if (collected_frames < static_cast<size_t>(-start_depth)) { |
| 214 | return ERR(ILLEGAL_ARGUMENT); |
| 215 | } |
| 216 | |
| 217 | size_t count = std::min(static_cast<size_t>(-start_depth), static_cast<size_t>(max_frame_count)); |
| 218 | memcpy(frame_buffer, |
| 219 | &frames.data()[collected_frames + start_depth], |
| 220 | count * sizeof(jvmtiFrameInfo)); |
| 221 | *count_ptr = static_cast<jint>(count); |
| 222 | return ERR(NONE); |
| 223 | } |
| 224 | |
Andreas Gampe | 850a0fe | 2017-06-12 18:37:19 -0700 | [diff] [blame] | 225 | struct GetStackTraceDirectClosure : public art::Closure { |
| 226 | public: |
| 227 | GetStackTraceDirectClosure(jvmtiFrameInfo* frame_buffer_, size_t start, size_t stop) |
| 228 | : frame_buffer(frame_buffer_), |
| 229 | start_input(start), |
| 230 | stop_input(stop), |
| 231 | index(0) { |
| 232 | DCHECK_GE(start_input, 0u); |
| 233 | } |
| 234 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 235 | void Run(art::Thread* self) override REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Andreas Gampe | 850a0fe | 2017-06-12 18:37:19 -0700 | [diff] [blame] | 236 | auto frames_fn = [&](jvmtiFrameInfo info) { |
| 237 | frame_buffer[index] = info; |
| 238 | ++index; |
| 239 | }; |
| 240 | auto visitor = MakeStackTraceVisitor(self, start_input, stop_input, frames_fn); |
Andreas Gampe | 6e89776 | 2018-10-16 13:09:32 -0700 | [diff] [blame] | 241 | visitor.WalkStack(/* include_transitions= */ false); |
Andreas Gampe | 850a0fe | 2017-06-12 18:37:19 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | jvmtiFrameInfo* frame_buffer; |
| 245 | |
| 246 | const size_t start_input; |
| 247 | const size_t stop_input; |
| 248 | |
| 249 | size_t index = 0; |
| 250 | }; |
| 251 | |
Alex Light | 342b694 | 2019-05-07 15:28:39 -0700 | [diff] [blame] | 252 | jvmtiError StackUtil::GetStackTrace(jvmtiEnv* jvmti_env, |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 253 | jthread java_thread, |
| 254 | jint start_depth, |
| 255 | jint max_frame_count, |
| 256 | jvmtiFrameInfo* frame_buffer, |
| 257 | jint* count_ptr) { |
Andreas Gampe | 28c4a23 | 2017-06-21 21:21:31 -0700 | [diff] [blame] | 258 | // It is not great that we have to hold these locks for so long, but it is necessary to ensure |
| 259 | // that the thread isn't dying on us. |
| 260 | art::ScopedObjectAccess soa(art::Thread::Current()); |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 261 | art::Locks::thread_list_lock_->ExclusiveLock(soa.Self()); |
Andreas Gampe | 28c4a23 | 2017-06-21 21:21:31 -0700 | [diff] [blame] | 262 | |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 263 | art::Thread* thread; |
Alex Light | 7ddc23d | 2017-09-22 15:33:41 -0700 | [diff] [blame] | 264 | jvmtiError thread_error = ERR(INTERNAL); |
| 265 | if (!ThreadUtil::GetAliveNativeThread(java_thread, soa, &thread, &thread_error)) { |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 266 | art::Locks::thread_list_lock_->ExclusiveUnlock(soa.Self()); |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 267 | return thread_error; |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 268 | } |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 269 | DCHECK(thread != nullptr); |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 270 | |
| 271 | art::ThreadState state = thread->GetState(); |
Alex Light | 7ddc23d | 2017-09-22 15:33:41 -0700 | [diff] [blame] | 272 | if (state == art::ThreadState::kStarting || thread->IsStillStarting()) { |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 273 | art::Locks::thread_list_lock_->ExclusiveUnlock(soa.Self()); |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 274 | return ERR(THREAD_NOT_ALIVE); |
| 275 | } |
| 276 | |
| 277 | if (max_frame_count < 0) { |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 278 | art::Locks::thread_list_lock_->ExclusiveUnlock(soa.Self()); |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 279 | return ERR(ILLEGAL_ARGUMENT); |
| 280 | } |
| 281 | if (frame_buffer == nullptr || count_ptr == nullptr) { |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 282 | art::Locks::thread_list_lock_->ExclusiveUnlock(soa.Self()); |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 283 | return ERR(NULL_POINTER); |
| 284 | } |
| 285 | |
| 286 | if (max_frame_count == 0) { |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 287 | art::Locks::thread_list_lock_->ExclusiveUnlock(soa.Self()); |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 288 | *count_ptr = 0; |
| 289 | return ERR(NONE); |
| 290 | } |
| 291 | |
Andreas Gampe | 850a0fe | 2017-06-12 18:37:19 -0700 | [diff] [blame] | 292 | if (start_depth >= 0) { |
| 293 | // Fast path: Regular order of stack trace. Fill into the frame_buffer directly. |
| 294 | GetStackTraceDirectClosure closure(frame_buffer, |
| 295 | static_cast<size_t>(start_depth), |
| 296 | static_cast<size_t>(max_frame_count)); |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 297 | // RequestSynchronousCheckpoint releases the thread_list_lock_ as a part of its execution. |
Alex Light | 318afe6 | 2018-03-22 16:50:10 -0700 | [diff] [blame] | 298 | if (!thread->RequestSynchronousCheckpoint(&closure)) { |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 299 | return ERR(THREAD_NOT_ALIVE); |
| 300 | } |
Andreas Gampe | 850a0fe | 2017-06-12 18:37:19 -0700 | [diff] [blame] | 301 | *count_ptr = static_cast<jint>(closure.index); |
Alex Light | 342b694 | 2019-05-07 15:28:39 -0700 | [diff] [blame] | 302 | if (closure.index == 0) { |
| 303 | JVMTI_LOG(INFO, jvmti_env) << "The stack is not large enough for a start_depth of " |
| 304 | << start_depth << "."; |
Andreas Gampe | 850a0fe | 2017-06-12 18:37:19 -0700 | [diff] [blame] | 305 | return ERR(ILLEGAL_ARGUMENT); |
| 306 | } |
| 307 | return ERR(NONE); |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 308 | } else { |
| 309 | GetStackTraceVectorClosure closure(0, 0); |
| 310 | // RequestSynchronousCheckpoint releases the thread_list_lock_ as a part of its execution. |
Alex Light | 318afe6 | 2018-03-22 16:50:10 -0700 | [diff] [blame] | 311 | if (!thread->RequestSynchronousCheckpoint(&closure)) { |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 312 | return ERR(THREAD_NOT_ALIVE); |
| 313 | } |
| 314 | |
| 315 | return TranslateFrameVector(closure.frames, |
| 316 | start_depth, |
| 317 | closure.start_result, |
| 318 | max_frame_count, |
| 319 | frame_buffer, |
| 320 | count_ptr); |
Andreas Gampe | 850a0fe | 2017-06-12 18:37:19 -0700 | [diff] [blame] | 321 | } |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 322 | } |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 323 | |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 324 | template <typename Data> |
| 325 | struct GetAllStackTracesVectorClosure : public art::Closure { |
Andreas Gampe | 6237cd3 | 2017-06-22 22:17:38 -0700 | [diff] [blame] | 326 | GetAllStackTracesVectorClosure(size_t stop, Data* data_) |
| 327 | : barrier(0), stop_input(stop), data(data_) {} |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 328 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 329 | void Run(art::Thread* thread) override |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 330 | REQUIRES_SHARED(art::Locks::mutator_lock_) |
| 331 | REQUIRES(!data->mutex) { |
| 332 | art::Thread* self = art::Thread::Current(); |
Andreas Gampe | 6237cd3 | 2017-06-22 22:17:38 -0700 | [diff] [blame] | 333 | Work(thread, self); |
| 334 | barrier.Pass(self); |
| 335 | } |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 336 | |
Andreas Gampe | 6237cd3 | 2017-06-22 22:17:38 -0700 | [diff] [blame] | 337 | void Work(art::Thread* thread, art::Thread* self) |
| 338 | REQUIRES_SHARED(art::Locks::mutator_lock_) |
| 339 | REQUIRES(!data->mutex) { |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 340 | // Skip threads that are still starting. |
| 341 | if (thread->IsStillStarting()) { |
| 342 | return; |
| 343 | } |
| 344 | |
| 345 | std::vector<jvmtiFrameInfo>* thread_frames = data->GetFrameStorageFor(self, thread); |
| 346 | if (thread_frames == nullptr) { |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | // Now collect the data. |
| 351 | auto frames_fn = [&](jvmtiFrameInfo info) { |
| 352 | thread_frames->push_back(info); |
| 353 | }; |
| 354 | auto visitor = MakeStackTraceVisitor(thread, 0u, stop_input, frames_fn); |
Andreas Gampe | 6e89776 | 2018-10-16 13:09:32 -0700 | [diff] [blame] | 355 | visitor.WalkStack(/* include_transitions= */ false); |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Andreas Gampe | 6237cd3 | 2017-06-22 22:17:38 -0700 | [diff] [blame] | 358 | art::Barrier barrier; |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 359 | const size_t stop_input; |
| 360 | Data* data; |
| 361 | }; |
| 362 | |
Andreas Gampe | 6237cd3 | 2017-06-22 22:17:38 -0700 | [diff] [blame] | 363 | template <typename Data> |
Andreas Gampe | 6baa1c9 | 2018-05-25 16:17:49 -0700 | [diff] [blame] | 364 | static void RunCheckpointAndWait(Data* data, size_t max_frame_count) |
| 365 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 366 | // Note: requires the mutator lock as the checkpoint requires the mutator lock. |
Andreas Gampe | 6237cd3 | 2017-06-22 22:17:38 -0700 | [diff] [blame] | 367 | GetAllStackTracesVectorClosure<Data> closure(max_frame_count, data); |
| 368 | size_t barrier_count = art::Runtime::Current()->GetThreadList()->RunCheckpoint(&closure, nullptr); |
| 369 | if (barrier_count == 0) { |
| 370 | return; |
| 371 | } |
| 372 | art::Thread* self = art::Thread::Current(); |
| 373 | art::ScopedThreadStateChange tsc(self, art::ThreadState::kWaitingForCheckPointsToRun); |
| 374 | closure.barrier.Increment(self, barrier_count); |
| 375 | } |
| 376 | |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 377 | jvmtiError StackUtil::GetAllStackTraces(jvmtiEnv* env, |
| 378 | jint max_frame_count, |
| 379 | jvmtiStackInfo** stack_info_ptr, |
| 380 | jint* thread_count_ptr) { |
| 381 | if (max_frame_count < 0) { |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 382 | return ERR(ILLEGAL_ARGUMENT); |
| 383 | } |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 384 | if (stack_info_ptr == nullptr || thread_count_ptr == nullptr) { |
| 385 | return ERR(NULL_POINTER); |
| 386 | } |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 387 | |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 388 | struct AllStackTracesData { |
| 389 | AllStackTracesData() : mutex("GetAllStackTraces", art::LockLevel::kAbortLock) {} |
| 390 | ~AllStackTracesData() { |
| 391 | JNIEnv* jni_env = art::Thread::Current()->GetJniEnv(); |
| 392 | for (jthread global_thread_ref : thread_peers) { |
| 393 | jni_env->DeleteGlobalRef(global_thread_ref); |
| 394 | } |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 395 | } |
| 396 | |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 397 | std::vector<jvmtiFrameInfo>* GetFrameStorageFor(art::Thread* self, art::Thread* thread) |
| 398 | REQUIRES_SHARED(art::Locks::mutator_lock_) |
| 399 | REQUIRES(!mutex) { |
| 400 | art::MutexLock mu(self, mutex); |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 401 | |
| 402 | threads.push_back(thread); |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 403 | |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 404 | jthread peer = art::Runtime::Current()->GetJavaVM()->AddGlobalRef( |
| 405 | self, thread->GetPeerFromOtherThread()); |
| 406 | thread_peers.push_back(peer); |
| 407 | |
Andreas Gampe | ad9173d | 2017-06-22 16:33:08 -0700 | [diff] [blame] | 408 | frames.emplace_back(new std::vector<jvmtiFrameInfo>()); |
| 409 | return frames.back().get(); |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | art::Mutex mutex; |
| 413 | |
| 414 | // Storage. Only access directly after completion. |
| 415 | |
| 416 | std::vector<art::Thread*> threads; |
| 417 | // "thread_peers" contains global references to their peers. |
| 418 | std::vector<jthread> thread_peers; |
| 419 | |
Andreas Gampe | ad9173d | 2017-06-22 16:33:08 -0700 | [diff] [blame] | 420 | std::vector<std::unique_ptr<std::vector<jvmtiFrameInfo>>> frames; |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 421 | }; |
| 422 | |
| 423 | AllStackTracesData data; |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 424 | art::Thread* current = art::Thread::Current(); |
Andreas Gampe | 6baa1c9 | 2018-05-25 16:17:49 -0700 | [diff] [blame] | 425 | { |
| 426 | art::ScopedObjectAccess soa(current); |
| 427 | RunCheckpointAndWait(&data, static_cast<size_t>(max_frame_count)); |
| 428 | } |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 429 | |
| 430 | // Convert the data into our output format. |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 431 | |
| 432 | // Note: we use an array of jvmtiStackInfo for convenience. The spec says we need to |
| 433 | // allocate one big chunk for this and the actual frames, which means we need |
| 434 | // to either be conservative or rearrange things later (the latter is implemented). |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 435 | std::unique_ptr<jvmtiStackInfo[]> stack_info_array(new jvmtiStackInfo[data.frames.size()]); |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 436 | std::vector<std::unique_ptr<jvmtiFrameInfo[]>> frame_infos; |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 437 | frame_infos.reserve(data.frames.size()); |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 438 | |
| 439 | // Now run through and add data for each thread. |
| 440 | size_t sum_frames = 0; |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 441 | for (size_t index = 0; index < data.frames.size(); ++index) { |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 442 | jvmtiStackInfo& stack_info = stack_info_array.get()[index]; |
| 443 | memset(&stack_info, 0, sizeof(jvmtiStackInfo)); |
| 444 | |
Andreas Gampe | ad9173d | 2017-06-22 16:33:08 -0700 | [diff] [blame] | 445 | const std::vector<jvmtiFrameInfo>& thread_frames = *data.frames[index].get(); |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 446 | |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 447 | // For the time being, set the thread to null. We'll fix it up in the second stage. |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 448 | stack_info.thread = nullptr; |
| 449 | stack_info.state = JVMTI_THREAD_STATE_SUSPENDED; |
| 450 | |
| 451 | size_t collected_frames = thread_frames.size(); |
| 452 | if (max_frame_count == 0 || collected_frames == 0) { |
| 453 | stack_info.frame_count = 0; |
| 454 | stack_info.frame_buffer = nullptr; |
| 455 | continue; |
| 456 | } |
| 457 | DCHECK_LE(collected_frames, static_cast<size_t>(max_frame_count)); |
| 458 | |
| 459 | jvmtiFrameInfo* frame_info = new jvmtiFrameInfo[collected_frames]; |
| 460 | frame_infos.emplace_back(frame_info); |
| 461 | |
| 462 | jint count; |
| 463 | jvmtiError translate_result = TranslateFrameVector(thread_frames, |
| 464 | 0, |
| 465 | 0, |
| 466 | static_cast<jint>(collected_frames), |
| 467 | frame_info, |
| 468 | &count); |
| 469 | DCHECK(translate_result == JVMTI_ERROR_NONE); |
| 470 | stack_info.frame_count = static_cast<jint>(collected_frames); |
| 471 | stack_info.frame_buffer = frame_info; |
| 472 | sum_frames += static_cast<size_t>(count); |
| 473 | } |
| 474 | |
| 475 | // No errors, yet. Now put it all into an output buffer. |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 476 | size_t rounded_stack_info_size = art::RoundUp(sizeof(jvmtiStackInfo) * data.frames.size(), |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 477 | alignof(jvmtiFrameInfo)); |
| 478 | size_t chunk_size = rounded_stack_info_size + sum_frames * sizeof(jvmtiFrameInfo); |
| 479 | unsigned char* chunk_data; |
| 480 | jvmtiError alloc_result = env->Allocate(chunk_size, &chunk_data); |
| 481 | if (alloc_result != ERR(NONE)) { |
| 482 | return alloc_result; |
| 483 | } |
| 484 | |
| 485 | jvmtiStackInfo* stack_info = reinterpret_cast<jvmtiStackInfo*>(chunk_data); |
| 486 | // First copy in all the basic data. |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 487 | memcpy(stack_info, stack_info_array.get(), sizeof(jvmtiStackInfo) * data.frames.size()); |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 488 | |
| 489 | // Now copy the frames and fix up the pointers. |
| 490 | jvmtiFrameInfo* frame_info = reinterpret_cast<jvmtiFrameInfo*>( |
| 491 | chunk_data + rounded_stack_info_size); |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 492 | for (size_t i = 0; i < data.frames.size(); ++i) { |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 493 | jvmtiStackInfo& old_stack_info = stack_info_array.get()[i]; |
| 494 | jvmtiStackInfo& new_stack_info = stack_info[i]; |
| 495 | |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 496 | // Translate the global ref into a local ref. |
| 497 | new_stack_info.thread = |
| 498 | static_cast<JNIEnv*>(current->GetJniEnv())->NewLocalRef(data.thread_peers[i]); |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 499 | |
| 500 | if (old_stack_info.frame_count > 0) { |
| 501 | // Only copy when there's data - leave the nullptr alone. |
| 502 | size_t frames_size = static_cast<size_t>(old_stack_info.frame_count) * sizeof(jvmtiFrameInfo); |
| 503 | memcpy(frame_info, old_stack_info.frame_buffer, frames_size); |
| 504 | new_stack_info.frame_buffer = frame_info; |
| 505 | frame_info += old_stack_info.frame_count; |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | *stack_info_ptr = stack_info; |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 510 | *thread_count_ptr = static_cast<jint>(data.frames.size()); |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 511 | |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 512 | return ERR(NONE); |
| 513 | } |
| 514 | |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 515 | jvmtiError StackUtil::GetThreadListStackTraces(jvmtiEnv* env, |
| 516 | jint thread_count, |
| 517 | const jthread* thread_list, |
| 518 | jint max_frame_count, |
| 519 | jvmtiStackInfo** stack_info_ptr) { |
| 520 | if (max_frame_count < 0) { |
| 521 | return ERR(ILLEGAL_ARGUMENT); |
| 522 | } |
| 523 | if (thread_count < 0) { |
| 524 | return ERR(ILLEGAL_ARGUMENT); |
| 525 | } |
| 526 | if (thread_count == 0) { |
| 527 | *stack_info_ptr = nullptr; |
| 528 | return ERR(NONE); |
| 529 | } |
Alex Light | 19a7d4f | 2018-03-23 10:05:49 -0700 | [diff] [blame] | 530 | if (thread_list == nullptr || stack_info_ptr == nullptr) { |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 531 | return ERR(NULL_POINTER); |
| 532 | } |
| 533 | |
| 534 | art::Thread* current = art::Thread::Current(); |
| 535 | art::ScopedObjectAccess soa(current); // Now we know we have the shared lock. |
| 536 | |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 537 | struct SelectStackTracesData { |
| 538 | SelectStackTracesData() : mutex("GetSelectStackTraces", art::LockLevel::kAbortLock) {} |
| 539 | |
| 540 | std::vector<jvmtiFrameInfo>* GetFrameStorageFor(art::Thread* self, art::Thread* thread) |
| 541 | REQUIRES_SHARED(art::Locks::mutator_lock_) |
| 542 | REQUIRES(!mutex) { |
| 543 | art::ObjPtr<art::mirror::Object> peer = thread->GetPeerFromOtherThread(); |
| 544 | for (size_t index = 0; index != handles.size(); ++index) { |
| 545 | if (peer == handles[index].Get()) { |
| 546 | // Found the thread. |
| 547 | art::MutexLock mu(self, mutex); |
| 548 | |
| 549 | threads.push_back(thread); |
| 550 | thread_list_indices.push_back(index); |
| 551 | |
Andreas Gampe | ad9173d | 2017-06-22 16:33:08 -0700 | [diff] [blame] | 552 | frames.emplace_back(new std::vector<jvmtiFrameInfo>()); |
| 553 | return frames.back().get(); |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 554 | } |
| 555 | } |
| 556 | return nullptr; |
| 557 | } |
| 558 | |
| 559 | art::Mutex mutex; |
| 560 | |
| 561 | // Selection data. |
| 562 | |
| 563 | std::vector<art::Handle<art::mirror::Object>> handles; |
| 564 | |
| 565 | // Storage. Only access directly after completion. |
| 566 | |
| 567 | std::vector<art::Thread*> threads; |
| 568 | std::vector<size_t> thread_list_indices; |
| 569 | |
Andreas Gampe | ad9173d | 2017-06-22 16:33:08 -0700 | [diff] [blame] | 570 | std::vector<std::unique_ptr<std::vector<jvmtiFrameInfo>>> frames; |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 571 | }; |
| 572 | |
| 573 | SelectStackTracesData data; |
| 574 | |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 575 | // Decode all threads to raw pointers. Put them into a handle scope to avoid any moving GC bugs. |
| 576 | art::VariableSizedHandleScope hs(current); |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 577 | for (jint i = 0; i != thread_count; ++i) { |
| 578 | if (thread_list[i] == nullptr) { |
| 579 | return ERR(INVALID_THREAD); |
| 580 | } |
| 581 | if (!soa.Env()->IsInstanceOf(thread_list[i], art::WellKnownClasses::java_lang_Thread)) { |
| 582 | return ERR(INVALID_THREAD); |
| 583 | } |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 584 | data.handles.push_back(hs.NewHandle(soa.Decode<art::mirror::Object>(thread_list[i]))); |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 585 | } |
| 586 | |
Andreas Gampe | 6237cd3 | 2017-06-22 22:17:38 -0700 | [diff] [blame] | 587 | RunCheckpointAndWait(&data, static_cast<size_t>(max_frame_count)); |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 588 | |
| 589 | // Convert the data into our output format. |
| 590 | |
| 591 | // Note: we use an array of jvmtiStackInfo for convenience. The spec says we need to |
| 592 | // allocate one big chunk for this and the actual frames, which means we need |
| 593 | // to either be conservative or rearrange things later (the latter is implemented). |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 594 | std::unique_ptr<jvmtiStackInfo[]> stack_info_array(new jvmtiStackInfo[data.frames.size()]); |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 595 | std::vector<std::unique_ptr<jvmtiFrameInfo[]>> frame_infos; |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 596 | frame_infos.reserve(data.frames.size()); |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 597 | |
| 598 | // Now run through and add data for each thread. |
| 599 | size_t sum_frames = 0; |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 600 | for (size_t index = 0; index < data.frames.size(); ++index) { |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 601 | jvmtiStackInfo& stack_info = stack_info_array.get()[index]; |
| 602 | memset(&stack_info, 0, sizeof(jvmtiStackInfo)); |
| 603 | |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 604 | art::Thread* self = data.threads[index]; |
Andreas Gampe | ad9173d | 2017-06-22 16:33:08 -0700 | [diff] [blame] | 605 | const std::vector<jvmtiFrameInfo>& thread_frames = *data.frames[index].get(); |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 606 | |
| 607 | // For the time being, set the thread to null. We don't have good ScopedLocalRef |
| 608 | // infrastructure. |
Nicolas Geoffray | ffc8cad | 2017-02-10 10:59:22 +0000 | [diff] [blame] | 609 | DCHECK(self->GetPeerFromOtherThread() != nullptr); |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 610 | stack_info.thread = nullptr; |
| 611 | stack_info.state = JVMTI_THREAD_STATE_SUSPENDED; |
| 612 | |
| 613 | size_t collected_frames = thread_frames.size(); |
| 614 | if (max_frame_count == 0 || collected_frames == 0) { |
| 615 | stack_info.frame_count = 0; |
| 616 | stack_info.frame_buffer = nullptr; |
| 617 | continue; |
| 618 | } |
| 619 | DCHECK_LE(collected_frames, static_cast<size_t>(max_frame_count)); |
| 620 | |
| 621 | jvmtiFrameInfo* frame_info = new jvmtiFrameInfo[collected_frames]; |
| 622 | frame_infos.emplace_back(frame_info); |
| 623 | |
| 624 | jint count; |
| 625 | jvmtiError translate_result = TranslateFrameVector(thread_frames, |
| 626 | 0, |
| 627 | 0, |
| 628 | static_cast<jint>(collected_frames), |
| 629 | frame_info, |
| 630 | &count); |
| 631 | DCHECK(translate_result == JVMTI_ERROR_NONE); |
| 632 | stack_info.frame_count = static_cast<jint>(collected_frames); |
| 633 | stack_info.frame_buffer = frame_info; |
| 634 | sum_frames += static_cast<size_t>(count); |
| 635 | } |
| 636 | |
| 637 | // No errors, yet. Now put it all into an output buffer. Note that this is not frames.size(), |
| 638 | // potentially. |
| 639 | size_t rounded_stack_info_size = art::RoundUp(sizeof(jvmtiStackInfo) * thread_count, |
| 640 | alignof(jvmtiFrameInfo)); |
| 641 | size_t chunk_size = rounded_stack_info_size + sum_frames * sizeof(jvmtiFrameInfo); |
| 642 | unsigned char* chunk_data; |
| 643 | jvmtiError alloc_result = env->Allocate(chunk_size, &chunk_data); |
| 644 | if (alloc_result != ERR(NONE)) { |
| 645 | return alloc_result; |
| 646 | } |
| 647 | |
| 648 | jvmtiStackInfo* stack_info = reinterpret_cast<jvmtiStackInfo*>(chunk_data); |
| 649 | jvmtiFrameInfo* frame_info = reinterpret_cast<jvmtiFrameInfo*>( |
| 650 | chunk_data + rounded_stack_info_size); |
| 651 | |
| 652 | for (size_t i = 0; i < static_cast<size_t>(thread_count); ++i) { |
| 653 | // Check whether we found a running thread for this. |
| 654 | // Note: For simplicity, and with the expectation that the list is usually small, use a simple |
| 655 | // search. (The list is *not* sorted!) |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 656 | auto it = std::find(data.thread_list_indices.begin(), data.thread_list_indices.end(), i); |
| 657 | if (it == data.thread_list_indices.end()) { |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 658 | // No native thread. Must be new or dead. We need to fill out the stack info now. |
| 659 | // (Need to read the Java "started" field to know whether this is starting or terminated.) |
| 660 | art::ObjPtr<art::mirror::Object> peer = soa.Decode<art::mirror::Object>(thread_list[i]); |
| 661 | art::ObjPtr<art::mirror::Class> klass = peer->GetClass(); |
| 662 | art::ArtField* started_field = klass->FindDeclaredInstanceField("started", "Z"); |
| 663 | CHECK(started_field != nullptr); |
| 664 | bool started = started_field->GetBoolean(peer) != 0; |
| 665 | constexpr jint kStartedState = JVMTI_JAVA_LANG_THREAD_STATE_NEW; |
| 666 | constexpr jint kTerminatedState = JVMTI_THREAD_STATE_TERMINATED | |
| 667 | JVMTI_JAVA_LANG_THREAD_STATE_TERMINATED; |
| 668 | stack_info[i].thread = reinterpret_cast<JNIEnv*>(soa.Env())->NewLocalRef(thread_list[i]); |
| 669 | stack_info[i].state = started ? kTerminatedState : kStartedState; |
| 670 | stack_info[i].frame_count = 0; |
| 671 | stack_info[i].frame_buffer = nullptr; |
| 672 | } else { |
| 673 | // Had a native thread and frames. |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 674 | size_t f_index = it - data.thread_list_indices.begin(); |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 675 | |
| 676 | jvmtiStackInfo& old_stack_info = stack_info_array.get()[f_index]; |
| 677 | jvmtiStackInfo& new_stack_info = stack_info[i]; |
| 678 | |
| 679 | memcpy(&new_stack_info, &old_stack_info, sizeof(jvmtiStackInfo)); |
| 680 | new_stack_info.thread = reinterpret_cast<JNIEnv*>(soa.Env())->NewLocalRef(thread_list[i]); |
| 681 | if (old_stack_info.frame_count > 0) { |
| 682 | // Only copy when there's data - leave the nullptr alone. |
| 683 | size_t frames_size = |
| 684 | static_cast<size_t>(old_stack_info.frame_count) * sizeof(jvmtiFrameInfo); |
| 685 | memcpy(frame_info, old_stack_info.frame_buffer, frames_size); |
| 686 | new_stack_info.frame_buffer = frame_info; |
| 687 | frame_info += old_stack_info.frame_count; |
| 688 | } |
| 689 | } |
| 690 | } |
| 691 | |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 692 | *stack_info_ptr = stack_info; |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 693 | |
| 694 | return ERR(NONE); |
| 695 | } |
| 696 | |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 697 | struct GetFrameCountClosure : public art::Closure { |
| 698 | public: |
| 699 | GetFrameCountClosure() : count(0) {} |
| 700 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 701 | void Run(art::Thread* self) override REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Andreas Gampe | c7d878d | 2018-11-19 18:42:06 +0000 | [diff] [blame] | 702 | // This is not StackVisitor::ComputeNumFrames, as runtime methods and transitions must not be |
| 703 | // counted. |
| 704 | art::StackVisitor::WalkStack( |
| 705 | [&](const art::StackVisitor* stack_visitor) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 706 | art::ArtMethod* m = stack_visitor->GetMethod(); |
| 707 | if (m != nullptr && !m->IsRuntimeMethod()) { |
| 708 | count++; |
| 709 | } |
| 710 | return true; |
| 711 | }, |
| 712 | self, |
| 713 | /* context= */ nullptr, |
| 714 | art::StackVisitor::StackWalkKind::kIncludeInlinedFrames); |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | size_t count; |
| 718 | }; |
| 719 | |
| 720 | jvmtiError StackUtil::GetFrameCount(jvmtiEnv* env ATTRIBUTE_UNUSED, |
| 721 | jthread java_thread, |
| 722 | jint* count_ptr) { |
Andreas Gampe | 28c4a23 | 2017-06-21 21:21:31 -0700 | [diff] [blame] | 723 | // It is not great that we have to hold these locks for so long, but it is necessary to ensure |
| 724 | // that the thread isn't dying on us. |
| 725 | art::ScopedObjectAccess soa(art::Thread::Current()); |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 726 | art::Locks::thread_list_lock_->ExclusiveLock(soa.Self()); |
Andreas Gampe | 28c4a23 | 2017-06-21 21:21:31 -0700 | [diff] [blame] | 727 | |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 728 | art::Thread* thread; |
Alex Light | 7ddc23d | 2017-09-22 15:33:41 -0700 | [diff] [blame] | 729 | jvmtiError thread_error = ERR(INTERNAL); |
| 730 | if (!ThreadUtil::GetAliveNativeThread(java_thread, soa, &thread, &thread_error)) { |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 731 | art::Locks::thread_list_lock_->ExclusiveUnlock(soa.Self()); |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 732 | return thread_error; |
| 733 | } |
Alex Light | 7ddc23d | 2017-09-22 15:33:41 -0700 | [diff] [blame] | 734 | |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 735 | DCHECK(thread != nullptr); |
Alex Light | 7ddc23d | 2017-09-22 15:33:41 -0700 | [diff] [blame] | 736 | art::ThreadState state = thread->GetState(); |
| 737 | if (state == art::ThreadState::kStarting || thread->IsStillStarting()) { |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 738 | art::Locks::thread_list_lock_->ExclusiveUnlock(soa.Self()); |
Alex Light | 7ddc23d | 2017-09-22 15:33:41 -0700 | [diff] [blame] | 739 | return ERR(THREAD_NOT_ALIVE); |
| 740 | } |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 741 | |
| 742 | if (count_ptr == nullptr) { |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 743 | art::Locks::thread_list_lock_->ExclusiveUnlock(soa.Self()); |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 744 | return ERR(NULL_POINTER); |
| 745 | } |
| 746 | |
| 747 | GetFrameCountClosure closure; |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 748 | // RequestSynchronousCheckpoint releases the thread_list_lock_ as a part of its execution. |
Alex Light | 318afe6 | 2018-03-22 16:50:10 -0700 | [diff] [blame] | 749 | if (!thread->RequestSynchronousCheckpoint(&closure)) { |
Alex Light | 7ddc23d | 2017-09-22 15:33:41 -0700 | [diff] [blame] | 750 | return ERR(THREAD_NOT_ALIVE); |
| 751 | } |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 752 | |
| 753 | *count_ptr = closure.count; |
| 754 | return ERR(NONE); |
| 755 | } |
| 756 | |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 757 | struct GetLocationClosure : public art::Closure { |
| 758 | public: |
| 759 | explicit GetLocationClosure(size_t n_in) : n(n_in), method(nullptr), dex_pc(0) {} |
| 760 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 761 | void Run(art::Thread* self) override REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Andreas Gampe | c7d878d | 2018-11-19 18:42:06 +0000 | [diff] [blame] | 762 | // Walks up the stack 'n' callers. |
| 763 | size_t count = 0u; |
| 764 | art::StackVisitor::WalkStack( |
| 765 | [&](const art::StackVisitor* stack_visitor) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 766 | art::ArtMethod* m = stack_visitor->GetMethod(); |
| 767 | if (m != nullptr && !m->IsRuntimeMethod()) { |
| 768 | DCHECK(method == nullptr); |
| 769 | if (count == n) { |
| 770 | method = m; |
| 771 | dex_pc = stack_visitor->GetDexPc(/*abort_on_failure=*/false); |
| 772 | return false; |
| 773 | } |
| 774 | count++; |
| 775 | } |
| 776 | return true; |
| 777 | }, |
| 778 | self, |
| 779 | /* context= */ nullptr, |
| 780 | art::StackVisitor::StackWalkKind::kIncludeInlinedFrames); |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | const size_t n; |
| 784 | art::ArtMethod* method; |
| 785 | uint32_t dex_pc; |
| 786 | }; |
| 787 | |
| 788 | jvmtiError StackUtil::GetFrameLocation(jvmtiEnv* env ATTRIBUTE_UNUSED, |
| 789 | jthread java_thread, |
| 790 | jint depth, |
| 791 | jmethodID* method_ptr, |
| 792 | jlocation* location_ptr) { |
Andreas Gampe | 28c4a23 | 2017-06-21 21:21:31 -0700 | [diff] [blame] | 793 | // It is not great that we have to hold these locks for so long, but it is necessary to ensure |
| 794 | // that the thread isn't dying on us. |
| 795 | art::ScopedObjectAccess soa(art::Thread::Current()); |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 796 | art::Locks::thread_list_lock_->ExclusiveLock(soa.Self()); |
Andreas Gampe | 28c4a23 | 2017-06-21 21:21:31 -0700 | [diff] [blame] | 797 | |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 798 | art::Thread* thread; |
Alex Light | 7ddc23d | 2017-09-22 15:33:41 -0700 | [diff] [blame] | 799 | jvmtiError thread_error = ERR(INTERNAL); |
| 800 | if (!ThreadUtil::GetAliveNativeThread(java_thread, soa, &thread, &thread_error)) { |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 801 | art::Locks::thread_list_lock_->ExclusiveUnlock(soa.Self()); |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 802 | return thread_error; |
| 803 | } |
| 804 | DCHECK(thread != nullptr); |
| 805 | |
Alex Light | 7ddc23d | 2017-09-22 15:33:41 -0700 | [diff] [blame] | 806 | art::ThreadState state = thread->GetState(); |
| 807 | if (state == art::ThreadState::kStarting || thread->IsStillStarting()) { |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 808 | art::Locks::thread_list_lock_->ExclusiveUnlock(soa.Self()); |
Alex Light | 7ddc23d | 2017-09-22 15:33:41 -0700 | [diff] [blame] | 809 | return ERR(THREAD_NOT_ALIVE); |
| 810 | } |
| 811 | |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 812 | if (depth < 0) { |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 813 | art::Locks::thread_list_lock_->ExclusiveUnlock(soa.Self()); |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 814 | return ERR(ILLEGAL_ARGUMENT); |
| 815 | } |
| 816 | if (method_ptr == nullptr || location_ptr == nullptr) { |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 817 | art::Locks::thread_list_lock_->ExclusiveUnlock(soa.Self()); |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 818 | return ERR(NULL_POINTER); |
| 819 | } |
| 820 | |
| 821 | GetLocationClosure closure(static_cast<size_t>(depth)); |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 822 | // RequestSynchronousCheckpoint releases the thread_list_lock_ as a part of its execution. |
Alex Light | 318afe6 | 2018-03-22 16:50:10 -0700 | [diff] [blame] | 823 | if (!thread->RequestSynchronousCheckpoint(&closure)) { |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 824 | return ERR(THREAD_NOT_ALIVE); |
| 825 | } |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 826 | |
| 827 | if (closure.method == nullptr) { |
| 828 | return ERR(NO_MORE_FRAMES); |
| 829 | } |
| 830 | |
| 831 | *method_ptr = art::jni::EncodeArtMethod(closure.method); |
Alex Light | 3dea212 | 2017-10-11 15:56:48 +0000 | [diff] [blame] | 832 | if (closure.method->IsNative() || closure.method->IsProxyMethod()) { |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 833 | *location_ptr = -1; |
| 834 | } else { |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 835 | if (closure.dex_pc == art::dex::kDexNoIndex) { |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 836 | return ERR(INTERNAL); |
| 837 | } |
| 838 | *location_ptr = static_cast<jlocation>(closure.dex_pc); |
| 839 | } |
| 840 | |
| 841 | return ERR(NONE); |
| 842 | } |
| 843 | |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 844 | struct MonitorVisitor : public art::StackVisitor, public art::SingleRootVisitor { |
| 845 | // We need a context because VisitLocks needs it retrieve the monitor objects. |
| 846 | explicit MonitorVisitor(art::Thread* thread) |
| 847 | REQUIRES_SHARED(art::Locks::mutator_lock_) |
| 848 | : art::StackVisitor(thread, |
| 849 | art::Context::Create(), |
| 850 | art::StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 851 | hs(art::Thread::Current()), |
| 852 | current_stack_depth(0) {} |
| 853 | |
| 854 | ~MonitorVisitor() { |
| 855 | delete context_; |
| 856 | } |
| 857 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 858 | bool VisitFrame() override REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 859 | art::Locks::mutator_lock_->AssertSharedHeld(art::Thread::Current()); |
| 860 | if (!GetMethod()->IsRuntimeMethod()) { |
| 861 | art::Monitor::VisitLocks(this, AppendOwnedMonitors, this); |
| 862 | ++current_stack_depth; |
| 863 | } |
| 864 | return true; |
| 865 | } |
| 866 | |
Vladimir Marko | f52d92f | 2019-03-29 12:33:02 +0000 | [diff] [blame] | 867 | static void AppendOwnedMonitors(art::ObjPtr<art::mirror::Object> owned_monitor, void* arg) |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 868 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 869 | art::Locks::mutator_lock_->AssertSharedHeld(art::Thread::Current()); |
| 870 | MonitorVisitor* visitor = reinterpret_cast<MonitorVisitor*>(arg); |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 871 | // Filter out duplicates. |
| 872 | for (const art::Handle<art::mirror::Object>& monitor : visitor->monitors) { |
Vladimir Marko | f52d92f | 2019-03-29 12:33:02 +0000 | [diff] [blame] | 873 | if (monitor.Get() == owned_monitor) { |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 874 | return; |
| 875 | } |
| 876 | } |
Vladimir Marko | f52d92f | 2019-03-29 12:33:02 +0000 | [diff] [blame] | 877 | visitor->monitors.push_back(visitor->hs.NewHandle(owned_monitor)); |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 878 | visitor->stack_depths.push_back(visitor->current_stack_depth); |
| 879 | } |
| 880 | |
| 881 | void VisitRoot(art::mirror::Object* obj, const art::RootInfo& info ATTRIBUTE_UNUSED) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 882 | override REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 883 | for (const art::Handle<art::mirror::Object>& m : monitors) { |
| 884 | if (m.Get() == obj) { |
| 885 | return; |
| 886 | } |
| 887 | } |
| 888 | monitors.push_back(hs.NewHandle(obj)); |
| 889 | stack_depths.push_back(-1); |
| 890 | } |
| 891 | |
| 892 | art::VariableSizedHandleScope hs; |
| 893 | jint current_stack_depth; |
| 894 | std::vector<art::Handle<art::mirror::Object>> monitors; |
| 895 | std::vector<jint> stack_depths; |
| 896 | }; |
| 897 | |
| 898 | template<typename Fn> |
| 899 | struct MonitorInfoClosure : public art::Closure { |
| 900 | public: |
Alex Light | 318afe6 | 2018-03-22 16:50:10 -0700 | [diff] [blame] | 901 | explicit MonitorInfoClosure(Fn handle_results) |
| 902 | : err_(OK), handle_results_(handle_results) {} |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 903 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 904 | void Run(art::Thread* target) override REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 905 | art::Locks::mutator_lock_->AssertSharedHeld(art::Thread::Current()); |
| 906 | // Find the monitors on the stack. |
| 907 | MonitorVisitor visitor(target); |
Andreas Gampe | 6e89776 | 2018-10-16 13:09:32 -0700 | [diff] [blame] | 908 | visitor.WalkStack(/* include_transitions= */ false); |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 909 | // Find any other monitors, including ones acquired in native code. |
| 910 | art::RootInfo root_info(art::kRootVMInternal); |
Ian Rogers | 55256cb | 2017-12-21 17:07:11 -0800 | [diff] [blame] | 911 | target->GetJniEnv()->VisitMonitorRoots(&visitor, root_info); |
Alex Light | 318afe6 | 2018-03-22 16:50:10 -0700 | [diff] [blame] | 912 | err_ = handle_results_(visitor); |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | jvmtiError GetError() { |
| 916 | return err_; |
| 917 | } |
| 918 | |
| 919 | private: |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 920 | jvmtiError err_; |
| 921 | Fn handle_results_; |
| 922 | }; |
| 923 | |
| 924 | |
| 925 | template <typename Fn> |
Alex Light | 318afe6 | 2018-03-22 16:50:10 -0700 | [diff] [blame] | 926 | static jvmtiError GetOwnedMonitorInfoCommon(const art::ScopedObjectAccessAlreadyRunnable& soa, |
| 927 | jthread thread, |
| 928 | Fn handle_results) |
| 929 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 930 | art::Thread* self = art::Thread::Current(); |
Alex Light | 318afe6 | 2018-03-22 16:50:10 -0700 | [diff] [blame] | 931 | MonitorInfoClosure<Fn> closure(handle_results); |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 932 | bool called_method = false; |
| 933 | { |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 934 | art::Locks::thread_list_lock_->ExclusiveLock(self); |
Alex Light | 7ddc23d | 2017-09-22 15:33:41 -0700 | [diff] [blame] | 935 | art::Thread* target = nullptr; |
| 936 | jvmtiError err = ERR(INTERNAL); |
| 937 | if (!ThreadUtil::GetAliveNativeThread(thread, soa, &target, &err)) { |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 938 | art::Locks::thread_list_lock_->ExclusiveUnlock(self); |
Alex Light | 7ddc23d | 2017-09-22 15:33:41 -0700 | [diff] [blame] | 939 | return err; |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 940 | } |
| 941 | if (target != self) { |
| 942 | called_method = true; |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 943 | // RequestSynchronousCheckpoint releases the thread_list_lock_ as a part of its execution. |
Alex Light | a5cd4c0 | 2018-03-28 16:07:39 -0700 | [diff] [blame] | 944 | // Since this deals with object references we need to avoid going to sleep. |
| 945 | art::ScopedAssertNoThreadSuspension sants("Getting owned monitor usage"); |
| 946 | if (!target->RequestSynchronousCheckpoint(&closure, art::ThreadState::kRunnable)) { |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 947 | return ERR(THREAD_NOT_ALIVE); |
| 948 | } |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 949 | } else { |
| 950 | art::Locks::thread_list_lock_->ExclusiveUnlock(self); |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 951 | } |
| 952 | } |
| 953 | // Cannot call the closure on the current thread if we have thread_list_lock since we need to call |
| 954 | // into the verifier which can cause the current thread to suspend for gc. Suspending would be a |
| 955 | // bad thing to do if we hold the ThreadListLock. For other threads since we are running it on a |
| 956 | // checkpoint we are fine but if the thread is the current one we need to drop the mutex first. |
| 957 | if (!called_method) { |
| 958 | closure.Run(self); |
| 959 | } |
| 960 | return closure.GetError(); |
| 961 | } |
| 962 | |
| 963 | jvmtiError StackUtil::GetOwnedMonitorStackDepthInfo(jvmtiEnv* env, |
| 964 | jthread thread, |
| 965 | jint* info_cnt, |
| 966 | jvmtiMonitorStackDepthInfo** info_ptr) { |
| 967 | if (info_cnt == nullptr || info_ptr == nullptr) { |
| 968 | return ERR(NULL_POINTER); |
| 969 | } |
Alex Light | 318afe6 | 2018-03-22 16:50:10 -0700 | [diff] [blame] | 970 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 971 | std::vector<art::GcRoot<art::mirror::Object>> mons; |
| 972 | std::vector<uint32_t> depths; |
| 973 | auto handle_fun = [&] (MonitorVisitor& visitor) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 974 | for (size_t i = 0; i < visitor.monitors.size(); i++) { |
Alex Light | 318afe6 | 2018-03-22 16:50:10 -0700 | [diff] [blame] | 975 | mons.push_back(art::GcRoot<art::mirror::Object>(visitor.monitors[i].Get())); |
| 976 | depths.push_back(visitor.stack_depths[i]); |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 977 | } |
| 978 | return OK; |
| 979 | }; |
Alex Light | 318afe6 | 2018-03-22 16:50:10 -0700 | [diff] [blame] | 980 | jvmtiError err = GetOwnedMonitorInfoCommon(soa, thread, handle_fun); |
| 981 | if (err != OK) { |
| 982 | return err; |
| 983 | } |
| 984 | auto nbytes = sizeof(jvmtiMonitorStackDepthInfo) * mons.size(); |
| 985 | err = env->Allocate(nbytes, reinterpret_cast<unsigned char**>(info_ptr)); |
| 986 | if (err != OK) { |
| 987 | return err; |
| 988 | } |
| 989 | *info_cnt = mons.size(); |
| 990 | for (uint32_t i = 0; i < mons.size(); i++) { |
| 991 | (*info_ptr)[i] = { |
| 992 | soa.AddLocalReference<jobject>(mons[i].Read()), |
| 993 | static_cast<jint>(depths[i]) |
| 994 | }; |
| 995 | } |
| 996 | return err; |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | jvmtiError StackUtil::GetOwnedMonitorInfo(jvmtiEnv* env, |
| 1000 | jthread thread, |
| 1001 | jint* owned_monitor_count_ptr, |
| 1002 | jobject** owned_monitors_ptr) { |
Alex Light | 19a7d4f | 2018-03-23 10:05:49 -0700 | [diff] [blame] | 1003 | if (owned_monitor_count_ptr == nullptr || owned_monitors_ptr == nullptr) { |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 1004 | return ERR(NULL_POINTER); |
| 1005 | } |
Alex Light | 318afe6 | 2018-03-22 16:50:10 -0700 | [diff] [blame] | 1006 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 1007 | std::vector<art::GcRoot<art::mirror::Object>> mons; |
| 1008 | auto handle_fun = [&] (MonitorVisitor& visitor) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 1009 | for (size_t i = 0; i < visitor.monitors.size(); i++) { |
Alex Light | 318afe6 | 2018-03-22 16:50:10 -0700 | [diff] [blame] | 1010 | mons.push_back(art::GcRoot<art::mirror::Object>(visitor.monitors[i].Get())); |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 1011 | } |
| 1012 | return OK; |
| 1013 | }; |
Alex Light | 318afe6 | 2018-03-22 16:50:10 -0700 | [diff] [blame] | 1014 | jvmtiError err = GetOwnedMonitorInfoCommon(soa, thread, handle_fun); |
| 1015 | if (err != OK) { |
| 1016 | return err; |
| 1017 | } |
| 1018 | auto nbytes = sizeof(jobject) * mons.size(); |
| 1019 | err = env->Allocate(nbytes, reinterpret_cast<unsigned char**>(owned_monitors_ptr)); |
| 1020 | if (err != OK) { |
| 1021 | return err; |
| 1022 | } |
| 1023 | *owned_monitor_count_ptr = mons.size(); |
| 1024 | for (uint32_t i = 0; i < mons.size(); i++) { |
| 1025 | (*owned_monitors_ptr)[i] = soa.AddLocalReference<jobject>(mons[i].Read()); |
| 1026 | } |
| 1027 | return err; |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 1028 | } |
| 1029 | |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 1030 | jvmtiError StackUtil::NotifyFramePop(jvmtiEnv* env, jthread thread, jint depth) { |
| 1031 | if (depth < 0) { |
| 1032 | return ERR(ILLEGAL_ARGUMENT); |
| 1033 | } |
| 1034 | ArtJvmTiEnv* tienv = ArtJvmTiEnv::AsArtJvmTiEnv(env); |
| 1035 | art::Thread* self = art::Thread::Current(); |
| 1036 | art::Thread* target; |
Alex Light | c723b81 | 2019-04-08 16:13:24 +0000 | [diff] [blame] | 1037 | |
Alex Light | 679dec1 | 2019-04-08 16:30:14 +0000 | [diff] [blame] | 1038 | ScopedNoUserCodeSuspension snucs(self); |
| 1039 | // From now on we know we cannot get suspended by user-code. |
| 1040 | // NB This does a SuspendCheck (during thread state change) so we need to make |
| 1041 | // sure we don't have the 'suspend_lock' locked here. |
| 1042 | art::ScopedObjectAccess soa(self); |
Alex Light | a4cdd36 | 2019-04-18 09:17:10 -0700 | [diff] [blame] | 1043 | art::Locks::thread_list_lock_->ExclusiveLock(self); |
Alex Light | 679dec1 | 2019-04-08 16:30:14 +0000 | [diff] [blame] | 1044 | jvmtiError err = ERR(INTERNAL); |
| 1045 | if (!ThreadUtil::GetAliveNativeThread(thread, soa, &target, &err)) { |
Alex Light | a4cdd36 | 2019-04-18 09:17:10 -0700 | [diff] [blame] | 1046 | art::Locks::thread_list_lock_->ExclusiveUnlock(self); |
Alex Light | 679dec1 | 2019-04-08 16:30:14 +0000 | [diff] [blame] | 1047 | return err; |
| 1048 | } |
| 1049 | if (target != self) { |
| 1050 | // TODO This is part of the spec but we could easily avoid needing to do it. |
| 1051 | // We would just put all the logic into a sync-checkpoint. |
Alex Light | a4cdd36 | 2019-04-18 09:17:10 -0700 | [diff] [blame] | 1052 | art::Locks::thread_suspend_count_lock_->ExclusiveLock(self); |
Alex Light | 679dec1 | 2019-04-08 16:30:14 +0000 | [diff] [blame] | 1053 | if (target->GetUserCodeSuspendCount() == 0) { |
Alex Light | a4cdd36 | 2019-04-18 09:17:10 -0700 | [diff] [blame] | 1054 | art::Locks::thread_suspend_count_lock_->ExclusiveUnlock(self); |
| 1055 | art::Locks::thread_list_lock_->ExclusiveUnlock(self); |
Alex Light | 679dec1 | 2019-04-08 16:30:14 +0000 | [diff] [blame] | 1056 | return ERR(THREAD_NOT_SUSPENDED); |
Alex Light | c723b81 | 2019-04-08 16:13:24 +0000 | [diff] [blame] | 1057 | } |
Alex Light | a4cdd36 | 2019-04-18 09:17:10 -0700 | [diff] [blame] | 1058 | art::Locks::thread_suspend_count_lock_->ExclusiveUnlock(self); |
Alex Light | 679dec1 | 2019-04-08 16:30:14 +0000 | [diff] [blame] | 1059 | } |
| 1060 | // We hold the user_code_suspension_lock_ so the target thread is staying |
| 1061 | // suspended until we are done (unless it's 'self' in which case we don't care |
| 1062 | // since we aren't going to be returning). |
| 1063 | // TODO We could implement this using a synchronous checkpoint and not bother |
| 1064 | // with any of the suspension stuff. The spec does specifically say to return |
| 1065 | // THREAD_NOT_SUSPENDED though. Find the requested stack frame. |
| 1066 | std::unique_ptr<art::Context> context(art::Context::Create()); |
| 1067 | FindFrameAtDepthVisitor visitor(target, context.get(), depth); |
| 1068 | visitor.WalkStack(); |
| 1069 | if (!visitor.FoundFrame()) { |
Alex Light | a4cdd36 | 2019-04-18 09:17:10 -0700 | [diff] [blame] | 1070 | art::Locks::thread_list_lock_->ExclusiveUnlock(self); |
Alex Light | 679dec1 | 2019-04-08 16:30:14 +0000 | [diff] [blame] | 1071 | return ERR(NO_MORE_FRAMES); |
| 1072 | } |
| 1073 | art::ArtMethod* method = visitor.GetMethod(); |
| 1074 | if (method->IsNative()) { |
Alex Light | a4cdd36 | 2019-04-18 09:17:10 -0700 | [diff] [blame] | 1075 | art::Locks::thread_list_lock_->ExclusiveUnlock(self); |
Alex Light | 679dec1 | 2019-04-08 16:30:14 +0000 | [diff] [blame] | 1076 | return ERR(OPAQUE_FRAME); |
| 1077 | } |
| 1078 | // From here we are sure to succeed. |
| 1079 | bool needs_instrument = false; |
| 1080 | // Get/create a shadow frame |
| 1081 | art::ShadowFrame* shadow_frame = |
| 1082 | visitor.GetOrCreateShadowFrame(&needs_instrument); |
| 1083 | { |
| 1084 | art::WriterMutexLock lk(self, tienv->event_info_mutex_); |
| 1085 | if (LIKELY(!shadow_frame->NeedsNotifyPop())) { |
| 1086 | // Ensure we won't miss exceptions being thrown if we get jit-compiled. We |
| 1087 | // only do this for the first NotifyPopFrame. |
| 1088 | target->IncrementForceInterpreterCount(); |
| 1089 | |
| 1090 | // Mark shadow frame as needs_notify_pop_ |
| 1091 | shadow_frame->SetNotifyPop(true); |
Alex Light | c723b81 | 2019-04-08 16:13:24 +0000 | [diff] [blame] | 1092 | } |
Alex Light | 679dec1 | 2019-04-08 16:30:14 +0000 | [diff] [blame] | 1093 | tienv->notify_frames.insert(shadow_frame); |
| 1094 | } |
| 1095 | // Make sure can we will go to the interpreter and use the shadow frames. |
| 1096 | if (needs_instrument) { |
Alex Light | a4cdd36 | 2019-04-18 09:17:10 -0700 | [diff] [blame] | 1097 | art::FunctionClosure fc([](art::Thread* self) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1098 | DeoptManager::Get()->DeoptimizeThread(self); |
| 1099 | }); |
| 1100 | target->RequestSynchronousCheckpoint(&fc); |
| 1101 | } else { |
| 1102 | art::Locks::thread_list_lock_->ExclusiveUnlock(self); |
Alex Light | 679dec1 | 2019-04-08 16:30:14 +0000 | [diff] [blame] | 1103 | } |
| 1104 | return OK; |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 1105 | } |
| 1106 | |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 1107 | namespace { |
| 1108 | |
| 1109 | enum class NonStandardExitType { |
| 1110 | kPopFrame, |
| 1111 | kForceReturn, |
| 1112 | }; |
| 1113 | |
| 1114 | template<NonStandardExitType kExitType> |
| 1115 | class NonStandardExitFrames { |
| 1116 | public: |
| 1117 | NonStandardExitFrames(art::Thread* self, jvmtiEnv* env, jthread thread) |
| 1118 | REQUIRES(!art::Locks::thread_suspend_count_lock_) |
| 1119 | ACQUIRE_SHARED(art::Locks::mutator_lock_) |
| 1120 | ACQUIRE(art::Locks::thread_list_lock_, art::Locks::user_code_suspension_lock_) |
| 1121 | : snucs_(self) { |
| 1122 | // We keep the user-code-suspend-count lock. |
| 1123 | art::Locks::user_code_suspension_lock_->AssertExclusiveHeld(self); |
| 1124 | |
| 1125 | // From now on we know we cannot get suspended by user-code. |
| 1126 | // NB This does a SuspendCheck (during thread state change) so we need to make sure we don't |
| 1127 | // have the 'suspend_lock' locked here. |
| 1128 | old_state_ = self->TransitionFromSuspendedToRunnable(); |
| 1129 | art::ScopedObjectAccessUnchecked soau(self); |
| 1130 | |
| 1131 | art::Locks::thread_list_lock_->ExclusiveLock(self); |
| 1132 | |
| 1133 | if (!ThreadUtil::GetAliveNativeThread(thread, soau, &target_, &result_)) { |
| 1134 | return; |
| 1135 | } |
| 1136 | { |
| 1137 | art::MutexLock tscl_mu(self, *art::Locks::thread_suspend_count_lock_); |
| 1138 | if (target_ != self && target_->GetUserCodeSuspendCount() == 0) { |
| 1139 | // We cannot be the current thread for this function. |
| 1140 | result_ = ERR(THREAD_NOT_SUSPENDED); |
| 1141 | return; |
| 1142 | } |
| 1143 | } |
| 1144 | JvmtiGlobalTLSData* tls_data = ThreadUtil::GetGlobalTLSData(target_); |
| 1145 | constexpr art::StackVisitor::StackWalkKind kWalkKind = |
| 1146 | art::StackVisitor::StackWalkKind::kIncludeInlinedFrames; |
| 1147 | if (tls_data != nullptr && |
| 1148 | tls_data->disable_pop_frame_depth != JvmtiGlobalTLSData::kNoDisallowedPopFrame && |
| 1149 | tls_data->disable_pop_frame_depth == |
| 1150 | art::StackVisitor::ComputeNumFrames(target_, kWalkKind)) { |
| 1151 | JVMTI_LOG(WARNING, env) << "Disallowing frame pop due to in-progress class-load/prepare. " |
| 1152 | << "Frame at depth " << tls_data->disable_pop_frame_depth << " was " |
| 1153 | << "marked as un-poppable by the jvmti plugin. See b/117615146 for " |
| 1154 | << "more information."; |
| 1155 | result_ = ERR(OPAQUE_FRAME); |
| 1156 | return; |
| 1157 | } |
| 1158 | // We hold the user_code_suspension_lock_ so the target thread is staying suspended until we are |
| 1159 | // done. |
| 1160 | std::unique_ptr<art::Context> context(art::Context::Create()); |
| 1161 | FindFrameAtDepthVisitor final_frame(target_, context.get(), 0); |
| 1162 | FindFrameAtDepthVisitor penultimate_frame(target_, context.get(), 1); |
| 1163 | final_frame.WalkStack(); |
| 1164 | penultimate_frame.WalkStack(); |
| 1165 | |
| 1166 | if (!final_frame.FoundFrame() || !penultimate_frame.FoundFrame()) { |
| 1167 | // Cannot do it if there is only one frame! |
| 1168 | JVMTI_LOG(INFO, env) << "Can not pop final frame off of a stack"; |
| 1169 | result_ = ERR(NO_MORE_FRAMES); |
| 1170 | return; |
| 1171 | } |
| 1172 | |
| 1173 | art::ArtMethod* called_method = final_frame.GetMethod(); |
| 1174 | art::ArtMethod* calling_method = penultimate_frame.GetMethod(); |
| 1175 | if (!CheckFunctions(env, calling_method, called_method)) { |
| 1176 | return; |
| 1177 | } |
| 1178 | DCHECK(!called_method->IsNative()) << called_method->PrettyMethod(); |
| 1179 | |
| 1180 | // From here we are sure to succeed. |
| 1181 | result_ = OK; |
| 1182 | |
| 1183 | // Get/create a shadow frame |
| 1184 | final_frame_ = final_frame.GetOrCreateShadowFrame(&created_final_frame_); |
| 1185 | penultimate_frame_ = |
| 1186 | (calling_method->IsNative() |
| 1187 | ? nullptr |
| 1188 | : penultimate_frame.GetOrCreateShadowFrame(&created_penultimate_frame_)); |
| 1189 | |
| 1190 | final_frame_id_ = final_frame.GetFrameId(); |
| 1191 | penultimate_frame_id_ = penultimate_frame.GetFrameId(); |
| 1192 | |
| 1193 | CHECK_NE(final_frame_, penultimate_frame_) << "Frames at different depths not different!"; |
| 1194 | } |
| 1195 | |
| 1196 | bool CheckFunctions(jvmtiEnv* env, art::ArtMethod* calling, art::ArtMethod* called) |
| 1197 | REQUIRES(art::Locks::thread_list_lock_, art::Locks::user_code_suspension_lock_) |
| 1198 | REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 1199 | |
| 1200 | ~NonStandardExitFrames() RELEASE_SHARED(art::Locks::mutator_lock_) |
| 1201 | REQUIRES(!art::Locks::thread_list_lock_) |
| 1202 | RELEASE(art::Locks::user_code_suspension_lock_) { |
| 1203 | art::Thread* self = art::Thread::Current(); |
| 1204 | DCHECK_EQ(old_state_, art::ThreadState::kNative) |
| 1205 | << "Unexpected thread state on entering PopFrame!"; |
| 1206 | self->TransitionFromRunnableToSuspended(old_state_); |
| 1207 | } |
| 1208 | |
| 1209 | ScopedNoUserCodeSuspension snucs_; |
| 1210 | art::ShadowFrame* final_frame_ GUARDED_BY(art::Locks::user_code_suspension_lock_) = nullptr; |
| 1211 | art::ShadowFrame* penultimate_frame_ GUARDED_BY(art::Locks::user_code_suspension_lock_) = nullptr; |
| 1212 | bool created_final_frame_ GUARDED_BY(art::Locks::user_code_suspension_lock_) = false; |
| 1213 | bool created_penultimate_frame_ GUARDED_BY(art::Locks::user_code_suspension_lock_) = false; |
| 1214 | uint32_t final_frame_id_ GUARDED_BY(art::Locks::user_code_suspension_lock_) = -1; |
| 1215 | uint32_t penultimate_frame_id_ GUARDED_BY(art::Locks::user_code_suspension_lock_) = -1; |
| 1216 | art::Thread* target_ GUARDED_BY(art::Locks::thread_list_lock_) = nullptr; |
| 1217 | art::ThreadState old_state_ = art::ThreadState::kTerminated; |
| 1218 | jvmtiError result_ = ERR(INTERNAL); |
| 1219 | }; |
| 1220 | |
| 1221 | template <> |
| 1222 | bool NonStandardExitFrames<NonStandardExitType::kForceReturn>::CheckFunctions( |
| 1223 | jvmtiEnv* env, art::ArtMethod* calling ATTRIBUTE_UNUSED, art::ArtMethod* called) { |
| 1224 | if (UNLIKELY(called->IsNative())) { |
| 1225 | result_ = ERR(OPAQUE_FRAME); |
| 1226 | JVMTI_LOG(INFO, env) << "Cannot force early return from " << called->PrettyMethod() |
| 1227 | << " because it is native."; |
| 1228 | return false; |
| 1229 | } else { |
| 1230 | return true; |
| 1231 | } |
| 1232 | } |
| 1233 | |
| 1234 | template <> |
| 1235 | bool NonStandardExitFrames<NonStandardExitType::kPopFrame>::CheckFunctions( |
| 1236 | jvmtiEnv* env, art::ArtMethod* calling, art::ArtMethod* called) { |
| 1237 | if (UNLIKELY(calling->IsNative() || called->IsNative())) { |
| 1238 | result_ = ERR(OPAQUE_FRAME); |
| 1239 | JVMTI_LOG(INFO, env) << "Cannot force early return from " << called->PrettyMethod() << " to " |
| 1240 | << calling->PrettyMethod() << " because at least one of them is native."; |
| 1241 | return false; |
| 1242 | } else { |
| 1243 | return true; |
| 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | class SetupMethodExitEvents { |
| 1248 | public: |
| 1249 | SetupMethodExitEvents(art::Thread* self, |
| 1250 | EventHandler* event_handler, |
| 1251 | jthread target) REQUIRES(!art::Locks::mutator_lock_, |
| 1252 | !art::Locks::user_code_suspension_lock_, |
| 1253 | !art::Locks::thread_list_lock_) |
| 1254 | : self_(self), event_handler_(event_handler), target_(target) { |
| 1255 | DCHECK(target != nullptr); |
| 1256 | art::Locks::mutator_lock_->AssertNotHeld(self_); |
| 1257 | art::Locks::user_code_suspension_lock_->AssertNotHeld(self_); |
| 1258 | art::Locks::thread_list_lock_->AssertNotHeld(self_); |
| 1259 | event_handler_->SetInternalEvent( |
| 1260 | target_, ArtJvmtiEvent::kForceEarlyReturnUpdateReturnValue, JVMTI_ENABLE); |
| 1261 | } |
| 1262 | |
| 1263 | ~SetupMethodExitEvents() REQUIRES(!art::Locks::mutator_lock_, |
| 1264 | !art::Locks::user_code_suspension_lock_, |
| 1265 | !art::Locks::thread_list_lock_) { |
| 1266 | art::Locks::mutator_lock_->AssertNotHeld(self_); |
| 1267 | art::Locks::user_code_suspension_lock_->AssertNotHeld(self_); |
| 1268 | art::Locks::thread_list_lock_->AssertNotHeld(self_); |
| 1269 | if (failed_) { |
| 1270 | event_handler_->SetInternalEvent( |
| 1271 | target_, ArtJvmtiEvent::kForceEarlyReturnUpdateReturnValue, JVMTI_DISABLE); |
| 1272 | } |
| 1273 | } |
| 1274 | |
| 1275 | void NotifyFailure() { |
| 1276 | failed_ = true; |
| 1277 | } |
| 1278 | |
| 1279 | private: |
| 1280 | art::Thread* self_; |
| 1281 | EventHandler* event_handler_; |
| 1282 | jthread target_; |
| 1283 | bool failed_ = false; |
| 1284 | }; |
| 1285 | |
| 1286 | template <typename T> |
| 1287 | void AddDelayedMethodExitEvent(EventHandler* handler, art::ShadowFrame* frame, T value) |
| 1288 | REQUIRES_SHARED(art::Locks::mutator_lock_) |
| 1289 | REQUIRES(art::Locks::user_code_suspension_lock_, art::Locks::thread_list_lock_); |
| 1290 | |
| 1291 | template <typename T> |
| 1292 | void AddDelayedMethodExitEvent(EventHandler* handler, art::ShadowFrame* frame, T value) { |
| 1293 | art::JValue val = art::JValue::FromPrimitive(value); |
| 1294 | jvalue jval{ .j = val.GetJ() }; |
| 1295 | handler->AddDelayedNonStandardExitEvent(frame, false, jval); |
| 1296 | } |
| 1297 | |
| 1298 | template <> |
| 1299 | void AddDelayedMethodExitEvent<std::nullptr_t>(EventHandler* handler, |
| 1300 | art::ShadowFrame* frame, |
| 1301 | std::nullptr_t null_val ATTRIBUTE_UNUSED) { |
| 1302 | jvalue jval; |
| 1303 | memset(&jval, 0, sizeof(jval)); |
| 1304 | handler->AddDelayedNonStandardExitEvent(frame, false, jval); |
| 1305 | } |
| 1306 | |
| 1307 | template <> |
| 1308 | void AddDelayedMethodExitEvent<jobject>(EventHandler* handler, |
| 1309 | art::ShadowFrame* frame, |
| 1310 | jobject obj) { |
| 1311 | jvalue jval{ .l = art::Thread::Current()->GetJniEnv()->NewGlobalRef(obj) }; |
| 1312 | handler->AddDelayedNonStandardExitEvent(frame, true, jval); |
| 1313 | } |
| 1314 | |
| 1315 | template <typename T> |
| 1316 | bool ValidReturnType(art::Thread* self, art::ObjPtr<art::mirror::Class> return_type, T value) |
| 1317 | REQUIRES_SHARED(art::Locks::mutator_lock_) |
| 1318 | REQUIRES(art::Locks::user_code_suspension_lock_, art::Locks::thread_list_lock_); |
| 1319 | |
| 1320 | #define SIMPLE_VALID_RETURN_TYPE(type, ...) \ |
| 1321 | template <> \ |
| 1322 | bool ValidReturnType<type>(art::Thread * self ATTRIBUTE_UNUSED, \ |
| 1323 | art::ObjPtr<art::mirror::Class> return_type, \ |
| 1324 | type value ATTRIBUTE_UNUSED) { \ |
| 1325 | static constexpr std::initializer_list<art::Primitive::Type> types{ __VA_ARGS__ }; \ |
| 1326 | return std::find(types.begin(), types.end(), return_type->GetPrimitiveType()) != types.end(); \ |
| 1327 | } |
| 1328 | |
| 1329 | SIMPLE_VALID_RETURN_TYPE(jlong, art::Primitive::kPrimLong); |
| 1330 | SIMPLE_VALID_RETURN_TYPE(jfloat, art::Primitive::kPrimFloat); |
| 1331 | SIMPLE_VALID_RETURN_TYPE(jdouble, art::Primitive::kPrimDouble); |
| 1332 | SIMPLE_VALID_RETURN_TYPE(nullptr_t, art::Primitive::kPrimVoid); |
| 1333 | SIMPLE_VALID_RETURN_TYPE(jint, |
| 1334 | art::Primitive::kPrimInt, |
| 1335 | art::Primitive::kPrimChar, |
| 1336 | art::Primitive::kPrimBoolean, |
| 1337 | art::Primitive::kPrimShort, |
| 1338 | art::Primitive::kPrimByte); |
| 1339 | #undef SIMPLE_VALID_RETURN_TYPE |
| 1340 | |
| 1341 | template <> |
| 1342 | bool ValidReturnType<jobject>(art::Thread* self, |
| 1343 | art::ObjPtr<art::mirror::Class> return_type, |
| 1344 | jobject return_value) { |
| 1345 | if (return_type->IsPrimitive()) { |
| 1346 | return false; |
| 1347 | } |
| 1348 | if (return_value == nullptr) { |
| 1349 | // Null can be used for anything. |
| 1350 | return true; |
| 1351 | } |
| 1352 | return return_type->IsAssignableFrom(self->DecodeJObject(return_value)->GetClass()); |
| 1353 | } |
| 1354 | |
| 1355 | } // namespace |
| 1356 | |
Alex Light | ae45cbb | 2018-10-18 15:49:56 -0700 | [diff] [blame] | 1357 | jvmtiError StackUtil::PopFrame(jvmtiEnv* env, jthread thread) { |
Alex Light | 0aa7a5a | 2018-10-10 15:58:14 +0000 | [diff] [blame] | 1358 | art::Thread* self = art::Thread::Current(); |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 1359 | NonStandardExitFrames<NonStandardExitType::kPopFrame> frames(self, env, thread); |
| 1360 | if (frames.result_ != OK) { |
Alex Light | a4cdd36 | 2019-04-18 09:17:10 -0700 | [diff] [blame] | 1361 | art::Locks::thread_list_lock_->ExclusiveUnlock(self); |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 1362 | return frames.result_; |
Alex Light | 679dec1 | 2019-04-08 16:30:14 +0000 | [diff] [blame] | 1363 | } |
Alex Light | 679dec1 | 2019-04-08 16:30:14 +0000 | [diff] [blame] | 1364 | // Tell the shadow-frame to return immediately and skip all exit events. |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 1365 | frames.penultimate_frame_->SetForceRetryInstruction(true); |
| 1366 | frames.final_frame_->SetForcePopFrame(true); |
| 1367 | frames.final_frame_->SetSkipMethodExitEvents(true); |
| 1368 | if (frames.created_final_frame_ || frames.created_penultimate_frame_) { |
| 1369 | art::FunctionClosure fc([](art::Thread* self) REQUIRES_SHARED(art::Locks::mutator_lock_){ |
Alex Light | a4cdd36 | 2019-04-18 09:17:10 -0700 | [diff] [blame] | 1370 | DeoptManager::Get()->DeoptimizeThread(self); |
| 1371 | }); |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 1372 | frames.target_->RequestSynchronousCheckpoint(&fc); |
Alex Light | a4cdd36 | 2019-04-18 09:17:10 -0700 | [diff] [blame] | 1373 | } else { |
| 1374 | art::Locks::thread_list_lock_->ExclusiveUnlock(self); |
Alex Light | 679dec1 | 2019-04-08 16:30:14 +0000 | [diff] [blame] | 1375 | } |
| 1376 | return OK; |
Alex Light | 0aa7a5a | 2018-10-10 15:58:14 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 1379 | template <typename T> |
| 1380 | jvmtiError |
| 1381 | StackUtil::ForceEarlyReturn(jvmtiEnv* env, EventHandler* event_handler, jthread thread, T value) { |
| 1382 | art::Thread* self = art::Thread::Current(); |
| 1383 | // We don't want to use the null == current-thread idiom since for events (that we use internally |
| 1384 | // to implement force-early-return) we instead have null == all threads. Instead just get the |
| 1385 | // current jthread if needed. |
| 1386 | ScopedLocalRef<jthread> cur_thread(self->GetJniEnv(), nullptr); |
| 1387 | if (UNLIKELY(thread == nullptr)) { |
| 1388 | art::ScopedObjectAccess soa(self); |
| 1389 | cur_thread.reset(soa.AddLocalReference<jthread>(self->GetPeer())); |
| 1390 | thread = cur_thread.get(); |
| 1391 | } |
| 1392 | // This sets up the exit events we implement early return using before we have the locks and |
| 1393 | // thanks to destructor ordering will tear them down if something goes wrong. |
| 1394 | SetupMethodExitEvents smee(self, event_handler, thread); |
| 1395 | NonStandardExitFrames<NonStandardExitType::kForceReturn> frames(self, env, thread); |
| 1396 | if (frames.result_ != OK) { |
| 1397 | smee.NotifyFailure(); |
| 1398 | art::Locks::thread_list_lock_->ExclusiveUnlock(self); |
| 1399 | return frames.result_; |
| 1400 | } else if (!ValidReturnType<T>( |
| 1401 | self, frames.final_frame_->GetMethod()->ResolveReturnType(), value)) { |
| 1402 | smee.NotifyFailure(); |
| 1403 | art::Locks::thread_list_lock_->ExclusiveUnlock(self); |
| 1404 | return ERR(TYPE_MISMATCH); |
| 1405 | } else if (frames.final_frame_->GetForcePopFrame()) { |
| 1406 | // TODO We should really support this. |
| 1407 | smee.NotifyFailure(); |
| 1408 | std::string thread_name; |
| 1409 | frames.target_->GetThreadName(thread_name); |
| 1410 | JVMTI_LOG(WARNING, env) << "PopFrame or force-return already pending on thread " << thread_name; |
| 1411 | art::Locks::thread_list_lock_->ExclusiveUnlock(self); |
| 1412 | return ERR(OPAQUE_FRAME); |
| 1413 | } |
| 1414 | // Tell the shadow-frame to return immediately and skip all exit events. |
| 1415 | frames.final_frame_->SetForcePopFrame(true); |
| 1416 | AddDelayedMethodExitEvent<T>(event_handler, frames.final_frame_, value); |
| 1417 | if (frames.created_final_frame_ || frames.created_penultimate_frame_) { |
| 1418 | art::FunctionClosure fc([](art::Thread* self) REQUIRES_SHARED(art::Locks::mutator_lock_){ |
| 1419 | DeoptManager::Get()->DeoptimizeThread(self); |
| 1420 | }); |
| 1421 | frames.target_->RequestSynchronousCheckpoint(&fc); |
| 1422 | } else { |
| 1423 | art::Locks::thread_list_lock_->ExclusiveUnlock(self); |
| 1424 | } |
| 1425 | return OK; |
| 1426 | } |
| 1427 | |
| 1428 | // Instantiate the ForceEarlyReturn templates. |
| 1429 | template jvmtiError StackUtil::ForceEarlyReturn(jvmtiEnv*, EventHandler*, jthread, jint); |
| 1430 | template jvmtiError StackUtil::ForceEarlyReturn(jvmtiEnv*, EventHandler*, jthread, jlong); |
| 1431 | template jvmtiError StackUtil::ForceEarlyReturn(jvmtiEnv*, EventHandler*, jthread, jfloat); |
| 1432 | template jvmtiError StackUtil::ForceEarlyReturn(jvmtiEnv*, EventHandler*, jthread, jdouble); |
| 1433 | template jvmtiError StackUtil::ForceEarlyReturn(jvmtiEnv*, EventHandler*, jthread, jobject); |
| 1434 | template jvmtiError StackUtil::ForceEarlyReturn(jvmtiEnv*, EventHandler*, jthread, nullptr_t); |
| 1435 | |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 1436 | } // namespace openjdkjvmti |