1 /* 2 * Copyright (C) 2007 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 dalvik.system; 18 19 import static android.annotation.SystemApi.Client.MODULE_LIBRARIES; 20 21 import android.annotation.SystemApi; 22 import android.compat.annotation.UnsupportedAppUsage; 23 24 import dalvik.annotation.optimization.FastNative; 25 26 import libcore.util.Nullable; 27 28 /** 29 * Provides a limited interface to the Dalvik VM stack. This class is mostly 30 * used for implementing security checks. 31 * 32 * @hide 33 */ 34 @libcore.api.CorePlatformApi(status = libcore.api.CorePlatformApi.Status.STABLE) 35 @SystemApi(client = MODULE_LIBRARIES) 36 public final class VMStack { 37 VMStack()38 private VMStack() { 39 } 40 41 /** 42 * Returns the defining class loader of the caller's caller. 43 * 44 * @return the requested class loader, or {@code null} if this is the 45 * bootstrap class loader. 46 * @deprecated Use {@code ClassLoader.getClassLoader(sun.reflect.Reflection.getCallerClass())}. 47 * Note that that can return {@link BootClassLoader} on Android where the RI 48 * would have returned null. 49 * 50 * @hide 51 */ 52 @UnsupportedAppUsage 53 @FastNative 54 @Deprecated getCallingClassLoader()55 native public static ClassLoader getCallingClassLoader(); 56 57 /** 58 * Returns the class of the caller's caller. 59 * 60 * @return the requested class, or {@code null}. 61 * @deprecated Use {@link sun.reflect.Reflection#getCallerClass()}. 62 * 63 * @hide 64 */ 65 @Deprecated getStackClass1()66 public static Class<?> getStackClass1() { 67 return getStackClass2(); 68 } 69 70 /** 71 * Returns the class of the caller's caller's caller. 72 * 73 * @return the requested class, or {@code null}. 74 * 75 * @hide 76 */ 77 @UnsupportedAppUsage 78 @FastNative getStackClass2()79 native public static Class<?> getStackClass2(); 80 81 /** 82 * Returns the first ClassLoader on the call stack that isn't the 83 * bootstrap class loader. 84 * 85 * @hide 86 */ 87 @FastNative getClosestUserClassLoader()88 public native static ClassLoader getClosestUserClassLoader(); 89 90 /** 91 * Retrieves the stack trace from the specified thread. 92 * 93 * @param t 94 * thread of interest 95 * @return an array of stack trace elements, or null if the thread 96 * doesn't have a stack trace (e.g. because it exited) 97 * 98 * @hide 99 */ 100 @UnsupportedAppUsage 101 @FastNative getThreadStackTrace(Thread t)102 native public static StackTraceElement[] getThreadStackTrace(Thread t); 103 104 /** 105 * Retrieves an annotated stack trace from the specified thread. 106 * 107 * @param t 108 * thread of interest 109 * @return an array of annotated stack frames, or null if the thread 110 * doesn't have a stack trace (e.g. because it exited) 111 * 112 * @hide 113 */ 114 @libcore.api.CorePlatformApi(status = libcore.api.CorePlatformApi.Status.STABLE) 115 @SystemApi(client = MODULE_LIBRARIES) 116 @FastNative 117 native public static @Nullable AnnotatedStackTraceElement[] getAnnotatedThreadStackTrace(Thread t)118 getAnnotatedThreadStackTrace(Thread t); 119 120 /** 121 * Retrieves a partial stack trace from the specified thread into 122 * the provided array. 123 * 124 * @param t 125 * thread of interest 126 * @param stackTraceElements 127 * preallocated array for use when only the top of stack is 128 * desired. Unused elements will be filled with null values. 129 * @return the number of elements filled 130 * 131 * @hide 132 */ 133 @UnsupportedAppUsage 134 @FastNative fillStackTraceElements(Thread t, StackTraceElement[] stackTraceElements)135 native public static int fillStackTraceElements(Thread t, 136 StackTraceElement[] stackTraceElements); 137 } 138