3, using GDB for program debugging

Source: Internet
Author: User

This article will use a practical example to explain how to debug the program through GDB.

First of all, what we need to understand is that GDB is a powerful UNIX program debugging tool released by the GNU Open Source organization, which is produced and debugged to let the debugger know what is happening inside the program at execution time, or what is being done in the process, in general, GDB mainly helps to achieve the following four functions.

1, start your program, you can follow your custom requirements to run the program at will.
2. Allow the program to be debugged to stop at the breakpoint where you have specified the adjustment. (Breakpoint can be a conditional expression)
3. When the program is stopped, you can check what happened in your program at this time.
4. Dynamically change the execution environment of your program.

You can start gdb by entering GDB on the command line, and once you have started it, you will be able to accept the commands entered by the user from the keyboard and complete the corresponding tasks. To exit GDB, simply enter the command quit in the working environment.

The following is a debug program MIAN.C

#include <stdio.h>intFuncintN) {intsum=0, I;  for(i=0; i<n; i++) {sum+=i; }             returnsum; } main () {inti; Longresult =0;  for(i=1; i<= -; i++) {result+=i; } printf ("result[1-100] =%d/n", result); printf ("result[1-250] =%d/n", Func ( -) ); }

Using directives

Gcc-g Main.c-o Main

After compiling, pass the

Gdb-tui Main

By entering return, you can use the "-tui" option to display the code in a nice interactive window (so called "Text user interface Tui"), where you can manipulate the cursor and enter commands in the following GDB shell.

Use the GDB Debug command to add a breakpoint and view the breakpoint information as follows:

Run the program to see the program stop after the breakpoint:

Continue to run and view related variables

Continue to run and delete breakpoint actions

View the program stack, run out of the current function and exit GDB debugging

Through the above more intuitive operation, you can have a general understanding of the operation of GDB.

The use syntax for the command GDB is as follows:

GDB Options  executable Core file | Process ID

The parameters of the command GDB are typically the program to be debugged, or the core file that is generated when the program runs the error, or the process ID that is running.

The following options are commonly used by GDB:

-C core uses the specified core File Checker.

-H gives a simple explanation of the help options

-N ignores the command specified in the./gdbinit file

-Q does not display copyright and other information.

-S uses the symbol table that is saved in the specified file.

All GDB commands

GDB has all the commands to view http://users.ece.utexas.edu/~adnan/gdb-refcard.pdf, but we usually do not have so many debug commands, here are some of the commands commonly used when debugging programs are listed as follows:

List Viewer Program

1. The break line number adds a breakpoint to the specified line.

2. The break filename line number adds a breakpoint to the specified line of the specified file.

3. Break line number if condition when the condition is true, the breakpoint at the specified line number takes effect.

For example:

5 if i =ten  //When i=10 5th breakpoint takes effect

4. set var variable = new variable assigns a value to a variable

5. Info Break View all set breakpoints

6. Delete Breakpoint Number Remove Breakpoint

7. Run starts the program

8, next single-step operation (do not enter the sub-function)

9, step single-step operation (Enter sub-function)

10, continue continue to run the program

11, Print (p) p is the variable name, view the value of the specified variable name

12. Finish run the program until the current function ends

13, watch variable name to monitor the specified variable name

14. Quit GDB

15, BT display all the debug stack frame, the command can be used to display the function call order

16. Clear delete Breakpoints set on a specific file, on a specific line

17. Display EXPR Displays the value of an expression after each program stop

18. File file is installed in the executable file to debug

19. Help name displays the assistance information for the specified command

20. Info Func Displays the names of all functions

21, Info local variable information in the display function

22. Info prog shows the execution status of the program being debugged.

23, Info var displays all the global and static variable names

24. Kill terminates the program being debugged

25. Make to run the Make tool without exiting GDB.

26. Print expr Displays the value of the expression expr.

Reference links

Https://baike.baidu.com/item/gdb/10869514?fr=aladdinGDB's Baidu Encyclopedia

Https://linux.cn/article-4302-1.html an instance of debugging using GDB

Http://blog.csdn.net/yyaong_520/article/details/6766831GDB Debugging Commands

3, using GDB for program debugging

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.