如何在GDB中调试失败的货物测试?

 张珮娟7063 发布于 2022-12-07 21:27

我有一个失败的货物测试:

$ cargo test
[snip]
    Running target/gunzip-c62d8688496249d8

running 2 tests
test test_extract_failure ... FAILED
test test_extract_success ... ok

failures:

---- test_extract_failure stdout ----
        task 'test_extract_failure' panicked at 'assertion failed: result.is_err()', /home/dhardy/other/flate2-rs/tests/gunzip.rs:19



failures:
    test_extract_failure

test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured

task '
' panicked at 'Some tests failed', /home/rustbuild/src/rust-buildbot/slave/nightly-linux/build/src/libtest/lib.rs:250

如何在GDB之类的调试器中启动失败测试?

这应该是一个普遍的问题,但对于那些想要回溯我的步骤的人来说,安装一个最近的每晚Rust构建和:

git clone https://github.com/dhardy/flate2-rs.git
git checkout 24979640a880
cd flate2-rs
cargo test

Chris Morgan.. 37

您可以通过向其传递其他参数来获取测试二进制文件以过滤其运行的测试; Cargo也直接暴露了这一点.因此,cargo test test_extract_failure只会运行该特定情况.(如果您有其他测试恐慌并且预计会失败,这很方便,因此他们不会调用rust_panic我要提及的功能,只留下那些违规的电话.)

为了使用gdb,你需要直接运行测试二进制文件(如果你使用Cargo它在子进程中运行,因此gdb不会在其中捕获恐慌).货物有用地告诉你文件名,target/gunzip-c62d8688496249d8.您可以直接运行--test它以使其成为测试运行:

$ target/gunzip-c62d8688496249d8 --test test_extract_failure
running 1 test
test test_extract_failure ... FAILED

failures:

---- test_extract_failure stdout ----
        task 'test_extract_failure' panicked at 'assertion failed: result.is_err()', /home/dhardy/other/flate2-rs/tests/gunzip.rs:19



failures:
    test_extract_failure

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured

task '
' panicked at 'Some tests failed', /home/rustbuild/src/rust-buildbot/slave/nightly-linux/build/src/libtest/lib.rs:250

现在用gdb连接它.有一个方便的功能,您可以插入断点,rust_panic.一旦进入gdb,break rust_panic意味着在实际进行展开之前,只要有什么东西触发了恐慌,它就会暂停.

以下是会话可能最终结果如下:

$ gdb target/demo-92d91e26f6ebc557
…
Reading symbols from target/demo-92d91e26f6ebc557...done.
(gdb) break rust_panic
Breakpoint 1 at 0xccb60
(gdb) run --test test_extract_failure
Starting program: /tmp/demo/target/demo-92d91e26f6ebc557 --test test_extract_failure
warning: Could not load shared library symbols for linux-vdso.so.1.
Do you need "set solib-search-path" or "set sysroot"?
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/libthread_db.so.1".

running 1 test
[New Thread 0x7ffff6ef4700 (LWP 14254)]
[New Thread 0x7ffff5fff700 (LWP 14255)]
[Switching to Thread 0x7ffff5fff700 (LWP 14255)]

Breakpoint 1, 0x0000555555620b60 in rust_panic ()
(gdb) bt
#0  0x0000555555620b60 in rust_panic ()
#1  0x0000555555621274 in unwind::begin_unwind_inner::hb821324209c8ed246Qc ()
#2  0x000055555556bb6d in unwind::begin_unwind::h7834652822578025936 ()
#3  0x000055555556b9fd in demo::do_something () at :8
#4  0x000055555556b98e in demo::test_extract_failure () at src/lib.rs:3
#5  0x000055555559aa4b in task::TaskBuilder::try_future::closure.8077 ()
#6  0x000055555560fd03 in task::TaskBuilder::spawn_internal::closure.30919 ()
#7  0x000055555561f672 in task::Task::spawn::closure.5759 ()
#8  0x0000555555621cac in rust_try_inner ()
#9  0x0000555555621c96 in rust_try ()
#10 0x000055555561f713 in unwind::try::ha8078a6ae9b50ccepFc ()
#11 0x000055555561f51c in task::Task::run::hdb5fabf381084abafOb ()
#12 0x000055555561f168 in task::Task::spawn::closure.5735 ()
#13 0x0000555555620595 in thread::thread_start::h4d73784c295273b3i6b ()
#14 0x00007ffff79c2314 in start_thread () from /usr/lib/libpthread.so.0
#15 0x00007ffff72e25bd in clone () from /usr/lib/libc.so.6
(gdb) 

