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

How to use gdb to debug C programs in Linux

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

Share

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

This article mainly shows you "how to use gdb to debug C programs in Linux", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "how to use gdb to debug C programs in Linux" this article.

No matter how experienced a programmer is, it is impossible to develop any software without bug. Therefore, troubleshooting and repairing bug has become one of the most important tasks in the software development cycle. There are many ways to troubleshoot bug (testing, code self-review, and so on), but there is also specialized software (called debuggers) that can help pinpoint the problem so that it can be fixed.

If you are a Cmax Cure + programmer or develop software in the Fortran and Modula-2 programming languages, you will be happy to know that there is such an excellent debugger-GDB-that can help you debug code bug and other problems more easily. In this article, we will discuss the basics of the GDB debugger, including some of the useful features / options it provides.

Before we begin, it is worth mentioning that all the instructions and examples in this article have been tested in Ubuntu 14.04 LTS. The sample code in the tutorial is written in C; the shell used is bash (4.3.11); the GDB version is 7.7.1.

GDB Debugger Basics

Generally speaking, GDB allows you to see the internal flow of the program during execution and helps you identify the problem. We'll discuss the use of the GDB debugger with a valid example in the next section, but before we do that, let's explore some basic points that will help you later.

First of all, in order to successfully use a debugger like GDB, you must compile the program in a specified way so that the compiler produces the debugging information needed by the debugger. For example, when compiling code using the gcc compiler (which we will use to compile C program examples later in this tutorial), you need to use the-g command line option.

To learn more about the-g command line option in the gcc compiler man page, see here.

Next, make sure that the GDB debugger is installed on your system. If it is not installed, and you are using a Debian-based system (such as Ubuntu), you can easily install the tool using the following command:

Sudo apt-get install gdb

For installation methods on other distributions, see here.

Now, when you have compiled the program as described above (the gcc-g command line option) and have the GDB debugger installed, you can use the following command to make the program run in debug mode:

Gdb [name of executable program]

Doing so will initialize the GDB debugger, but your executable will not be started at this time. At this point you can define debugging-related settings. For example, you can set a breakpoint on a specific line or function for GDB to suspend the execution of the program on that line.

Then, in order to start your program, you must type and execute the following gdb command:

Run

It is worth mentioning here that if your program requires some command-line arguments, you can specify them here. For example:

Run [parameters]

GDB provides a lot of useful commands that always come in handy when debugging. We will discuss some of these commands in the next section.

GDB Debugger use case

Now we have a basic concept of GDB and its usage. Therefore, let's give an example to apply what we have learned. This is a sample code:

# include int main () {int out = 0, tot = 0, cnt = 0; int val [] = {5,54,76,91,35,27,45,15,99,0}; while (cnt < 10) {out = val [cnt]; tot = tot + 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff

Explain briefly what this code is going to do. Get each value in the val array, assign it to the out variable, then add the previous value of tot with the result value of 0xffffffff/out, and assign it to the tot variable.

The problem encountered here is that the following error occurs when executing the compiled executable of this code:

$. / gdb-testFloating point exception (core dumped)

Therefore, to debug this code, the * step is to compile the program with the-g option. The command is as follows:

Gcc-g-Wall gdb-test.c-o gdb-test

Next, let's run the GDB debugger and specify the executable program to debug. The command is as follows:

Gdb. / gdb-test

Now, the error I just got is Floating point exception, and most of you probably already know that this is due to the error caused by n% x when x is 0. So, with this in mind, I added a breakpoint at the location of the division operation in 11 lines of code. As follows:

(gdb) &; break 11

Note that (gdb) is a prompt for the debugger, and I only entered the break 11 command.

Now, let GDB start running the program:

Run

When the breakpoint is * for * times, GDB displays the following output:

Breakpoint 1, main () at gdb-test.c:11 11 tot = tot + 0xffffffffffffffffue out; (gdb)

As you can see, the debugger displays the line code where the breakpoint is located. Now, let's print out the value of out at this time. As follows:

(gdb) print out$1 = 5 (gdb)

As shown above, the value of 5 is printed. Everything is normal at this time. Letting the debugger continue to execute the program until the next breakpoint can be done by using the c command

C

Repeat until the out value changes to 0.

......... Breakpoint 1, main () at gdb-test.c:11 11 tot = tot + 0 xffffffffffffffffffffffffffffffUniver out; (gdb) print out $2 = 99 (gdb) c Continuing. Breakpoint 1, main () at gdb-test.c: 1111 tot = tot + 0 xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffout; (gdb) print out $3 = 0 (gdb)

Now, to further confirm the problem, I use GDB's s (or step) command instead of the c command. Because I just want the current program to be paused after line 11 and executed step by step to see if a crash occurs at this time.

The following is the output information after execution:

(gdb) s Program received signal SIGFPE, Arithmetic exception. 0x080484aa in main () at gdb-test.c: 1111 tot = tot + 0xffffffff/out

Yes, as shown in the * line in the output above, this is where the exception is thrown. When I tried to run the s command again, the problem was finally confirmed:

(gdb) s Program terminated with signal SIGFPE, Arithmetic exception. The program no longer exists.

In this way, you can debug your program using GDB.

The above is all the contents of this article "how to use gdb to debug C programs in Linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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