ODBC and Python
Just like any other piece of software we can make use of debuggers to step through our application code and see what is happening with ODBC. To do this with
Python you should be running a version with debug symbols included. You can do this via:
git clone email@example.com:python/cpython.git mkdir debug cd debug ../configure --with-pydebug make make test
Additionally you will want to clone
pyodbc so that we can make use of symbols.
git clone firstname.lastname@example.org:mkleehammer/pyodbc.git CFLAGS='-Wall -O0 -g' python setup.py build
Finally you'll need some code and a database to interact with. If you want I have an example repo which uses docker to start
MSSQL. It also contains some
python example code and
pyodbc in the repo for debugging.
One final note, if you wish to explore code all the way into the driver manager and/or driver you will need a debug version of each. For Mac and Linux you can do this with
unixodbc found here or here and specify debug with
make similar to CPython above. For a debug driver build checkout Postgres psqlodbc.
I'm writing this on OSX, but the concepts are the same regardless of platform. On OSX you can use LLDB or GDB (I used LLDB as a learning exercise), on Linux GDB is probably your go to and on Windows you can use WinGDB or the debugger built into Visual Studio for C/C++.
From the command line start your debugger, or if using GDB/LLDB call your tool with the
-f flag specifying you want to load a file and call Python with your debugger so the Python interpretter will run the file inside your debugger.
lldb -f python -- -m pdb main.py
From here you can execute the application, use normal step, thread and frame functions to inspect the stack at different steps or get additional dump file information. Some
breakpoints I found interesting can be set with:
breakpoint set --file connection.cpp --line 232 breakpoint set --file connection.cpp --line 52 breakpoint set --file cursor.cpp --line 1100 breakpoint set --file getdata.cpp --line 776 run
In case it is helpful you can find an
gdb map here.