Hacker News new | past | comments | ask | show | jobs | submit login

Heisenbugs are the worst.

Just last week I was on the verge of literally ripping my hair out trying to figure out a frustrating bug that never occurred when running in the debugger. After a lot of frustration I had an idea: rather than starting the application in the debugger, I’d attach later. When doing this, I began to see a huge amount of interesting first-chance exceptions (this was managed C# code) that clued me into the root cause of the issue and I was finally able to solve the problem.

I came to learn that an underlying library I was using had some code that ran on startup that actually detected if a debugger was attached and would turn off the problematic code path in this case! I came to learn that this was an intentional optimization the initialization of this code caused a bunch of noise during regular debugging (lots of first-chance exceptions that were ignorable). Unfortunately, this also meant that the interesting failure case I was running into was now completely hidden.




Production code that checks if a debugger is attached should be illegal.

Ideally there would be a way to get the debugger to lie to the process that a debugger is indeed not attached. Sure, maybe there's a reason today why you actually need to know a debugger isn't attached but at least give future-developers an escape hatch if they need it.


On Linux, you can "lie" to the process by running it with the LD_PRELOAD environment variable set to a library that hijacks ptrace(PTRACE_TRACEME, ...) to return 0. If the process is smart enough to check for LD_PRELOAD as well, you might be able to use something like seccomp to hijack the return value at the OS level.


You don't need LD_PRELOAD; the ptrace mechanism by itself can intercept system calls and modify them on the fly.


It's mostly an attempt at stopping someone attaching a debugger to an application you've shipped


Yep, and good luck using a debugger against race conditions (or any other time sensitive bugs).


The worst is when adding log lines changes timings and hides the race!


That can be where a debugger comes in handy! Instead of breakpoints it's (sometimes) faster to have the debugger log & continue. This doesn't always work, but often does with microcontrollers that have some dedicated debug/trace hardware.


Sounds like an easy bugix in that case, just leave the log in!




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: