从Windows转到Linux下已经有一段时间了,每次刷算法题碰到问题需要调试的时候,就分分钟想关机,切换到Windows上调试。于是,花了一点时间来搜索一下Linux下常见的调试工具,这不搜不知道,一搜吓一跳,居然差点错过了这么好的调试利器GDB。上手十分简单,几分钟就可以开开心心调试你的代码了。
GDB概述
GDB是一个由GNU开源组织发布的、UNIX/LINUX操作系统下的、基于命令行的、功能强大的程序调试工具。相比于VS里面的图形化调试工具,其功能更加强大。
GDB安装
在终端下运行如下代码即可安装GDB调试器:
sudo apt-get install gdb<!-- more -->检查安装是否成功,输入gdb -version,会输出如下代码:
GNU gdb (Ubuntu 7.10-1ubuntu2) 7.10Copyright (C) 2015 Free Software Foundation, Inc.License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>This is free software: you are free to change and redistribute it.There is NO WARRANTY, to the extent permitted by law.Type "show copying"and "show warranty" for details.This GDB was configured as "x86_64-linux-gnu".Type "show configuration" for configuration details.GDB调试步骤
调试用例
写了一个简单实现两个数相加的程序,来示范gdb调试的相关步骤:
#include <stdio.h>int add(int a , int b){return a+b;}int main(){int m,n;scanf("%d%d",&m,&n);int sum = add(m , n);return 0;}编译生成
一般在编译程序的时候都是直接生成release可执行文件,
gcc -o add add.c如果需要调试的话,应该编译成debug版本,此时,只用在编译选项中加入 -g,如下:
gcc -g add.c -o add进入调试状态
进入gdb调试状态有两种,第一种直接在gdb命令后面加上编译好的文件名:
gdb add另一种方式就是输入gdb,然后通过使用file命令来打开待调试的文件:
gdbfile add终端界面会显示如下信息:
GNU gdb (Ubuntu 7.10-1ubuntu2) 7.10Copyright (C) 2015 Free Software Foundation, Inc.License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>This is free software: you are free to change and redistribute it.There is NO WARRANTY, to the extent permitted by law.Type "show copying"and "show warranty" for details.This GDB was configured as "x86_64-linux-gnu".Type "show configuration" for configuration details.For bug reporting instructions, please see:<http://www.gnu.org/software/gdb/bugs/>.Find the GDB manual and other documentation resources online at:<http://www.gnu.org/software/gdb/documentation/>.For help, type "help".Type "apropos word" to search for commands related to "word"...Reading symbols from add...done.(gdb) 常用调试命令
| 常用命令 | 简写命令 | 用法 | 说明 |
|---|
| file | f | file filename | 在gdb中载入某可执行文件 |
| list | l | list [开始,结束] | 列出文件的代码清单,支持指定行号 |
| print | p | p 变量名 | 答应出变量的值 |
| break | b | break 行号or函数名 | 在指定行号或函数上设置断点 |
| clear | cl | clear 行号or函数名 | 删除指定行号或函数上的断点 |
| continue | c | continue [开始,结束] | 从断点处继续运行 |
| next | n | next | 运行到下一行 |
| step | s | step | 单步调试 |
| run | r | run | 执行程序 |
实际调试
列出代码清单
(gdb) l1 #include <stdio.h>2 3 int add(int a , int b){4 return a+b;5 }6 int main(){7 int m,n;8 scanf("%d%d",&m,&n);9 int sum = add(m , n);10return 0;加断点
在main函数和程序的第4行加两个断点。
(gdb) break mainBreakpoint 1 at 0x4005e2: file add.c, line 6.(gdb) break 4Breakpoint 2 at 0x4005d0: file add.c, line 4.运行
(gdb) runStarting program: /home/Documents/add Breakpoint 1, main () at add.c:66 int main(){//这里显示运行到的位置,停在了第一个断点位置mainnext下一行
(gdb) n8 scanf("%d%d",&m,&n);//下一行(gdb) n //下一行需要输入两个数字5 49 int sum = add(m , n); //执行到add函数位置step单步
(gdb) sBreakpoint 2, add (a=5, b=4) at add.c:4 //单步调试恰好到达第二个断点的位置4 return a+b;打印变量的值
(gdb) p a// 打印a的值$1 = 5(gdb) p b//打印b的值$2 = 4清除断点
清楚指定行的断点,不加表示清除所有的断点
(gdb) break 9Breakpoint 5 at 0x40060b: file add.c, line 9.//加一个断点(gdb) cl 9Deleted breakpoint 5//测试清除从断点处继续运行
(gdb) cContinuing.[Inferior 1 (process 8168) exited normally] //程序正常退出,调试完毕高级命令
gdb还有很多高级用法,如查看堆栈列表,分割窗口等,这里就不一一测试了。
查询运行信息
- where/bt:查看当前运行的堆栈列表
- up/down:改变堆栈显示的深度
- set args:参数:指定运行时的参数
- show args:查看设置好的参数
- info program: 来查看程序的是否在运行,进程号,被暂停的原因。
分割窗口
- layout:用于分割窗口,可以一边查看代码,一边测试
- layout src:显示源代码窗口
- layout asm:显示反汇编窗口
- layout regs:显示源代码/反汇编和CPU寄存器窗口
- layout split:显示源代码和反汇编窗口
- Ctrl + L:刷新窗口
后记
学习gdb调试只需要几分钟就能上手,但是用处超级大!!受益匪浅啊!这就是所谓的低投入高回报,还在等什么,赶紧行动吧。GDB调试程序用法 http://www.linuxidc.com/Linux/2013-06/86044.htmGDB+GDBserver无源码调试Android 动态链接库的技巧 http://www.linuxidc.com/Linux/2013-06/85936.htm使用hello-gl2建立ndk-GDB环境(有源码和无源码调试环境) http://www.linuxidc.com/Linux/2013-06/85935.htm在Ubuntu上用GDB调试printf源码 http://www.linuxidc.com/Linux/2013-03/80346.htmLinux下用GDB调试可加载模块 http://www.linuxidc.com/Linux/2013-01/77969.htmUbuntu下使用GDB断点Go程序 http://www.linuxidc.com/Linux/2012-06/62941.htm使用GDB命令行调试器调试C/C++程序 http://www.linuxidc.com/Linux/2014-11/109845.htmGDB调试命令总结 http://www.linuxidc.com/Linux/2016-08/133988.htm
GDB 的详细介绍:请点这里
GDB 的下载地址:请点这里
本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-09/135168.htm