[go: up one dir, main page]

Skip to content

bronifty/c-debugging

Repository files navigation

LLDB Debugger Config for VS Code and Cursor Running in Tailscale Container

Local Dev

Hit ctrl + shift + p and select Reopen in Container. The .devcontainer config will refer to the Dockerfile.local, which is a copy of the Dockerfile but with the entrypoint commented out.

Tailscale Dev

This requires an oauth key passed into the .env file (rename sample.env to .env and fill in the key).

docker compose up
  • The bind mount (map to local file system of this repo) is in /workspaces/c-debugging inside the container you may need to switch to that directory once inside.

You can access the container on the tailnet. Currently, it won't work locally unless the entrypoint is commented out.

Language Support

  • c/c++ (you will need the lldb extension to debug; notes below)
  • python (python3 and pip3 are installed)
  • go (you may have to get into the ~/.bashrc file and insert the line export PATH="$PATH:/usr/local/go/bin:/go/bin")

:::info This will work for c and c++. You will need 2 ide extensions, one local (on your machine) and one remote (in the container). :::

How to use

  1. Have the Docker daemon running on your machine.
  2. Install the Dev Containers Extension locally.
  3. Jump into the repo root and hit cmd + shift + p and select Reopen in Container.
  4. Install the CodeLLDB Extension in the container (if not done automatically by the devcontainer.json config file in the root of this repo).
  5. Open any c or c++ file and go to the debugger window cmd + shift + d.
  6. Click on the green play button to run the launch configuration lldb.

Notes

C++ is a superset of C, just extern C functions in C++ files due to the way C++ handles overloading.

    extern "C" {
        void my_c_function();
    }

:::warning You will be prompted to install the C++ extension in the container, but it is not strictly necessary with this setup. :::

Debugging with LLDB

  • lldb means llvm debugger; it hooks into the compiler architecture of llvm which is the next generation of compiler for c/c++
  • clang++ uses llvm and is the compiler for c/c++
  • a c/c++ file needs to be compiled with the -g flag to enable debugger metadata, which is then used by lldb
  • the launch config does just what a terminal session would do but dynamically for the active file in the ide and it runs the build task (clang++ with -g flag) beforehand to enable debugging in the window for active file.

Config

  • .vscode/launch.json
// launch config
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "lldb",
      "type": "lldb",
      "request": "launch",
      "program": "${fileDirname}/${fileBasenameNoExtension}",
      "cwd": "${workspaceFolder}",
      "preLaunchTask": "clang++"
    }
  ]
}
  • .vscode/tasks.json
// build task
{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "clang++",
      "type": "shell",
      "command": "/usr/bin/clang++",
      "args": [
        "-std=c++17",
        "-g",
        "${file}",
        "-o",
        "${fileDirname}/${fileBasenameNoExtension}"
      ]
    }
  ]
}
  • Dockerfile
FROM ubuntu:22.04

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && apt-get install -y \
    build-essential \
    clang \
    gdb \
    cmake \
    git \
    curl \
    vim \
    lldb \
    python3 \
    python3-pip \
    && rm -rf /var/lib/apt/lists/*

RUN pip3 install --upgrade pip

RUN pip3 install matplotlib numpy

RUN update-alternatives --install /usr/bin/python python /usr/bin/python3 10

CMD ["bash"]
  • .devcontainer/devcontainer.json
// https://code.visualstudio.com/docs/devcontainers/containers#_create-a-devcontainerjson-file
{
  "name": "Existing Dockerfile",
  "build": {
    "context": "..",
    "dockerfile": "../Dockerfile"
  },
  "customizations": {
    "vscode": {
      "extensions": ["vadimcn.vscode-lldb"]
    }
  }
}

Bash Script Notes

dockerstop() {
    docker stop $(docker ps -aq)
    docker rm $(docker ps -aq)
    docker volume rm $(docker volume ls -q)
}
# get the shell type to get the correct shell config
echo $SHELL

# assuming bash and bashrc is the shell and config file
# add go to path
cat << 'EOF' >> ~/.bashrc
export PATH=$PATH:/usr/local/go/bin:/go/bin
EOF

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published