Documentation
¶
Overview ¶
Package debug provides a k6 extension module for debugging JavaScript test scripts. It exports `capture` and `breakpoint` functions that are injected by the Babel preprocessor.
Usage:
import { capture, breakpoint } from 'k6/x/debug';
When K6_DEBUG_DAP environment variable is set (e.g., K6_DEBUG_DAP=:4711), a DAP server is started for IDE integration. Otherwise, debug output goes to stderr and breakpoints can be resumed via stdin.
Index ¶
- type BreakpointManager
- func (bm *BreakpointManager) EnsureVUState(vuID uint64)
- func (bm *BreakpointManager) EnterScope(vuID uint64, scopeID int, parentID int)
- func (bm *BreakpointManager) GetVUState(vuID uint64) *VUDebugState
- func (bm *BreakpointManager) GetVisibleVariables(vuID uint64) []VisibleVar
- func (bm *BreakpointManager) HasAnyBreakpoints() bool
- func (bm *BreakpointManager) IsDisconnected() bool
- func (bm *BreakpointManager) IsSet(file string, line int) bool
- func (bm *BreakpointManager) IsSteppingOver(vuID uint64, line int) bool
- func (bm *BreakpointManager) ListVUs() []uint64
- func (bm *BreakpointManager) ListenStdinResume()
- func (bm *BreakpointManager) Pause(ctx context.Context, vuID uint64, file string, line int, scopeID int, ...) ResumeAction
- func (bm *BreakpointManager) RemoveVU(vuID uint64)
- func (bm *BreakpointManager) RequestPauseAll()
- func (bm *BreakpointManager) Resume(vuID uint64, action ResumeAction)
- func (bm *BreakpointManager) ResumeAll(action ResumeAction)
- func (bm *BreakpointManager) SetBreakpoints(file string, lines []int)
- func (bm *BreakpointManager) SetDisconnected()
- func (bm *BreakpointManager) SetOnStopped(fn func(vuID uint64, file string, line int))
- func (bm *BreakpointManager) SetOnThread(fn func(vuID uint64, reason string))
- func (bm *BreakpointManager) ShouldPauseAll(vuID uint64) bool
- func (bm *BreakpointManager) ShouldStepPause(vuID uint64, line int) bool
- func (bm *BreakpointManager) StoreVariable(vuID uint64, name string, value sobek.Value, scopeID int)
- type DAPVariable
- type ModuleInstance
- func (mi *ModuleInstance) Breakpoint(location sobek.Value)
- func (mi *ModuleInstance) Capture(location sobek.Value, value sobek.Value)
- func (mi *ModuleInstance) EnterScope(scopeID int, parentID int)
- func (mi *ModuleInstance) Exports() modules.Exports
- func (mi *ModuleInstance) Line(location sobek.Value)
- type ResumeAction
- type RootModule
- type ScopedVariable
- type VUDebugState
- type VariableStore
- type VisibleVar
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BreakpointManager ¶
type BreakpointManager struct {
// contains filtered or unexported fields
}
BreakpointManager manages breakpoints and per-VU debug state.
func NewBreakpointManager ¶
func NewBreakpointManager() *BreakpointManager
NewBreakpointManager creates a new BreakpointManager.
func (*BreakpointManager) EnsureVUState ¶
func (bm *BreakpointManager) EnsureVUState(vuID uint64)
EnsureVUState initializes VU state if it doesn't exist.
func (*BreakpointManager) EnterScope ¶
func (bm *BreakpointManager) EnterScope(vuID uint64, scopeID int, parentID int)
EnterScope registers a scope's parent relationship and clears any variables from a previous entry of this scope (e.g., previous loop iteration or function call).
func (*BreakpointManager) GetVUState ¶
func (bm *BreakpointManager) GetVUState(vuID uint64) *VUDebugState
GetVUState returns the debug state for a VU.
func (*BreakpointManager) GetVisibleVariables ¶
func (bm *BreakpointManager) GetVisibleVariables(vuID uint64) []VisibleVar
GetVisibleVariables returns variables that are in scope at the VU's current pause point, in the order they were first captured.
func (*BreakpointManager) HasAnyBreakpoints ¶
func (bm *BreakpointManager) HasAnyBreakpoints() bool
HasAnyBreakpoints returns true if any breakpoints have been set via DAP.
func (*BreakpointManager) IsDisconnected ¶
func (bm *BreakpointManager) IsDisconnected() bool
IsDisconnected returns true if the DAP client has disconnected.
func (*BreakpointManager) IsSet ¶
func (bm *BreakpointManager) IsSet(file string, line int) bool
IsSet checks if a breakpoint is set at the given file and line. It first tries an exact path match, then falls back to basename matching to handle the mismatch between VS Code's absolute paths and the preprocessor's relative filenames.
func (*BreakpointManager) IsSteppingOver ¶
func (bm *BreakpointManager) IsSteppingOver(vuID uint64, line int) bool
IsSteppingOver returns true if the VU is currently in a step operation and the given line is the same as where it last paused. This prevents breakpoints on the current line from re-triggering immediately after a step resumes.
func (*BreakpointManager) ListVUs ¶
func (bm *BreakpointManager) ListVUs() []uint64
ListVUs returns all known VU IDs.
func (*BreakpointManager) ListenStdinResume ¶
func (bm *BreakpointManager) ListenStdinResume()
ListenStdinResume reads lines from stdin and resumes all paused VUs on each line. This is the fallback mode when no DAP server is active.
func (*BreakpointManager) Pause ¶
func (bm *BreakpointManager) Pause(ctx context.Context, vuID uint64, file string, line int, scopeID int, notify bool) ResumeAction
Pause blocks the VU goroutine until resumed or context is cancelled. If notify is true, the onStopped callback is invoked (sends DAP stopped event), but only if this VU is the first to stop in this round (allStopped not yet set). If another VU already triggered the stop, this VU pauses silently. Returns the resume action taken, or ActionContinue if context was cancelled.
func (*BreakpointManager) RemoveVU ¶
func (bm *BreakpointManager) RemoveVU(vuID uint64)
RemoveVU cleans up state for a VU.
func (*BreakpointManager) RequestPauseAll ¶
func (bm *BreakpointManager) RequestPauseAll()
RequestPauseAll sets the allStopped flag so all VUs pause at their next instrumented statement. Called when one VU hits a breakpoint.
func (*BreakpointManager) Resume ¶
func (bm *BreakpointManager) Resume(vuID uint64, action ResumeAction)
Resume unblocks a paused VU with the given action.
func (*BreakpointManager) ResumeAll ¶
func (bm *BreakpointManager) ResumeAll(action ResumeAction)
ResumeAll resumes all paused VUs and clears the stop flags.
func (*BreakpointManager) SetBreakpoints ¶
func (bm *BreakpointManager) SetBreakpoints(file string, lines []int)
SetBreakpoints sets the active breakpoints for a file. Replaces any previous breakpoints for that file.
func (*BreakpointManager) SetDisconnected ¶
func (bm *BreakpointManager) SetDisconnected()
SetDisconnected marks the client as disconnected and resumes all paused VUs. After this, Pause() becomes a no-op to prevent VUs from blocking.
func (*BreakpointManager) SetOnStopped ¶
func (bm *BreakpointManager) SetOnStopped(fn func(vuID uint64, file string, line int))
SetOnStopped sets a callback invoked when a VU pauses at a breakpoint.
func (*BreakpointManager) SetOnThread ¶
func (bm *BreakpointManager) SetOnThread(fn func(vuID uint64, reason string))
SetOnThread sets a callback invoked when a VU starts or exits.
func (*BreakpointManager) ShouldPauseAll ¶
func (bm *BreakpointManager) ShouldPauseAll(vuID uint64) bool
ShouldPauseAll returns true if the VU should pause because another VU triggered a breakpoint (all-stop mode). Only returns true for VUs that aren't already paused.
func (*BreakpointManager) ShouldStepPause ¶
func (bm *BreakpointManager) ShouldStepPause(vuID uint64, line int) bool
ShouldStepPause checks if the VU should pause due to a pending step (next/stepIn). It only triggers when the current line differs from where the VU was previously paused, so multiple captures on the same source line (e.g. variable + __line marker) don't cause duplicate pauses. Clears the stepping flag when it returns true.
func (*BreakpointManager) StoreVariable ¶
func (bm *BreakpointManager) StoreVariable(vuID uint64, name string, value sobek.Value, scopeID int)
StoreVariable stores a captured variable for a VU with its scope ID.
type DAPVariable ¶
type DAPVariable struct {
Name string
Value string
Type string
VariablesReference int // >0 if expandable
}
DAPVariable represents a variable for the DAP protocol.
type ModuleInstance ¶
type ModuleInstance struct {
// contains filtered or unexported fields
}
ModuleInstance is the per-VU instance of the debug module.
func (*ModuleInstance) Breakpoint ¶
func (mi *ModuleInstance) Breakpoint(location sobek.Value)
Breakpoint is called in place of `debugger` statements. location is {line, col, file, scope}.
func (*ModuleInstance) Capture ¶
func (mi *ModuleInstance) Capture(location sobek.Value, value sobek.Value)
Capture is called after each instrumented variable assignment. It only stores the variable value — all pause logic is handled by Line(). location is {line, col, name, file, scope}, value is the JS variable value.
func (*ModuleInstance) EnterScope ¶
func (mi *ModuleInstance) EnterScope(scopeID int, parentID int)
EnterScope registers a scope's parent relationship and clears stale variables from a previous entry of this scope (e.g., previous iteration or loop pass).
func (*ModuleInstance) Exports ¶
func (mi *ModuleInstance) Exports() modules.Exports
Exports returns the named exports for `import { line, capture, breakpoint, enterScope } from 'k6/x/debug'`.
func (*ModuleInstance) Line ¶
func (mi *ModuleInstance) Line(location sobek.Value)
Line is called at the start of each instrumented source line. It handles breakpoint detection and step-over logic, pausing the VU if needed. location is {line, col, file, scope}.
type ResumeAction ¶
type ResumeAction int
ResumeAction represents the action to take when resuming a paused VU.
const ( // ActionContinue resumes execution until next breakpoint. ActionContinue ResumeAction = iota // ActionNext steps to the next instrumented statement (behaves like continue for now). ActionNext // ActionStepIn steps into the next instrumented statement (behaves like continue for now). ActionStepIn )
type RootModule ¶
type RootModule struct {
// contains filtered or unexported fields
}
RootModule is the top-level module, created once per test run.
func (*RootModule) NewModuleInstance ¶
func (rm *RootModule) NewModuleInstance(vu modules.VU) modules.Instance
NewModuleInstance creates a per-VU module instance.
type ScopedVariable ¶
ScopedVariable pairs a captured value with its lexical scope ID.
type VUDebugState ¶
type VUDebugState struct {
Resume chan ResumeAction
Paused bool
PauseLine int
PauseFile string
PauseScope int // scope ID at the pause point
Variables map[string]ScopedVariable // variable name → scoped value
VarOrder []string // insertion order of variable names
LastAction ResumeAction // the action that last resumed this VU
}
VUDebugState holds the debug state for a single VU.
type VariableStore ¶
type VariableStore struct {
// contains filtered or unexported fields
}
VariableStore manages DAP variablesReference IDs for expandable objects.
func NewVariableStore ¶
func NewVariableStore() *VariableStore
NewVariableStore creates a new VariableStore.
func (*VariableStore) Add ¶
func (vs *VariableStore) Add(obj *sobek.Object) int
Add stores a sobek object and returns a new reference ID.
func (*VariableStore) Clear ¶
func (vs *VariableStore) Clear()
Clear resets the store between pauses.
type VisibleVar ¶
VisibleVar is a variable name-value pair returned in capture order.