xref: /aosp_15_r20/art/test/911-get-stack-trace/src/art/Frames.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.util.Arrays;
20*795d594fSAndroid Build Coastguard Worker 
21*795d594fSAndroid Build Coastguard Worker public class Frames {
doTest()22*795d594fSAndroid Build Coastguard Worker   public static void doTest() throws Exception {
23*795d594fSAndroid Build Coastguard Worker     doTestSameThread();
24*795d594fSAndroid Build Coastguard Worker 
25*795d594fSAndroid Build Coastguard Worker     System.out.println();
26*795d594fSAndroid Build Coastguard Worker 
27*795d594fSAndroid Build Coastguard Worker     doTestOtherThreadWait();
28*795d594fSAndroid Build Coastguard Worker 
29*795d594fSAndroid Build Coastguard Worker     System.out.println();
30*795d594fSAndroid Build Coastguard Worker 
31*795d594fSAndroid Build Coastguard Worker     doTestOtherThreadBusyLoop();
32*795d594fSAndroid Build Coastguard Worker   }
33*795d594fSAndroid Build Coastguard Worker 
doTestSameThread()34*795d594fSAndroid Build Coastguard Worker   public static void doTestSameThread() {
35*795d594fSAndroid Build Coastguard Worker     System.out.println("###################");
36*795d594fSAndroid Build Coastguard Worker     System.out.println("### Same thread ###");
37*795d594fSAndroid Build Coastguard Worker     System.out.println("###################");
38*795d594fSAndroid Build Coastguard Worker 
39*795d594fSAndroid Build Coastguard Worker     Thread t = Thread.currentThread();
40*795d594fSAndroid Build Coastguard Worker 
41*795d594fSAndroid Build Coastguard Worker     int count = getFrameCount(t);
42*795d594fSAndroid Build Coastguard Worker     System.out.println(count);
43*795d594fSAndroid Build Coastguard Worker     try {
44*795d594fSAndroid Build Coastguard Worker       System.out.println(Arrays.toString(getFrameLocation(t, -1)));
45*795d594fSAndroid Build Coastguard Worker     } catch (RuntimeException e) {
46*795d594fSAndroid Build Coastguard Worker       System.out.println(e.getMessage());
47*795d594fSAndroid Build Coastguard Worker     }
48*795d594fSAndroid Build Coastguard Worker     for (int i = 0; i < count; i++) {
49*795d594fSAndroid Build Coastguard Worker       System.out.println(Arrays.toString(getFrameLocation(t, i)));
50*795d594fSAndroid Build Coastguard Worker     }
51*795d594fSAndroid Build Coastguard Worker     try {
52*795d594fSAndroid Build Coastguard Worker       System.out.println(Arrays.toString(getFrameLocation(t, count)));
53*795d594fSAndroid Build Coastguard Worker     } catch (RuntimeException e) {
54*795d594fSAndroid Build Coastguard Worker       System.out.println(e.getMessage());
55*795d594fSAndroid Build Coastguard Worker     }
56*795d594fSAndroid Build Coastguard Worker   }
57*795d594fSAndroid Build Coastguard Worker 
doTestOtherThreadWait()58*795d594fSAndroid Build Coastguard Worker   public static void doTestOtherThreadWait() throws Exception {
59*795d594fSAndroid Build Coastguard Worker     System.out.println("################################");
60*795d594fSAndroid Build Coastguard Worker     System.out.println("### Other thread (suspended) ###");
61*795d594fSAndroid Build Coastguard Worker     System.out.println("################################");
62*795d594fSAndroid Build Coastguard Worker     final ControlData data = new ControlData();
63*795d594fSAndroid Build Coastguard Worker     data.waitFor = new Object();
64*795d594fSAndroid Build Coastguard Worker     Thread t = new Thread("Frames doTestOtherThreadWait") {
65*795d594fSAndroid Build Coastguard Worker       public void run() {
66*795d594fSAndroid Build Coastguard Worker         Recurse.foo(4, 0, 0, data);
67*795d594fSAndroid Build Coastguard Worker       }
68*795d594fSAndroid Build Coastguard Worker     };
69*795d594fSAndroid Build Coastguard Worker     t.start();
70*795d594fSAndroid Build Coastguard Worker     data.reached.await();
71*795d594fSAndroid Build Coastguard Worker     Thread.yield();
72*795d594fSAndroid Build Coastguard Worker     Thread.sleep(500);  // A little bit of time...
73*795d594fSAndroid Build Coastguard Worker 
74*795d594fSAndroid Build Coastguard Worker     int count = getFrameCount(t);
75*795d594fSAndroid Build Coastguard Worker     System.out.println(count);
76*795d594fSAndroid Build Coastguard Worker     try {
77*795d594fSAndroid Build Coastguard Worker       System.out.println(Arrays.toString(getFrameLocation(t, -1)));
78*795d594fSAndroid Build Coastguard Worker     } catch (RuntimeException e) {
79*795d594fSAndroid Build Coastguard Worker       System.out.println(e.getMessage());
80*795d594fSAndroid Build Coastguard Worker     }
81*795d594fSAndroid Build Coastguard Worker     for (int i = 0; i < count; i++) {
82*795d594fSAndroid Build Coastguard Worker       System.out.println(Arrays.toString(getFrameLocation(t, i)));
83*795d594fSAndroid Build Coastguard Worker     }
84*795d594fSAndroid Build Coastguard Worker     try {
85*795d594fSAndroid Build Coastguard Worker       System.out.println(Arrays.toString(getFrameLocation(t, count)));
86*795d594fSAndroid Build Coastguard Worker     } catch (RuntimeException e) {
87*795d594fSAndroid Build Coastguard Worker       System.out.println(e.getMessage());
88*795d594fSAndroid Build Coastguard Worker     }
89*795d594fSAndroid Build Coastguard Worker 
90*795d594fSAndroid Build Coastguard Worker     // Let the thread make progress and die.
91*795d594fSAndroid Build Coastguard Worker     synchronized(data.waitFor) {
92*795d594fSAndroid Build Coastguard Worker       data.waitFor.notifyAll();
93*795d594fSAndroid Build Coastguard Worker     }
94*795d594fSAndroid Build Coastguard Worker     t.join();
95*795d594fSAndroid Build Coastguard Worker   }
96*795d594fSAndroid Build Coastguard Worker 
doTestOtherThreadBusyLoop()97*795d594fSAndroid Build Coastguard Worker   public static void doTestOtherThreadBusyLoop() throws Exception {
98*795d594fSAndroid Build Coastguard Worker     System.out.println("###########################");
99*795d594fSAndroid Build Coastguard Worker     System.out.println("### Other thread (live) ###");
100*795d594fSAndroid Build Coastguard Worker     System.out.println("###########################");
101*795d594fSAndroid Build Coastguard Worker     final ControlData data = new ControlData();
102*795d594fSAndroid Build Coastguard Worker     Thread t = new Thread("Frames doTestOtherThreadBusyLoop") {
103*795d594fSAndroid Build Coastguard Worker       public void run() {
104*795d594fSAndroid Build Coastguard Worker         Recurse.foo(4, 0, 0, data);
105*795d594fSAndroid Build Coastguard Worker       }
106*795d594fSAndroid Build Coastguard Worker     };
107*795d594fSAndroid Build Coastguard Worker     t.start();
108*795d594fSAndroid Build Coastguard Worker     data.reached.await();
109*795d594fSAndroid Build Coastguard Worker     Thread.yield();
110*795d594fSAndroid Build Coastguard Worker     Thread.sleep(500);  // A little bit of time...
111*795d594fSAndroid Build Coastguard Worker 
112*795d594fSAndroid Build Coastguard Worker     int count = getFrameCount(t);
113*795d594fSAndroid Build Coastguard Worker     System.out.println(count);
114*795d594fSAndroid Build Coastguard Worker     try {
115*795d594fSAndroid Build Coastguard Worker       System.out.println(Arrays.toString(getFrameLocation(t, -1)));
116*795d594fSAndroid Build Coastguard Worker     } catch (RuntimeException e) {
117*795d594fSAndroid Build Coastguard Worker       System.out.println(e.getMessage());
118*795d594fSAndroid Build Coastguard Worker     }
119*795d594fSAndroid Build Coastguard Worker     for (int i = 0; i < count; i++) {
120*795d594fSAndroid Build Coastguard Worker       System.out.println(Arrays.toString(getFrameLocation(t, i)));
121*795d594fSAndroid Build Coastguard Worker     }
122*795d594fSAndroid Build Coastguard Worker     try {
123*795d594fSAndroid Build Coastguard Worker       System.out.println(Arrays.toString(getFrameLocation(t, count)));
124*795d594fSAndroid Build Coastguard Worker     } catch (RuntimeException e) {
125*795d594fSAndroid Build Coastguard Worker       System.out.println(e.getMessage());
126*795d594fSAndroid Build Coastguard Worker     }
127*795d594fSAndroid Build Coastguard Worker 
128*795d594fSAndroid Build Coastguard Worker     // Let the thread stop looping and die.
129*795d594fSAndroid Build Coastguard Worker     data.stop = true;
130*795d594fSAndroid Build Coastguard Worker     t.join();
131*795d594fSAndroid Build Coastguard Worker   }
132*795d594fSAndroid Build Coastguard Worker 
getFrameCount(Thread thread)133*795d594fSAndroid Build Coastguard Worker   public static native int getFrameCount(Thread thread);
getFrameLocation(Thread thread, int depth)134*795d594fSAndroid Build Coastguard Worker   public static native Object[] getFrameLocation(Thread thread, int depth);
135*795d594fSAndroid Build Coastguard Worker }
136