pyocd / pyOCD

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

0.19.0 FreeBSD gdb server usb dev.kernel_detach_driver(interface_number) #675

Closed cederom closed 4 years ago

cederom commented 5 years ago

Hello world :-) I have noticed a problem with kernel detach on usb when starting the gdbserver as standard user, it works when running as root, all permissions for user are granted to access the usb devices except kernel operations, will have to find if that detach is really necessary to operate..

(venv36bsd) [cd@0xCFC2 ~/cederom/usr/local/venv36bsd/bin]$ pyocd gdb
0000736:INFO:board:Target type is nrf52
0000749:ERROR:__main__:uncaught exception: [Errno None] Other error
Traceback (most recent call last):
  File "/home/cd/cederom/usr/local/venv36bsd/lib/python3.6/site-packages/pyocd/__main__.py", line 300, in run
    self._commands[self._args.cmd]()
  File "/home/cd/cederom/usr/local/venv36bsd/lib/python3.6/site-packages/pyocd/__main__.py", line 460, in do_gdbserver
    **sessionOptions)
  File "/home/cd/cederom/usr/local/venv36bsd/lib/python3.6/site-packages/pyocd/core/helpers.py", line 199, in session_with_chosen_probe
    session.open()
  File "/home/cd/cederom/usr/local/venv36bsd/lib/python3.6/site-packages/pyocd/core/session.py", line 278, in open
    self._probe.open()
  File "/home/cd/cederom/usr/local/venv36bsd/lib/python3.6/site-packages/pyocd/probe/cmsis_dap_probe.py", line 137, in open
    self._link.open()
  File "/home/cd/cederom/usr/local/venv36bsd/lib/python3.6/site-packages/pyocd/probe/pydapaccess/dap_access_cmsis_dap.py", line 562, in open
    self._interface.open()
  File "/home/cd/cederom/usr/local/venv36bsd/lib/python3.6/site-packages/pyocd/probe/pydapaccess/interface/pyusb_backend.py", line 104, in open
    dev.detach_kernel_driver(interface_number)
  File "/home/cd/cederom/usr/local/venv36bsd/lib/python3.6/site-packages/usb/core.py", line 1077, in detach_kernel_driver
    interface)
  File "/home/cd/cederom/usr/local/venv36bsd/lib/python3.6/site-packages/usb/backend/libusb1.py", line 902, in detach_kernel_driver
    _check(self.lib.libusb_detach_kernel_driver(dev_handle.handle, intf))
  File "/home/cd/cederom/usr/local/venv36bsd/lib/python3.6/site-packages/usb/backend/libusb1.py", line 595, in _check
    raise USBError(_strerror(ret), ret, _libusb_errno[ret])
usb.core.USBError: [Errno None] Other error

(venv36bsd) [cd@0xCFC2 ~/cederom/usr/local/venv36bsd/bin]$ pyocd -V
0.19.0

(venv36bsd) [cd@0xCFC2 ~/cederom/usr/local/venv36bsd/bin]$ uname -a
FreeBSD 0xCFC2 12.0-RELEASE-p4 FreeBSD 12.0-RELEASE-p4 GENERIC  amd64
cederom commented 5 years ago

The problem is:

Any hints welcome @flit :-)

cederom commented 4 years ago

Got back to this problem even with simple pyocd reset after update. Will look into it and propose a solution :-)

Note: All kernel modules related to umodem/ucom/u3g are unloaded and devd is not working so no modules are loaded upon device attach. I observed a problem with umodem module but it should not interfere with pyOCD operations right? Anyways serial port comes handy to talk with Target over DAPLink and also it is necessary for my LTE modem to operate so unloading is not an option :-)

cederom commented 4 years ago

Allright, the pyOCD tries to unload kernel driver for interface 3 on DAPLink, that is HID CMSIS-DAP. We do not really want to detach HID driver for the whole system and normal user is not permitted to. Nothing happens if we skip that unload, DAPLink works fine.. also nothing was handled in the exception handler anyway.. so I have changed source to simply catch all exceptions and print some debug and warning messages that may come useful :-)

https://github.com/mbedmicro/pyOCD/pull/939

cederom commented 4 years ago

The only minor issue left is that exception raises [None] Other error code / message. FreeBSD has its own LibUSB implementation. It looks like the error codes may differ between implementations. Someone reported similar problem with LimeSDR device. I will look into it in a free moment.. but the blocker seems to be fixed :-)

cederom commented 4 years ago

Patch committed to the master. Closing. Thank you @flit :-)