5 ms·
Just add more print instructions. And if that doesn't work, add even more print instructions! Add some instructions to print "**1**", "**2**", "**3**", ... to
by gus_massa 6y ago
Just add more print instructions. And if that doesn't work, add even more print instructions!
Add some instructions to print "**1**", "**2**", "**3**", ... to see how far the program goes, which branches it takes, print a few variables that look interesting. If that fails print the uninteresting variables, remove "**1**" and add some "**2a**", "**2b**", ... in between 2 and 3 ...
After a while, you discover the correct spot and the correct variable, and the error is trivial.
- rgoulter 6y agoI do this too; but it seems to me this is the case where using a debugging tool would probably save more time. Looking at the effort involved: here you have to spend the time to add a bunch of print statements throughout bits of code; then compile/run the program (repeating until you've found the right place). With a debugger: you'd have to take the time to set up the environment and learn how to use it (e.g. set breakpoints in parts where things would be interesting). But then the "look at interesting values" wherever isn't significant additional effort.
- scoutt 6y agoI am sorry, but this approach seems terrible. It seems the thing I do when I have to debug something I don't know, like PHP ("echo hello1", "echo hello2"). And that is just because my field is far from web development (I do embedded C), and I might be forced to do it like once every 5 years or so. I don't know if you program for a living, but I would be kind-of ashamed if I see a colleague debugging like this. It seems a desperate approach.
- danaris 6y agoThat's quite a judgement when you have no idea what kinds of tools the language the person you're responding to is using has. Frankly, even when I'm using a language that has a stellar GUI debugger, there are times when print debugging is vastly more effective. Why? Because I have a loop that iterates thousands of times, and crashes on some indeterminate but high iteration. A simple line-based breakpoint requires me to manually continue the code thousands of times, then take note of what state the variables were in the time just before it crashes. Even the ability to set a more complex variable-value-based breakpoint doesn't help that much when I don't know what the values are where it breaks. Printing out the loop variables every time lets me just check the last printout, and say, "Ah, it's when it hits i=37129", then follow that to the array value it corresponds to, with a substring in it that just happens to trip an edge case of one of the calculations. Or whatever.
- scoutt 6y agoI totally agree. I also use print message sometimes for the case you mention. Or for example when doing audio applications, to monitor the rate of buffers being filled under different loads. Generally high rate stuff, happening later in time. The difference I believe is that in both cases the problems are circumscript. It's not random "hello1", "hello2" messages to understand the program flow, guessing where the problem could be. That's what I called desperate. But it's true I don't know which language we are talking about.
- db48x 6y agoYou should give RR or Pernosco a try. They both make it easy to record the program, stop execution at the crash, and then step backwards to see what went wrong. But honestly, even an ordinary debugger would let you print the value of the iteration variable at the point of the crash, along with all the other variables. I think your example was much too simplified.
- danaris 6y agoThose tools sound cool, but they appear to be specific to a few languages on Linux, none of which has ever significantly applied to me (I've been a Mac user my whole life). As for the other, that's sometimes true, and sometimes not; depends on what GUI debugger you're using. To be honest, it's been long enough since I was regularly working on languages with a non-terrible GUI debugger that I don't recall off the top of my head whether theirs did allowed that or not. These days, I'm mostly working in PHP, and unless something's changed quite recently, the only GUI debugging tools available for it require using Eclipse or similar godawful monstrosities. I will gladly eschew the benefits of such tools, and suffer the slings and arrows of snooty and judgmental developers like yourself (which I already get in plenty just for staining my hands with PHP), if it means that I don't have to use those lumbering behemoths every time I go to write or debug some code.
- db48x 6y ago> Those tools sound cool, but they appear to be specific to a few languages on Linux, none of which has ever significantly applied to me (I've been a Mac user my whole life). Correct. The author’s premise is that if we didn’t neglect our tools so much, all debuggers for all languages on all platforms could be that cool.
- falcolas 6y ago> I would be kind-of ashamed if I see a colleague debugging like this Nice passive-aggressive attack there.
- scoutt 6y agoNow that I re-read it, it is harsh. I meant no personal attacks. Sorry. But it's what I would feel if I see a colleague (C) programmer doing that.
- AnimalMuppet 6y agoAs I said elsewhere in this thread, in embedded C you can't always set a breakpoint. If there are real time things going on, you won't meet the timing as soon as you hit the breakpoint. If there are multiple threads, stopping one of the threads can change everything, making other threads take a completely different path than they would have without the breakpoint. All you can get from it is, yes, this thread hit this breakpoint, and the local variables have these values. But (at least in many embedded situations) you can't continue from there to learn anything more from the next breakpoint. You have to start over.
- scoutt 6y agoI don’t follow you. Usually breakpoints are hardware (not conditional breakpoints), and the core halts completely. You can continue execution as if nothing happened. Often watchdogs and timers are aware of breakpoints and core halting. What would be the alternative? Semihosting? Using an uart? That screws the timing even more.
- AnimalMuppet 6y agoAh, I was thinking of a soft breakpoint, not a hard one. A hardware breakpoint solves the threading problem, but it doesn't stop the external world. If there are external events that your system responds to (and what embedded system doesn't?) then those events will either be missed, or else all queued up, when you resume. Shoving chars out a uart, in contrast, takes time, but considerably less time than a human hitting continue after a breakpoint. That can still mess up the timing. But a breakpoint absolutely blows up the timing - seconds worth of external events have happened. If your external events are coming from a test rig, and the test rig is aware of you hitting a breakpoint, then that problem goes away.
- dmitriid 6y agoOr, you know, just step through with a debugger once. Maybe twice (I use both)