Wednesday, October 21, 2009

17.6 Interactive Debugging Tips and Tricks




I l@ve RuBoard










17.6 Interactive Debugging Tips and Tricks



Interactive debuggers work well for most programs, but sometimes they
need a little help. Consider Example 17-4. We try to
debug it and find it fails when point_number is
735. Why it fails on call 735 to lookup we
don't know, but the first 734 calls work and the
next one doesn't. All we know is that we call
float_point_color 800 times, it calls
lookup 800 times, and something goes wrong at 735.



We want to put a breakpoint before the calculation is made. When the
debugger inserts a breakpoint into a program, the program will
execute normally until it hits the breakpoint, then control will
return to the debugger. This allows the user to examine and change
variables, as well as perform other debugging commands. When a
cont command is typed, the program will continue
execution as though nothing happened. The problem is that there are
734 points before the one we want, and we don't want
to stop for each of them.




Example 17-4. debug/cstop.cpp

float point_color(int point_number)
{
float correction; // color correction factor
extern float red,green,blue;// current colors

// Lookup color correction
extern lookup(int point_number);

correction = lookup(point_number);
return (red*correction * 100.0 +
blue*correction * 10.0 +
green*correction);
}



How do we force the debugger to stop only when point_number
== 735
? We can do this by adding the following temporary
code:



48:    if (point_number == 735)  /* ### Temp code ### */ 
49: point_number = point_number; /* ### Line to stop on ### */


Line 49 does nothing useful except serve as a line that the debugger
can stop on. We can put a breakpoint on that line with the command
break 49. The program will process the first 734
points, then execute line 49, hitting the breakpoint. (Some debuggers
have a
conditional
breakpoint. The advanced GDB command break 49 if
point_number == 735
would also work; however, your debugger
may not have such advanced features.)









    I l@ve RuBoard



    No comments:

    Post a Comment