GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing cs.baylor.edu — How to Debug Using GDB How to Debug Using GDBWe are going to be using two programs to illustrate how GDB can be used to debug code.Debugging a program with a logical errorThe first sample program has some logical errors. The program is supposed to output the summation of (X^0)/0! + (X^1)/1! + (X^2)/2! + (X^3)/3! + (X^4)/4! + ... + (X^n)/n!, given x and n as inputs. However the program outputs a value of infinity, regardless of the inputs. We will take you step by step through the debugging process and trace the errors:
% g++ -g broken.cpp -o broken % ./brokenWhatever the input, the output will be inf. The -g option is important because it enables meaningful GDB debugging. % gdb brokenThis only starts the debugger; it does not start running the program in the debugger. (gdb) b 43which is double seriesValue = ComputeSeriesValue(x, n); (gdb) runNote: If you need to supply the command-line arguments for the execution of the program, simply include them after the run command, just as normally done on the command line. This program is used to compute the value of the following series : (x^0)/0! + (x^1)/1! + (x^2)/2! + (x^3)/3! + (x^4)/4! + ........ + (x^n)/n! Please enter the value of x : 2 Please enter an integer value for n : 3 Breakpoint 1, main () at broken.cpp:43 43 double seriesValue = ComputeSeriesValue(x, n);Note that the program execution stopped at our first (and only) breakpoint. (gdb) step ComputeSeriesValue (x=2, n=3) at broken.cpp:17 17 double seriesValue=0.0;At this point, the program control is at the first statement of the function ComputeSeriesValue (x=2, n=3) (gdb) bt #0 ComputeFactorial (number=0) at broken.cpp:7 #1 0x08048907 in ComputeSeriesValue (x=3, n=2) at broken.cpp:21 #2 0x08048a31 in main () at broken.cpp:43 |
| You are visitor 01402860 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |