Running and debugging#

Typing each time commands to the terminal to compile and run our program is not necessary. Many IDEs feature a shortcut like F5 which combines compilation and inspection of our program. The latter is a natural process of program development, because we engineers make errors and introduce so called bugs into our program.

debugging

the process of finding the root cause, workarounds, and possible fixes for bugs.

bug

a design defect in an engineered system

In this section we will install and configure extension CodeLLDB, which uses the LLDB debugger that is part of the clang project. Then we will create a debug configuration that will be used upon F5.

Installing CodeLLDB#

  1. Install CodeLLDB extension from the author vadimcn in the editor.

  2. Click on Restart Extensions

Creating a launch configuration#

Warning

If the following steps do not work, skip these and create launch.json by creating a new file and copy pasting the content at the end of this section.

Details here.

So let us add a configuration which will describe what happens if we press F5.

  1. Go back to main.c tab.

  2. Press F5. You should see a popup window stating Cannot start debugging because no launch configuration has been provided..

  3. Click OK. A launch.json will be created in your project folder under .vscode folder. It will contain the configuration documented here.

  4. configurations contains a configuration named Debug. Our executable program should be assigned to program.

    Now substitute the name of your program for <executable file>. If you cannot remember it, take a peek to the previous section where we compiled and run our program.

  5. We want to work in an edit, debug cycle, however our launch configuration is not aware of the fact that our program must be compiled before running and debugging. So add the following line to your configuration so that the default build task is executed before debug:

    "preLaunchTask": "${defaultBuildTask}",
    
  6. You can remove cwd (current working directory) line, because we will use the default setting.

After these changes, your launch.json should look like this without the comments on the top for Windows. Change a.exe to a.out for other operating systems.

{
    "configurations": [
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug",
            "program": "${workspaceFolder}/main",
            "args": [],
            "preLaunchTask": "${defaultBuildTask}",
        }
    ],
    "version": "0.2.0",
}

Debugging takes care of running our program. Our default build task builds our program without running. That is the reason why we specified ${defaultBuildTask}.

In the next section, we will try our configuration. You don’t have to save launch.json. Project files are typically saved when you press F5.

Starting a debug session#

  1. Click on the tab main.c.

  2. Press F5. Run and debug bar will be activated. Additionally, you should see some output in the Debug console below as follows:

    ...
    Process exited with code 0.
    

    And in the terminal the hello message.

    Troubleshooting

    If you get an error, e.g., ${defaultBuildTask} not found, check if you have CodeLLDB extension installed.

Now you can edit your code and run & debug it again and again in cycle.

Debugger can also inspect our code by placing red breakpoints, which we will introduce later. For now, we will use the debugging feature for a convenient way of compiling and running our code.

Appendix#