Skip to content

Commit 178a0f4

Browse files
authored
Directions on how to debug pydevd (#1694)
* Directions on how to debug pydevd * Review feedback
1 parent f7d5df0 commit 178a0f4

File tree

1 file changed

+109
-1
lines changed

1 file changed

+109
-1
lines changed

CONTRIBUTING.md

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,117 @@ You might need to regenerate the Cython modules after any changes. This can be d
117117
- pip install cython, django>=1.9, setuptools>=0.9, wheel>0.21, twine
118118
- On a windows machine:
119119
- set FORCE_PYDEVD_VC_VARS=C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Auxiliary\Build\vcvars64.bat
120-
- set PYDEVD_FORCE_BUILD_ALL=True
121120
- in the pydevd folder: python .\build_tools\build.py
122121

122+
## Pushing pydevd back to PyDev.Debugger
123+
124+
If you've made changes to pydevd (at src/debugpy/_vendored/pydevd), you'll want to push back changes to pydevd so as Fabio makes changes to pydevd we can continue to share updates.
125+
126+
### Setting up pydevd to be testable
127+
128+
Follow these steps to get pydevd testable:
129+
130+
- git clone https://github.com/fabioz/PyDev.Debugger (or using your own fork)
131+
- copy all of your changes from src/debugpy/_vendored/pydevd to the root of your PyDev.Debugger clone
132+
- remove the pdb files (pydevd doesn't ship those) if you rebuilt the attach dlls
133+
- create an environment to test. The list of stuff in your environment is outlined [here](https://github.com/fabioz/PyDev.Debugger/blob/6cd4d431e6a794448f33a73857d479149041500a/.github/workflows/pydevd-tests-python.yml#L83).
134+
135+
### Testing pydevd and fixing test failures
136+
137+
Pydevd has a lot more tests on execution than debugpy. They reside in all of the `test` folders under the root. The majority of the execution tests are in the `tests_python` folder.
138+
139+
You run all of the tests with (from the root folder):
140+
141+
- python -m pytest -n auto -rfE
142+
143+
That will run all of the tests in parallel and output any failures.
144+
145+
If you want to just see failures you can do this:
146+
147+
- python -m pytest -n auto -q
148+
149+
That should generate output that just lists the tests which failed.
150+
151+
```
152+
=============================================== short test summary info ===============================================
153+
FAILED tests_python/test_debugger.py::test_path_translation[True] - AssertionError: TimeoutError (note: error trying to dump threads on timeout).
154+
FAILED tests_python/test_debugger.py::test_remote_debugger_multi_proc[False] - AssertionError: TimeoutError
155+
FAILED tests_python/test_debugger.py::test_path_translation[False] - AssertionError: TimeoutError (note: error trying to dump threads on timeout).
156+
======================== 3 failed, 661 passed, 169 skipped, 77 warnings in 319.05s (0:05:19) =========================
157+
```
158+
With that you can then run individual tests like so:
159+
160+
- python -m pytest -n auto tests_python/test_debugger.py::test_path_translation[False]
161+
162+
That will generate a log from the test run.
163+
164+
Logging the test output can be tricky so here's some information on how to debug the tests.
165+
166+
#### How to add more logging
167+
168+
The pydevd tests log everything to the console and to a text file during the test. If you scroll up in the console, it should show the log file it read the logs from:
169+
170+
```
171+
Log on failure:
172+
-------------------- C:\Users\rchiodo\AppData\Local\Temp\pytest-of-rchiodo\pytest-77\popen-gw3\test_path_translation_and_sour0\pydevd_debug_file_23524.32540.txt ------------------
173+
```
174+
175+
If you want to add more logging in order to investigate something that isn't working, you simply add a line like so in the code:
176+
177+
```python
178+
pydevd_log.debug("Some test logging", frame, etc)
179+
```
180+
181+
Make sure if you add this in a module that gets `cythonized`, that you turn off `Cython` support as listed above. Otherwise you'll have to regen the C code or you won't actually see your new log output.
182+
183+
#### How to use logs to debug failures
184+
185+
Investigating log failures can be done in multiple ways.
186+
187+
If you have an existing test failing, you can investigate it by running the test with the main branch and comparing the results. To do so you would:
188+
189+
- Clone the repo a second time
190+
- Change the code in `tests_python/debugger_unittest.py` so that the test prints out logs on success too (by default it only logs the output on a failure)
191+
- Run the failing test in the second clone
192+
- Run the failing test in your original clone
193+
- Diff the results by finding the log file name in the output and diffing those two files
194+
- Add more logging around where the differences first appear
195+
- Repeat running and diffing
196+
197+
If you're adding a new test or just trying to figure out what the expected log output is, you would look at the failing test to see what steps are expected in the output. Here's an example:
198+
199+
```python
200+
def test_case_double_remove_breakpoint(case_setup):
201+
with case_setup.test_file("_debugger_case_remove_breakpoint.py") as writer:
202+
breakpoint_id = writer.write_add_breakpoint(writer.get_line_index_with_content("break here"))
203+
writer.write_make_initial_run()
204+
205+
hit = writer.wait_for_breakpoint_hit()
206+
writer.write_remove_breakpoint(breakpoint_id)
207+
writer.write_remove_breakpoint(breakpoint_id) # Double-remove (just check that we don't have an error).
208+
writer.write_run_thread(hit.thread_id)
209+
210+
writer.finished_ok = True
211+
```
212+
213+
That test would have events correlating to:
214+
215+
- Initialization (all debug sessions have this)
216+
- Setting breakpoints on a specific line
217+
- Breakpoint event being hit
218+
- Setting breakpoints to empty
219+
- Setting breakpoints to empty
220+
- Continue event
221+
222+
Those would show up in the log like so:
223+
224+
Breakpoint command
225+
```
226+
0.00s - Received command: CMD_SET_BREAK 111 3 1 python-line C:\Users\rchiodo\source\repos\PyDev.Debugger\tests_python\resources\_debugger_case_remove_breakpoint.py 7 None None None
227+
```
228+
229+
In order to investigate a failure you'd look for the CMDs you expect and then see where the CMDs deviate. At that point you'd add logging around what might have happened next.
230+
123231
## Using modified debugpy in Visual Studio Code
124232
To test integration between debugpy and Visual Studio Code, the latter can be directed to use a custom version of debugpy in lieu of the one bundled with the Python extension. This is done by specifying `"debugAdapterPath"` in `launch.json` - it must point at the root directory of the *package*, which is `src/debugpy` inside the repository:
125233

0 commit comments

Comments
 (0)