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 Debugger Work?

102 pointsby wazari972over 10 years ago

12 comments

cpleaseover 10 years ago
Nice article, but it doesn&#x27;t quite deliver. It says, the trick is not &quot;black magic&quot;, but then defines debugging in terms of ptrace syscalls, describing the API a little bit, but without giving a clue as to how ptrace actually works. So, ptrace is essentially black magic.<p>And this is not really an explanation of &quot;how a debugger works,&quot; or even &quot;how gdb works.&quot; ptrace is just one of several debug targets for gdb. There are simulators, core files, various embedded monitors, VxWorks, Windows, gdb remote debug servers over various interfaces, and on and on. ptrace is irrelevant to other targets.
评论 #8654509 未加载
评论 #8656223 未加载
评论 #8654634 未加载
评论 #8655308 未加载
评论 #8654469 未加载
ChuckMcMover 10 years ago
One of the more interesting things about the ARM Cortex-M series is that debugging is &quot;built in&quot; to the CPU core on all licensed processors. No hacks required. Something that I&#x27;m sure x86 machines would have had, if transistors has been as cheap then as they are now. Of course early on Intel made even <i>more</i> margin on versions of the processor used for doing in circuit emulation by &#x27;bonding out&#x27; to an unused pad access to internal trace registers.
评论 #8655297 未加载
msvanover 10 years ago
I&#x27;m not too knowledgeable about this subject, but I&#x27;ve been interested in learning how native code debuggers work for a long time. One thing I wonder is, if the debugger inserts an invalid instruction or a hardware breakpoint instruction into the code at runtime, wouldn&#x27;t all of the in-memory code need to be reallocated and recalculated in order to make room for the new instruction and recalculate jump addresses? How is this handled?
评论 #8654087 未加载
lostpixelover 10 years ago
As someone who used to play with debugger implementations a bunch it&#x27;s nice to see some articles digging into this.<p>Only feedback I would give is to remove the shadow on your text, I had to manually disable the shadow before I was able to read :).
评论 #8654507 未加载
评论 #8653839 未加载
jbnover 10 years ago
Also relevant, and a good read to boot: <a href="http://www.cs.tufts.edu/~nr/pubs/retargetable-abstract.html" rel="nofollow">http:&#x2F;&#x2F;www.cs.tufts.edu&#x2F;~nr&#x2F;pubs&#x2F;retargetable-abstract.html</a>
agumonkeyover 10 years ago
Just `gdb gdb`. Might please infinite interpretation towers lovers around here.
esfandiaover 10 years ago
Based on the title of the article, I expected it to describe very general principles for writing debuggers, but it seems very specific to gdb. Are things similar in, say, Python or Java?
评论 #8655651 未加载
评论 #8654891 未加载
评论 #8656931 未加载
MichaelGGover 10 years ago
So how are the ptrace functions implemented? Is the &quot;hack&quot; of inserting invalid instructions used even for single stepping? (Though hardware breakpoints are probably easier?)
评论 #8654369 未加载
评论 #8655610 未加载
评论 #8654192 未加载
评论 #8654092 未加载
评论 #8656993 未加载
wazari972over 10 years ago
I&#x27;ve updated the article based on you comments, thanks :-) (title more focused, How is Ptrace implemented, What about systems without Ptrace)
omegoteover 10 years ago
By the way, it&#x27;d be seriously cool to find a good tutorial about gdb. I&#x27;ve been using it for years, but just the basic operations...
评论 #8654796 未加载
评论 #8655599 未加载
tcasover 10 years ago
Don&#x27;t modern processors support hardware breakpoints &#x2F; watchpoints?
评论 #8654051 未加载
评论 #8653816 未加载
pmalyninover 10 years ago
Didn&#x27;t read the article (I find the topic rather bland), but debugging on x86-64 is quite simple: you have your debugging registers (DR0-DR4) which set trigger addresses and conditions (execute, read, write) and then call a system interrupt when the condition is satisfied. This approach is limited to 3 breakpoints. Most moder debuggers do software breakpoints, that is when you set a break point for a particular line or instruction, the debuggrr replaces the first byte of the instruction with an int3 instruction (usually interrupt instructions are two byte wide, so technically int3 and &quot;int 3&quot; are different) but regardless the debugger slusually stores the actual instruction byte in a table to replace the int3 when it is actually hit. I suppose one could do this differently by causing a page fault (a simple bit switch from present to not present in the page table) and then monitoring the CR2 register to get the address of the executing code or the daya that is being accesed. One point I forgot to mention is that the x86 has hardware support for single-stepping instructions (a simple flag). But all of these methods require operating system support.