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.

Posts from blogs I like

Pikmin 3: No Fruit, No Problem - 0 Fruit in 2h24m

Want to watch this in your video player of choice? Take this: https://cdn.xeiaso.net/file/christine-static/video/2024/pikmin-3-0-fruit/2/index.m3u8 Recently I saw @TheBacklogs do a Pikmin 3 challenge run where they collected 0 fruit to beat the game. I thought I could do better, so I beat it in 2 hours 24 minutes. Full run without commentary: https://youtu.be/nHFIn74-mLk @TheBacklogs' run: https://youtu.be/eSEUdgVX7o4 My website: https://xeiaso.net My Mastodon: …

via Xe Iaso's blog May 04, 2024

Copyleft licenses are not “restrictive”

One may observe an axis, or a “spectrum”, along which free and open source software licenses can be organized, where one end is “permissive” and the other end is “copyleft”. It is important to acknowledge, however, that though copyleft can be found at the opposite end of an axis with respect to permissive, it is not synonymous with the linguistic antonym of permissive – that is, copyleft licenses are not “restrictive” by comparison with permissive licenses. Aside: Free software is not synonymous with copyle…

via Drew DeVault's blog April 19, 2024

Airfoil

The dream of soaring in the sky like a bird has captivated the human mind for ages. Although many failed, some eventually succeeded in achieving that goal. These days we take air transportation for granted, but the physics of flight can still be puzzling. In this article we’ll investigate what makes airplanes fly by looking at the forces generated by the flow of air around the aircraft’s wings. More specifically, we’ll focus on the cross section of those wings to reveal the shape of an airfoil – you can see…

via Bartosz Ciechanowski February 27, 2024