VMStack.java revision 3d93b50c1e9d8cb12d17a4331edd27ee2a1dc7a9
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
17package 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 */
25public 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     */
32    native public static ClassLoader getCallingClassLoader();
33
34    /**
35     * Returns the defining class loader of the caller's caller's caller.
36     *
37     * @return the requested class loader, or {@code null} if this is the
38     *         bootstrap class loader.
39     */
40    native public static ClassLoader getCallingClassLoader2();
41
42    /**
43     * Returns the class of the caller's caller's caller.
44     *
45     * @return the requested class, or {@code null}.
46     */
47    native public static Class<?> getStackClass2();
48
49    /**
50     * Creates an array of classes from the methods at the top of the stack.
51     * We continue until we reach the bottom of the stack or exceed the
52     * specified maximum depth.
53     * <p>
54     * The topmost stack frame (this method) and the one above that (the
55     * caller) are excluded from the array.  Frames with java.lang.reflect
56     * classes are skipped over.
57     * <p>
58     * The classes in the array are the defining classes of the methods.
59     * <p>
60     * This is similar to Harmony's VMStack.getClasses, except that this
61     * implementation doesn't have a concept of "privileged" frames.
62     *
63     * @param maxDepth
64     *      maximum number of classes to return, or -1 for all
65     * @return an array with classes for the most-recent methods on the stack
66     */
67    native public static Class<?>[] getClasses(int maxDepth);
68
69    /**
70     * Retrieves the stack trace from the specified thread.
71     *
72     * @param t
73     *      thread of interest
74     * @return an array of stack trace elements, or null if the thread
75     *      doesn't have a stack trace (e.g. because it exited)
76     */
77    native public static StackTraceElement[] getThreadStackTrace(Thread t);
78}
79