ILD

gdb远程调试Segmentation fault
作者:Yuan Jianpeng 邮箱:yuanjp89@163.com
发布时间:2020-2-10 站点:Inside Linux Development

远程设备是一个arm路由器。运行程序报Segmentation fault。首先使用-g选项重新编译,然后开始gdb调试:


1 远程运行gdbserver

$ gdbserver :1234 /usr/bin/netmand

中间的COMM参数可以是一个tty设备。也可以是host:port,表示监听tcp连接。

2 本地执行交叉编译版本的gdb

$ /opt/toolchain-ipq4018/bin/arm-unknown-linux-gnueabihf-gdb usr/bin/netmand

GNU gdb (crosstool-NG 1.24.0) 8.2.1
Copyright (C) 2018 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 "--host=x86_64-build_pc-linux-gnu --target=arm-unknown-linux-gnueabihf".
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 usr/bin/netmand...done.
(gdb) set solib-search-path .
(gdb) target remote 192.168.0.2:1234
Remote debugging using 192.168.0.2:1234
Reading symbols from /opt/toolchain-ipq4018/arm-unknown-linux-gnueabihf/sysroot/lib/ld-linux-armhf.so.3...done.
0xb6fcea00 in _start () from /opt/toolchain-ipq4018/arm-unknown-linux-gnueabihf/sysroot/lib/ld-linux-armhf.so.3
(gdb) b main
Breakpoint 1 at 0x11500: file netman.c, line 83.
(gdb) c
Continuing.

Breakpoint 1, main (argc=1, argv=0xbefffea4) at netman.c:83
83              if (fd_init(&netman_ctx) < 0) {
(gdb) info sharedlibrary
From        To          Syms Read   Shared Object Library
0xb6fcea00  0xb6fe9e04  Yes         /opt/toolchain-ipq4018/arm-unknown-linux-gnueabihf/sysroot/lib/ld-linux-armhf.so.3
0xb6fbb914  0xb6fbc72c  Yes         /work/xrouter/output/ipq4018/rootfs/usr/lib/liblog.so
0xb6fa87e8  0xb6fa914c  Yes         /work/xrouter/output/ipq4018/rootfs/usr/lib/libcommon.so
0xb6f93a88  0xb6f96050  Yes         /work/xrouter/output/ipq4018/rootfs/usr/lib/libconfig.so
0xb6e6a140  0xb6f5eb30  Yes         /opt/toolchain-ipq4018/arm-unknown-linux-gnueabihf/sysroot/lib/libc.so.6
(gdb) c
Continuing.

Program received signal SIGSEGV, Segmentation fault.
0x00012018 in setup_proto (proto=0x26000) at proto.c:154
154             if (proto->is_bridge)
(gdb) bt
#0  0x00012018 in setup_proto (proto=0x26000) at proto.c:154
#1  0x0001144c in apply_proto (ctx=0x250ac <netman_ctx>) at netman.c:56
#2  0x000115b8 in main (argc=1, argv=0xbefffea4) at netman.c:100
(gdb) l proto.c:154
149     int setup_proto(struct proto *proto)
150     {
151             int ret;
152             int i;
153
154             if (proto->is_bridge)
155             {
156                     ret = add_br(proto->l2if);
157                     if (ret < 0) {
158                             plog(NETMAN, ERR, "create bridge %s failed: %d (%s)",

用到了相关的命令:

调用的时候带了文件路径:/opt/toolchain-ipq4018/bin/arm-unknown-linux-gnueabihf-gdb usr/bin/netmand,如果没有带,直接执行gdb,则可以使用file命令加载。


set solib-search-path .

设置共享库的搜索路径,由于是远程调试,不设置的话,会直接使用/usr/lib/liblog.so等,显然会找不到,使用set sysroot . 设置系统根目录也是可以的。


target remote 192.168.0.2:1234

连接到远程target


info sharedlibrary 

显示共享库


bt

显示调用栈


list proto.c:154

显示出错的源代码


Note:测试发现远程使用非调试版本,源码不变,本地编译为-g版本,也是可以的。


参考:

https://sourceware.org/gdb/current/onlinedocs/gdb/Files.html


Copyright © linuxdev.cc 2017-2024. Some Rights Reserved.