BitBangDevice segfault with pylibftdi 0.16.1.2

Issue #27 closed
Jorge Pereira
created an issue

Hello. I'm using a 'Sparkfun Breakout Board for FT232RL USB to Serial', trying BitBang for the first time. I'm using Ubuntu 17.04 x64.

I installed pylibftdi with pip3 and when trying examples I always get a segmentation fault when initializing the device.

Seen issue #25 and using 'auto_detach=False' as a workaround works.

Also tried installing 'libftdi1-dev' but didn't work.

pylibftdi version     : 0.16.1.2
libftdi version       : libftdi_version(major=1, minor=3, micro=0, version_str=b'1.3', snapshot_str=b'unknown')
libftdi library name  : libftdi1.so.2
libusb version        : libusb_version(major=1, minor=0, micro=21, nano=11156, rc=b'', describe=b'http://libusb.info')
libusb library name   : libusb-1.0.so.0
Python version        : 3.5.3
OS platform           : Linux-4.10.0-35-lowlatency-x86_64-with-Ubuntu-17.04-zesty

dmesg when connecting FTDI:

New USB device found, idVendor=0403, idProduct=6001
New USB device strings: Mfr=1, Product=2, SerialNumber=3
Product: FT232R USB UART
Manufacturer: FTDI
SerialNumber: A105BPBO
usbcore: registered new interface driver ftdi_sio
usbserial: USB Serial support registered for FTDI USB Serial Device
ftdi_sio 1-6.2:1.0: FTDI USB Serial Device converter detected
Detected FT232RL
FTDI USB Serial Device converter now attached to ttyUSB3

I'm running python3 with sudo, didn't create udev rule yet.

sudo python3 -m pylibftdi.examples.list_devices
FTDI:FT232R USB UART:A105BPBO

