Skip to content

Add a “step back” Feature to pdb #141599

@the-osiris

Description

@the-osiris

Proposal:

Summary

I’d like to propose adding support for a “step back” (reverse stepping) feature in the pdb debugger. Today, pdb supports forward stepping (step, next, until), but there is no mechanism to move execution backward to a previous line or state. This feature would make pdb more powerful for debugging complex execution paths, especially when investigating state changes, loops, and regressions.

Motivation

Reverse debugging is supported in several modern debuggers (e.g., gdb with process recording, rr, C# debugger “Step Back”, and many JavaScript tooling environments). It is particularly useful when you:
• Step too far and want to return to the previous state
• Inspect how a particular variable changed across steps
• Debug loops and recursive calls
• Investigate nondeterministic behavior or regressions more efficiently

Today, pdb users must restart their program or manually set breakpoints to re-enter earlier states, which is slow and unintuitive compared to modern debugging workflows.

Possible Approaches (Open for Discussion)

There are several potential designs, and I am looking for guidance from core maintainers:

  1. Record & replay mode (lightweight time-travel)
    • Record snapshots of frames, locals/globals, and last executed lines.
    • Rewind to the last recorded state when the user runs back or reverse-step.

  2. AST- or bytecode-based re-execution
    • Restart execution from an earlier safe point up to the target line.
    • Similar to how some trace-based debuggers “rewind” by re-running code deterministically.

  3. Integration with existing tracing hooks
    • pdb already uses sys.settrace; a buffer of recent trace events could allow stepping backward.

  4. Minimal prototype
    • Support only a “single step back” (undo the last step) as a starting point.
    • Later could expand to multiple steps / reverse-continue.

I’m happy to develop an initial prototype once there is agreement on a feasible direction.

Key Questions for Maintainers

• Is a step back feature appropriate for pdb’s scope?
• Should this begin as an experimental or opt-in feature?
• Would a single-step-back MVP be acceptable for a first contribution?

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions