Debug Program method under Linux

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.