Use GDB to debug Linux software

Source: Internet
Author: User
Article title: use GDB to debug Linux software. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Author: David Seager
  
Introduction to the GNU Debugger
Author: David Seager
  
  
Most of the features of Linux are derived from the GNU Debugger of shell, also known as gdb. Gdb allows you to view the internal structure of the program, print the variable value, set the breakpoint, and debug the source code in one step. It is an extremely powerful tool for fixing problems in program code. In this article, I will try to explain how great and practical gdb is.
  
Compile
Before debugging, you must use the debugging information in the program to compile the program to be debugged. In this way, gdb can debug the variables, code lines, and functions used. To compile the program, use the extra-g option in gcc (or g ++) to compile the program:
  
Gcc-g eg. c-o eg
  
Run gdb
In shell, you can use the gdb command and specify the program name as the parameter to run gdb, for example, gdb eg; or in gdb, you can use the file command to load the program to be debugged, for example, file eg. Both methods assume that you are executing commands in the directory containing the program. After the program is loaded, run the gdb command un to start the program.
  
Debugging session example
If everything is normal, the program will be executed until the end, and gdb will get control again. But what if there is an error? In this case, gdb gets control and interrupts the program so that you can check the status of all things. if you are lucky, you can find out the cause. To cause this problem, we will use an example program:
  
Code example eg1.c
# Include
  
Int wib (int no1, int no2)
{
Int result, diff;
Diff = no1-no2;
Result = no1/diff;
Return result;
}
  
Int main (int argc, char * argv [])
{
Int value, div, result, I, total;
  
Value = 10;
Div = 6;
Total = 0;
  
For (I = 0; I <10; I ++)
{
Result = wib (value, div );
Total + = result;
Div ++;
Value --;
}
  
Printf ("% d wibed by % d equals % dn", value, div, total );
Return 0;
}
  
  
  
  
This program will run 10 for loops, use the wib () function to calculate the cumulative value, and finally print the result.
  
Enter this program in your favorite text editor (to keep the same line spacing), save it as eg1.c, compile it with gcc-g eg1.c-o eg1, and start gdb with gdb eg1. Running a program using un may produce the following message:
  
  
Program received signal SIGFPE, Arithmetic exception.
0x80483ea in wib (no1 = 8, no2 = 8) at eg1.c: 7
7 result = no1/diff;
(Gdb)
  
  
  
Gdb indicates that an arithmetic exception occurs in row 7th of the program. generally, it prints this row and the independent variable value of the wib () function. To view the source code before and after 7th lines, use the list command, which usually prints 10 lines. Enter list again (or press enter to repeat the previous command) to list the next 10 lines of the program. From the gdb message, we can see that the division operation in row 7th has an error. The program divides the variable "no1" by "diff" in this row ".
  
To view the value of a variable, run the gdb print command and specify the variable name. Enter print no1 and print diff. the values of "no1" and "diff" are displayed. The result is as follows:
  
  
(Gdb) print no1
$5 = 8
(Gdb) print diff
$2 = 0
  
  
  
Gdb indicates that "no1" is equal to 8, and "diff" is equal to 0. Based on these values and the statements in the 7th rows, we can infer that an arithmetic exception is caused by a division operation with a divisor of 0. The list shows the variable "diff" calculated in line 6th. we can print the "diff" expression (using the print no1-no2 command) to re-estimate the variable. Gdb tells us that the two independent variables of the wib function are both equal to 8, so we need to check the main () function that calls the wib () function to see when this happened. While the program is allowed to terminate naturally, we use the continue command to tell gdb to continue the execution.
  
  
(Gdb) continue
Continuing.
  
Program terminated with signal SIGFPE, Arithmetic exception.
The program no longer exists.
  
  
  
Use breakpoint
To view what happened in main (), you can set a breakpoint in a specific row or function in the program code, so that gdb will interrupt execution when a breakpoint occurs. You can use the reak main command to set a breakpoint when entering the main () function, or specify any other function name of interest to set a breakpoint. However, we only want to interrupt the execution before calling the wib () function. Input list main to print the source code list starting from the main () function. press enter again to display the wib () function call on Line 1. To set a breakpoint on that line, you only need to enter reak 21. Gdb will send the following response:
  
  
(Gdb) break 21
Breakpoint 1 at 0x8048428: file eg1.c, line 21.
  
  
  
