TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

How Does a C Debugger Work? (2014)

131 pointsby btashtonover 4 years ago

7 comments

userbinatorover 4 years ago
<i>It writes an invalid instruction at this location. What ever this instruction, it just has to be invalid.</i><p>On x86 at least, it is a valid instruction. INT3, or CC in hex. There are also the debug registers which implement breakpoints without modifying any code, although it&#x27;s limited to a maximum of 4 at once.<p>Characterising gdb as a &quot;C debugger&quot; is quite appropriate --- try to debug the Asm directly with it is an excruciating experience.
评论 #24815696 未加载
评论 #24817275 未加载
评论 #24815710 未加载
评论 #24816735 未加载
评论 #24815658 未加载
elvis70over 4 years ago
See also the &quot;Writing a Linux Debugger&quot; series of posts in which a source-level debugger is implemented: <a href="https:&#x2F;&#x2F;blog.tartanllama.xyz&#x2F;writing-a-linux-debugger-setup&#x2F;" rel="nofollow">https:&#x2F;&#x2F;blog.tartanllama.xyz&#x2F;writing-a-linux-debugger-setup&#x2F;</a>
woodruffwover 4 years ago
This is a fantastic summary of debugger implementation!<p>Another great one that actually walks through writing a basic debugger is Eli Bendersky&#x27;s series[1].<p>One nitpick:<p>&gt; It could, and that would work (that the way valgrind memory debugger works), but that would be too slow. Valgrind slows the application 1000x down, GDB doesn&#x27;t. That&#x27;s also the way virtual machines like Qemu work.<p>This is usecase-dependent: running a program until you hit a breakpoint will be significantly faster with `int 3`, but running a piece of instrumentation on every instruction (or branch, or basic block, or ...) will be significantly faster with Valgrind (or another dynamic binary instrumentation framework). This is because Valgrind and other DBI tools can rewrite the instruction stream to sidecar instrumentation into the same process, versus converting every instruction (or other program feature) into a sequence of expensive system calls.<p>[1]: <a href="https:&#x2F;&#x2F;eli.thegreenplace.net&#x2F;tag&#x2F;debuggers" rel="nofollow">https:&#x2F;&#x2F;eli.thegreenplace.net&#x2F;tag&#x2F;debuggers</a>
jbnover 4 years ago
One useful reference for this is <a href="https:&#x2F;&#x2F;www.cs.tufts.edu&#x2F;~nr&#x2F;pubs&#x2F;retargetable-abstract.html" rel="nofollow">https:&#x2F;&#x2F;www.cs.tufts.edu&#x2F;~nr&#x2F;pubs&#x2F;retargetable-abstract.html</a>
qlk1123over 4 years ago
It writes an invalid instruction at this location. What ever this instruction, it just has to be invalid.<p>RISC-V actually did this in a special instruction called ebreak. It can change the CPU privileged mode into Debug Mode.
评论 #24816740 未加载
Gunaxover 4 years ago
What about optimizations?<p>Isn&#x27;t it possible the compiler will re-order or combine statements differently than how they are written in source?
评论 #24817118 未加载
评论 #24816547 未加载
jonny383over 4 years ago
And here I was thinking this would be an article on writing printf() statements.