jdk/src/java.base/share/classes/java/lang/StackStreamFactory.java
author rriggs
Tue, 16 Feb 2016 11:36:20 -0500
changeset 35978 752d505da547
parent 34882 ce2a8ec851c1
child 37363 329dba26ffd2
child 37526 dc4669f222ab
permissions -rw-r--r--
8148775: Spec for j.l.ProcessBuilder.Redirect.DISCARD need to be improved Reviewed-by: martin
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
34882
ce2a8ec851c1 8145544: Move sun.misc.VM to jdk.internal.misc
chegar
parents: 34373
diff changeset
    27
import jdk.internal.misc.VM;
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    28
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    29
import java.io.PrintStream;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    30
import java.lang.StackWalker.Option;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    31
import java.lang.StackWalker.StackFrame;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    32
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    33
import java.lang.annotation.Native;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    34
import java.lang.reflect.Method;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    35
import java.security.AccessController;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    36
import java.security.PrivilegedAction;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    37
import java.util.Arrays;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    38
import java.util.EnumSet;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    39
import java.util.HashSet;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    40
import java.util.NoSuchElementException;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    41
import java.util.Objects;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    42
import java.util.Optional;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    43
import java.util.Set;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    44
import java.util.Spliterator;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    45
import java.util.function.Consumer;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    46
import java.util.function.Function;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    47
import java.util.stream.Stream;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    48
import java.util.stream.StreamSupport;
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
import static java.lang.StackStreamFactory.WalkerState.*;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    51
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
 * StackStreamFactory class provides static factory methods
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    54
 * to get different kinds of stack walker/traverser.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    55
 *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    56
 * AbstractStackWalker provides the basic stack walking support
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    57
 * fetching stack frames from VM in batches.
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
 * AbstractStackWalker subclass is specialized for a specific kind of stack traversal
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    60
 * to avoid overhead of Stream/Lambda
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    61
 * 1. Support traversing Stream<StackFrame>
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    62
 * 2. StackWalker::getCallerClass
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    63
 * 3. Throwable::init and Throwable::getStackTrace
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    64
 * 4. AccessControlContext getting ProtectionDomain
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
final class StackStreamFactory {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    67
    private StackStreamFactory() {}
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    68
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    69
    // Stack walk implementation classes to be excluded during stack walking
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    70
    // lazily add subclasses when they are loaded.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    71
    private final static Set<Class<?>> stackWalkImplClasses = init();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    72
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    73
    private static final int SMALL_BATCH       = 8;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    74
    private static final int BATCH_SIZE        = 32;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    75
    private static final int LARGE_BATCH_SIZE  = 256;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    76
    private static final int MIN_BATCH_SIZE    = SMALL_BATCH;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    77
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    78
    // These flags must match the values maintained in the VM
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    79
    @Native private static final int DEFAULT_MODE              = 0x0;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    80
    @Native private static final int FILL_CLASS_REFS_ONLY      = 0x2;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    81
    @Native private static final int FILTER_FILL_IN_STACKTRACE = 0x10;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    82
    @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
    83
    @Native private static final int FILL_LIVE_STACK_FRAMES    = 0x100;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    84
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    85
    /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    86
     * For Throwable to use StackWalker, set useNewThrowable to true.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    87
     * Performance work and extensive testing is needed to replace the
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    88
     * VM built-in backtrace filled in Throwable with the StackWalker.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    89
     */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    90
    final static boolean useNewThrowable = getProperty("stackwalk.newThrowable", false);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    91
    final static boolean isDebug = getProperty("stackwalk.debug", false);
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
    static <T> StackFrameTraverser<T>
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    94
        makeStackTraverser(StackWalker walker, Function<? super Stream<StackFrame>, ? extends T> function)
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
        if (walker.hasLocalsOperandsOption())
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    97
            return new LiveStackInfoTraverser<T>(walker, function);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    98
        else
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    99
            return new StackFrameTraverser<T>(walker, function);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   100
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   101
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   102
    /**
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   103
     * Gets a stack stream to find caller class.
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
    static CallerClassFinder makeCallerFinder(StackWalker walker) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   106
        return new CallerClassFinder(walker);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   107
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   108
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   109
    static boolean useStackTrace(Throwable t) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   110
        if (t instanceof VirtualMachineError)
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   111
            return false;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   112
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   113
        return VM.isBooted() && StackStreamFactory.useNewThrowable;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   114
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   115
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   116
    /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   117
     * This should only be used by Throwable::<init>.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   118
     */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   119
    static StackTrace makeStackTrace(Throwable ex) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   120
        return StackTrace.dump(ex);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   121
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   122
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   123
    /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   124
     * This creates StackTrace for Thread::dumpThread to use.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   125
     */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   126
    static StackTrace makeStackTrace() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   127
        return StackTrace.dump();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   128
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   129
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   130
    enum WalkerState {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   131
        NEW,     // the stream is new and stack walking has not started
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   132
        OPEN,    // the stream is open when it is being traversed.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   133
        CLOSED;  // the stream is closed when the stack walking is done
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   134
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   135
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   136
    static abstract class AbstractStackWalker<T> {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   137
        protected final StackWalker walker;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   138
        protected final Thread thread;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   139
        protected final int maxDepth;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   140
        protected final long mode;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   141
        protected int depth;                 // traversed stack depth
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   142
        protected FrameBuffer frameBuffer;   // buffer for VM to fill in
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   143
        protected long anchor;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   144
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   145
        // buffers to fill in stack frame information
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   146
        protected AbstractStackWalker(StackWalker walker, int mode) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   147
            this(walker, mode, Integer.MAX_VALUE);
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
        protected AbstractStackWalker(StackWalker walker, int mode, int maxDepth) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   150
            this.thread = Thread.currentThread();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   151
            this.mode = toStackWalkMode(walker, mode);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   152
            this.walker = walker;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   153
            this.maxDepth = maxDepth;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   154
            this.depth = 0;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   155
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   156
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   157
        private int toStackWalkMode(StackWalker walker, int mode) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   158
            int newMode = mode;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   159
            if (walker.hasOption(Option.SHOW_HIDDEN_FRAMES) &&
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   160
                    !fillCallerClassOnly(newMode) /* don't show hidden frames for getCallerClass */)
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   161
                newMode |= SHOW_HIDDEN_FRAMES;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   162
            if (walker.hasLocalsOperandsOption())
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   163
                newMode |= FILL_LIVE_STACK_FRAMES;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   164
            return newMode;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   165
        }
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
        private boolean fillCallerClassOnly(int mode) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   168
            return (mode|FILL_CLASS_REFS_ONLY) != FILL_CLASS_REFS_ONLY;
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
        /**
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   171
         * A callback method to consume the stack frames.  This method is invoked
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   172
         * 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
   173
         *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   174
         * Each specialized AbstractStackWalker subclass implements the consumeFrames method
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   175
         * to control the following:
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   176
         * 1. fetch the subsequent batches of stack frames
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   177
         * 2. reuse or expand the allocated buffers
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   178
         * 3. create specialized StackFrame objects
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
         * @return the number of consumed frames
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
         protected abstract T consumeFrames();
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
        /**
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   185
         * Initialize FrameBuffer.  Subclass should implement this method to
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   186
         * create its custom frame buffers.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   187
         */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   188
         protected abstract void initFrameBuffer();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   189
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
         * Returns the suggested next 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
         * Subclass should override this method to change the batch size
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
         * @param lastBatchFrameCount number of frames in the last batch; or zero
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   196
         * @return suggested batch size
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   197
         */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   198
        protected abstract int batchSize(int lastBatchFrameCount);
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
        /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   201
         * Returns the next batch size, always >= minimum batch size (32)
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   202
         *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   203
         * Subclass may override this method if the minimum batch size is different.
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
        protected int getNextBatchSize() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   206
            int lastBatchSize = depth == 0 ? 0 : frameBuffer.curBatchFrameCount();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   207
            int nextBatchSize = batchSize(lastBatchSize);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   208
            if (isDebug) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   209
                System.err.println("last batch size = " + lastBatchSize +
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   210
                                   " next batch size = " + nextBatchSize);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   211
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   212
            return nextBatchSize >= MIN_BATCH_SIZE ? nextBatchSize : MIN_BATCH_SIZE;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   213
        }
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
        /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   216
         * Checks if this stream is in the given state. Otherwise, throws IllegalStateException.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   217
         *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   218
         * VM also validates this stream if it's anchored for stack walking
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   219
         * when stack frames are fetched for each batch.
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
        final void checkState(WalkerState state) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   222
            if (thread != Thread.currentThread()) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   223
                throw new IllegalStateException("Invalid thread walking this stack stream: " +
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   224
                        Thread.currentThread().getName() + " " + thread.getName());
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
            switch (state) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   227
                case NEW:
34373
a551d53250dd 8143967: The stream state check should handle unsigned anchor value
mchung
parents: 34362
diff changeset
   228
                    if (anchor != 0) {
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   229
                        throw new IllegalStateException("This stack stream is being reused.");
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
                    break;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   232
                case OPEN:
34373
a551d53250dd 8143967: The stream state check should handle unsigned anchor value
mchung
parents: 34362
diff changeset
   233
                    if (anchor == 0 || anchor == -1L) {
a551d53250dd 8143967: The stream state check should handle unsigned anchor value
mchung
parents: 34362
diff changeset
   234
                        throw new IllegalStateException("This stack stream is not valid for walking.");
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   235
                    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   236
                    break;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   237
                case CLOSED:
34373
a551d53250dd 8143967: The stream state check should handle unsigned anchor value
mchung
parents: 34362
diff changeset
   238
                    if (anchor != -1L) {
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   239
                        throw new IllegalStateException("This stack stream is not closed.");
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   240
                    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   241
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   242
        }
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
         * Close this stream.  This stream becomes invalid to walk.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   246
         */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   247
        private void close() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   248
            this.anchor = -1L;
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
         * Walks stack frames until {@link #consumeFrames} is done consuming
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   253
         * the frames it is interested in.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   254
         */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   255
        final T walk() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   256
            checkState(NEW);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   257
            try {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   258
                // VM will need to stablize the stack before walking.  It will invoke
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   259
                // the AbstractStackWalker::doStackWalk method once it fetches the first batch.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   260
                // the callback will be invoked within the scope of the callStackWalk frame.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   261
                return beginStackWalk();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   262
            } finally {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   263
                close();  // done traversal; close the stream
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   264
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   265
        }
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
        private boolean skipReflectionFrames() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   268
            return !walker.hasOption(Option.SHOW_REFLECT_FRAMES) &&
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   269
                       !walker.hasOption(Option.SHOW_HIDDEN_FRAMES);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   270
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   271
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
         * Returns {@code Class} object at the current frame;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   274
         * or {@code null} if no more frame. If advanceToNextBatch is true,
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   275
         * it will only fetch the next batch.
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
        final Class<?> peekFrame() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   278
            while (frameBuffer.isActive() && depth < maxDepth) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   279
                if (frameBuffer.isEmpty()) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   280
                    // fetch another batch of stack frames
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   281
                    getNextBatch();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   282
                } else {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   283
                    Class<?> c = frameBuffer.get();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   284
                    if (skipReflectionFrames() && isReflectionFrame(c)) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   285
                        if (isDebug)
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   286
                            System.err.println("  skip: frame " + frameBuffer.getIndex() + " " + c);
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
                        frameBuffer.next();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   289
                        depth++;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   290
                        continue;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   291
                    } else {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   292
                        return c;
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
                }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   295
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   296
            return null;
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
        /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   300
         * This method is only invoked by VM.
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
         * It will invoke the consumeFrames method to start the stack walking
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   303
         * with the first batch of stack frames.  Each specialized AbstractStackWalker
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   304
         * subclass implements the consumeFrames method to control the following:
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   305
         * 1. fetch the subsequent batches of stack frames
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   306
         * 2. reuse or expand the allocated buffers
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   307
         * 3. create specialized StackFrame objects
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 Object doStackWalk(long anchor, int skipFrames, int batchSize,
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   310
                                                int bufStartIndex, int bufEndIndex) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   311
            checkState(NEW);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   312
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   313
            frameBuffer.check(skipFrames);
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
            if (isDebug) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   316
                System.err.format("doStackWalk: skip %d start %d end %d%n",
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   317
                        skipFrames, bufStartIndex, bufEndIndex);
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
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   320
            this.anchor = anchor;  // set anchor for this bulk stack frame traversal
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   321
            frameBuffer.setBatch(bufStartIndex, bufEndIndex);
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
            // 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
   324
            return consumeFrames();
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
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   327
        /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   328
         * Get next batch of stack frames.
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
        private int getNextBatch() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   331
            int nextBatchSize = Math.min(maxDepth - depth, getNextBatchSize());
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   332
            if (!frameBuffer.isActive() || nextBatchSize <= 0) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   333
                if (isDebug) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   334
                    System.out.format("  more stack walk done%n");
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
                frameBuffer.freeze();   // stack walk done
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   337
                return 0;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   338
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   339
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   340
            return fetchStackFrames(nextBatchSize);
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
        /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   344
         * This method traverses the next stack frame and returns the Class
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   345
         * invoking that stack frame.
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
         * 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
   348
         * 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
   349
         * this stack stream will be invalidated once walk is done.
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
         * @see #tryNextFrame
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   352
         */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   353
        final Class<?> nextFrame() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   354
            if (!hasNext()) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   355
                return null;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   356
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   357
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   358
            Class<?> c = frameBuffer.next();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   359
            depth++;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   360
            return c;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   361
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   363
        /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   364
         * Returns true if there is next frame to be traversed.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   365
         * This skips hidden frames unless this StackWalker has
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   366
         * {@link Option#SHOW_REFLECT_FRAMES}
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   367
         */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   368
        final boolean hasNext() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   369
            return peekFrame() != null;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   370
        }
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
         * 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
   374
         * stack frame information.
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
         * VM first anchors the frame of the current thread.  A traversable stream
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   377
         * 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
   378
         * of stack frames and call AbstractStackWalker::doStackWalk to invoke the
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   379
         * stack walking function on each stack frame.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   380
         *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   381
         * If all fetched stack frames are traversed, AbstractStackWalker::fetchStackFrames will
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   382
         * fetch the next batch of stack frames to continue.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   383
         */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   384
        private T beginStackWalk() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   385
            // initialize buffers for VM to fill the stack frame info
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   386
            initFrameBuffer();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   387
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   388
            return callStackWalk(mode, 0,
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   389
                                 frameBuffer.curBatchFrameCount(),
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   390
                                 frameBuffer.startIndex(),
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   391
                                 frameBuffer.classes,
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   392
                                 frameBuffer.stackFrames);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   393
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   394
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
         * Fetches stack frames.
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
         * @params batchSize number of elements of the frame  buffers for this batch
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   399
         * @returns number of frames fetched in this batch
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   400
         */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   401
        private int fetchStackFrames(int batchSize) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   402
            int startIndex = frameBuffer.startIndex();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   403
            frameBuffer.resize(startIndex, batchSize);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   404
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   405
            int endIndex = fetchStackFrames(mode, anchor, batchSize,
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   406
                                            startIndex,
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   407
                                            frameBuffer.classes,
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   408
                                            frameBuffer.stackFrames);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   409
            if (isDebug) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   410
                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
   411
                                  batchSize, frameBuffer.startIndex(), endIndex);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   412
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   413
            int numFrames = endIndex - startIndex;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   414
            if (numFrames == 0) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   415
                frameBuffer.freeze(); // done stack walking
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   416
            } else {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   417
                frameBuffer.setBatch(startIndex, endIndex);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   418
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   419
            return numFrames;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   420
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   421
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   422
        /**
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   423
         * Begins stack walking.  This method anchors this frame and invokes
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   424
         * AbstractStackWalker::doStackWalk after fetching the firt batch of stack frames.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   425
         *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   426
         * @param mode        mode of stack walking
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   427
         * @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
   428
         * @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
   429
         * @param startIndex  start index of the frame buffers to be filled.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   430
         * @param classes     Classes buffer of the stack frames
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   431
         * @param frames      StackFrame buffer, or null
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   432
         * @return            Result of AbstractStackWalker::doStackWalk
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   433
         */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   434
        private native T callStackWalk(long mode, int skipframes,
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   435
                                       int batchSize, int startIndex,
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   436
                                       Class<?>[] classes,
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   437
                                       StackFrame[] frames);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   438
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
         * Fetch the next batch of stack frames.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   441
         *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   442
         * @param mode        mode of stack walking
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   443
         * @param anchor
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   444
         * @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
   445
         * @param startIndex  start index of the frame buffers to be filled.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   446
         * @param classes     Classes buffer of the stack frames
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   447
         * @param frames      StackFrame buffer, or null
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   448
         *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   449
         * @return the end index to the frame buffers
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   450
         */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   451
        private native int fetchStackFrames(long mode, long anchor,
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   452
                                            int batchSize, int startIndex,
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   453
                                            Class<?>[] classes,
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   454
                                            StackFrame[] frames);
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
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   457
        /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   458
         * Frame buffer
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   459
         *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   460
         * Each specialized AbstractStackWalker subclass may subclass the FrameBuffer.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   461
         */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   462
        class FrameBuffer {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   463
            static final int START_POS = 2;     // 0th and 1st elements are reserved
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   464
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   465
            // buffers for VM to fill stack frame info
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   466
            int currentBatchSize;    // current batch size
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   467
            Class<?>[] classes;      // caller class for fast path
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
            StackFrame[] stackFrames;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   470
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   471
            int origin;         // index to the current traversed stack frame
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   472
            int fence;          // index to the last frame in the current batch
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   473
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   474
            FrameBuffer(int initialBatchSize) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   475
                if (initialBatchSize < MIN_BATCH_SIZE) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   476
                    throw new IllegalArgumentException(initialBatchSize + " < minimum batch size: " + MIN_BATCH_SIZE);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   477
                }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   478
                this.origin = START_POS;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   479
                this.fence = 0;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   480
                this.currentBatchSize = initialBatchSize;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   481
                this.classes = new Class<?>[currentBatchSize];
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   482
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   483
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   484
            int curBatchFrameCount() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   485
                return currentBatchSize-START_POS;
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
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   488
            /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   489
             * Tests if this frame buffer is empty.  All frames are fetched.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   490
             */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   491
            final boolean isEmpty() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   492
                return origin >= fence || (origin == START_POS && fence == 0);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   493
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   494
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   495
            /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   496
             * Freezes this frame buffer.  The stack stream source is done fetching.
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
            final void freeze() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   499
                origin = 0;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   500
                fence = 0;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   501
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   502
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   503
            /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   504
             * Tests if this frame buffer is active.  It is inactive when
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   505
             * it is done for traversal.  All stack frames have been traversed.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   506
             */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   507
            final boolean isActive() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   508
                return origin > 0 && (fence == 0 || origin < fence || fence == currentBatchSize);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   509
            }
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
             * Gets the class at the current frame and move to the next frame.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   513
             */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   514
            final Class<?> next() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   515
                if (isEmpty()) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   516
                    throw new NoSuchElementException("origin=" + origin + " fence=" + fence);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   517
                }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   518
                Class<?> c = classes[origin++];
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   519
                if (isDebug) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   520
                    int index = origin-1;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   521
                    System.out.format("  next frame at %d: %s (origin %d fence %d)%n", index,
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   522
                                      Objects.toString(c), index, fence);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   523
                }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   524
                return c;
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
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   527
            /**
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   528
             * Gets the class at the current frame.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   529
             */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   530
            final Class<?> get() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   531
                if (isEmpty()) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   532
                    throw new NoSuchElementException("origin=" + origin + " fence=" + fence);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   533
                }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   534
                return classes[origin];
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
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   537
            /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   538
             * Returns the index of the current frame.
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
            final int getIndex() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   541
                return origin;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   542
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   543
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   544
            /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   545
             * Set the start and end index of a new batch of stack frames that have
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   546
             * been filled in this frame buffer.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   547
             */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   548
            final void setBatch(int startIndex, int endIndex) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   549
                if (startIndex <= 0 || endIndex <= 0)
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   550
                    throw new IllegalArgumentException("startIndex=" + startIndex + " endIndex=" + endIndex);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   551
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   552
                this.origin = startIndex;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   553
                this.fence = endIndex;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   554
                if (depth == 0 && fence > 0) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   555
                    // filter the frames due to the stack stream implementation
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   556
                    for (int i = START_POS; i < fence; i++) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   557
                        Class<?> c = classes[i];
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   558
                        if (isDebug) System.err.format("  frame %d: %s%n", i, c);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   559
                        if (filterStackWalkImpl(c)) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   560
                            origin++;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   561
                        } else {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   562
                            break;
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
                    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   565
                }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   566
            }
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
             * Checks if the origin is the expected start index.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   570
             */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   571
            final void check(int skipFrames) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   572
                int index = skipFrames + START_POS;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   573
                if (origin != index) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   574
                    // stack walk must continue with the previous frame depth
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   575
                    throw new IllegalStateException("origin " + origin + " != " + index);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   576
                }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   577
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   578
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   579
            // ------ subclass may override the following methods -------
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
             * Resizes the buffers for VM to fill in the next batch of stack frames.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   582
             * The next batch will start at the given startIndex with the maximum number
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   583
             * of elements.
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
             * <p> Subclass may override this method to manage the allocated buffers.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   586
             *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   587
             * @param startIndex the start index for the first frame of the next batch to fill in.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   588
             * @param elements the number of elements for the next batch to fill in.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   589
             *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   590
             */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   591
            void resize(int startIndex, int elements) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   592
                if (!isActive())
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   593
                    throw new IllegalStateException("inactive frame buffer can't be resized");
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   594
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   595
                int size = startIndex+elements;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   596
                if (classes.length < size) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   597
                    // copy the elements in classes array to the newly allocated one.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   598
                    // classes[0] is a Thread object
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   599
                    Class<?>[] prev = classes;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   600
                    classes = new Class<?>[size];
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   601
                    System.arraycopy(prev, 0, classes, 0, START_POS);
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
                currentBatchSize = size;
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
            /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   607
             * Returns the start index for this frame buffer is refilled.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   608
             *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   609
             * This implementation reuses the allocated buffer for the next batch
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   610
             * of stack frames.  For subclass to retain the fetched stack frames,
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   611
             * it should override this method to return the index at which the frame
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   612
             * should be filled in for the next batch.
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
            int startIndex() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   615
                return START_POS;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   616
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   617
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   618
            /**
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   619
             * Returns next StackFrame object in the current batch of stack frames
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   620
             */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   621
            StackFrame nextStackFrame() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   622
                throw new InternalError("should not reach here");
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   623
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   624
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   625
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   626
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   627
    /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   628
     * This StackFrameTraverser supports {@link Stream} traversal.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   629
     *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   630
     * This class implements Spliterator::forEachRemaining and Spliterator::tryAdvance.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   631
     */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   632
    static class StackFrameTraverser<T> extends AbstractStackWalker<T>
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   633
            implements Spliterator<StackFrame>
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   634
    {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   635
        static {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   636
            stackWalkImplClasses.add(StackFrameTraverser.class);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   637
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   638
        private static final int CHARACTERISTICS = Spliterator.ORDERED | Spliterator.IMMUTABLE;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   639
        class Buffer extends FrameBuffer {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   640
            Buffer(int initialBatchSize) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   641
                super(initialBatchSize);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   642
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   643
                this.stackFrames = new StackFrame[initialBatchSize];
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   644
                for (int i = START_POS; i < initialBatchSize; i++) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   645
                    stackFrames[i] = new StackFrameInfo(walker);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   646
                }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   647
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   648
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   649
            @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   650
            void resize(int startIndex, int elements) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   651
                super.resize(startIndex, elements);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   652
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   653
                int size = startIndex+elements;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   654
                if (stackFrames.length < size) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   655
                    stackFrames = new StackFrame[size];
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   656
                }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   657
                for (int i = startIndex(); i < size; i++) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   658
                    stackFrames[i] = new StackFrameInfo(walker);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   659
                }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   660
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   661
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   662
            @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   663
            StackFrame nextStackFrame() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   664
                if (isEmpty()) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   665
                    throw new NoSuchElementException("origin=" + origin + " fence=" + fence);
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
                StackFrame frame = stackFrames[origin];
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   669
                origin++;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   670
                return frame;
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
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   674
        final Function<? super Stream<StackFrame>, ? extends T> function;  // callback
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   675
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   676
        StackFrameTraverser(StackWalker walker,
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   677
                            Function<? super Stream<StackFrame>, ? extends T> function) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   678
            this(walker, function, DEFAULT_MODE);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   679
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   680
        StackFrameTraverser(StackWalker walker,
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   681
                            Function<? super Stream<StackFrame>, ? extends T> function,
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   682
                            int mode) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   683
            super(walker, mode);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   684
            this.function = function;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   685
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   686
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   687
        /**
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   688
         * Returns next StackFrame object in the current batch of stack frames;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   689
         * or null if no more stack frame.
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
        StackFrame nextStackFrame() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   692
            if (!hasNext()) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   693
                return null;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   694
            }
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
            StackFrame frame = frameBuffer.nextStackFrame();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   697
            depth++;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   698
            return frame;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   699
        }
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
        @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   702
        protected T consumeFrames() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   703
            checkState(OPEN);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   704
            Stream<StackFrame> stream = StreamSupport.stream(this, false);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   705
            if (function != null) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   706
                return function.apply(stream);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   707
            } else
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   708
                throw new UnsupportedOperationException();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   709
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   710
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   711
        @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   712
        protected void initFrameBuffer() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   713
            this.frameBuffer = new Buffer(getNextBatchSize());
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   714
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   715
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   716
        @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   717
        protected int batchSize(int lastBatchFrameCount) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   718
            if (lastBatchFrameCount == 0) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   719
                // First batch, use estimateDepth if not exceed the large batch size
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   720
                // and not too small
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   721
                int initialBatchSize = Math.max(walker.estimateDepth(), SMALL_BATCH);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   722
                return Math.min(initialBatchSize, LARGE_BATCH_SIZE);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   723
            } else {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   724
                if (lastBatchFrameCount > BATCH_SIZE) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   725
                    return lastBatchFrameCount;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   726
                } else {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   727
                    return Math.min(lastBatchFrameCount*2, BATCH_SIZE);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   728
                }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   729
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   730
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   731
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   732
        // ------- Implementation of Spliterator
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   733
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   734
        @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   735
        public Spliterator<StackFrame> trySplit() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   736
            return null;   // ordered stream and do not allow to split
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   737
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   738
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   739
        @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   740
        public long estimateSize() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   741
            return maxDepth;
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
        @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   745
        public int characteristics() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   746
            return CHARACTERISTICS;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   747
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   748
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   749
        @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   750
        public void forEachRemaining(Consumer<? super StackFrame> action) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   751
            checkState(OPEN);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   752
            for (int n = 0; n < maxDepth; n++) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   753
                StackFrame frame = nextStackFrame();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   754
                if (frame == null) break;
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
                action.accept(frame);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   757
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   758
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   759
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   760
        @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   761
        public boolean tryAdvance(Consumer<? super StackFrame> action) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   762
            checkState(OPEN);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   763
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   764
            int index = frameBuffer.getIndex();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   765
            if (hasNext()) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   766
                StackFrame frame = nextStackFrame();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   767
                action.accept(frame);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   768
                if (isDebug) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   769
                    System.err.println("tryAdvance: " + index + " " + frame);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   770
                }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   771
                return true;
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
            if (isDebug) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   774
                System.err.println("tryAdvance: " + index + " NO element");
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   775
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   776
            return false;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   777
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   778
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   779
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   780
    /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   781
     * CallerClassFinder is specialized to return Class<?> for each stack frame.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   782
     * StackFrame is not requested.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   783
     */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   784
    static class CallerClassFinder extends AbstractStackWalker<Integer> {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   785
        static {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   786
            stackWalkImplClasses.add(CallerClassFinder.class);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   787
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   788
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   789
        private Class<?> caller;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   790
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   791
        CallerClassFinder(StackWalker walker) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   792
            super(walker, FILL_CLASS_REFS_ONLY);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   793
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   794
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   795
        Class<?> findCaller() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   796
            walk();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   797
            return caller;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   798
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   799
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   800
        @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   801
        protected Integer consumeFrames() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   802
            checkState(OPEN);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   803
            int n = 0;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   804
            Class<?>[] frames = new Class<?>[2];
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   805
            // skip the API calling this getCallerClass method
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   806
            // 0: StackWalker::getCallerClass
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   807
            // 1: caller-sensitive method
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   808
            // 2: caller class
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   809
            while (n < 2 && (caller = nextFrame()) != null) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   810
                if (isMethodHandleFrame(caller)) continue;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   811
                frames[n++] = caller;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   812
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   813
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   814
            if (frames[1] == null)
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   815
                throw new IllegalStateException("no caller frame");
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   816
            return n;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   817
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   818
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   819
        @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   820
        protected void initFrameBuffer() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   821
            this.frameBuffer = new FrameBuffer(getNextBatchSize());
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   822
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   823
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   824
        @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   825
        protected int batchSize(int lastBatchFrameCount) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   826
            return MIN_BATCH_SIZE;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   827
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   828
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   829
        @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   830
        protected int getNextBatchSize() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   831
            return MIN_BATCH_SIZE;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   832
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   833
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   834
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   835
    /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   836
     * StackTrace caches all frames in the buffer.  StackTraceElements are
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   837
     * created lazily when Throwable::getStackTrace is called.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   838
     */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   839
    static class StackTrace extends AbstractStackWalker<Integer> {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   840
        static {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   841
            stackWalkImplClasses.add(StackTrace.class);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   842
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   843
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   844
        class GrowableBuffer extends FrameBuffer {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   845
            GrowableBuffer(int initialBatchSize) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   846
                super(initialBatchSize);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   847
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   848
                this.stackFrames = new StackFrame[initialBatchSize];
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   849
                for (int i = START_POS; i < initialBatchSize; i++) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   850
                    stackFrames[i] = new StackFrameInfo(walker);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   851
                }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   852
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   853
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   854
            /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   855
             * Returns the next index to fill
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   856
             */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   857
            @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   858
            int startIndex() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   859
                return origin;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   860
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   861
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   862
            /**
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   863
             * Initialize the buffers for VM to fill in the stack frame information.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   864
             * The next batch will start at the given startIndex to
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   865
             * the length of the buffer.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   866
             */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   867
            @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   868
            void resize(int startIndex, int elements) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   869
                // Expand the frame buffer.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   870
                // Do not call super.resize that will reuse the filled elements
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   871
                // in this frame buffer
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   872
                int size = startIndex+elements;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   873
                if (classes.length < size) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   874
                    // resize the frame buffer
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   875
                    classes = Arrays.copyOf(classes, size);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   876
                    stackFrames = Arrays.copyOf(stackFrames, size);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   877
                }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   878
                for (int i = startIndex; i < size; i++) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   879
                    stackFrames[i] = new StackFrameInfo(walker);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   880
                }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   881
                currentBatchSize = size;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   882
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   883
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   884
            StackTraceElement get(int index) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   885
                return new StackTraceElement(classes[index].getName(), "unknown", null, -1);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   886
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   887
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   888
            /**
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   889
             * Returns an array of StackTraceElement for all stack frames cached in
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   890
             * this StackTrace object.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   891
             * <p>
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   892
             * This method is intended for Throwable::getOurStackTrace use only.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   893
             */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   894
            StackTraceElement[] toStackTraceElements() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   895
                int startIndex = START_POS;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   896
                for (int i = startIndex; i < classes.length; i++) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   897
                    if (classes[i] != null && filterStackWalkImpl(classes[i])) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   898
                        startIndex++;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   899
                    } else {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   900
                        break;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   901
                    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   902
                }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   903
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   904
                // VM fills in the method name, filename, line number info
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   905
                StackFrameInfo.fillInStackFrames(0, stackFrames, startIndex, startIndex + depth);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   906
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   907
                StackTraceElement[] stes = new StackTraceElement[depth];
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   908
                for (int i = startIndex, j = 0; i < classes.length && j < depth; i++, j++) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   909
                    if (isDebug) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   910
                        System.err.println("StackFrame: " + i + " " + stackFrames[i]);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   911
                    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   912
                    stes[j] = stackFrames[i].toStackTraceElement();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   913
                }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   914
                return stes;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   915
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   916
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   917
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   918
        private static final int MAX_STACK_FRAMES = 1024;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   919
        private static final StackWalker STACKTRACE_WALKER =
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   920
            StackWalker.newInstanceNoCheck(EnumSet.of(Option.SHOW_REFLECT_FRAMES));
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   921
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   922
        private StackTraceElement[] stes;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   923
        static StackTrace dump() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   924
            return new StackTrace();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   925
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   926
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   927
        static StackTrace dump(Throwable ex) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   928
            return new StackTrace(ex);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   929
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   930
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   931
        private StackTrace() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   932
            this(STACKTRACE_WALKER, DEFAULT_MODE);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   933
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   934
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   935
        /*
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   936
         * Throwable::fillInStackTrace and <init> of Throwable and subclasses
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   937
         * are filtered in the VM.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   938
         */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   939
        private StackTrace(Throwable ex) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   940
            this(STACKTRACE_WALKER, FILTER_FILL_IN_STACKTRACE);  // skip Throwable::init frames
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   941
            if (isDebug) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   942
                System.err.println("dump stack for " + ex.getClass().getName());
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   943
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   944
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   945
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   946
        StackTrace(StackWalker walker, int mode) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   947
            super(walker, mode, MAX_STACK_FRAMES);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   948
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   949
            // snapshot the stack trace
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   950
            walk();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   951
        }
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
        @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   954
        protected Integer consumeFrames() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   955
            // 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
   956
            int n = 0;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   957
            while (n < maxDepth && nextFrame() != null) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   958
                n++;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   959
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   960
            return n;
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
        @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   964
        protected void initFrameBuffer() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   965
            this.frameBuffer = new GrowableBuffer(getNextBatchSize());
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   966
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   967
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   968
        // TODO: implement better heuristic
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   969
        @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   970
        protected int batchSize(int lastBatchFrameCount) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   971
            // chunk size of VM backtrace is 32
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   972
            return lastBatchFrameCount == 0 ? 32 : 32;
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
        /**
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   976
         * Returns an array of StackTraceElement for all stack frames cached in
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   977
         * this StackTrace object.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   978
         * <p>
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   979
         * This method is intended for Throwable::getOurStackTrace use only.
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
        synchronized StackTraceElement[] getStackTraceElements() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   982
            if (stes == null) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   983
                stes = ((GrowableBuffer) frameBuffer).toStackTraceElements();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   984
                // release the frameBuffer memory
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   985
                frameBuffer = null;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   986
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   987
            return stes;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   988
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   989
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
         * Prints stack trace to the given PrintStream.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   992
         *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   993
         * Further implementation could skip creating StackTraceElement objects
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   994
         * print directly to the PrintStream.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   995
         */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   996
        void printStackTrace(PrintStream s) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   997
            StackTraceElement[] stes = getStackTraceElements();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   998
            synchronized (s) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   999
                s.println("Stack trace");
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1000
                for (StackTraceElement traceElement : stes)
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1001
                    s.println("\tat " + traceElement);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1002
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1003
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1004
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1005
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1006
    static class LiveStackInfoTraverser<T> extends StackFrameTraverser<T> {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1007
        static {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1008
            stackWalkImplClasses.add(LiveStackInfoTraverser.class);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1009
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1010
        // VM will fill in all method info and live stack info directly in StackFrameInfo
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1011
        class Buffer extends FrameBuffer {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1012
            Buffer(int initialBatchSize) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1013
                super(initialBatchSize);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1014
                this.stackFrames = new StackFrame[initialBatchSize];
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1015
                for (int i = START_POS; i < initialBatchSize; i++) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1016
                    stackFrames[i] = new LiveStackFrameInfo(walker);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1017
                }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1018
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1019
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1020
            @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1021
            void resize(int startIndex, int elements) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1022
                super.resize(startIndex, elements);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1023
                int size = startIndex + elements;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1024
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1025
                if (stackFrames.length < size) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1026
                    this.stackFrames = new StackFrame[size];
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1027
                }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1028
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1029
                for (int i = startIndex(); i < size; i++) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1030
                    stackFrames[i] = new LiveStackFrameInfo(walker);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1031
                }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1032
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1033
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1034
            @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1035
            StackFrame nextStackFrame() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1036
                if (isEmpty()) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1037
                    throw new NoSuchElementException("origin=" + origin + " fence=" + fence);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1038
                }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1039
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1040
                StackFrame frame = stackFrames[origin];
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1041
                origin++;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1042
                return frame;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1043
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1044
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1045
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1046
        LiveStackInfoTraverser(StackWalker walker,
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1047
                               Function<? super Stream<StackFrame>, ? extends T> function) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1048
            super(walker, function, DEFAULT_MODE);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1049
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1050
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1051
        @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1052
        protected void initFrameBuffer() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1053
            this.frameBuffer = new Buffer(getNextBatchSize());
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1054
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1055
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1056
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1057
    private static native boolean checkStackWalkModes();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1058
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1059
    // avoid loading other subclasses as they may not be used
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1060
    private static Set<Class<?>> init() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1061
        if (!checkStackWalkModes()) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1062
            throw new InternalError("StackWalker mode values do not match with JVM");
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1063
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1064
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1065
        Set<Class<?>> classes = new HashSet<>();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1066
        classes.add(StackWalker.class);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1067
        classes.add(StackStreamFactory.class);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1068
        classes.add(AbstractStackWalker.class);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1069
        return classes;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1070
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1071
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1072
    private static boolean filterStackWalkImpl(Class<?> c) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1073
        return stackWalkImplClasses.contains(c) ||
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1074
                c.getName().startsWith("java.util.stream.");
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1075
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1076
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1077
    // MethodHandle frames are not hidden and CallerClassFinder has
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1078
    // to filter them out
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1079
    private static boolean isMethodHandleFrame(Class<?> c) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1080
        return c.getName().startsWith("java.lang.invoke.");
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1081
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1082
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1083
    private static boolean isReflectionFrame(Class<?> c) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1084
        if (c.getName().startsWith("sun.reflect") &&
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1085
                !sun.reflect.MethodAccessor.class.isAssignableFrom(c)) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1086
            throw new InternalError("Not sun.reflect.MethodAccessor: " + c.toString());
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1087
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1088
        // ## should filter all @Hidden frames?
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1089
        return c == Method.class ||
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1090
                sun.reflect.MethodAccessor.class.isAssignableFrom(c) ||
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1091
                c.getName().startsWith("java.lang.invoke.LambdaForm");
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1092
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1093
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1094
    private static boolean getProperty(String key, boolean value) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1095
        String s = AccessController.doPrivileged(new PrivilegedAction<>() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1096
            @Override
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1097
            public String run() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1098
                return System.getProperty(key);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1099
            }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1100
        });
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1101
        if (s != null) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1102
            return Boolean.valueOf(s);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1103
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1104
        return value;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1105
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
  1106
}