Skip to content

How interactive debugging works (old way)

Rich Chiodo edited this page Jul 22, 2022 · 15 revisions

The Interactive Window is used for running cells in a python script. One of the capabilities of running these cells is to debug them.

This page will describe how this debugging is implemented under the covers.

What does debug cell do?

image

This sequence diagram is explained below:

Inject debugpy

The first step is to inject debugpy into kernel. Without knowing what the process id of the kernel is, we need a way to have the kernel ready for attach. We do this by running this code in the kernel:

import debugpy
debugpy.listen(('localhost', 0))

That causes debugpy to start a server and returns the port to listen on. We use this port to generate a launch config. Something that looks like so:

        {
            "type": "python",
            "request": "attach",
            "connect": { "host": "localhost", "port": 5678 },
            "justMyCode": true
        },

This

Attaching

The launch config generated in the previous step is used to attach the debugger to the running debugpy server (much like done here for launching debugging of a python file).

VS code then transitions to debug mode. It just sits there waiting for an event from the debuggee.

Translation of file paths

Changing cell hashes

Clone this wiki locally