Good programmers know that they spend as much time debugging as writing so they try to learn from their mistakes.
Techniques that help reduce debugging time include good design, good style, boundary condition tests, assertions and sanity checks in the code, defensive programming, well-designed interfaces, limited global data, and checking tools. An ounce of prevention really is worth a pound of cure.
Some language features make classes of errors less likely: range checking on subscripts. restricted pointers or no pointers at all, garbage collection, string data types, typed UO. and strong type-checking.
On the opposite side of the coin, some features are prone to error, like goto statements, global variables, unrestricted pointers, and automatic type conversions.
5.1 Debuggers
As a personal choice, we tend not to use debuggers beyond getting a stack trace or the value of a variable or two. One reason is that it is easy to get lost in details of complicated data structures and control flow; we find stepping through a program less productive than thinking harder and adding output statements and self-checking code at critical places. Clicking over statements takes longer than scanning the output of judiciously-placed displays. It takes less time to decide where to put print statements than to single-step to the critical section of code, even assuming we know where that is. More important, debugging statements stay with the program; debugger sessions are transient.
5.2 Good Clues, Easy bugs
Thinking backwards from the result to discover the reasons.
** Look for familiar patterns **
** Examine the most recent change **
** Don't make the same mistake twice **
** Get a stack trace **
** Read before typing **
** Explain your code to someone else **
5.3 No Clues, Hard Bugs
** Make the bug reproducible **
** Divide and conquer **
** Study the numerology of failures **
** * Display output to localize your search* **
If you are displaying the value of a variable, format it the same way each time. In C and C++, show pointers as hexadecimal numbers with %x or %p; this will help you to see whether two pointers have the same value or are related .Learn to read pointer values and recognize likely and unlikely ones, like zero, negative numbers, odd numbers, and small numbers. Familiarity with the form of addresses will pay off when you're using a debugger, too.
** Write self-checking code **
** Wrote a log file **
Be sure to flush I/O buffers so the final log records appear in the log file. Output functions like printf normally buffer their output to print it efficiently; abnormal termination may discard this buffered output. In C, a call to fflush guarantees that all output is written before the program dies; there are analogous flush functions for output streams in C++ and Java. Or, if you can afford the overhead, you can avoid the flushing problem altogether by using unbuffered I/O for log files.The standard functions setbuf and setvbuf control buffering; setbuf (fp, NULL) turns off buffering on the stream fp. The standard error streams (stderr, cerr, System. err) are normally unbuffered by default
** Draw a picture **
** Use tools **
For example,a file comparison program like diff compares the outputs from successful and failed debugging runs so you can focus on what has changed. If your debugging output is long, use grep to search it or an editor to examine it. Resist the temptation to send debugging output to a printer: computers scan voluminous output better than people do. Use shell scripts and other tools to automate the processing of the output from debugging runs.
** Keep records **
5.4 Last Resorts
Use a good debugger to step through the program.
5.5 Non-reproducible Bugs
Check whether all variables have been initialized; you may be picking up a random value from whatever was previously stored in the same memory location. Local variables of functions and memory obtained from allocators are the most likely culprits in C and C++. Set all variables to known values; if there's a random number
seed that is normally set from the time of day, force it to a constant, like zero.
If the bug changes behavior or even disappears when debugging code is added. it may be a memory allocation error-somewhere you have written outside of allocated memory, and the addition of debugging code changes the layout of storage enough to change the effect of the bug. Most output functions, from printf to dialog windows, allocate memory themselves, further muddying the waters.
If the crash site seems far away from anything that could be wrong, the most likely problem is overwriting memory by storing into a memory location that isn't used until much later. Sometimes this is a dangling pointer problem, where a pointer to a local variable is inadvertently returned from a function, then used. Returning the address of a local variable is a recipe for delayed disaster:
char *msg(int n,char *s)
{
char buf[100];
sprintf(buf,"error %d: %s\n",n,s);
return buf;
}
By the time the pointer returned by msg is used, it no longer points to meaningful storage. You must allocate storage with malloc. use a static array, or require the caller to provide the space. Using a dynamically allocated value after it has been freed has similar symptoms.We mentioned this in Chapter 2 when we wrote freeall . This code is wrong:
for(p=listp;p!=NULL;p=p->next)
free(p);
Once memory has been freed, it must not be used since its contents may have changed and there is no guarantee that p->next still points to the right place.
In some implementations of malloc and free. freeing an item twice corrupts the internal data structures hut doesn't cause trouble until much later, when a subsequent call slips on the mess made earlier. Some allocators come with debugging options that can be set to check the consistency of the arena at each call; turn them on if you have a non-deterministic bug. Failing that, you can write your own allocator that does some of its own consistency checking or logs all calls for separate analysis. An allocator that doesn't have to run fast is easy to write, so this strategy is feasible when the situation is dire. There are also excellent commercial products that check memory management and catch errors and leaks: writing your own malloc and free can give you some of their benefits if you don't have access to them.
When a program works for one person but fails for another, something must depend on the external environment of the program. This might include files read by the program, file permissions, environment variables, search path for commands, defaults, or startup files. It's hard to be a consultant for these situations, since you have to become the other person to duplicate the environment of the broken program.
Debugging Tools
Other people's bug
Realistically, most programmers do not have the fun of developing a brand new system from the ground up. Instead, they spend much of their time using, maintaining. modifying and thus, inevitably, debugging code written by other people.
This is a place where tools can help significantly. Text-search programs like grep can find all the occurrences of names. Cross-referencers give some idea of the program's structure. A display of the graph of function calls is valuable if it isn't too big. Stepping through a program a function call at a time with a debugger can reveal the sequence of events. A revision history of the program may give some clues by showing what has been done to the program over time. Frequent changes are often a sign of code that is poorly understood or subject to changing requirements. and thus
potentially buggy
The best bug reports are the ones that need only a line or two of input on a plain vanilla system to demonstrate the fault, and that include a fix. Send the kind of bug
report you'd like to receive yourself.