blob: 184fd804d05e62d52d7c039ecc06c9a3954899bb [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 Gampef6f3b5f2017-01-13 09:21:42 -0800181static jvmtiError GetThread(JNIEnv* env, jthread java_thread, art::Thread** thread) {
182 if (java_thread == nullptr) {
183 *thread = art::Thread::Current();
184 if (*thread == nullptr) {
185 // GetStackTrace can only be run during the live phase, so the current thread should be
186 // attached and thus available. Getting a null for current means we're starting up or
187 // dying.
188 return ERR(WRONG_PHASE);
189 }
190 } else {
191 if (!env->IsInstanceOf(java_thread, art::WellKnownClasses::java_lang_Thread)) {
192 return ERR(INVALID_THREAD);
193 }
194
195 // TODO: Need non-aborting call here, to return JVMTI_ERROR_INVALID_THREAD.
196 art::ScopedObjectAccess soa(art::Thread::Current());
197 art::MutexLock mu(soa.Self(), *art::Locks::thread_list_lock_);
198 *thread = art::Thread::FromManagedThread(soa, java_thread);
199 if (*thread == nullptr) {
200 return ERR(THREAD_NOT_ALIVE);
201 }
202 }
203 return ERR(NONE);
204}
205
Andreas Gampeb5eb94a2016-10-27 19:23:09 -0700206jvmtiError StackUtil::GetStackTrace(jvmtiEnv* jvmti_env ATTRIBUTE_UNUSED,
207 jthread java_thread,
208 jint start_depth,
209 jint max_frame_count,
210 jvmtiFrameInfo* frame_buffer,
211 jint* count_ptr) {
Andreas Gampeb5eb94a2016-10-27 19:23:09 -0700212 art::Thread* thread;
Andreas Gampef6f3b5f2017-01-13 09:21:42 -0800213 jvmtiError thread_error = GetThread(art::Thread::Current()->GetJniEnv(), java_thread, &thread);
214 if (thread_error != ERR(NONE)) {
215 return thread_error;
Andreas Gampeb5eb94a2016-10-27 19:23:09 -0700216 }
Andreas Gampef6f3b5f2017-01-13 09:21:42 -0800217 DCHECK(thread != nullptr);
Andreas Gampeb5eb94a2016-10-27 19:23:09 -0700218
219 art::ThreadState state = thread->GetState();
220 if (state == art::ThreadState::kStarting ||
221 state == art::ThreadState::kTerminated ||
222 thread->IsStillStarting()) {
223 return ERR(THREAD_NOT_ALIVE);
224 }
225
226 if (max_frame_count < 0) {
227 return ERR(ILLEGAL_ARGUMENT);
228 }
229 if (frame_buffer == nullptr || count_ptr == nullptr) {
230 return ERR(NULL_POINTER);
231 }
232
233 if (max_frame_count == 0) {
234 *count_ptr = 0;
235 return ERR(NONE);
236 }
237
Andreas Gampe6db6b4d2017-06-12 16:36:33 -0700238 GetStackTraceVectorClosure closure(start_depth >= 0 ? static_cast<size_t>(start_depth) : 0,
239 start_depth >= 0 ? static_cast<size_t>(max_frame_count) : 0);
Andreas Gampeb5eb94a2016-10-27 19:23:09 -0700240 thread->RequestSynchronousCheckpoint(&closure);
241
Andreas Gampea1a27c62017-01-11 16:37:16 -0800242 return TranslateFrameVector(closure.frames,
243 start_depth,
244 closure.start_result,
245 max_frame_count,
246 frame_buffer,
247 count_ptr);
248}
Andreas Gampeb5eb94a2016-10-27 19:23:09 -0700249
Andreas Gampea1a27c62017-01-11 16:37:16 -0800250jvmtiError 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 Gampeb5eb94a2016-10-27 19:23:09 -0700255 return ERR(ILLEGAL_ARGUMENT);
256 }
Andreas Gampea1a27c62017-01-11 16:37:16 -0800257 if (stack_info_ptr == nullptr || thread_count_ptr == nullptr) {
258 return ERR(NULL_POINTER);
259 }
Andreas Gampeb5eb94a2016-10-27 19:23:09 -0700260
Andreas Gampea1a27c62017-01-11 16:37:16 -0800261
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 Gampe984efb52017-01-12 17:43:13 -0800277 // Skip threads that are still starting.
278 if (thread->IsStillStarting()) {
279 continue;
280 }
281
Andreas Gampe6db6b4d2017-06-12 16:36:33 -0700282 GetStackTraceVectorClosure closure(0u, static_cast<size_t>(max_frame_count));
Andreas Gampea1a27c62017-01-11 16:37:16 -0800283 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.
Nicolas Geoffrayffc8cad2017-02-10 10:59:22 +0000312 DCHECK(self->GetPeerFromOtherThread() != nullptr);
Andreas Gampea1a27c62017-01-11 16:37:16 -0800313 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
Andreas Gampe202f85a2017-02-06 10:23:26 -0800361 jthread thread_peer = current->GetJniEnv()->AddLocalReference<jthread>(
362 threads[i]->GetPeerFromOtherThread());
Andreas Gampea1a27c62017-01-11 16:37:16 -0800363 new_stack_info.thread = thread_peer;
364
365 if (old_stack_info.frame_count > 0) {
366 // Only copy when there's data - leave the nullptr alone.
367 size_t frames_size = static_cast<size_t>(old_stack_info.frame_count) * sizeof(jvmtiFrameInfo);
368 memcpy(frame_info, old_stack_info.frame_buffer, frames_size);
369 new_stack_info.frame_buffer = frame_info;
370 frame_info += old_stack_info.frame_count;
371 }
372 }
373
374 *stack_info_ptr = stack_info;
375 *thread_count_ptr = static_cast<jint>(frames.size());
376
Andreas Gampeb5eb94a2016-10-27 19:23:09 -0700377 return ERR(NONE);
378}
379
Andreas Gampeeba32fb2017-01-12 17:40:05 -0800380jvmtiError StackUtil::GetThreadListStackTraces(jvmtiEnv* env,
381 jint thread_count,
382 const jthread* thread_list,
383 jint max_frame_count,
384 jvmtiStackInfo** stack_info_ptr) {
385 if (max_frame_count < 0) {
386 return ERR(ILLEGAL_ARGUMENT);
387 }
388 if (thread_count < 0) {
389 return ERR(ILLEGAL_ARGUMENT);
390 }
391 if (thread_count == 0) {
392 *stack_info_ptr = nullptr;
393 return ERR(NONE);
394 }
395 if (stack_info_ptr == nullptr || stack_info_ptr == nullptr) {
396 return ERR(NULL_POINTER);
397 }
398
399 art::Thread* current = art::Thread::Current();
400 art::ScopedObjectAccess soa(current); // Now we know we have the shared lock.
401
402 // Decode all threads to raw pointers. Put them into a handle scope to avoid any moving GC bugs.
403 art::VariableSizedHandleScope hs(current);
404 std::vector<art::Handle<art::mirror::Object>> handles;
405 for (jint i = 0; i != thread_count; ++i) {
406 if (thread_list[i] == nullptr) {
407 return ERR(INVALID_THREAD);
408 }
409 if (!soa.Env()->IsInstanceOf(thread_list[i], art::WellKnownClasses::java_lang_Thread)) {
410 return ERR(INVALID_THREAD);
411 }
412 handles.push_back(hs.NewHandle(soa.Decode<art::mirror::Object>(thread_list[i])));
413 }
414
415 std::vector<art::Thread*> threads;
416 std::vector<size_t> thread_list_indices;
417 std::vector<std::vector<jvmtiFrameInfo>> frames;
418
419 {
420 art::ScopedThreadSuspension sts(current, art::kWaitingForDebuggerSuspension);
421 art::ScopedSuspendAll ssa("GetThreadListStackTraces");
422
423 {
424 std::list<art::Thread*> art_thread_list;
425 {
426 art::MutexLock mu(current, *art::Locks::thread_list_lock_);
427 art_thread_list = art::Runtime::Current()->GetThreadList()->GetList();
428 }
429
430 for (art::Thread* thread : art_thread_list) {
431 if (thread->IsStillStarting()) {
432 // Skip this. We can't get the jpeer, and if it is for a thread in the thread_list,
433 // we'll just report STARTING.
434 continue;
435 }
436
437 // Get the peer, and check whether we know it.
Andreas Gampe202f85a2017-02-06 10:23:26 -0800438 art::ObjPtr<art::mirror::Object> peer = thread->GetPeerFromOtherThread();
Andreas Gampeeba32fb2017-01-12 17:40:05 -0800439 for (size_t index = 0; index != handles.size(); ++index) {
440 if (peer == handles[index].Get()) {
441 // Found the thread.
Andreas Gampe6db6b4d2017-06-12 16:36:33 -0700442 GetStackTraceVectorClosure closure(0u, static_cast<size_t>(max_frame_count));
Andreas Gampeeba32fb2017-01-12 17:40:05 -0800443 thread->RequestSynchronousCheckpoint(&closure);
444
445 threads.push_back(thread);
446 thread_list_indices.push_back(index);
447 frames.emplace_back();
448 frames.back().swap(closure.frames);
449
450 continue;
451 }
452 }
453
454 // Must be not started, or dead. We'll deal with it at the end.
455 }
456 }
457 }
458
459 // Convert the data into our output format.
460
461 // Note: we use an array of jvmtiStackInfo for convenience. The spec says we need to
462 // allocate one big chunk for this and the actual frames, which means we need
463 // to either be conservative or rearrange things later (the latter is implemented).
464 std::unique_ptr<jvmtiStackInfo[]> stack_info_array(new jvmtiStackInfo[frames.size()]);
465 std::vector<std::unique_ptr<jvmtiFrameInfo[]>> frame_infos;
466 frame_infos.reserve(frames.size());
467
468 // Now run through and add data for each thread.
469 size_t sum_frames = 0;
470 for (size_t index = 0; index < frames.size(); ++index) {
471 jvmtiStackInfo& stack_info = stack_info_array.get()[index];
472 memset(&stack_info, 0, sizeof(jvmtiStackInfo));
473
474 art::Thread* self = threads[index];
475 const std::vector<jvmtiFrameInfo>& thread_frames = frames[index];
476
477 // For the time being, set the thread to null. We don't have good ScopedLocalRef
478 // infrastructure.
Nicolas Geoffrayffc8cad2017-02-10 10:59:22 +0000479 DCHECK(self->GetPeerFromOtherThread() != nullptr);
Andreas Gampeeba32fb2017-01-12 17:40:05 -0800480 stack_info.thread = nullptr;
481 stack_info.state = JVMTI_THREAD_STATE_SUSPENDED;
482
483 size_t collected_frames = thread_frames.size();
484 if (max_frame_count == 0 || collected_frames == 0) {
485 stack_info.frame_count = 0;
486 stack_info.frame_buffer = nullptr;
487 continue;
488 }
489 DCHECK_LE(collected_frames, static_cast<size_t>(max_frame_count));
490
491 jvmtiFrameInfo* frame_info = new jvmtiFrameInfo[collected_frames];
492 frame_infos.emplace_back(frame_info);
493
494 jint count;
495 jvmtiError translate_result = TranslateFrameVector(thread_frames,
496 0,
497 0,
498 static_cast<jint>(collected_frames),
499 frame_info,
500 &count);
501 DCHECK(translate_result == JVMTI_ERROR_NONE);
502 stack_info.frame_count = static_cast<jint>(collected_frames);
503 stack_info.frame_buffer = frame_info;
504 sum_frames += static_cast<size_t>(count);
505 }
506
507 // No errors, yet. Now put it all into an output buffer. Note that this is not frames.size(),
508 // potentially.
509 size_t rounded_stack_info_size = art::RoundUp(sizeof(jvmtiStackInfo) * thread_count,
510 alignof(jvmtiFrameInfo));
511 size_t chunk_size = rounded_stack_info_size + sum_frames * sizeof(jvmtiFrameInfo);
512 unsigned char* chunk_data;
513 jvmtiError alloc_result = env->Allocate(chunk_size, &chunk_data);
514 if (alloc_result != ERR(NONE)) {
515 return alloc_result;
516 }
517
518 jvmtiStackInfo* stack_info = reinterpret_cast<jvmtiStackInfo*>(chunk_data);
519 jvmtiFrameInfo* frame_info = reinterpret_cast<jvmtiFrameInfo*>(
520 chunk_data + rounded_stack_info_size);
521
522 for (size_t i = 0; i < static_cast<size_t>(thread_count); ++i) {
523 // Check whether we found a running thread for this.
524 // Note: For simplicity, and with the expectation that the list is usually small, use a simple
525 // search. (The list is *not* sorted!)
526 auto it = std::find(thread_list_indices.begin(), thread_list_indices.end(), i);
527 if (it == thread_list_indices.end()) {
528 // No native thread. Must be new or dead. We need to fill out the stack info now.
529 // (Need to read the Java "started" field to know whether this is starting or terminated.)
530 art::ObjPtr<art::mirror::Object> peer = soa.Decode<art::mirror::Object>(thread_list[i]);
531 art::ObjPtr<art::mirror::Class> klass = peer->GetClass();
532 art::ArtField* started_field = klass->FindDeclaredInstanceField("started", "Z");
533 CHECK(started_field != nullptr);
534 bool started = started_field->GetBoolean(peer) != 0;
535 constexpr jint kStartedState = JVMTI_JAVA_LANG_THREAD_STATE_NEW;
536 constexpr jint kTerminatedState = JVMTI_THREAD_STATE_TERMINATED |
537 JVMTI_JAVA_LANG_THREAD_STATE_TERMINATED;
538 stack_info[i].thread = reinterpret_cast<JNIEnv*>(soa.Env())->NewLocalRef(thread_list[i]);
539 stack_info[i].state = started ? kTerminatedState : kStartedState;
540 stack_info[i].frame_count = 0;
541 stack_info[i].frame_buffer = nullptr;
542 } else {
543 // Had a native thread and frames.
544 size_t f_index = it - thread_list_indices.begin();
545
546 jvmtiStackInfo& old_stack_info = stack_info_array.get()[f_index];
547 jvmtiStackInfo& new_stack_info = stack_info[i];
548
549 memcpy(&new_stack_info, &old_stack_info, sizeof(jvmtiStackInfo));
550 new_stack_info.thread = reinterpret_cast<JNIEnv*>(soa.Env())->NewLocalRef(thread_list[i]);
551 if (old_stack_info.frame_count > 0) {
552 // Only copy when there's data - leave the nullptr alone.
553 size_t frames_size =
554 static_cast<size_t>(old_stack_info.frame_count) * sizeof(jvmtiFrameInfo);
555 memcpy(frame_info, old_stack_info.frame_buffer, frames_size);
556 new_stack_info.frame_buffer = frame_info;
557 frame_info += old_stack_info.frame_count;
558 }
559 }
560 }
561
562 * stack_info_ptr = stack_info;
563
564 return ERR(NONE);
565}
566
Andreas Gampef6f3b5f2017-01-13 09:21:42 -0800567// Walks up the stack counting Java frames. This is not StackVisitor::ComputeNumFrames, as
568// runtime methods and transitions must not be counted.
569struct GetFrameCountVisitor : public art::StackVisitor {
570 explicit GetFrameCountVisitor(art::Thread* thread)
571 : art::StackVisitor(thread, nullptr, art::StackVisitor::StackWalkKind::kIncludeInlinedFrames),
572 count(0) {}
573
574 bool VisitFrame() REQUIRES_SHARED(art::Locks::mutator_lock_) {
575 art::ArtMethod* m = GetMethod();
576 const bool do_count = !(m == nullptr || m->IsRuntimeMethod());
577 if (do_count) {
578 count++;
579 }
580 return true;
581 }
582
583 size_t count;
584};
585
586struct GetFrameCountClosure : public art::Closure {
587 public:
588 GetFrameCountClosure() : count(0) {}
589
590 void Run(art::Thread* self) OVERRIDE REQUIRES_SHARED(art::Locks::mutator_lock_) {
591 GetFrameCountVisitor visitor(self);
592 visitor.WalkStack(false);
593
594 count = visitor.count;
595 }
596
597 size_t count;
598};
599
600jvmtiError StackUtil::GetFrameCount(jvmtiEnv* env ATTRIBUTE_UNUSED,
601 jthread java_thread,
602 jint* count_ptr) {
603 art::Thread* thread;
604 jvmtiError thread_error = GetThread(art::Thread::Current()->GetJniEnv(), java_thread, &thread);
605 if (thread_error != ERR(NONE)) {
606 return thread_error;
607 }
608 DCHECK(thread != nullptr);
609
610 if (count_ptr == nullptr) {
611 return ERR(NULL_POINTER);
612 }
613
614 GetFrameCountClosure closure;
615 thread->RequestSynchronousCheckpoint(&closure);
616
617 *count_ptr = closure.count;
618 return ERR(NONE);
619}
620
621// Walks up the stack 'n' callers, when used with Thread::WalkStack.
622struct GetLocationVisitor : public art::StackVisitor {
623 GetLocationVisitor(art::Thread* thread, size_t n_in)
624 : art::StackVisitor(thread, nullptr, art::StackVisitor::StackWalkKind::kIncludeInlinedFrames),
625 n(n_in),
626 count(0),
627 caller(nullptr),
628 caller_dex_pc(0) {}
629
630 bool VisitFrame() REQUIRES_SHARED(art::Locks::mutator_lock_) {
631 art::ArtMethod* m = GetMethod();
632 const bool do_count = !(m == nullptr || m->IsRuntimeMethod());
633 if (do_count) {
634 DCHECK(caller == nullptr);
635 if (count == n) {
636 caller = m;
637 caller_dex_pc = GetDexPc(false);
638 return false;
639 }
640 count++;
641 }
642 return true;
643 }
644
645 const size_t n;
646 size_t count;
647 art::ArtMethod* caller;
648 uint32_t caller_dex_pc;
649};
650
651struct GetLocationClosure : public art::Closure {
652 public:
653 explicit GetLocationClosure(size_t n_in) : n(n_in), method(nullptr), dex_pc(0) {}
654
655 void Run(art::Thread* self) OVERRIDE REQUIRES_SHARED(art::Locks::mutator_lock_) {
656 GetLocationVisitor visitor(self, n);
657 visitor.WalkStack(false);
658
659 method = visitor.caller;
660 dex_pc = visitor.caller_dex_pc;
661 }
662
663 const size_t n;
664 art::ArtMethod* method;
665 uint32_t dex_pc;
666};
667
668jvmtiError StackUtil::GetFrameLocation(jvmtiEnv* env ATTRIBUTE_UNUSED,
669 jthread java_thread,
670 jint depth,
671 jmethodID* method_ptr,
672 jlocation* location_ptr) {
673 art::Thread* thread;
674 jvmtiError thread_error = GetThread(art::Thread::Current()->GetJniEnv(), java_thread, &thread);
675 if (thread_error != ERR(NONE)) {
676 return thread_error;
677 }
678 DCHECK(thread != nullptr);
679
680 if (depth < 0) {
681 return ERR(ILLEGAL_ARGUMENT);
682 }
683 if (method_ptr == nullptr || location_ptr == nullptr) {
684 return ERR(NULL_POINTER);
685 }
686
687 GetLocationClosure closure(static_cast<size_t>(depth));
688 thread->RequestSynchronousCheckpoint(&closure);
689
690 if (closure.method == nullptr) {
691 return ERR(NO_MORE_FRAMES);
692 }
693
694 *method_ptr = art::jni::EncodeArtMethod(closure.method);
695 if (closure.method->IsNative()) {
696 *location_ptr = -1;
697 } else {
698 if (closure.dex_pc == art::DexFile::kDexNoIndex) {
699 return ERR(INTERNAL);
700 }
701 *location_ptr = static_cast<jlocation>(closure.dex_pc);
702 }
703
704 return ERR(NONE);
705}
706
Andreas Gampeb5eb94a2016-10-27 19:23:09 -0700707} // namespace openjdkjvmti