Accessing the BCI of Throwable's StackTraceElement
Mandy Chung
mandy.chung at oracle.com
Fri Feb 18 20:36:34 UTC 2022
On 2/18/22 3:42 AM, Eirik Bjørsnøs wrote:
> Hi,
>
> Currently, StackWalker.StackFrame::getByteCodeIndex provides a way to get
> the BCI of stack frames during a stack walk.
>
> Similarly, it might be useful to get the BCI when inspecting a Throwable's
> StackTraceElements. This does however not seem possible, given that
> StackTraceElement currently does not expose the BCI. (It exposes the
> lineNumber, but that's not useful for my current use case).
>
> On another note, it seems like calling Throwable.getStackTrace() would be a
> wasteful way to just find the BCI of the top stack frame. Would be really
> nice if one could do a StackWalk with a Throwable as input, which I imagine
> should work a lot more efficiently.
>
> (My use case here is to attribute any exception to a BCI in the same method
> such that I don't need to instrument code to track the BCI myself).
>
> So I guess I have two questions:
>
> 1: Would it be reasonable to add a getByteCodeIndex method to
> StackTraceElement? Or perhaps even make StackTraceElement implement
> StackWalker.StackFrame?
It's possible to consider adding a method in StackTraceElement to return
BCI but I think having StackWalker to take a Throwable may be a better
solution.
> 2: Could the StackWalker API be suitable for walking not just the stack of
> current threads, but also the stacks of Throwables? Was this discussed
> during the development of StackWalker for Java 9?
Yes it's tracked by JDK-8189752 [1] and one idea is to provide a way to
traverse the backtrace of a Throwable.
[1] https://bugs.openjdk.java.net/browse/JDK-8189752
More information about the core-libs-dev
mailing list