I have a problem with a segfault in pyodbc and would like to debug it in Windows XP x86. However, the information online seems primarily Linux-centric. What is the best way to go about this?
4 Answers
So I was able to successfully resolve my issue by using Visual Studio 2008. I loosely followed the steps listed here -
http://www.velocityreviews.com/forums/t329214-debugging-python-extensions.html
And some tips on workarounds here -
Compiling python modules whith DEBUG defined on MSVC
Here is my version of the steps for anyone else who may encounter this problem.
In case you haven't already, be sure to setup the Python header and libs directories in VS
a. Go to Tools > Options > Projects and Solutions > VC++ Directories. Be sure to add your include and libs path to the Include and Library files' path, respectively. (e.g.
C:\Python27\include,C:\Python27\libs)Go to your Python include folder (once again, e.g.
C:\Python27\include) and editpyconfig.h. Comment out the line# define Py_DEBUGand save. Go to your libs folder (e.g.C:\Python27\libs) and make a copy ofpython27.lib. Name the copypython27_d.lib.Create a new project. Choose Win32 Project and name it the module name (in my case
pyodbc. Click Next then choose DLL for Application type and check Empty Project.In the Solution Explorer, right-click on Header Files and choose Add > Existing Item. Select all of the header files that you need. Do the same for Source Files.
Go to Project > Properties, then under Configuration Properties -
a. General - ensure that you are using the correct Character Set. For me it was
Use Multi-Byte Character Set. Python 3 probably needsUse Unicode Character Set.b. Debugging - enter the path to Python in the Command field. (e.g.
C:\Python27\python.exe). Then set Attach toYes.c. Linker > General - change the Output File to end in
.pydinstead of.dll.Ensure that your configuration is set to Debug. Go to Build > Build Solution.
Open a cmd and
cdinto the directory where yourpydfile was compiled. Start python from the cmd window. To attach debugger on this running python process, go back to Visual Studio and click the green play button to start debugging. You can also use Debugging -> Attach to Process... Now go back to Python and import your module. Play, test, and try to break it!
Debugging workflow with WinDbg
This workflow will create debugging information for a Release build, so you don't have to mess with the original include and library files of Python.
Download and install Debugging Tools for Windows
Get the symbol files for your Python version and extract them. For Python 2.7.3 this would be http://www.python.org/ftp/python/2.7.3/python-2.7.3-pdb.zip.
Modify
setup.pyto generate debugging files. You have to add'/Zi'toextra_compile_argsand'/DEBUG'toextra_link_args. Example:ext_modules = [Extension('pyuv', sources=['src/pyuv.c'], extra_compile_args=['/Zi'], extra_link_args=['/DEBUG']) ]Build the extension as always (
python setup.py ...).Start WinDbg and specify the Symbol Search Path (Ctrl + S).
C:\Path\To\Extension_pdb C:\Path\To\Extracted\python-2.7.3-pdb srv*;SRV*c:\tmp*http://msdl.microsoft.com/download/symbolsThe last line will download and cache required symbols for Windows modules.
Start the Python executable (Ctrl + E). You can directly execute a script or run in interactive mode.
Skip the initial breakpoint with "Go" (F5).
If there is a Segmentation fault the execution will break and you will see something like Access violation - code c0000005 (first chance) in the WinDbg console.
You can get detailed exception information by typing
!analyze -vin the WinDbg console and the current stack trace withkb. Here is an example of such an output.
You should be able to combine this approach with pyrospade's answer to debug with Visual Studio if you omit his second step and build the project with Release configuration.
A further tutorial for WinDbg could be found here.
Segfaults are especially mysterious, as there is no way to trap for them from your Python code, or even to get much stacktrace information on the C side of things. One thing that can give you at least a little more info is to use the Google breakpad C library to report a C stack trace when the segfault occurs.
You may want to try David Malcolm's tool CPyChecker which statically analyses C extensions for memory leaks and other bugs. The tool is documented here.
python-2.7.3-pdb.zipfromhttp://www.python.org/ftp/python/2.7.3/. Rebuild pyodbc with debug symbols using msvc9 compiler. Add/DEBUGtoldflags_sharedand/Zitocompile_optionsinPython27\Lib\distutils\msvc9compiler.py- cgohlke