I wonder, am I doing something wrong? Is there something I'm missing? Ironically, my colleagues are always the ones to come to me when they can't debug something.
Debugger or print? That is the question
1–10 of 17 posts
Re: Debugger or print? That is the question
#2Imagine you’re working on a bug that’s extremely hard to reproduce (say, a race condition), takes a lot of time to reproduce (maybe at the bottom of a long running function), or is somewhere you can’t easily access (a remote device). Your print statement shows that $foo is -1, which doesn’t make sense. Better check what $bar is. So you now have to rerun the program, only to see $bar prints “???????” because for some reason it’s bytes but you’re printing it as UTF8. So now you have to modify and rerun your program to print out the hex values to figure out if there’s any meaning to them.
Congrats, you spent an hour getting three pieces of information.
If you just set a breakpoint, you can go for lunch and come back to a paused program where you can check the state, display information and data structures in whatever way you’d like, and proceed one step at a time to see what happens at each line of your code.
Re: Debugger or print? That is the question
#3Re: Debugger or print? That is the question
#4That said there are certain things that confound debuggers or require technical tricks (like the method for debugging Java/C apps with both jdb and gdb which requires turning off the segfault trap). If the key to solving the problem involves understanding the sequence of events, or where the problem turns up when the breakpoint has been hit for the 74532th time prints win too.
Do you use a language with a logging facility? Learn how to use it and improve your skills.
In Java I use the debugger and unit tests a bit like I use the REPL in Python, that is I can code up an experiment in a unit test and rapidly cycle and experiment, and the great thing is I have a unit test that I can check in when I am done.
Re: Debugger or print? That is the question
#5I could see a strong use for things like multithreading, profiling, seeing a list of variables/values that are in-scope at a breakpoint. The more complex the software, the more useful...
The last time I needed a software debugger, a tool I was using would communicate with another by sending its PID as an ID. Problem? The tool thought the PID fit in a 16-bit, so it was sending a totally bogus number. I probably would have found this eventually, but with the debugger, it was as easy as "Why is the sent PID 39283 instead of 170355?" This only occurs after Windows has been on for several weeks and enough conhost/svchost/wslhost processes push new PIDs above 65535.
Humorously, such a bug goes away when you "turn the PC off and on again"
-----
Hardware debuggers (for microcontrollers) often have a whole host of benefits that are immediately obvious once you involve any physical I/O. Also, they're vital for things like event logging (for Arm, PMU events flag for cache misses, branch events, operation stalls, and much more) or seeing what's happening with an off-chip device, peripheral, other cores, anything to do with watchdogs, etc.
Writing something like impact failure handlers for a drive head controller for a hard drive, you'll want to be able to set an external trigger as soon as the failure is initiated, connect that to your debugger via one of its trigger-detecting inputs, and then you can capture the real-time data and execution traces and move back to the exact moment the failure occurred, how your variables changed, and what exactly the failure handler did to ensure the drive head attempted to park as soon as possible. In short, this is not printf-able, but it is also an extreme example.
Re: Debugger or print? That is the question
#6Learn how to use a debugger. It sounds like the code base you’re working on a small enough that print statements are fine, but that won’t always be the case. Imagine you’re working on a bug that’s extremely hard to reproduce (say, a race condition), takes a lot of time to reproduce (maybe at the bottom of a long running function), or is somewhere you can’t easily access (a remote device). Your print statement shows t…
Perhaps this is why I've never needed a debugger: prevention is the best cure.
Re: Debugger or print? That is the question
#7Re: Debugger or print? That is the question
#8I've interviewed folk who say they don't use debuggers. They usually don't get the job.
Re: Debugger or print? That is the question
#9I'm assuming you mean a software-only debugger, and I mostly feel the same way as you. The process of starting the debugger, setting breakpoints, etc. feels a lot like going to the doctor when you're sick rather than self-diagnosing, which works great 90% of the time and doesn't involve getting into the car and lots of waiting. I could see a strong use for things like multithreading, profiling, seeing a list of varia…