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."
AppSight Debugger (Score:2, Informative)
Another cool technique (Score:5, Informative)
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
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).
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:.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.
Some flamebait of my own: I will try to not look down my nose at you for being a VB "programmer."
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");
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.
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.
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...
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.
Debugging is much, much nicer... (Score:5, Informative)
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.
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.
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: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 itself, not in the implementation.
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.
Re:Exceptions (Score:3, Informative)
Re:Print statements work fine for me, too (Score:3, Informative)
h
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:the problem with print statements... (Score:3, Informative)
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.
Re:don't debug (Score:3, Informative)
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:
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)
Also, if you're smart, you can take the stack traces and find exactly what functions/datum those were. Had to do that a bunch on the IBM S/390. It's not hard, and with debugging code enabled, any exception generated will be able to produce line numbers and function traces.
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...
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
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
Re:Valgrind (Score:5, Informative)
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.
Welcome to the 70s! (Score:3, Informative)
One thing that shouldn't be missed is that object-oriented exception handling, as popularized by Java, was invented in MIT Lisp Machine Lisp. This simplified and regularized error handling tremendously, to the point where today hooks into the deepest aspects of a typical implementation of Common Lisps' debugger are reduced to about a page's worth of code to deal with stack handling. I did a little work on the CLISP [cons.org] backend for SLIME [common-lisp.net], and this really surprised me.
Self Certifying Code (Score:1, Informative)
Delta Debugging (Score:1, Informative)
Basic idea: Run a program with input data until it crashes. Debugger will find the diff in the input data that triggered the crash and the location in the source code where the crash originated. Also works for same input and different program versions.
It's free and they have an online demo where you can upload your program with test cases and they will show you where the bug lies. (http://www.st.cs.uni-sb.de/askigor/)
Also works in Eclipse with automated testing using JUNIT.
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.
Ida Pro 4.6, SoftICE, gdb and more... (Score:2, Informative)
the best disassembler but also a great debugger, it can
graph function flows, display pentium microcode, supports
nearly every processors on the market (including your car's
CPU.) Works nice with linux ELF binaries, etc... It is used by most antivirus researchers, crackers (who remove software protections), reverse engineers, hackers (who write exploits), etc. It runs perfect under wine without tweaking. Grab the
demo and give it a go.
Also under windows, SoftICE [compuware.com], is also an excellent debugger which lets you assemble in place and do many other neat things.
Under linux, people have been trying to make SoftICE look-a-like debugger, such as LinICE, etc. and gdb is quite a powerful tool and is scriptable.