Search code examples
pythongdbtensorflowpdb

Debugging TensorFlow tests: pdb or gdb?


I am debugging decode_raw_op_test from TensorFlow. The test file is written in python however it executes code from underlying C++ files.

Using pdb, I could debug python test file however it doesn't recognize c++ file. Is there a way in which we can debug underlying c++ code?

(I tried using gdb on decode_raw_op_test but it gives "File not in executable format: File format not recognized")


Solution

  • Debugging a mixed Python and C++ program is tricky. You can use gdb to debug the C++ parts of TensorFlow, however. There are two main ways to do this:

    1. Run python under gdb, rather than the test script itself. Let's say that your test script is in bazel-bin/tensorflow/python/kernel_tests/decode_raw_op_test. You would run the following command:

      $ gdb python bazel-bin/tensorflow/python/kernel_tests/decode_raw_op_test
      (gdb) run
      

      Note that gdb does not have great support for debugging the Python parts of the code. I'd recommend narrowing down the test case that you run to a single, simple test, and setting a breakpoint on a TensorFlow C API method, such as TF_Run, which is the main entry point from Python into C++ in TensorFlow.

    2. Attach gdb to a running process. You can get the process ID of the Python test using ps and then run (where $PID is the process ID):

      $ gdb -p $PID
      

      You will probably need to arrange for your Python code to block so that there's time to attach. Calling the raw_input() function is an easy way to do this.