Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [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 | package art; |
| 18 | |
| 19 | import java.lang.reflect.Field; |
| 20 | import java.lang.reflect.Executable; |
| 21 | |
| 22 | public class StackTrace { |
| 23 | public static class StackFrameData { |
| 24 | public final Thread thr; |
| 25 | public final Executable method; |
| 26 | public final long current_location; |
| 27 | public final int depth; |
| 28 | |
| 29 | public StackFrameData(Thread thr, Executable e, long loc, int depth) { |
| 30 | this.thr = thr; |
| 31 | this.method = e; |
| 32 | this.current_location = loc; |
| 33 | this.depth = depth; |
| 34 | } |
| 35 | @Override |
| 36 | public String toString() { |
| 37 | return String.format( |
| 38 | "StackFrameData { thr: '%s', method: '%s', loc: %d, depth: %d }", |
| 39 | this.thr, |
| 40 | this.method, |
| 41 | this.current_location, |
| 42 | this.depth); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | public static native int GetStackDepth(Thread thr); |
| 47 | |
| 48 | private static native StackFrameData[] nativeGetStackTrace(Thread thr); |
| 49 | |
| 50 | public static StackFrameData[] GetStackTrace(Thread thr) { |
| 51 | // The RI seems to give inconsistent (and sometimes nonsensical) results if the thread is not |
| 52 | // suspended. The spec says that not being suspended is fine but since we want this to be |
| 53 | // consistent we will suspend for the RI. |
| 54 | boolean suspend_thread = |
| 55 | !System.getProperty("java.vm.name").equals("Dalvik") && |
| 56 | !thr.equals(Thread.currentThread()) && |
| 57 | !Suspension.isSuspended(thr); |
| 58 | if (suspend_thread) { |
| 59 | Suspension.suspend(thr); |
| 60 | } |
| 61 | StackFrameData[] out = nativeGetStackTrace(thr); |
| 62 | if (suspend_thread) { |
| 63 | Suspension.resume(thr); |
| 64 | } |
| 65 | return out; |
| 66 | } |
| 67 | } |
| 68 | |