1: Debug the function of a series of commands, the source code is as follows MAIN.C
#include <stdio.h>
int add_range (int low, int.)
{
int i,sum;
for (i=low;i<=high;i++)
Sum=sum+i;
return sum;
}
int main (void)
{
int result[100];
Result[0]=add_range (1,10);
Result[1]=add_range (1,100);
printf ("result[0]=%d\nresult[1]=%d\n", result[0],result[1]);
return 0;
}
The result is 55 5015 different from the correct result, debug as follows
1 Step: gcc-g main.c-o main Linux under C source file compilation (contains source code, can be debugged):
GDB main enters the debug of the main function
Help
L 1 or L main View source code
Start debugging
N (Next) Next
S (step) Jump function
BT (backtrace) view the stack frame of a function call
I (info) Locals view local variables of the method
F (Frame) 1 Select Stack Frame No. 1th
I locals view the local variable of stack frame 1th, which is the local variable of the main function
P (print) sum view the value of the sum variable
Finish jumps out of the current function and returns to the main function
Set var sum=0 change the value of the variable sum to 0
P (print) result[2]=33 print can also set the value of a variable like set
2
int main ()
{
int sum=0,i=0;
Char input[5];
while (1)
{
scanf ("%s", input);
for (i=0;input[i]!= ' n '; i++)
sum=sum*10+input[i]-' 0 ';
printf ("input=%d\n", sum);
}
return 0;
}
First time input 123 correct, second error
The debug commands are as follows:
Start Startup debugging
Display sum displays the value of sum each time it is set
Undisplay to cancel the tracking of this variable
B (Break) 9 setting a breakpoint parameter on line 9th can also be a function name
C (continue) means running continuously, jumping to the next breakpoint
I breakpoints show breakpoints that have been set
Delete Breakpoints 2 Remove Breakpoint 2
Delete breakpoints Remove all breakpoints
Disable Breakpoints 3 invalidates a breakpoint
Break 9 if sum! = 0 satisfies the condition to use the breakpoint
R re-execution from the beginning of the program
X command print the contents of the memory x/7b input 7b is a print format, b means a group of each byte, 7 means printing 7 groups
Watch INPUT[5] Tracking a variable
Debug Program method under Linux