Class TraceSession

java.lang.Object
cs1302.tracer.execution.TraceSession
All Implemented Interfaces:
AutoCloseable

public final class TraceSession extends Object implements AutoCloseable
Owns a single tracing operation and its cancellation/retention budgets. The thread-local binding bridges existing extraction helpers; it is never inherited.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
    Controlled non-success exit from extraction or event handling.
  • Constructor Summary

    Constructors
    Constructor
    Description
    TraceSession(TraceLimits limits, InspectionPolicy inspection, boolean accumulate)
    Binds a session to the calling thread until closed.
    TraceSession(TraceLimits limits, InspectionPolicy inspection, boolean accumulate, boolean evalEnumHash)
    Binds a session to the calling thread until closed with enum hash configuration.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Reports whether all captured states are retained.
    void
    allocate(long bytes)
    Accounts estimated extraction storage before allocation.
    void
    Attaches a launched guest, handling cancellation during launch.
    void
    attach(Process guest, boolean destroyOnClose)
    Attaches a launched guest process with control over destruction on normal close.
    void
    Begins an atomic snapshot extraction.
    void
    Requests cancellation from any thread.
    void
    Checks cancellation and the monotonic tracing deadline.
    void
    checkThreadCounts(long threads, long frames)
    Enforces live thread and total frame ceilings before reading stack frames.
    void
     
    void
    Commits a completed snapshot, retaining only the latest when requested.
    Returns the calling thread's session, or null for legacy API calls.
    static void
    elements(long count)
    Accounts element storage before fetching or allocating it.
    void
    Enables chronological thread capture without invoking guest methods.
    boolean
    encounter(long id)
    Accounts a reachable reference before queueing it.
    void
    enforce(long used, long limit, String failure)
    Applies a resource ceiling, permitting values exactly at the boundary.
    void
    Refreshes final output on the last complete snapshot after successful execution.
    void
    Refreshes final output on the last complete snapshot using explicit slices.
    void
    guestException(String description)
    Records an observed uncaught exception without losing later output.
    boolean
    Returns whether this session has been stopped or cancelled.
    void
    Materializes all completed snapshot output slices into self-contained buffers.
    void
    materializeSnapshots(byte[] sharedStdout, byte[] sharedStderr)
    Materializes all completed snapshot output slices using shared output buffers.
    static boolean
    Returns whether helpers may invoke guest methods.
    long
    Returns the per-stream output cap.
    void
    phase(String value)
    Starts the independent watchdog before guest launch.
    void
    Registers an output drainer for session cleanup and counters.
    result(String format, Object payload, Throwable failure)
    Builds a result after tracing or a recoverable failure.
    void
    setCapturedOutput(String stdout, String stderr, long stdoutBytes, long stderrBytes)
    Sets isolated guest process output explicitly.
    static boolean
    Returns whether helpers should evaluate lazy enum hash codes.
    Returns a copy of committed snapshots for serialization.
    void
    stop(String value)
    Stops this job; the first observed reason wins.
    Returns the machine-readable stop reason if stopped, or null.
    Returns thread capture state, or null for legacy capture.
    boolean
    Returns whether a guest was launched, even if it produced no snapshots.
    withEvalEnumHash(boolean evalEnumHash)
    Temporarily sets enum hash evaluation setting when running outside a session.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • TraceSession

      public TraceSession(TraceLimits limits, InspectionPolicy inspection, boolean accumulate)
      Binds a session to the calling thread until closed.
      Parameters:
      limits - Effective budgets.
      inspection - Guest inspection policy.
      accumulate - Whether every snapshot is retained.
    • TraceSession

      public TraceSession(TraceLimits limits, InspectionPolicy inspection, boolean accumulate, boolean evalEnumHash)
      Binds a session to the calling thread until closed with enum hash configuration.
      Parameters:
      limits - Effective budgets.
      inspection - Guest inspection policy.
      accumulate - Whether every snapshot is retained.
      evalEnumHash - Whether lazy enum hash codes should be evaluated.
  • Method Details

    • current

      public static TraceSession current()
      Returns the calling thread's session, or null for legacy API calls.
      Returns:
      Current session.
    • enableMultithread

      public void enableMultithread()
      Enables chronological thread capture without invoking guest methods. Use DebugTraceHelper.traceChronological with this accumulating session.
    • threadCapture

      public ThreadCapture threadCapture()
      Returns thread capture state, or null for legacy capture.
      Returns:
      Per-job thread capture.
    • checkThreadCounts

      public void checkThreadCounts(long threads, long frames)
      Enforces live thread and total frame ceilings before reading stack frames.
      Parameters:
      threads - Live application threads.
      frames - Total frames across these threads.
    • withEvalEnumHash

      public static AutoCloseable withEvalEnumHash(boolean evalEnumHash)
      Temporarily sets enum hash evaluation setting when running outside a session.
      Parameters:
      evalEnumHash - Whether lazy enum hash codes should be evaluated.
      Returns:
      AutoCloseable scope restoring previous setting.
    • mayInvoke

      public static boolean mayInvoke()
      Returns whether helpers may invoke guest methods.
      Returns:
      True for trusted inspection.
    • shouldEvalEnumHash

      public static boolean shouldEvalEnumHash()
      Returns whether helpers should evaluate lazy enum hash codes.
      Returns:
      True if enabled and methods may be invoked.
    • check

      public void check()
      Checks cancellation and the monotonic tracing deadline.
    • phase

      public void phase(String value)
      Starts the independent watchdog before guest launch.
      Parameters:
      value - Current execution phase.
    • attach

      public void attach(VirtualMachine vm)
      Attaches a launched guest, handling cancellation during launch.
      Parameters:
      vm - Guest debugger connection.
    • attach

      public void attach(Process guest, boolean destroyOnClose)
      Attaches a launched guest process with control over destruction on normal close.
      Parameters:
      guest - Launched process.
      destroyOnClose - Whether process should be forcibly destroyed on normal close.
    • register

      public void register(StreamDrainer drainer)
      Registers an output drainer for session cleanup and counters.
      Parameters:
      drainer - Guest stream drainer.
    • outputLimit

      public long outputLimit()
      Returns the per-stream output cap.
      Returns:
      Byte limit, or zero for unlimited.
    • cancel

      public void cancel()
      Requests cancellation from any thread.
    • isStopped

      public boolean isStopped()
      Returns whether this session has been stopped or cancelled.
      Returns:
      True if stopped.
    • stopReason

      public String stopReason()
      Returns the machine-readable stop reason if stopped, or null.
      Returns:
      Machine-readable stop reason.
    • stop

      public void stop(String value)
      Stops this job; the first observed reason wins.
      Parameters:
      value - Machine-readable stop reason.
    • beginSnapshot

      public void beginSnapshot()
      Begins an atomic snapshot extraction.
    • elements

      public static void elements(long count)
      Accounts element storage before fetching or allocating it.
      Parameters:
      count - Elements to inspect.
    • allocate

      public void allocate(long bytes)
      Accounts estimated extraction storage before allocation.
      Parameters:
      bytes - Conservative storage units.
    • encounter

      public boolean encounter(long id)
      Accounts a reachable reference before queueing it.
      Parameters:
      id - Guest object identity.
      Returns:
      True if this is the first encounter in this snapshot.
    • enforce

      public void enforce(long used, long limit, String failure)
      Applies a resource ceiling, permitting values exactly at the boundary.
      Parameters:
      used - Accounted usage.
      limit - Zero or finite ceiling.
      failure - Stop reason.
    • commit

      public void commit(ExecutionSnapshot snapshot)
      Commits a completed snapshot, retaining only the latest when requested.
      Parameters:
      snapshot - Fully extracted state.
    • guestException

      public void guestException(String description)
      Records an observed uncaught exception without losing later output.
      Parameters:
      description - Exception type and source location.
    • setCapturedOutput

      public void setCapturedOutput(String stdout, String stderr, long stdoutBytes, long stderrBytes)
      Sets isolated guest process output explicitly.
      Parameters:
      stdout - Captured standard output string.
      stderr - Captured standard error string.
      stdoutBytes - Total standard output bytes.
      stderrBytes - Total standard error bytes.
    • traceAvailable

      public boolean traceAvailable()
      Returns whether a guest was launched, even if it produced no snapshots.
      Returns:
      Whether trace capture was available.
    • accumulates

      public boolean accumulates()
      Reports whether all captured states are retained.
      Returns:
      True for chronological or accumulating jobs.
    • snapshots

      public List<ExecutionSnapshot> snapshots()
      Returns a copy of committed snapshots for serialization.
      Returns:
      Completed snapshots only.
    • result

      public TraceResult result(String format, Object payload, Throwable failure)
      Builds a result after tracing or a recoverable failure.
      Parameters:
      format - Payload format name.
      payload - Trace or null when unavailable.
      failure - Optional caught failure.
      Returns:
      Versioned result.
    • finishOutput

      public void finishOutput()
      Refreshes final output on the last complete snapshot after successful execution.
    • finishOutput

      public void finishOutput(OutputSlice stdout, OutputSlice stderr)
      Refreshes final output on the last complete snapshot using explicit slices.
      Parameters:
      stdout - Captured standard output slice.
      stderr - Captured standard error slice.
    • materializeSnapshots

      public void materializeSnapshots()
      Materializes all completed snapshot output slices into self-contained buffers.
    • materializeSnapshots

      public void materializeSnapshots(byte[] sharedStdout, byte[] sharedStderr)
      Materializes all completed snapshot output slices using shared output buffers.
      Parameters:
      sharedStdout - Shared standard output buffer.
      sharedStderr - Shared standard error buffer.
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable