pyOCD icon indicating copy to clipboard operation
pyOCD copied to clipboard

debug pyOCD with pdb/pudb before install

Open cederom opened this issue 3 years ago • 6 comments

Hello world :-)

I would like to try and debug my local changes to pyOCD even before package is created and installed with setup.py. How can I do that? :-)

% python pyocd/tools/pyocd.py
Traceback (most recent call last):
  File "pyocd/tools/pyocd.py", line 40, in <module>
    from .. import __version__
ValueError: attempted relative import beyond top-level package

Any hints welcome :-)

cederom avatar Aug 20 '20 12:08 cederom

Okay, I had to create a helper script in top level pyOCD source directory where I can specify what module I want to start:

#!/usr/bin/env python
import pyocd.tools.pyocd
p=pyocd.tools.pyocd.PyOCDTool()
p.run()

Then I call that script with pudb3 debug.py some_parameters.

I can now put import pudb; pudb.set_trace() wherever I want to inervene and I just jump into the soruce code and variables browser provided by pudb :-)

If you know any better way please let me know :-)

cederom avatar Aug 20 '20 13:08 cederom

Hi @cederom 😄

I've been using VSCode for debugging pyOCD with great success. It's so much more efficient (for me, at least) than command line debugging.

Are you trying to debug the install process? I guess not since you're not debugging setup.py. So the question is why you need to debug before the package is installed?

If you're not already, use an editable install: pip install -e . (from the pyocd working copy root)

Here's an example config for VSCode launch.json. This assumes an editable install into a virtualenv.

        {
            "name": "pyocd gdb",
            "type": "python",
            "request": "launch",
            "module": "pyocd.__main__",
            "console": "integratedTerminal",
            "stopOnEntry": false,
            "pythonPath": "${workspaceFolder}/sandbox/bin/python3",
            "showReturnValue": true,
            "args": ["gdb", "-Svv"]
        },

In case where pyOCD is being called by another tool, you can use the technique presented in #471. This then lets you attach VSCode to a running instance.

(I intend to add all this to the docs at some point!)

flit avatar Aug 20 '20 22:08 flit

Thank you @flit :-) I avoid Microsoft crap by any means necessary :-) I only use BSD Unix, ZSH, TMux, Vim, some GNU stuff, and basically only Open-Source utilities :-)

I just wanted to quickly debug the USB HID driver detach issue step by step before install.. and it worked :-) With time and your hints I will probably get a smarter more versatile approach. When its done I can put that into the documentation if you like. Thanks again :-)

cederom avatar Aug 21 '20 00:08 cederom

A non-Microsoft option is Eclipse with the pyDev plugins. I used this for years and was very happy.

(I used to be rabidly anti-Microsoft, but they've changed quite a bit recently. I've been really impressed by VSCode.)

flit avatar Aug 21 '20 14:08 flit

Hello world :-) I have came back again here regarding https://github.com/pyocd/pyOCD/issues/1144. I have created and provided simple helper script to allow pyOCD debug directly in Unix shell - even in a situation when exception occurs during pyocd import (simple pudb3 pyocd does not work in this case). Not to waste time a simple shell script can be launched to jump into the curses based python debugger :-)

Here is the solution: https://github.com/pyocd/pyOCD/pull/1151

cederom avatar May 12 '21 13:05 cederom

Whoops. Internals of pyOCD changed a lot recently. I have updated the script. With this kind of reorganization why not a major number bump (i.e. 1.0 instead 0.30)? :-)

cederom avatar May 12 '21 14:05 cederom