linux c编程:gdb的使用
首先用一个简单的打印字符的程序来做下示例
#include<stdio.h>
#include<string.h>
void main()
{
int i=0;
char a[4]="abc";
for(i=0;i<strlen(a);i++)
{
printf("%c",a[i]);
}
}
要想用gdb调试文件,有两种方法 1 gcc生成可执行的文件, gdb 直接调试文件2 直接进入gdb 然后输入file 可执行文件
方法一:
root@zhf-linux:/home/zhf/zhf/c_prj# gcc -g -o test1 test1.c
root@zhf-linux:/home/zhf/zhf/c_prj# gdb test1
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 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 "i686-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 test1...(no debugging symbols found)...done.
(gdb) run
Starting program: /home/zhf/zhf/c_prj/test1
abc[Inferior 1 (process 3802) exited normally]
方法二:
root@zhf-linux:/home/zhf/zhf/c_prj#
gdb
GNU
gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright
(C) 2016 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 "i686-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".
(gdb)
file test1
Reading
symbols from test1...(no debugging symbols found)...done.
(gdb)
run
Starting
program: /home/zhf/zhf/c_prj/test1
abc[Inferior
1 (process 3818) exited normally]
进入gdb后,我们来看下有哪些功能可供调试。在程序调试中,断点是必不可少的。我们先来看下如何设置断点。break命令就是用来设置断点用的。
Break
行号 运行到某行停止
break
函数名称 运行到某个函数停止运行
break
行号/函数名称
if
条件
上面的程序中我们在第7行也就是for循环的地方做一个断点。disable
断点号可以停止断点。比如disable
1就是停止掉第一个断点
(gdb)
break 7
Breakpoint
1 at 0x80484b7: file test1.c, line 7.
然后执行run,程序执行到第7行的时候停止
(gdb)
run
Starting
program: /home/zhf/zhf/c_prj/test1
Breakpoint
1, main () at test1.c:7
7 char
a[4]="abc";
进入断点后我们可以通过print命令来打印具体的变量,来查看变量的值。其中$1代表的是第几次使用print命令。
(gdb)
print i
$1
= 0
(gdb)
print a[0]
$2
= 97 'a'
要在断点处继续执行需要执行next命令,与此同时我们还可以设置display
变量来查看每次运行后变量的
(gdb)
display i
1:
i = 0
(gdb)
next
9 printf("%c",a[i]);
1:
i = 0
(gdb)
next
7 for(i=0;i<strlen(a);i++)
1:
i = 0
(gdb)
next
9 printf("%c",a[i]);
1:
i = 1
(gdb)
next
7 for(i=0;i<strlen(a);i++)
1:
i = 1
(gdb)
next
9 printf("%c",a[i]);
1:
i = 2
如果不想单步的执行,可以运行continue命令运行到第二个断点处停止,如果没有第二个断点,则直接运行到程序结尾
通过whatis
变量名可以查看变量或者函数的类型:
(gdb)
whatis i
type
= int
在运行的时候还可以实时的改变变量的值来控制程序或者循环
(gdb)
set var i=0
(gdb)
print i
$7
= 0
另外还可以设置观察窗口,当变量发生变化的时候,程序立即停止,并显示变化的值。命令是watch
这里设置当i>0的时候停止,并显示出old
value和new
value
(gdb)
watch i>0
Hardware
watchpoint 5: i>0
(gdb)
continue
Continuing.
Hardware
watchpoint 3: i
Old
value = 0
New
value = 1
Hardware
watchpoint 5: i>0
Old
value = 0
New
value = 1
0x080484ec
in main () at test1.c:7
7 for(i=0;i<strlen(a);i++)
另外还有rwatch:当表达式值被读取的时候,就停止程序。awatch:用于当表达式的值被读或被写的时候,停止程序
在运行过程中还可以实时的查看源代码。采用list命令。
(gdb)
list
4 {
5 int
i=0;
6 char
a[4]="abc";
7 for(i=0;i<strlen(a);i++)
8 {
9 printf("%c",a[i]);
10 }
11 }
(gdb)
list 4,6
4 {
5 int
i=0;
6 char
a[4]="abc";
list
<function> 列出某个函数的代码
list
<first,last>列出first到last行的代码
list
<,last>列出当前行到last行的代码
list
<filename,n> 列出文件的第n行代码
list
<filename,function>列出文件名函数名为function的代码
linux c编程:gdb的使用的更多相关文章
- linux C编程 gdb的使用
linux C编程 gdb的使用 通常来说,gdb是linux在安装时自带的,在命令行键入"gdb"字符并按回车键会启动gdb调试环境. 1.gdb的基本命令 命令 说明 file ...
- gdb笔记 ---《Linux.C编程一站式学习》
gdb笔记 ---<Linux.C编程一站式学习> 单步执行和跟踪函数调用 函数调试实例 #include <stdio.h> int add_range(int low, i ...
- Linux环境编程相关的文章
Linux环境编程相关的文章 好几年没有接触Linux环境下编程了,好多东西都有点生疏了.趁着现在有空打算把相关的一些技能重拾一下,顺手写一些相关的文章加深印象. 因为不是写书,也受到许多外部因素限制 ...
- Linux C编程之一:Linux下c语言的开发环境
---恢复内容开始--- 今天开始根据Linux C编程相关视频的学习所做的笔记,希望能一直坚持下去... 1.开发环境的构成 编辑器:VI: 编译器:选择GNU C/C++编译器gcc: 调试器: ...
- Linux多任务编程之七:Linux守护进程及其基础实验(转)
来源:CSDN 作者:王文松 转自Linux公社 ------------------------------------------------------------------------- ...
- Linux 系统编程 学习:03-进程间通信1:Unix IPC(2)信号
Linux 系统编程 学习:03-进程间通信1:Unix IPC(2)信号 背景 上一讲我们介绍了Unix IPC中的2种管道. 回顾一下上一讲的介绍,IPC的方式通常有: Unix IPC包括:管道 ...
- [Linux] Linux C编程一站式学习 Part.3
Linux系统编程 文件与I/O C标准I/O库函数与Unbuffered I/O函数 C标准I/O库函数printf().putchar().fputs(),会在用户空间开辟I/O缓冲区 系统函数o ...
- 【深入浅出Linux网络编程】 "开篇 -- 知其然,知其所以然"
[深入浅出Linux网络编程]是一个连载博客,内容源于本人的工作经验,旨在给读者提供靠谱高效的学习途径,不必在零散的互联网资源中浪费精力,快速的掌握Linux网络编程. 连载包含4篇,会陆续编写发出, ...
- 【linux草鞋应用编程系列】_5_ Linux网络编程
一.网络通信简介 第一部分内容,暂时没法描述,内容实在太多,待后续专门的系列文章. 二.linux网络通信 在linux中继承了Unix下“一切皆文件”的思想, 在linux中要实现网 ...
- 学习linux/unix编程方法的建议(转)
假设你是计算机科班出身,计算机系的基本课程如数据结构.操作系统.体系结构.编译原理.计算机网络你全修过 我想大概可以分为4个阶段,水平从低到高从安装使用=>linux常用命令=>linux ...
随机推荐
- Linux命令大总结
from http://elain.blog.51cto.com/3339379/623310 Linux命令大总结------------------------------------------ ...
- css 让两个div重叠
做网页的时候在div里放了一个别的网页的天气插件,但是点击了会跳到广告页面的,想去网上找个禁止div点击的方法,可是发现没有,用了js的方法好像也没有成功,后来觉得还是用两个层重叠的方法来阻止点击,虽 ...
- ajax跨域解决办法
在使用jquery的ajax作请求时,http://127.0.0.1:8080,类似这样的一个本地请求,会产生跨域问题, 解决办法一: jsonp: var url= "http://12 ...
- MapWindowPoints
中文名 MapWindowPoints Windows CE 1.0及以上版本 头文件 winuser.h 库文件 user32.lib MapWindowPoints函数把相对于一个窗口的坐标空间的 ...
- dubbo服务接口开发者必备调试利器,你值得拥有
dubbo服务接口开发者必备调试利器,你值得拥有 学习了:https://my.oschina.net/vboxtop/blog/1524290 找到了:http://www.vbox.top/?fr ...
- 基于SNMP的交换机入侵的内网渗透
前言:局域网在管理中常常使用SNMP协议来进行设备的管理和监控,而SNMP的弱点也成为了我们此次渗透的关键. 使用SNMP管理设备只需要一个community string,而这个所谓的密码经常采用默 ...
- struts2学习笔记之表单标签的详解:s:checkbox/radio/select/optiontransferselect/doubleselect/combobox
struts2中的表单标签都是以s标签的方式定义的,同时,struts2为所有标签都提供了一个模板,C:\Users\180172\Desktop\struts2-core-2.2.1.1.jar\t ...
- HDU 3657 Game(取数 最小割)经典
Game Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- 【Python数据分析】IPython快捷键
命令 说明 CTRL+P(或向上箭头) 后向搜索命令历史中以当前输入的文本开头的命令 CTRL+N(或向下箭头) ...
- iphone手机分辨率--持久维护
6.5英寸 —— 1242 x 2688 px —— Xs Max 6.1英寸 —— 828 x 1792 px —— XR 5.8英寸 —— 1125 x 2436 px —— X/Xs 5.5英寸 ...