在那种特殊情况下,#0-#2和#5-#15是噪声,#3和#4是我们想要的信号.

1 个回答
  • 您可以通过向其传递其他参数来获取测试二进制文件以过滤其运行的测试; Cargo也直接暴露了这一点.因此,cargo test test_extract_failure只会运行该特定情况.(如果您有其他测试恐慌并且预计会失败,这很方便,因此他们不会调用rust_panic我要提及的功能,只留下那些违规的电话.)

    为了使用gdb,你需要直接运行测试二进制文件(如果你使用Cargo它在子进程中运行,因此gdb不会在其中捕获恐慌).货物有用地告诉你文件名,target/gunzip-c62d8688496249d8.您可以直接运行--test它以使其成为测试运行:

    $ target/gunzip-c62d8688496249d8 --test test_extract_failure
    running 1 test
    test test_extract_failure ... FAILED
    
    failures:
    
    ---- test_extract_failure stdout ----
            task 'test_extract_failure' panicked at 'assertion failed: result.is_err()', /home/dhardy/other/flate2-rs/tests/gunzip.rs:19
    
    
    
    failures:
        test_extract_failure
    
    test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured
    
    task '<main>' panicked at 'Some tests failed', /home/rustbuild/src/rust-buildbot/slave/nightly-linux/build/src/libtest/lib.rs:250
    

    现在用gdb连接它.有一个方便的功能,您可以插入断点,rust_panic.一旦进入gdb,break rust_panic意味着在实际进行展开之前,只要有什么东西触发了恐慌,它就会暂停.

    以下是会话可能最终结果如下:

    $ gdb target/demo-92d91e26f6ebc557
    …
    Reading symbols from target/demo-92d91e26f6ebc557...done.
    (gdb) break rust_panic
    Breakpoint 1 at 0xccb60
    (gdb) run --test test_extract_failure
    Starting program: /tmp/demo/target/demo-92d91e26f6ebc557 --test test_extract_failure
    warning: Could not load shared library symbols for linux-vdso.so.1.
    Do you need "set solib-search-path" or "set sysroot"?
    [Thread debugging using libthread_db enabled]
    Using host libthread_db library "/usr/lib/libthread_db.so.1".
    
    running 1 test
    [New Thread 0x7ffff6ef4700 (LWP 14254)]
    [New Thread 0x7ffff5fff700 (LWP 14255)]
    [Switching to Thread 0x7ffff5fff700 (LWP 14255)]
    
    Breakpoint 1, 0x0000555555620b60 in rust_panic ()
    (gdb) bt
    #0  0x0000555555620b60 in rust_panic ()
    #1  0x0000555555621274 in unwind::begin_unwind_inner::hb821324209c8ed246Qc ()
    #2  0x000055555556bb6d in unwind::begin_unwind::h7834652822578025936 ()
    #3  0x000055555556b9fd in demo::do_something () at <std macros>:8
    #4  0x000055555556b98e in demo::test_extract_failure () at src/lib.rs:3
    #5  0x000055555559aa4b in task::TaskBuilder::try_future::closure.8077 ()
    #6  0x000055555560fd03 in task::TaskBuilder::spawn_internal::closure.30919 ()
    #7  0x000055555561f672 in task::Task::spawn::closure.5759 ()
    #8  0x0000555555621cac in rust_try_inner ()
    #9  0x0000555555621c96 in rust_try ()
    #10 0x000055555561f713 in unwind::try::ha8078a6ae9b50ccepFc ()
    #11 0x000055555561f51c in task::Task::run::hdb5fabf381084abafOb ()
    #12 0x000055555561f168 in task::Task::spawn::closure.5735 ()
    #13 0x0000555555620595 in thread::thread_start::h4d73784c295273b3i6b ()
    #14 0x00007ffff79c2314 in start_thread () from /usr/lib/libpthread.so.0
    #15 0x00007ffff72e25bd in clone () from /usr/lib/libc.so.6
    (gdb) 
    

    在那种特殊情况下,#0-#2和#5-#15是噪声,#3和#4是我们想要的信号.

    2022-12-11 02:15 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有