1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package art; 18 19 import java.util.Arrays; 20 21 public class Frames { doTest()22 public static void doTest() throws Exception { 23 doTestSameThread(); 24 25 System.out.println(); 26 27 doTestOtherThreadWait(); 28 29 System.out.println(); 30 31 doTestOtherThreadBusyLoop(); 32 } 33 doTestSameThread()34 public static void doTestSameThread() { 35 System.out.println("###################"); 36 System.out.println("### Same thread ###"); 37 System.out.println("###################"); 38 39 Thread t = Thread.currentThread(); 40 41 int count = getFrameCount(t); 42 System.out.println(count); 43 try { 44 System.out.println(Arrays.toString(getFrameLocation(t, -1))); 45 } catch (RuntimeException e) { 46 System.out.println(e.getMessage()); 47 } 48 for (int i = 0; i < count; i++) { 49 System.out.println(Arrays.toString(getFrameLocation(t, i))); 50 } 51 try { 52 System.out.println(Arrays.toString(getFrameLocation(t, count))); 53 } catch (RuntimeException e) { 54 System.out.println(e.getMessage()); 55 } 56 } 57 doTestOtherThreadWait()58 public static void doTestOtherThreadWait() throws Exception { 59 System.out.println("################################"); 60 System.out.println("### Other thread (suspended) ###"); 61 System.out.println("################################"); 62 final ControlData data = new ControlData(); 63 data.waitFor = new Object(); 64 Thread t = new Thread("Frames doTestOtherThreadWait") { 65 public void run() { 66 Recurse.foo(4, 0, 0, data); 67 } 68 }; 69 t.start(); 70 data.reached.await(); 71 Thread.yield(); 72 Thread.sleep(500); // A little bit of time... 73 74 int count = getFrameCount(t); 75 System.out.println(count); 76 try { 77 System.out.println(Arrays.toString(getFrameLocation(t, -1))); 78 } catch (RuntimeException e) { 79 System.out.println(e.getMessage()); 80 } 81 for (int i = 0; i < count; i++) { 82 System.out.println(Arrays.toString(getFrameLocation(t, i))); 83 } 84 try { 85 System.out.println(Arrays.toString(getFrameLocation(t, count))); 86 } catch (RuntimeException e) { 87 System.out.println(e.getMessage()); 88 } 89 90 // Let the thread make progress and die. 91 synchronized(data.waitFor) { 92 data.waitFor.notifyAll(); 93 } 94 t.join(); 95 } 96 doTestOtherThreadBusyLoop()97 public static void doTestOtherThreadBusyLoop() throws Exception { 98 System.out.println("###########################"); 99 System.out.println("### Other thread (live) ###"); 100 System.out.println("###########################"); 101 final ControlData data = new ControlData(); 102 Thread t = new Thread("Frames doTestOtherThreadBusyLoop") { 103 public void run() { 104 Recurse.foo(4, 0, 0, data); 105 } 106 }; 107 t.start(); 108 data.reached.await(); 109 Thread.yield(); 110 Thread.sleep(500); // A little bit of time... 111 112 int count = getFrameCount(t); 113 System.out.println(count); 114 try { 115 System.out.println(Arrays.toString(getFrameLocation(t, -1))); 116 } catch (RuntimeException e) { 117 System.out.println(e.getMessage()); 118 } 119 for (int i = 0; i < count; i++) { 120 System.out.println(Arrays.toString(getFrameLocation(t, i))); 121 } 122 try { 123 System.out.println(Arrays.toString(getFrameLocation(t, count))); 124 } catch (RuntimeException e) { 125 System.out.println(e.getMessage()); 126 } 127 128 // Let the thread stop looping and die. 129 data.stop = true; 130 t.join(); 131 } 132 getFrameCount(Thread thread)133 public static native int getFrameCount(Thread thread); getFrameLocation(Thread thread, int depth)134 public static native Object[] getFrameLocation(Thread thread, int depth); 135 } 136