• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /**
20  * Provides a limited interface to the Dalvik VM stack. This class is mostly
21  * used for implementing security checks.
22  *
23  * @hide
24  */
25 public final class VMStack {
26     /**
27      * Returns the defining class loader of the caller's caller.
28      *
29      * @return the requested class loader, or {@code null} if this is the
30      *         bootstrap class loader.
31      */
getCallingClassLoader()32     native public static ClassLoader getCallingClassLoader();
33 
34     /**
35      * Returns the class of the caller's caller's caller.
36      *
37      * @return the requested class, or {@code null}.
38      */
getStackClass2()39     native public static Class<?> getStackClass2();
40 
41     /**
42      * Creates an array of classes from the methods at the top of the stack.
43      * We continue until we reach the bottom of the stack or exceed the
44      * specified maximum depth.
45      * <p>
46      * The topmost stack frame (this method) and the one above that (the
47      * caller) are excluded from the array.  Frames with java.lang.reflect
48      * classes are skipped over.
49      * <p>
50      * The classes in the array are the defining classes of the methods.
51      * <p>
52      * This is similar to Harmony's VMStack.getClasses, except that this
53      * implementation doesn't have a concept of "privileged" frames.
54      *
55      * @param maxDepth
56      *      maximum number of classes to return, or -1 for all
57      * @return an array with classes for the most-recent methods on the stack
58      */
getClasses(int maxDepth)59     native public static Class<?>[] getClasses(int maxDepth);
60 
61     /**
62      * Returns the first ClassLoader on the call stack that isn't either of
63      * the passed-in ClassLoaders.
64      */
getClosestUserClassLoader(ClassLoader bootstrap, ClassLoader system)65     public static ClassLoader getClosestUserClassLoader(ClassLoader bootstrap,
66                                                         ClassLoader system) {
67         Class<?>[] stackClasses = VMStack.getClasses(-1);
68         for (Class<?> stackClass : stackClasses) {
69             ClassLoader loader = stackClass.getClassLoader();
70             if (loader != null && loader != bootstrap && loader != system) {
71                 return loader;
72             }
73         }
74         return null;
75     }
76 
77     /**
78      * Retrieves the stack trace from the specified thread.
79      *
80      * @param t
81      *      thread of interest
82      * @return an array of stack trace elements, or null if the thread
83      *      doesn't have a stack trace (e.g. because it exited)
84      */
getThreadStackTrace(Thread t)85     native public static StackTraceElement[] getThreadStackTrace(Thread t);
86 
87     /**
88      * Retrieves a partial stack trace from the specified thread into
89      * the provided array.
90      *
91      * @param t
92      *      thread of interest
93      * @param stackTraceElements
94      *      preallocated array for use when only the top of stack is
95      *      desired. Unused elements will be filled with null values.
96      * @return the number of elements filled
97      */
fillStackTraceElements(Thread t, StackTraceElement[] stackTraceElements)98     native public static int fillStackTraceElements(Thread t,
99         StackTraceElement[] stackTraceElements);
100 }
101