Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of using GDB to debug C++ program in Linux system

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains the "method of using GDB to debug C++ programs in the Linux system". The content of the article is simple and clear, and it is easy to learn and understand. Now please follow the editor's train of thought slowly and deeply, to study and learn "the method of using GDB to debug C++ programs in the Linux system".

Install GDB

Most distribution repositories have GDB

Debian or Ubuntu

The code is as follows:

$sudo apt-get install gdb

Arch Linux

The code is as follows:

$sudo pacman-S gdb

Fedora,CentOS or RHEL:

The code is as follows:

$sudo yum install gdb

If you can't find it in the warehouse, you can download it from the official website.

Sample code

When learning GDB, it is best to have a code and try it out. The following code is a simple example I wrote, which can well reflect the characteristics of GDB. Copy it and experiment with it-this is the best way.

The code is as follows:

# include

# include

Int main (int argc, char * * argv)

{

Int i

Int axiom 0, bounty 0, cis 0

Double d

For (iInfo; i97)

D = I / 2.0

Baked +

}

Return 0

}

The use of GDB

First and foremost, you need to use the compiler's "- g" option to compile the program so that the executable can run through GDB. Start debugging with the following statement:

The code is as follows:

$gdb-tui [executable program name]

Use the "- tui" option to display the code in a beautiful interactive window (so called "text user interface TUI"), where you can use the cursor to manipulate and enter commands in the GDB shell below.

Now we can set breakpoints anywhere in the program. You can set a breakpoint for a line of the current source file with the following command.

The code is as follows:

Break [line number]

Or set a breakpoint for a specific function:

The code is as follows:

Break [function name]

You can even set conditional breakpoints.

The code is as follows:

Break [line number] if [condition]

For example, in our sample code, we can set this up as follows:

The code is as follows:

Break 11 if I > 97

In this way, the program loops 97 times and stays on the "averse +" statement. This is very convenient and avoids the need to manually cycle 97 times.

Last but not least, we can set an "observation breakpoint" where the program will be stopped when the observed variable changes.

The code is as follows:

Watch [variable]

Here we can set it as follows:

The code is as follows:

Watch d

The program stops running when the value of d changes (for example, when I > 97 is true).

When the breakpoint is set, use the "run" command to start the program, or as follows:

The code is as follows:

R [input parameters of the program, if any]

In gdb, most command words can be abbreviated to one letter.

No accident, the program will stay at 11 lines. Here, we can do some interesting things. The following commands:

The code is as follows:

Bt

The backtracking function (backtrace) lets us know how the program reaches this statement.

Info locals

This statement displays all local variables and their values (as you can see, I didn't set the initial value for d, so it now has an arbitrary value).

Of course:

The code is as follows:

P [variable]

This command displays the value of a specific variable, and goes a step further:

The code is as follows:

Ptype [variable]

You can display the type of variable. So here we can make sure that d is a simple type.

Now that we've come this far, we might as well do this:

The code is as follows:

Set var [variable] = [new value]

This overrides the value of the variable. Note, however, that you cannot create a new variable or change the type of variable. We can do this:

The code is as follows:

Set var a = 0

Like other good debuggers, we can step through:

The code is as follows:

Step

Using the above command, run to the next statement and it is possible to enter a function. Or use:

The code is as follows:

Next

This allows you to run the next statement directly without going inside the subfunction.

At the end of the test, delete the breakpoint:

The code is as follows:

Delete [line number]

Continue to run the program from the current breakpoint:

The code is as follows:

Continue

Exit GDB:

The code is as follows:

Quit

Thank you for your reading, the above is the content of "using GDB to debug C++ programs in the Linux system". After the study of this article, I believe you have a deeper understanding of the method of using GDB to debug C++ programs in the Linux system, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report