{"id":13731020,"url":"https://github.com/scottt/debugbreak","last_synced_at":"2025-10-21T04:40:36.170Z","repository":{"id":37587636,"uuid":"2643157","full_name":"scottt/debugbreak","owner":"scottt","description":"break into the debugger programmatically","archived":false,"fork":false,"pushed_at":"2024-02-15T16:31:02.000Z","size":44,"stargazers_count":664,"open_issues_count":11,"forks_count":76,"subscribers_count":34,"default_branch":"master","last_synced_at":"2025-10-19T17:48:21.036Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/scottt.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"COPYING","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":"2011-10-25T12:22:21.000Z","updated_at":"2025-10-18T10:36:05.000Z","dependencies_parsed_at":"2024-11-14T21:33:04.316Z","dependency_job_id":"42a69578-d1bd-4b2f-bd6a-d5179530ec50","html_url":"https://github.com/scottt/debugbreak","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/scottt/debugbreak","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scottt%2Fdebugbreak","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scottt%2Fdebugbreak/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scottt%2Fdebugbreak/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scottt%2Fdebugbreak/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/scottt","download_url":"https://codeload.github.com/scottt/debugbreak/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scottt%2Fdebugbreak/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":280207158,"owners_count":26290613,"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","status":"online","status_checked_at":"2025-10-21T02:00:06.614Z","response_time":58,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2024-08-03T02:01:22.860Z","updated_at":"2025-10-21T04:40:36.142Z","avatar_url":"https://github.com/scottt.png","language":"Python","readme":"# Debug Break\n\n[debugbreak.h](https://github.com/scottt/debugbreak/blob/master/debugbreak.h) allows you to put breakpoints in your C/C++ code with a call to **debug_break()**:\n```C\n#include \u003cstdio.h\u003e\n#include \"debugbreak.h\"\n\nint main()\n{\n\tdebug_break(); /* will break into debugger */\n\tprintf(\"hello world\\n\");\n\treturn 0;\n}\n```\n* Include one header file and insert calls to `debug_break()` in the code where you wish to break into the debugger.\n* Supports GCC, Clang and MSVC.\n* Works well on ARM, AArch64, i686, x86-64, POWER and has a fallback code path for other architectures.\n* Works like the **DebugBreak()** fuction provided by [Windows](http://msdn.microsoft.com/en-us/library/ea9yy3ey.aspx) and [QNX](http://www.qnx.com/developers/docs/6.3.0SP3/neutrino/lib_ref/d/debugbreak.html).\n\n**License**: the very permissive [2-Clause BSD](https://github.com/scottt/debugbreak/blob/master/COPYING).\n\nKnown Problem: if continuing execution after a debugbreak breakpoint hit doesn't work (e.g. on ARM or POWER), see [HOW-TO-USE-DEBUGBREAK-GDB-PY.md](HOW-TO-USE-DEBUGBREAK-GDB-PY.md) for a workaround.\n\nImplementation Notes\n================================\n\nThe requirements for the **debug_break()** function are:\n* Act as a compiler code motion barrier\n* Don't cause the compiler optimizers to think the code following it can be removed\n* Trigger a software breakpoint hit when executed (e.g. **SIGTRAP** on Linux)\n* GDB commands like **continue**, **next**, **step**, **stepi** must work after a **debug_break()** hit\n\nIdeally, both GCC and Clang would provide a **__builtin_debugtrap()** that satisfies the above on all architectures and operating systems. Unfortunately, that is not the case (yet).\nGCC's [__builtin_trap()](http://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#index-g_t_005f_005fbuiltin_005ftrap-3278) causes the optimizers to think the code follwing can be removed ([test/trap.c](https://github.com/scottt/debugbreak/blob/master/test/trap.c)):\n```C\n#include \u003cstdio.h\u003e\n\nint main()\n{\n\t__builtin_trap();\n\tprintf(\"hello world\\n\");\n\treturn 0;\n}\n```\ncompiles to:\n```\nmain\n0x0000000000400390 \u003c+0\u003e:     0f 0b\tud2    \n```\nNotice how the call to `printf()` is not present in the assembly output. \n\nFurther, on i386 / x86-64 **__builtin_trap()** generates an **ud2** instruction which triggers **SIGILL** instead of **SIGTRAP**. This makes it necessary to change GDB's default behavior on **SIGILL** to not terminate the process being debugged:\n```\n(gdb) handle SIGILL stop nopass\n```\nEven after this, continuing execution in GDB doesn't work well on some GCC, GDB combinations. See [GCC Bugzilla 84595](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84595).\n\nOn ARM, **__builtin_trap()** generates a call to **abort()**, making it even less suitable.\n\n**debug_break()** generates an **int3** instruction on i386 / x86-64 ([test/break.c](https://github.com/scottt/debugbreak/blob/master/test/break.c)):\n```C\n#include \u003cstdio.h\u003e\n#include \"debugbreak.h\"\n   \nint main()\n{\n\tdebug_break();\n\tprintf(\"hello world\\n\");\n\treturn 0;\n}\n```\ncompiles to:\n```\nmain\n0x00000000004003d0 \u003c+0\u003e:     50\tpush   %rax\n0x00000000004003d1 \u003c+1\u003e:     cc\tint3   \n0x00000000004003d2 \u003c+2\u003e:     bf a0 05 40 00\tmov    $0x4005a0,%edi\n0x00000000004003d7 \u003c+7\u003e:     e8 d4 ff ff ff\tcallq  0x4003b0 \u003cputs@plt\u003e\n0x00000000004003dc \u003c+12\u003e:    31 c0\txor    %eax,%eax\n0x00000000004003de \u003c+14\u003e:    5a\tpop    %rdx\n0x00000000004003df \u003c+15\u003e:    c3\tretq   \n```\nwhich correctly trigges **SIGTRAP** and single-stepping in GDB after a **debug_break()** hit works well.\n\nClang / LLVM also has a **__builtin_trap()** that generates **ud2** but further provides **__builtin_debugtrap()** that generates **int3** on i386 / x86-64 ([original LLVM intrinsic](http://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20120507/142621.html), [further fixes](https://reviews.llvm.org/rL166300#96cef7d3), [Clang builtin support](https://reviews.llvm.org/rL166298)).\n\nOn ARM, **debug_break()** generates **.inst 0xe7f001f0** in ARM mode and **.inst 0xde01** in Thumb mode which correctly triggers **SIGTRAP** on Linux. Unfortunately, stepping in GDB after a **debug_break()** hit doesn't work and requires a workaround like:\n```\n(gdb) set $l = 2\n(gdb) tbreak *($pc + $l)\n(gdb) jump   *($pc + $l)\n(gdb) # Change $l from 2 to 4 for ARM mode\n```\nto jump over the instruction.\nA new GDB command, **debugbreak-step**, is defined in [debugbreak-gdb.py](https://github.com/scottt/debugbreak/blob/master/debugbreak-gdb.py) to automate the above. See [HOW-TO-USE-DEBUGBREAK-GDB-PY.md](HOW-TO-USE-DEBUGBREAK-GDB-PY.md) for sample usage.\n```\n$ arm-none-linux-gnueabi-gdb -x debugbreak-gdb.py test/break-c++\n\u003c...\u003e\n(gdb) run\nProgram received signal SIGTRAP, Trace/breakpoint trap.\nmain () at test/break-c++.cc:6\n6\t\tdebug_break();\n\n(gdb) debugbreak-step\n\n7\t\tstd::cout \u003c\u003c \"hello, world\\n\";\n```\n\nOn AArch64, **debug_break()** generates **.inst 0xd4200000**.\n\nSee table below for the behavior of **debug_break()** on other architecturs.\n\nBehavior on Different Architectures\n----------------\n\n| Architecture       | debug_break() |\n| -------------      | ------------- |\n| x86/x86-64         | `int3`  |\n| ARM mode, 32-bit   | `.inst 0xe7f001f0`  |\n| Thumb mode, 32-bit | `.inst 0xde01`  |\n| AArch64, ARMv8     | `.inst 0xd4200000` |\n| POWER              | `.4byte 0x7d821008` |\n| RISC-V             | `.4byte 0x00100073` |\n| MSVC compiler      | `__debugbreak` |\n| Apple compiler on AArch64     | `__builtin_trap()` |\n| Otherwise          | `raise(SIGTRAP)` |\n\n","funding_links":[],"categories":["C++"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscottt%2Fdebugbreak","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fscottt%2Fdebugbreak","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscottt%2Fdebugbreak/lists"}