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> |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 35 | #include <list> |
| 36 | #include <unordered_map> |
| 37 | #include <vector> |
| 38 | |
Andreas Gampe | e5d2398 | 2019-01-08 10:34:26 -0800 | [diff] [blame] | 39 | #include "arch/context.h" |
Andreas Gampe | a1d2f95 | 2017-04-20 22:53:58 -0700 | [diff] [blame] | 40 | #include "art_field-inl.h" |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 41 | #include "art_method-inl.h" |
Andreas Gampe | a1d2f95 | 2017-04-20 22:53:58 -0700 | [diff] [blame] | 42 | #include "art_jvmti.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 43 | #include "art_method-inl.h" |
Andreas Gampe | 6237cd3 | 2017-06-22 22:17:38 -0700 | [diff] [blame] | 44 | #include "barrier.h" |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 45 | #include "base/bit_utils.h" |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 46 | #include "base/enums.h" |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 47 | #include "base/mutex.h" |
Alex Light | a4cdd36 | 2019-04-18 09:17:10 -0700 | [diff] [blame] | 48 | #include "deopt_manager.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 49 | #include "dex/code_item_accessors-inl.h" |
| 50 | #include "dex/dex_file.h" |
| 51 | #include "dex/dex_file_annotations.h" |
| 52 | #include "dex/dex_file_types.h" |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 53 | #include "gc_root.h" |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 54 | #include "handle_scope-inl.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 55 | #include "jni/jni_env_ext.h" |
| 56 | #include "jni/jni_internal.h" |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 57 | #include "mirror/class.h" |
| 58 | #include "mirror/dex_cache.h" |
Andreas Gampe | 373a9b5 | 2017-10-18 09:01:57 -0700 | [diff] [blame] | 59 | #include "nativehelper/scoped_local_ref.h" |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 60 | #include "scoped_thread_state_change-inl.h" |
| 61 | #include "stack.h" |
Alex Light | ae45cbb | 2018-10-18 15:49:56 -0700 | [diff] [blame] | 62 | #include "ti_logging.h" |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 63 | #include "ti_thread.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 64 | #include "thread-current-inl.h" |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 65 | #include "thread_list.h" |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 66 | #include "thread_pool.h" |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 67 | #include "ti_thread.h" |
Andreas Gampe | a1d2f95 | 2017-04-20 22:53:58 -0700 | [diff] [blame] | 68 | #include "well_known_classes.h" |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 69 | |
| 70 | namespace openjdkjvmti { |
| 71 | |
Andreas Gampe | 6db6b4d | 2017-06-12 16:36:33 -0700 | [diff] [blame] | 72 | template <typename FrameFn> |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 73 | struct GetStackTraceVisitor : public art::StackVisitor { |
| 74 | GetStackTraceVisitor(art::Thread* thread_in, |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 75 | size_t start_, |
Andreas Gampe | 6db6b4d | 2017-06-12 16:36:33 -0700 | [diff] [blame] | 76 | size_t stop_, |
| 77 | FrameFn fn_) |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 78 | : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
Andreas Gampe | 6db6b4d | 2017-06-12 16:36:33 -0700 | [diff] [blame] | 79 | fn(fn_), |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 80 | start(start_), |
| 81 | stop(stop_) {} |
Andreas Gampe | 6db6b4d | 2017-06-12 16:36:33 -0700 | [diff] [blame] | 82 | GetStackTraceVisitor(const GetStackTraceVisitor&) = default; |
Andreas Gampe | 44b3174 | 2018-10-01 19:30:57 -0700 | [diff] [blame] | 83 | GetStackTraceVisitor(GetStackTraceVisitor&&) noexcept = default; |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 84 | |
Andreas Gampe | fa6a1b0 | 2018-09-07 08:11:55 -0700 | [diff] [blame] | 85 | bool VisitFrame() override REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 86 | art::ArtMethod* m = GetMethod(); |
| 87 | if (m->IsRuntimeMethod()) { |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | if (start == 0) { |
| 92 | m = m->GetInterfaceMethodIfProxy(art::kRuntimePointerSize); |
Andreas Gampe | 13b2784 | 2016-11-07 16:48:23 -0800 | [diff] [blame] | 93 | jmethodID id = art::jni::EncodeArtMethod(m); |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 94 | |
Andreas Gampe | 2340e3f | 2016-12-12 19:37:19 -0800 | [diff] [blame] | 95 | uint32_t dex_pc = GetDexPc(false); |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 96 | 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] | 97 | |
Andreas Gampe | 2340e3f | 2016-12-12 19:37:19 -0800 | [diff] [blame] | 98 | jvmtiFrameInfo info = { id, dex_location }; |
Andreas Gampe | 6db6b4d | 2017-06-12 16:36:33 -0700 | [diff] [blame] | 99 | fn(info); |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 100 | |
| 101 | if (stop == 1) { |
| 102 | return false; // We're done. |
| 103 | } else if (stop > 0) { |
| 104 | stop--; |
| 105 | } |
| 106 | } else { |
| 107 | start--; |
| 108 | } |
| 109 | |
| 110 | return true; |
| 111 | } |
| 112 | |
Andreas Gampe | 6db6b4d | 2017-06-12 16:36:33 -0700 | [diff] [blame] | 113 | FrameFn fn; |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 114 | size_t start; |
| 115 | size_t stop; |
| 116 | }; |
| 117 | |
Alex Light | 0aa7a5a | 2018-10-10 15:58:14 +0000 | [diff] [blame] | 118 | art::ShadowFrame* FindFrameAtDepthVisitor::GetOrCreateShadowFrame(bool* created_frame) { |
| 119 | art::ShadowFrame* cur = GetCurrentShadowFrame(); |
| 120 | if (cur == nullptr) { |
| 121 | *created_frame = true; |
| 122 | art::ArtMethod* method = GetMethod(); |
| 123 | const uint16_t num_regs = method->DexInstructionData().RegistersSize(); |
| 124 | cur = GetThread()->FindOrCreateDebuggerShadowFrame(GetFrameId(), |
| 125 | num_regs, |
| 126 | method, |
| 127 | GetDexPc()); |
| 128 | DCHECK(cur != nullptr); |
| 129 | } else { |
| 130 | *created_frame = false; |
| 131 | } |
| 132 | return cur; |
| 133 | } |
| 134 | |
Andreas Gampe | 6db6b4d | 2017-06-12 16:36:33 -0700 | [diff] [blame] | 135 | template <typename FrameFn> |
| 136 | GetStackTraceVisitor<FrameFn> MakeStackTraceVisitor(art::Thread* thread_in, |
| 137 | size_t start, |
| 138 | size_t stop, |
| 139 | FrameFn fn) { |
| 140 | return GetStackTraceVisitor<FrameFn>(thread_in, start, stop, fn); |
| 141 | } |
| 142 | |
| 143 | struct GetStackTraceVectorClosure : public art::Closure { |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 144 | public: |
Andreas Gampe | 6db6b4d | 2017-06-12 16:36:33 -0700 | [diff] [blame] | 145 | GetStackTraceVectorClosure(size_t start, size_t stop) |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 146 | : start_input(start), |
| 147 | stop_input(stop), |
| 148 | start_result(0), |
| 149 | stop_result(0) {} |
| 150 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 151 | void Run(art::Thread* self) override REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Andreas Gampe | 6db6b4d | 2017-06-12 16:36:33 -0700 | [diff] [blame] | 152 | auto frames_fn = [&](jvmtiFrameInfo info) { |
| 153 | frames.push_back(info); |
| 154 | }; |
| 155 | auto visitor = MakeStackTraceVisitor(self, start_input, stop_input, frames_fn); |
Andreas Gampe | 6e89776 | 2018-10-16 13:09:32 -0700 | [diff] [blame] | 156 | visitor.WalkStack(/* include_transitions= */ false); |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 157 | |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 158 | start_result = visitor.start; |
| 159 | stop_result = visitor.stop; |
| 160 | } |
| 161 | |
| 162 | const size_t start_input; |
| 163 | const size_t stop_input; |
| 164 | |
| 165 | std::vector<jvmtiFrameInfo> frames; |
| 166 | size_t start_result; |
| 167 | size_t stop_result; |
| 168 | }; |
| 169 | |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 170 | static jvmtiError TranslateFrameVector(const std::vector<jvmtiFrameInfo>& frames, |
| 171 | jint start_depth, |
| 172 | size_t start_result, |
| 173 | jint max_frame_count, |
| 174 | jvmtiFrameInfo* frame_buffer, |
| 175 | jint* count_ptr) { |
| 176 | size_t collected_frames = frames.size(); |
| 177 | |
| 178 | // Assume we're here having collected something. |
| 179 | DCHECK_GT(max_frame_count, 0); |
| 180 | |
| 181 | // Frames from the top. |
| 182 | if (start_depth >= 0) { |
| 183 | if (start_result != 0) { |
| 184 | // Not enough frames. |
| 185 | return ERR(ILLEGAL_ARGUMENT); |
| 186 | } |
| 187 | DCHECK_LE(collected_frames, static_cast<size_t>(max_frame_count)); |
| 188 | if (frames.size() > 0) { |
| 189 | memcpy(frame_buffer, frames.data(), collected_frames * sizeof(jvmtiFrameInfo)); |
| 190 | } |
| 191 | *count_ptr = static_cast<jint>(frames.size()); |
| 192 | return ERR(NONE); |
| 193 | } |
| 194 | |
| 195 | // Frames from the bottom. |
| 196 | if (collected_frames < static_cast<size_t>(-start_depth)) { |
| 197 | return ERR(ILLEGAL_ARGUMENT); |
| 198 | } |
| 199 | |
| 200 | size_t count = std::min(static_cast<size_t>(-start_depth), static_cast<size_t>(max_frame_count)); |
| 201 | memcpy(frame_buffer, |
| 202 | &frames.data()[collected_frames + start_depth], |
| 203 | count * sizeof(jvmtiFrameInfo)); |
| 204 | *count_ptr = static_cast<jint>(count); |
| 205 | return ERR(NONE); |
| 206 | } |
| 207 | |
Andreas Gampe | 850a0fe | 2017-06-12 18:37:19 -0700 | [diff] [blame] | 208 | struct GetStackTraceDirectClosure : public art::Closure { |
| 209 | public: |
| 210 | GetStackTraceDirectClosure(jvmtiFrameInfo* frame_buffer_, size_t start, size_t stop) |
| 211 | : frame_buffer(frame_buffer_), |
| 212 | start_input(start), |
| 213 | stop_input(stop), |
| 214 | index(0) { |
| 215 | DCHECK_GE(start_input, 0u); |
| 216 | } |
| 217 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 218 | void Run(art::Thread* self) override REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Andreas Gampe | 850a0fe | 2017-06-12 18:37:19 -0700 | [diff] [blame] | 219 | auto frames_fn = [&](jvmtiFrameInfo info) { |
| 220 | frame_buffer[index] = info; |
| 221 | ++index; |
| 222 | }; |
| 223 | auto visitor = MakeStackTraceVisitor(self, start_input, stop_input, frames_fn); |
Andreas Gampe | 6e89776 | 2018-10-16 13:09:32 -0700 | [diff] [blame] | 224 | visitor.WalkStack(/* include_transitions= */ false); |
Andreas Gampe | 850a0fe | 2017-06-12 18:37:19 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | jvmtiFrameInfo* frame_buffer; |
| 228 | |
| 229 | const size_t start_input; |
| 230 | const size_t stop_input; |
| 231 | |
| 232 | size_t index = 0; |
| 233 | }; |
| 234 | |
Alex Light | 342b694 | 2019-05-07 15:28:39 -0700 | [diff] [blame] | 235 | jvmtiError StackUtil::GetStackTrace(jvmtiEnv* jvmti_env, |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 236 | jthread java_thread, |
| 237 | jint start_depth, |
| 238 | jint max_frame_count, |
| 239 | jvmtiFrameInfo* frame_buffer, |
| 240 | jint* count_ptr) { |
Andreas Gampe | 28c4a23 | 2017-06-21 21:21:31 -0700 | [diff] [blame] | 241 | // It is not great that we have to hold these locks for so long, but it is necessary to ensure |
| 242 | // that the thread isn't dying on us. |
| 243 | art::ScopedObjectAccess soa(art::Thread::Current()); |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 244 | art::Locks::thread_list_lock_->ExclusiveLock(soa.Self()); |
Andreas Gampe | 28c4a23 | 2017-06-21 21:21:31 -0700 | [diff] [blame] | 245 | |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 246 | art::Thread* thread; |
Alex Light | 7ddc23d | 2017-09-22 15:33:41 -0700 | [diff] [blame] | 247 | jvmtiError thread_error = ERR(INTERNAL); |
| 248 | if (!ThreadUtil::GetAliveNativeThread(java_thread, soa, &thread, &thread_error)) { |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 249 | art::Locks::thread_list_lock_->ExclusiveUnlock(soa.Self()); |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 250 | return thread_error; |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 251 | } |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 252 | DCHECK(thread != nullptr); |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 253 | |
| 254 | art::ThreadState state = thread->GetState(); |
Alex Light | 7ddc23d | 2017-09-22 15:33:41 -0700 | [diff] [blame] | 255 | if (state == art::ThreadState::kStarting || thread->IsStillStarting()) { |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 256 | art::Locks::thread_list_lock_->ExclusiveUnlock(soa.Self()); |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 257 | return ERR(THREAD_NOT_ALIVE); |
| 258 | } |
| 259 | |
| 260 | if (max_frame_count < 0) { |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 261 | art::Locks::thread_list_lock_->ExclusiveUnlock(soa.Self()); |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 262 | return ERR(ILLEGAL_ARGUMENT); |
| 263 | } |
| 264 | if (frame_buffer == nullptr || count_ptr == nullptr) { |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 265 | art::Locks::thread_list_lock_->ExclusiveUnlock(soa.Self()); |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 266 | return ERR(NULL_POINTER); |
| 267 | } |
| 268 | |
| 269 | if (max_frame_count == 0) { |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 270 | art::Locks::thread_list_lock_->ExclusiveUnlock(soa.Self()); |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 271 | *count_ptr = 0; |
| 272 | return ERR(NONE); |
| 273 | } |
| 274 | |
Andreas Gampe | 850a0fe | 2017-06-12 18:37:19 -0700 | [diff] [blame] | 275 | if (start_depth >= 0) { |
| 276 | // Fast path: Regular order of stack trace. Fill into the frame_buffer directly. |
| 277 | GetStackTraceDirectClosure closure(frame_buffer, |
| 278 | static_cast<size_t>(start_depth), |
| 279 | static_cast<size_t>(max_frame_count)); |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 280 | // RequestSynchronousCheckpoint releases the thread_list_lock_ as a part of its execution. |
Alex Light | 318afe6 | 2018-03-22 16:50:10 -0700 | [diff] [blame] | 281 | if (!thread->RequestSynchronousCheckpoint(&closure)) { |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 282 | return ERR(THREAD_NOT_ALIVE); |
| 283 | } |
Andreas Gampe | 850a0fe | 2017-06-12 18:37:19 -0700 | [diff] [blame] | 284 | *count_ptr = static_cast<jint>(closure.index); |
Alex Light | 342b694 | 2019-05-07 15:28:39 -0700 | [diff] [blame] | 285 | if (closure.index == 0) { |
| 286 | JVMTI_LOG(INFO, jvmti_env) << "The stack is not large enough for a start_depth of " |
| 287 | << start_depth << "."; |
Andreas Gampe | 850a0fe | 2017-06-12 18:37:19 -0700 | [diff] [blame] | 288 | return ERR(ILLEGAL_ARGUMENT); |
| 289 | } |
| 290 | return ERR(NONE); |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 291 | } else { |
| 292 | GetStackTraceVectorClosure closure(0, 0); |
| 293 | // RequestSynchronousCheckpoint releases the thread_list_lock_ as a part of its execution. |
Alex Light | 318afe6 | 2018-03-22 16:50:10 -0700 | [diff] [blame] | 294 | if (!thread->RequestSynchronousCheckpoint(&closure)) { |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 295 | return ERR(THREAD_NOT_ALIVE); |
| 296 | } |
| 297 | |
| 298 | return TranslateFrameVector(closure.frames, |
| 299 | start_depth, |
| 300 | closure.start_result, |
| 301 | max_frame_count, |
| 302 | frame_buffer, |
| 303 | count_ptr); |
Andreas Gampe | 850a0fe | 2017-06-12 18:37:19 -0700 | [diff] [blame] | 304 | } |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 305 | } |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 306 | |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 307 | template <typename Data> |
| 308 | struct GetAllStackTracesVectorClosure : public art::Closure { |
Andreas Gampe | 6237cd3 | 2017-06-22 22:17:38 -0700 | [diff] [blame] | 309 | GetAllStackTracesVectorClosure(size_t stop, Data* data_) |
| 310 | : barrier(0), stop_input(stop), data(data_) {} |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 311 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 312 | void Run(art::Thread* thread) override |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 313 | REQUIRES_SHARED(art::Locks::mutator_lock_) |
| 314 | REQUIRES(!data->mutex) { |
| 315 | art::Thread* self = art::Thread::Current(); |
Andreas Gampe | 6237cd3 | 2017-06-22 22:17:38 -0700 | [diff] [blame] | 316 | Work(thread, self); |
| 317 | barrier.Pass(self); |
| 318 | } |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 319 | |
Andreas Gampe | 6237cd3 | 2017-06-22 22:17:38 -0700 | [diff] [blame] | 320 | void Work(art::Thread* thread, art::Thread* self) |
| 321 | REQUIRES_SHARED(art::Locks::mutator_lock_) |
| 322 | REQUIRES(!data->mutex) { |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 323 | // Skip threads that are still starting. |
| 324 | if (thread->IsStillStarting()) { |
| 325 | return; |
| 326 | } |
| 327 | |
| 328 | std::vector<jvmtiFrameInfo>* thread_frames = data->GetFrameStorageFor(self, thread); |
| 329 | if (thread_frames == nullptr) { |
| 330 | return; |
| 331 | } |
| 332 | |
| 333 | // Now collect the data. |
| 334 | auto frames_fn = [&](jvmtiFrameInfo info) { |
| 335 | thread_frames->push_back(info); |
| 336 | }; |
| 337 | auto visitor = MakeStackTraceVisitor(thread, 0u, stop_input, frames_fn); |
Andreas Gampe | 6e89776 | 2018-10-16 13:09:32 -0700 | [diff] [blame] | 338 | visitor.WalkStack(/* include_transitions= */ false); |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Andreas Gampe | 6237cd3 | 2017-06-22 22:17:38 -0700 | [diff] [blame] | 341 | art::Barrier barrier; |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 342 | const size_t stop_input; |
| 343 | Data* data; |
| 344 | }; |
| 345 | |
Andreas Gampe | 6237cd3 | 2017-06-22 22:17:38 -0700 | [diff] [blame] | 346 | template <typename Data> |
Andreas Gampe | 6baa1c9 | 2018-05-25 16:17:49 -0700 | [diff] [blame] | 347 | static void RunCheckpointAndWait(Data* data, size_t max_frame_count) |
| 348 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 349 | // Note: requires the mutator lock as the checkpoint requires the mutator lock. |
Andreas Gampe | 6237cd3 | 2017-06-22 22:17:38 -0700 | [diff] [blame] | 350 | GetAllStackTracesVectorClosure<Data> closure(max_frame_count, data); |
| 351 | size_t barrier_count = art::Runtime::Current()->GetThreadList()->RunCheckpoint(&closure, nullptr); |
| 352 | if (barrier_count == 0) { |
| 353 | return; |
| 354 | } |
| 355 | art::Thread* self = art::Thread::Current(); |
| 356 | art::ScopedThreadStateChange tsc(self, art::ThreadState::kWaitingForCheckPointsToRun); |
| 357 | closure.barrier.Increment(self, barrier_count); |
| 358 | } |
| 359 | |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 360 | jvmtiError StackUtil::GetAllStackTraces(jvmtiEnv* env, |
| 361 | jint max_frame_count, |
| 362 | jvmtiStackInfo** stack_info_ptr, |
| 363 | jint* thread_count_ptr) { |
| 364 | if (max_frame_count < 0) { |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 365 | return ERR(ILLEGAL_ARGUMENT); |
| 366 | } |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 367 | if (stack_info_ptr == nullptr || thread_count_ptr == nullptr) { |
| 368 | return ERR(NULL_POINTER); |
| 369 | } |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 370 | |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 371 | struct AllStackTracesData { |
| 372 | AllStackTracesData() : mutex("GetAllStackTraces", art::LockLevel::kAbortLock) {} |
| 373 | ~AllStackTracesData() { |
| 374 | JNIEnv* jni_env = art::Thread::Current()->GetJniEnv(); |
| 375 | for (jthread global_thread_ref : thread_peers) { |
| 376 | jni_env->DeleteGlobalRef(global_thread_ref); |
| 377 | } |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 378 | } |
| 379 | |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 380 | std::vector<jvmtiFrameInfo>* GetFrameStorageFor(art::Thread* self, art::Thread* thread) |
| 381 | REQUIRES_SHARED(art::Locks::mutator_lock_) |
| 382 | REQUIRES(!mutex) { |
| 383 | art::MutexLock mu(self, mutex); |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 384 | |
| 385 | threads.push_back(thread); |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 386 | |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 387 | jthread peer = art::Runtime::Current()->GetJavaVM()->AddGlobalRef( |
| 388 | self, thread->GetPeerFromOtherThread()); |
| 389 | thread_peers.push_back(peer); |
| 390 | |
Andreas Gampe | ad9173d | 2017-06-22 16:33:08 -0700 | [diff] [blame] | 391 | frames.emplace_back(new std::vector<jvmtiFrameInfo>()); |
| 392 | return frames.back().get(); |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | art::Mutex mutex; |
| 396 | |
| 397 | // Storage. Only access directly after completion. |
| 398 | |
| 399 | std::vector<art::Thread*> threads; |
| 400 | // "thread_peers" contains global references to their peers. |
| 401 | std::vector<jthread> thread_peers; |
| 402 | |
Andreas Gampe | ad9173d | 2017-06-22 16:33:08 -0700 | [diff] [blame] | 403 | std::vector<std::unique_ptr<std::vector<jvmtiFrameInfo>>> frames; |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 404 | }; |
| 405 | |
| 406 | AllStackTracesData data; |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 407 | art::Thread* current = art::Thread::Current(); |
Andreas Gampe | 6baa1c9 | 2018-05-25 16:17:49 -0700 | [diff] [blame] | 408 | { |
| 409 | art::ScopedObjectAccess soa(current); |
| 410 | RunCheckpointAndWait(&data, static_cast<size_t>(max_frame_count)); |
| 411 | } |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 412 | |
| 413 | // Convert the data into our output format. |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 414 | |
| 415 | // Note: we use an array of jvmtiStackInfo for convenience. The spec says we need to |
| 416 | // allocate one big chunk for this and the actual frames, which means we need |
| 417 | // to either be conservative or rearrange things later (the latter is implemented). |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 418 | std::unique_ptr<jvmtiStackInfo[]> stack_info_array(new jvmtiStackInfo[data.frames.size()]); |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 419 | std::vector<std::unique_ptr<jvmtiFrameInfo[]>> frame_infos; |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 420 | frame_infos.reserve(data.frames.size()); |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 421 | |
| 422 | // Now run through and add data for each thread. |
| 423 | size_t sum_frames = 0; |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 424 | for (size_t index = 0; index < data.frames.size(); ++index) { |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 425 | jvmtiStackInfo& stack_info = stack_info_array.get()[index]; |
| 426 | memset(&stack_info, 0, sizeof(jvmtiStackInfo)); |
| 427 | |
Andreas Gampe | ad9173d | 2017-06-22 16:33:08 -0700 | [diff] [blame] | 428 | const std::vector<jvmtiFrameInfo>& thread_frames = *data.frames[index].get(); |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 429 | |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 430 | // 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] | 431 | stack_info.thread = nullptr; |
| 432 | stack_info.state = JVMTI_THREAD_STATE_SUSPENDED; |
| 433 | |
| 434 | size_t collected_frames = thread_frames.size(); |
| 435 | if (max_frame_count == 0 || collected_frames == 0) { |
| 436 | stack_info.frame_count = 0; |
| 437 | stack_info.frame_buffer = nullptr; |
| 438 | continue; |
| 439 | } |
| 440 | DCHECK_LE(collected_frames, static_cast<size_t>(max_frame_count)); |
| 441 | |
| 442 | jvmtiFrameInfo* frame_info = new jvmtiFrameInfo[collected_frames]; |
| 443 | frame_infos.emplace_back(frame_info); |
| 444 | |
| 445 | jint count; |
| 446 | jvmtiError translate_result = TranslateFrameVector(thread_frames, |
| 447 | 0, |
| 448 | 0, |
| 449 | static_cast<jint>(collected_frames), |
| 450 | frame_info, |
| 451 | &count); |
| 452 | DCHECK(translate_result == JVMTI_ERROR_NONE); |
| 453 | stack_info.frame_count = static_cast<jint>(collected_frames); |
| 454 | stack_info.frame_buffer = frame_info; |
| 455 | sum_frames += static_cast<size_t>(count); |
| 456 | } |
| 457 | |
| 458 | // No errors, yet. Now put it all into an output buffer. |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 459 | 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] | 460 | alignof(jvmtiFrameInfo)); |
| 461 | size_t chunk_size = rounded_stack_info_size + sum_frames * sizeof(jvmtiFrameInfo); |
| 462 | unsigned char* chunk_data; |
| 463 | jvmtiError alloc_result = env->Allocate(chunk_size, &chunk_data); |
| 464 | if (alloc_result != ERR(NONE)) { |
| 465 | return alloc_result; |
| 466 | } |
| 467 | |
| 468 | jvmtiStackInfo* stack_info = reinterpret_cast<jvmtiStackInfo*>(chunk_data); |
| 469 | // First copy in all the basic data. |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 470 | memcpy(stack_info, stack_info_array.get(), sizeof(jvmtiStackInfo) * data.frames.size()); |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 471 | |
| 472 | // Now copy the frames and fix up the pointers. |
| 473 | jvmtiFrameInfo* frame_info = reinterpret_cast<jvmtiFrameInfo*>( |
| 474 | chunk_data + rounded_stack_info_size); |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 475 | for (size_t i = 0; i < data.frames.size(); ++i) { |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 476 | jvmtiStackInfo& old_stack_info = stack_info_array.get()[i]; |
| 477 | jvmtiStackInfo& new_stack_info = stack_info[i]; |
| 478 | |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 479 | // Translate the global ref into a local ref. |
| 480 | new_stack_info.thread = |
| 481 | static_cast<JNIEnv*>(current->GetJniEnv())->NewLocalRef(data.thread_peers[i]); |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 482 | |
| 483 | if (old_stack_info.frame_count > 0) { |
| 484 | // Only copy when there's data - leave the nullptr alone. |
| 485 | size_t frames_size = static_cast<size_t>(old_stack_info.frame_count) * sizeof(jvmtiFrameInfo); |
| 486 | memcpy(frame_info, old_stack_info.frame_buffer, frames_size); |
| 487 | new_stack_info.frame_buffer = frame_info; |
| 488 | frame_info += old_stack_info.frame_count; |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | *stack_info_ptr = stack_info; |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 493 | *thread_count_ptr = static_cast<jint>(data.frames.size()); |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 494 | |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 495 | return ERR(NONE); |
| 496 | } |
| 497 | |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 498 | jvmtiError StackUtil::GetThreadListStackTraces(jvmtiEnv* env, |
| 499 | jint thread_count, |
| 500 | const jthread* thread_list, |
| 501 | jint max_frame_count, |
| 502 | jvmtiStackInfo** stack_info_ptr) { |
| 503 | if (max_frame_count < 0) { |
| 504 | return ERR(ILLEGAL_ARGUMENT); |
| 505 | } |
| 506 | if (thread_count < 0) { |
| 507 | return ERR(ILLEGAL_ARGUMENT); |
| 508 | } |
| 509 | if (thread_count == 0) { |
| 510 | *stack_info_ptr = nullptr; |
| 511 | return ERR(NONE); |
| 512 | } |
Alex Light | 19a7d4f | 2018-03-23 10:05:49 -0700 | [diff] [blame] | 513 | if (thread_list == nullptr || stack_info_ptr == nullptr) { |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 514 | return ERR(NULL_POINTER); |
| 515 | } |
| 516 | |
| 517 | art::Thread* current = art::Thread::Current(); |
| 518 | art::ScopedObjectAccess soa(current); // Now we know we have the shared lock. |
| 519 | |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 520 | struct SelectStackTracesData { |
| 521 | SelectStackTracesData() : mutex("GetSelectStackTraces", art::LockLevel::kAbortLock) {} |
| 522 | |
| 523 | std::vector<jvmtiFrameInfo>* GetFrameStorageFor(art::Thread* self, art::Thread* thread) |
| 524 | REQUIRES_SHARED(art::Locks::mutator_lock_) |
| 525 | REQUIRES(!mutex) { |
| 526 | art::ObjPtr<art::mirror::Object> peer = thread->GetPeerFromOtherThread(); |
| 527 | for (size_t index = 0; index != handles.size(); ++index) { |
| 528 | if (peer == handles[index].Get()) { |
| 529 | // Found the thread. |
| 530 | art::MutexLock mu(self, mutex); |
| 531 | |
| 532 | threads.push_back(thread); |
| 533 | thread_list_indices.push_back(index); |
| 534 | |
Andreas Gampe | ad9173d | 2017-06-22 16:33:08 -0700 | [diff] [blame] | 535 | frames.emplace_back(new std::vector<jvmtiFrameInfo>()); |
| 536 | return frames.back().get(); |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 537 | } |
| 538 | } |
| 539 | return nullptr; |
| 540 | } |
| 541 | |
| 542 | art::Mutex mutex; |
| 543 | |
| 544 | // Selection data. |
| 545 | |
| 546 | std::vector<art::Handle<art::mirror::Object>> handles; |
| 547 | |
| 548 | // Storage. Only access directly after completion. |
| 549 | |
| 550 | std::vector<art::Thread*> threads; |
| 551 | std::vector<size_t> thread_list_indices; |
| 552 | |
Andreas Gampe | ad9173d | 2017-06-22 16:33:08 -0700 | [diff] [blame] | 553 | std::vector<std::unique_ptr<std::vector<jvmtiFrameInfo>>> frames; |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 554 | }; |
| 555 | |
| 556 | SelectStackTracesData data; |
| 557 | |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 558 | // Decode all threads to raw pointers. Put them into a handle scope to avoid any moving GC bugs. |
| 559 | art::VariableSizedHandleScope hs(current); |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 560 | for (jint i = 0; i != thread_count; ++i) { |
| 561 | if (thread_list[i] == nullptr) { |
| 562 | return ERR(INVALID_THREAD); |
| 563 | } |
| 564 | if (!soa.Env()->IsInstanceOf(thread_list[i], art::WellKnownClasses::java_lang_Thread)) { |
| 565 | return ERR(INVALID_THREAD); |
| 566 | } |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 567 | 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] | 568 | } |
| 569 | |
Andreas Gampe | 6237cd3 | 2017-06-22 22:17:38 -0700 | [diff] [blame] | 570 | RunCheckpointAndWait(&data, static_cast<size_t>(max_frame_count)); |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 571 | |
| 572 | // Convert the data into our output format. |
| 573 | |
| 574 | // Note: we use an array of jvmtiStackInfo for convenience. The spec says we need to |
| 575 | // allocate one big chunk for this and the actual frames, which means we need |
| 576 | // to either be conservative or rearrange things later (the latter is implemented). |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 577 | std::unique_ptr<jvmtiStackInfo[]> stack_info_array(new jvmtiStackInfo[data.frames.size()]); |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 578 | std::vector<std::unique_ptr<jvmtiFrameInfo[]>> frame_infos; |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 579 | frame_infos.reserve(data.frames.size()); |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 580 | |
| 581 | // Now run through and add data for each thread. |
| 582 | size_t sum_frames = 0; |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 583 | for (size_t index = 0; index < data.frames.size(); ++index) { |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 584 | jvmtiStackInfo& stack_info = stack_info_array.get()[index]; |
| 585 | memset(&stack_info, 0, sizeof(jvmtiStackInfo)); |
| 586 | |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 587 | art::Thread* self = data.threads[index]; |
Andreas Gampe | ad9173d | 2017-06-22 16:33:08 -0700 | [diff] [blame] | 588 | const std::vector<jvmtiFrameInfo>& thread_frames = *data.frames[index].get(); |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 589 | |
| 590 | // For the time being, set the thread to null. We don't have good ScopedLocalRef |
| 591 | // infrastructure. |
Nicolas Geoffray | ffc8cad | 2017-02-10 10:59:22 +0000 | [diff] [blame] | 592 | DCHECK(self->GetPeerFromOtherThread() != nullptr); |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 593 | stack_info.thread = nullptr; |
| 594 | stack_info.state = JVMTI_THREAD_STATE_SUSPENDED; |
| 595 | |
| 596 | size_t collected_frames = thread_frames.size(); |
| 597 | if (max_frame_count == 0 || collected_frames == 0) { |
| 598 | stack_info.frame_count = 0; |
| 599 | stack_info.frame_buffer = nullptr; |
| 600 | continue; |
| 601 | } |
| 602 | DCHECK_LE(collected_frames, static_cast<size_t>(max_frame_count)); |
| 603 | |
| 604 | jvmtiFrameInfo* frame_info = new jvmtiFrameInfo[collected_frames]; |
| 605 | frame_infos.emplace_back(frame_info); |
| 606 | |
| 607 | jint count; |
| 608 | jvmtiError translate_result = TranslateFrameVector(thread_frames, |
| 609 | 0, |
| 610 | 0, |
| 611 | static_cast<jint>(collected_frames), |
| 612 | frame_info, |
| 613 | &count); |
| 614 | DCHECK(translate_result == JVMTI_ERROR_NONE); |
| 615 | stack_info.frame_count = static_cast<jint>(collected_frames); |
| 616 | stack_info.frame_buffer = frame_info; |
| 617 | sum_frames += static_cast<size_t>(count); |
| 618 | } |
| 619 | |
| 620 | // No errors, yet. Now put it all into an output buffer. Note that this is not frames.size(), |
| 621 | // potentially. |
| 622 | size_t rounded_stack_info_size = art::RoundUp(sizeof(jvmtiStackInfo) * thread_count, |
| 623 | alignof(jvmtiFrameInfo)); |
| 624 | size_t chunk_size = rounded_stack_info_size + sum_frames * sizeof(jvmtiFrameInfo); |
| 625 | unsigned char* chunk_data; |
| 626 | jvmtiError alloc_result = env->Allocate(chunk_size, &chunk_data); |
| 627 | if (alloc_result != ERR(NONE)) { |
| 628 | return alloc_result; |
| 629 | } |
| 630 | |
| 631 | jvmtiStackInfo* stack_info = reinterpret_cast<jvmtiStackInfo*>(chunk_data); |
| 632 | jvmtiFrameInfo* frame_info = reinterpret_cast<jvmtiFrameInfo*>( |
| 633 | chunk_data + rounded_stack_info_size); |
| 634 | |
| 635 | for (size_t i = 0; i < static_cast<size_t>(thread_count); ++i) { |
| 636 | // Check whether we found a running thread for this. |
| 637 | // Note: For simplicity, and with the expectation that the list is usually small, use a simple |
| 638 | // search. (The list is *not* sorted!) |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 639 | auto it = std::find(data.thread_list_indices.begin(), data.thread_list_indices.end(), i); |
| 640 | if (it == data.thread_list_indices.end()) { |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 641 | // No native thread. Must be new or dead. We need to fill out the stack info now. |
| 642 | // (Need to read the Java "started" field to know whether this is starting or terminated.) |
| 643 | art::ObjPtr<art::mirror::Object> peer = soa.Decode<art::mirror::Object>(thread_list[i]); |
| 644 | art::ObjPtr<art::mirror::Class> klass = peer->GetClass(); |
| 645 | art::ArtField* started_field = klass->FindDeclaredInstanceField("started", "Z"); |
| 646 | CHECK(started_field != nullptr); |
| 647 | bool started = started_field->GetBoolean(peer) != 0; |
| 648 | constexpr jint kStartedState = JVMTI_JAVA_LANG_THREAD_STATE_NEW; |
| 649 | constexpr jint kTerminatedState = JVMTI_THREAD_STATE_TERMINATED | |
| 650 | JVMTI_JAVA_LANG_THREAD_STATE_TERMINATED; |
| 651 | stack_info[i].thread = reinterpret_cast<JNIEnv*>(soa.Env())->NewLocalRef(thread_list[i]); |
| 652 | stack_info[i].state = started ? kTerminatedState : kStartedState; |
| 653 | stack_info[i].frame_count = 0; |
| 654 | stack_info[i].frame_buffer = nullptr; |
| 655 | } else { |
| 656 | // Had a native thread and frames. |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 657 | size_t f_index = it - data.thread_list_indices.begin(); |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 658 | |
| 659 | jvmtiStackInfo& old_stack_info = stack_info_array.get()[f_index]; |
| 660 | jvmtiStackInfo& new_stack_info = stack_info[i]; |
| 661 | |
| 662 | memcpy(&new_stack_info, &old_stack_info, sizeof(jvmtiStackInfo)); |
| 663 | new_stack_info.thread = reinterpret_cast<JNIEnv*>(soa.Env())->NewLocalRef(thread_list[i]); |
| 664 | if (old_stack_info.frame_count > 0) { |
| 665 | // Only copy when there's data - leave the nullptr alone. |
| 666 | size_t frames_size = |
| 667 | static_cast<size_t>(old_stack_info.frame_count) * sizeof(jvmtiFrameInfo); |
| 668 | memcpy(frame_info, old_stack_info.frame_buffer, frames_size); |
| 669 | new_stack_info.frame_buffer = frame_info; |
| 670 | frame_info += old_stack_info.frame_count; |
| 671 | } |
| 672 | } |
| 673 | } |
| 674 | |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame] | 675 | *stack_info_ptr = stack_info; |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 676 | |
| 677 | return ERR(NONE); |
| 678 | } |
| 679 | |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 680 | struct GetFrameCountClosure : public art::Closure { |
| 681 | public: |
| 682 | GetFrameCountClosure() : count(0) {} |
| 683 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 684 | void Run(art::Thread* self) override REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Andreas Gampe | c7d878d | 2018-11-19 18:42:06 +0000 | [diff] [blame] | 685 | // This is not StackVisitor::ComputeNumFrames, as runtime methods and transitions must not be |
| 686 | // counted. |
| 687 | art::StackVisitor::WalkStack( |
| 688 | [&](const art::StackVisitor* stack_visitor) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 689 | art::ArtMethod* m = stack_visitor->GetMethod(); |
| 690 | if (m != nullptr && !m->IsRuntimeMethod()) { |
| 691 | count++; |
| 692 | } |
| 693 | return true; |
| 694 | }, |
| 695 | self, |
| 696 | /* context= */ nullptr, |
| 697 | art::StackVisitor::StackWalkKind::kIncludeInlinedFrames); |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | size_t count; |
| 701 | }; |
| 702 | |
| 703 | jvmtiError StackUtil::GetFrameCount(jvmtiEnv* env ATTRIBUTE_UNUSED, |
| 704 | jthread java_thread, |
| 705 | jint* count_ptr) { |
Andreas Gampe | 28c4a23 | 2017-06-21 21:21:31 -0700 | [diff] [blame] | 706 | // It is not great that we have to hold these locks for so long, but it is necessary to ensure |
| 707 | // that the thread isn't dying on us. |
| 708 | art::ScopedObjectAccess soa(art::Thread::Current()); |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 709 | art::Locks::thread_list_lock_->ExclusiveLock(soa.Self()); |
Andreas Gampe | 28c4a23 | 2017-06-21 21:21:31 -0700 | [diff] [blame] | 710 | |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 711 | art::Thread* thread; |
Alex Light | 7ddc23d | 2017-09-22 15:33:41 -0700 | [diff] [blame] | 712 | jvmtiError thread_error = ERR(INTERNAL); |
| 713 | if (!ThreadUtil::GetAliveNativeThread(java_thread, soa, &thread, &thread_error)) { |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 714 | art::Locks::thread_list_lock_->ExclusiveUnlock(soa.Self()); |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 715 | return thread_error; |
| 716 | } |
Alex Light | 7ddc23d | 2017-09-22 15:33:41 -0700 | [diff] [blame] | 717 | |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 718 | DCHECK(thread != nullptr); |
Alex Light | 7ddc23d | 2017-09-22 15:33:41 -0700 | [diff] [blame] | 719 | art::ThreadState state = thread->GetState(); |
| 720 | if (state == art::ThreadState::kStarting || thread->IsStillStarting()) { |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 721 | art::Locks::thread_list_lock_->ExclusiveUnlock(soa.Self()); |
Alex Light | 7ddc23d | 2017-09-22 15:33:41 -0700 | [diff] [blame] | 722 | return ERR(THREAD_NOT_ALIVE); |
| 723 | } |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 724 | |
| 725 | if (count_ptr == nullptr) { |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 726 | art::Locks::thread_list_lock_->ExclusiveUnlock(soa.Self()); |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 727 | return ERR(NULL_POINTER); |
| 728 | } |
| 729 | |
| 730 | GetFrameCountClosure closure; |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 731 | // RequestSynchronousCheckpoint releases the thread_list_lock_ as a part of its execution. |
Alex Light | 318afe6 | 2018-03-22 16:50:10 -0700 | [diff] [blame] | 732 | if (!thread->RequestSynchronousCheckpoint(&closure)) { |
Alex Light | 7ddc23d | 2017-09-22 15:33:41 -0700 | [diff] [blame] | 733 | return ERR(THREAD_NOT_ALIVE); |
| 734 | } |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 735 | |
| 736 | *count_ptr = closure.count; |
| 737 | return ERR(NONE); |
| 738 | } |
| 739 | |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 740 | struct GetLocationClosure : public art::Closure { |
| 741 | public: |
| 742 | explicit GetLocationClosure(size_t n_in) : n(n_in), method(nullptr), dex_pc(0) {} |
| 743 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 744 | void Run(art::Thread* self) override REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Andreas Gampe | c7d878d | 2018-11-19 18:42:06 +0000 | [diff] [blame] | 745 | // Walks up the stack 'n' callers. |
| 746 | size_t count = 0u; |
| 747 | art::StackVisitor::WalkStack( |
| 748 | [&](const art::StackVisitor* stack_visitor) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 749 | art::ArtMethod* m = stack_visitor->GetMethod(); |
| 750 | if (m != nullptr && !m->IsRuntimeMethod()) { |
| 751 | DCHECK(method == nullptr); |
| 752 | if (count == n) { |
| 753 | method = m; |
| 754 | dex_pc = stack_visitor->GetDexPc(/*abort_on_failure=*/false); |
| 755 | return false; |
| 756 | } |
| 757 | count++; |
| 758 | } |
| 759 | return true; |
| 760 | }, |
| 761 | self, |
| 762 | /* context= */ nullptr, |
| 763 | art::StackVisitor::StackWalkKind::kIncludeInlinedFrames); |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | const size_t n; |
| 767 | art::ArtMethod* method; |
| 768 | uint32_t dex_pc; |
| 769 | }; |
| 770 | |
| 771 | jvmtiError StackUtil::GetFrameLocation(jvmtiEnv* env ATTRIBUTE_UNUSED, |
| 772 | jthread java_thread, |
| 773 | jint depth, |
| 774 | jmethodID* method_ptr, |
| 775 | jlocation* location_ptr) { |
Andreas Gampe | 28c4a23 | 2017-06-21 21:21:31 -0700 | [diff] [blame] | 776 | // It is not great that we have to hold these locks for so long, but it is necessary to ensure |
| 777 | // that the thread isn't dying on us. |
| 778 | art::ScopedObjectAccess soa(art::Thread::Current()); |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 779 | art::Locks::thread_list_lock_->ExclusiveLock(soa.Self()); |
Andreas Gampe | 28c4a23 | 2017-06-21 21:21:31 -0700 | [diff] [blame] | 780 | |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 781 | art::Thread* thread; |
Alex Light | 7ddc23d | 2017-09-22 15:33:41 -0700 | [diff] [blame] | 782 | jvmtiError thread_error = ERR(INTERNAL); |
| 783 | if (!ThreadUtil::GetAliveNativeThread(java_thread, soa, &thread, &thread_error)) { |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 784 | art::Locks::thread_list_lock_->ExclusiveUnlock(soa.Self()); |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 785 | return thread_error; |
| 786 | } |
| 787 | DCHECK(thread != nullptr); |
| 788 | |
Alex Light | 7ddc23d | 2017-09-22 15:33:41 -0700 | [diff] [blame] | 789 | art::ThreadState state = thread->GetState(); |
| 790 | if (state == art::ThreadState::kStarting || thread->IsStillStarting()) { |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 791 | art::Locks::thread_list_lock_->ExclusiveUnlock(soa.Self()); |
Alex Light | 7ddc23d | 2017-09-22 15:33:41 -0700 | [diff] [blame] | 792 | return ERR(THREAD_NOT_ALIVE); |
| 793 | } |
| 794 | |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 795 | if (depth < 0) { |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 796 | art::Locks::thread_list_lock_->ExclusiveUnlock(soa.Self()); |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 797 | return ERR(ILLEGAL_ARGUMENT); |
| 798 | } |
| 799 | if (method_ptr == nullptr || location_ptr == nullptr) { |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 800 | art::Locks::thread_list_lock_->ExclusiveUnlock(soa.Self()); |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 801 | return ERR(NULL_POINTER); |
| 802 | } |
| 803 | |
| 804 | GetLocationClosure closure(static_cast<size_t>(depth)); |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 805 | // RequestSynchronousCheckpoint releases the thread_list_lock_ as a part of its execution. |
Alex Light | 318afe6 | 2018-03-22 16:50:10 -0700 | [diff] [blame] | 806 | if (!thread->RequestSynchronousCheckpoint(&closure)) { |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 807 | return ERR(THREAD_NOT_ALIVE); |
| 808 | } |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 809 | |
| 810 | if (closure.method == nullptr) { |
| 811 | return ERR(NO_MORE_FRAMES); |
| 812 | } |
| 813 | |
| 814 | *method_ptr = art::jni::EncodeArtMethod(closure.method); |
Alex Light | 3dea212 | 2017-10-11 15:56:48 +0000 | [diff] [blame] | 815 | if (closure.method->IsNative() || closure.method->IsProxyMethod()) { |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 816 | *location_ptr = -1; |
| 817 | } else { |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 818 | if (closure.dex_pc == art::dex::kDexNoIndex) { |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 819 | return ERR(INTERNAL); |
| 820 | } |
| 821 | *location_ptr = static_cast<jlocation>(closure.dex_pc); |
| 822 | } |
| 823 | |
| 824 | return ERR(NONE); |
| 825 | } |
| 826 | |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 827 | struct MonitorVisitor : public art::StackVisitor, public art::SingleRootVisitor { |
| 828 | // We need a context because VisitLocks needs it retrieve the monitor objects. |
| 829 | explicit MonitorVisitor(art::Thread* thread) |
| 830 | REQUIRES_SHARED(art::Locks::mutator_lock_) |
| 831 | : art::StackVisitor(thread, |
| 832 | art::Context::Create(), |
| 833 | art::StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 834 | hs(art::Thread::Current()), |
| 835 | current_stack_depth(0) {} |
| 836 | |
| 837 | ~MonitorVisitor() { |
| 838 | delete context_; |
| 839 | } |
| 840 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 841 | bool VisitFrame() override REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 842 | art::Locks::mutator_lock_->AssertSharedHeld(art::Thread::Current()); |
| 843 | if (!GetMethod()->IsRuntimeMethod()) { |
| 844 | art::Monitor::VisitLocks(this, AppendOwnedMonitors, this); |
| 845 | ++current_stack_depth; |
| 846 | } |
| 847 | return true; |
| 848 | } |
| 849 | |
Vladimir Marko | f52d92f | 2019-03-29 12:33:02 +0000 | [diff] [blame] | 850 | static void AppendOwnedMonitors(art::ObjPtr<art::mirror::Object> owned_monitor, void* arg) |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 851 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 852 | art::Locks::mutator_lock_->AssertSharedHeld(art::Thread::Current()); |
| 853 | MonitorVisitor* visitor = reinterpret_cast<MonitorVisitor*>(arg); |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 854 | // Filter out duplicates. |
| 855 | for (const art::Handle<art::mirror::Object>& monitor : visitor->monitors) { |
Vladimir Marko | f52d92f | 2019-03-29 12:33:02 +0000 | [diff] [blame] | 856 | if (monitor.Get() == owned_monitor) { |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 857 | return; |
| 858 | } |
| 859 | } |
Vladimir Marko | f52d92f | 2019-03-29 12:33:02 +0000 | [diff] [blame] | 860 | visitor->monitors.push_back(visitor->hs.NewHandle(owned_monitor)); |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 861 | visitor->stack_depths.push_back(visitor->current_stack_depth); |
| 862 | } |
| 863 | |
| 864 | void VisitRoot(art::mirror::Object* obj, const art::RootInfo& info ATTRIBUTE_UNUSED) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 865 | override REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 866 | for (const art::Handle<art::mirror::Object>& m : monitors) { |
| 867 | if (m.Get() == obj) { |
| 868 | return; |
| 869 | } |
| 870 | } |
| 871 | monitors.push_back(hs.NewHandle(obj)); |
| 872 | stack_depths.push_back(-1); |
| 873 | } |
| 874 | |
| 875 | art::VariableSizedHandleScope hs; |
| 876 | jint current_stack_depth; |
| 877 | std::vector<art::Handle<art::mirror::Object>> monitors; |
| 878 | std::vector<jint> stack_depths; |
| 879 | }; |
| 880 | |
| 881 | template<typename Fn> |
| 882 | struct MonitorInfoClosure : public art::Closure { |
| 883 | public: |
Alex Light | 318afe6 | 2018-03-22 16:50:10 -0700 | [diff] [blame] | 884 | explicit MonitorInfoClosure(Fn handle_results) |
| 885 | : err_(OK), handle_results_(handle_results) {} |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 886 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 887 | void Run(art::Thread* target) override REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 888 | art::Locks::mutator_lock_->AssertSharedHeld(art::Thread::Current()); |
| 889 | // Find the monitors on the stack. |
| 890 | MonitorVisitor visitor(target); |
Andreas Gampe | 6e89776 | 2018-10-16 13:09:32 -0700 | [diff] [blame] | 891 | visitor.WalkStack(/* include_transitions= */ false); |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 892 | // Find any other monitors, including ones acquired in native code. |
| 893 | art::RootInfo root_info(art::kRootVMInternal); |
Ian Rogers | 55256cb | 2017-12-21 17:07:11 -0800 | [diff] [blame] | 894 | target->GetJniEnv()->VisitMonitorRoots(&visitor, root_info); |
Alex Light | 318afe6 | 2018-03-22 16:50:10 -0700 | [diff] [blame] | 895 | err_ = handle_results_(visitor); |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | jvmtiError GetError() { |
| 899 | return err_; |
| 900 | } |
| 901 | |
| 902 | private: |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 903 | jvmtiError err_; |
| 904 | Fn handle_results_; |
| 905 | }; |
| 906 | |
| 907 | |
| 908 | template <typename Fn> |
Alex Light | 318afe6 | 2018-03-22 16:50:10 -0700 | [diff] [blame] | 909 | static jvmtiError GetOwnedMonitorInfoCommon(const art::ScopedObjectAccessAlreadyRunnable& soa, |
| 910 | jthread thread, |
| 911 | Fn handle_results) |
| 912 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 913 | art::Thread* self = art::Thread::Current(); |
Alex Light | 318afe6 | 2018-03-22 16:50:10 -0700 | [diff] [blame] | 914 | MonitorInfoClosure<Fn> closure(handle_results); |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 915 | bool called_method = false; |
| 916 | { |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 917 | art::Locks::thread_list_lock_->ExclusiveLock(self); |
Alex Light | 7ddc23d | 2017-09-22 15:33:41 -0700 | [diff] [blame] | 918 | art::Thread* target = nullptr; |
| 919 | jvmtiError err = ERR(INTERNAL); |
| 920 | if (!ThreadUtil::GetAliveNativeThread(thread, soa, &target, &err)) { |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 921 | art::Locks::thread_list_lock_->ExclusiveUnlock(self); |
Alex Light | 7ddc23d | 2017-09-22 15:33:41 -0700 | [diff] [blame] | 922 | return err; |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 923 | } |
| 924 | if (target != self) { |
| 925 | called_method = true; |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 926 | // RequestSynchronousCheckpoint releases the thread_list_lock_ as a part of its execution. |
Alex Light | a5cd4c0 | 2018-03-28 16:07:39 -0700 | [diff] [blame] | 927 | // Since this deals with object references we need to avoid going to sleep. |
| 928 | art::ScopedAssertNoThreadSuspension sants("Getting owned monitor usage"); |
| 929 | if (!target->RequestSynchronousCheckpoint(&closure, art::ThreadState::kRunnable)) { |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 930 | return ERR(THREAD_NOT_ALIVE); |
| 931 | } |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 932 | } else { |
| 933 | art::Locks::thread_list_lock_->ExclusiveUnlock(self); |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 934 | } |
| 935 | } |
| 936 | // Cannot call the closure on the current thread if we have thread_list_lock since we need to call |
| 937 | // into the verifier which can cause the current thread to suspend for gc. Suspending would be a |
| 938 | // bad thing to do if we hold the ThreadListLock. For other threads since we are running it on a |
| 939 | // checkpoint we are fine but if the thread is the current one we need to drop the mutex first. |
| 940 | if (!called_method) { |
| 941 | closure.Run(self); |
| 942 | } |
| 943 | return closure.GetError(); |
| 944 | } |
| 945 | |
| 946 | jvmtiError StackUtil::GetOwnedMonitorStackDepthInfo(jvmtiEnv* env, |
| 947 | jthread thread, |
| 948 | jint* info_cnt, |
| 949 | jvmtiMonitorStackDepthInfo** info_ptr) { |
| 950 | if (info_cnt == nullptr || info_ptr == nullptr) { |
| 951 | return ERR(NULL_POINTER); |
| 952 | } |
Alex Light | 318afe6 | 2018-03-22 16:50:10 -0700 | [diff] [blame] | 953 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 954 | std::vector<art::GcRoot<art::mirror::Object>> mons; |
| 955 | std::vector<uint32_t> depths; |
| 956 | auto handle_fun = [&] (MonitorVisitor& visitor) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 957 | for (size_t i = 0; i < visitor.monitors.size(); i++) { |
Alex Light | 318afe6 | 2018-03-22 16:50:10 -0700 | [diff] [blame] | 958 | mons.push_back(art::GcRoot<art::mirror::Object>(visitor.monitors[i].Get())); |
| 959 | depths.push_back(visitor.stack_depths[i]); |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 960 | } |
| 961 | return OK; |
| 962 | }; |
Alex Light | 318afe6 | 2018-03-22 16:50:10 -0700 | [diff] [blame] | 963 | jvmtiError err = GetOwnedMonitorInfoCommon(soa, thread, handle_fun); |
| 964 | if (err != OK) { |
| 965 | return err; |
| 966 | } |
| 967 | auto nbytes = sizeof(jvmtiMonitorStackDepthInfo) * mons.size(); |
| 968 | err = env->Allocate(nbytes, reinterpret_cast<unsigned char**>(info_ptr)); |
| 969 | if (err != OK) { |
| 970 | return err; |
| 971 | } |
| 972 | *info_cnt = mons.size(); |
| 973 | for (uint32_t i = 0; i < mons.size(); i++) { |
| 974 | (*info_ptr)[i] = { |
| 975 | soa.AddLocalReference<jobject>(mons[i].Read()), |
| 976 | static_cast<jint>(depths[i]) |
| 977 | }; |
| 978 | } |
| 979 | return err; |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | jvmtiError StackUtil::GetOwnedMonitorInfo(jvmtiEnv* env, |
| 983 | jthread thread, |
| 984 | jint* owned_monitor_count_ptr, |
| 985 | jobject** owned_monitors_ptr) { |
Alex Light | 19a7d4f | 2018-03-23 10:05:49 -0700 | [diff] [blame] | 986 | if (owned_monitor_count_ptr == nullptr || owned_monitors_ptr == nullptr) { |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 987 | return ERR(NULL_POINTER); |
| 988 | } |
Alex Light | 318afe6 | 2018-03-22 16:50:10 -0700 | [diff] [blame] | 989 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 990 | std::vector<art::GcRoot<art::mirror::Object>> mons; |
| 991 | auto handle_fun = [&] (MonitorVisitor& visitor) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 992 | for (size_t i = 0; i < visitor.monitors.size(); i++) { |
Alex Light | 318afe6 | 2018-03-22 16:50:10 -0700 | [diff] [blame] | 993 | mons.push_back(art::GcRoot<art::mirror::Object>(visitor.monitors[i].Get())); |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 994 | } |
| 995 | return OK; |
| 996 | }; |
Alex Light | 318afe6 | 2018-03-22 16:50:10 -0700 | [diff] [blame] | 997 | jvmtiError err = GetOwnedMonitorInfoCommon(soa, thread, handle_fun); |
| 998 | if (err != OK) { |
| 999 | return err; |
| 1000 | } |
| 1001 | auto nbytes = sizeof(jobject) * mons.size(); |
| 1002 | err = env->Allocate(nbytes, reinterpret_cast<unsigned char**>(owned_monitors_ptr)); |
| 1003 | if (err != OK) { |
| 1004 | return err; |
| 1005 | } |
| 1006 | *owned_monitor_count_ptr = mons.size(); |
| 1007 | for (uint32_t i = 0; i < mons.size(); i++) { |
| 1008 | (*owned_monitors_ptr)[i] = soa.AddLocalReference<jobject>(mons[i].Read()); |
| 1009 | } |
| 1010 | return err; |
Alex Light | 88e1ddd | 2017-08-21 13:09:55 -0700 | [diff] [blame] | 1011 | } |
| 1012 | |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 1013 | jvmtiError StackUtil::NotifyFramePop(jvmtiEnv* env, jthread thread, jint depth) { |
| 1014 | if (depth < 0) { |
| 1015 | return ERR(ILLEGAL_ARGUMENT); |
| 1016 | } |
| 1017 | ArtJvmTiEnv* tienv = ArtJvmTiEnv::AsArtJvmTiEnv(env); |
| 1018 | art::Thread* self = art::Thread::Current(); |
| 1019 | art::Thread* target; |
Alex Light | c723b81 | 2019-04-08 16:13:24 +0000 | [diff] [blame] | 1020 | |
Alex Light | 679dec1 | 2019-04-08 16:30:14 +0000 | [diff] [blame] | 1021 | ScopedNoUserCodeSuspension snucs(self); |
| 1022 | // From now on we know we cannot get suspended by user-code. |
| 1023 | // NB This does a SuspendCheck (during thread state change) so we need to make |
| 1024 | // sure we don't have the 'suspend_lock' locked here. |
| 1025 | art::ScopedObjectAccess soa(self); |
Alex Light | a4cdd36 | 2019-04-18 09:17:10 -0700 | [diff] [blame] | 1026 | art::Locks::thread_list_lock_->ExclusiveLock(self); |
Alex Light | 679dec1 | 2019-04-08 16:30:14 +0000 | [diff] [blame] | 1027 | jvmtiError err = ERR(INTERNAL); |
| 1028 | if (!ThreadUtil::GetAliveNativeThread(thread, soa, &target, &err)) { |
Alex Light | a4cdd36 | 2019-04-18 09:17:10 -0700 | [diff] [blame] | 1029 | art::Locks::thread_list_lock_->ExclusiveUnlock(self); |
Alex Light | 679dec1 | 2019-04-08 16:30:14 +0000 | [diff] [blame] | 1030 | return err; |
| 1031 | } |
| 1032 | if (target != self) { |
| 1033 | // TODO This is part of the spec but we could easily avoid needing to do it. |
| 1034 | // We would just put all the logic into a sync-checkpoint. |
Alex Light | a4cdd36 | 2019-04-18 09:17:10 -0700 | [diff] [blame] | 1035 | art::Locks::thread_suspend_count_lock_->ExclusiveLock(self); |
Alex Light | 679dec1 | 2019-04-08 16:30:14 +0000 | [diff] [blame] | 1036 | if (target->GetUserCodeSuspendCount() == 0) { |
Alex Light | a4cdd36 | 2019-04-18 09:17:10 -0700 | [diff] [blame] | 1037 | art::Locks::thread_suspend_count_lock_->ExclusiveUnlock(self); |
| 1038 | art::Locks::thread_list_lock_->ExclusiveUnlock(self); |
Alex Light | 679dec1 | 2019-04-08 16:30:14 +0000 | [diff] [blame] | 1039 | return ERR(THREAD_NOT_SUSPENDED); |
Alex Light | c723b81 | 2019-04-08 16:13:24 +0000 | [diff] [blame] | 1040 | } |
Alex Light | a4cdd36 | 2019-04-18 09:17:10 -0700 | [diff] [blame] | 1041 | art::Locks::thread_suspend_count_lock_->ExclusiveUnlock(self); |
Alex Light | 679dec1 | 2019-04-08 16:30:14 +0000 | [diff] [blame] | 1042 | } |
| 1043 | // We hold the user_code_suspension_lock_ so the target thread is staying |
| 1044 | // suspended until we are done (unless it's 'self' in which case we don't care |
| 1045 | // since we aren't going to be returning). |
| 1046 | // TODO We could implement this using a synchronous checkpoint and not bother |
| 1047 | // with any of the suspension stuff. The spec does specifically say to return |
| 1048 | // THREAD_NOT_SUSPENDED though. Find the requested stack frame. |
| 1049 | std::unique_ptr<art::Context> context(art::Context::Create()); |
| 1050 | FindFrameAtDepthVisitor visitor(target, context.get(), depth); |
| 1051 | visitor.WalkStack(); |
| 1052 | if (!visitor.FoundFrame()) { |
Alex Light | a4cdd36 | 2019-04-18 09:17:10 -0700 | [diff] [blame] | 1053 | art::Locks::thread_list_lock_->ExclusiveUnlock(self); |
Alex Light | 679dec1 | 2019-04-08 16:30:14 +0000 | [diff] [blame] | 1054 | return ERR(NO_MORE_FRAMES); |
| 1055 | } |
| 1056 | art::ArtMethod* method = visitor.GetMethod(); |
| 1057 | if (method->IsNative()) { |
Alex Light | a4cdd36 | 2019-04-18 09:17:10 -0700 | [diff] [blame] | 1058 | art::Locks::thread_list_lock_->ExclusiveUnlock(self); |
Alex Light | 679dec1 | 2019-04-08 16:30:14 +0000 | [diff] [blame] | 1059 | return ERR(OPAQUE_FRAME); |
| 1060 | } |
| 1061 | // From here we are sure to succeed. |
| 1062 | bool needs_instrument = false; |
| 1063 | // Get/create a shadow frame |
| 1064 | art::ShadowFrame* shadow_frame = |
| 1065 | visitor.GetOrCreateShadowFrame(&needs_instrument); |
| 1066 | { |
| 1067 | art::WriterMutexLock lk(self, tienv->event_info_mutex_); |
| 1068 | if (LIKELY(!shadow_frame->NeedsNotifyPop())) { |
| 1069 | // Ensure we won't miss exceptions being thrown if we get jit-compiled. We |
| 1070 | // only do this for the first NotifyPopFrame. |
| 1071 | target->IncrementForceInterpreterCount(); |
| 1072 | |
| 1073 | // Mark shadow frame as needs_notify_pop_ |
| 1074 | shadow_frame->SetNotifyPop(true); |
Alex Light | c723b81 | 2019-04-08 16:13:24 +0000 | [diff] [blame] | 1075 | } |
Alex Light | 679dec1 | 2019-04-08 16:30:14 +0000 | [diff] [blame] | 1076 | tienv->notify_frames.insert(shadow_frame); |
| 1077 | } |
| 1078 | // Make sure can we will go to the interpreter and use the shadow frames. |
| 1079 | if (needs_instrument) { |
Alex Light | a4cdd36 | 2019-04-18 09:17:10 -0700 | [diff] [blame] | 1080 | art::FunctionClosure fc([](art::Thread* self) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1081 | DeoptManager::Get()->DeoptimizeThread(self); |
| 1082 | }); |
| 1083 | target->RequestSynchronousCheckpoint(&fc); |
| 1084 | } else { |
| 1085 | art::Locks::thread_list_lock_->ExclusiveUnlock(self); |
Alex Light | 679dec1 | 2019-04-08 16:30:14 +0000 | [diff] [blame] | 1086 | } |
| 1087 | return OK; |
Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 1088 | } |
| 1089 | |
Alex Light | ae45cbb | 2018-10-18 15:49:56 -0700 | [diff] [blame] | 1090 | jvmtiError StackUtil::PopFrame(jvmtiEnv* env, jthread thread) { |
Alex Light | 0aa7a5a | 2018-10-10 15:58:14 +0000 | [diff] [blame] | 1091 | art::Thread* self = art::Thread::Current(); |
| 1092 | art::Thread* target; |
Alex Light | 0aa7a5a | 2018-10-10 15:58:14 +0000 | [diff] [blame] | 1093 | |
Alex Light | 679dec1 | 2019-04-08 16:30:14 +0000 | [diff] [blame] | 1094 | ScopedNoUserCodeSuspension snucs(self); |
| 1095 | // From now on we know we cannot get suspended by user-code. |
| 1096 | // NB This does a SuspendCheck (during thread state change) so we need to make |
| 1097 | // sure we don't have the 'suspend_lock' locked here. |
| 1098 | art::ScopedObjectAccess soa(self); |
Alex Light | a4cdd36 | 2019-04-18 09:17:10 -0700 | [diff] [blame] | 1099 | art::Locks::thread_list_lock_->ExclusiveLock(self); |
Alex Light | 679dec1 | 2019-04-08 16:30:14 +0000 | [diff] [blame] | 1100 | jvmtiError err = ERR(INTERNAL); |
| 1101 | if (!ThreadUtil::GetAliveNativeThread(thread, soa, &target, &err)) { |
Alex Light | a4cdd36 | 2019-04-18 09:17:10 -0700 | [diff] [blame] | 1102 | art::Locks::thread_list_lock_->ExclusiveUnlock(self); |
Alex Light | 679dec1 | 2019-04-08 16:30:14 +0000 | [diff] [blame] | 1103 | return err; |
| 1104 | } |
| 1105 | { |
Alex Light | a4cdd36 | 2019-04-18 09:17:10 -0700 | [diff] [blame] | 1106 | art::Locks::thread_suspend_count_lock_->ExclusiveLock(self); |
Alex Light | 679dec1 | 2019-04-08 16:30:14 +0000 | [diff] [blame] | 1107 | if (target == self || target->GetUserCodeSuspendCount() == 0) { |
| 1108 | // We cannot be the current thread for this function. |
Alex Light | a4cdd36 | 2019-04-18 09:17:10 -0700 | [diff] [blame] | 1109 | art::Locks::thread_suspend_count_lock_->ExclusiveUnlock(self); |
| 1110 | art::Locks::thread_list_lock_->ExclusiveUnlock(self); |
Alex Light | 679dec1 | 2019-04-08 16:30:14 +0000 | [diff] [blame] | 1111 | return ERR(THREAD_NOT_SUSPENDED); |
Alex Light | c723b81 | 2019-04-08 16:13:24 +0000 | [diff] [blame] | 1112 | } |
Alex Light | a4cdd36 | 2019-04-18 09:17:10 -0700 | [diff] [blame] | 1113 | art::Locks::thread_suspend_count_lock_->ExclusiveUnlock(self); |
Alex Light | 679dec1 | 2019-04-08 16:30:14 +0000 | [diff] [blame] | 1114 | } |
| 1115 | JvmtiGlobalTLSData* tls_data = ThreadUtil::GetGlobalTLSData(target); |
| 1116 | constexpr art::StackVisitor::StackWalkKind kWalkKind = |
| 1117 | art::StackVisitor::StackWalkKind::kIncludeInlinedFrames; |
| 1118 | if (tls_data != nullptr && |
| 1119 | tls_data->disable_pop_frame_depth != |
| 1120 | JvmtiGlobalTLSData::kNoDisallowedPopFrame && |
| 1121 | tls_data->disable_pop_frame_depth == |
| 1122 | art::StackVisitor::ComputeNumFrames(target, kWalkKind)) { |
| 1123 | JVMTI_LOG(WARNING, env) |
| 1124 | << "Disallowing frame pop due to in-progress class-load/prepare. " |
| 1125 | << "Frame at depth " << tls_data->disable_pop_frame_depth << " was " |
| 1126 | << "marked as un-poppable by the jvmti plugin. See b/117615146 for " |
| 1127 | << "more information."; |
Alex Light | a4cdd36 | 2019-04-18 09:17:10 -0700 | [diff] [blame] | 1128 | art::Locks::thread_list_lock_->ExclusiveUnlock(self); |
Alex Light | 679dec1 | 2019-04-08 16:30:14 +0000 | [diff] [blame] | 1129 | return ERR(OPAQUE_FRAME); |
| 1130 | } |
| 1131 | // We hold the user_code_suspension_lock_ so the target thread is staying |
| 1132 | // suspended until we are done. |
| 1133 | std::unique_ptr<art::Context> context(art::Context::Create()); |
| 1134 | FindFrameAtDepthVisitor final_frame(target, context.get(), 0); |
| 1135 | FindFrameAtDepthVisitor penultimate_frame(target, context.get(), 1); |
| 1136 | final_frame.WalkStack(); |
| 1137 | penultimate_frame.WalkStack(); |
Alex Light | 0aa7a5a | 2018-10-10 15:58:14 +0000 | [diff] [blame] | 1138 | |
Alex Light | 679dec1 | 2019-04-08 16:30:14 +0000 | [diff] [blame] | 1139 | if (!final_frame.FoundFrame() || !penultimate_frame.FoundFrame()) { |
| 1140 | // Cannot do it if there is only one frame! |
Alex Light | a4cdd36 | 2019-04-18 09:17:10 -0700 | [diff] [blame] | 1141 | art::Locks::thread_list_lock_->ExclusiveUnlock(self); |
Alex Light | 679dec1 | 2019-04-08 16:30:14 +0000 | [diff] [blame] | 1142 | return ERR(NO_MORE_FRAMES); |
| 1143 | } |
Alex Light | 0aa7a5a | 2018-10-10 15:58:14 +0000 | [diff] [blame] | 1144 | |
Alex Light | 679dec1 | 2019-04-08 16:30:14 +0000 | [diff] [blame] | 1145 | art::ArtMethod* called_method = final_frame.GetMethod(); |
| 1146 | art::ArtMethod* calling_method = penultimate_frame.GetMethod(); |
| 1147 | if (calling_method->IsNative() || called_method->IsNative()) { |
Alex Light | a4cdd36 | 2019-04-18 09:17:10 -0700 | [diff] [blame] | 1148 | art::Locks::thread_list_lock_->ExclusiveUnlock(self); |
Alex Light | 679dec1 | 2019-04-08 16:30:14 +0000 | [diff] [blame] | 1149 | return ERR(OPAQUE_FRAME); |
| 1150 | } |
| 1151 | // From here we are sure to succeed. |
Alex Light | 0aa7a5a | 2018-10-10 15:58:14 +0000 | [diff] [blame] | 1152 | |
Alex Light | 679dec1 | 2019-04-08 16:30:14 +0000 | [diff] [blame] | 1153 | // Get/create a shadow frame |
| 1154 | bool created_final_frame = false; |
| 1155 | bool created_penultimate_frame = false; |
| 1156 | art::ShadowFrame* called_shadow_frame = |
| 1157 | final_frame.GetOrCreateShadowFrame(&created_final_frame); |
| 1158 | art::ShadowFrame* calling_shadow_frame = |
| 1159 | penultimate_frame.GetOrCreateShadowFrame(&created_penultimate_frame); |
Alex Light | 0aa7a5a | 2018-10-10 15:58:14 +0000 | [diff] [blame] | 1160 | |
Alex Light | 679dec1 | 2019-04-08 16:30:14 +0000 | [diff] [blame] | 1161 | CHECK_NE(called_shadow_frame, calling_shadow_frame) |
| 1162 | << "Frames at different depths not different!"; |
Alex Light | 9c8f344 | 2019-04-04 15:01:42 -0700 | [diff] [blame] | 1163 | |
Alex Light | 679dec1 | 2019-04-08 16:30:14 +0000 | [diff] [blame] | 1164 | // Tell the shadow-frame to return immediately and skip all exit events. |
| 1165 | called_shadow_frame->SetForcePopFrame(true); |
| 1166 | calling_shadow_frame->SetForceRetryInstruction(true); |
| 1167 | |
| 1168 | // Make sure can we will go to the interpreter and use the shadow frames. The |
| 1169 | // early return for the final frame will force everything to the interpreter |
| 1170 | // so we only need to instrument if it was not present. |
| 1171 | if (created_final_frame) { |
Alex Light | a4cdd36 | 2019-04-18 09:17:10 -0700 | [diff] [blame] | 1172 | art::FunctionClosure fc([](art::Thread* self) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1173 | DeoptManager::Get()->DeoptimizeThread(self); |
| 1174 | }); |
| 1175 | target->RequestSynchronousCheckpoint(&fc); |
| 1176 | } else { |
| 1177 | art::Locks::thread_list_lock_->ExclusiveUnlock(self); |
Alex Light | 679dec1 | 2019-04-08 16:30:14 +0000 | [diff] [blame] | 1178 | } |
| 1179 | return OK; |
Alex Light | 0aa7a5a | 2018-10-10 15:58:14 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 1182 | } // namespace openjdkjvmti |