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
,查看调用关系