blob: 9f4002daf443e2ab1d48147386e3e642f38a1c14 [file] [log] [blame]
Andreas Gampeb5eb94a2016-10-27 19:23:09 -07001/* 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 Gampeeba32fb2017-01-12 17:40:05 -080034#include <algorithm>
Andreas Gampea1a27c62017-01-11 16:37:16 -080035#include <list>
36#include <unordered_map>
37#include <vector>
38
Andreas Gampea1d2f952017-04-20 22:53:58 -070039#include "art_field-inl.h"
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070040#include "art_method-inl.h"
Andreas Gampea1d2f952017-04-20 22:53:58 -070041#include "art_jvmti.h"
Andreas Gampea1a27c62017-01-11 16:37:16 -080042#include "base/bit_utils.h"
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070043#include "base/enums.h"
Andreas Gampea1a27c62017-01-11 16:37:16 -080044#include "base/mutex.h"
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070045#include "dex_file.h"
46#include "dex_file_annotations.h"
Andreas Gampeeba32fb2017-01-12 17:40:05 -080047#include "handle_scope-inl.h"
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070048#include "jni_env_ext.h"
Andreas Gampe13b27842016-11-07 16:48:23 -080049#include "jni_internal.h"
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070050#include "mirror/class.h"
51#include "mirror/dex_cache.h"
52#include "scoped_thread_state_change-inl.h"
Andreas Gampea1a27c62017-01-11 16:37:16 -080053#include "ScopedLocalRef.h"
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070054#include "stack.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070055#include "thread-current-inl.h"
Andreas Gampea1a27c62017-01-11 16:37:16 -080056#include "thread_list.h"
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070057#include "thread_pool.h"
Andreas Gampea1d2f952017-04-20 22:53:58 -070058#include "well_known_classes.h"
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070059
60namespace openjdkjvmti {
61
Andreas Gampe6db6b4d2017-06-12 16:36:33 -070062template <typename FrameFn>
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070063struct GetStackTraceVisitor : public art::StackVisitor {
64 GetStackTraceVisitor(art::Thread* thread_in,
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070065 size_t start_,
Andreas Gampe6db6b4d2017-06-12 16:36:33 -070066 size_t stop_,
67 FrameFn fn_)
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070068 : StackVisitor(thread_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
Andreas Gampe6db6b4d2017-06-12 16:36:33 -070069 fn(fn_),
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070070 start(start_),
71 stop(stop_) {}
Andreas Gampe6db6b4d2017-06-12 16:36:33 -070072 GetStackTraceVisitor(const GetStackTraceVisitor&) = default;
73 GetStackTraceVisitor(GetStackTraceVisitor&&) = default;
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070074
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 Gampe13b27842016-11-07 16:48:23 -080083 jmethodID id = art::jni::EncodeArtMethod(m);
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070084
Andreas Gampe2340e3f2016-12-12 19:37:19 -080085 uint32_t dex_pc = GetDexPc(false);
86 jlong dex_location = (dex_pc == art::DexFile::kDexNoIndex) ? -1 : static_cast<jlong>(dex_pc);
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070087
Andreas Gampe2340e3f2016-12-12 19:37:19 -080088 jvmtiFrameInfo info = { id, dex_location };
Andreas Gampe6db6b4d2017-06-12 16:36:33 -070089 fn(info);
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070090
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 Gampe6db6b4d2017-06-12 16:36:33 -0700103 FrameFn fn;
Andreas Gampeb5eb94a2016-10-27 19:23:09 -0700104 size_t start;
105 size_t stop;
106};
107
Andreas Gampe6db6b4d2017-06-12 16:36:33 -0700108template <typename FrameFn>
109GetStackTraceVisitor<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
116struct GetStackTraceVectorClosure : public art::Closure {
Andreas Gampeb5eb94a2016-10-27 19:23:09 -0700117 public:
Andreas Gampe6db6b4d2017-06-12 16:36:33 -0700118 GetStackTraceVectorClosure(size_t start, size_t stop)
Andreas Gampeb5eb94a2016-10-27 19:23:09 -0700119 : start_input(start),
120 stop_input(stop),
121 start_result(0),
122 stop_result(0) {}
123
Andreas Gampea1a27c62017-01-11 16:37:16 -0800124 void Run(art::Thread* self) OVERRIDE REQUIRES_SHARED(art::Locks::mutator_lock_) {
Andreas Gampe6db6b4d2017-06-12 16:36:33 -0700125 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 Gampeb5eb94a2016-10-27 19:23:09 -0700130
Andreas Gampeb5eb94a2016-10-27 19:23:09 -0700131 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 Gampea1a27c62017-01-11 16:37:16 -0800143static 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 Gampe850a0fe2017-06-12 18:37:19 -0700181struct 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 Gampef6f3b5f2017-01-13 09:21:42 -0800208static 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 Gampeb5eb94a2016-10-27 19:23:09 -0700233jvmtiError 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 Gampeb5eb94a2016-10-27 19:23:09 -0700239 art::Thread* thread;
Andreas Gampef6f3b5f2017-01-13 09:21:42 -0800240 jvmtiError thread_error = GetThread(art::Thread::Current()->GetJniEnv(), java_thread, &thread);
241 if (thread_error != ERR(NONE)) {
242 return thread_error;
Andreas Gampeb5eb94a2016-10-27 19:23:09 -0700243 }
Andreas Gampef6f3b5f2017-01-13 09:21:42 -0800244 DCHECK(thread != nullptr);
Andreas Gampeb5eb94a2016-10-27 19:23:09 -0700245
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 Gampe850a0fe2017-06-12 18:37:19 -0700265 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 Gampeb5eb94a2016-10-27 19:23:09 -0700279 thread->RequestSynchronousCheckpoint(&closure);
280
Andreas Gampea1a27c62017-01-11 16:37:16 -0800281 return TranslateFrameVector(closure.frames,
282 start_depth,
283 closure.start_result,
284 max_frame_count,
285 frame_buffer,
286 count_ptr);
287}
Andreas Gampeb5eb94a2016-10-27 19:23:09 -0700288
Andreas Gampef1221a12017-06-21 21:20:47 -0700289template <typename Data>
290struct 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 Gampea1a27c62017-01-11 16:37:16 -0800320jvmtiError 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 Gampeb5eb94a2016-10-27 19:23:09 -0700325 return ERR(ILLEGAL_ARGUMENT);
326 }
Andreas Gampea1a27c62017-01-11 16:37:16 -0800327 if (stack_info_ptr == nullptr || thread_count_ptr == nullptr) {
328 return ERR(NULL_POINTER);
329 }
Andreas Gampeb5eb94a2016-10-27 19:23:09 -0700330
Andreas Gampef1221a12017-06-21 21:20:47 -0700331 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 Gampea1a27c62017-01-11 16:37:16 -0800338 }
339
Andreas Gampef1221a12017-06-21 21:20:47 -0700340 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 Gampea1a27c62017-01-11 16:37:16 -0800344
345 threads.push_back(thread);
Andreas Gampea1a27c62017-01-11 16:37:16 -0800346
Andreas Gampef1221a12017-06-21 21:20:47 -0700347 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 Gampea1a27c62017-01-11 16:37:16 -0800374
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 Gampef1221a12017-06-21 21:20:47 -0700378 std::unique_ptr<jvmtiStackInfo[]> stack_info_array(new jvmtiStackInfo[data.frames.size()]);
Andreas Gampea1a27c62017-01-11 16:37:16 -0800379 std::vector<std::unique_ptr<jvmtiFrameInfo[]>> frame_infos;
Andreas Gampef1221a12017-06-21 21:20:47 -0700380 frame_infos.reserve(data.frames.size());
Andreas Gampea1a27c62017-01-11 16:37:16 -0800381
382 // Now run through and add data for each thread.
383 size_t sum_frames = 0;
Andreas Gampef1221a12017-06-21 21:20:47 -0700384 for (size_t index = 0; index < data.frames.size(); ++index) {
Andreas Gampea1a27c62017-01-11 16:37:16 -0800385 jvmtiStackInfo& stack_info = stack_info_array.get()[index];
386 memset(&stack_info, 0, sizeof(jvmtiStackInfo));
387
Andreas Gampef1221a12017-06-21 21:20:47 -0700388 const std::vector<jvmtiFrameInfo>& thread_frames = data.frames[index];
Andreas Gampea1a27c62017-01-11 16:37:16 -0800389
Andreas Gampef1221a12017-06-21 21:20:47 -0700390 // For the time being, set the thread to null. We'll fix it up in the second stage.
Andreas Gampea1a27c62017-01-11 16:37:16 -0800391 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 Gampef1221a12017-06-21 21:20:47 -0700419 size_t rounded_stack_info_size = art::RoundUp(sizeof(jvmtiStackInfo) * data.frames.size(),
Andreas Gampea1a27c62017-01-11 16:37:16 -0800420 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 Gampef1221a12017-06-21 21:20:47 -0700430 memcpy(stack_info, stack_info_array.get(), sizeof(jvmtiStackInfo) * data.frames.size());
Andreas Gampea1a27c62017-01-11 16:37:16 -0800431
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 Gampef1221a12017-06-21 21:20:47 -0700435 for (size_t i = 0; i < data.frames.size(); ++i) {
Andreas Gampea1a27c62017-01-11 16:37:16 -0800436 jvmtiStackInfo& old_stack_info = stack_info_array.get()[i];
437 jvmtiStackInfo& new_stack_info = stack_info[i];
438
Andreas Gampef1221a12017-06-21 21:20:47 -0700439 // 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 Gampea1a27c62017-01-11 16:37:16 -0800442
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 Gampef1221a12017-06-21 21:20:47 -0700453 *thread_count_ptr = static_cast<jint>(data.frames.size());
Andreas Gampea1a27c62017-01-11 16:37:16 -0800454
Andreas Gampeb5eb94a2016-10-27 19:23:09 -0700455 return ERR(NONE);
456}
457
Andreas Gampeeba32fb2017-01-12 17:40:05 -0800458jvmtiError 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 Gampef1221a12017-06-21 21:20:47 -0700480 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 Gampeeba32fb2017-01-12 17:40:05 -0800518 // Decode all threads to raw pointers. Put them into a handle scope to avoid any moving GC bugs.
519 art::VariableSizedHandleScope hs(current);
Andreas Gampeeba32fb2017-01-12 17:40:05 -0800520 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 Gampef1221a12017-06-21 21:20:47 -0700527 data.handles.push_back(hs.NewHandle(soa.Decode<art::mirror::Object>(thread_list[i])));
Andreas Gampeeba32fb2017-01-12 17:40:05 -0800528 }
529
Andreas Gampef1221a12017-06-21 21:20:47 -0700530 GetAllStackTracesVectorClosure<SelectStackTracesData> closure(
531 static_cast<size_t>(max_frame_count), &data);
532 art::Runtime::Current()->GetThreadList()->RunCheckpoint(&closure, nullptr);
Andreas Gampeeba32fb2017-01-12 17:40:05 -0800533
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 Gampef1221a12017-06-21 21:20:47 -0700539 std::unique_ptr<jvmtiStackInfo[]> stack_info_array(new jvmtiStackInfo[data.frames.size()]);
Andreas Gampeeba32fb2017-01-12 17:40:05 -0800540 std::vector<std::unique_ptr<jvmtiFrameInfo[]>> frame_infos;
Andreas Gampef1221a12017-06-21 21:20:47 -0700541 frame_infos.reserve(data.frames.size());
Andreas Gampeeba32fb2017-01-12 17:40:05 -0800542
543 // Now run through and add data for each thread.
544 size_t sum_frames = 0;
Andreas Gampef1221a12017-06-21 21:20:47 -0700545 for (size_t index = 0; index < data.frames.size(); ++index) {
Andreas Gampeeba32fb2017-01-12 17:40:05 -0800546 jvmtiStackInfo& stack_info = stack_info_array.get()[index];
547 memset(&stack_info, 0, sizeof(jvmtiStackInfo));
548
Andreas Gampef1221a12017-06-21 21:20:47 -0700549 art::Thread* self = data.threads[index];
550 const std::vector<jvmtiFrameInfo>& thread_frames = data.frames[index];
Andreas Gampeeba32fb2017-01-12 17:40:05 -0800551
552 // For the time being, set the thread to null. We don't have good ScopedLocalRef
553 // infrastructure.
Nicolas Geoffrayffc8cad2017-02-10 10:59:22 +0000554 DCHECK(self->GetPeerFromOtherThread() != nullptr);
Andreas Gampeeba32fb2017-01-12 17:40:05 -0800555 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 Gampef1221a12017-06-21 21:20:47 -0700601 auto it = std::find(data.thread_list_indices.begin(), data.thread_list_indices.end(), i);
602 if (it == data.thread_list_indices.end()) {
Andreas Gampeeba32fb2017-01-12 17:40:05 -0800603 // 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 Gampef1221a12017-06-21 21:20:47 -0700619 size_t f_index = it - data.thread_list_indices.begin();
Andreas Gampeeba32fb2017-01-12 17:40:05 -0800620
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 Gampef1221a12017-06-21 21:20:47 -0700637 *stack_info_ptr = stack_info;
Andreas Gampeeba32fb2017-01-12 17:40:05 -0800638
639 return ERR(NONE);
640}
641
Andreas Gampef6f3b5f2017-01-13 09:21:42 -0800642// Walks up the stack counting Java frames. This is not StackVisitor::ComputeNumFrames, as
643// runtime methods and transitions must not be counted.
644struct 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
661struct 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
675jvmtiError 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.
697struct 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
726struct 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
743jvmtiError 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 Gampeb5eb94a2016-10-27 19:23:09 -0700782} // namespace openjdkjvmti