嵌入式Linux应用调试【1】—— strace、gdb

1 strace

strace可记录执行过程中的系统调用、接收到的信号。
(1)移植

tar -xjf strace-4.5.15.tar.bz2
cd strace-4.5.15/
patch -p1 <../strace-fix-arm-bad-syscall.patch  //在arm
./configure --host=arm-linux  CC=arm-linux-gcc         
make                      //生成strace命令文件

将生成的strace放入到开发板,测试

strace  -o log.txt  ./led_text 1 on

2 GDB

2.1 安装gdb

在虚拟机中安装gdb

tar xjf gdb-7.4.tar.bz2                  
cd gdb-7.4/                              
./configure --target=arm-linux         #GDB需要在pc本机里运行,并调试开发板里的应用程序,所以--target设为arm-linux
make                                    #编译
mkdir    tmp                            
make install prefix=$PWD/tmp            #安装到./tmp目录下
cp tmp/bin/arm-linux-gdb  /bin/       
/bin/arm-linux-gdb  -v                     #-v: 确定一下gdb的版本VID,是否是7.4

拷贝gdbserver到根文件系统

cd gdb/gdbserver/                                   #在gdb-7.4目录下输入
./configure --target=arm-linux --host=arm-linux     #设GDBServer的工作环境
make       

2.2 测试程序

功能:在程序中,A2(&a),执行程序;A( p ),A–>B–>C,出错。


#include <stdio.h>

void C(int *p)
{
	*p = 0x12;
}


void B(int *p)
{
	C(p);
}

void A(int *p)
{
	B(p);
}

void A2(int *p)
{
	C(p);
}

int main(int argc, char **argv)
{
	int a;
	int *p = NULL;

	A2(&a);  // A2 > C
	printf("a = 0x%x\n", a);

	A(p);    // A > B > C

	return 0;
}

编译,加入-g选项

arm-linux-gcc -g -o test_debug test_debug.c   #-g:附带调试信息

2.3 调试

2.3.1 建立连接

(1)在开发板上

gdbserver 192.168.2.107:2345 ./test_debug
#192.168.2.107:本地IP地址
#2345:端口号,用来让gdb来连接用的
#./test_debug:要测试的哪个文件

(2)在虚拟机中

/bin/arm-linux-gdb   ./test_debug    # 启动gdb,指定调试文件为test_debug
target remote  192.168.2.107:2345    #与gdbserver建立连接
symbol-file test_debug    
2.3.2 调试指令

常用指令

(gdb)list   or l

(gdb)break func

(gdb)break 22

(gdb)info br   

(gdb)continue   or c    // 这里不能用 run

(gdb)next   or n

(gdb)print or p    result 

(gdb) finish        // 跳出func函数

(gdb) next

(gdb) quit

2.4 gdb+coredump

当程序运行出错时,便会生成core文件,并将程序里的运行状况存到core中,也就是coredump,供给gdb来调。
(1)coredump设置
查看是否开启

ulimit -c

如果输出为0,则没有开启,设置

ulimit -c unlimited

开启core dump功能
(2)生成core文件
运行程序

./test_debug

出错,提示“Segmentation fault (core dumped)”,输入ls,看到当前文件夹下,出现了core文件。
(3)分析
将core文件拷贝到虚拟机,执行

/bin/arm-linux-gdb ./test_debug ./core

然后输入bt,查看调用关系
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值