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.

  1. First of all, connect to your Linux machine using SmarTTY and create a C++ file (e.g. test.cpp) with the following contents:
    #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.

  2. In the same directory create a basic Python script that will call the module:
    import test
    test.hello("World")
  3. Now build the module, set PYTHONPATH to point at the current directory and run the script:
    g++ test.cpp -ggdb -o test.so -lpython2.7 -I/usr/include/python2.7 -shared
    export PYTHONPATH=$PYTHONPATH:.
    python main.py

    Observe how the Python process crashes without printing any details:segfault

  4. 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:qdwnd
  5. 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:01-qd

  6. Press “Debug”. VisualGDB will start debugging the script, automatically download it to the Windows machine and stop at the first line:02-py
  7. Press F5 to continue debugging. Visual Studio will report that it has received a SIGSEGV signal corresponding to a segmentation fault:
    03-sigsegv
  8. Press “Break” to see the code location that caused the crash:04-readonlyNote 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.
  9. Open the file on the Linux side with SmarTTY and uncomment the call to PyArg_ParseTuple():05-uncomment
  10. Build the module again:06-rebuild
  11. 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:07-qd2
  12. Observe how the script now exits without any errors and outputs the “Hello, World “message:08-output