Use of GDB in Linux

Source: Internet
Author: User

Errors in program code can be divided into several classes. Apart from the most easily excluded syntax errors, the Compilation Program will tell you where the errors are. Most errors can be classified as execution errors. The GDB function is to find errors during execution. If there is no debugging program, we can only add the command of output variable value to the program to understand the program execution status. With the gdb debugging program, we can set to stop the program execution anywhere, then we can view variable values and change variables at will, and execute the program line by line.


The process of executing a debugging program is usually as follows:

1. Enter the debugging program and specify the executable file.

2. Specify the directory where the program code is located.

3. Set the breakpoint and run the program.

4. After a program is interrupted at a breakpoint, you can (1) view the program execution status and variable values or change variable values;

(2) Gradually execute the program, or execute the program at full speed until the next breakpoint or until the program ends.

5. Leave the debugging program.


1: Enter GDB and read the executable file (File Name: 'program '), and prepare for debugging:

GDB Program


2: Specify the directory where the program code is located and view the program code

2.1: add the directory dir to the directory list for searching program code (If your program code and executable file are in the same directory, you do not need to specify the directory where the program code is located .) :

(GDB) Directory dir

2.2: view the program code in the following format:

(GDB) List => displays the code of each five lines before and after the currently executed program code; or displays the code of the program after the last list.

(GDB) list function => display the program code of the first five lines at the beginning of the program.

(GDB) List-=> the first 10 lines of the program code are displayed last time.


3: breakpoint setting and clearing

3.1: Set the breakpoint (the command is break, which can be abbreviated as (B). The format is as follows:

(GDB) Break filename. C: 30 =>; stop running at row 30th of filename. C.

(GDB) Break function =>; interrupts program execution when the function is enabled.

(GDB) Break filename. C: function =>; set the breakpoint at the function in the filename. c file of the program code.

(GDB) Break =>; set the breakpoint for the next command to be executed.

(GDB) Break... If cond =>; it is interrupted only when cond is established. Cond must be written in C syntax.

3.2: displays the information of each breakpoint.

(GDB) info break

3.3: Clear the breakpoint (command is clear) in the same format as break. For example:

(GDB) Clear filename. C: 30

3.4: Clear the breakpoint. Num indicates the breakpoint number displayed in info break.

(GDB) delete num


4: full speed and step-by-step execution

4.1: Execute the program at full speed starting with the program until the breakpoint or program execution is completed.

(GDB) Run

4.2: after the program is interrupted, the program is executed at full speed until the next breakpoint or the program ends (the continue command can be abbreviated as C ).

(GDB) continue

4.3: execute a program. If a function is called, the code containing the function program is considered as a program (the next command can be abbreviated as N ).

(GDB) Next

4.4: execute a program. If a call function is called, the function is executed line by line (the step command can be abbreviated as S ).

(GDB) Step

4.5: execute a program. If the program is in the last line of the For/while/do loop, the program is executed until the end of the loop and stops (the until command can be abbreviated as U ).

(GDB)

4.6: Execute the current program until it returns to the previous program.

(GDB) Finish


5: view and modify the value

5.1: Print description, showing the execution result of this description (the print command can be abbreviated as P ). For example

(GDB) print a => display the content of the variable.

(GDB) print sizeof (A) => display the length of variable.

5.2: Display description. The description value is displayed at each breakpoint or when each step is executed. For example

(GDB) display

5.3: more variable value:

(GDB) print (A = 10) => set the value of variable A to 10.


6. View program execution status

Check which function call program (the backtrace command can be abbreviated as BT) is executed by the program, that is, view the function call stack.

(GDB) backtrace


7. Read Core File Information

Read the program and program. core files to view the program variable value and the program process status during core dump.

GDB program Core

Note: The 'core' file is the core file name generated when a core dump occurs after the program file is executed. If you still need this core file, we recommend that you change the file name to program. Core. After entering the preceding command, you can use the view variable value provided by GDB and view the program execution status to read the state of the program core dump.

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.