Page 1 of 1

Python script segfaults.

PostPosted: May 31st, '12, 16:49
by camorri
I have a mail notification device that works with Slackware without problems. Lsusb shows the device as
Code: Select all
Bus 002 Device 002: ID 1294:1320 RISO KAGAKU CORP.


The script I can post, if that is necessary. It requires pyusb to be installed. I installed pyusb, version 1.0.0 release 1.mag2 noarch from the repos. On Slackware I have version 0.4.1 installed. It is for a 64 bit system, Mag is 64 bit also.

I am not a programmer, so I have no idea how to debug a segfault. The script was copied from my Slack system, so I dought the problem is the script itself. It is simple enough, it turns on ( or off ) leds in the device.

Not sure what to do next. Advice is appreciated.

Re: Python script segfaults.

PostPosted: May 31st, '12, 21:57
by doktor5000
Can you please show the output of the program with the segfault?
For how to debug it, please take a look at https://wiki.mageia.org/en/Debugging_software_crashes

Re: Python script segfaults.

PostPosted: Jun 1st, '12, 15:35
by camorri
Not much to show, here it is.

Code: Select all
./setcolor.py 6
Segmentation fault


Same result if I run it as root, or from the file installed in /usr/bin.

Here is the script.

Code: Select all
#!/usr/bin/env python

import usb
import sys
import time

class MailNotifier:

        def makeData(self, color):
                return (color, 0, 0, 0, 0)

        def __init__(self):
                self.dev=UsbDevice(0x1294, 0x1320)
                self.dev.open()
                self.dev.handle.reset()

        def setColor(self, color):
                self.dev.handle.interruptWrite(0x02, self.makeData(color), 1000)

class UsbDevice:
        def __init__(self, vendor_id, product_id):
                busses = usb.busses()
                self.handle = None
                count = 0
                for bus in busses:
                                devices = bus.devices
                                for dev in devices:
                                        if dev.idVendor==vendor_id and dev.idProduct==product_id:
                                                self.dev = dev
                                                self.conf = self.dev.configurations[0]
                                                self.intf = self.conf.interfaces[0][0]
                                                self.endpoints = []
                                                for endpoint in self.intf.endpoints:
                                                        self.endpoints.append(endpoint)
                                                return
                sys.stderr.write("No mail notifier found\n")

        def open(self):
                if self.handle:
                        self.handle = None
                try:
                        self.handle = self.dev.open()
                        self.handle.detachKernelDriver(0)
                        self.handle.detachKernelDriver(1)
                        self.handle.setConfiguration(self.conf)
                        self.handle.claimInterface(self.intf)
                        self.handle.setAltInterface(self.intf)
                        return True
                except:
                        return False

def main(argv):
        if len(argv) != 2:
                sys.stderr.write("Usage : %s color_number\n" % argv[0])
        else:
                m = MailNotifier()
                m.setColor(int(argv[1]))

if __name__=="__main__":
        main(sys.argv)

Re: Python script segfaults.

PostPosted: Jun 1st, '12, 16:09
by isadora

Re: Python script segfaults.

PostPosted: Jun 1st, '12, 16:14
by camorri
Further investigation on pyusb package shows in the readme:
2) At least one of the supported libraries (libusb 1.0, libusb 0.1 or OpenUSB)


I looked for any of the libusb packages and none are installed. Looking through the list shows only packages for Arch i586, and nothing for x86_64.

So, I guess the next wuestion is are my repos set up correctly? The other possibility is the 64 bit versions are not there; where can I find them?

Futher investigation, I have lib64usb1.0_0 version 1.0.9 installed, I'm guessing that looks after the above lib requirement.

Re: Python script segfaults.

PostPosted: Jun 2nd, '12, 19:20
by doktor5000
Please show the output of
Code: Select all
rpm -qa | grep -i usb

Re: Python script segfaults.

PostPosted: Jun 2nd, '12, 23:45
by camorri
Code: Select all
rpm -qa | grep -i usb
lib64usb1.0_0-1.0.9-1.mga2
usb_modeswitch-1.2.1-1.mga2
lib64gusb2-0.1.3-1.mga2
usbutils-005-1.mga2
x11-driver-video-sisusb-0.9.4-6.mga2
lib64usbmuxd1-1.0.7-2.mga2
usb_modeswitch-data-20111023-1.mga2
lib64usb-compat0.1_4-0.1.3-6.mga2
usbmuxd-1.0.7-2.mga2
pyusb-1.0.0-1.mga2

Re: Python script segfaults.

PostPosted: Jun 3rd, '12, 18:53
by doktor5000
Well, you have both libusb 1.0 and libusb 0.1 as the script requires, so it should work. But it maybe that those two are conflicting, and you can only have one installed. Or you need the i586 version, but i'm not sure.

I'm afraid i'm also not good at python debugging, but i suggest to run it under strace, and study the output, and maybe it gives a hint to what the actual problem is.
Just do something like
Code: Select all
strace -f -o output.log sh setcolor.py 6
and then afterwards have a look in the file output.log

