jdk/src/java.base/share/classes/java/lang/StackStreamFactory.java
author redestad
Tue, 03 May 2016 15:50:54 +0200
changeset 37781 71ed5645f17c
parent 37593 824750ada3d6
child 37819 8a2559d6fe5b
permissions -rw-r--r--
8155775: Re-examine naming of privileged methods to access System properties Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     1
/*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     4
 *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    10
 *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    15
 * accompanied this code).
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    16
 *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    20
 *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    23
 * questions.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    24
 */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    25
package java.lang;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    26
37363
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 34882
diff changeset
    27
import jdk.internal.reflect.MethodAccessor;
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    28
import java.lang.StackWalker.Option;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    29
import java.lang.StackWalker.StackFrame;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    30
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    31
import java.lang.annotation.Native;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    32
import java.lang.reflect.Method;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    33
import java.util.HashSet;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    34
import java.util.NoSuchElementException;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    35
import java.util.Objects;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    36
import java.util.Set;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    37
import java.util.Spliterator;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    38
import java.util.function.Consumer;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    39
import java.util.function.Function;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    40
import java.util.stream.Stream;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    41
import java.util.stream.StreamSupport;
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 37534
diff changeset
    42
import sun.security.action.GetPropertyAction;
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    43
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    44
import static java.lang.StackStreamFactory.WalkerState.*;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    45
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    46
/**
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    47
 * StackStreamFactory class provides static factory methods
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    48
 * to get different kinds of stack walker/traverser.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    49
 *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    50
 * AbstractStackWalker provides the basic stack walking support
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    51
 * fetching stack frames from VM in batches.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    52
 *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    53
 * AbstractStackWalker subclass is specialized for a specific kind of stack traversal
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    54
 * to avoid overhead of Stream/Lambda
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    55
 * 1. Support traversing Stream<StackFrame>
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    56
 * 2. StackWalker::getCallerClass
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
    57
 * 3. AccessControlContext getting ProtectionDomain
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    58
 */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    59
