xref: /aosp_15_r20/art/openjdkjvmti/ti_stack.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /* Copyright (C) 2016 The Android Open Source Project
2*795d594fSAndroid Build Coastguard Worker  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * This file implements interfaces from the file jvmti.h. This implementation
5*795d594fSAndroid Build Coastguard Worker  * is licensed under the same terms as the file jvmti.h.  The
6*795d594fSAndroid Build Coastguard Worker  * copyright and license information for the file jvmti.h follows.
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
9*795d594fSAndroid Build Coastguard Worker  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
10*795d594fSAndroid Build Coastguard Worker  *
11*795d594fSAndroid Build Coastguard Worker  * This code is free software; you can redistribute it and/or modify it
12*795d594fSAndroid Build Coastguard Worker  * under the terms of the GNU General Public License version 2 only, as
13*795d594fSAndroid Build Coastguard Worker  * published by the Free Software Foundation.  Oracle designates this
14*795d594fSAndroid Build Coastguard Worker  * particular file as subject to the "Classpath" exception as provided
15*795d594fSAndroid Build Coastguard Worker  * by Oracle in the LICENSE file that accompanied this code.
16*795d594fSAndroid Build Coastguard Worker  *
17*795d594fSAndroid Build Coastguard Worker  * This code is distributed in the hope that it will be useful, but WITHOUT
18*795d594fSAndroid Build Coastguard Worker  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19*795d594fSAndroid Build Coastguard Worker  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20*795d594fSAndroid Build Coastguard Worker  * version 2 for more details (a copy is included in the LICENSE file that
21*795d594fSAndroid Build Coastguard Worker  * accompanied this code).
22*795d594fSAndroid Build Coastguard Worker  *
23*795d594fSAndroid Build Coastguard Worker  * You should have received a copy of the GNU General Public License version
24*795d594fSAndroid Build Coastguard Worker  * 2 along with this work; if not, write to the Free Software Foundation,
25*795d594fSAndroid Build Coastguard Worker  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26*795d594fSAndroid Build Coastguard Worker  *
27*795d594fSAndroid Build Coastguard Worker  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
28*795d594fSAndroid Build Coastguard Worker  * or visit www.oracle.com if you need additional information or have any
29*795d594fSAndroid Build Coastguard Worker  * questions.
30*795d594fSAndroid Build Coastguard Worker  */
31*795d594fSAndroid Build Coastguard Worker 
32*795d594fSAndroid Build Coastguard Worker #ifndef ART_OPENJDKJVMTI_TI_STACK_H_
33*795d594fSAndroid Build Coastguard Worker #define ART_OPENJDKJVMTI_TI_STACK_H_
34*795d594fSAndroid Build Coastguard Worker 
35*795d594fSAndroid Build Coastguard Worker #include "jni.h"
36*795d594fSAndroid Build Coastguard Worker #include "jvmti.h"
37*795d594fSAndroid Build Coastguard Worker 
38*795d594fSAndroid Build Coastguard Worker #include "art_method.h"
39*795d594fSAndroid Build Coastguard Worker #include "base/mutex.h"
40*795d594fSAndroid Build Coastguard Worker #include "events.h"
41*795d594fSAndroid Build Coastguard Worker #include "stack.h"
42*795d594fSAndroid Build Coastguard Worker 
43*795d594fSAndroid Build Coastguard Worker namespace openjdkjvmti {
44*795d594fSAndroid Build Coastguard Worker 
45*795d594fSAndroid Build Coastguard Worker class StackUtil {
46*795d594fSAndroid Build Coastguard Worker  public:
47*795d594fSAndroid Build Coastguard Worker   static jvmtiError GetAllStackTraces(jvmtiEnv* env,
48*795d594fSAndroid Build Coastguard Worker                                       jint max_frame_count,
49*795d594fSAndroid Build Coastguard Worker                                       jvmtiStackInfo** stack_info_ptr,
50*795d594fSAndroid Build Coastguard Worker                                       jint* thread_count_ptr)
51*795d594fSAndroid Build Coastguard Worker       REQUIRES(!art::Locks::thread_list_lock_);
52*795d594fSAndroid Build Coastguard Worker 
53*795d594fSAndroid Build Coastguard Worker   static jvmtiError GetFrameCount(jvmtiEnv* env, jthread thread, jint* count_ptr);
54*795d594fSAndroid Build Coastguard Worker 
55*795d594fSAndroid Build Coastguard Worker   static jvmtiError GetFrameLocation(jvmtiEnv* env,
56*795d594fSAndroid Build Coastguard Worker                                      jthread thread,
57*795d594fSAndroid Build Coastguard Worker                                      jint depth,
58*795d594fSAndroid Build Coastguard Worker                                      jmethodID* method_ptr,
59*795d594fSAndroid Build Coastguard Worker                                      jlocation* location_ptr);
60*795d594fSAndroid Build Coastguard Worker 
61*795d594fSAndroid Build Coastguard Worker   static jvmtiError GetStackTrace(jvmtiEnv* env,
62*795d594fSAndroid Build Coastguard Worker                                   jthread thread,
63*795d594fSAndroid Build Coastguard Worker                                   jint start_depth,
64*795d594fSAndroid Build Coastguard Worker                                   jint max_frame_count,
65*795d594fSAndroid Build Coastguard Worker                                   jvmtiFrameInfo* frame_buffer,
66*795d594fSAndroid Build Coastguard Worker                                   jint* count_ptr);
67*795d594fSAndroid Build Coastguard Worker 
68*795d594fSAndroid Build Coastguard Worker   static jvmtiError GetThreadListStackTraces(jvmtiEnv* env,
69*795d594fSAndroid Build Coastguard Worker                                              jint thread_count,
70*795d594fSAndroid Build Coastguard Worker                                              const jthread* thread_list,
71*795d594fSAndroid Build Coastguard Worker                                              jint max_frame_count,
72*795d594fSAndroid Build Coastguard Worker                                              jvmtiStackInfo** stack_info_ptr);
73*795d594fSAndroid Build Coastguard Worker 
74*795d594fSAndroid Build Coastguard Worker   static jvmtiError GetOwnedMonitorStackDepthInfo(jvmtiEnv* env,
75*795d594fSAndroid Build Coastguard Worker                                                   jthread thread,
76*795d594fSAndroid Build Coastguard Worker                                                   jint* info_cnt_ptr,
77*795d594fSAndroid Build Coastguard Worker                                                   jvmtiMonitorStackDepthInfo** info_ptr);
78*795d594fSAndroid Build Coastguard Worker 
79*795d594fSAndroid Build Coastguard Worker   static jvmtiError GetOwnedMonitorInfo(jvmtiEnv* env,
80*795d594fSAndroid Build Coastguard Worker                                         jthread thread,
81*795d594fSAndroid Build Coastguard Worker                                         jint* owned_monitor_count_ptr,
82*795d594fSAndroid Build Coastguard Worker                                         jobject** owned_monitors_ptr);
83*795d594fSAndroid Build Coastguard Worker 
84*795d594fSAndroid Build Coastguard Worker   static jvmtiError NotifyFramePop(jvmtiEnv* env, jthread thread, jint depth);
85*795d594fSAndroid Build Coastguard Worker 
86*795d594fSAndroid Build Coastguard Worker   static jvmtiError PopFrame(jvmtiEnv* env, jthread thread);
87*795d594fSAndroid Build Coastguard Worker 
88*795d594fSAndroid Build Coastguard Worker   template <typename T>
89*795d594fSAndroid Build Coastguard Worker   static jvmtiError ForceEarlyReturn(
90*795d594fSAndroid Build Coastguard Worker       jvmtiEnv* env, EventHandler* event_handler, jthread thread, T value);
91*795d594fSAndroid Build Coastguard Worker };
92*795d594fSAndroid Build Coastguard Worker 
93*795d594fSAndroid Build Coastguard Worker struct FindFrameAtDepthVisitor : art::StackVisitor {
94*795d594fSAndroid Build Coastguard Worker  public:
FindFrameAtDepthVisitorFindFrameAtDepthVisitor95*795d594fSAndroid Build Coastguard Worker   FindFrameAtDepthVisitor(art::Thread* target, art::Context* ctx, jint depth)
96*795d594fSAndroid Build Coastguard Worker       REQUIRES_SHARED(art::Locks::mutator_lock_)
97*795d594fSAndroid Build Coastguard Worker       : art::StackVisitor(target, ctx, art::StackVisitor::StackWalkKind::kIncludeInlinedFrames),
98*795d594fSAndroid Build Coastguard Worker         found_frame_(false),
99*795d594fSAndroid Build Coastguard Worker         cnt_(0),
100*795d594fSAndroid Build Coastguard Worker         depth_(static_cast<size_t>(depth)) { }
101*795d594fSAndroid Build Coastguard Worker 
FoundFrameFindFrameAtDepthVisitor102*795d594fSAndroid Build Coastguard Worker   bool FoundFrame() {
103*795d594fSAndroid Build Coastguard Worker     return found_frame_;
104*795d594fSAndroid Build Coastguard Worker   }
105*795d594fSAndroid Build Coastguard Worker 
VisitFrameFindFrameAtDepthVisitor106*795d594fSAndroid Build Coastguard Worker   bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
107*795d594fSAndroid Build Coastguard Worker     if (GetMethod()->IsRuntimeMethod()) {
108*795d594fSAndroid Build Coastguard Worker       return true;
109*795d594fSAndroid Build Coastguard Worker     }
110*795d594fSAndroid Build Coastguard Worker     if (cnt_ == depth_) {
111*795d594fSAndroid Build Coastguard Worker       // We found our frame, exit.
112*795d594fSAndroid Build Coastguard Worker       found_frame_ = true;
113*795d594fSAndroid Build Coastguard Worker       return false;
114*795d594fSAndroid Build Coastguard Worker     } else {
115*795d594fSAndroid Build Coastguard Worker       cnt_++;
116*795d594fSAndroid Build Coastguard Worker       return true;
117*795d594fSAndroid Build Coastguard Worker     }
118*795d594fSAndroid Build Coastguard Worker   }
119*795d594fSAndroid Build Coastguard Worker 
120*795d594fSAndroid Build Coastguard Worker   art::ShadowFrame* GetOrCreateShadowFrame(/*out*/bool* created_frame)
121*795d594fSAndroid Build Coastguard Worker       REQUIRES_SHARED(art::Locks::mutator_lock_);
122*795d594fSAndroid Build Coastguard Worker 
123*795d594fSAndroid Build Coastguard Worker  private:
124*795d594fSAndroid Build Coastguard Worker   bool found_frame_;
125*795d594fSAndroid Build Coastguard Worker   size_t cnt_;
126*795d594fSAndroid Build Coastguard Worker   size_t depth_;
127*795d594fSAndroid Build Coastguard Worker };
128*795d594fSAndroid Build Coastguard Worker 
129*795d594fSAndroid Build Coastguard Worker }  // namespace openjdkjvmti
130*795d594fSAndroid Build Coastguard Worker 
131*795d594fSAndroid Build Coastguard Worker #endif  // ART_OPENJDKJVMTI_TI_STACK_H_
132