Search code examples
c++tensorflowvisual-studio-codegoogletestbazel

how to debug bazel c++ google test in visual studio code


I have Visual Studio Code, C++ bazel tests that I built using command like this

bazel test //tensorflow/lite/kernels:xxx_test --test_arg=gtest_filter=XXXTest -c dbg

Then I can debug it using gdb like this

gdb ./bazel-bin/tensorflow/lite/kernels/xxx_test.runfiles/org_tensorflow/tensorflow/lite/kernels/xxx_test

But is it another way to debug it properly in VS Code ? I installed plugin for Google Tests, but it does not see them and there is no gtest.exe.

Thanks!


Solution

  • The following steps work for me when using the Google Test Adapter plugin. Go to Debug -> Add Configuration... to add a new launch target to your launch.json file and fill in the program path under "program" (note that a relative path does not work for me here, so maybe you need to fill in the complete path, EDIT: As pointed out in the comments adding $(workspaceFolder) before the path helps):

    {
        // Use IntelliSense to learn about possible attributes.
        // Hover to view descriptions of existing attributes.
        // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
        "version": "0.2.0",
        "configurations": [
            {
                "name": "gtest",
                "type": "cppdbg",
                "request": "launch",
                "program": "(maybe insert complete path)/bazel-bin/tensorflow/lite/kernels/xxx_test.runfiles/org_tensorflow/tensorflow/lite/kernels/xxx_test",
                "args": [],
                "stopAtEntry": false,
                "cwd": "${workspaceFolder}",
                "environment": [],
                "externalConsole": false,
                "MIMode": "gdb",
                "setupCommands": [
                    {
                        "description": "Enable pretty-printing for gdb",
                        "text": "-enable-pretty-printing",
                        "ignoreFailures": true
                    }
                ],
                "preLaunchTask": "",
                "miDebuggerPath": "/usr/bin/gdb"
            }
        ]
    }
    

    Check if you can correctly debug your app, by setting a break point in your code and then pressing F5.

    If it works go to the Google Test Adapter Plugin and hover the mouse on the left side (where it normally shows the available tests) and in the bar where it says "GOOGLE TESTS" (with all the run and stop buttons) press on the double arrow <-> and set the hook on the above debug configuration. Now pressing on Run All should correctly work.

    Let me know if it works and if not, which steps don't work.