Question

complete Python noob here (and rusty in C).

I am using a Mac with Lion OS. Trying to use NFCpy, which uses USBpy, which uses libUSB. libUSB is crashing due to a null pointer but I have no idea how to debug that since there are so many parts involved.

Right now I am using xcode to view the code highlighted but I run everything from bash. I can switch to Windows or Linux if this is going to be somehow easier with a different environment.

Any suggestions on how to debug this would be much appreciated ;-)

PS: It would be just fine if I could see the prints I put in C in the bash where I run the Python script

Was it helpful?

Solution

You should see your printf() you put in C in your terminal, something is already wrong here. Are you sure that you're using the latest compiled library? To be sure, instead of print, you can use use assert(0) (you need to include assert.h).

Anyway, you can debug your software using gdb:

gdb --args python yourfile.py

# type "run" to start the program
# if you put assert() in your code, gdb will stop at the assert, or you can put 
# manual breakpoint by using "b filename:lineno" before "run"

OTHER TIPS

Enable core dumps (ulimit -Sc unlimited) and crash the program to produce a core file. Examine the core file with gdb to learn more about the conditions leading up to the crash. Inspect the functions and local variables on the call stack for clues.

Or run the program under gdb to begin with and inspect the live process after it crashes and gdb intercepts the signal (SIGSEGV, SIGBUS, whatever).

Both of these approaches will be easier if you make sure all relevant native code (Python, libUSB, etc) have debugging symbols available.

Isolating the problem in a program which is as small as you can manage to make it, as Tio suggested, will also make this process easier.

PS: It would be just fine if I could see the prints I put in C in the bash where I run the Python script

You didn't mention anything about adding prints "in C" elsewhere in your question. Did you modify libUSB to add debugging prints? If so, did you rebuild it? What steps did you take to ensure that your new build would be used instead of the previously available libUSB? You may need to adjust your dylib-related environment variables to get the dynamic linker to prefer your version over the system version. If you did something else, explain what. :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top