@@ -16,7 +16,9 @@ The module :mod:`pdb` defines an interactive source code debugger for Python
16
16
programs. It supports setting (conditional) breakpoints and single stepping at
17
17
the source line level, inspection of stack frames, source code listing, and
18
18
evaluation of arbitrary Python code in the context of any stack frame. It also
19
- supports post-mortem debugging and can be called under program control.
19
+ supports post-mortem debugging and can be called under program control. The
20
+ :mod: `pdb ` module also supports remote attaching to a running Python process
21
+ using a ``-p PID `` command-line option.
20
22
21
23
..index ::
22
24
single: Pdb (class in pdb)
@@ -80,7 +82,10 @@ The debugger's prompt is ``(Pdb)``, which is the indicator that you are in debug
80
82
You can also invoke:mod: `pdb ` from the command line to debug other scripts. For
81
83
example::
82
84
83
- python -m pdb [-c command] (-m module | pyfile) [args ...]
85
+ python -m pdb [-c command] (-m module | -p pid | pyfile) [args ...]
86
+
87
+ Remote debugging is supported by attaching to a running Python process using
88
+ the ``-p `` option and a PID.
84
89
85
90
When invoked as a module, pdb will automatically enter post-mortem debugging if
86
91
the program being debugged exits abnormally. After post-mortem debugging (or
@@ -104,6 +109,27 @@ useful than quitting the debugger upon program's exit.
104
109
..versionchanged ::3.7
105
110
Added the ``-m `` option.
106
111
112
+ ..option ::-p ,--pid <pid >
113
+
114
+ Attach to the process with the specified PID.
115
+
116
+ ..versionchanged ::3.14
117
+ Added the ``-p `` and ``--pid `` options. This feature leverages:pep: `768 `
118
+ and the:func: `sys.remote_exec ` function to attach to the remote process
119
+ and send the PDB commands to it.
120
+
121
+
122
+ To attach to a running Python process for remote debugging, use the ``-p `` or
123
+ ``--pid `` option with the target process's PID::
124
+
125
+ python -m pdb -p 1234
126
+
127
+ This will connect to the Python process with the given PID and allow you to
128
+ debug it interactively. Notice that due to how the Python interpreter works,
129
+ attaching to a remote process that is blocked in a system call or waiting for
130
+ I/O will only work once the next bytecode instruction is executed or when the
131
+ process receives a signal.
132
+
107
133
Typical usage to execute a statement under control of the debugger is::
108
134
109
135
>>> import pdb