Skip to content

Commit 77654f6

Browse files
author
Pavel Minaev
authored
Merge pull request #39 from int19h/apiref
Refactor debugpy API and CLI for clarity and consistency.
2 parents 7d34a12 + 8447a15 commit 77654f6

22 files changed

Lines changed: 628 additions & 519 deletions

README.md

Lines changed: 34 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,23 @@ This debugger implements the Debug Adapter Protocol: [debugProtocol.json](https:
1717

1818
## `debugpy` CLI Usage
1919
### Debugging a script file
20-
To run a script file with debugging enabled, but without waiting for the IDE to attach (i.e. code starts executing immediately):
20+
To run a script file with debugging enabled, but without waiting for the client to attach (i.e. code starts executing immediately):
2121
```console
22-
-m debugpy --host localhost --port 5678 myfile.py
22+
-m debugpy --listen localhost:5678 myfile.py
2323
```
24-
To wait until the IDE attaches before running your code, use the `--wait` switch.
24+
To wait until the client attaches before running your code, use the `--wait-for-client` switch.
2525
```console
26-
-m debugpy --host localhost --port 5678 --wait myfile.py
26+
-m debugpy --listen localhost:5678 --wait-for-client myfile.py
2727
```
28-
The `--host` option specifies the interface on which the debug server is listening for connections. To be able to attach from another machine, make sure that the server is listening on a public interface - using `0.0.0.0` will make it listen on all available interfaces:
28+
The hostname passed to `--listen` specifies the interface on which the debug adapter will be listening for connections from DAP clients. It can be omitted, with only the port number specified:
2929
```console
30-
-m debugpy --host 0.0.0.0 --port 5678 myfile.py
30+
-m debugpy --listen 5678 ...
31+
```
32+
in which case the default interface is 127.0.0.1.
33+
34+
To be able to attach from another machine, make sure that the adapter is listening on a public interface - using `0.0.0.0` will make it listen on all available interfaces:
35+
```console
36+
-m debugpy --listen 0.0.0.0:5678 myfile.py
3137
```
3238
This should only be done on secure networks, since anyone who can connect to the specified port can then execute arbitrary code within the debugged process.
3339

@@ -36,100 +42,64 @@ To pass arguments to the script, just specify them after the filename. This work
3642
### Debugging a module
3743
To run a module, use the `-m` switch instead of filename:
3844
```console
39-
-m debugpy --host localhost --port 5678 -m mymodule
45+
-m debugpy --listen localhost:5678 -m mymodule
4046
```
41-
Same as with scripts, command line arguments can be passed to the module by specifying them after the module name. All other debugpy switches work identically in this mode; in particular, `--wait` can be used to block execution until the IDE attaches.
47+
Same as with scripts, command line arguments can be passed to the module by specifying them after the module name. All other debugpy switches work identically in this mode; in particular, `--wait-for-client` can be used to block execution until the client attaches.
4248

4349
### Attaching to a running process by ID
4450
The following command injects the debugger into a process with a given PID that is running Python code. Once the command returns, a debugpy server is running within the process, as if that process was launched via `-m debugpy` itself.
4551
```console
46-
-m debugpy --host localhost --port 5678 --pid 12345
52+
-m debugpy --listen localhost:5678 --pid 12345
4753
```
4854

4955
## `debugpy` Import usage
5056
### Enabling debugging
51-
At the beginning of your script, import debugpy, and call `debugpy.enable_attach()` to start the debug server. The default hostname is `0.0.0.0`, and the default port is 5678; these can be overridden by passing a `(host, port)` tuple as the first argument of `enable_attach()`.
57+
At the beginning of your script, import debugpy, and call `debugpy.listen()` to start the debug adapter, passing a `(host, port)` tuple as the first argument.
5258
```python
5359
import debugpy
54-
debugpy.enable_attach()
60+
debugpy.listen(("localhost", 5678))
61+
...
62+
```
63+
As with the `--listen` command line switch, hostname can be omitted, and defaults to `"127.0.0.1"`:
64+
```python
65+
debugpy.listen(5678)
5566
...
5667
```
5768

58-
### Waiting for the IDE to attach
59-
Use the `debugpy.wait_for_attach()` function to block program execution until the IDE is attached.
69+
### Waiting for the client to attach
70+
Use the `debugpy.wait_for_client()` function to block program execution until the client is attached.
6071
```python
6172
import debugpy
62-
debugpy.enable_attach()
63-
debugpy.wait_for_attach() # blocks execution until IDE is attached
73+
debugpy.listen(5678)
74+
debugpy.wait_for_client() # blocks execution until client is attached
6475
...
6576
```
6677

6778
### `breakpoint()` function
68-
In Python 3.7 and above, `debugpy` supports the standard `breakpoint()` function. Use `debugpy.break_into_debugger()` function for similar behavior and compatibility with older versions of Python (3.6 and below). If the debugger is attached when either of these functions is invoked, it will pause execution on the calling line, as if it had a breakpoint set. If there's no IDE attached, the functions do nothing, and the code continues to execute normally.
79+
In Python 3.7 and above, `debugpy` supports the standard `breakpoint()` function. Use `debugpy.breakpoint()` function for similar behavior and compatibility with older versions of Python. If the debugger is attached when either of these functions is invoked, it will pause execution on the calling line, as if it had a breakpoint set. If there's no client attached, the functions do nothing, and the code continues to execute normally.
6980
```python
7081
import debugpy
71-
debugpy.enable_attach()
82+
debugpy.listen(...)
7283

7384
while True:
7485
...
75-
breakpoint() # or debugpy.break_into_debugger() on <3.7
86+
breakpoint() # or debugpy.breakpoint() on 3.6 and below
7687
...
7788
```
7889

79-
## Custom Protocol arguments
80-
### Launch request arguments
81-
```json5
82-
{
83-
"debugOptions": [
84-
"RedirectOutput", // Whether to redirect stdout and stderr (see pydevd_comm.CMD_REDIRECT_OUTPUT)
85-
"WaitOnNormalExit", // Wait for user input after user code exits normally
86-
"WaitOnAbnormalExit", // Wait for user input after user code exits with error
87-
"Django", // Enables Django Template debugging
88-
"Jinja", // Enables Jinja (Flask) Template debugging
89-
"FixFilePathCase", // See FIX_FILE_PATH_CASE in wrapper.py
90-
"DebugStdLib", // Whether to enable debugging of standard library functions
91-
"StopOnEntry", // Whether to stop at first line of user code
92-
"ShowReturnValue", // Show return values of functions
93-
]
94-
}
95-
```
96-
97-
### Attach request arguments
98-
```json5
99-
{
100-
"debugOptions": [
101-
"RedirectOutput", // Whether to redirect stdout and stderr (see pydevd_comm.CMD_REDIRECT_OUTPUT)
102-
"Django", // Enables Django Template debugging
103-
"Jinja", // Enables Jinja (Flask) Template debugging
104-
"FixFilePathCase", // See FIX_FILE_PATH_CASE in wrapper.py
105-
"DebugStdLib", // Whether to enable debugging of standard library functions
106-
"WindowsClient", // Whether client OS is Windows
107-
"UnixClient", // Whether client OS is Unix
108-
"ShowReturnValue", // Show return values of functions
109-
],
110-
"pathMappings": [
111-
{
112-
"localRoot": "C:\\Project\\src", // Local root (where the IDE is running)
113-
"remoteRoot": "/home/smith/proj" // Remote root (where remote code is running)
114-
},
115-
// Add more path mappings
116-
]
117-
}
118-
```
119-
12090
## Debugger logging
12191

122-
To enable debugger internal logging via CLI, the `--log-dir` switch can be used:
92+
To enable debugger internal logging via CLI, the `--log-to` switch can be used:
12393
```console
124-
-m debugpy --log-dir path/to/logs ...
94+
-m debugpy --log-to path/to/logs ...
12595
```
12696

127-
When using `enable_attach`, the same can be done with `log_dir` argument:
97+
When using the API, the same can be done with `debugpy.log_to()`:
12898
```py
129-
debugpy.enable_attach(log_dir='path/to/logs')
99+
debugpy.log_to('path/to/logs')
100+
debugpy.listen(...)
130101
```
131102

132103
In both cases, the environment variable `DEBUGPY_LOG_DIR` can also be set to the same effect.
133104

134105
When logging is enabled, debugpy will create several log files with names matching `debugpy*.log` in the specified directory, corresponding to different components of the debugger. When subprocess debugging is enabled, separate logs are created for every subprocess.
135-

0 commit comments

Comments
 (0)