Re: Python script segfaults.

PostPosted: Jun 4th, '12, 01:05
by JoesCat
Some USB devices need to load a binary file to make them work.

What is this device you are using? 0x1294 0x1320

It might be a possibility that this device isn't getting whatever internal firmware driver loaded.
Sometimes these devices change ID after they have their binary loaded, for example, your device 0x1294,0x1320 may be 1294,0x1330 before it loads the binary file.

You can type
lsusb
on the command line to see if it identifies itself.

It may need some sort of appropriate startup file within udev's rules.d directory.

...just throwing some ideas.

Re: Python script segfaults.

PostPosted: Jun 4th, '12, 20:45
by doktor5000
Have a look at the first post for the lsusb output ;)

Re: Python script segfaults.

PostPosted: Jun 4th, '12, 23:38
by camorri
Got a question. The command you asked to run
Code: Select all
strace -f -o output.log sh setcolor.py 6
If I run this command, with the 'sh' in front of the setcolor.py; I get a symbol on the command line, looking for me to draw an elastic box around something. If I do, then I do not see the program run to completion, or the segfault error. I do not understand what the 'sh' is doing. I ran this command as root.

I ran the command without the sh and get the expected output.log file created. It Segfaults in Magia. I ran the same command on my slack system, and created an output file there also. I can not point at anything that I see as a problem. I' going to include the last 20 or so lines from each run. My hope is someone hear can point me in the right direction.

First the Magia run.

Code: Select all
7246  open("/dev/bus/usb/002/003", O_RDONLY) = 7
7246  lseek(7, 18, SEEK_SET)            = 18
7246  read(7, "\t\2\261\0\2\1\0\300\0\t\4\0\0\3\340\1\1\0\7\5\201\3\20\0\1\7\5\2\2@\0\1"..., 177) = 177
7246  close(7)                          = 0
7246  open("/dev/bus/usb/002/003", O_RDONLY) = 7
7246  lseek(7, 18, SEEK_SET)            = 18
7246  read(7, "\t\2\261\0\2\1\0\300", 8) = 8
7246  close(7)                          = 0
7246  open("/dev/bus/usb/002/003", O_RDONLY) = 7
7246  lseek(7, 18, SEEK_SET)            = 18
7246  read(7, "\t\2\261\0\2\1\0\300\0\t\4\0\0\3\340\1\1\0\7\5\201\3\20\0\1\7\5\2\2@\0\1"..., 177) = 177
7246  close(7)                          = 0
7246  open("/dev/bus/usb/002/002", O_RDWR) = 7
7246  write(4, "\1", 1)                 = 1
7246  read(3, "\1", 1)                  = 1
7246  ioctl(7, SNDRV_CTL_IOCTL_ELEM_READ or USBDEVFS_IOCTL or USBDEVFS_IOCTL32, {ifno=0x0, ioctl_code=0x5516}) = -1 ENODATA (No data available)
7246  ioctl(7, SNDRV_CTL_IOCTL_ELEM_LOCK or USBDEVFS_RESET, 0) = 0
7246  write(4, "\1", 1)                 = 1
7246  read(3, "\1", 1)                  = 1
7246  close(7)                          = 0
7246  --- {si_signo=SIGSEGV, si_code=SEGV_MAPERR, si_addr=0x40} (Segmentation fault) ---
7246  +++ killed by SIGSEGV +++


Now, from the Slackware run.

Code: Select all
3321  read(3, "\t\2\31\0\1\1\0\340", 8) = 8
3321  close(3)                          = 0
3321  open("/dev/bus/usb/002/001", O_RDONLY) = 3
3321  lseek(3, 18, SEEK_SET)            = 18
3321  read(3, "\t\2\31\0\1\1\0\340\0\t\4\0\0\1\t\0\0\0\7\5\201\3\2\0\377", 25) = 25
3321  close(3)                          = 0
3321  open("/dev/bus/usb/002/002", O_RDWR) = 3
3321  write(5, "\1", 1)                 = 1
3321  read(4, "\1", 1)                  = 1
3321  ioctl(3, USBDEVFS_IOCTL or USBDEVFS_IOCTL32, 0x7fff057fe170) = -1 ENODATA (No data available)
3321  ioctl(3, USBDEVFS_RESET, 0)       = 0
3321  ioctl(3, USBDEVFS_SUBMITURB or USBDEVFS_SUBMITURB32, 0x6a2900) = 0
3321  timerfd_settime(6, TFD_TIMER_ABSTIME, {it_interval={0, 0}, it_value={367, 381043000}}, NULL) = 0
3321  poll([{fd=4, events=POLLIN}, {fd=6, events=POLLIN}, {fd=3, events=POLLOUT}], 3, 60000) = 1 ([{fd=3, revents=POLLOUT}])
3321  ioctl(3, USBDEVFS_REAPURBNDELAY or USBDEVFS_REAPURBNDELAY32, 0x7fff057fe658) = 0
3321  timerfd_settime(6, 0, {it_interval={0, 0}, it_value={0, 0}}, NULL) = 0
3321  write(5, "\1", 1)                 = 1
3321  read(4, "\1", 1)                  = 1
3321  close(3)                          = 0
3321  rt_sigaction(SIGINT, {SIG_DFL, [], SA_RESTORER, 0x7fe5c3e0ac50}, {0x7fe5c4138090, [], SA_RESTORER, 0x7fe5c3e0ac50}, 8) = 0
3321  exit_group(0)


