如果程序崩溃则中断

如果程序崩溃则中断

本文介绍了如果程序成功如何让gdb退出,如果程序崩溃则中断?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码中似乎存在某种多线程错误,导致其测试套件每运行 30 次就会崩溃一次.测试套件是非交互式的.我想在 gdb 中运行我的测试套件,如果程序正常退出,则 gdb 正常退出,如果崩溃则中断(并显示调试提示).这样我就可以让测试套件重复运行,去喝杯咖啡,回来,并得到一个很好的调试提示.如何使用 gdb 做到这一点?

I seem to have some kind of multithreading bug in my code that makes it crash once every 30 runs of its test suite. The test suite is non-interactive. I want to run my test suite in gdb, and have gdb exit normally if the program exits normally, or break (and show a debugging prompt) if it crashes. This way I can let the test suite run repeatedly, go grab a cup of coffee, come back, and be presented with a nice debugging prompt. How can I do this with gdb?

推荐答案

这有点 hacky 但你可以这样做:

This is a little hacky but you could do:

gdb -ex='set confirm on' -ex=run -ex=quit --args ./a.out

如果 a.out 正常终止,它只会让你退出 GDB.但是如果你崩溃了,程序仍然是活动的,所以如果你真的想用一个活动的下级退出,GDB 通常会提示:

If a.out terminates normally, it will just drop you out of GDB. But if you crash, the program will still be active, so GDB will typically prompt if you really want to quit with an active inferior:

Program received signal SIGABRT, Aborted.
0x00007ffff72dad05 in raise (sig=...) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64
64  ../nptl/sysdeps/unix/sysv/linux/raise.c: No such file or directory.
    in ../nptl/sysdeps/unix/sysv/linux/raise.c
A debugging session is active.

    Inferior 1 [process 15126] will be killed.

Quit anyway? (y or n)

就像我说的那样,不是很漂亮,但只要您没有关闭提示以退出活动进程,它就可以工作.也可能有一种方法可以使用 gdb 的 quit 命令:它接受一个数字参数,即调试会话的退出代码.所以也许你可以使用 --eval-command="quit stuff",其中的 stuff 是一些 GDB 表达式,它反映了下级是否正在运行.

Like I said, not pretty, but it works, as long as you haven't toggled off the prompt to quit with an active process. There is probably a way to use gdb's quit command too: it takes a numeric argument which is the exit code for the debugging session. So maybe you can use --eval-command="quit stuff", where stuff is some GDB expression that reflects whether the inferior is running or not.

这个程序可以用来测试一下:

This program can be used to test it out:

#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
    if (time(NULL) % 2) {
        raise(SIGINT);
    }
    puts("no crash");
    return EXIT_SUCCESS;
}

这篇关于如果程序成功如何让gdb退出,如果程序崩溃则中断?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 08:29