Skip to content

debug - 调试方法

由于目标板子资源受限, 不能直接在目标板子上运行gdb进行调试, 目前能采用的方法有 debug with remote gdb 或者 debug with coredump.

[[toc]]

Remote gdb

  • Step 1: 将你要debug的package, 编译出有debug info并且不能strip symbol的应用程序;

  • make {package}/{clean,compile} V=s STRIP=/bin/true CONFIG_DEBUG=y 重新编译固件, 不strip 符号;

  • staging_dir/toolchain-mipsel_24kc_gcc-8.4.0_musl/bin/mipsel-openwrt-linux-nm {package-bin} 使用nm命令检测应用程序是否有符号;
  • Step 2: 网关安装上面编译好的程序;
  • Step 3: 运行Make menuconfig, 选择gdbserver;
  • Step 4: make package/devel/gdb/{clean,compile} V=s;
  • Step 5: 通过scp或者tftp等命令将gdbserver的ipk文件拷贝到被网关中,并安装;
  • Step 6: 网关上运行gdbserver

gdbserver --once :9000 /usr/bin/pubmsg

  • Step 7: linux主机上运行 gdb客户端

./scripts/remote-gdb 192.168.0.102:9000 ./build_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/pubmsg-esl/pubmsg

  • Step 8: 上一步进入gdb后, 输入r即可开始运行调试.

如果gdbserver运行是没有加上--once参数, 可以在客户端上运行monitor exit命令将gdbserver退出监听

with cgdb

cgdb是gdb的前端界面,方便直接查看源码.

# cat scripts/remote-gdb
#!/usr/bin/env perl

use strict;
use warnings;
use FindBin '$Bin';
use File::Temp 'tempfile';

@ARGV >= 2 || do {
        die "Usage: $0 <corefile|host:port> <executable> [enable_cgdb]\n";
        exit 1;
};

if( opendir SD, "$Bin/../staging_dir" )
{
        my ( $tid, $arch, $libc, @arches );

        if( $ARGV[1] =~ m!\btarget-(.+?)_([^/_]+libc|musl)_?([^/]*).*\b!i )
        {
                print("Using target $1 ($2, $3)\n");
                ($arch, $libc) = ($1, $2);
        }
        else
        {
                # Find arches
                print("Choose target:\n");

                while( defined( my $e = readdir SD ) )
                {
                        if( -d "$Bin/../staging_dir/$e" && $e =~ /^target-(.+?)_([^\/_]+libc|musl)_?([^\/]*).*/i )
                        {
                                push @arches, [ $1, $2 ];
                                printf(" %2d) %s (%s %s)\n", @arches + 0, $1, $2, $3);
                        }
                }

                if( @arches > 1 )
                {
                        # Query arch
                        do {
                                print("Target? > ");
                                chomp($tid = <STDIN>);
                        } while( !defined($tid) || $tid !~ /^\d+$/ || $tid < 1 || $tid > @arches );

                        ($arch, $libc) = @{$arches[$tid-1]};
                }
                else
                {
                        ($arch, $libc) = @{$arches[0]};
                }
        }

        closedir SD;

        # Find gdb
        my ($gdb) = glob("$Bin/../staging_dir/toolchain-${arch}_*_${libc}*/bin/*-gdb");
        if( defined($gdb) && -x $gdb )
        {
                my ( $fh, $fp ) = tempfile();

                # Find sysroot
                my ($sysroot) = glob("$Bin/../staging_dir/target-${arch}_${libc}*/root-*/");

                print $fh "set sysroot $sysroot\n" if $sysroot;
                my $cmd = "target extended-remote";
                -f $ARGV[0] and $cmd = "core-file";
                print $fh "$cmd $ARGV[0]\n";

                # History settings
                print $fh "set history filename $Bin/../tmp/.gdb_history\n";
                print $fh "set history size 100000000\n";
                print $fh "set history save on\n";

                my $file = -f "$sysroot/$ARGV[1]" ? "$sysroot/$ARGV[1]" : $ARGV[1];
                if( @ARGV == 2 ){
                        system($gdb, '-x', $fp, $file);
                }else{
                        system('cgdb', '-d', $gdb, '-x', $fp, $file);
            }
                close($fh);
                unlink($fp);
        }
        else
        {
                print("No gdb found! Make sure that CONFIG_GDB is set!\n");
                exit(1);
        }
}
else
{
        print("No staging_dir found! You need to compile at least once!\n");
        exit(1);
}

以上是remote-gdb脚本的修改版, 在最后加上一个参数,就可以使能cgdb功能

  • ./scripts/remote-gdb 192.168.5.123:9000 build_dir/target-mipsel_24kc_musl/pubmsg-g1-e-grapes/pubmsg enable_cgdb

debug-methods-cgdb

with vscode

在openwrt的根目录下创建一个.vscode目录,并在里面新建一个launch.json文件,文件的内容如下,适当的根据自己的情况修改路径.

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "cppdbg",
            "request": "launch",
            "name": "gdbserver(pubmsg)",
            "miDebuggerServerAddress": "192.168.5.123:9000",
            "program": "./build_dir/target-mipsel_24kc_musl/pubmsg-g1-e-grapes/pubmsg",
            "MIMode": "gdb",
            "internalConsoleOptions": "neverOpen",
            "externalConsole": true,
            "miDebuggerPath": "${workspaceRoot}/staging_dir/toolchain-mipsel_24kc_gcc-7.5.0_musl/bin/mipsel-openwrt-linux-gdb",
            "cwd": "${workspaceRoot}",
            "stopAtEntry": true,
            "setupCommands": [
                {
                    "description": "为gdb启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "set sysroot",
                    "text": "set sysroot ./staging_dir/target-mipsel_24kc_musl/root-ramips",
                    "ignoreFailures": false
                }
            ]
        }
    ]
}

网关端准备并运行gdbserver监听后,点击vscode的debug按键进行debug。

Core dump

这种方式是让程序在崩溃时, 生成一个core dump文件, 再利用core dump文件进行调试.

  • Step 1: 和 Remote gdb的方式一样;
  • Step 2: 和Remote gdb的方式一样;
  • Step 3: 去掉限制, 让程序在崩溃时生成一个core dump文件;
  1. 运行 ulimit -c unlimited即可;
  2. 运行ulimit -a可以查看上一次命令是否生效;
  3. core dump文件的存放的位置根据/proc/sys/kernel/core_pattern文件而定的; 详细看core的介绍

  4. Step 4: 直接运行你需要debug的应用程序, 将程序崩溃后生成的core dump文件后拷贝到linux主机上;

  5. Step 5: linux主机上运行 gdb客户端;

./scripts/remote-gdb pubmsg.1661223325.6754.6.core build_dir/target-mipsel_24kc_musl/pubmsg-mg4-b-berry/pubmsg

  • Step 6: 通过上面步骤进入gdb调试控制台后, 运行bt/ info threads/ 等gdb命令查看程序奔溃的地方即可.