这里介绍对文件first.c的基本GDB调试操作,只有部分命令,只是一个示例,运行环境为装有gcc编译器和gdb调试器的Linux环境,基本GDB调试命令如下表:

命令                描述
backtrace(或bt)  查看各级函数调用及参数
finish            连续运行到当前函数返回为止,然后停下来等待命令
frame(或f)       帧编号 选择栈帧
info(或i)        locals 查看当前栈帧局部变量的值
list(或l)        列出源代码,接着上次的位置往下列,每次列10行
list 行号        列出从第几行开始的源代码
list 函数名        列出某个函数的源代码
next(或n)        执行下一行语句
print(或p)    打印表达式的值,通过表达式可以修改变量的值或者调用函数
set var            修改变量的值
start            开始执行程序,停在main函数第一行语句前面等待命令
step(或s)        执行下一行语句,单步调试用这个
x                查看变量内存
b                后面加行号表示在该行打断点
c                继续运行到下一个断点
r                运行
quit            退出调试
vi                后面加文件名可以打开文件用vim进行编辑

disass (/m) main 查看main函数的汇编

以下是示例的全过程:

[LLL@localhost LLL]$ cd test //到目标目录
[LLL@localhost test]$ ls
first.c server.c sock.c
[LLL@localhost test]$ cat first.c //查看first.c内容
#include <stdio.h> int add(int m, int n)
{
int result;
m = 10*m;
n = 100*n;
result = m+n;
return result;
} int main()
{
int a = 10;
int b = 100;
int i = 0;
printf("%d",i);
i = add(a,b);
b = add(b,a);
printf("%d %d %d",a,b,i);
return 0;
}
[LLL@localhost test]$ gcc -g first.c -o first //gcc 编译
[LLL@localhost test]$ ls
first first.c server.c sock.c
[LLL@localhost test]$ ./first //运行目标文件
010 2000 10100[LLL@localhost test]$ ls
first first.c server.c sock.c
[LLL@localhost test]$ vi first.c //由于运行看出没有换行,因此用vim编辑修改,打印中加入了换行
[LLL@localhost test]$ rm -f first
[LLL@localhost test]$ ls
first.c server.c sock.c
[LLL@localhost test]$ gcc -g first.c -o first
[LLL@localhost test]$ ./first //换行后的结果
0
10 2000 10100
[LLL@localhost test]$ gdb first //启动GDB调试
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-100.el7
Copyright (C) 2013 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-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /data/ManyCore/software/LLL/test/first...done.
(gdb) l //列出内容
5 int result;
6 m = 10*m;
7 n = 100*n;
8 result = m+n;
9 return result;
10 }
11
12 int main()
13 {
14 int a = 10;
(gdb) //Enter翻页
15 int b = 100;
16 int i = 0;
17 printf("%d\n",i);
18 i = add(a,b);
19 b = add(b,a);
20 printf("%d %d %d\n",a,b,i);
21 return 0;
22 }
(gdb) b 17 //在17行打断点
Breakpoint 1 at 0x40057c: file first.c, line 17.
(gdb) start //开始运行程序
Temporary breakpoint 2 at 0x400567: file first.c, line 14.
Starting program: /data/ManyCore/software/LLL/test/first
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1". Temporary breakpoint 2, main () at first.c:14
14 int a = 10; //进入main函数的首行
Missing separate debuginfos, use: debuginfo-install glibc-2.17-196.el7_4.2.x86_64
(gdb) n //下一步,遇到子函数不进入,当做一行执行
15 int b = 100;
(gdb) s //下一步,s可以进入子函数
16 int i = 0;
(gdb) s Breakpoint 1, main () at first.c:17
17 printf("%d\n",i);
(gdb) s
0
18 i = add(a,b);
(gdb) b 6 //在6行打断点
Breakpoint 3 at 0x400537: file first.c, line 6.
(gdb) c //运行到下一个断点
Continuing. Breakpoint 3, add (m=10, n=100) at first.c:6
6 m = 10*m;
(gdb) c
Continuing. Breakpoint 3, add (m=100, n=10) at first.c:6
6 m = 10*m;
(gdb) i locals //查看当栈帧中的局部变量(这里的add函数)
result = 10100
(gdb) s
7 n = 100*n;
(gdb) s
8 result = m+n;
(gdb) s
9 return result;
(gdb) s
10 }
(gdb) s
main () at first.c:20
20 printf("%d %d %d\n",a,b,i);
(gdb) i locals //查看当栈帧中的局部变量(这里的main函数)
a = 10
b = 2000
i = 10100
(gdb) p a //查看变量
$1 = 10
(gdb) p b
$2 = 2000
(gdb) p &a //查看变量的地址
$3 = (int *) 0x7fffffffe3cc
(gdb) x 0x7ffffffe3cc
0x7ffffffe3cc: Cannot access memory at address 0x7ffffffe3cc
(gdb) x 0x7fffffffe3cc //查看内存的值
0x7fffffffe3cc: 0x0000000a
(gdb) r //运行程序
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /data/ManyCore/software/LLL/test/first
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1". Breakpoint 1, main () at first.c:17
17 printf("%d\n",i);
(gdb) c
Continuing.
0 Breakpoint 3, add (m=10, n=100) at first.c:6
6 m = 10*m;
(gdb) c
Continuing. Breakpoint 3, add (m=100, n=10) at first.c:6
6 m = 10*m;
(gdb) c
Continuing.
10 2000 10100
[Inferior 1 (process 118146) exited normally]
(gdb)

  

Linux下GDB调试简单示例的更多相关文章

  1. Linux知识(5)----LINUX下GDB调试

    命令 解释 示例   file 加载被调试的可执行程序文件.因为一般都在被调试程序所在目录下执行GDB,因而文本名不需要带路径. (gdb) file gdb-sample     r c Run的简 ...

  2. 一文入门Linux下gdb调试(二)

    作者:良知犹存 转载授权以及围观:欢迎添加微信号:Conscience_Remains 总述     今天我们介绍一下core dump文件,Core dump叫做核心转储,它是进程运行时在突然崩溃的 ...

  3. 一文入门Linux下gdb调试(一)

    作者:良知犹存 转载授权以及围观:欢迎添加微信号:Conscience_Remains 总述 在window下我们习惯了IDE的各种调试按钮,说实话确实挺方便的,但到了Linux下,没有那么多的IDE ...

  4. Linux下GDB调试

    GDB 是一个强大的命令行调试工具.大家知道命令行的强大就是在于,其可以形成执行 序列,形成脚本.UNIX 下的软件全是命令行的,这给程序开发提供了极大的便利,命令行 软件的优势在于, 他们可以非常容 ...

  5. Linux下gdb调试(tui)

    1 处于TUI模式的GDB 为了以TUI模式运行GDB,可以在调用GDB时在命令行上指定-tui选项,或者处于非TUI模式时在GDB中使用Ctrl+X+A组合键.如果当前处于TUI模式,后一种命令方式 ...

  6. Linux下GDB调试C/C++

    首先先编译程序并生成调试符号: gcc -g -c main.cpp gcc -o exefile main.o 以上的exefile为可执行程序的文件名 然后: gdb exefile 可以开始gd ...

  7. (十五)linux下gdb调试

    一.gdb常用命令: 命令 描述 backtrace(或bt) 查看各级函数调用及参数 finish 连续运行到当前函数返回为止,然后停下来等待命令 frame(或f) 帧编号 选择栈帧 info(或 ...

  8. linux下gdb调试方法与技巧整理

    参考博客:  https://blog.csdn.net/niyaozuozuihao/article/details/91802994 1.运行命令run:简记为 r ,其作用是运行程序,当遇到断点 ...

  9. 25. Linux下gdb调试

    1.什么是core文件?有问题的程序运行后,产生"段错误 (核心已转储)"时生成的具有堆栈信息和调试信息的文件. 编译时需要加 -g 选项使程序生成调试信息: gcc -g cor ...

随机推荐

  1. CentOS之文档的压缩与打包

    .rar压缩文件linux中不识别,.zip在windows和Linux中动能使用. .gz:由gzip压缩工具压缩的文件 .bz2:bzip2压缩工具压缩的文件 .tar:由tar打包程序打包的文件 ...

  2. Docker 系列01: Centos7.3 上安装docker

    Docker从1.13版本之后采用时间线的方式作为版本号,分为社区版CE和企业版EE. 社区版是免费提供给个人开发者和小型团体使用的,企业版会提供额外的收费服务,比如经过官方测试认证过的基础设施.容器 ...

  3. delphi EncdDecd.pas单元中Encoding方法出现#$D#$A的解决方法

    例如: s:= 'http://detail.tmall.com/item.htm?id=45545826531&abbucket=_AB-M129_B17&acm=03130.100 ...

  4. .NET 里delegate和event的区别

    最近一朋友找工作面试遇到这么个题目,正好我也对此有点模糊,遂进行了一番资料查询,找到了这个文章: http://www.cnblogs.com/chengxingliang/archive/2013/ ...

  5. spark2.3.0 配置spark sql 操作hive

    spark可以通过读取hive的元数据来兼容hive,读取hive的表数据,然后在spark引擎中进行sql统计分析,从而,通过spark sql与hive结合实现数据分析将成为一种最佳实践.配置步骤 ...

  6. window系统更新导致很多服务出错

    window7,win10,window server各版本系统中,经常会出现下载完成更新补丁后要求重启更新,此时很可能会出现很多服务失效的莫名其妙的问题,比如数据库连不上,IIS某功能不好使等等问题 ...

  7. vs2017配置文件目录

    C:\Users\Administrator\AppData\Local\Microsoft\VisualStudio\15.0_6d0a0a42

  8. datagrid行内编辑时为datetimebox

    $.extend($.fn.datagrid.defaults.editors, { datetimebox: {// datetimebox就是你要自定义editor的名称 init: functi ...

  9. 关于python无法显示中文的问题:SyntaxError: Non-ASCII character '\xe4' in file test.py on line 3, but no encoding declared。

    [已解决]关于python无法显示中文的问题:SyntaxError: Non-ASCII character '\xe4' in file test.py on line 3, but no enc ...

  10. 【Linux】【JDK】常用命令使用集和裸机配置JDK步骤。

    使用Zstack创建完成后的linux服务器,使用SSH登录后,就是一下图,可以查看当前路径下的所有文件. 1.常用的命令: 列出当前文件夹下内容:ll 查看目录中的文件 :ls 创建文件夹:mkdi ...