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 | a1d2f95 | 2017-04-20 22:53:58 -0700 | [diff] [blame] | 39 | #include "art_field-inl.h" |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 40 | #include "art_method-inl.h" |
Andreas Gampe | a1d2f95 | 2017-04-20 22:53:58 -0700 | [diff] [blame] | 41 | #include "art_jvmti.h" |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 42 | #include "base/bit_utils.h" |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 43 | #include "base/enums.h" |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 44 | #include "base/mutex.h" |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 45 | #include "dex_file.h" |
| 46 | #include "dex_file_annotations.h" |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 47 | #include "handle_scope-inl.h" |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 48 | #include "jni_env_ext.h" |
Andreas Gampe | 13b2784 | 2016-11-07 16:48:23 -0800 | [diff] [blame] | 49 | #include "jni_internal.h" |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 50 | #include "mirror/class.h" |
| 51 | #include "mirror/dex_cache.h" |
| 52 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 53 | #include "ScopedLocalRef.h" |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 54 | #include "stack.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 55 | #include "thread-current-inl.h" |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 56 | #include "thread_list.h" |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 57 | #include "thread_pool.h" |
Andreas Gampe | a1d2f95 | 2017-04-20 22:53:58 -0700 | [diff] [blame] | 58 | #include "well_known_classes.h" |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 59 | |
| 60 | namespace openjdkjvmti { |
| 61 | |
Andreas Gampe | 6db6b4d | 2017-06-12 16:36:33 -0700 | [diff] [blame] | 62 | template <typename FrameFn> |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 63 | struct GetStackTraceVisitor : public art::StackVisitor { |
| 64 | GetStackTraceVisitor(art::Thread* thread_in, |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 65 | size_t start_, |
Andreas Gampe | 6db6b4d | 2017-06-12 16:36:33 -0700 | [diff] [blame] | 66 | size_t stop_, |
| 67 | FrameFn fn_) |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 68 | : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
Andreas Gampe | 6db6b4d | 2017-06-12 16:36:33 -0700 | [diff] [blame] | 69 | fn(fn_), |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 70 | start(start_), |
| 71 | stop(stop_) {} |
Andreas Gampe | 6db6b4d | 2017-06-12 16:36:33 -0700 | [diff] [blame] | 72 | GetStackTraceVisitor(const GetStackTraceVisitor&) = default; |
| 73 | GetStackTraceVisitor(GetStackTraceVisitor&&) = default; |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 74 | |
| 75 | bool VisitFrame() REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 76 | art::ArtMethod* m = GetMethod(); |
| 77 | if (m->IsRuntimeMethod()) { |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | if (start == 0) { |
| 82 | m = m->GetInterfaceMethodIfProxy(art::kRuntimePointerSize); |
Andreas Gampe | 13b2784 | 2016-11-07 16:48:23 -0800 | [diff] [blame] | 83 | jmethodID id = art::jni::EncodeArtMethod(m); |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 84 | |
Andreas Gampe | 2340e3f | 2016-12-12 19:37:19 -0800 | [diff] [blame] | 85 | uint32_t dex_pc = GetDexPc(false); |
| 86 | jlong dex_location = (dex_pc == art::DexFile::kDexNoIndex) ? -1 : static_cast<jlong>(dex_pc); |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 87 | |
Andreas Gampe | 2340e3f | 2016-12-12 19:37:19 -0800 | [diff] [blame] | 88 | jvmtiFrameInfo info = { id, dex_location }; |
Andreas Gampe | 6db6b4d | 2017-06-12 16:36:33 -0700 | [diff] [blame] | 89 | fn(info); |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 90 | |
| 91 | if (stop == 1) { |
| 92 | return false; // We're done. |
| 93 | } else if (stop > 0) { |
| 94 | stop--; |
| 95 | } |
| 96 | } else { |
| 97 | start--; |
| 98 | } |
| 99 | |
| 100 | return true; |
| 101 | } |
| 102 | |
Andreas Gampe | 6db6b4d | 2017-06-12 16:36:33 -0700 | [diff] [blame] | 103 | FrameFn fn; |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 104 | size_t start; |
| 105 | size_t stop; |
| 106 | }; |
| 107 | |
Andreas Gampe | 6db6b4d | 2017-06-12 16:36:33 -0700 | [diff] [blame] | 108 | template <typename FrameFn> |
| 109 | GetStackTraceVisitor<FrameFn> MakeStackTraceVisitor(art::Thread* thread_in, |
| 110 | size_t start, |
| 111 | size_t stop, |
| 112 | FrameFn fn) { |
| 113 | return GetStackTraceVisitor<FrameFn>(thread_in, start, stop, fn); |
| 114 | } |
| 115 | |
| 116 | struct GetStackTraceVectorClosure : public art::Closure { |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 117 | public: |
Andreas Gampe | 6db6b4d | 2017-06-12 16:36:33 -0700 | [diff] [blame] | 118 | GetStackTraceVectorClosure(size_t start, size_t stop) |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 119 | : start_input(start), |
| 120 | stop_input(stop), |
| 121 | start_result(0), |
| 122 | stop_result(0) {} |
| 123 | |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 124 | void Run(art::Thread* self) OVERRIDE REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Andreas Gampe | 6db6b4d | 2017-06-12 16:36:33 -0700 | [diff] [blame] | 125 | auto frames_fn = [&](jvmtiFrameInfo info) { |
| 126 | frames.push_back(info); |
| 127 | }; |
| 128 | auto visitor = MakeStackTraceVisitor(self, start_input, stop_input, frames_fn); |
| 129 | visitor.WalkStack(/* include_transitions */ false); |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 130 | |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 131 | start_result = visitor.start; |
| 132 | stop_result = visitor.stop; |
| 133 | } |
| 134 | |
| 135 | const size_t start_input; |
| 136 | const size_t stop_input; |
| 137 | |
| 138 | std::vector<jvmtiFrameInfo> frames; |
| 139 | size_t start_result; |
| 140 | size_t stop_result; |
| 141 | }; |
| 142 | |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 143 | static jvmtiError TranslateFrameVector(const std::vector<jvmtiFrameInfo>& frames, |
| 144 | jint start_depth, |
| 145 | size_t start_result, |
| 146 | jint max_frame_count, |
| 147 | jvmtiFrameInfo* frame_buffer, |
| 148 | jint* count_ptr) { |
| 149 | size_t collected_frames = frames.size(); |
| 150 | |
| 151 | // Assume we're here having collected something. |
| 152 | DCHECK_GT(max_frame_count, 0); |
| 153 | |
| 154 | // Frames from the top. |
| 155 | if (start_depth >= 0) { |
| 156 | if (start_result != 0) { |
| 157 | // Not enough frames. |
| 158 | return ERR(ILLEGAL_ARGUMENT); |
| 159 | } |
| 160 | DCHECK_LE(collected_frames, static_cast<size_t>(max_frame_count)); |
| 161 | if (frames.size() > 0) { |
| 162 | memcpy(frame_buffer, frames.data(), collected_frames * sizeof(jvmtiFrameInfo)); |
| 163 | } |
| 164 | *count_ptr = static_cast<jint>(frames.size()); |
| 165 | return ERR(NONE); |
| 166 | } |
| 167 | |
| 168 | // Frames from the bottom. |
| 169 | if (collected_frames < static_cast<size_t>(-start_depth)) { |
| 170 | return ERR(ILLEGAL_ARGUMENT); |
| 171 | } |
| 172 | |
| 173 | size_t count = std::min(static_cast<size_t>(-start_depth), static_cast<size_t>(max_frame_count)); |
| 174 | memcpy(frame_buffer, |
| 175 | &frames.data()[collected_frames + start_depth], |
| 176 | count * sizeof(jvmtiFrameInfo)); |
| 177 | *count_ptr = static_cast<jint>(count); |
| 178 | return ERR(NONE); |
| 179 | } |
| 180 | |
Andreas Gampe | 850a0fe | 2017-06-12 18:37:19 -0700 | [diff] [blame] | 181 | struct GetStackTraceDirectClosure : public art::Closure { |
| 182 | public: |
| 183 | GetStackTraceDirectClosure(jvmtiFrameInfo* frame_buffer_, size_t start, size_t stop) |
| 184 | : frame_buffer(frame_buffer_), |
| 185 | start_input(start), |
| 186 | stop_input(stop), |
| 187 | index(0) { |
| 188 | DCHECK_GE(start_input, 0u); |
| 189 | } |
| 190 | |
| 191 | void Run(art::Thread* self) OVERRIDE REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 192 | auto frames_fn = [&](jvmtiFrameInfo info) { |
| 193 | frame_buffer[index] = info; |
| 194 | ++index; |
| 195 | }; |
| 196 | auto visitor = MakeStackTraceVisitor(self, start_input, stop_input, frames_fn); |
| 197 | visitor.WalkStack(/* include_transitions */ false); |
| 198 | } |
| 199 | |
| 200 | jvmtiFrameInfo* frame_buffer; |
| 201 | |
| 202 | const size_t start_input; |
| 203 | const size_t stop_input; |
| 204 | |
| 205 | size_t index = 0; |
| 206 | }; |
| 207 | |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 208 | static jvmtiError GetThread(JNIEnv* env, jthread java_thread, art::Thread** thread) { |
| 209 | if (java_thread == nullptr) { |
| 210 | *thread = art::Thread::Current(); |
| 211 | if (*thread == nullptr) { |
| 212 | // GetStackTrace can only be run during the live phase, so the current thread should be |
| 213 | // attached and thus available. Getting a null for current means we're starting up or |
| 214 | // dying. |
| 215 | return ERR(WRONG_PHASE); |
| 216 | } |
| 217 | } else { |
| 218 | if (!env->IsInstanceOf(java_thread, art::WellKnownClasses::java_lang_Thread)) { |
| 219 | return ERR(INVALID_THREAD); |
| 220 | } |
| 221 | |
| 222 | // TODO: Need non-aborting call here, to return JVMTI_ERROR_INVALID_THREAD. |
| 223 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 224 | art::MutexLock mu(soa.Self(), *art::Locks::thread_list_lock_); |
| 225 | *thread = art::Thread::FromManagedThread(soa, java_thread); |
| 226 | if (*thread == nullptr) { |
| 227 | return ERR(THREAD_NOT_ALIVE); |
| 228 | } |
| 229 | } |
| 230 | return ERR(NONE); |
| 231 | } |
| 232 | |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 233 | jvmtiError StackUtil::GetStackTrace(jvmtiEnv* jvmti_env ATTRIBUTE_UNUSED, |
| 234 | jthread java_thread, |
| 235 | jint start_depth, |
| 236 | jint max_frame_count, |
| 237 | jvmtiFrameInfo* frame_buffer, |
| 238 | jint* count_ptr) { |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 239 | art::Thread* thread; |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 240 | jvmtiError thread_error = GetThread(art::Thread::Current()->GetJniEnv(), java_thread, &thread); |
| 241 | if (thread_error != ERR(NONE)) { |
| 242 | return thread_error; |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 243 | } |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 244 | DCHECK(thread != nullptr); |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 245 | |
| 246 | art::ThreadState state = thread->GetState(); |
| 247 | if (state == art::ThreadState::kStarting || |
| 248 | state == art::ThreadState::kTerminated || |
| 249 | thread->IsStillStarting()) { |
| 250 | return ERR(THREAD_NOT_ALIVE); |
| 251 | } |
| 252 | |
| 253 | if (max_frame_count < 0) { |
| 254 | return ERR(ILLEGAL_ARGUMENT); |
| 255 | } |
| 256 | if (frame_buffer == nullptr || count_ptr == nullptr) { |
| 257 | return ERR(NULL_POINTER); |
| 258 | } |
| 259 | |
| 260 | if (max_frame_count == 0) { |
| 261 | *count_ptr = 0; |
| 262 | return ERR(NONE); |
| 263 | } |
| 264 | |
Andreas Gampe | 850a0fe | 2017-06-12 18:37:19 -0700 | [diff] [blame] | 265 | if (start_depth >= 0) { |
| 266 | // Fast path: Regular order of stack trace. Fill into the frame_buffer directly. |
| 267 | GetStackTraceDirectClosure closure(frame_buffer, |
| 268 | static_cast<size_t>(start_depth), |
| 269 | static_cast<size_t>(max_frame_count)); |
| 270 | thread->RequestSynchronousCheckpoint(&closure); |
| 271 | *count_ptr = static_cast<jint>(closure.index); |
| 272 | if (closure.index < static_cast<size_t>(start_depth)) { |
| 273 | return ERR(ILLEGAL_ARGUMENT); |
| 274 | } |
| 275 | return ERR(NONE); |
| 276 | } |
| 277 | |
| 278 | GetStackTraceVectorClosure closure(0, 0); |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 279 | thread->RequestSynchronousCheckpoint(&closure); |
| 280 | |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 281 | return TranslateFrameVector(closure.frames, |
| 282 | start_depth, |
| 283 | closure.start_result, |
| 284 | max_frame_count, |
| 285 | frame_buffer, |
| 286 | count_ptr); |
| 287 | } |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 288 | |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame^] | 289 | template <typename Data> |
| 290 | struct GetAllStackTracesVectorClosure : public art::Closure { |
| 291 | GetAllStackTracesVectorClosure(size_t stop, Data* data_) : stop_input(stop), data(data_) {} |
| 292 | |
| 293 | void Run(art::Thread* thread) OVERRIDE |
| 294 | REQUIRES_SHARED(art::Locks::mutator_lock_) |
| 295 | REQUIRES(!data->mutex) { |
| 296 | art::Thread* self = art::Thread::Current(); |
| 297 | |
| 298 | // Skip threads that are still starting. |
| 299 | if (thread->IsStillStarting()) { |
| 300 | return; |
| 301 | } |
| 302 | |
| 303 | std::vector<jvmtiFrameInfo>* thread_frames = data->GetFrameStorageFor(self, thread); |
| 304 | if (thread_frames == nullptr) { |
| 305 | return; |
| 306 | } |
| 307 | |
| 308 | // Now collect the data. |
| 309 | auto frames_fn = [&](jvmtiFrameInfo info) { |
| 310 | thread_frames->push_back(info); |
| 311 | }; |
| 312 | auto visitor = MakeStackTraceVisitor(thread, 0u, stop_input, frames_fn); |
| 313 | visitor.WalkStack(/* include_transitions */ false); |
| 314 | } |
| 315 | |
| 316 | const size_t stop_input; |
| 317 | Data* data; |
| 318 | }; |
| 319 | |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 320 | jvmtiError StackUtil::GetAllStackTraces(jvmtiEnv* env, |
| 321 | jint max_frame_count, |
| 322 | jvmtiStackInfo** stack_info_ptr, |
| 323 | jint* thread_count_ptr) { |
| 324 | if (max_frame_count < 0) { |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 325 | return ERR(ILLEGAL_ARGUMENT); |
| 326 | } |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 327 | if (stack_info_ptr == nullptr || thread_count_ptr == nullptr) { |
| 328 | return ERR(NULL_POINTER); |
| 329 | } |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 330 | |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame^] | 331 | struct AllStackTracesData { |
| 332 | AllStackTracesData() : mutex("GetAllStackTraces", art::LockLevel::kAbortLock) {} |
| 333 | ~AllStackTracesData() { |
| 334 | JNIEnv* jni_env = art::Thread::Current()->GetJniEnv(); |
| 335 | for (jthread global_thread_ref : thread_peers) { |
| 336 | jni_env->DeleteGlobalRef(global_thread_ref); |
| 337 | } |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 338 | } |
| 339 | |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame^] | 340 | std::vector<jvmtiFrameInfo>* GetFrameStorageFor(art::Thread* self, art::Thread* thread) |
| 341 | REQUIRES_SHARED(art::Locks::mutator_lock_) |
| 342 | REQUIRES(!mutex) { |
| 343 | art::MutexLock mu(self, mutex); |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 344 | |
| 345 | threads.push_back(thread); |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 346 | |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame^] | 347 | jthread peer = art::Runtime::Current()->GetJavaVM()->AddGlobalRef( |
| 348 | self, thread->GetPeerFromOtherThread()); |
| 349 | thread_peers.push_back(peer); |
| 350 | |
| 351 | frames.emplace_back(); |
| 352 | return &frames.back(); |
| 353 | } |
| 354 | |
| 355 | art::Mutex mutex; |
| 356 | |
| 357 | // Storage. Only access directly after completion. |
| 358 | |
| 359 | std::vector<art::Thread*> threads; |
| 360 | // "thread_peers" contains global references to their peers. |
| 361 | std::vector<jthread> thread_peers; |
| 362 | |
| 363 | std::vector<std::vector<jvmtiFrameInfo>> frames; |
| 364 | }; |
| 365 | |
| 366 | AllStackTracesData data; |
| 367 | GetAllStackTracesVectorClosure<AllStackTracesData> closure( |
| 368 | static_cast<size_t>(max_frame_count), &data); |
| 369 | art::Runtime::Current()->GetThreadList()->RunCheckpoint(&closure, nullptr); |
| 370 | |
| 371 | art::Thread* current = art::Thread::Current(); |
| 372 | |
| 373 | // Convert the data into our output format. |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 374 | |
| 375 | // Note: we use an array of jvmtiStackInfo for convenience. The spec says we need to |
| 376 | // allocate one big chunk for this and the actual frames, which means we need |
| 377 | // to either be conservative or rearrange things later (the latter is implemented). |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame^] | 378 | std::unique_ptr<jvmtiStackInfo[]> stack_info_array(new jvmtiStackInfo[data.frames.size()]); |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 379 | std::vector<std::unique_ptr<jvmtiFrameInfo[]>> frame_infos; |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame^] | 380 | frame_infos.reserve(data.frames.size()); |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 381 | |
| 382 | // Now run through and add data for each thread. |
| 383 | size_t sum_frames = 0; |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame^] | 384 | for (size_t index = 0; index < data.frames.size(); ++index) { |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 385 | jvmtiStackInfo& stack_info = stack_info_array.get()[index]; |
| 386 | memset(&stack_info, 0, sizeof(jvmtiStackInfo)); |
| 387 | |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame^] | 388 | const std::vector<jvmtiFrameInfo>& thread_frames = data.frames[index]; |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 389 | |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame^] | 390 | // 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] | 391 | stack_info.thread = nullptr; |
| 392 | stack_info.state = JVMTI_THREAD_STATE_SUSPENDED; |
| 393 | |
| 394 | size_t collected_frames = thread_frames.size(); |
| 395 | if (max_frame_count == 0 || collected_frames == 0) { |
| 396 | stack_info.frame_count = 0; |
| 397 | stack_info.frame_buffer = nullptr; |
| 398 | continue; |
| 399 | } |
| 400 | DCHECK_LE(collected_frames, static_cast<size_t>(max_frame_count)); |
| 401 | |
| 402 | jvmtiFrameInfo* frame_info = new jvmtiFrameInfo[collected_frames]; |
| 403 | frame_infos.emplace_back(frame_info); |
| 404 | |
| 405 | jint count; |
| 406 | jvmtiError translate_result = TranslateFrameVector(thread_frames, |
| 407 | 0, |
| 408 | 0, |
| 409 | static_cast<jint>(collected_frames), |
| 410 | frame_info, |
| 411 | &count); |
| 412 | DCHECK(translate_result == JVMTI_ERROR_NONE); |
| 413 | stack_info.frame_count = static_cast<jint>(collected_frames); |
| 414 | stack_info.frame_buffer = frame_info; |
| 415 | sum_frames += static_cast<size_t>(count); |
| 416 | } |
| 417 | |
| 418 | // No errors, yet. Now put it all into an output buffer. |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame^] | 419 | 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] | 420 | alignof(jvmtiFrameInfo)); |
| 421 | size_t chunk_size = rounded_stack_info_size + sum_frames * sizeof(jvmtiFrameInfo); |
| 422 | unsigned char* chunk_data; |
| 423 | jvmtiError alloc_result = env->Allocate(chunk_size, &chunk_data); |
| 424 | if (alloc_result != ERR(NONE)) { |
| 425 | return alloc_result; |
| 426 | } |
| 427 | |
| 428 | jvmtiStackInfo* stack_info = reinterpret_cast<jvmtiStackInfo*>(chunk_data); |
| 429 | // First copy in all the basic data. |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame^] | 430 | memcpy(stack_info, stack_info_array.get(), sizeof(jvmtiStackInfo) * data.frames.size()); |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 431 | |
| 432 | // Now copy the frames and fix up the pointers. |
| 433 | jvmtiFrameInfo* frame_info = reinterpret_cast<jvmtiFrameInfo*>( |
| 434 | chunk_data + rounded_stack_info_size); |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame^] | 435 | for (size_t i = 0; i < data.frames.size(); ++i) { |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 436 | jvmtiStackInfo& old_stack_info = stack_info_array.get()[i]; |
| 437 | jvmtiStackInfo& new_stack_info = stack_info[i]; |
| 438 | |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame^] | 439 | // Translate the global ref into a local ref. |
| 440 | new_stack_info.thread = |
| 441 | static_cast<JNIEnv*>(current->GetJniEnv())->NewLocalRef(data.thread_peers[i]); |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 442 | |
| 443 | if (old_stack_info.frame_count > 0) { |
| 444 | // Only copy when there's data - leave the nullptr alone. |
| 445 | size_t frames_size = static_cast<size_t>(old_stack_info.frame_count) * sizeof(jvmtiFrameInfo); |
| 446 | memcpy(frame_info, old_stack_info.frame_buffer, frames_size); |
| 447 | new_stack_info.frame_buffer = frame_info; |
| 448 | frame_info += old_stack_info.frame_count; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | *stack_info_ptr = stack_info; |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame^] | 453 | *thread_count_ptr = static_cast<jint>(data.frames.size()); |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 454 | |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 455 | return ERR(NONE); |
| 456 | } |
| 457 | |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 458 | jvmtiError StackUtil::GetThreadListStackTraces(jvmtiEnv* env, |
| 459 | jint thread_count, |
| 460 | const jthread* thread_list, |
| 461 | jint max_frame_count, |
| 462 | jvmtiStackInfo** stack_info_ptr) { |
| 463 | if (max_frame_count < 0) { |
| 464 | return ERR(ILLEGAL_ARGUMENT); |
| 465 | } |
| 466 | if (thread_count < 0) { |
| 467 | return ERR(ILLEGAL_ARGUMENT); |
| 468 | } |
| 469 | if (thread_count == 0) { |
| 470 | *stack_info_ptr = nullptr; |
| 471 | return ERR(NONE); |
| 472 | } |
| 473 | if (stack_info_ptr == nullptr || stack_info_ptr == nullptr) { |
| 474 | return ERR(NULL_POINTER); |
| 475 | } |
| 476 | |
| 477 | art::Thread* current = art::Thread::Current(); |
| 478 | art::ScopedObjectAccess soa(current); // Now we know we have the shared lock. |
| 479 | |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame^] | 480 | struct SelectStackTracesData { |
| 481 | SelectStackTracesData() : mutex("GetSelectStackTraces", art::LockLevel::kAbortLock) {} |
| 482 | |
| 483 | std::vector<jvmtiFrameInfo>* GetFrameStorageFor(art::Thread* self, art::Thread* thread) |
| 484 | REQUIRES_SHARED(art::Locks::mutator_lock_) |
| 485 | REQUIRES(!mutex) { |
| 486 | art::ObjPtr<art::mirror::Object> peer = thread->GetPeerFromOtherThread(); |
| 487 | for (size_t index = 0; index != handles.size(); ++index) { |
| 488 | if (peer == handles[index].Get()) { |
| 489 | // Found the thread. |
| 490 | art::MutexLock mu(self, mutex); |
| 491 | |
| 492 | threads.push_back(thread); |
| 493 | thread_list_indices.push_back(index); |
| 494 | |
| 495 | frames.emplace_back(); |
| 496 | return &frames.back(); |
| 497 | } |
| 498 | } |
| 499 | return nullptr; |
| 500 | } |
| 501 | |
| 502 | art::Mutex mutex; |
| 503 | |
| 504 | // Selection data. |
| 505 | |
| 506 | std::vector<art::Handle<art::mirror::Object>> handles; |
| 507 | |
| 508 | // Storage. Only access directly after completion. |
| 509 | |
| 510 | std::vector<art::Thread*> threads; |
| 511 | std::vector<size_t> thread_list_indices; |
| 512 | |
| 513 | std::vector<std::vector<jvmtiFrameInfo>> frames; |
| 514 | }; |
| 515 | |
| 516 | SelectStackTracesData data; |
| 517 | |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 518 | // Decode all threads to raw pointers. Put them into a handle scope to avoid any moving GC bugs. |
| 519 | art::VariableSizedHandleScope hs(current); |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 520 | for (jint i = 0; i != thread_count; ++i) { |
| 521 | if (thread_list[i] == nullptr) { |
| 522 | return ERR(INVALID_THREAD); |
| 523 | } |
| 524 | if (!soa.Env()->IsInstanceOf(thread_list[i], art::WellKnownClasses::java_lang_Thread)) { |
| 525 | return ERR(INVALID_THREAD); |
| 526 | } |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame^] | 527 | 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] | 528 | } |
| 529 | |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame^] | 530 | GetAllStackTracesVectorClosure<SelectStackTracesData> closure( |
| 531 | static_cast<size_t>(max_frame_count), &data); |
| 532 | art::Runtime::Current()->GetThreadList()->RunCheckpoint(&closure, nullptr); |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 533 | |
| 534 | // Convert the data into our output format. |
| 535 | |
| 536 | // Note: we use an array of jvmtiStackInfo for convenience. The spec says we need to |
| 537 | // allocate one big chunk for this and the actual frames, which means we need |
| 538 | // to either be conservative or rearrange things later (the latter is implemented). |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame^] | 539 | std::unique_ptr<jvmtiStackInfo[]> stack_info_array(new jvmtiStackInfo[data.frames.size()]); |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 540 | std::vector<std::unique_ptr<jvmtiFrameInfo[]>> frame_infos; |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame^] | 541 | frame_infos.reserve(data.frames.size()); |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 542 | |
| 543 | // Now run through and add data for each thread. |
| 544 | size_t sum_frames = 0; |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame^] | 545 | for (size_t index = 0; index < data.frames.size(); ++index) { |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 546 | jvmtiStackInfo& stack_info = stack_info_array.get()[index]; |
| 547 | memset(&stack_info, 0, sizeof(jvmtiStackInfo)); |
| 548 | |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame^] | 549 | art::Thread* self = data.threads[index]; |
| 550 | const std::vector<jvmtiFrameInfo>& thread_frames = data.frames[index]; |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 551 | |
| 552 | // For the time being, set the thread to null. We don't have good ScopedLocalRef |
| 553 | // infrastructure. |
Nicolas Geoffray | ffc8cad | 2017-02-10 10:59:22 +0000 | [diff] [blame] | 554 | DCHECK(self->GetPeerFromOtherThread() != nullptr); |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 555 | stack_info.thread = nullptr; |
| 556 | stack_info.state = JVMTI_THREAD_STATE_SUSPENDED; |
| 557 | |
| 558 | size_t collected_frames = thread_frames.size(); |
| 559 | if (max_frame_count == 0 || collected_frames == 0) { |
| 560 | stack_info.frame_count = 0; |
| 561 | stack_info.frame_buffer = nullptr; |
| 562 | continue; |
| 563 | } |
| 564 | DCHECK_LE(collected_frames, static_cast<size_t>(max_frame_count)); |
| 565 | |
| 566 | jvmtiFrameInfo* frame_info = new jvmtiFrameInfo[collected_frames]; |
| 567 | frame_infos.emplace_back(frame_info); |
| 568 | |
| 569 | jint count; |
| 570 | jvmtiError translate_result = TranslateFrameVector(thread_frames, |
| 571 | 0, |
| 572 | 0, |
| 573 | static_cast<jint>(collected_frames), |
| 574 | frame_info, |
| 575 | &count); |
| 576 | DCHECK(translate_result == JVMTI_ERROR_NONE); |
| 577 | stack_info.frame_count = static_cast<jint>(collected_frames); |
| 578 | stack_info.frame_buffer = frame_info; |
| 579 | sum_frames += static_cast<size_t>(count); |
| 580 | } |
| 581 | |
| 582 | // No errors, yet. Now put it all into an output buffer. Note that this is not frames.size(), |
| 583 | // potentially. |
| 584 | size_t rounded_stack_info_size = art::RoundUp(sizeof(jvmtiStackInfo) * thread_count, |
| 585 | alignof(jvmtiFrameInfo)); |
| 586 | size_t chunk_size = rounded_stack_info_size + sum_frames * sizeof(jvmtiFrameInfo); |
| 587 | unsigned char* chunk_data; |
| 588 | jvmtiError alloc_result = env->Allocate(chunk_size, &chunk_data); |
| 589 | if (alloc_result != ERR(NONE)) { |
| 590 | return alloc_result; |
| 591 | } |
| 592 | |
| 593 | jvmtiStackInfo* stack_info = reinterpret_cast<jvmtiStackInfo*>(chunk_data); |
| 594 | jvmtiFrameInfo* frame_info = reinterpret_cast<jvmtiFrameInfo*>( |
| 595 | chunk_data + rounded_stack_info_size); |
| 596 | |
| 597 | for (size_t i = 0; i < static_cast<size_t>(thread_count); ++i) { |
| 598 | // Check whether we found a running thread for this. |
| 599 | // Note: For simplicity, and with the expectation that the list is usually small, use a simple |
| 600 | // search. (The list is *not* sorted!) |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame^] | 601 | auto it = std::find(data.thread_list_indices.begin(), data.thread_list_indices.end(), i); |
| 602 | if (it == data.thread_list_indices.end()) { |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 603 | // No native thread. Must be new or dead. We need to fill out the stack info now. |
| 604 | // (Need to read the Java "started" field to know whether this is starting or terminated.) |
| 605 | art::ObjPtr<art::mirror::Object> peer = soa.Decode<art::mirror::Object>(thread_list[i]); |
| 606 | art::ObjPtr<art::mirror::Class> klass = peer->GetClass(); |
| 607 | art::ArtField* started_field = klass->FindDeclaredInstanceField("started", "Z"); |
| 608 | CHECK(started_field != nullptr); |
| 609 | bool started = started_field->GetBoolean(peer) != 0; |
| 610 | constexpr jint kStartedState = JVMTI_JAVA_LANG_THREAD_STATE_NEW; |
| 611 | constexpr jint kTerminatedState = JVMTI_THREAD_STATE_TERMINATED | |
| 612 | JVMTI_JAVA_LANG_THREAD_STATE_TERMINATED; |
| 613 | stack_info[i].thread = reinterpret_cast<JNIEnv*>(soa.Env())->NewLocalRef(thread_list[i]); |
| 614 | stack_info[i].state = started ? kTerminatedState : kStartedState; |
| 615 | stack_info[i].frame_count = 0; |
| 616 | stack_info[i].frame_buffer = nullptr; |
| 617 | } else { |
| 618 | // Had a native thread and frames. |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame^] | 619 | size_t f_index = it - data.thread_list_indices.begin(); |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 620 | |
| 621 | jvmtiStackInfo& old_stack_info = stack_info_array.get()[f_index]; |
| 622 | jvmtiStackInfo& new_stack_info = stack_info[i]; |
| 623 | |
| 624 | memcpy(&new_stack_info, &old_stack_info, sizeof(jvmtiStackInfo)); |
| 625 | new_stack_info.thread = reinterpret_cast<JNIEnv*>(soa.Env())->NewLocalRef(thread_list[i]); |
| 626 | if (old_stack_info.frame_count > 0) { |
| 627 | // Only copy when there's data - leave the nullptr alone. |
| 628 | size_t frames_size = |
| 629 | static_cast<size_t>(old_stack_info.frame_count) * sizeof(jvmtiFrameInfo); |
| 630 | memcpy(frame_info, old_stack_info.frame_buffer, frames_size); |
| 631 | new_stack_info.frame_buffer = frame_info; |
| 632 | frame_info += old_stack_info.frame_count; |
| 633 | } |
| 634 | } |
| 635 | } |
| 636 | |
Andreas Gampe | f1221a1 | 2017-06-21 21:20:47 -0700 | [diff] [blame^] | 637 | *stack_info_ptr = stack_info; |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame] | 638 | |
| 639 | return ERR(NONE); |
| 640 | } |
| 641 | |
Andreas Gampe | f6f3b5f | 2017-01-13 09:21:42 -0800 | [diff] [blame] | 642 | // Walks up the stack counting Java frames. This is not StackVisitor::ComputeNumFrames, as |
| 643 | // runtime methods and transitions must not be counted. |
| 644 | struct GetFrameCountVisitor : public art::StackVisitor { |
| 645 | explicit GetFrameCountVisitor(art::Thread* thread) |
| 646 | : art::StackVisitor(thread, nullptr, art::StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 647 | count(0) {} |
| 648 | |
| 649 | bool VisitFrame() REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 650 | art::ArtMethod* m = GetMethod(); |
| 651 | const bool do_count = !(m == nullptr || m->IsRuntimeMethod()); |
| 652 | if (do_count) { |
| 653 | count++; |
| 654 | } |
| 655 | return true; |
| 656 | } |
| 657 | |
| 658 | size_t count; |
| 659 | }; |
| 660 | |
| 661 | struct GetFrameCountClosure : public art::Closure { |
| 662 | public: |
| 663 | GetFrameCountClosure() : count(0) {} |
| 664 | |
| 665 | void Run(art::Thread* self) OVERRIDE REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 666 | GetFrameCountVisitor visitor(self); |
| 667 | visitor.WalkStack(false); |
| 668 | |
| 669 | count = visitor.count; |
| 670 | } |
| 671 | |
| 672 | size_t count; |
| 673 | }; |
| 674 | |
| 675 | jvmtiError StackUtil::GetFrameCount(jvmtiEnv* env ATTRIBUTE_UNUSED, |
| 676 | jthread java_thread, |
| 677 | jint* count_ptr) { |
| 678 | art::Thread* thread; |
| 679 | jvmtiError thread_error = GetThread(art::Thread::Current()->GetJniEnv(), java_thread, &thread); |
| 680 | if (thread_error != ERR(NONE)) { |
| 681 | return thread_error; |
| 682 | } |
| 683 | DCHECK(thread != nullptr); |
| 684 | |
| 685 | if (count_ptr == nullptr) { |
| 686 | return ERR(NULL_POINTER); |
| 687 | } |
| 688 | |
| 689 | GetFrameCountClosure closure; |
| 690 | thread->RequestSynchronousCheckpoint(&closure); |
| 691 | |
| 692 | *count_ptr = closure.count; |
| 693 | return ERR(NONE); |
| 694 | } |
| 695 | |
| 696 | // Walks up the stack 'n' callers, when used with Thread::WalkStack. |
| 697 | struct GetLocationVisitor : public art::StackVisitor { |
| 698 | GetLocationVisitor(art::Thread* thread, size_t n_in) |
| 699 | : art::StackVisitor(thread, nullptr, art::StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 700 | n(n_in), |
| 701 | count(0), |
| 702 | caller(nullptr), |
| 703 | caller_dex_pc(0) {} |
| 704 | |
| 705 | bool VisitFrame() REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 706 | art::ArtMethod* m = GetMethod(); |
| 707 | const bool do_count = !(m == nullptr || m->IsRuntimeMethod()); |
| 708 | if (do_count) { |
| 709 | DCHECK(caller == nullptr); |
| 710 | if (count == n) { |
| 711 | caller = m; |
| 712 | caller_dex_pc = GetDexPc(false); |
| 713 | return false; |
| 714 | } |
| 715 | count++; |
| 716 | } |
| 717 | return true; |
| 718 | } |
| 719 | |
| 720 | const size_t n; |
| 721 | size_t count; |
| 722 | art::ArtMethod* caller; |
| 723 | uint32_t caller_dex_pc; |
| 724 | }; |
| 725 | |
| 726 | struct GetLocationClosure : public art::Closure { |
| 727 | public: |
| 728 | explicit GetLocationClosure(size_t n_in) : n(n_in), method(nullptr), dex_pc(0) {} |
| 729 | |
| 730 | void Run(art::Thread* self) OVERRIDE REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 731 | GetLocationVisitor visitor(self, n); |
| 732 | visitor.WalkStack(false); |
| 733 | |
| 734 | method = visitor.caller; |
| 735 | dex_pc = visitor.caller_dex_pc; |
| 736 | } |
| 737 | |
| 738 | const size_t n; |
| 739 | art::ArtMethod* method; |
| 740 | uint32_t dex_pc; |
| 741 | }; |
| 742 | |
| 743 | jvmtiError StackUtil::GetFrameLocation(jvmtiEnv* env ATTRIBUTE_UNUSED, |
| 744 | jthread java_thread, |
| 745 | jint depth, |
| 746 | jmethodID* method_ptr, |
| 747 | jlocation* location_ptr) { |
| 748 | art::Thread* thread; |
| 749 | jvmtiError thread_error = GetThread(art::Thread::Current()->GetJniEnv(), java_thread, &thread); |
| 750 | if (thread_error != ERR(NONE)) { |
| 751 | return thread_error; |
| 752 | } |
| 753 | DCHECK(thread != nullptr); |
| 754 | |
| 755 | if (depth < 0) { |
| 756 | return ERR(ILLEGAL_ARGUMENT); |
| 757 | } |
| 758 | if (method_ptr == nullptr || location_ptr == nullptr) { |
| 759 | return ERR(NULL_POINTER); |
| 760 | } |
| 761 | |
| 762 | GetLocationClosure closure(static_cast<size_t>(depth)); |
| 763 | thread->RequestSynchronousCheckpoint(&closure); |
| 764 | |
| 765 | if (closure.method == nullptr) { |
| 766 | return ERR(NO_MORE_FRAMES); |
| 767 | } |
| 768 | |
| 769 | *method_ptr = art::jni::EncodeArtMethod(closure.method); |
| 770 | if (closure.method->IsNative()) { |
| 771 | *location_ptr = -1; |
| 772 | } else { |
| 773 | if (closure.dex_pc == art::DexFile::kDexNoIndex) { |
| 774 | return ERR(INTERNAL); |
| 775 | } |
| 776 | *location_ptr = static_cast<jlocation>(closure.dex_pc); |
| 777 | } |
| 778 | |
| 779 | return ERR(NONE); |
| 780 | } |
| 781 | |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 782 | } // namespace openjdkjvmti |