Linux C Programming Learning 3---GDB debugger

Source: Internet
Author: User
Tags gdb debugger

Brief introduction

GDB is a powerful interactive program debugging tool that works primarily under the character interface.

GDB can be used not only to debug programs written in C + + languages, but also to debug programs written in Pascal, Objective-c, and Fortran languages.

GDB Common Commands

The GDB debugger has a number of commands, from simple file loading, breakpoint setting to complex memory viewing, signal capture, and more.

Before using the GDB debugger, the source file must be compiled with GCC (or g++) using the-G option to add debugging information to the program to be debugged, and GCC's-options cannot be used with optimization options

GDB's basic commands include GDB's startup and exit, loading programs, listing source code, executing programs, and using Help functions.

1.GDB start-Up and exit

Entering GDB directly on the command line will enter GDB's interactive mode

In GDB's interactive mode, using the Q command will exit GDB

2. Loading procedures

Mode 1: Direct use command: GDB executable file, example: GDB test

Mode 2: Use GDB to enter GDB's interactive mode first, and then use the file executable in GDB interactive mode (example: File test) to load the executable file that will be debugged

3. List Source code

After loading the program, the first 10 lines of code are listed using the list (or using the abbreviated form L)

then enter L (or list) and the next 10 lines of code are listed

 L 3,5: List the third to fifth lines of code

 L 9: List line nineth and its upper and lower five codes

4. Execution procedures

Use R (or run) to run the loaded program directly, if there is no breakpoint set in the program, then the program will be executed, if there is a breakpoint in the program, then will be executed to the place where the first breakpoint, and then stop

5. Using the Help function

You can use the Help command in GDB's interactive mode to list the types of commands (such as running, stack, status ...). )

If you add a category name after help, all the command information in this category will be listed, such as help running will give you all the commands for running the program

You can get an introduction to this command directly in the name of the help followed by the command, for example, get a detailed description of run

Breakpoint Settings and Management

Breakpoint settings are set breakpoints in the program you are debugging, management of breakpoints including viewing breakpoint information, deleting or disabling breakpoints, etc.

1. Set Breakpoints (break)

command is break, or abbreviated to B

  B 8: Set a breakpoint on line 8th, and if this is used to run the program again with R, you will notice that the program pauses on line 8th

If you want to continue running, you can continue with continue (or C) and pause when a breakpoint is encountered

  b Function name : The program pauses execution when it encounters the specified function

  b 8 If i>7: sets a breakpoint on line 8th and pauses only if the i>7 of line 8th is true

2. View breakpoint information (info break)

  InfoBreak: Displays all breakpoint information currently set by GDB in the debugger

3. Delete the specified breakpoint (d)

  d Breakpoint Number : Either using Delete, specifying the breakpoint number will delete the specified breakpoint, and if you do not specify a breakpoint, all breakpoints will be deleted

4. Delete breakpoints on the specified line (clear)

  Clear Line number (or range): Deletes a breakpoint on the specified line

5. Disable the specified breakpoint (disable)

  Disable Breakpoint number : Temporarily disables the specified breakpoint number and disables all breakpoints if you do not specify a breakpoint number

6. Restore the specified breakpoint (enable)

  Enable breakpoint number : Makes the breakpoint forbidden with disable valid again

7. Set the observation point (watch)

In the process of debugging a program, it is often necessary to locate where a variable has been changed.

Use observer points to detect changes in the value of a variable or expression and to pause the program if there is a change

  Watch variable name (or name of expression): Watch set observer must be set during program run, that is, set after running Run command

Data display and variable assignment

The application of the breakpoint is described above, but if only the breakpoint is of little use, to keep track of the program's operation, you must be able to view or modify the value of the variable at the breakpoint

The following introduction is the example of this source code (TEST.C)

#include <stdio.h>int main () {    int i;    int sum = 0;    for (I=1; i<=10; i++)    {        sum + = i;    }    printf ("1+2+...+10=%d\n", sum);    return 0;}

1. Display the value of a variable or expression (print or P)

1. Compile the program (be sure to use the-G option):gcc-g-o test test.c

2. Load the files to be debugged using gdb test (The following commands are executed in GDB's interactive mode)

3.B 8, set breakpoint on line eighth

4.R, run the program, pause on line eighth

5.p sum, which displays the value of sum at this time, the value of sum is 0

or use p/x sumto output the value of sum in 16 binary form

6. Continue with the C command, and the second loop pauses on line 8th

7.p sum, shows that the sum value at this time is 1 (...). Loop in turn)

The variable after p must be a global variable, or a local variable that is valid in that scope, and if the global variable and local variable have the same name, then the local variable hides the global variable, and if you look at the value of the global variable at this time, use :: operator

2. Automatically display the value of a variable or expression (display)

After you use this command, each time the program runs to a breakpoint, the previously set variable or expression is automatically displayed

1, 2, 3, 4 ibid, at this time suspend execution

5. Displaysum: set to automatically display sum

6.C : Continue execution, and automatically pause when the second loop executes to line 8th, the value of sum is automatically displayed instead of using P sum

3. Display the data type of the variable (Whatis, PType)

1, 2, 3, 4 ibid, at this time suspend execution

5. Using Whatis sum, or ptype sum, will display: type = int, which is the int of the data type of sum

4. Modifying the value of a variable (set)

1, 2, 3, 4 ibid, at this time suspend execution

5. Setsum=100, the sum at this time is 100, then the following run will be executed on the basis of sum=100

6.C , continue to execute, and automatically in the second loop execution to the 8th row and pause, with P sum, you can see the value of sum is 101 (the first cycle is sum+=i; I is 1)

7.C , continue to execute, and automatically in the second loop execution to the 8th row and pause, with P sum, you can see the value of Sum is 103 (the first cycle is sum+=i; I is 2)

Program execution and function invocation

Run and continue are described above, and the other

1. Single Step Execution (step, next)

Step can be abbreviated as S;next can be abbreviated to n

Step: Where the function calls are executed, the inside of the function is tracked (and a hint is given: the entry address of the called function, the line number ...). )

Next: Where a function call is performed, it is not traced to the inside of the function, but directly to the function call as a statement, directly executing the past

2. Exit the called function (return)

Step can track into the inside of the called function, but if you want to exit the called function, you can use the return command

3. Execute to the specified line (until line number)

The premise of using until is that there is no breakpoint from the current line to the specified line, otherwise it pauses at the first breakpoint

4. Jump Execution (jumping)

In general, the program executes in the order of the program code, but if you want to change the order in which the programs are run, you can use the jump

5. Invoke function (call)

Other Debug commands

1. View stack information (BackTrace, frame)

2. View source program information (info source)

3. View register (Info registers)

4. View the assembly code for the program (disassemble)

 

Linux C Programming Learning 3---GDB debugger

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.