Table of Contents
GDB的相关概念
GDB的相关操作
GDB单步调试
Home Operation and Maintenance Linux Operation and Maintenance Linux debugging tool GDB usage tutorial

Linux debugging tool GDB usage tutorial

Jan 24, 2018 pm 01:12 PM
linux use Tutorial

本文主要介绍了Linux调试工具GDB使用简易教程,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下,希望能帮助到大家。

声明:本博客内容是由本人经过实验楼教程整理得来。

GDB的相关概念

GDB, 是 The GNU Project Debugger 的缩写, 是 Linux 下功能全面的调试工具。GDB 支持断点、单步执行、打印变量、观察变量、查看寄存器、查看堆栈等调试手段。在 Linux 环境软件开发中,GDB 是主要的调试工具,用来调试 C 和 C++ 程序。

GDB的相关操作

下面给出一个具有buging.c的程序:

[yqtao@localhost linux]$ cat buging.c 
#include <stdio.h>
int getSum(int n) {
    int sum=0,i;
    for (i=1;i<=n;i++)
        sum+=i;
    return sum;
}
int main(){
    int res=getSum(100);
    printf("1+2+...+100=%d\n",res);
}
Copy after login

如果要调试程序,则在进行gcc编译的时候要加上-g参数

gcc -g -o bugging bugging.c

进入gdb:

gdb buging

部分gdb命令:

// 查看源码
// 查看第几行附近
// 可简写成l 2
(gdb) list 2   
1    #include <stdio.h>
2    int getSum(int n) {
3        int sum=0,i;
4        for (i=1;i<=n;i++)
5            sum+=i;
6        return sum;
7    }

// 查看main函数附近的源码
(gdb) list main
3        int sum=0,i;
4        for (i=1;i<=n;i++)
5            sum+=i;
6        return sum;
7    }
8    int main(){
9        int res=getSum(100);
10       printf("1+2+...+100=%d\n",res);
11   }


// 设置断点
// break +n在第几行设断点
(gdb) break 8
Breakpoint 1 at 0x4004fa: file buging.c, line 8.
// break +函数名 ,在函数名处设断点
(gdb) break getSum
Breakpoint 2 at 0x4004cb: file buging.c, line 3.

//查看断点信息
(gdb) info breakpoints 
Num   Type      Disp Enb Address      What
1    breakpoint   keep y  0x00000000004004fa in main at buging.c:8
2    breakpoint   keep y  0x00000000004004cb in getSum at buging.c:3
Copy after login

断点信息中每一项的信息:

1.Num 列代表断点编号,该编号可以作为 delete/enalbe/disable 等控制断点命令的参数
2.Type 列代表断点类型,一般为 breakpoint
3.Disp 列代表断点被命中后,该断点保留(keep)、删除(del)还是关闭(dis)
4.Enb 列代表该断点是 enable(y) 还是 disable(n)
5.Address 列代表该断点处虚拟内存的地址
6.What 列代表该断点在源文件中的信息

// 删除断点
// delete +n 或 d +n
// 如果不加数字则删除全部的断点
(gdb) d 2
(gdb) info breakpoints 
Num   Type      Disp Enb Address      What
1    breakpoint   keep y  0x00000000004004fa in main at buging.c:8
Copy after login

关闭和启用断点:

有时候,我们会遇到这种情况:有些断点可能暂时不用但又不想删除,那么可以disable.

disable +n //关闭断点
enable +n  //启动断点
Copy after login

GDB单步调试

以本文开头的buging.c文件为例,进行单步调试。

// 进入gdb
[yqtao@localhost linux]$ gdb buging
// 在main()函数处设断点
(gdb) info breakpoints 
// 运行程序
(gdb) run
Breakpoint 1, main () at buging.c:9
9        int res=getSum(100);
Copy after login

注意:我们要进入函数要是用step(s),而单步执行next(n),next是不进入函数内部的,要区分两者的关系。