To show that it has set a breakpoint 1 on the row we requested. The un Command will re-run the program from the beginning until gdb is interrupted. In this case, gdb generates a message indicating the breakpoint at which it is interrupted and where the program runs:
  
  
Breakpoint 1, main (argc = 1, argv = 0xbffff954) at eg1.c: 21
21 result = wib (value, div );
  
  
  
When print value and print div are issued, the variables are equal to 10 and 6 when wib () is called for the first time, and print I is 0. Fortunately, gdb will display the values of all local variables and use the info locals command to save a large amount of input information.
  
As can be seen from the above survey, when "value" and "div" are equal, there will be problems. Therefore, enter continue to continue the execution until the next breakpoint occurs. For this iteration, info locals shows value = 9 and div = 7.
  
Instead of continuing again, you can use the ext command to debug the program in one step to see how "value" and "div" are changed. Gdb will respond:
  
  
(Gdb) next
22 total + = result;
  
  
  
Press enter twice to display the addition and subtraction expressions:
  
  
(Gdb)
23 div ++;
(Gdb)
24 value --;
  
  
  
Press enter twice to display the 21st rows, wib () call. Info locals will display the current "div" is equal to "value", which means the problem will occur. If you are interested, you can use the step command (compared with ext, ext will skip the function call) to continue executing the wib () function to view the division error again, use ext to calculate "result ".
  
Now that debugging has been completed, you can use the quit command to exit gdb. Because the program is still running, this operation will terminate it, and gdb will prompt you to confirm.
  
More breakpoint and observation points
Since we want to know when "value" equals "div" before calling the wib () function, in the previous example, we set a breakpoint in row 21st. This happens only when we continue executing the program twice, but we only need to set a condition on the breakpoint so that gdb can be paused only when "value" and "div" are truly equal. To set conditions, you can specify "break if" when defining a breakpoint ". Load eg1 into gdb again and enter:
  
  
(Gdb) break 21 if value = div
Breakpoint 1 at 0x8048428: file eg1.c, line 21.
  
  
  
If you have already set a breakpoint in line 1, for example, breakpoint 1, you can use the condition command to set the condition on the breakpoint:
  
  
(Gdb) condition 1 value = div
  
  
  
When running eg1.c using un, if "value" is equal to "div", gdb will be interrupted, thus avoiding the need to manually execute continue before they are equal. When debugging a C program, the breakpoint condition can be any valid C expression, which must be any valid expression of the language used by the program. The variable specified in the condition must be in the row where the breakpoint is set; otherwise, the expression has no meaning!
  
When you use the condition command, if you specify the breakpoint number but do not specify the expression, you can set the breakpoint to an unconditional breakpoint. for example, condition 1 sets the breakpoint 1 to an unconditional breakpoint.
  
To view the defined breakpoints and their conditions, run info break:
  
  
(Gdb) info break
Num Type Disp Enb Address What
1 breakpoint keep y 0x08048428 in main at eg1.c: 21
Stop only if value = div
Breakpoint already hit 1 time
  
  
  
In addition to all conditions and the number of times a breakpoint has been encountered, the breakpoint information also specifies whether to enable the breakpoint in the Enb column. You can use the disable, enable, or delete command to disable, enable, and permanently delete a breakpoint. for example, disable 1 will prevent interruption at breakpoint 1.
  
If we are more interested in when "value" is equal to "div", we can use another breakpoint called monitoring. When the value of the specified expression changes, the monitoring point interrupts program execution, but the variables used in the expression must be set to the monitoring point in the scope. To obtain "value" and "div" in the scope, you can set a breakpoint on the main function, then run the program, and set a monitoring point when a main () breakpoint occurs. Restart gdb, load eg1, and enter:
  
  
(Gdb) break main
Breakpoint 1 at 0x8048402: file eg1.c, line 15.
(Gdb) run
...
Breakpoint 1, main (argc = 1, argv = 0xbffff954) at eg1.c: 15
15 value = 10;
  
  
  
Watch div can be used to understand when "div" is changed. However, because it is interrupted when "div" is equal to "value", enter:
  
  
(Gdb) watch div = value
Hardware watchpoint 2: div = value
  
  
  
If execution continues, when the value of the expression "div = value" changes from 0 (false) to 1 (true), gdb will interrupt:
  
  
(Gdb) continue
Continuing.
Hardware watchpoint 2: div = value
<

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.