Using Python Quick Debug to debug external Python scripts
This tutorial shows how to quickly debug Python scripts and C++ modules built outside VisualGDB without creating a project for them. We will create a simple Python C++ module on a Linux machine via SSH and show how to quickly debug it with VisualGDB.
Before you begin, install VisualGDB 5.1 or later.
- First of all, connect to your Linux machine using SmarTTY and create a C++ file (e.g. test.cpp) with the following contents:
12345678910111213141516171819202122232425262728293031323334#include <Python.h>#include <stdio.h>static PyObject *HelloMethod(PyObject *self, PyObject *args){const char *pName = NULL;//if (!PyArg_ParseTuple(args, "s", &pName))//return NULL;if (pName[0])printf("Hello, %s\n", pName);return Py_BuildValue("i", 1);}static PyMethodDef testMethods[] ={{"hello",HelloMethod,METH_VARARGS,"Displays a \"Hello\" message."},{ NULL, NULL, 0, NULL } /* End of list */};PyMODINIT_FUNC inittest(void){PyObject *m = Py_InitModule("test", testMethods);if (!m)return;}
This defines a very basic Python module called “test” with a single method called “hello” that will crash the program as we commented out the actual parsing of the input arguments and are trying to dereference a NULL pointer. - In the same directory create a basic Python script that will call the module:
12import testtest.hello("World") - Now build the module, set PYTHONPATH to point at the current directory and run the script:
123g++ test.cpp -ggdb -o test.so -lpython2.7 -I/usr/include/python2.7 -sharedexport PYTHONPATH=$PYTHONPATH:.python main.py
Observe how the Python process crashes without printing any details: - Now we will use Visual Studio to quickly diagnose the crash without creating any projects. Start Visual Studio and select Debug->Quick Debug with GDB:
- In the “Quick Debug” window select “Python Script” -> ” Linux System” and enter the following settings:
- Debugged script: the full path to the Python script
- Working directory: the directory with the script and the .so module causing the crash
- Additional environment: PYTHONPATH=%PYTHONPATH%:.
- Python executable – python
Save the preset for future use by checking a checkbox at the bottom of the window:
- Press “Debug”. VisualGDB will start debugging the script, automatically download it to the Windows machine and stop at the first line:
- Press F5 to continue debugging. Visual Studio will report that it has received a SIGSEGV signal corresponding to a segmentation fault:
- Press “Break” to see the code location that caused the crash:
Note that the source file will be read-only and you won’t be able to modify it as it’s not a part of a VisualGDB project and VisualGDB does not how how and when to upload it.
- Open the file on the Linux side with SmarTTY and uncomment the call to PyArg_ParseTuple():
- Build the module again:
- Now if you select Debug->Quick Debug with GDB, VisualGDB will automatically load the previously saved preset, so you won’t need to re-enter anything. Simply click “Debug” to start debugging:
- Observe how the script now exits without any errors and outputs the “Hello, World “message: