1. 下载gdb
ftp://ftp.gnu.org/gnu/gdb
2. 交叉编译
tar -xvv gdb-7.12.1.tar.gz
./configure --host=arm-linux-gnueabihf CC=arm-linux-gnueabihf-gcc --prefix=`pwd`/_install
make
make install
编译完成后我们把gdb可执行文件拷贝到目标板上
3. ulimit 打开core文件
ulimit -c unlimited
打开core文件,在程序挂掉的时候会产生core文件
gdb xxx core.xxx # xxx是你的可执行文件
4. 测试
void test(char *p)
{
strcpy(p, "hello");
}
int main()
{
cout << "test() ============" << endl;
char * p1 = new char[10];
p1 = NULL;
// strcpy(p1, "hello");
test(p1);
return 0;
}
上面的代码在执行时会发生段错误,我们可以用gdb追踪其错误
g++ test.cpp -g
./a.out
发生段错误后会产生core文件,然后我们使用gdb追踪发生段错误的地方
5. 总结
使用gdb调试段错误还是很好用的,能很快帮我们追踪到发生错误的地方