本文主要介绍了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<p>如果要调试程序,则在进行gcc编译的时候要加上-g参数 <br></p> <p>gcc -g -o bugging bugging.c</p> <p>进入gdb:</p> <p>gdb buging</p> <p>部分gdb命令:</p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/054/025/d0002bdecbd5b625a3f93a1354ce067f-0.png?x-oss-process=image/resize,p_40" class="lazy" id="theimg" alt=""></p> <pre class="brush:php;toolbar:false">// 查看源码 // 查看第几行附近 // 可简写成l 2 (gdb) list 2 1 #include <stdio.h> 2 int getSum(int n) { 3 int sum=0,i; 4 for (i=1;i<p>断点信息中每一项的信息:</p> <p>1.Num 列代表断点编号,该编号可以作为 delete/enalbe/disable 等控制断点命令的参数<br>2.Type 列代表断点类型,一般为 breakpoint<br>3.Disp 列代表断点被命中后,该断点保留(keep)、删除(del)还是关闭(dis)<br>4.Enb 列代表该断点是 enable(y) 还是 disable(n)<br>5.Address 列代表该断点处虚拟内存的地址<br>6.What 列代表该断点在源文件中的信息</p> <pre class="brush:php;toolbar:false">// 删除断点 // 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
关闭和启用断点:
有时候,我们会遇到这种情况:有些断点可能暂时不用但又不想删除,那么可以disable.
disable +n //关闭断点 enable +n //启动断点
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);
注意:我们要进入函数要是用step(s),而单步执行next(n),next是不进入函数内部的,要区分两者的关系。
(gdb) step getSum (n=100) at buging.c:4 4 for (i=1;i 2 int getSum(int n) { 3 int sum=0,i; 4 for (i=1;i<p>其余的一些命令:</p><p>finish <br></p><p>执行程序到当前函数结束</p><p>continue <br></p><p>执行程序到下个断点</p><p>until <br></p><p>until N,执行程序到源代码的某一行</p><p>相关推荐:</p><p><a href="http://www.php.cn/php-weizijiaocheng-331774.html" target="_self">用GDB调试nginx</a></p><p><a href="http://www.php.cn/php-weizijiaocheng-307818.html" target="_self">基于Linux调试工具strace与gdb的常用命令总结_PHP教程</a></p><p><a href="http://www.php.cn/mysql-tutorials-146629.html" target="_self">Linux+Eclipse+GDB调试PostgreSQL源码</a></p>
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!

MaintenanceModeinLinuxisaspecialbootenvironmentforcriticalsystemmaintenancetasks.Itallowsadministratorstoperformtaskslikeresettingpasswords,repairingfilesystems,andrecoveringfrombootfailuresinaminimalenvironment.ToenterMaintenanceMode,interrupttheboo

The core components of Linux include kernel, file system, shell, user and kernel space, device drivers, and performance optimization and best practices. 1) The kernel is the core of the system, managing hardware, memory and processes. 2) The file system organizes data and supports multiple types such as ext4, Btrfs and XFS. 3) Shell is the command center for users to interact with the system and supports scripting. 4) Separate user space from kernel space to ensure system stability. 5) The device driver connects the hardware to the operating system. 6) Performance optimization includes tuning system configuration and following best practices.

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

Linux maintenance mode can be entered through the GRUB menu. The specific steps are: 1) Select the kernel in the GRUB menu and press 'e' to edit, 2) Add 'single' or '1' at the end of the 'linux' line, 3) Press Ctrl X to start. Maintenance mode provides a secure environment for tasks such as system repair, password reset and system upgrade.

The steps to enter Linux recovery mode are: 1. Restart the system and press the specific key to enter the GRUB menu; 2. Select the option with (recoverymode); 3. Select the operation in the recovery mode menu, such as fsck or root. Recovery mode allows you to start the system in single-user mode, perform file system checks and repairs, edit configuration files, and other operations to help solve system problems.

The core components of Linux include the kernel, file system, shell and common tools. 1. The kernel manages hardware resources and provides basic services. 2. The file system organizes and stores data. 3. Shell is the interface for users to interact with the system. 4. Common tools help complete daily tasks.

The basic structure of Linux includes the kernel, file system, and shell. 1) Kernel management hardware resources and use uname-r to view the version. 2) The EXT4 file system supports large files and logs and is created using mkfs.ext4. 3) Shell provides command line interaction such as Bash, and lists files using ls-l.

The key steps in Linux system management and maintenance include: 1) Master the basic knowledge, such as file system structure and user management; 2) Carry out system monitoring and resource management, use top, htop and other tools; 3) Use system logs to troubleshoot, use journalctl and other tools; 4) Write automated scripts and task scheduling, use cron tools; 5) implement security management and protection, configure firewalls through iptables; 6) Carry out performance optimization and best practices, adjust kernel parameters and develop good habits.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools