/*
 * Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * The Universal Permissive License (UPL), Version 1.0
 *
 * Subject to the condition set forth below, permission is hereby granted to any
 * person obtaining a copy of this software, associated documentation and/or
 * data (collectively the "Software"), free of charge and under any and all
 * copyright rights in the Software, and any and all patent rights owned or
 * freely licensable by each licensor hereunder covering either (i) the
 * unmodified Software as contributed to or provided by such licensor, or (ii)
 * the Larger Works (as defined below), to deal in both
 *
 * (a) the Software, and
 *
 * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
 * one is included with the Software each a "Larger Work" to which the Software
 * is contributed by such licensors),
 *
 * without restriction, including without limitation the rights to copy, create
 * derivative works of, display, perform, and distribute the Software and make,
 * use, sell, offer for sale, import, export, have made, and have sold the
 * Software and the Larger Work(s), and to sublicense the foregoing rights on
 * either these or other terms.
 *
 * This license is subject to the following condition:
 *
 * The above copyright notice and either this complete permission notice or at a
 * minimum a reference to the UPL must be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.oracle.truffle.api.debug;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.RootCallTarget;
import com.oracle.truffle.api.TruffleStackTraceElement;
import com.oracle.truffle.api.frame.Frame;
import com.oracle.truffle.api.frame.MaterializedFrame;
import com.oracle.truffle.api.interop.NodeLibrary;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.nodes.LanguageInfo;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.nodes.RootNode;
import com.oracle.truffle.api.source.SourceSection;

Represents a trace element in the guest language stack trace. A guest language trace element consists of a name, the current source location and scopes containing local variables and arguments.

The difference between this class and DebugStackFrame is the limited life-time of DebugStackFrame, that is associated with a SuspendedEvent.

See Also:
Since:19.0
/** * Represents a trace element in the guest language stack trace. A guest language trace element * consists of a {@link #getName() name}, the current {@link #getSourceSection() source location} * and {@link #getScope() scopes} containing local variables and arguments. * <p> * The difference between this class and {@link DebugStackFrame} is the limited life-time of * {@link DebugStackFrame}, that is associated with a {@link SuspendedEvent}. * * @see DebugException#getDebugStackTrace() * @since 19.0 */
public final class DebugStackTraceElement { private final DebuggerSession session; final TruffleStackTraceElement traceElement; private final StackTraceElement hostTraceElement; private StackTraceElement stackTraceElement; DebugStackTraceElement(DebuggerSession session, TruffleStackTraceElement traceElement) { this.session = session; this.traceElement = traceElement; this.hostTraceElement = null; } DebugStackTraceElement(DebuggerSession session, StackTraceElement hostTraceElement) { this.session = session; this.traceElement = null; this.hostTraceElement = hostTraceElement; }
Returns whether this trace element is a language implementation artifact.

The decision to mark a method as internal is language-specific, reflects judgments about tool usability, and is subject to change.

Since:19.0
/** * Returns whether this trace element is a language implementation artifact. * <p> * The decision to mark a method as <em>internal</em> is language-specific, reflects judgments * about tool usability, and is subject to change. * * @since 19.0 */
public boolean isInternal() { if (isHost()) { return false; } RootNode root = findCurrentRoot(); if (root == null) { return true; } return root.isInternal(); }
Returns true if this element is a host element. Host elements provide stack trace element, have no scope, and no source section.

Host elements are provided only when host info is set to true.

See Also:
Since:20.3
/** * Returns <code>true</code> if this element is a host element. Host elements provide * {@link #getHostTraceElement() stack trace element}, have no {@link #getScope() scope}, and no * {@link #getSourceSection() source section}. * <p> * Host elements are provided only when {@link DebuggerSession#setShowHostStackFrames(boolean) * host info} is set to <code>true</code>. * * @since 20.3 * @see DebuggerSession#setShowHostStackFrames(boolean) */
public boolean isHost() { return hostTraceElement != null; }
Provides a host element. Returns the host stack trace element if and only if this is host element.
See Also:
Returns:the host stack trace element, or null when not a host element.
Since:20.3
/** * Provides a host element. Returns the host stack trace element if and only if this is * {@link #isHost() host} element. * * @return the host stack trace element, or <code>null</code> when not a host element. * @since 20.3 * @see #isHost() */
public StackTraceElement getHostTraceElement() { return hostTraceElement; }
A description of the trace element. If the language does not provide such a description then null is returned.
Since:19.0
/** * A description of the trace element. If the language does not provide such a description then * <code>null</code> is returned. * * @since 19.0 */
public String getName() { if (hostTraceElement != null) { return hostTraceElement.getClassName() + '.' + hostTraceElement.getMethodName(); } RootNode root = findCurrentRoot(); if (root == null) { return null; } try { return root.getName(); } catch (ThreadDeath td) { throw td; } catch (Throwable ex) { throw new DebugException(session, ex, root.getLanguageInfo(), null, true, null); } } private String getName0() { RootNode root = findCurrentRoot(); if (root == null) { return null; } try { return root.getName(); } catch (ThreadDeath td) { throw td; } catch (Throwable ex) { return null; } }
Returns the source section location of this trace element. The source section is null if the source location is not available.
Since:19.0
/** * Returns the source section location of this trace element. The source section is * <code>null</code> if the source location is not available. * * @since 19.0 */
public SourceSection getSourceSection() { if (isHost()) { return null; } Node node = traceElement.getLocation(); if (node != null) { return session.resolveSection(node); } return null; }
Get the current inner-most scope. The scope might not provide valid information if the execution path diverges from this trace element.
Returns:the scope, or null when no language is associated with this frame location, or when no local scope exists.
Since:19.0
/** * Get the current inner-most scope. The scope might not provide valid information if the * execution path diverges from this trace element. * * @return the scope, or <code>null</code> when no language is associated with this frame * location, or when no local scope exists. * @since 19.0 */
public DebugScope getScope() { if (isHost()) { return null; } Node node = traceElement.getLocation(); if (node == null) { return null; } RootNode root = node.getRootNode(); if (root.getLanguageInfo() == null) { // no language, no scopes return null; } Frame elementFrame = traceElement.getFrame(); MaterializedFrame frame = (elementFrame != null) ? elementFrame.materialize() : null; if (!NodeLibrary.getUncached().hasScope(node, frame)) { return null; } try { Object scope = NodeLibrary.getUncached().getScope(node, frame, true); return new DebugScope(scope, session, null, node, frame, root); } catch (UnsupportedMessageException e) { throw CompilerDirectives.shouldNotReachHere(e); } } private LanguageInfo getLanguage() { if (isHost()) { return null; } LanguageInfo language; RootNode root = findCurrentRoot(); if (root != null) { language = root.getLanguageInfo(); } else { language = null; } return language; } private RootNode findCurrentRoot() { if (isHost()) { return null; } Node node = traceElement.getLocation(); if (node != null) { return node.getRootNode(); } RootCallTarget target = traceElement.getTarget(); return target.getRootNode(); } StackTraceElement toTraceElement() { if (stackTraceElement == null) { if (hostTraceElement != null) { stackTraceElement = hostTraceElement; } else { LanguageInfo language = getLanguage(); String declaringClass = language != null ? "<" + language.getId() + ">" : "<unknown>"; String methodName = getName0(); if (methodName == null) { methodName = ""; } SourceSection sourceLocation = getSourceSection(); String fileName = sourceLocation != null ? sourceLocation.getSource().getName() : "Unknown"; int startLine = sourceLocation != null ? sourceLocation.getStartLine() : -1; stackTraceElement = new StackTraceElement(declaringClass, methodName, fileName, startLine); } } return stackTraceElement; } }