If needed, I can post the entire output.log from each system. The only other thing I noticed from the trace is Slack is running Python 2.6, and Mag is running 2.7. No idea if this is significant or not.

Thank-you for your continued help.

Re: Python script segfaults.

PostPosted: Jun 5th, '12, 20:13
by doktor5000
camorri wrote:Got a question. The command you asked to run
Code: Select all
strace -f -o output.log sh setcolor.py 6
If I run this command, with the 'sh' in front of the setcolor.py; I get a symbol on the command line, looking for me to draw an elastic box around something. If I do, then I do not see the program run to completion, or the segfault error. I do not understand what the 'sh' is doing. I ran this command as root.

I ran the command without the sh and get the expected output.log file created.


Sorry, i didn't know how to run a python script under strace, but you found out yourself :)
Maybe you user needs to be in the usb group? Please show the output of
Code: Select all
id name_of_your_user
and maybe compare that to slackware.
Also it could be that in that slackware install usbfs is mounted, could you please show the output of
Code: Select all
mount
from slackware?

Re: Python script segfaults.

PostPosted: Jun 5th, '12, 21:53
by camorri
From my Slackware system
Code: Select all
mount
/dev/sdb1 on / type ext4 (rw,relatime,user_xattr,acl,barrier=1,data=ordered)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
none on /proc/bus/usb type usbfs (rw,devgid=1000,devmode=664)
/dev/sdb5 on /home type ext4 (rw,user_xattr)
/dev/sdb6 on /usr type ext4 (rw)
/dev/sda6 on /mnt/sda6 type ext4 (rw)
tmpfs on /dev/shm type tmpfs (rw)
tmpfs on /tmp type tmpfs (rw)
none on /home/cliff/.thumbnails type tmpfs (rw)
none on /tmp type tmpfs (rw)
nfsd on /proc/fs/nfs type nfsd (rw)
nfsd on /proc/fs/nfsd type nfsd (rw)


I'll boot, and post the rest of the asked for information.

From Magia,

Code: Select all
id cliff
uid=1000(cliff) gid=4(adm) groups=4(adm),2(daemon),7(lp),12(mail),19(floppy),22(cdrom),43(usb),80(cdwriter),81(audio),82(video),100(users),497(avahi),494(haldaemon),490(usbmux),486(mlocate),420(vboxusers),415(toruser)

Re: Python script segfaults.

PostPosted: Jun 5th, '12, 22:36
by doktor5000
camorri wrote:
Code: Select all
none on /proc/bus/usb type usbfs (rw,devgid=1000,devmode=664)

See? That could already be the important difference.

Re: Python script segfaults.

PostPosted: Jun 6th, '12, 16:07
by camorri
I added this line to /etc/fstab on Mag

Code: Select all
#Entry for usbfs
none     /proc/bus/usb     usbfs      devgid=1000,devmode=664    0    0


Now mount shows

Code: Select all
mount | grep usbfs
usbfs on /proc/bus/usb type usbfs (rw,relatime,devgid=43,devmode=664)


The script still segfaults, run as a regular user and root user.

Re: Python script segfaults.

PostPosted: Jun 7th, '12, 20:56
by camorri
I have spent some time learning how to debug python scripts. It appears to me the failure is not in the script itself. I now believe the error is in libusb. Here is the error as reported to syslog.

Code: Select all
python[11039]: segfault at 48 ip 00007f2861b354f6 sp 00007fffe33052b0 error 4 in libusb-1.0.so.0.1.0[7f2861b2e000+f000]


Thoughts on what to do from here?

Re: Python script segfaults.

PostPosted: Jun 8th, '12, 21:27
by doktor5000

Re: Python script segfaults.

PostPosted: Jun 11th, '12, 09:47
by oldcodger
There seems to be a problem with libusb-1.0 which your script is using.
I have been wrestling with a similar problem. After a lot of googling and reading it turns out there is a bug in the library file required.
As yet I haven't found an answer.

Re: Python script segfaults.

PostPosted: Jun 11th, '12, 22:43
by doktor5000
camorri wrote:Further investigation on pyusb package shows in the readme:
2) At least one of the supported libraries (libusb 1.0, libusb 0.1 or OpenUSB)



As the script says it can support both which are available on Mageia, maybe just try to rule out one of those?