xref: /aosp_15_r20/art/test/jvmti-common/StackTrace.java (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2017 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker package art;
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker import java.lang.reflect.Field;
20*795d594fSAndroid Build Coastguard Worker import java.lang.reflect.Executable;
21*795d594fSAndroid Build Coastguard Worker 
22*795d594fSAndroid Build Coastguard Worker public class StackTrace {
23*795d594fSAndroid Build Coastguard Worker   public static class StackFrameData {
24*795d594fSAndroid Build Coastguard Worker     public final Thread thr;
25*795d594fSAndroid Build Coastguard Worker     public final Executable method;
26*795d594fSAndroid Build Coastguard Worker     public final long current_location;
27*795d594fSAndroid Build Coastguard Worker     public final int depth;
28*795d594fSAndroid Build Coastguard Worker 
StackFrameData(Thread thr, Executable e, long loc, int depth)29*795d594fSAndroid Build Coastguard Worker     public StackFrameData(Thread thr, Executable e, long loc, int depth) {
30*795d594fSAndroid Build Coastguard Worker       this.thr = thr;
31*795d594fSAndroid Build Coastguard Worker       this.method = e;
32*795d594fSAndroid Build Coastguard Worker       this.current_location = loc;
33*795d594fSAndroid Build Coastguard Worker       this.depth = depth;
34*795d594fSAndroid Build Coastguard Worker     }
35*795d594fSAndroid Build Coastguard Worker     @Override
toString()36*795d594fSAndroid Build Coastguard Worker     public String toString() {
37*795d594fSAndroid Build Coastguard Worker       return String.format(
38*795d594fSAndroid Build Coastguard Worker           "StackFrameData { thr: '%s', method: '%s', loc: %d, depth: %d }",
39*795d594fSAndroid Build Coastguard Worker           this.thr,
40*795d594fSAndroid Build Coastguard Worker           this.method,
41*795d594fSAndroid Build Coastguard Worker           this.current_location,
42*795d594fSAndroid Build Coastguard Worker           this.depth);
43*795d594fSAndroid Build Coastguard Worker     }
44*795d594fSAndroid Build Coastguard Worker   }
45*795d594fSAndroid Build Coastguard Worker 
GetStackDepth(Thread thr)46*795d594fSAndroid Build Coastguard Worker   public static native int GetStackDepth(Thread thr);
47*795d594fSAndroid Build Coastguard Worker 
nativeGetStackTrace(Thread thr)48*795d594fSAndroid Build Coastguard Worker   private static native StackFrameData[] nativeGetStackTrace(Thread thr);
49*795d594fSAndroid Build Coastguard Worker 
GetStackTrace(Thread thr)50*795d594fSAndroid Build Coastguard Worker   public static StackFrameData[] GetStackTrace(Thread thr) {
51*795d594fSAndroid Build Coastguard Worker     // The RI seems to give inconsistent (and sometimes nonsensical) results if the thread is not
52*795d594fSAndroid Build Coastguard Worker     // suspended. The spec says that not being suspended is fine but since we want this to be
53*795d594fSAndroid Build Coastguard Worker     // consistent we will suspend for the RI.
54*795d594fSAndroid Build Coastguard Worker     boolean suspend_thread =
55*795d594fSAndroid Build Coastguard Worker         !System.getProperty("java.vm.name").equals("Dalvik") &&
56*795d594fSAndroid Build Coastguard Worker         !thr.equals(Thread.currentThread()) &&
57*795d594fSAndroid Build Coastguard Worker         !Suspension.isSuspended(thr);
58*795d594fSAndroid Build Coastguard Worker     if (suspend_thread) {
59*795d594fSAndroid Build Coastguard Worker       Suspension.suspend(thr);
60*795d594fSAndroid Build Coastguard Worker     }
61*795d594fSAndroid Build Coastguard Worker     StackFrameData[] out = nativeGetStackTrace(thr);
62*795d594fSAndroid Build Coastguard Worker     if (suspend_thread) {
63*795d594fSAndroid Build Coastguard Worker       Suspension.resume(thr);
64*795d594fSAndroid Build Coastguard Worker     }
65*795d594fSAndroid Build Coastguard Worker     return out;
66*795d594fSAndroid Build Coastguard Worker   }
67*795d594fSAndroid Build Coastguard Worker }
68*795d594fSAndroid Build Coastguard Worker 
69