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