Debugging C in VS Code

Knowing how to use a debugger for the tools that you use is one of the best investments you can make.

Debuggers help you explore the state of your program. They provide feedback much faster than other debugging methods, like print statements. They also allow you to see everything, whereas a print statement will only show what you choose to print. This can be very helpful when part of a program that you expect to be working correctly is actually misbehaving.

Advanced Operating Systems, the course that I’m currently taking for my Masters are Georgia Tech, requires that you spend a fair bit of time working in C. C is a fine language, but it is very cumbersome to debug programs without a debugger. Save yourself some frustration and set your debugging environment up before you start working on your program.

VS Code makes this very easy.

Setup

  1. Install the C/C++ extension

  2. Open up the “Run and Debug” pane

  3. Click the cog icon to open up the launch.json file which contains your debugging configuration

  4. Copy this into the launch.json file:

    {
      "name": "debug",
      "type": "cppdbg",
      "request": "launch",
      "program": "${workspaceFolder}/<path to your compiled binary>",
      "args": ["<your program arguments>"],
      "stopAtEntry": false,
      "cwd": "${fileDirname}",
      "environment": [],
      "externalConsole": false,
      "MIMode": "gdb",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        },
        {
          "description": "Set Disassembly Flavor to Intel",
          "text": "-gdb-set disassembly-flavor intel",
          "ignoreFailures": true
        }
      ]
    }
    
  5. Save the file and click on “Start Debugging”

VS Code will launch your binary and attach a debuggger. You can do all of the usual debugger things like set breakpoints and inspect program state.

Recent posts from blogs that I like

The case of the missing ordinal 380

Untangling the error message and developing a theory. The post The case of the missing ordinal 380 appeared first on The Old New Thing.

via The Old New Thing

"No way to prevent this" say users of only language where this regularly happens

In the hours following the release of CVE-2024-38063 for the project Microsoft Windows, site reliability workers and systems administrators scrambled to desperately rebuild and patch all their systems to fix a vulnerability where a specially crafted IPv6 packet can result in remote code execution.....

via Xe Iaso

Thinking about perceptiveness

links

via Henrik Karlsson