hn.today

C++26: The New Debugging Header

sandordargo.com50 points4 comments
Screenshot of C++26: The New Debugging Header

C++26 adds a new <debug> header (via P2546R5 and companion P2810R4, credited to René Ferdinand Rivera Morell) that standardizes a tiny, pragmatic debugging API: std::breakpoint() (unconditional), std::is_debugger_present() (query), and std::breakpoint_if_debugging() (convenience). All three are noexcept, freestanding and non-constexpr; breakpoint() typically emits an int3 on x86 and has implementation-defined behavior when no debugger is attached (often delivering SIGTRAP). The intent is to replace the myriad project-specific macros and platform checks that currently reinvent how to programmatically hit a breakpoint.

The standard leaves detection mechanisms implementation-defined - Windows implementations can call ::IsDebuggerPresent(), Linux ones can read /proc/self/status’s TracerPid - and makes std::is_debugger_present() replaceable so bare-metal or custom platforms can supply their own check. breakpoint_if_debugging() is simply if (std::is_debugger_present()) std::breakpoint(). Separating detection from the trap enables other debugger-aware behavior: extra diagnostics, disabling timeouts during stepping, switching to debugger-friendly allocators, or avoiding optimizations that impede debugging. This is a small, practical addition that codifies well-known platform knowledge and saves projects from repeating the same boilerplate.

Read on sandordargo.com4 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Programming

My C23 Conventions

My C23 Conventions

Abner Coimbre discusses modern C conventions in C23, emphasizing the use of maybe_unused to handle unused return values and improve code clarity. He also highlights issues with designated initializers not zero-initializing structs, leading to more careful struct initialization practices. (abner.page)

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.