fabioz / PyDev.Debugger

Sources for the debugger used in PyDev, PyCharm and VSCode Python
Eclipse Public License 1.0
419 stars 121 forks source link

Does pydev.debugger support debugpy DAP client? #242

Closed oldljh closed 1 year ago

oldljh commented 1 year ago

Does pydev.debugger support debugpy DAP client? When run python pydev_run_in_console.py --help, it need client port parameter. But debugpy does not have corresponding parameter.

fabioz commented 1 year ago

It does, however it has 2 shortcomings vs using debugpy:

  1. It doesn't have the functionality to automatically attach to subprocesses (this isn't right now a part of the standard DAP, although it appears that the DAP will add support for that, at which point the pydev debugger may support it, but right now this needs some custom messages only available in debugpy).
  2. It doesn't handle the part related to launching the actual program (i.e.: in the DAP you'd usually start the DAP and based on the launch configuration it'd issue a request to launch the actual code to be run for you, in the pydev debugger use case the debugger is expected to run directly in the same process without an additional intermediary process, so, you'd start the code as you'd want and the debugger would run in the same process).

I'm curious though, what is your use case? i.e.: is there a particular reason for wanting to use PyDev.Debugger instead of debugpy (the engine on both is the pydev debugger, the only difference is that debugpy does massage the API a bit so that it's easier to use the DAP directly).

Regarding starting pydevd in dap mode, I need to take a look and add some documentation on how to do that (it'd be definitely be possible, but I need to add better docs for doing that).

oldljh commented 1 year ago

Actually I just want to know how to use pydev.Debugger in order to develop a special purpose ide based on it. You said it's a DAP debugger backend, I started thinking debugpy was the frontend but it wasn't. Does github have an open source frontend program that uses pydev.Debugger as a backend? Although vscode is open source, it is too complicated to understand how frontend uses DAP to interact with pydev.Debugger.

fabioz commented 1 year ago

In this case my suggestion would be to take a look into the debugger integration tests. It does all the scaffolding and acts as a pseudo-client for testing -- see: https://github.com/fabioz/PyDev.Debugger/blob/main/tests_python/test_debugger_json.py.

The debugger actually has more than one message format it supports (I still recommend using the DAP approach though as DAP messages are better documented at https://microsoft.github.io/debug-adapter-protocol/specification).

The command line would end up being something as

python -u /path/to/pydevd.py --client 127.0.0.1 --port 55987 --debug-mode debugpy-dap --json-dap-http --file file_to_launch.py

Note that you need to open the --port before and the debugger will connect to that port.

It'll differ a bit from the default protocol because:

  1. It can only connect through a port, not using stdin/stdout as would be expected from a dap
  2. You're doing the launch of the program along with the DAP, so, you should do an attach request and not a launch request (in practice it can only deal with the attach for its use case as you're launching the program along with the debugger at the same time vs the dap use case which would launch the program only afterwards through the launch request).
fabioz commented 1 year ago

p.s.: In this case you can actually automatically attach to new sub processes (it'll just try to create a new session that'll attach to the port specified initially).

oldljh commented 1 year ago

thank you very much!

fabioz commented 1 year ago

I'm closing this as I think there's nothing really actionable to do, but fell free to ask more questions...