New & Revolutionary Debugging Techniques? 351
An anonymous reader writes "It seems that people are still using print statements to debug programs (Brian Kernighan does!).
Besides the ol' traditional debugger, do you know any new debugger that has a revolutionary way to help us inspect the data? (don't answer it with ddd, or any other debugger that got fancy data display), what I mean is a new revolutionary way. I have only found one answer.
It seems that Relative Debugging is quite neat and cool."
Exceptions (Score:3, Interesting)
Re:Exceptions (Score:3, Insightful)
Better emphasis (Score:3, Insightful)
Re:Better emphasis (Score:3, Interesting)
This class of bugs is significantly less painful than the little known 'shrodinbug' which is reported by testers and/or users, and cannot be reproduced in the prescence of a qualified observer or logged in any usable way.
The question is whether there is a 'planck constant' associated with debugging any sufficiently complex algorithmic function that constrains the ability of the coder to localize the bug while specifing its effects.
A question for the theorists amon
Re:Exceptions (Score:3, Insightful)
Java Exceptions *were* a revolution in debugging.
Because everyone knows that Java invented exception handling...
Re:Exceptions (Score:4, Informative)
If you are into dynamic analysis and recovery of exceptions -- that is, self-healing software --, that is a very powerful tool.
Re:Exceptions (Score:4, Informative)
Java invented the dynamic analysis and handling of stack traces, not just exceptions.
Where is your evidence that Java "invented" this? I have seen several other languages that are at least as old as Java that contain this feature, so some facts wouldn't go astray...
Re:Exceptions (Score:3, Interesting)
Re:Exceptions (Score:3, Informative)
See this post [slashdot.org] for a concrete example of such a language. It would be nice to see some evidence of a) when Java grew these features, and b) that it was the first language to have such features.
this implies that the language must have native multithread capabilities
Huh? What does threading have to do with exception handling? The two are almost completely unrelated, and the presence of one feature in a language in no way requires nor implies the presence of the other.
Re:Exceptions (Score:3, Informative)
I'd like to reinforce this statement. The only reason you would need multithreading is if you set up a watchdog timer to anticipate an infinite/semi-infinite loop state. Exceptions are almost exaclty like interrupt vectors. You set up a handler, it gets stored in a table, and if needed, it's called. In fact,
Re:Exceptions (Score:5, Informative)
How much has been forgotten. Time and time again I hear people claiming Java invented something when it was just the place they first saw it compared to programming in C or TurboPascal or whatever. Java does have some ideas it popularized -- but they are things like interfaces. Much of its class design like for Swing was taken from ParcPlace Smatallk's VisualWorks. Hotspot profiling came from Smalltalk. MVC came from Smalltalk. etc. etc. Between Forth, Smalltalk, and Lisp (and a few other languages and libraries) most of the innovations people see now were invented a long time ago. VMs came from Smalltalk and IBM mainframes (first) and Pascal and Forth. Another example -- XML is a stupid version of Lisp s-expressions. And so it goes...
Welcome to the 70s! (Score:3, Informative)
Re:Exceptions (Score:2)
Re:Exceptions (Score:3, Informative)
doSomeFunc()
Now you know that doSomeFunc() should be correct, and shouldn't have any errors. But you might have missed something - perhaps a divide by zero, or something.
So you do:
try {
doSomeFunc();
} catch (Exception e) {
}
where you try your best to handle the error gracefully (tell the user, disable the button, contact admin team, suggest a work around, etc)
Re:Exceptions (Score:3, Informative)
Java invented the dynamic analysis and handling of stack traces, not just exceptions.
Python has the same feature and Python is older than Java. It would take some effort to prove that Python had introspection of stack traces before Java did, but it seems quite likely to me. And it seems even more likely that some variant of Lisp had it long before Python.
Re:Exceptions (Score:3, Informative)
Re: Old news (Score:5, Interesting)
Besides, nontrivial bugs don't result in stack traces or crashes. They result in infrequent, hard-to-spot, anomoalies in the output. No amount of Java stack traces will help you find them.
Re: Old news (Score:3, Interesting)
I second that. I currently have a piece of software that runs as a daemon. It silently crashes about once a week. Tell me a way of debugging it that doesn't take months, and I'll be happy. But until then, I'll have to add debugging statements and triple-check each line of code, run it again and wait another week or so.
living under a rock? (Score:3, Informative)
Only if you have been living under a rock. Most languages and compilers other than C and C++ have been doing that forever. Even C and C++ allowed you to get a complete backtrace and inspect the complete program state from a core file (software bloat has made more and more people turn off that feature, however).
Re:Exceptions (Score:2)
Re:Exceptions (Score:3, Funny)
Re:Exceptions (Score:2)
"Get on that teletext!"
Re:Exceptions (Score:2)
Re:Exceptions (Score:4, Interesting)
Just as a historical note, the APL system that I used in 1975 provided this capability. When an exception occurred, the interpreter halted program execution, identified the problem and the source line, and provided access to the stack info on how (functions and line numbers) you had gotten there. You also had the ability to examine any variable that was currently in scope, and could change values and resume execution. Given the cryptic nature of the language, you needed all the debugging help you could get. Still, for certain types of numerical problems, you could get a lot of effective code written in a very short period of time.
Avoid debugging (Score:5, Insightful)
Instead concentrate on building software in many small incremental steps so that problems are caught quickly, and on separation of design so that dependencies are rare.
If you can't find a problem, leave it and do something else.
Otherwise, print statements, yes, that's about the right level to debug at.
Which is nice... (Score:5, Interesting)
I've got this thing with OpenSSL, Qt and my code right now. On a one-time run, it works fine. When I put it into a program and try to loop it, it crashes on some mysterious and horrible error, sometimes on 2nd pass, sometimes 3rd, 4th pass or more.
All I'm getting from traceback logs are some wierd memory allocation errors in different places, e.g. in Qt code that *never* crashes if I replace the OpenSSL code with dummy code, but has nothing to do with each other. Or in OpenSSL itself, which I hardly think is their fault, if it was that buggy noone would use it. And only if this is put together and looped. Each taken apart, they work perfectly.
Kjella
Re:Which is nice... (Score:3, Interesting)
Re:Avoid debugging (Score:2)
Re:Avoid debugging (Score:5, Insightful)
Inserting printf statements into the code is probably not logging - usually if you are debugging they are destined for removal anyway. I use a logging system that shows the asynchronous, high-level overview of events being dispatched and then can use the debugger to zero in on the problem very quickly without recompiliation. In addition if a test machine screws up I can remotely debug it.
If you want to throw out debugging because Linus isn't a fan of it, be my guest. But I'm not a fan of wasting time, and injecting print statements into the code plus recompiling is a waste of time and ultimately accomplishes close to the same thing as debugging. Any decent IDE will let you slap a breakpoint down and execute to that point quickly. But I assume someone will come along and tell me that IDEs are for the weak as well.
Re:Avoid debugging (Score:4, Insightful)
The tricky part about IDE/interactive debugging is understanding the behavior of loops, for instance. Sure you can put a breakpoint in the loop, and check things everytime, but you quickly find out that the first 99 times are fine, and somewhere after 100 you get into trouble, but you don't quite know where, because after the loop, everything is total chaos. So you have to switch gears; put in some watch condition that still traps too often (because if you knew exactly what to watch for, you would know what the bug was, and would just fix it), and hope that things went wrong, but left enough evidence, when it traps.
Whereas print statements let you combine the best of both worlds: expose the data you care about (what you would examine at breakpoints), but the ability to scan through the text result to find the particular conditions that cause the problem (what you could potentially get from watch conditions).
Re:Avoid debugging (Score:4, Insightful)
Another advantage of printfs or adding code to the app is that most languages are more powerful than the UIs of interactive debuggers; even the best inspectors make it hard to filter out large arrays to find the problem in element 1085. But I can add a little helper function to scan through the array in code and find exactly what I want. In Lisp, the full language is available in the debugger, so the debugging and coding are hard to distinguish.
Sometimes the tools you write to make the code are the same tools that are useful for debugging, whether you plan it that way or not.
Different strokes for different folks---we're all fighting the same enemy: the bugs.
There is something weird about debugging, however, which I can't quite put my finger on. Powerful language features have a return on investment which has a longer time to compound. You can attack bigger problems by understanding the language better, so spending time to understand the language pays off.
Powerful debugger features don't really have time to compound. Sure, they may save you 50% of your time tracking down a particular bug, but only if you recognize that the bug you have is solved with that tool.If you get a lot of practice using that tool, however, you'll tend to stop making the kind of specific mistake that makes the tool valuable.
Before you know how to use a language feature, you can write toy examples until you can feel comfortable. It's hard to practice with a debugger; how do you make toy mistakes---make a mistake deliberately and forget what mistake you made?
With hardware you have no choice (Score:5, Informative)
The neatest debugging tricks I've seen so far are those logging all inputs and returns from the OS level. Since you can replay them you can rerun the app to an earlier point and investigate - in effect you can run it backwards from a bug to see how it got there.
Valgrind (Score:5, Interesting)
Re:Valgrind (Score:4, Informative)
Valgrind is possibly the most useful debugging tool I've found lately. It's especially great for tracking down slippery memory bugs -- you know, the type that are virtually impossible to find using most debugging tools.
For people who haven't used it, what it basically does is recompile your program to target a simulated x86 CPU. It can detect branches that depend on uninitialized values, writes through a freed pointer, and a whole slew of other nasties that are difficult or impossible to detect with other tools.
Daniel
Re:Valgrind (Score:5, Informative)
More of the same (Score:5, Interesting)
All you are doing is replacing human eyes with a computer at the first "filter" process. Instead of having to compare a bunch of values and look for the errors, let the machine point them out to you - grep anyone?
I see nothing reolutionary about this. You still have the DUT making "assertions" - duuuuh can you say "print?"
Print statements work fine for me, too (Score:5, Interesting)
But bear in mind that almost all of my work these days are in environments where the bugs that traditional debuggers help you find are pretty much impossible to make in the first place (Python, Java, etc.). Instead of tracing data structures through bits of memory and navigating stack frames, you just focus on the application itself. It's kind of refreshing.
Re:Print statements work fine for me, too (Score:5, Insightful)
When the next poor programmer comes along, trying to fix/find a bug in that code, he a) can #ifdef the prints back on and quickly get debugging output about the important events taking place in his run, and b) read the code and see where the hairy bits are, because they tend to be the sections most heavily littered with debugging print calls.
Fancy debugger IDEs just don't support this preservation of institutional knowledge.
Re:Print statements work fine for me, too (Score:2, Insightful)
1. When reading the code for logic, the print statements can be distracting and take up valuable vertical screen realestate. An algorithm without printfs can usually fit on a single screen. With printfs it may spill over two pages. That can make debugging harder if you need to understand what you're looking at at a conceptual level.
2. Almost invariably I find that a previous person's printfs are a
Re:Print statements work fine for me, too (Score:3, Insightful)
For one thing my editor doesn't support this, and not all editors do. For another thing it depends on the folding implementation in the editor as to how distracting this is.
> Redirect stdout/stderr to a file. Besides, this sounds like a straw man. There's nothing stopping you from having differently detailed level of debugging output.
So now you want me to sit down and create sed/awk/grep/perl scripts to filter out stuff I don't want. No thanks. I'd rather jus
Re:Print statements work fine for me, too (Score:3, Informative)
h
I prefer a debug print func (Score:3, Interesting)
Somewhere, you have a list of what the various debug levels are. It's useful to do something like
0 = off
1 = entering major functions
2 = less major functions
3 = specific breakpoints
4 = loop variables
The debug print checks a constant global variable, or if more work is required, gets set by a command line. Means you dont need to remove the statements for the final compile.
Re:Print statements work fine for me, too (Score:3, Interesting)
What's especially potent is that you don't actually need to comment the assertions out : leave them in your code, so that downstream users can activate them and confirm that there's nothing broken in your code. There's no performance hit when they
the problem with print statements... (Score:2, Insightful)
For example, a program in C that uses lots of signals and semaphores could perform differently when print statements are added. This is because print statements take a (relatively) long time to execute. Print statements can affect the bug their supposed to be monitoring.
I had a situation very much like this. One process would fork and exec another, and they would send signals to each other to communicate. But there were a few small bugs that caused one
Re:the problem with print statements... (Score:3, Informative)
Re:Print statements work fine for me, too (Score:3, Funny)
echo 'bork! ';
Nothing I've done needs relative debugging (Score:5, Interesting)
I suppose this would be useful if you were writing something in a new programming language. You could port your code and run the relative debugger to make sure that both implementations acted the same. In such a situation, that would be great, but such a situation isn't the common case for me.
Good idea.. (Score:2, Insightful)
You feed some data to functions, you expect some sane pre-calculated output from them. Simple yet powerful.
And more important it's automatic. So you can integrate it into build process.
AppSight Debugger (Score:2, Informative)
Another cool technique (Score:5, Informative)
cool, but not useful (Score:4, Insightful)
Usually, it's best to avoid going back and forth through the code altogether; insert assertions and see which ones fail.
Re:cool, but not useful (Score:5, Interesting)
Hey, nice ad! (Score:5, Insightful)
I think it's a good idea, but I do wonder how many situations you'll be in where you already have an exisiting program that does everything you want to test against.
Having said, that, I can see how this would help with regression testing - making sure that you've not introduced any new bugs when fixing old ones. But I wonder how much it gives you above a general testing framework anyway...
Re:Hey, nice ad! (Score:5, Insightful)
The fundamental issue here is that people are ALWAYS looking for a way to avoid having to write unit tests. I'm happy with a combination of Intellij and print statements. So far I've never had a situation where I though "the debugger isn't giving me enough information."
I think that one of the reasons I'm happy with the debugging options available to me, is that I write my code so that it can be easily followed in the debugger. That means splitting my declarations and assignments, and other such things that make my code a bit more verbose, but eminently more readable. Lord knows as a child, I loved those complicated boolean switches, and cramming as much line into one line of code as possible. Now that my code is maintained by more people than me, I'm tired of people having ot ask me "what does this do." I used to get angry at them, but now I get angry at myself when that happens. We don't just write code for the users, we write it for our peers. Write code that your sibling developers will be able to follow in a debugger. I know some code is hard to follow, even with a debugger, so I write all my conditions as clearly as possible, name my methods and variables as clearly as I can and refactor reusable code into well named "submethods", so that we can solve "modules".
This is because I want my code to last beyond my employment. Therefore it has to be maintainable by someone other than me. The real test of your code is: can someone ELSE debug it, using whatever the heck tools they want. A fancy debugger is a fine thing, but someday someone is going to have to debug your code with inadequate tools. My rule of them is "Code as if your life depended on someone else being able to fix it"
Re:Hey, nice ad! (Score:5, Funny)
Aspect-Oriented Programming can help (Score:5, Informative)
Basically, you can define an aspect to capture points in your program that are of particular note, and then do debug handling at those points. Aspect oriented programming allows you to break out that debug-handling logic into seperate modules, keeping your main sourcecode nice and clean.
Aspect-oriented programming (AOP) has a lot of other uses too. I think in 5 years or so talking about AOP will be as commonplace as talking about OOP. They are orthogonal concepts.
Cheers, Me
Re:Aspect-Oriented Programming can help (Score:3, Interesting)
Relative debugging, an idea whose time has come (Score:2, Funny)
Re:Relative debugging, an idea whose time has come (Score:2, Offtopic)
I'm not sure if there is a way to reboot female relatives. Except perhaps pulling the plug and doing a cold reboot, but I'm not sure if that's legal.
Revolutionary? NO. (Score:4, Interesting)
Old methods best. (Score:4, Insightful)
The best debugging method is to have a fast build environment so that you can add one printf, rebuild, reproduce the bug, move the printf to an even better place, rebuild and reproduce, etc. The more you rely on your tools to do the work for you, the less you understand the code and the less you understand the code, the more bugs you will make in the future.
There are no shortcuts to good code.
Revolutionary way of debugging what? (Score:5, Interesting)
I'm noy sure what the question is here. Any debugger will allow you to watch data. If your program is special enough that you can't use a standard debugger, you probably need to write a test suite to go with it (and well, for any reasonably sized project, you should anyway).
That's to help you find "surface" bug, i.e. to catch things like misaligned words, wrong data types, buffer overflows
For deep structural problems, like when you try to code something and you have no clue how to go about it, and the end result is just not good and never going to be, the cure is usually a total rewrite, so debuggers won't help you there. That's a problem due to bad architecture of the code.
So, I'm not sure anything else is required. FYI, when I code, I believe I have enough experience to architecture and code something relatively clean the first time, then because I've done it for many years, I sort of "instinctively" expect to find a certain amount of this or that types bugs. And usually, I can fix them without debugging because they jump at me. When they dont (and I can do it), I pull out the old "print to stdout" debugging (or LED wiggling, sound generating
Best debug technique ever (Score:3, Interesting)
I could be all the way across the room, and suddenly there would be this nice clear tone, as my solenoids 'sang' to alert me of trouble.
old technique... (Score:4, Insightful)
However, like many programming techniques, most real world programmers won't know about them unless they can shell out $1000 for a tool; reading a paper or book just would be too much intellectual challenge, right?
This news item seems to be a thinly veiled attempt to drum up business for that company.
Visual software testing (Score:5, Informative)
nothing new (Score:4, Informative)
Almost those features you see in Visual C++, Visual Studio.NET, Eclipse, NetBeans, etc. have been around in IDEs since the 1980's. Debuggers have allowed you to step forwards and backwards, see the source code, examine data structures graphically, and modify the running source code for about as long.
If anything, current commercial IDEs and debuggers still haven't caught up to the state of the art.
Re:nothing new (Score:3, Interesting)
Sorry, you're right, taken literally, what I said doesn't make much sense.
What I meant was that the currently big and commercial platforms (Windows, Macintosh, Java) are still behind what was already available commercially 10-20 years ago, although it wasn't very successful back then.
The reason why it's successful now and wasn't back then is because programmers in industry didn't understand they need
Polyheaded Debugger (Score:5, Funny)
Blinking Lights (Score:2)
A better solution (Score:3, Informative)
And when you launch the program in debug mode everything is printed to a log file and when it crashes or a bug occurs you can just halt everything (if it hasn't crashed) and look at the log to see what it was doing.
Different levels of logging could be used. Say level 1 with the most basic logging (database connections, disk access, network access, etc), level 2 includes all level 1 plus network traffic, level 3 has all object creations, etc.
ex: logEvent(3,"DBO_Connection create");
Some ideas (Score:5, Informative)
which can be just to reduce the number of bugs in a program.
1) 100% unit test coverage of your programs. [all-technology.com]
2) Statistical Debugging [berkeley.edu]
3) Valgrind [kde.org]
4) The D programing Language [digitalmars.com]
with build in support for unit testing, contracts and class Invariants.
Data logging (Score:4, Informative)
In complex, multi-threaded systems where you are debugging timing events more often than programmer logic, data logging (aka print statements) is probably the only technique that works.
In fact, one of the first things we implement in embedded systems is a data logger that can spit out your print statements over RS232. Yes, we can single-step through code using in-circuit emulators and JTAG interfaces, however I found this rarely useful.
Re:Data logging (Score:5, Insightful)
I know it's happened to me
Re:Data logging (Score:5, Informative)
Of course. A good data-logger design does not call expensive output routines in the timing sensitive threads. The routines should be low-cost and append information to some kind of shared memory block such that low-priority threads occasionally format and spit them out to your output device.
Debugging is much, much nicer... (Score:5, Informative)
Re:Debugging is much, much nicer... (Score:3, Insightful)
Now, some people use
You still need self-discipline... (Score:3, Insightful)
Re:Debugging is much, much nicer... (Score:3, Informative)
Most of the time, yes. The reasons are, first, the strict checks it does before even agreeing to run your program, which eliminates a huge class of errors (usually emanating from typos that C will ignore), and the generally clean structure of the language that makes it easier to code what you have in mind. Consequently any errors you see when running the program are likely to be bugs in your algorithm it
RetroVue (Score:4, Interesting)
When I was wandering through JavaOne last year, I ran across this booth by VisiComp, Inc. who sells this debugger called RetroVue [visicomp.com]. I think it's an interesting attempt at bridging the gap between live-breakpoint debugging and logging.
The main issue with debugging vs. logging is that logging provides you with a history of operations that allows you to determine the execution order and state of variables at various times of the execution, something that debuggers don't actually help you with.
RetroVue seems to instrument your Java bytecode to generate a journal file. This journal file is quite similar to a core file extended over time, by recording all operations that occurred in the program over time: every method call, every variable assignment, exception thrown, and context switch. RetroVue then allows you to play back the execution of the application.
It includes a timeline view to jump around the various execution points of the program, as well as an ongoing call-list to show the call sequence that has occurred. It also notes every context switch that the VM makes, and detects deadlocks, thus making it a great tool for multi-threaded application debugging. You can adjust the speed of the playback if you would like to watch things unfold in front of you, or you can pause it at any time and step through the various operations. Want to find out when that variable was last assigned to? Just click a button. Want to find out when that method is called? Same.
It's not free/cheap, but it seems quite useful.
Unit testing (Score:3, Insightful)
Print statements good, debuggers good (Score:4, Interesting)
debugging what? (Score:2)
So, this isn't for developing or implementing a new algorithm.
However, it might be a step closer to fully automating the re-implementation of existing ones ...which is inherently a rote task to begin with.
The best debugging (Score:3, Informative)
Features like exception handling with full stack trace in Java are great, but nothing beats the Smalltalk system of suspending execution and keeping the application 'alive', so it can be modified, inspected and resumed, when an error occurs.
Re:The best debugging (Score:2)
Features like exception handling with full stack trace in Java are great, but nothing beats the Smalltalk system of suspending execution and keeping the application 'alive', so it can be modified, inspected and resumed, when an error occurs.
GDB does this. gdb --pid=[running process].
Re:The best debugging (Score:2)
Valgrind, the ultimate debugging tool (Score:2, Informative)
Valgrind is http://valgrind.kde.org and requires that you turn off all pax protections for the binary you wish to debug.
Rewind (Score:3, Interesting)
Very handy, IMHO, although the O'Caml debugger sucks in other ways. (E.g. no watch conditions.)
*Bzzzt* (Score:4, Insightful)
The key to their idea is that The user first formulates a set of assertions about key data structures, which equals traditional techniques. The reason such traditional techniques have failed and continue to fail is that those assertions are always an order of magnitude simpler than the code itself. These people forget that a program *is* a set of assumptions. Dumbing it down to "x must be > y" doesn't help with the complex flow of information.
Peace & Blessings,
bmac
don't debug (Score:5, Insightful)
Re:don't debug (Score:3, Informative)
So I need two running programs to debug relatively (Score:5, Insightful)
Seriously, though. I've worked as a programmer for the last 15 years. Mostly, I've been fixing other people's bugs. Here's what I like to see in code that I need to fix (and generally don't see):
1) Consistency in formatting, style, variable names, design - I don't care what style you use as long as it's consistent. I prefer my own form of Hungarian Notation, where a variable's prefix indicates its scope (global, static, etc), as well as the type. If any of that information changes, I should darn well follow through to make sure I've fixed everything that depends on them. Bring on strong type checking!
2) No spaghetti code. Give me this:
instead of this: It doesn't look like it matters much yet, but try adding eight more error checks to both, and see which you can track better. The "early bailout on error" model clearly surpasses the "endless nesting" model.3) Use of descriptive variable and procedure names. Source code is not meant to be understood by the computer. This is why we have compilers, and interpreters. Source code is meant to be understood by humans. Write your code for humans, and you'll be surprised at how much faster you can grind through code. You'll only write the code once, but when you have to debug it, you'll spend eternity sifting through line after line, wondering what the hell you meant by that overused "temp" variable (temporary value? temperature? celsius? kelvin?). If you had only taken the time to spell out, "surface_temperature_C", you'd know for sure. Vowels are good for you.
4) Comment! Not every line. Not an impossible to maintain function header comment with dates and initials of everyone who's edited it. Don't fall for nor rely on that "self-documenting" code nonsense. Just one comment line every three to ten code lines. That's all. Give me an overview of what's supposed to happen in each logical block of code. Tell me what if conditions are checking for. A good rule of thumb is to sketch out your functions in comments first, then fill in the blanks.
That's all I can come up with off the top of my head, but there are certainly more...
NOTE: for the pedants who think they noticed an apparent conflict between my hungarian notation style and the "surface_temperature_C" variable: since there is no scope or type prefix on the variable, it's a local variable, and I can change it at will, knowing that it will not affect any code outside the function at hand. If it had been "m_fSurfaceTemperature_C", then I'd know it could have repercussions affecting the state of the current object. If it were "g_fSurfaceTemperature_F", then I'd know I could hose my whole program with an invalid value. And should have converted from Celsius to Fahrenheit before doing so...
Simplicity is more important (Score:4, Informative)
The most effective debugging tool is still careful thought, coupled with
judiciously placed print statements.
-- Brian W. Kernighan, in the paper Unix for Beginners (1979)
But I think the key to debugging is not the technique used for debugging, but how one wrote the code in the first place, here again God Kernighan hits the nail in the head:
Debugging is twice as hard as writing the code in the first place. Therefore,
if you write the code as cleverly as possible, you are, by definition, not
smart enough to debug it.
-- Brian W. Kernighan
Once again, at the time of debugging, simplicity shows it's superiority to the complexity that seems to be so much in fashion this days. That is why I still prefer C to C++; rc [bell-labs.com] to bash; AWK/sed to Perl; Plan 9 [bell-labs.com] to Linux; Limbo [vitanuova.com] to Java; 9p [bell-labs.com] to NFS,...
This is the forgotten key to software design:
so simple that there are obviously no deficiencies and the other way is to make
it so complicated that there are no obvious deficiencies.
-- C.A.R. Hoare, The 1980 ACM Turing Award Lecture
Or put in another way:
The cheapest, fastest, and most reliable components are those that aren't there.
-- Gordon Bell
Back in the topic of debugging, aside from the sacred printf, the Plan 9 [bell-labs.com] debugger acid [bell-labs.com] is often helpful, and now you can even use it on Linux/BSD!
Plan 9 on Unix [swtch.com]
Also the chapter on debugging in The Practice of Programming [bell-labs.com] by Brian W. Kernighan and Rob Pike is very good.
Always remember:
Microsoft Excel used this technique (Score:3, Informative)
So they had the super-optimized version running in parallel with the dumb, calulate-every-cell-every-time engine, and then they'd compare the results.
In certain cases, like this one, the technique is useful, but it's neither revolutionary nor new.
-elan
UPS is worth a look (Score:4, Interesting)
No one has mentioned UPS [demon.co.uk] yet. I'm not sure you could really call it revolutionary, but it does have a few interesting features:
Like other people here I debug mostly with printfs() logged to a file for easy searching, supplemented with valgrind, memprof and occasionally UPS. They are all tools and you need to try to pick the right one for the sort of bug you think you're facing.
Jbuilder Eclipse and co... (Score:4, Informative)
It also has good refactoring support, so no need to debug my poor hand refactoring. I guess that's kinda debugging.
And it's very good at displaying my code in a way that allows me to find any bugs before running it, getters, setters, things I may have wanted to overload, UML diagrams etc... So I guess that's debugging.
Debugging without even having to run the application, and wizards to perform all the monkey work so you don't gte bugs in the first place and intergrated junit testing.
I think Eclipse has simila support.
I'm not a very experianced java programmer, but my productivity is more than 4 times that of a friend whos been programming in java for more than 6 years. I do very little runtime debugging because my code is by and large bug free thanks to the design time and code time debugging in the IDE.
Go download jbuilder [borland.com] trial or Eclipse with some sister project [eclipse.org] plugins (eclipse is a bit of a pain to use because it's still quite a recient product)
I have found... (Score:4, Informative)
Failing that, as most of us do, the next best practice is to program defensively: anticipate where problems might occur in your code and include assertion checking and logging (yes, print statements) to illuminate those problem spots. Generally, I include debugging flags on the command line that allow me to control the level of assertion checking and logging (0=no logging, except for errors (the default), 1=log all branches, 2=log branches and variable values, 4=log everything).
This defensive debugging strategy works quite well. First, it forces the programmer to think harder about both the algorithms they are using, and their implementation. I catch about a quarter of my programming errors just in the process of adding assertions. Second, the program will tend to abort as soon as a problem is detected, rather than running on for a couple billion instructions, dumping crap into the output file or database and then either aborting mysteriously on some marginally related condition, or, worse, completing without any reported errors! Finally, when errors are detected, the debugging can usually be done simply by inspecting the soure and following actual execution from the log file.
All debugging comes down to one, fairly simple, idea: show me the program status at crucial points in the flow of control (generally at every branch and return). A few other tools are of some use under special circumstances: Purify [ibm.com], Electric Fence [perens.com] or Valgrind [kde.org] for detecting problems with dynamically allocated memory, or something like ddd [gnu.org] for examining linked structures (though I prefer to just write a validation function for my data structures, see my AVL-tree [bellatlantic.net] code for an example). Defensive programming works because it answers the important question that usually forces you into using the debugger: what the hell just happened?!? Defensive programming gives you a way to examine program states without invoking an outside tool.
The only class of bugs that doesn't succumb well to this approach is race conditions. Unfortunately, anything that changes the timing of the program (such as stepping instruction-by-instruction in a debugger, or writting log messages out to a disk file) will change the behavior of the race condition. I'd be really interested in tools or techniques that could address this class of bugs.
Distributed, multi-threaded, timing-related (Score:4, Informative)
Those of you who have written multi-threaded applications know what a bitch it can be to debug something when multiple threads are involved.
Those of you who have written timing-sensitive code know what a bitch it can be to debug something that is timing-related.
Now, put all three of those into a pot and stir it around. That's what I and a co-worker have been working on the past four days.
We sent four or five debug versions of the code to the customer for them to run in their production test environment over the past several days with various information printed to the console. With the dials turned way up, the problem usually manifested after a few hours (as opposed to a day or more, when operating under normal conditions). Each time, we'd get back a multi-megabyte log file which we would pore over to see if we had found the root cause of the problem. (Yes, grep was our dear, dear companion -- we're taking it out for drinks as soon as we've verified the problem has been fixed.)
The problem was caused by a specific set of conditions -- the right things happening at the right time, in the right sequence, with a particular timing. To "trap" those conditions would require running both the client and server under a tracing debugger that recorded the time and "event" (e.g. method call, assignment, exception) of everything the system did and then allowed complex queries on the data produced. E.g. "How times per minute was update() called prior to isDead() returning true, on this instance?"
The data could perhaps be recorded using AOP. Next time we run into a scenario like this, it might be worthwhile to break out AspectJ or AspectWorkz. But analysing it will be tricky.
Re:.NET (Score:2, Informative)
2) gdb will do everthing VCDB does, just not as easily (ok not even remotely as easily)
3) there are guys/gals at my work who write very tight code under gcc - they rarely need a debugger anyway. Their debugging is almost all done at design time
Posts like that are just going to rile up their audiance and deserve their low score. Grow up.
So
Re:The use of formal methods? (Score:2)
Re:The use of formal methods? (Score:2)
Ah yes, those damn errors in hardware are surely remedied by printf! Plus, there's tons of buggy hardware floating around that invalidates formal methods! And whose blasted compiler writers sure aren't very good, we might as well write our own compiler! Who needs mathematical proofs of correctness when we can have printf's strewn everywhere? And I'm sure its completely impossible to use BOTH formal methods and printf's!