The GDB debugging tool for Linux

Source: Internet
Author: User

1. Start GDB

You must use the gcc-g command to generate an executable program with debug information before you can debug a C + + program using GDB:

$ gcc-g-O Hello hello.c

You can then use GDB to debug the generated hello binaries.

$ gdb Hello

GDB will first promise some version information on the screen and then display a prompt (GDB) to wait for the user instruction.

2. Get Help

You can use the Help command at any time to view the helpful information:

(GDB) Help

You can view help information for specific commands, such as breakpoints

(GDB) Help breakpoints

3. View Source code

Using the List command (abbreviated L) for viewing the program's source code, GDB automatically adds a line number to the program source code, the first 10 rows are listed with the list, and the list lists the next 10 rows, and so on.

(GDB) List

You can also assign a line number to the list, displaying 10 lines of source code around the line number.

15

GDB also supports search commands for specific content, showing the first occurrence of the content to be searched, press ENTER to continue the search, it is clear that the search command can only be searched from the beginning to the end:

(GDB) Search main

GDB provides a reverse search command Reverse-search,search and Reverse-search both support regular expression searches.

4. Set Breakpoints

Use the break command to set a breakpoint on the specified line:

 Break Ten // set breakpoints on line 10th

You can use the following command to view information about breakpoints that have been set:

Break

Use the clear command to clear a breakpoint on the current line

(GDB) Clear

5. Run the program and run it in one step

After you set the breakpoint, you can run the program and run to the breakpoint using the Run command (abbreviation R)

(GDB) Run

Wait for the user to issue a command after running to a breakpoint, and use the next command to run one step

(GDB) Next

You can also make a number that GDB executes n times consecutively and then stops

(GDB) n  2

The Continue command (abbreviated C) instructs GDB to run to the next breakpoint.

(GDB)continue

And the step-by-step command, which differs from next, is that next is just an honest step, not into the inside of the function, and step is able to enter the inside of the function.

6. Monitoring variables

The Print command (abbreviated R) allows GDB to output the value of the specified variable:

(gdb) Print sum

But print is cumbersome, and each time you need to enter the Print command to keep tabs on a variable, GDB provides the watch command to set a variable as a watch point

7. Modifying variables temporarily

GDB provides the set var command to change the value of a variable while the program is running

Set var i=1

8. View Stack condition

BT Command View current run-time stack condition

9. Exit GDB

Quit GDB debugging using the Quit command

The GDB debugging tool for 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.