{"id":20435932,"url":"https://github.com/andyneff/hello-world-gdb","last_synced_at":"2025-04-12T21:36:08.814Z","repository":{"id":90969524,"uuid":"85001784","full_name":"andyneff/hello-world-gdb","owner":"andyneff","description":"Simple hello world program for debugging with gdb","archived":false,"fork":false,"pushed_at":"2024-09-13T22:51:23.000Z","size":8,"stargazers_count":33,"open_issues_count":1,"forks_count":9,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-26T15:48:07.268Z","etag":null,"topics":["debugger","docker","gdb","vscode"],"latest_commit_sha":null,"homepage":null,"language":"Dockerfile","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/andyneff.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-03-14T22:05:40.000Z","updated_at":"2024-10-28T10:44:55.000Z","dependencies_parsed_at":null,"dependency_job_id":"42dee3d4-f24d-4689-be48-445693b807dc","html_url":"https://github.com/andyneff/hello-world-gdb","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andyneff%2Fhello-world-gdb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andyneff%2Fhello-world-gdb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andyneff%2Fhello-world-gdb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andyneff%2Fhello-world-gdb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/andyneff","download_url":"https://codeload.github.com/andyneff/hello-world-gdb/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248637201,"owners_count":21137530,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["debugger","docker","gdb","vscode"],"created_at":"2024-11-15T08:38:29.955Z","updated_at":"2025-04-12T21:36:08.786Z","avatar_url":"https://github.com/andyneff.png","language":"Dockerfile","funding_links":[],"categories":[],"sub_categories":[],"readme":"Simple counter program to demonstrate gdb in docker\n\n# Running the docker\n\n```\ngit clone git@github.com:andyneff/hello-world-gdb.git .\ndocker run -it --rm --privileged -v `pwd`:/src:ro --name hello_gdb andyneff/hello-world-gdb\n```\n\n# Launch Debugging\n\nMost debuggers are not going to support this gracefully, so the simplest thing to do is to\nstart the docker yourself, and compile the binary, and then launch via `docker exec`\n\n\n## Start process\n\n### Use docker\n\n#### Simple\n\n#### Allow you to edit hello.c\n\n```\ndocker run -it --rm --privileged -v `pwd`:/src:ro --name hello_gdb andyneff/hello-world-gdb:debian # Debian\ndocker run -it --rm --privileged -v `pwd`:/src:ro --name hello_gdb andyneff/hello-world-gdb:ubuntu # Ubuntu 22.04\ndocker run -it --rm --privileged -v `pwd`:/src:ro --name hello_gdb andyneff/hello-world-gdb:alpine # Alpine\n```\n\n### Use docker compose\n\n```\ndocker compose run --rm --name hello_gdb debian # Debian\ndocker compose run --rm --name hello_gdb ubuntu # Ubuntu 22.04\ndocker compose run --rm --name hello_gdb alpine # Alpine\n```\n\nIt should start printing out the container pid, usually around 12\n\nAttach with your *favorite* debugger.\n\n### Podman\n\nPodman should work the same way, but is untested. Uou just need to replace the words `docker` with `podman`.\n\n### Using singularity/apptainer\n\nSingularity doesn't instance containers in the exact same way as docker. There is no `singularity container ps` or an `exec` that runs in the same container.\n\nI do not know how you would make this work currently. Neither `run`/`exec` nor `instance start`/`instance run` give a way to run a _second_ executable in the container, which is crucial for this. The pid that will be reported in a normal `singularity run` command will print out the real pid, which if you can get GDB to attach to, but I can't figure out how to get VSCode to not freeze.\n\n## Using VSCode - With Microsoft Remote Extension\n\nAfter this repo was created, Microsoft continued to develop the Remote Extension to be easier to use. It is actually a smoother process, but it might not always fit in with your development pattern. So pick what's best for you.\n\n## Using VSCode - Without Microsoft Remote Extension (The original method)\n\nThe included `launch.json` will automatically allow you to attach the VSCode using the \"Microsoft C/C++ Extension\"\n\n1. Click the 🪲▶️ button on the Primary Sidebar on the left for the debugger extension.\n2. Select \"C++ Attach in hello_gdb container\" on the left.\n3. Press the ▶️ button to attach.\n4. Vscode will list the PIDs in the container, and you need to pick the one for the c program (Which is why I have it print it's PID every second). It should be trivial to be able to determine this without that.\n5. Press the ⏸️ button when you would like to start stepping through the process\n6. Hover over the `x` variable, and you should see the value\n\n## Attaching in other debuggers\n\nIf your debugger supports specifying the `gdb` executable, you can try pointing it to\n- `docker exec -i {container name} /usr/bin/gdb` as a wrapper\n- or `docker exec -i {container name} bash -c '/usr/bin/gdb ${1} /usr/bin/gdb` if the\n  debugger sends multiple arguments as one single argument.\n\nHowever if your debugger only supports \"one\" argument (with no spaces), you will have to save this to a script:\n\n```bash\n#!/usr/bin/env bash\n\ndocker exec -it hello_gdb /usr/bin/gdb \"${@}\"\n```\n\nThe clear downside to this is hard coding the container name.\n\n# FAQ\n\n## How does this work?\n\nMagic\n\n## Why --privileged?\n\n`gdb` needs ptrace permissions. Getting these in a docker varies from OS to OS.\nThe easiest thing to do is just give it all permissions. However, if you want the\nmost secure option, you need to figure out what is right for your operating systems,\nfor example, on Ubuntu with apparmor you need:\n\n- `--security-opt=seccomp:unconfined` : needed for --attach pid#\n- `--security-opt apparmor:unconfined` : needed for --multi (not currently used)\n- `--cap-add SYS_PTRACE` : needed for ptrace\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandyneff%2Fhello-world-gdb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandyneff%2Fhello-world-gdb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandyneff%2Fhello-world-gdb/lists"}