Class PrintInstructionTracer
- All Implemented Interfaces:
InstructionTracer
InstructionTracer implementation that prints a textual trace of executed bytecode
instructions.
Each time an instruction is about to execute, this tracer formats a single line containing:
- a monotonically increasing instruction counter,
- the qualified root name of the
BytecodeNodebeing interpreted, and - a human readable representation of the instruction at the current bytecode index.
Consumer<String> (for example,
System.out::println) or to a PrintStream via the
newBuilder(PrintStream) convenience factory.
A tracer may optionally be restricted to a subset of bytecode roots by installing a filter
predicate. If a BytecodeNode does not match the filter, its instructions are skipped.
This tracer is intended for debugging and profiling during development. It runs on the language execution thread and executes on the hot path before every instruction; it should not be left enabled in production code.
- Since:
- 25.1
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classBuilder forPrintInstructionTracerinstances.Nested classes/interfaces inherited from interface InstructionTracer
InstructionTracer.InstructionAccess -
Method Summary
Modifier and TypeMethodDescriptionnewBuilder(PrintStream out) Convenience overload ofnewBuilder(Consumer)that writes to aPrintStream.newBuilder(Consumer<String> out) Creates a newPrintInstructionTracer.Builderthat will emit trace lines to the given consumer.voidonInstructionEnter(InstructionTracer.InstructionAccess access, BytecodeNode bytecode, int bytecodeIndex, Frame frame) Called immediately before executing the instruction atbytecodeIndexof the givenbytecodenode.voidreset()Resets the internal instruction counter back to zero.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface InstructionTracer
getExclusiveBytecodeDescriptor
-
Method Details
-
onInstructionEnter
public void onInstructionEnter(InstructionTracer.InstructionAccess access, BytecodeNode bytecode, int bytecodeIndex, Frame frame) Called immediately before executing the instruction atbytecodeIndexof the givenbytecodenode.This implementation formats and emits a single trace line for the current instruction, unless the
BytecodeNodeis excluded by the active filter.- Specified by:
onInstructionEnterin interfaceInstructionTracer- Parameters:
access- accessor to query information about the current instruction, valid only during this callbytecode- theBytecodeNodecurrently being interpretedbytecodeIndex- the bytecode index (BCI) of the instruction to be executedframe- the current frame for the root being interpreted- Since:
- 25.1
-
reset
public void reset()Resets the internal instruction counter back to zero.This only affects the counter included in the printed trace lines. It does not clear any output sink or filter configuration.
- Since:
- 25.1
-
newBuilder
Creates a newPrintInstructionTracer.Builderthat will emit trace lines to the given consumer.Each generated line is already formatted. The consumer is typically a logger or
System.out::println, but anyConsumeris accepted.- Parameters:
out- sink for formatted trace lines- Returns:
- a new builder
- Since:
- 25.1
-
newBuilder
Convenience overload ofnewBuilder(Consumer)that writes to aPrintStream.The resulting tracer will call
PrintStream.println(String)for every traced instruction.- Parameters:
out- a print stream (for exampleSystem.out)- Returns:
- a new builder
- Since:
- 25.1
-