嵌入式Linux(三):GDB程序调试

前言

搞嵌入式Linux应用开发,GDB调试是必备技能,这里,简单记录一下GDB一些常用的调试方式。

GDB can do four main kinds of things (plus other things in support of these) to help you catch bugs in the act:

· Start your program, specifying anything that might affect its behavior.

· Make your program stop on specified conditions.

· Examine what has happened, when your program has stopped.

· Change things in your program, so you can experiment with correcting the effects of one bug and go on to learn about another.

(一)常用指令

进入GDB

要对C程序进行代码调试时,编译前,编译的命令是

gcc -g hello.c -o hello
gdb hello

break [file:]function

 Set a breakpoint at function (in file).

run [arglist]

 Start your program (with arglist, if specified).

print expr

 Display the value of an expression.

c

 Continue running your program (after stopping, e.g. at a breakpoint).

next (会把函数当成一行代码)

 Execute next program line (after stopping); step over any function calls in the line.

list [file:]function

 type the text of the program in the vicinity of where it is presently stopped.

step (会进入函数)

 Execute next program line (after stopping); step into any function calls in the line.

help [name]

 Show information about GDB command name, or general information about using GDB.

quit

 Exit from GDB.

(二)调试段错误

下面是一段会出现段错误的代码,我的ubuntu18.04没有生成core文件
在**~/bashrc** 最后添加 ulimit -c unlimited 也没有生成。
直接跑gdb,

int main(int args,char **argv){
	int *temp = NULL;
	*temp = 123;
	return 0;
}

请添加图片描述
若生成core,可以用下面命令调试

gdb test core

(三)调试进程

查看进程的命令

//查看程序test的进程
ps -ef | grep test

1.调试正在运行的进程

gdb test 9078
gdb -p 9078
gdb test -p 9078

2.调试父子进程

//显示当前所有进程
show follow-folk-mode
//设置调试父进程或子进程
set follow-fork-mode [parent|child]

3.挂起指定进程

show detach-on-fork -- Show whether gdb will detach the child of a fork
set detach-on-fork -- Set whether gdb will detach the child of a fork

4.调试多个进程

inferior <num>
Use this command to switch between inferiors.
The new inferior ID must be currently known.

info inferior
show IDs of specified inferiors (all inferiors if no argument).

5.调试多线程

编译使用到线程代码时,需要引入外部库

gcc -g test.c -o test -lpthread
//查看线程
info threads
//跳转线程
thread <num>
//挂起线程
set scheduler-locking
Set mode for locking scheduler during execution.
off    == no locking (threads may preempt at any time)
on     == full locking (no thread except the current thread may run)
          This applies to both normal execution and replay mode.
step   == scheduler locked during stepping commands (step, next, stepi, nexti).
          In this mode, other threads may run during other commands.
          This applies to both normal execution and replay mode.
replay == scheduler locked in replay mode and unlocked during normal execution.

6.一些技巧

//设置日记输出
set loggin on
//调用shell命令
shell ls

后言

上面笔记参考下面视频:
https://www.bilibili.com/video/BV18S4y1J725?p=1&vd_source=9385b7f8c739b9e3ef3f21ddaebd2eb9

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值