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
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"
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. :)
Related
everyone. I've been using python for a while with PyCharm, however I decided to go back on VsCode because I'm used to it.
I installed the python extension, specified the interpreter and compiled a basic file. Everything seems ok, except a strange path in the terminal which is annoying.
When I compile JavaScript it specifies the path rather simple:
C:\Program Files\nodejs\node.exe tests.js
However when I compile python it is a lot bigger:
PS D:\Projects\> ${env:DEBUGPY_LAUNCHER_PORT}='12345'; & 'C:\Users\user1\AppData\Local\Programs\Python
\Python38-32\python.exe' 'c:\Users\user1\.vscode\extensions\ms-python.python-2020.4.76186\pythonFiles
\lib\python\debugpy\no_wheels\debugpy\launcher' 'd:\Projects\app.py'
My question is, is this normal and is there a way to remove it or should I get comfortable with.
Two things. One, that's not compiling your code, it's running your code under the the debugger. Python doesn't really have a compiler step in the traditional sense.
Two, there isn't anything you can do about it. It's how the debugger launches.
I am currently interning at a place where they've asked me to make a standalone python program to do something (say X).
Now, that program is to be run by some commands sent by their proprietary software which is written in their proprietary language. Now the reason I'm saying proprietary so many times is because they aren't ready to take me anywhere near their code. I am just supposed to make a Python code that does X based on the input given by their software.
So is there a way I can make an API and wrap it around my code so as to let the software control it? Also I need to make the whole thing standalone (maybe an installer of some kind) so that they don't have to install Python and the accompanying modules (like opencv) just to run my script?
All I could get out of them was "there are dll files that will be calling your app and we want an executable"
Any programm can execute any other program (if it has the appropriate rights) so there is no real distinction between "python file" and "python executable" (that is because python is an interpreted language. The python source files and the "final python program" are "identical" (asuming cpython), in contrast to e.g. a C program where the source files and the executable are vastly different).
If you are on windows there is the additional problem that the user must have installed python to execute .py files. There are some ways to mitigate that problem - there are python libraries that "freeze" the python interpreter and your code into a single .exe file (from the comment by Bakuriu see e.g. freeze) . You could bundle the python interpreter with your code. You can just say your users to install python (if the amount of users is low that might be the good way).
"API" is just a fancy way of saying "this is how you communicate with my programm". This might be how you call a library (e.g. what functions a python module exports) or this might be an HTTP API or which command line arguments are passed or which protocoll over an TCP socket is spoken. Without knowing which API you are supposed to implement you cannot fulfill your job.
Without knowing further specifications (what inputdoes the other program give to yours, how does it call your programm?) it's very hard to say anything more helpful.
I am writing a python program that includes an extension module written in C. The extension module defines a function that is used in my program continually.
I have a memory leak somewhere in my program, but I don't know how to find it. I have tried
1) Installing Valgrind. I can't get this to work however since I have OSX Mavericks (10.9) and Valgrind only supports OXS 10.7 (and 10.8 somewhat unstably).
2) Using gdb. I can't seem to get gdb to run python scripts, as I'd need to set a breakpoint in the c function that gets called from my python code. I believe i need to install python-debuginfo in order to do that, but I have not been able to.
3) Using pdb. I had, however, no idea what to do to debug c code using pdb.
Is there a good tool to use to debug memory leaks in my program?
The debugger that currently ships with the Mac development tools is now lldb, it replaces gdb. The interface is somewhat similar to gdb. You can install gdb using Homebrew.
The way to debug C extensions is just like any other shared library. Debug the executable (python) that loads the shared library (your extension), and set breakpoints that would become valid after the executable loads the shared library.
Run lldb:
lldb /path/to/python -- your_python_script.py
set some breakpoints (replace with your relevant breakpoints)
(lldb) b some_source_file.c:4343
then run, and the debugger will stop at the breakpoint
(lldb) r
now you can debug the C extension normally.
I have PyOpenCL code with OpenCL C kernel code. I catch segmentation fault error when I run my app. How to debug such error with some debugger or some other development tool? I don't know what exactly to do to find out the problem. I have in mind option with printf or something but I want use more powerful stuff.
I believe that error in kernel code, so I want to debug kernel code firstly.
UPD. I'm on linux (Arch Linux, 3.6.11), python 2 or 3, PyOpenCl 2012.1
Kernel debugging is an implementation-dependent affair. On Linux, the best I've found is to use AMD's CL implementation on the CPU, compile the kernel with -g, and use gdb. They've got instructions on this in their programming guide, here:
AMD CL Documentation page
If you're using nvidia instead of ATI/AMD GPU, the OpenCL support in nvidia SDK is...less than desired.
Intel provides a CPU-based OpenCL SDK for their recent processors, see http://software.intel.com/en-us/vcsource/tools/opencl-sdk-2013 -- (to use the RPM packages they provide on Ubuntu, you need to run "fakeroot alien --to-deb" on each package, then "dpkg -i").
With that SDK, you need to add the "-g" and "-s filename" flags to the compiler options in build(). (If your kernel only exists as a string in your program, you can add code to save it to a file just before you run it.) Then try "gdb --args python-cmd", you can start debugging by typing "break mykernel", answer Y when asked if you want to wait for the "mykernel" symbol to be dynamically loaded, then type "run".
Once you have the debugger running manually typing the command, I suggest making an executable shell script to launch your favorite .py file with the debugger (which will also be a convenient place to add hackery to your application's launch, e.g. python -m unittest, PYTHONPATH, virtualenv, LD_LIBRARY_PATH, LD_PRELOAD, etc.).
I wouldn't jump into conclusions without fully testing your software suite. You are running the last released version of PyOpenCl. Chances are you are passing in something to the module that isn't being populated correctly and the backend module doesn't do the necessary error-checking before using something that isn't properly populated (impossible to really help you debug without any code being made available)
Have you tried using the python debugger to set different breakpoints (import pdb; pdb.set_trace()) right before different pyopencl calls to even see where in your code it is seg faulting? This should definitely be your first task. When you find out where it is seg faulting, you need to closely look at pyopencl examples/api to see why you've errored out.
Segmentation fault is usually because of a bad memory access in your kernel. There is a cool tool to detect bad memory accesses, similar to valgrind: https://github.com/jrprice/Oclgrind . In combination with some printfs in the kernel code it makes it quite a bit easier to localize the problem.
I am new to python and haven't been able to find out whether this is possible or not.
I am using the PyDev plugin under Eclipse, and basically all I want to find out is, is it possible to edit code whilst you're sitting at a breakpoint? I.e. Edit code whilst you're debugging.
It allows me to do this at present, but it seems to still be executing the line of code that previously existed before I made changes.
Also, are you able to drag program execution back like you can in VBA and C# for example?
If either of these are possible, how can I enable them?
PyDev supports this to some extend since version 1.4.8, see the change notes and the corresponding blog entry.
When you start a Python program, it will be compiled into bytecode (and possibly saved as .pyc file). That means you can change the source but since you don't "open" the source again, the change won't be picked up.
There are systems like TurboGears (a web framework) which detect these changes and restart themselves but that's probably going to confuse the debugger.
Going back in time also isn't possible currently since the bytecode interpreter would need support for this.
You can run arbitrary commands in the console during the breakpoint. For my needs, this typically achieves the same purpose as coding live, although I do wish it were as elegant as simply using the editor.