Comments (6)

  1. Jonas Jonsson

    I'm seeing similar issue on Fedora 27 with pylibftdi installed with pip3. auto_detach=False is a workaround.

    $ python3 -m pylibftdi.examples.info
    pylibftdi version     : 0.16.1.2
    libftdi version       : libftdi_version(major=1, minor=3, micro=0, version_str=b'1.3', snapshot_str=b'unknown')
    libftdi library name  : libftdi1.so.2
    libusb version        : libusb_version(major=1, minor=0, micro=21, nano=11156, rc=b'', describe=b'http://libusb.info')
    libusb library name   : libusb-1.0.so.0
    Python version        : 3.6.4
    OS platform           : Linux-4.15.8-300.fc27.x86_64-x86_64-with-fedora-27-Twenty_Seven
    
    gdb --args /usr/bin/python3 -m pylibftdi.examples.pin_read
    ...
    Thread 1 "python3" received signal SIGSEGV, Segmentation fault.
    libusb_set_auto_detach_kernel_driver (dev_handle=0x558c33c0, enable=1) at core.c:2013
    2013    core.c: No such file or directory.
    (gdb) bt
    #0  libusb_set_auto_detach_kernel_driver (dev_handle=0x558c33c0, enable=1) at core.c:2013
    #1  0x00007fffef899d1e in ffi_call_unix64 () at ../src/x86/unix64.S:76
    #2  0x00007fffef89968f in ffi_call (cif=<optimized out>, fn=<optimized out>, rvalue=<optimized out>, avalue=<optimized out>) at ../src/x86/ffi64.c:525
    #3  0x00007fffefaadcbb in _call_function_pointer (argcount=2, resmem=0x7fffffffbf70, restype=<optimized out>, atypes=0x7fffffffbf30, avalues=0x7fffffffbf50, pProc=0x7fffecaef5c0 <libusb_set_auto_detach_kernel_driver>, flags=4353) at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Modules/_ctypes/callproc.c:809
    #4  _ctypes_callproc () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Modules/_ctypes/callproc.c:1147
    #5  0x00007fffefaae6c4 in PyCFuncPtr_call () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Modules/_ctypes/_ctypes.c:3962
    #6  0x00007ffff7997494 in _PyObject_FastCallDict () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Objects/abstract.c:2331
    #7  0x00007ffff7a079ed in call_function () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Python/ceval.c:4848
    #8  0x00007ffff7a39aba in _PyEval_EvalFrameDefault () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Python/ceval.c:3322
    #9  0x00007ffff79c966a in _PyFunction_FastCall (globals=<optimized out>, nargs=1, args=<optimized out>, co=<optimized out>) at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Python/ceval.c:4906
    #10 fast_function () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Python/ceval.c:4941
    #11 0x00007ffff7a0797e in call_function () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Python/ceval.c:4845
    #12 0x00007ffff7a39aba in _PyEval_EvalFrameDefault () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Python/ceval.c:3322
    #13 0x00007ffff7996505 in _PyEval_EvalCodeWithName () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Python/ceval.c:4153
    #14 0x00007ffff79c989f in fast_function () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Python/ceval.c:4965
    #15 0x00007ffff7a0797e in call_function () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Python/ceval.c:4845
    #16 0x00007ffff7a39aba in _PyEval_EvalFrameDefault () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Python/ceval.c:3322
    #17 0x00007ffff7996505 in _PyEval_EvalCodeWithName () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Python/ceval.c:4153
    #18 0x00007ffff799731d in _PyFunction_FastCallDict () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Python/ceval.c:5057
    #19 0x00007ffff799766e in _PyObject_FastCallDict () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Objects/abstract.c:2310
    #20 0x00007ffff79a1741 in _PyObject_Call_Prepend () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Objects/abstract.c:2373
    #21 0x00007ffff7997aab in PyObject_Call () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Objects/abstract.c:2261
    #22 0x00007ffff79fde89 in slot_tp_init () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Objects/typeobject.c:6407
    #23 0x00007ffff79fa8ee in type_call () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Objects/typeobject.c:915
    #24 0x00007ffff7997494 in _PyObject_FastCallDict () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Objects/abstract.c:2331
    #25 0x00007ffff79fa858 in _PyObject_FastCallKeywords () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Objects/abstract.c:2496
    #26 0x00007ffff7a079ed in call_function () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Python/ceval.c:4848
    #27 0x00007ffff7a3a9a5 in _PyEval_EvalFrameDefault () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Python/ceval.c:3338
    #28 0x00007ffff79c966a in _PyFunction_FastCall (globals=<optimized out>, nargs=0, args=<optimized out>, co=<optimized out>) at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Python/ceval.c:4906
    #29 fast_function () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Python/ceval.c:4941
    #30 0x00007ffff7a0797e in call_function () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Python/ceval.c:4845
    #31 0x00007ffff7a39aba in _PyEval_EvalFrameDefault () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Python/ceval.c:3322
    #32 0x00007ffff7996373 in _PyEval_EvalCodeWithName () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Python/ceval.c:4153
    #33 0x00007ffff79c989f in fast_function () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Python/ceval.c:4965
    #34 0x00007ffff7a0797e in call_function () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Python/ceval.c:4845
    #35 0x00007ffff7a39aba in _PyEval_EvalFrameDefault () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Python/ceval.c:3322
    #36 0x00007ffff79c9538 in _PyFunction_FastCall (co=<optimized out>, args=<optimized out>, nargs=1, globals=<optimized out>) at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Python/ceval.c:4906
    #37 0x00007ffff7a0797e in call_function () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Python/ceval.c:4845
    #38 0x00007ffff7a39aba in _PyEval_EvalFrameDefault () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Python/ceval.c:3322
    #39 0x00007ffff7a149e8 in _PyEval_EvalCodeWithName (qualname=0x0, name=<optimized out>, closure=<optimized out>, kwdefs=<optimized out>, defcount=0, defs=<optimized out>, kwstep=2, kwcount=<optimized out>, kwargs=0x0, kwnames=0x0, argcount=0, args=0x0, 
        locals={'__name__': '__main__', '__doc__': '\nDisplay values on input pins of a BitBangDevice.\n\nTODO:\n * ANSI colours / display differences in bold\n\nexample - beep on pin 1 going high:\n    $ pylibftdi/examples/pin_read.py -n 0.01 -m 1 -k 1 && beep\n\nCopyright (c) 2011-2014 Ben Bass <benbass@codedstructure.net>\nAll rights reserved.\n', '__package__': 'pylibftdi.examples', '__loader__': <SourceFileLoader(name='pylibftdi.examples.pin_read', path='/home/jonasj/.local/lib/python3.6/site-packages/pylibftdi/examples/pin_read.py') at remote 0x7fffecf59400>, '__spec__': <ModuleSpec(name='pylibftdi.examples.pin_read', loader=<...>, origin='/home/jonasj/.local/lib/python3.6/site-packages/pylibftdi/examples/pin_read.py', loader_state=None, submodule_search_locations=None, _set_fileattr=True, _cached='/home/jonasj/.local/lib/python3.6/site-packages/pylibftdi/examples/__pycache__/pin_read.cpython-36.pyc') at remote 0x7fffecf59470>, '__annotations__': {}, '__builtins__': <module at remote 0x7ffff7fc4638>, '__file__': '/hom...(truncated), 
        globals={'__name__': '__main__', '__doc__': '\nDisplay values on input pins of a BitBangDevice.\n\nTODO:\n * ANSI colours / display differences in bold\n\nexample - beep on pin 1 going high:\n    $ pylibftdi/examples/pin_read.py -n 0.01 -m 1 -k 1 && beep\n\nCopyright (c) 2011-2014 Ben Bass <benbass@codedstructure.net>\nAll rights reserved.\n', '__package__': 'pylibftdi.examples', '__loader__': <SourceFileLoader(name='pylibftdi.examples.pin_read', path='/home/jonasj/.local/lib/python3.6/site-packages/pylibftdi/examples/pin_read.py') at remote 0x7fffecf59400>, '__spec__': <ModuleSpec(name='pylibftdi.examples.pin_read', loader=<...>, origin='/home/jonasj/.local/lib/python3.6/site-packages/pylibftdi/examples/pin_read.py', loader_state=None, submodule_search_locations=None, _set_fileattr=True, _cached='/home/jonasj/.local/lib/python3.6/site-packages/pylibftdi/examples/__pycache__/pin_read.cpython-36.pyc') at remote 0x7fffecf59470>, '__annotations__': {}, '__builtins__': <module at remote 0x7ffff7fc4638>, '__file__': '/hom...(truncated), _co=<code at remote 0x7fffecf5b270>) at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Python/ceval.c:4153
    #40 PyEval_EvalCodeEx () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Python/ceval.c:4174
    #41 0x00007ffff7a1577b in PyEval_EvalCode (co=co@entry=<code at remote 0x7fffecf5b270>, 
        globals=globals@entry={'__name__': '__main__', '__doc__': '\nDisplay values on input pins of a BitBangDevice.\n\nTODO:\n * ANSI colours / display differences in bold\n\nexample - beep on pin 1 going high:\n    $ pylibftdi/examples/pin_read.py -n 0.01 -m 1 -k 1 && beep\n\nCopyright (c) 2011-2014 Ben Bass <benbass@codedstructure.net>\nAll rights reserved.\n', '__package__': 'pylibftdi.examples', '__loader__': <SourceFileLoader(name='pylibftdi.examples.pin_read', path='/home/jonasj/.local/lib/python3.6/site-packages/pylibftdi/examples/pin_read.py') at remote 0x7fffecf59400>, '__spec__': <ModuleSpec(name='pylibftdi.examples.pin_read', loader=<...>, origin='/home/jonasj/.local/lib/python3.6/site-packages/pylibftdi/examples/pin_read.py', loader_state=None, submodule_search_locations=None, _set_fileattr=True, _cached='/home/jonasj/.local/lib/python3.6/site-packages/pylibftdi/examples/__pycache__/pin_read.cpython-36.pyc') at remote 0x7fffecf59470>, '__annotations__': {}, '__builtins__': <module at remote 0x7ffff7fc4638>, '__file__': '/hom...(truncated), 
        locals=locals@entry={'__name__': '__main__', '__doc__': '\nDisplay values on input pins of a BitBangDevice.\n\nTODO:\n * ANSI colours / display differences in bold\n\nexample - beep on pin 1 going high:\n    $ pylibftdi/examples/pin_read.py -n 0.01 -m 1 -k 1 && beep\n\nCopyright (c) 2011-2014 Ben Bass <benbass@codedstructure.net>\nAll rights reserved.\n', '__package__': 'pylibftdi.examples', '__loader__': <SourceFileLoader(name='pylibftdi.examples.pin_read', path='/home/jonasj/.local/lib/python3.6/site-packages/pylibftdi/examples/pin_read.py') at remote 0x7fffecf59400>, '__spec__': <ModuleSpec(name='pylibftdi.examples.pin_read', loader=<...>, origin='/home/jonasj/.local/lib/python3.6/site-packages/pylibftdi/examples/pin_read.py', loader_state=None, submodule_search_locations=None, _set_fileattr=True, _cached='/home/jonasj/.local/lib/python3.6/site-packages/pylibftdi/examples/__pycache__/pin_read.cpython-36.pyc') at remote 0x7fffecf59470>, '__annotations__': {}, '__builtins__': <module at remote 0x7ffff7fc4638>, '__file__': '/hom...(truncated)) at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Python/ceval.c:730
    #42 0x00007ffff7a4b8f2 in builtin_exec_impl.isra.11 (
        locals={'__name__': '__main__', '__doc__': '\nDisplay values on input pins of a BitBangDevice.\n\nTODO:\n * ANSI colours / display differences in bold\n\nexample - beep on pin 1 going high:\n    $ pylibftdi/examples/pin_read.py -n 0.01 -m 1 -k 1 && beep\n\nCopyright (c) 2011-2014 Ben Bass <benbass@codedstructure.net>\nAll rights reserved.\n', '__package__': 'pylibftdi.examples', '__loader__': <SourceFileLoader(name='pylibftdi.examples.pin_read', path='/home/jonasj/.local/lib/python3.6/site-packages/pylibftdi/examples/pin_read.py') at remote 0x7fffecf59400>, '__spec__': <ModuleSpec(name='pylibftdi.examples.pin_read', loader=<...>, origin='/home/jonasj/.local/lib/python3.6/site-packages/pylibftdi/examples/pin_read.py', loader_state=None, submodule_search_locations=None, _set_fileattr=True, _cached='/home/jonasj/.local/lib/python3.6/site-packages/pylibftdi/examples/__pycache__/pin_read.cpython-36.pyc') at remote 0x7fffecf59470>, '__annotations__': {}, '__builtins__': <module at remote 0x7ffff7fc4638>, '__file__': '/hom...(truncated), 
        globals={'__name__': '__main__', '__doc__': '\nDisplay values on input pins of a BitBangDevice.\n\nTODO:\n * ANSI colours / display differences in bold\n\nexample - beep on pin 1 going high:\n    $ pylibftdi/examples/pin_read.py -n 0.01 -m 1 -k 1 && beep\n\nCopyright (c) 2011-2014 Ben Bass <benbass@codedstructure.net>\nAll rights reserved.\n', '__package__': 'pylibftdi.examples', '__loader__': <SourceFileLoader(name='pylibftdi.examples.pin_read', path='/home/jonasj/.local/lib/python3.6/site-packages/pylibftdi/examples/pin_read.py') at remote 0x7fffecf59400>, '__spec__': <ModuleSpec(name='pylibftdi.examples.pin_read', loader=<...>, origin='/home/jonasj/.local/lib/python3.6/site-packages/pylibftdi/examples/pin_read.py', loader_state=None, submodule_search_locations=None, _set_fileattr=True, _cached='/home/jonasj/.local/lib/python3.6/site-packages/pylibftdi/examples/__pycache__/pin_read.cpython-36.pyc') at remote 0x7fffecf59470>, '__annotations__': {}, '__builtins__': <module at remote 0x7ffff7fc4638>, '__file__': '/hom...(truncated), source=<code at remote 0x7fffecf5b270>) at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Python/bltinmodule.c:983
    #43 builtin_exec () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Python/clinic/bltinmodule.c.h:283
    #44 0x00007ffff79fa37f in _PyCFunction_FastCallDict () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Objects/methodobject.c:234
    #45 0x00007ffff7a078ba in call_function () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Python/ceval.c:4824
    #46 0x00007ffff7a39aba in _PyEval_EvalFrameDefault () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Python/ceval.c:3322
    #47 0x00007ffff7996373 in _PyEval_EvalCodeWithName () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Python/ceval.c:4153
    #48 0x00007ffff79c989f in fast_function () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Python/ceval.c:4965
    #49 0x00007ffff7a0797e in call_function () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Python/ceval.c:4845
    #50 0x00007ffff7a39aba in _PyEval_EvalFrameDefault () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Python/ceval.c:3322
    #51 0x00007ffff7a149e8 in _PyEval_EvalCodeWithName (qualname=0x0, name=<optimized out>, closure=<optimized out>, kwdefs=<optimized out>, defcount=1, defs=<optimized out>, kwstep=2, kwcount=<optimized out>, kwargs=0x0, kwnames=0x0, argcount=<optimized out>, args=0x55555581aa40, locals=0x0, globals=<optimized out>, _co=<code at remote 0x7fffefcc9300>) at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Python/ceval.c:4153
    #52 PyEval_EvalCodeEx () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Python/ceval.c:4174
    #53 0x00007ffff7a1592c in function_call () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Objects/funcobject.c:604
    #54 0x00007ffff7997aab in PyObject_Call () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Objects/abstract.c:2261
    #55 0x00007ffff7aa01d1 in RunModule () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Modules/main.c:217
    #56 0x00007ffff7aa06ad in Py_Main () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Modules/main.c:754
    #57 0x0000555555554d45 in main () at /usr/src/debug/python3-3.6.4-9.fc27.x86_64/Programs/python.c:102
    #58 0x00007ffff6b73f2a in __libc_start_main () from /lib64/libc.so.6
    #59 0x0000555555554eba in _start ()
    
  2. Jonas Jonsson

    Some initial debugging shows that there is some 32/64 bit issue with the pointer.

    The libusb device handle is 0x5555558c9790 but libusb_set_auto_detach_kernel_driver (dev_handle=0x558c9790, .... hints that the pointer is cast to a 32-bit pointer instead of using the full 64-bit pointer.

    See below output from gdb.

    ftdi_init (ftdi=0x55555581c340) at /usr/src/debug/libftdi-1.3-11.fc28.x86_64/src/ftdi.c:111
    111     /usr/src/debug/libftdi-1.3-11.fc28.x86_64/src/ftdi.c: No such file or directory.
    (gdb) print ftdi
    $4 = (struct ftdi_context *) 0x55555581c340
    (gdb) c
    Continuing.
    Detaching after fork from child process 28345.
    
    Thread 1 "python3" received signal SIGSEGV, Segmentation fault.
    libusb_set_auto_detach_kernel_driver (dev_handle=0x558c9790, enable=1) at core.c:2013
    2013    core.c: No such file or directory.
    (gdb) print (struct ftdi_context *) 0x55555581c340
    $5 = (struct ftdi_context *) 0x55555581c340
    (gdb) print *(struct ftdi_context *) 0x55555581c340
    $6 = {usb_ctx = 0x5555558c9150, usb_dev = 0x5555558c9790, usb_read_timeout = 5000, usb_write_timeout = 5000, type = TYPE_R,
      baudrate = 9600, bitbang_enabled = 0 '\000', readbuffer = 0x5555558dfe30 "\240B\360\366\377\177", readbuffer_offset = 0,
      readbuffer_remaining = 0, readbuffer_chunksize = 4096, writebuffer_chunksize = 4096, max_packet_size = 64, interface = 0,
      index = 1, in_ep = 2, out_ep = 129, bitbang_mode = 1 '\001', eeprom = 0x5555557ec400,
      error_str = 0x7fffecd0e860 "all fine", module_detach_mode = AUTO_DETACH_SIO_MODULE}
    
  3. Ben Bass repo owner

    Thanks for this - really appreciate your time debugging it as I was thinking of taking out the auto detach altogether.

    I'll get this merged and push a new release to PyPI over the next few days.

  4. Log in to comment