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 | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 39 | #include "art_jvmti.h" |
| 40 | #include "art_method-inl.h" |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 41 | #include "base/bit_utils.h" |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 42 | #include "base/enums.h" |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 43 | #include "base/mutex.h" |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 44 | #include "dex_file.h" |
| 45 | #include "dex_file_annotations.h" |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame^] | 46 | #include "handle_scope-inl.h" |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 47 | #include "jni_env_ext.h" |
Andreas Gampe | 13b2784 | 2016-11-07 16:48:23 -0800 | [diff] [blame] | 48 | #include "jni_internal.h" |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 49 | #include "mirror/class.h" |
| 50 | #include "mirror/dex_cache.h" |
| 51 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 52 | #include "ScopedLocalRef.h" |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 53 | #include "stack.h" |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 54 | #include "thread-inl.h" |
| 55 | #include "thread_list.h" |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 56 | #include "thread_pool.h" |
| 57 | |
| 58 | namespace openjdkjvmti { |
| 59 | |
| 60 | struct GetStackTraceVisitor : public art::StackVisitor { |
| 61 | GetStackTraceVisitor(art::Thread* thread_in, |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 62 | size_t start_, |
| 63 | size_t stop_) |
| 64 | : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 65 | start(start_), |
| 66 | stop(stop_) {} |
| 67 | |
| 68 | bool VisitFrame() REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 69 | art::ArtMethod* m = GetMethod(); |
| 70 | if (m->IsRuntimeMethod()) { |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | if (start == 0) { |
| 75 | m = m->GetInterfaceMethodIfProxy(art::kRuntimePointerSize); |
Andreas Gampe | 13b2784 | 2016-11-07 16:48:23 -0800 | [diff] [blame] | 76 | jmethodID id = art::jni::EncodeArtMethod(m); |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 77 | |
Andreas Gampe | 2340e3f | 2016-12-12 19:37:19 -0800 | [diff] [blame] | 78 | uint32_t dex_pc = GetDexPc(false); |
| 79 | 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] | 80 | |
Andreas Gampe | 2340e3f | 2016-12-12 19:37:19 -0800 | [diff] [blame] | 81 | jvmtiFrameInfo info = { id, dex_location }; |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 82 | frames.push_back(info); |
| 83 | |
| 84 | if (stop == 1) { |
| 85 | return false; // We're done. |
| 86 | } else if (stop > 0) { |
| 87 | stop--; |
| 88 | } |
| 89 | } else { |
| 90 | start--; |
| 91 | } |
| 92 | |
| 93 | return true; |
| 94 | } |
| 95 | |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 96 | std::vector<jvmtiFrameInfo> frames; |
| 97 | size_t start; |
| 98 | size_t stop; |
| 99 | }; |
| 100 | |
| 101 | struct GetStackTraceClosure : public art::Closure { |
| 102 | public: |
| 103 | GetStackTraceClosure(size_t start, size_t stop) |
| 104 | : start_input(start), |
| 105 | stop_input(stop), |
| 106 | start_result(0), |
| 107 | stop_result(0) {} |
| 108 | |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 109 | void Run(art::Thread* self) OVERRIDE REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 110 | GetStackTraceVisitor visitor(self, start_input, stop_input); |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 111 | visitor.WalkStack(false); |
| 112 | |
| 113 | frames.swap(visitor.frames); |
| 114 | start_result = visitor.start; |
| 115 | stop_result = visitor.stop; |
| 116 | } |
| 117 | |
| 118 | const size_t start_input; |
| 119 | const size_t stop_input; |
| 120 | |
| 121 | std::vector<jvmtiFrameInfo> frames; |
| 122 | size_t start_result; |
| 123 | size_t stop_result; |
| 124 | }; |
| 125 | |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 126 | static jvmtiError TranslateFrameVector(const std::vector<jvmtiFrameInfo>& frames, |
| 127 | jint start_depth, |
| 128 | size_t start_result, |
| 129 | jint max_frame_count, |
| 130 | jvmtiFrameInfo* frame_buffer, |
| 131 | jint* count_ptr) { |
| 132 | size_t collected_frames = frames.size(); |
| 133 | |
| 134 | // Assume we're here having collected something. |
| 135 | DCHECK_GT(max_frame_count, 0); |
| 136 | |
| 137 | // Frames from the top. |
| 138 | if (start_depth >= 0) { |
| 139 | if (start_result != 0) { |
| 140 | // Not enough frames. |
| 141 | return ERR(ILLEGAL_ARGUMENT); |
| 142 | } |
| 143 | DCHECK_LE(collected_frames, static_cast<size_t>(max_frame_count)); |
| 144 | if (frames.size() > 0) { |
| 145 | memcpy(frame_buffer, frames.data(), collected_frames * sizeof(jvmtiFrameInfo)); |
| 146 | } |
| 147 | *count_ptr = static_cast<jint>(frames.size()); |
| 148 | return ERR(NONE); |
| 149 | } |
| 150 | |
| 151 | // Frames from the bottom. |
| 152 | if (collected_frames < static_cast<size_t>(-start_depth)) { |
| 153 | return ERR(ILLEGAL_ARGUMENT); |
| 154 | } |
| 155 | |
| 156 | size_t count = std::min(static_cast<size_t>(-start_depth), static_cast<size_t>(max_frame_count)); |
| 157 | memcpy(frame_buffer, |
| 158 | &frames.data()[collected_frames + start_depth], |
| 159 | count * sizeof(jvmtiFrameInfo)); |
| 160 | *count_ptr = static_cast<jint>(count); |
| 161 | return ERR(NONE); |
| 162 | } |
| 163 | |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 164 | jvmtiError StackUtil::GetStackTrace(jvmtiEnv* jvmti_env ATTRIBUTE_UNUSED, |
| 165 | jthread java_thread, |
| 166 | jint start_depth, |
| 167 | jint max_frame_count, |
| 168 | jvmtiFrameInfo* frame_buffer, |
| 169 | jint* count_ptr) { |
| 170 | if (java_thread == nullptr) { |
| 171 | return ERR(INVALID_THREAD); |
| 172 | } |
| 173 | |
| 174 | art::Thread* thread; |
| 175 | { |
| 176 | // TODO: Need non-aborting call here, to return JVMTI_ERROR_INVALID_THREAD. |
| 177 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 178 | art::MutexLock mu(soa.Self(), *art::Locks::thread_list_lock_); |
| 179 | thread = art::Thread::FromManagedThread(soa, java_thread); |
| 180 | DCHECK(thread != nullptr); |
| 181 | } |
| 182 | |
| 183 | art::ThreadState state = thread->GetState(); |
| 184 | if (state == art::ThreadState::kStarting || |
| 185 | state == art::ThreadState::kTerminated || |
| 186 | thread->IsStillStarting()) { |
| 187 | return ERR(THREAD_NOT_ALIVE); |
| 188 | } |
| 189 | |
| 190 | if (max_frame_count < 0) { |
| 191 | return ERR(ILLEGAL_ARGUMENT); |
| 192 | } |
| 193 | if (frame_buffer == nullptr || count_ptr == nullptr) { |
| 194 | return ERR(NULL_POINTER); |
| 195 | } |
| 196 | |
| 197 | if (max_frame_count == 0) { |
| 198 | *count_ptr = 0; |
| 199 | return ERR(NONE); |
| 200 | } |
| 201 | |
| 202 | GetStackTraceClosure closure(start_depth >= 0 ? static_cast<size_t>(start_depth) : 0, |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 203 | start_depth >= 0 ? static_cast<size_t>(max_frame_count) : 0); |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 204 | thread->RequestSynchronousCheckpoint(&closure); |
| 205 | |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 206 | return TranslateFrameVector(closure.frames, |
| 207 | start_depth, |
| 208 | closure.start_result, |
| 209 | max_frame_count, |
| 210 | frame_buffer, |
| 211 | count_ptr); |
| 212 | } |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 213 | |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 214 | struct GetAllStackTraceClosure : public art::Closure { |
| 215 | public: |
| 216 | explicit GetAllStackTraceClosure(size_t stop) |
| 217 | : start_input(0), |
| 218 | stop_input(stop), |
| 219 | frames_lock("GetAllStackTraceGuard", art::LockLevel::kAbortLock), |
| 220 | start_result(0), |
| 221 | stop_result(0) {} |
| 222 | |
| 223 | void Run(art::Thread* self) |
| 224 | OVERRIDE REQUIRES_SHARED(art::Locks::mutator_lock_) REQUIRES(!frames_lock) { |
| 225 | // self should be live here (so it could be suspended). No need to filter. |
| 226 | |
| 227 | art::Thread* current = art::Thread::Current(); |
| 228 | std::vector<jvmtiFrameInfo> self_frames; |
| 229 | |
| 230 | GetStackTraceVisitor visitor(self, start_input, stop_input); |
| 231 | visitor.WalkStack(false); |
| 232 | |
| 233 | self_frames.swap(visitor.frames); |
| 234 | |
| 235 | art::MutexLock mu(current, frames_lock); |
| 236 | frames.emplace(self, self_frames); |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 239 | const size_t start_input; |
| 240 | const size_t stop_input; |
| 241 | |
| 242 | art::Mutex frames_lock; |
| 243 | std::unordered_map<art::Thread*, std::vector<jvmtiFrameInfo>> frames GUARDED_BY(frames_lock); |
| 244 | size_t start_result; |
| 245 | size_t stop_result; |
| 246 | }; |
| 247 | |
| 248 | |
| 249 | |
| 250 | jvmtiError StackUtil::GetAllStackTraces(jvmtiEnv* env, |
| 251 | jint max_frame_count, |
| 252 | jvmtiStackInfo** stack_info_ptr, |
| 253 | jint* thread_count_ptr) { |
| 254 | if (max_frame_count < 0) { |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 255 | return ERR(ILLEGAL_ARGUMENT); |
| 256 | } |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 257 | if (stack_info_ptr == nullptr || thread_count_ptr == nullptr) { |
| 258 | return ERR(NULL_POINTER); |
| 259 | } |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 260 | |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 261 | |
| 262 | art::Thread* current = art::Thread::Current(); |
| 263 | art::ScopedObjectAccess soa(current); // Now we know we have the shared lock. |
| 264 | art::ScopedThreadSuspension sts(current, art::kWaitingForDebuggerSuspension); |
| 265 | art::ScopedSuspendAll ssa("GetAllStackTraces"); |
| 266 | |
| 267 | std::vector<art::Thread*> threads; |
| 268 | std::vector<std::vector<jvmtiFrameInfo>> frames; |
| 269 | { |
| 270 | std::list<art::Thread*> thread_list; |
| 271 | { |
| 272 | art::MutexLock mu(current, *art::Locks::thread_list_lock_); |
| 273 | thread_list = art::Runtime::Current()->GetThreadList()->GetList(); |
| 274 | } |
| 275 | |
| 276 | for (art::Thread* thread : thread_list) { |
Andreas Gampe | 984efb5 | 2017-01-12 17:43:13 -0800 | [diff] [blame] | 277 | // Skip threads that are still starting. |
| 278 | if (thread->IsStillStarting()) { |
| 279 | continue; |
| 280 | } |
| 281 | |
Andreas Gampe | a1a27c6 | 2017-01-11 16:37:16 -0800 | [diff] [blame] | 282 | GetStackTraceClosure closure(0u, static_cast<size_t>(max_frame_count)); |
| 283 | thread->RequestSynchronousCheckpoint(&closure); |
| 284 | |
| 285 | threads.push_back(thread); |
| 286 | frames.emplace_back(); |
| 287 | frames.back().swap(closure.frames); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | // Convert the data into our output format. Note: we need to keep the threads suspended, |
| 292 | // as we need to access them for their peers. |
| 293 | |
| 294 | // Note: we use an array of jvmtiStackInfo for convenience. The spec says we need to |
| 295 | // allocate one big chunk for this and the actual frames, which means we need |
| 296 | // to either be conservative or rearrange things later (the latter is implemented). |
| 297 | std::unique_ptr<jvmtiStackInfo[]> stack_info_array(new jvmtiStackInfo[frames.size()]); |
| 298 | std::vector<std::unique_ptr<jvmtiFrameInfo[]>> frame_infos; |
| 299 | frame_infos.reserve(frames.size()); |
| 300 | |
| 301 | // Now run through and add data for each thread. |
| 302 | size_t sum_frames = 0; |
| 303 | for (size_t index = 0; index < frames.size(); ++index) { |
| 304 | jvmtiStackInfo& stack_info = stack_info_array.get()[index]; |
| 305 | memset(&stack_info, 0, sizeof(jvmtiStackInfo)); |
| 306 | |
| 307 | art::Thread* self = threads[index]; |
| 308 | const std::vector<jvmtiFrameInfo>& thread_frames = frames[index]; |
| 309 | |
| 310 | // For the time being, set the thread to null. We don't have good ScopedLocalRef |
| 311 | // infrastructure. |
| 312 | DCHECK(self->GetPeer() != nullptr); |
| 313 | stack_info.thread = nullptr; |
| 314 | stack_info.state = JVMTI_THREAD_STATE_SUSPENDED; |
| 315 | |
| 316 | size_t collected_frames = thread_frames.size(); |
| 317 | if (max_frame_count == 0 || collected_frames == 0) { |
| 318 | stack_info.frame_count = 0; |
| 319 | stack_info.frame_buffer = nullptr; |
| 320 | continue; |
| 321 | } |
| 322 | DCHECK_LE(collected_frames, static_cast<size_t>(max_frame_count)); |
| 323 | |
| 324 | jvmtiFrameInfo* frame_info = new jvmtiFrameInfo[collected_frames]; |
| 325 | frame_infos.emplace_back(frame_info); |
| 326 | |
| 327 | jint count; |
| 328 | jvmtiError translate_result = TranslateFrameVector(thread_frames, |
| 329 | 0, |
| 330 | 0, |
| 331 | static_cast<jint>(collected_frames), |
| 332 | frame_info, |
| 333 | &count); |
| 334 | DCHECK(translate_result == JVMTI_ERROR_NONE); |
| 335 | stack_info.frame_count = static_cast<jint>(collected_frames); |
| 336 | stack_info.frame_buffer = frame_info; |
| 337 | sum_frames += static_cast<size_t>(count); |
| 338 | } |
| 339 | |
| 340 | // No errors, yet. Now put it all into an output buffer. |
| 341 | size_t rounded_stack_info_size = art::RoundUp(sizeof(jvmtiStackInfo) * frames.size(), |
| 342 | alignof(jvmtiFrameInfo)); |
| 343 | size_t chunk_size = rounded_stack_info_size + sum_frames * sizeof(jvmtiFrameInfo); |
| 344 | unsigned char* chunk_data; |
| 345 | jvmtiError alloc_result = env->Allocate(chunk_size, &chunk_data); |
| 346 | if (alloc_result != ERR(NONE)) { |
| 347 | return alloc_result; |
| 348 | } |
| 349 | |
| 350 | jvmtiStackInfo* stack_info = reinterpret_cast<jvmtiStackInfo*>(chunk_data); |
| 351 | // First copy in all the basic data. |
| 352 | memcpy(stack_info, stack_info_array.get(), sizeof(jvmtiStackInfo) * frames.size()); |
| 353 | |
| 354 | // Now copy the frames and fix up the pointers. |
| 355 | jvmtiFrameInfo* frame_info = reinterpret_cast<jvmtiFrameInfo*>( |
| 356 | chunk_data + rounded_stack_info_size); |
| 357 | for (size_t i = 0; i < frames.size(); ++i) { |
| 358 | jvmtiStackInfo& old_stack_info = stack_info_array.get()[i]; |
| 359 | jvmtiStackInfo& new_stack_info = stack_info[i]; |
| 360 | |
| 361 | jthread thread_peer = current->GetJniEnv()->AddLocalReference<jthread>(threads[i]->GetPeer()); |
| 362 | new_stack_info.thread = thread_peer; |
| 363 | |
| 364 | if (old_stack_info.frame_count > 0) { |
| 365 | // Only copy when there's data - leave the nullptr alone. |
| 366 | size_t frames_size = static_cast<size_t>(old_stack_info.frame_count) * sizeof(jvmtiFrameInfo); |
| 367 | memcpy(frame_info, old_stack_info.frame_buffer, frames_size); |
| 368 | new_stack_info.frame_buffer = frame_info; |
| 369 | frame_info += old_stack_info.frame_count; |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | *stack_info_ptr = stack_info; |
| 374 | *thread_count_ptr = static_cast<jint>(frames.size()); |
| 375 | |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 376 | return ERR(NONE); |
| 377 | } |
| 378 | |
Andreas Gampe | eba32fb | 2017-01-12 17:40:05 -0800 | [diff] [blame^] | 379 | jvmtiError StackUtil::GetThreadListStackTraces(jvmtiEnv* env, |
| 380 | jint thread_count, |
| 381 | const jthread* thread_list, |
| 382 | jint max_frame_count, |
| 383 | jvmtiStackInfo** stack_info_ptr) { |
| 384 | if (max_frame_count < 0) { |
| 385 | return ERR(ILLEGAL_ARGUMENT); |
| 386 | } |
| 387 | if (thread_count < 0) { |
| 388 | return ERR(ILLEGAL_ARGUMENT); |
| 389 | } |
| 390 | if (thread_count == 0) { |
| 391 | *stack_info_ptr = nullptr; |
| 392 | return ERR(NONE); |
| 393 | } |
| 394 | if (stack_info_ptr == nullptr || stack_info_ptr == nullptr) { |
| 395 | return ERR(NULL_POINTER); |
| 396 | } |
| 397 | |
| 398 | art::Thread* current = art::Thread::Current(); |
| 399 | art::ScopedObjectAccess soa(current); // Now we know we have the shared lock. |
| 400 | |
| 401 | // Decode all threads to raw pointers. Put them into a handle scope to avoid any moving GC bugs. |
| 402 | art::VariableSizedHandleScope hs(current); |
| 403 | std::vector<art::Handle<art::mirror::Object>> handles; |
| 404 | for (jint i = 0; i != thread_count; ++i) { |
| 405 | if (thread_list[i] == nullptr) { |
| 406 | return ERR(INVALID_THREAD); |
| 407 | } |
| 408 | if (!soa.Env()->IsInstanceOf(thread_list[i], art::WellKnownClasses::java_lang_Thread)) { |
| 409 | return ERR(INVALID_THREAD); |
| 410 | } |
| 411 | handles.push_back(hs.NewHandle(soa.Decode<art::mirror::Object>(thread_list[i]))); |
| 412 | } |
| 413 | |
| 414 | std::vector<art::Thread*> threads; |
| 415 | std::vector<size_t> thread_list_indices; |
| 416 | std::vector<std::vector<jvmtiFrameInfo>> frames; |
| 417 | |
| 418 | { |
| 419 | art::ScopedThreadSuspension sts(current, art::kWaitingForDebuggerSuspension); |
| 420 | art::ScopedSuspendAll ssa("GetThreadListStackTraces"); |
| 421 | |
| 422 | { |
| 423 | std::list<art::Thread*> art_thread_list; |
| 424 | { |
| 425 | art::MutexLock mu(current, *art::Locks::thread_list_lock_); |
| 426 | art_thread_list = art::Runtime::Current()->GetThreadList()->GetList(); |
| 427 | } |
| 428 | |
| 429 | for (art::Thread* thread : art_thread_list) { |
| 430 | if (thread->IsStillStarting()) { |
| 431 | // Skip this. We can't get the jpeer, and if it is for a thread in the thread_list, |
| 432 | // we'll just report STARTING. |
| 433 | continue; |
| 434 | } |
| 435 | |
| 436 | // Get the peer, and check whether we know it. |
| 437 | art::ObjPtr<art::mirror::Object> peer = thread->GetPeer(); |
| 438 | for (size_t index = 0; index != handles.size(); ++index) { |
| 439 | if (peer == handles[index].Get()) { |
| 440 | // Found the thread. |
| 441 | GetStackTraceClosure closure(0u, static_cast<size_t>(max_frame_count)); |
| 442 | thread->RequestSynchronousCheckpoint(&closure); |
| 443 | |
| 444 | threads.push_back(thread); |
| 445 | thread_list_indices.push_back(index); |
| 446 | frames.emplace_back(); |
| 447 | frames.back().swap(closure.frames); |
| 448 | |
| 449 | continue; |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | // Must be not started, or dead. We'll deal with it at the end. |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | // Convert the data into our output format. |
| 459 | |
| 460 | // Note: we use an array of jvmtiStackInfo for convenience. The spec says we need to |
| 461 | // allocate one big chunk for this and the actual frames, which means we need |
| 462 | // to either be conservative or rearrange things later (the latter is implemented). |
| 463 | std::unique_ptr<jvmtiStackInfo[]> stack_info_array(new jvmtiStackInfo[frames.size()]); |
| 464 | std::vector<std::unique_ptr<jvmtiFrameInfo[]>> frame_infos; |
| 465 | frame_infos.reserve(frames.size()); |
| 466 | |
| 467 | // Now run through and add data for each thread. |
| 468 | size_t sum_frames = 0; |
| 469 | for (size_t index = 0; index < frames.size(); ++index) { |
| 470 | jvmtiStackInfo& stack_info = stack_info_array.get()[index]; |
| 471 | memset(&stack_info, 0, sizeof(jvmtiStackInfo)); |
| 472 | |
| 473 | art::Thread* self = threads[index]; |
| 474 | const std::vector<jvmtiFrameInfo>& thread_frames = frames[index]; |
| 475 | |
| 476 | // For the time being, set the thread to null. We don't have good ScopedLocalRef |
| 477 | // infrastructure. |
| 478 | DCHECK(self->GetPeer() != nullptr); |
| 479 | stack_info.thread = nullptr; |
| 480 | stack_info.state = JVMTI_THREAD_STATE_SUSPENDED; |
| 481 | |
| 482 | size_t collected_frames = thread_frames.size(); |
| 483 | if (max_frame_count == 0 || collected_frames == 0) { |
| 484 | stack_info.frame_count = 0; |
| 485 | stack_info.frame_buffer = nullptr; |
| 486 | continue; |
| 487 | } |
| 488 | DCHECK_LE(collected_frames, static_cast<size_t>(max_frame_count)); |
| 489 | |
| 490 | jvmtiFrameInfo* frame_info = new jvmtiFrameInfo[collected_frames]; |
| 491 | frame_infos.emplace_back(frame_info); |
| 492 | |
| 493 | jint count; |
| 494 | jvmtiError translate_result = TranslateFrameVector(thread_frames, |
| 495 | 0, |
| 496 | 0, |
| 497 | static_cast<jint>(collected_frames), |
| 498 | frame_info, |
| 499 | &count); |
| 500 | DCHECK(translate_result == JVMTI_ERROR_NONE); |
| 501 | stack_info.frame_count = static_cast<jint>(collected_frames); |
| 502 | stack_info.frame_buffer = frame_info; |
| 503 | sum_frames += static_cast<size_t>(count); |
| 504 | } |
| 505 | |
| 506 | // No errors, yet. Now put it all into an output buffer. Note that this is not frames.size(), |
| 507 | // potentially. |
| 508 | size_t rounded_stack_info_size = art::RoundUp(sizeof(jvmtiStackInfo) * thread_count, |
| 509 | alignof(jvmtiFrameInfo)); |
| 510 | size_t chunk_size = rounded_stack_info_size + sum_frames * sizeof(jvmtiFrameInfo); |
| 511 | unsigned char* chunk_data; |
| 512 | jvmtiError alloc_result = env->Allocate(chunk_size, &chunk_data); |
| 513 | if (alloc_result != ERR(NONE)) { |
| 514 | return alloc_result; |
| 515 | } |
| 516 | |
| 517 | jvmtiStackInfo* stack_info = reinterpret_cast<jvmtiStackInfo*>(chunk_data); |
| 518 | jvmtiFrameInfo* frame_info = reinterpret_cast<jvmtiFrameInfo*>( |
| 519 | chunk_data + rounded_stack_info_size); |
| 520 | |
| 521 | for (size_t i = 0; i < static_cast<size_t>(thread_count); ++i) { |
| 522 | // Check whether we found a running thread for this. |
| 523 | // Note: For simplicity, and with the expectation that the list is usually small, use a simple |
| 524 | // search. (The list is *not* sorted!) |
| 525 | auto it = std::find(thread_list_indices.begin(), thread_list_indices.end(), i); |
| 526 | if (it == thread_list_indices.end()) { |
| 527 | // No native thread. Must be new or dead. We need to fill out the stack info now. |
| 528 | // (Need to read the Java "started" field to know whether this is starting or terminated.) |
| 529 | art::ObjPtr<art::mirror::Object> peer = soa.Decode<art::mirror::Object>(thread_list[i]); |
| 530 | art::ObjPtr<art::mirror::Class> klass = peer->GetClass(); |
| 531 | art::ArtField* started_field = klass->FindDeclaredInstanceField("started", "Z"); |
| 532 | CHECK(started_field != nullptr); |
| 533 | bool started = started_field->GetBoolean(peer) != 0; |
| 534 | constexpr jint kStartedState = JVMTI_JAVA_LANG_THREAD_STATE_NEW; |
| 535 | constexpr jint kTerminatedState = JVMTI_THREAD_STATE_TERMINATED | |
| 536 | JVMTI_JAVA_LANG_THREAD_STATE_TERMINATED; |
| 537 | stack_info[i].thread = reinterpret_cast<JNIEnv*>(soa.Env())->NewLocalRef(thread_list[i]); |
| 538 | stack_info[i].state = started ? kTerminatedState : kStartedState; |
| 539 | stack_info[i].frame_count = 0; |
| 540 | stack_info[i].frame_buffer = nullptr; |
| 541 | } else { |
| 542 | // Had a native thread and frames. |
| 543 | size_t f_index = it - thread_list_indices.begin(); |
| 544 | |
| 545 | jvmtiStackInfo& old_stack_info = stack_info_array.get()[f_index]; |
| 546 | jvmtiStackInfo& new_stack_info = stack_info[i]; |
| 547 | |
| 548 | memcpy(&new_stack_info, &old_stack_info, sizeof(jvmtiStackInfo)); |
| 549 | new_stack_info.thread = reinterpret_cast<JNIEnv*>(soa.Env())->NewLocalRef(thread_list[i]); |
| 550 | if (old_stack_info.frame_count > 0) { |
| 551 | // Only copy when there's data - leave the nullptr alone. |
| 552 | size_t frames_size = |
| 553 | static_cast<size_t>(old_stack_info.frame_count) * sizeof(jvmtiFrameInfo); |
| 554 | memcpy(frame_info, old_stack_info.frame_buffer, frames_size); |
| 555 | new_stack_info.frame_buffer = frame_info; |
| 556 | frame_info += old_stack_info.frame_count; |
| 557 | } |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | * stack_info_ptr = stack_info; |
| 562 | |
| 563 | return ERR(NONE); |
| 564 | } |
| 565 | |
Andreas Gampe | b5eb94a | 2016-10-27 19:23:09 -0700 | [diff] [blame] | 566 | } // namespace openjdkjvmti |