pyocd / pyOCD

Open source Python library for programming and debugging Arm Cortex-M microcontrollers
https://pyocd.io
Apache License 2.0
1.13k stars 484 forks source link

pyOCD FreeBSD USB problem / faulthandler module #1032

Open cederom opened 3 years ago

cederom commented 3 years ago

Hello world :-)

Something terrible happened on my system and pyOCD started to segfault :-( I have tried different pyOCD versions, Python versions, etc. Was there a big change in USB handling recently in pyOCD @flit ? Other applications using Python and PyUSB/LibUSB works fine. Any hints welcome :-)

I was not really able to debug whole python with pyocd neither with gdb nor pdb/pudb3.

But I have found amazing module that simply catches all segfaults and prints nice backtrack automatically on crash :-)

Without faulthandler:

(venv37zephyr) pyocd list
Segmentation fault

With faulthandler we instantly get a backtrace on crash and we simply can see the cause of the problem:

(venv37zephyr) pyocd list
Fatal Python error: Segmentation fault

Current thread 0x0000000800a3a000 (most recent call first):
  File "/home/cd/usr/local/venv37zephyr/lib/python3.7/site-packages/usb/backend/libusb1.py", line 611 in __init__
  File "/home/cd/usr/local/venv37zephyr/lib/python3.7/site-packages/usb/backend/libusb1.py", line 644 in __iter__
  File "/home/cd/usr/local/venv37zephyr/lib/python3.7/site-packages/usb/core.py", line 1280 in device_iter
  File "/home/cd/usr/local/venv37zephyr/lib/python3.7/site-packages/pyocd/probe/pydapaccess/interface/pyusb_v2_backend.py", line 184 in get_all_connected_interfaces
  File "/home/cd/usr/local/venv37zephyr/lib/python3.7/site-packages/pyocd/probe/pydapaccess/dap_access_cmsis_dap.py", line 67 in _get_interfaces
  File "/home/cd/usr/local/venv37zephyr/lib/python3.7/site-packages/pyocd/probe/pydapaccess/dap_access_cmsis_dap.py", line 466 in get_connected_devices
  File "/home/cd/usr/local/venv37zephyr/lib/python3.7/site-packages/pyocd/probe/cmsis_dap_probe.py", line 73 in get_all_connected_probes
  File "/home/cd/usr/local/venv37zephyr/lib/python3.7/site-packages/pyocd/probe/aggregator.py", line 64 in get_all_connected_probes
  File "/home/cd/usr/local/venv37zephyr/lib/python3.7/site-packages/pyocd/core/helpers.py", line 82 in get_all_connected_probes
  File "/home/cd/usr/local/venv37zephyr/lib/python3.7/site-packages/pyocd/core/helpers.py", line 109 in list_connected_probes
  File "/home/cd/usr/local/venv37zephyr/lib/python3.7/site-packages/pyocd/__main__.py", line 458 in do_list
  File "/home/cd/usr/local/venv37zephyr/lib/python3.7/site-packages/pyocd/__main__.py", line 398 in run
  File "/home/cd/usr/local/venv37zephyr/lib/python3.7/site-packages/pyocd/__main__.py", line 928 in main
  File "/home/cd/usr/local/venv37zephyr/bin/pyocd", line 10 in <module>
Segmentation fault

This only required two lines to be added:

import re⏎ 
import sys⏎
import faulthandler⏎
faulthandler.enable()⏎
from pyocd.__main__ import main⏎
if __name__ == '__main__':⏎
    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])⏎
    sys.exit(main())⏎

The question is do we want to have such traceback in the code just in case code crashes and we simply get what we need at will?

If so where should we put that? In pyocd script?

cederom commented 3 years ago

Code proposition for easy backtracking this kind of problems of python crashes: https://github.com/pyocd/pyOCD/pull/1033 :-)

cederom commented 3 years ago

faulthandler module is not default part of Python 3.3+. This may bring important/helpful information in case of problems :-)