(gdb) step
getSum (n=100) at buging.c:4
4        for (i=1;i<=n;i++)
// 查看代码
(gdb) l
1    #include <stdio.h>
2    int getSum(int n) {
3        int sum=0,i;
4        for (i=1;i<=n;i++)
5            sum+=i;
6        return sum;
7    }
8    int main(){
9        int res=getSum(100);
10       printf("1+2+...+100=%d\n",res);
// 下一步
(gdb) s
5            sum+=i;
// 打印sum的值
// 看到sum是为赋初值的
(gdb) p sum
$1 = 4195633
// info locals
//打印当前断点处所在函数的所有局部变量的值,不包括函数参数。
(gdb) info locals
sum = 4195635
i = 3
Copy after login

其余的一些命令:

finish

执行程序到当前函数结束

continue

执行程序到下个断点

until

until N,执行程序到源代码的某一行

相关推荐:

用GDB调试nginx

基于Linux调试工具strace与gdb的常用命令总结_PHP教程

Linux+Eclipse+GDB调试PostgreSQL源码

The above is the detailed content of Linux debugging tool GDB usage tutorial. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

BITGet official website installation (2025 beginner's guide) BITGet official website installation (2025 beginner's guide) Feb 21, 2025 pm 08:42 PM

BITGet is a cryptocurrency exchange that provides a variety of trading services including spot trading, contract trading and derivatives. Founded in 2018, the exchange is headquartered in Singapore and is committed to providing users with a safe and reliable trading platform. BITGet offers a variety of trading pairs, including BTC/USDT, ETH/USDT and XRP/USDT. Additionally, the exchange has a reputation for security and liquidity and offers a variety of features such as premium order types, leveraged trading and 24/7 customer support.

Get the gate.io installation package for free Get the gate.io installation package for free Feb 21, 2025 pm 08:21 PM

Gate.io is a popular cryptocurrency exchange that users can use by downloading its installation package and installing it on their devices. The steps to obtain the installation package are as follows: Visit the official website of Gate.io, click "Download", select the corresponding operating system (Windows, Mac or Linux), and download the installation package to your computer. It is recommended to temporarily disable antivirus software or firewall during installation to ensure smooth installation. After completion, the user needs to create a Gate.io account to start using it.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Ouyi okx installation package is directly included Ouyi okx installation package is directly included Feb 21, 2025 pm 08:00 PM

Ouyi OKX, the world's leading digital asset exchange, has now launched an official installation package to provide a safe and convenient trading experience. The OKX installation package of Ouyi does not need to be accessed through a browser. It can directly install independent applications on the device, creating a stable and efficient trading platform for users. The installation process is simple and easy to understand. Users only need to download the latest version of the installation package and follow the prompts to complete the installation step by step.

Why does an error occur when installing an extension using PECL in a Docker environment? How to solve it? Why does an error occur when installing an extension using PECL in a Docker environment? How to solve it? Apr 01, 2025 pm 03:06 PM

Causes and solutions for errors when using PECL to install extensions in Docker environment When using Docker environment, we often encounter some headaches...

How to efficiently integrate Node.js or Python services under LAMP architecture? How to efficiently integrate Node.js or Python services under LAMP architecture? Apr 01, 2025 pm 02:48 PM

Many website developers face the problem of integrating Node.js or Python services under the LAMP architecture: the existing LAMP (Linux Apache MySQL PHP) architecture website needs...

How to transfer coins to Binance? How to mention the crypto assets in HTX to the Binance platform? How to transfer coins to Binance? How to mention the crypto assets in HTX to the Binance platform? Mar 03, 2025 pm 08:51 PM

Huobi to Binance Transfer Guide: Safe and conveniently transfer your crypto assets Many investors use Huobi and Binance at the same time. This article will guide you how to safely transfer crypto assets on Huobi (HTX), such as TRUMP and USDT, to the Binance platform. Binance is popular for its high security, rich currency and trading pairs, and world-leading trading volume. Binance Exchange’s advantages: the world’s number one trading volume, accounting for 50% of the global market; transparent reserve assets are mainly mainstream stablecoins such as Bitcoin, Ethereum and USDT; it has effectively avoided the potential risks of the US SEC and is one of the most stable and reliable exchanges at present. This tutorial will take TRUMP and USDT as examples

See all articles