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