final class StackStreamFactory {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    60
    private StackStreamFactory() {}
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    61
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    62
    // Stack walk implementation classes to be excluded during stack walking
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    63
    // lazily add subclasses when they are loaded.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    64
    private final static Set<Class<?>> stackWalkImplClasses = init();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    65
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    66
    private static final int SMALL_BATCH       = 8;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    67
    private static final int BATCH_SIZE        = 32;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    68
    private static final int LARGE_BATCH_SIZE  = 256;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    69
    private static final int MIN_BATCH_SIZE    = SMALL_BATCH;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    70
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    71
    // These flags must match the values maintained in the VM
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    72
    @Native private static final int DEFAULT_MODE              = 0x0;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    73
    @Native private static final int FILL_CLASS_REFS_ONLY      = 0x2;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    74
    @Native private static final int SHOW_HIDDEN_FRAMES        = 0x20;  // LambdaForms are hidden by the VM
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    75
    @Native private static final int FILL_LIVE_STACK_FRAMES    = 0x100;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    76
    /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    77
     * For Throwable to use StackWalker, set useNewThrowable to true.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    78
     * Performance work and extensive testing is needed to replace the
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    79
     * VM built-in backtrace filled in Throwable with the StackWalker.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    80
     */
37781
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
    81
    final static boolean isDebug =
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
    82
            "true".equals(GetPropertyAction.privilegedGetProperty("stackwalk.debug"));
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    83
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    84
    static <T> StackFrameTraverser<T>
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    85
        makeStackTraverser(StackWalker walker, Function<? super Stream<StackFrame>, ? extends T> function)
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    86
    {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    87
        if (walker.hasLocalsOperandsOption())
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
    88
            return new LiveStackInfoTraverser<>(walker, function);
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    89
        else
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
    90
            return new StackFrameTraverser<>(walker, function);
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    91
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    92
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    93
    /**
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    94
     * Gets a stack stream to find caller class.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    95
     */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    96
    static CallerClassFinder makeCallerFinder(StackWalker walker) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    97
        return new CallerClassFinder(walker);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    98
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    99
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   100
    enum WalkerState {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   101
        NEW,     // the stream is new and stack walking has not started
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   102
        OPEN,    // the stream is open when it is being traversed.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   103
        CLOSED;  // the stream is closed when the stack walking is done
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   104
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   105
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   106
    /**
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   107
     * Subclass of AbstractStackWalker implements a specific stack walking logic.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   108
     * It needs to set up the frame buffer and stack walking mode.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   109
     *
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   110
     * It initiates the VM stack walking via the callStackWalk method that serves
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   111
     * as the anchored frame and VM will call up to AbstractStackWalker::doStackWalk.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   112
     *
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   113
     * @param <R> the type of the result returned from stack walking
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   114
     * @param <T> the type of the data gathered for each frame.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   115
     *            For example, StackFrameInfo for StackWalker::walk or
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   116
     *            Class<?> for StackWalker::getCallerClass
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   117
     */
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   118
    static abstract class AbstractStackWalker<R, T> {
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   119
        protected final StackWalker walker;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   120
        protected final Thread thread;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   121
        protected final int maxDepth;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   122
        protected final long mode;
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   123
        protected int depth;    // traversed stack depth
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   124
        protected FrameBuffer<? extends T> frameBuffer;
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   125
        protected long anchor;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   126
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   127
        // buffers to fill in stack frame information
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   128
        protected AbstractStackWalker(StackWalker walker, int mode) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   129
            this(walker, mode, Integer.MAX_VALUE);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   130
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   131
        protected AbstractStackWalker(StackWalker walker, int mode, int maxDepth) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   132
            this.thread = Thread.currentThread();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   133
            this.mode = toStackWalkMode(walker, mode);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   134
            this.walker = walker;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   135
            this.maxDepth = maxDepth;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   136
            this.depth = 0;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   137
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   138
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   139
        private int toStackWalkMode(StackWalker walker, int mode) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   140
            int newMode = mode;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   141
            if (walker.hasOption(Option.SHOW_HIDDEN_FRAMES) &&
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   142
                    (mode & FILL_CLASS_REFS_ONLY) != FILL_CLASS_REFS_ONLY)
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   143
                newMode |= SHOW_HIDDEN_FRAMES;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   144
            if (walker.hasLocalsOperandsOption())
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   145
                newMode |= FILL_LIVE_STACK_FRAMES;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   146
            return newMode;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   147
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   148
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   149
        /**
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   150
         * A callback method to consume the stack frames.  This method is invoked
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   151
         * once stack walking begins (i.e. it is only invoked when walkFrames is called).
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   152
         *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   153
         * Each specialized AbstractStackWalker subclass implements the consumeFrames method
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   154
         * to control the following:
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   155
         * 1. fetch the subsequent batches of stack frames
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   156
         * 2. reuse or expand the allocated buffers
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   157
         * 3. create specialized StackFrame objects
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   158
         *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   159
         * @return the number of consumed frames
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   160
         */
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   161
         protected abstract R consumeFrames();
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   162
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   163
        /**
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   164
         * Initialize FrameBuffer.  Subclass should implement this method to
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   165
         * create its custom frame buffers.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   166
         */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   167
         protected abstract void initFrameBuffer();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   168
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   169
        /**
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   170
         * Returns the suggested next batch size.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   171
         *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   172
         * Subclass should override this method to change the batch size
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   173
         *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   174
         * @param lastBatchFrameCount number of frames in the last batch; or zero
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   175
         * @return suggested batch size
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   176
         */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   177
        protected abstract int batchSize(int lastBatchFrameCount);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   178
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   179
        /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   180
         * Returns the next batch size, always >= minimum batch size (32)
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   181
         *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   182
         * Subclass may override this method if the minimum batch size is different.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   183
         */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   184
        protected int getNextBatchSize() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   185
            int lastBatchSize = depth == 0 ? 0 : frameBuffer.curBatchFrameCount();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   186
            int nextBatchSize = batchSize(lastBatchSize);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   187
            if (isDebug) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   188
                System.err.println("last batch size = " + lastBatchSize +
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   189
                                   " next batch size = " + nextBatchSize);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   190
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   191
            return nextBatchSize >= MIN_BATCH_SIZE ? nextBatchSize : MIN_BATCH_SIZE;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   192
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   193
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   194
        /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   195
         * Checks if this stream is in the given state. Otherwise, throws IllegalStateException.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   196
         *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   197
         * VM also validates this stream if it's anchored for stack walking
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   198
         * when stack frames are fetched for each batch.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   199
         */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   200
        final void checkState(WalkerState state) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   201
            if (thread != Thread.currentThread()) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   202
                throw new IllegalStateException("Invalid thread walking this stack stream: " +
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   203
                        Thread.currentThread().getName() + " " + thread.getName());
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   204
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   205
            switch (state) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   206
                case NEW:
34373
a551d53250dd 8143967: The stream state check should handle unsigned anchor value
mchung
parents: 34362
diff changeset
   207
                    if (anchor != 0) {
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   208
                        throw new IllegalStateException("This stack stream is being reused.");
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   209
                    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   210
                    break;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   211
                case OPEN:
34373
a551d53250dd 8143967: The stream state check should handle unsigned anchor value
mchung
parents: 34362
diff changeset
   212
                    if (anchor == 0 || anchor == -1L) {
a551d53250dd 8143967: The stream state check should handle unsigned anchor value
mchung
parents: 34362
diff changeset
   213
                        throw new IllegalStateException("This stack stream is not valid for walking.");
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   214
                    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   215
                    break;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   216
                case CLOSED:
34373
a551d53250dd 8143967: The stream state check should handle unsigned anchor value
mchung
parents: 34362
diff changeset
   217
                    if (anchor != -1L) {
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   218
                        throw new IllegalStateException("This stack stream is not closed.");
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   219
                    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   220
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   221
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   222
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   223
        /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   224
         * Close this stream.  This stream becomes invalid to walk.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   225
         */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   226
        private void close() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   227
            this.anchor = -1L;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   228
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   229
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   230
        /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   231
         * Walks stack frames until {@link #consumeFrames} is done consuming
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   232
         * the frames it is interested in.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   233
         */
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   234
        final R walk() {
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   235
            checkState(NEW);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   236
            try {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   237
                // VM will need to stablize the stack before walking.  It will invoke
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   238
                // the AbstractStackWalker::doStackWalk method once it fetches the first batch.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   239
                // the callback will be invoked within the scope of the callStackWalk frame.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   240
                return beginStackWalk();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   241
            } finally {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   242
                close();  // done traversal; close the stream
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   243
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   244
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   245
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   246
        private boolean skipReflectionFrames() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   247
            return !walker.hasOption(Option.SHOW_REFLECT_FRAMES) &&
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   248
                       !walker.hasOption(Option.SHOW_HIDDEN_FRAMES);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   249
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   250
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   251
        /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   252
         * Returns {@code Class} object at the current frame;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   253
         * or {@code null} if no more frame. If advanceToNextBatch is true,
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   254
         * it will only fetch the next batch.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   255
         */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   256
        final Class<?> peekFrame() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   257
            while (frameBuffer.isActive() && depth < maxDepth) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   258
                if (frameBuffer.isEmpty()) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   259
                    // fetch another batch of stack frames
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   260
                    getNextBatch();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   261
                } else {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   262
                    Class<?> c = frameBuffer.get();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   263
                    if (skipReflectionFrames() && isReflectionFrame(c)) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   264
                        if (isDebug)
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   265
                            System.err.println("  skip: frame " + frameBuffer.getIndex() + " " + c);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   266
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   267
                        frameBuffer.next();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   268
                        depth++;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   269
                        continue;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   270
                    } else {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   271
                        return c;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   272
                    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   273
                }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   274
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   275
            return null;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   276
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   277
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   278
        /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   279
         * This method is only invoked by VM.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   280
         *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   281
         * It will invoke the consumeFrames method to start the stack walking
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   282
         * with the first batch of stack frames.  Each specialized AbstractStackWalker
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   283
         * subclass implements the consumeFrames method to control the following:
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   284
         * 1. fetch the subsequent batches of stack frames
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   285
         * 2. reuse or expand the allocated buffers
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   286
         * 3. create specialized StackFrame objects
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   287
         */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   288
        private Object doStackWalk(long anchor, int skipFrames, int batchSize,
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   289
                                                int bufStartIndex, int bufEndIndex) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   290
            checkState(NEW);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   291
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   292
            frameBuffer.check(skipFrames);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   293
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   294
            if (isDebug) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   295
                System.err.format("doStackWalk: skip %d start %d end %d%n",
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   296
                        skipFrames, bufStartIndex, bufEndIndex);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   297
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   298
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   299
            this.anchor = anchor;  // set anchor for this bulk stack frame traversal
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   300
            frameBuffer.setBatch(depth, bufStartIndex, bufEndIndex);
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   301
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   302
            // traverse all frames and perform the action on the stack frames, if specified
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   303
            return consumeFrames();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   304
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   305
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   306
        /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   307
         * Get next batch of stack frames.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   308
         */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   309
        private int getNextBatch() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   310
            int nextBatchSize = Math.min(maxDepth - depth, getNextBatchSize());
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   311
            if (!frameBuffer.isActive() || nextBatchSize <= 0) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   312
                if (isDebug) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   313
                    System.out.format("  more stack walk done%n");
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   314
                }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   315
                frameBuffer.freeze();   // stack walk done
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   316
                return 0;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   317
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   318
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   319
            return fetchStackFrames(nextBatchSize);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   320
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   321
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   322
        /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   323
         * This method traverses the next stack frame and returns the Class
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   324
         * invoking that stack frame.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   325
         *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   326
         * This method can only be called during the walk method.  This is intended
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   327
         * to be used to walk the stack frames in one single invocation and
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   328
         * this stack stream will be invalidated once walk is done.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   329
         *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   330
         * @see #tryNextFrame
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   331
         */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   332
        final Class<?> nextFrame() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   333
            if (!hasNext()) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   334
                return null;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   335
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   336
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   337
            Class<?> c = frameBuffer.next();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   338
            depth++;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   339
            return c;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   340
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   341
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   342
        /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   343
         * Returns true if there is next frame to be traversed.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   344
         * This skips hidden frames unless this StackWalker has
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   345
         * {@link Option#SHOW_REFLECT_FRAMES}
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   346
         */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   347
        final boolean hasNext() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   348
            return peekFrame() != null;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   349
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   350
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   351
        /**
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   352
         * Begin stack walking - pass the allocated arrays to the VM to fill in
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   353
         * stack frame information.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   354
         *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   355
         * VM first anchors the frame of the current thread.  A traversable stream
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   356
         * on this thread's stack will be opened.  The VM will fetch the first batch
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   357
         * of stack frames and call AbstractStackWalker::doStackWalk to invoke the
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   358
         * stack walking function on each stack frame.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   359
         *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   360
         * If all fetched stack frames are traversed, AbstractStackWalker::fetchStackFrames will
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   361
         * fetch the next batch of stack frames to continue.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   362
         */
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   363
        private R beginStackWalk() {
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   364
            // initialize buffers for VM to fill the stack frame info
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   365
            initFrameBuffer();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   366
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   367
            return callStackWalk(mode, 0,
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   368
                                 frameBuffer.curBatchFrameCount(),
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   369
                                 frameBuffer.startIndex(),
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   370
                                 frameBuffer.frames());
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   371
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   372
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   373
        /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   374
         * Fetches stack frames.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   375
         *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   376
         * @params batchSize number of elements of the frame  buffers for this batch
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   377
         * @returns number of frames fetched in this batch
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   378
         */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   379
        private int fetchStackFrames(int batchSize) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   380
            int startIndex = frameBuffer.startIndex();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   381
            frameBuffer.resize(startIndex, batchSize);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   382
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   383
            int endIndex = fetchStackFrames(mode, anchor, batchSize,
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   384
                                            startIndex,
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   385
                                            frameBuffer.frames());
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   386
            if (isDebug) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   387
                System.out.format("  more stack walk requesting %d got %d to %d frames%n",
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   388
                                  batchSize, frameBuffer.startIndex(), endIndex);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   389
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   390
            int numFrames = endIndex - startIndex;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   391
            if (numFrames == 0) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   392
                frameBuffer.freeze(); // done stack walking
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   393
            } else {
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   394
                frameBuffer.setBatch(depth, startIndex, endIndex);
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   395
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   396
            return numFrames;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   397
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   398
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   399
        /**
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   400
         * Begins stack walking.  This method anchors this frame and invokes
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   401
         * AbstractStackWalker::doStackWalk after fetching the first batch of stack frames.
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   402
         *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   403
         * @param mode        mode of stack walking
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   404
         * @param skipframes  number of frames to be skipped before filling the frame buffer.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   405
         * @param batchSize   the batch size, max. number of elements to be filled in the frame buffers.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   406
         * @param startIndex  start index of the frame buffers to be filled.
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   407
         * @param frames      Either a Class<?> array, if mode is {@link #FILL_CLASS_REFS_ONLY}
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   408
         *                    or a {@link StackFrameInfo} (or derivative) array otherwise.
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   409
         * @return            Result of AbstractStackWalker::doStackWalk
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   410
         */
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   411
        private native R callStackWalk(long mode, int skipframes,
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   412
                                       int batchSize, int startIndex,
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   413
                                       T[] frames);
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   414
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   415
        /**
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   416
         * Fetch the next batch of stack frames.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   417
         *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   418
         * @param mode        mode of stack walking
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   419
         * @param anchor
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   420
         * @param batchSize   the batch size, max. number of elements to be filled in the frame buffers.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   421
         * @param startIndex  start index of the frame buffers to be filled.
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   422
         * @param frames      Either a Class<?> array, if mode is {@link #FILL_CLASS_REFS_ONLY}
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   423
         *                    or a {@link StackFrameInfo} (or derivative) array otherwise.
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   424
         *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   425
         * @return the end index to the frame buffers
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   426
         */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   427
        private native int fetchStackFrames(long mode, long anchor,
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   428
                                            int batchSize, int startIndex,
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   429
                                            T[] frames);
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   430
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   431
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   432
    /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   433
     * This StackFrameTraverser supports {@link Stream} traversal.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   434
     *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   435
     * This class implements Spliterator::forEachRemaining and Spliterator::tryAdvance.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   436
     */
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   437
    static class StackFrameTraverser<T> extends AbstractStackWalker<T, StackFrameInfo>
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   438
            implements Spliterator<StackFrame>
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   439
    {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   440
        static {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   441
            stackWalkImplClasses.add(StackFrameTraverser.class);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   442
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   443
        private static final int CHARACTERISTICS = Spliterator.ORDERED | Spliterator.IMMUTABLE;
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   444
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   445
        final class StackFrameBuffer extends FrameBuffer<StackFrameInfo> {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   446
            private StackFrameInfo[] stackFrames;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   447
            StackFrameBuffer(int initialBatchSize) {
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   448
                super(initialBatchSize);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   449
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   450
                this.stackFrames = new StackFrameInfo[initialBatchSize];
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   451
                for (int i = START_POS; i < initialBatchSize; i++) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   452
                    stackFrames[i] = new StackFrameInfo(walker);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   453
                }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   454
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   455
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   456
            @Override
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   457
            StackFrameInfo[] frames() {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   458
                return stackFrames;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   459
            }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   460
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   461
            @Override
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   462
            void resize(int startIndex, int elements) {
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   463
                if (!isActive())
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   464
                    throw new IllegalStateException("inactive frame buffer can't be resized");
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   465
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   466
                assert startIndex == START_POS :
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   467
                       "bad start index " + startIndex + " expected " + START_POS;
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   468
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   469
                int size = startIndex+elements;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   470
                if (stackFrames.length < size) {
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   471
                    StackFrameInfo[] newFrames = new StackFrameInfo[size];
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   472
                    // copy initial magic...
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   473
                    System.arraycopy(stackFrames, 0, newFrames, 0, startIndex);
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   474
                    stackFrames = newFrames;
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   475
                }
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   476
                for (int i = startIndex; i < size; i++) {
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   477
                    stackFrames[i] = new StackFrameInfo(walker);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   478
                }
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   479
                currentBatchSize = size;
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   480
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   481
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   482
            @Override
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   483
            StackFrameInfo nextStackFrame() {
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   484
                if (isEmpty()) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   485
                    throw new NoSuchElementException("origin=" + origin + " fence=" + fence);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   486
                }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   487
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   488
                StackFrameInfo frame = stackFrames[origin];
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   489
                origin++;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   490
                return frame;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   491
            }
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   492
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   493
            @Override
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   494
            final Class<?> at(int index) {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   495
                return stackFrames[index].declaringClass;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   496
            }
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   497
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   498
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   499
        final Function<? super Stream<StackFrame>, ? extends T> function;  // callback
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   500
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   501
        StackFrameTraverser(StackWalker walker,
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   502
                            Function<? super Stream<StackFrame>, ? extends T> function) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   503
            this(walker, function, DEFAULT_MODE);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   504
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   505
        StackFrameTraverser(StackWalker walker,
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   506
                            Function<? super Stream<StackFrame>, ? extends T> function,
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   507
                            int mode) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   508
            super(walker, mode);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   509
            this.function = function;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   510
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   511
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   512
        /**
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   513
         * Returns next StackFrame object in the current batch of stack frames;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   514
         * or null if no more stack frame.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   515
         */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   516
        StackFrame nextStackFrame() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   517
            if (!hasNext()) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   518
                return null;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   519
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   520
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   521
            StackFrameInfo frame = frameBuffer.nextStackFrame();
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   522
            depth++;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   523
            return frame;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   524
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   525
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   526
        @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   527
        protected T consumeFrames() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   528
            checkState(OPEN);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   529
            Stream<StackFrame> stream = StreamSupport.stream(this, false);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   530
            if (function != null) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   531
                return function.apply(stream);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   532
            } else
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   533
                throw new UnsupportedOperationException();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   534
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   535
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   536
        @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   537
        protected void initFrameBuffer() {
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   538
            this.frameBuffer = new StackFrameBuffer(getNextBatchSize());
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   539
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   540
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   541
        @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   542
        protected int batchSize(int lastBatchFrameCount) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   543
            if (lastBatchFrameCount == 0) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   544
                // First batch, use estimateDepth if not exceed the large batch size
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   545
                // and not too small
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   546
                int initialBatchSize = Math.max(walker.estimateDepth(), SMALL_BATCH);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   547
                return Math.min(initialBatchSize, LARGE_BATCH_SIZE);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   548
            } else {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   549
                if (lastBatchFrameCount > BATCH_SIZE) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   550
                    return lastBatchFrameCount;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   551
                } else {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   552
                    return Math.min(lastBatchFrameCount*2, BATCH_SIZE);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   553
                }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   554
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   555
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   556
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   557
        // ------- Implementation of Spliterator
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   558
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   559
        @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   560
        public Spliterator<StackFrame> trySplit() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   561
            return null;   // ordered stream and do not allow to split
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   562
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   563
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   564
        @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   565
        public long estimateSize() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   566
            return maxDepth;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   567
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   568
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   569
        @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   570
        public int characteristics() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   571
            return CHARACTERISTICS;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   572
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   573
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   574
        @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   575
        public void forEachRemaining(Consumer<? super StackFrame> action) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   576
            checkState(OPEN);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   577
            for (int n = 0; n < maxDepth; n++) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   578
                StackFrame frame = nextStackFrame();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   579
                if (frame == null) break;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   580
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   581
                action.accept(frame);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   582
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   583
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   584
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   585
        @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   586
        public boolean tryAdvance(Consumer<? super StackFrame> action) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   587
            checkState(OPEN);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   588
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   589
            int index = frameBuffer.getIndex();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   590
            if (hasNext()) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   591
                StackFrame frame = nextStackFrame();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   592
                action.accept(frame);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   593
                if (isDebug) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   594
                    System.err.println("tryAdvance: " + index + " " + frame);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   595
                }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   596
                return true;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   597
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   598
            if (isDebug) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   599
                System.err.println("tryAdvance: " + index + " NO element");
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   600
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   601
            return false;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   602
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   603
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   604
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   605
    /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   606
     * CallerClassFinder is specialized to return Class<?> for each stack frame.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   607
     * StackFrame is not requested.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   608
     */
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   609
    static final class CallerClassFinder extends AbstractStackWalker<Integer, Class<?>> {
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   610
        static {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   611
            stackWalkImplClasses.add(CallerClassFinder.class);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   612
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   613
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   614
        private Class<?> caller;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   615
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   616
        CallerClassFinder(StackWalker walker) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   617
            super(walker, FILL_CLASS_REFS_ONLY);
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   618
            assert (mode & FILL_CLASS_REFS_ONLY) == FILL_CLASS_REFS_ONLY
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   619
                   : "mode should contain FILL_CLASS_REFS_ONLY";
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   620
        }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   621
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   622
        final class ClassBuffer extends FrameBuffer<Class<?>> {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   623
            Class<?>[] classes;      // caller class for fast path
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   624
            ClassBuffer(int batchSize) {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   625
                super(batchSize);
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   626
                classes = new Class<?>[batchSize];
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   627
            }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   628
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   629
            @Override
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   630
            Class<?>[] frames() { return classes;}
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   631
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   632
            @Override
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   633
            final Class<?> at(int index) { return classes[index];}
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   634
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   635
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   636
            // ------ subclass may override the following methods -------
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   637
            /**
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   638
             * Resizes the buffers for VM to fill in the next batch of stack frames.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   639
             * The next batch will start at the given startIndex with the maximum number
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   640
             * of elements.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   641
             *
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   642
             * <p> Subclass may override this method to manage the allocated buffers.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   643
             *
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   644
             * @param startIndex the start index for the first frame of the next batch to fill in.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   645
             * @param elements the number of elements for the next batch to fill in.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   646
             *
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   647
             */
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   648
            @Override
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   649
            void resize(int startIndex, int elements) {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   650
                if (!isActive())
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   651
                    throw new IllegalStateException("inactive frame buffer can't be resized");
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   652
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   653
                assert startIndex == START_POS :
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   654
                       "bad start index " + startIndex + " expected " + START_POS;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   655
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   656
                int size = startIndex+elements;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   657
                if (classes.length < size) {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   658
                    // copy the elements in classes array to the newly allocated one.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   659
                    // classes[0] is a Thread object
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   660
                    Class<?>[] prev = classes;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   661
                    classes = new Class<?>[size];
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   662
                    System.arraycopy(prev, 0, classes, 0, startIndex);
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   663
                }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   664
                currentBatchSize = size;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   665
            }
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   666
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   667
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   668
        Class<?> findCaller() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   669
            walk();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   670
            return caller;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   671
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   672
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   673
        @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   674
        protected Integer consumeFrames() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   675
            checkState(OPEN);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   676
            int n = 0;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   677
            Class<?>[] frames = new Class<?>[2];
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   678
            // skip the API calling this getCallerClass method
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   679
            // 0: StackWalker::getCallerClass
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   680
            // 1: caller-sensitive method
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   681
            // 2: caller class
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   682
            while (n < 2 && (caller = nextFrame()) != null) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   683
                if (isMethodHandleFrame(caller)) continue;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   684
                frames[n++] = caller;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   685
            }
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   686
            if (frames[1] == null) {
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   687
                throw new IllegalStateException("no caller frame");
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   688
            }
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   689
            return n;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   690
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   691
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   692
        @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   693
        protected void initFrameBuffer() {
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   694
            this.frameBuffer = new ClassBuffer(getNextBatchSize());
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   695
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   696
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   697
        @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   698
        protected int batchSize(int lastBatchFrameCount) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   699
            return MIN_BATCH_SIZE;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   700
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   701
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   702
        @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   703
        protected int getNextBatchSize() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   704
            return MIN_BATCH_SIZE;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   705
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   706
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   707
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   708
    static final class LiveStackInfoTraverser<T> extends StackFrameTraverser<T> {
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   709
        static {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   710
            stackWalkImplClasses.add(LiveStackInfoTraverser.class);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   711
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   712
        // VM will fill in all method info and live stack info directly in StackFrameInfo
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   713
        final class LiveStackFrameBuffer extends FrameBuffer<LiveStackFrameInfo> {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   714
            private LiveStackFrameInfo[] stackFrames;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   715
            LiveStackFrameBuffer(int initialBatchSize) {
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   716
                super(initialBatchSize);
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   717
                this.stackFrames = new LiveStackFrameInfo[initialBatchSize];
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   718
                for (int i = START_POS; i < initialBatchSize; i++) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   719
                    stackFrames[i] = new LiveStackFrameInfo(walker);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   720
                }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   721
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   722
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   723
            @Override
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   724
            LiveStackFrameInfo[] frames() {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   725
                return stackFrames;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   726
            }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   727
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   728
            @Override
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   729
            void resize(int startIndex, int elements) {
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   730
                if (!isActive()) {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   731
                    throw new IllegalStateException("inactive frame buffer can't be resized");
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   732
                }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   733
                assert startIndex == START_POS :
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   734
                       "bad start index " + startIndex + " expected " + START_POS;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   735
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   736
                int size = startIndex + elements;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   737
                if (stackFrames.length < size) {
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   738
                    LiveStackFrameInfo[] newFrames = new LiveStackFrameInfo[size];
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   739
                    // copy initial magic...
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   740
                    System.arraycopy(stackFrames, 0, newFrames, 0, startIndex);
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   741
                    stackFrames = newFrames;
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   742
                }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   743
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   744
                for (int i = startIndex(); i < size; i++) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   745
                    stackFrames[i] = new LiveStackFrameInfo(walker);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   746
                }
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   747
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   748
                currentBatchSize = size;
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   749
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   750
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   751
            @Override
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   752
            LiveStackFrameInfo nextStackFrame() {
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   753
                if (isEmpty()) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   754
                    throw new NoSuchElementException("origin=" + origin + " fence=" + fence);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   755
                }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   756
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   757
                LiveStackFrameInfo frame = stackFrames[origin];
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   758
                origin++;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   759
                return frame;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   760
            }
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   761
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   762
            @Override
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   763
            final Class<?> at(int index) {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   764
                return stackFrames[index].declaringClass;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   765
            }
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   766
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   767
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   768
        LiveStackInfoTraverser(StackWalker walker,
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   769
                               Function<? super Stream<StackFrame>, ? extends T> function) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   770
            super(walker, function, DEFAULT_MODE);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   771
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   772
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   773
        @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   774
        protected void initFrameBuffer() {
37526
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   775
            this.frameBuffer = new LiveStackFrameBuffer(getNextBatchSize());
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   776
        }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   777
    }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   778
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   779
    /*
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   780
     * Frame buffer
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   781
     *
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   782
     * Each specialized AbstractStackWalker subclass may subclass the FrameBuffer.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   783
     */
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   784
    static abstract class FrameBuffer<F> {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   785
        static final int START_POS = 2;     // 0th and 1st elements are reserved
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   786
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   787
        // buffers for VM to fill stack frame info
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   788
        int currentBatchSize;    // current batch size
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   789
        int origin;         // index to the current traversed stack frame
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   790
        int fence;          // index to the last frame in the current batch
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   791
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   792
        FrameBuffer(int initialBatchSize) {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   793
            if (initialBatchSize < MIN_BATCH_SIZE) {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   794
                throw new IllegalArgumentException(initialBatchSize +
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   795
                        " < minimum batch size: " + MIN_BATCH_SIZE);
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   796
            }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   797
            this.origin = START_POS;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   798
            this.fence = 0;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   799
            this.currentBatchSize = initialBatchSize;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   800
        }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   801
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   802
        /**
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   803
         * Returns an array of frames that may be used to store frame objects
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   804
         * when walking the stack.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   805
         *
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   806
         * May be an array of {@code Class<?>} if the {@code AbstractStackWalker}
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   807
         * mode is {@link #FILL_CLASS_REFS_ONLY}, or an array of
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   808
         * {@link StackFrameInfo} (or derivative) array otherwise.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   809
         *
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   810
         * @return An array of frames that may be used to store frame objects
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   811
         * when walking the stack. Must not be null.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   812
         */
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   813
        abstract F[] frames(); // must not return null
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   814
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   815
        /**
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   816
         * Resizes the buffers for VM to fill in the next batch of stack frames.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   817
         * The next batch will start at the given startIndex with the maximum number
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   818
         * of elements.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   819
         *
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   820
         * <p> Subclass may override this method to manage the allocated buffers.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   821
         *
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   822
         * @param startIndex the start index for the first frame of the next batch to fill in.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   823
         * @param elements the number of elements for the next batch to fill in.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   824
         *
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   825
         */
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   826
        abstract void resize(int startIndex, int elements);
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   827
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   828
        /**
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   829
         * Return the class at the given position in the current batch.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   830
         * @param index the position of the frame.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   831
         * @return the class at the given position in the current batch.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   832
         */
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   833
        abstract Class<?> at(int index);
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   834
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   835
        // ------ subclass may override the following methods -------
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   836
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   837
        /*
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   838
         * Returns the start index for this frame buffer is refilled.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   839
         *
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   840
         * This implementation reuses the allocated buffer for the next batch
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   841
         * of stack frames.  For subclass to retain the fetched stack frames,
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   842
         * it should override this method to return the index at which the frame
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   843
         * should be filled in for the next batch.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   844
         */
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   845
        int startIndex() {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   846
            return START_POS;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   847
        }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   848
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   849
        /**
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   850
         * Returns next StackFrame object in the current batch of stack frames
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   851
         */
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   852
        F nextStackFrame() {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   853
            throw new InternalError("should not reach here");
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   854
        }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   855
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   856
        // ------ FrameBuffer implementation ------
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   857
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   858
        final int curBatchFrameCount() {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   859
            return currentBatchSize-START_POS;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   860
        }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   861
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   862
        /*
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   863
         * Tests if this frame buffer is empty.  All frames are fetched.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   864
         */
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   865
        final boolean isEmpty() {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   866
            return origin >= fence || (origin == START_POS && fence == 0);
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   867
        }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   868
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   869
        /*
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   870
         * Freezes this frame buffer.  The stack stream source is done fetching.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   871
         */
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   872
        final void freeze() {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   873
            origin = 0;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   874
            fence = 0;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   875
        }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   876
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   877
        /*
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   878
         * Tests if this frame buffer is active.  It is inactive when
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   879
         * it is done for traversal.  All stack frames have been traversed.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   880
         */
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   881
        final boolean isActive() {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   882
            return origin > 0 && (fence == 0 || origin < fence || fence == currentBatchSize);
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   883
        }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   884
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   885
        /**
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   886
         * Gets the class at the current frame and move to the next frame.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   887
         */
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   888
        final Class<?> next() {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   889
            if (isEmpty()) {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   890
                throw new NoSuchElementException("origin=" + origin + " fence=" + fence);
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   891
            }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   892
            Class<?> c = at(origin);
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   893
            origin++;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   894
            if (isDebug) {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   895
                int index = origin-1;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   896
                System.out.format("  next frame at %d: %s (origin %d fence %d)%n", index,
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   897
                        Objects.toString(c), index, fence);
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   898
            }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   899
            return c;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   900
        }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   901
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   902
        /**
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   903
         * Gets the class at the current frame.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   904
         */
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   905
        final Class<?> get() {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   906
            if (isEmpty()) {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   907
                throw new NoSuchElementException("origin=" + origin + " fence=" + fence);
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   908
            }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   909
            return at(origin);
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   910
        }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   911
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   912
        /*
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   913
         * Returns the index of the current frame.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   914
         */
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   915
        final int getIndex() {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   916
            return origin;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   917
        }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   918
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   919
        /*
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   920
         * Set the start and end index of a new batch of stack frames that have
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   921
         * been filled in this frame buffer.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   922
         */
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   923
        final void setBatch(int depth, int startIndex, int endIndex) {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   924
            if (startIndex <= 0 || endIndex <= 0)
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   925
                throw new IllegalArgumentException("startIndex=" + startIndex + " endIndex=" + endIndex);
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   926
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   927
            this.origin = startIndex;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   928
            this.fence = endIndex;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   929
            if (depth == 0 && fence > 0) {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   930
                // filter the frames due to the stack stream implementation
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   931
                for (int i = START_POS; i < fence; i++) {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   932
                    Class<?> c = at(i);
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   933
                    if (isDebug) System.err.format("  frame %d: %s%n", i, c);
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   934
                    if (filterStackWalkImpl(c)) {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   935
                        origin++;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   936
                    } else {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   937
                        break;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   938
                    }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   939
                }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   940
            }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   941
        }
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   942
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   943
        /*
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   944
         * Checks if the origin is the expected start index.
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   945
         */
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   946
        final void check(int skipFrames) {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   947
            int index = skipFrames + START_POS;
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   948
            if (origin != index) {
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   949
                // stack walk must continue with the previous frame depth
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   950
                throw new IllegalStateException("origin " + origin + " != " + index);
dc4669f222ab 8153123: Streamline StackWalker code
bchristi
parents: 34882
diff changeset
   951
            }
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   952
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   953
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   954
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   955
    private static native boolean checkStackWalkModes();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   956
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   957
    // avoid loading other subclasses as they may not be used
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   958
    private static Set<Class<?>> init() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   959
        if (!checkStackWalkModes()) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   960
            throw new InternalError("StackWalker mode values do not match with JVM");
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   961
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   962
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   963
        Set<Class<?>> classes = new HashSet<>();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   964
        classes.add(StackWalker.class);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   965
        classes.add(StackStreamFactory.class);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   966
        classes.add(AbstractStackWalker.class);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   967
        return classes;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   968
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   969
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   970
    private static boolean filterStackWalkImpl(Class<?> c) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   971
        return stackWalkImplClasses.contains(c) ||
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   972
                c.getName().startsWith("java.util.stream.");
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   973
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   974
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   975
    // MethodHandle frames are not hidden and CallerClassFinder has
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   976
    // to filter them out
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   977
    private static boolean isMethodHandleFrame(Class<?> c) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   978
        return c.getName().startsWith("java.lang.invoke.");
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   979
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   980
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   981
    private static boolean isReflectionFrame(Class<?> c) {
37363
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 34882
diff changeset
   982
        if (c.getName().startsWith("jdk.internal.reflect") &&
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 34882
diff changeset
   983
                !MethodAccessor.class.isAssignableFrom(c)) {
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 34882
diff changeset
   984
            throw new InternalError("Not jdk.internal.reflect.MethodAccessor: " + c.toString());
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   985
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   986
        // ## should filter all @Hidden frames?
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   987
        return c == Method.class ||
37363
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 34882
diff changeset
   988
                MethodAccessor.class.isAssignableFrom(c) ||
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   989
                c.getName().startsWith("java.lang.invoke.LambdaForm");
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   990
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   991
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   992
}