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