如果程序成功,如何让gdb退出,如果程序崩溃会中断? [英] How to have gdb exit if program succeeds, break if program crashes?

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

问题描述

我的代码中似乎有一些多线程错误,每30次运行它的测试套件就会发生一次崩溃。测试套件是非交互式的。我想在gdb中运行测试套件,并且如果程序正常退出,gdb会正常退出,如果崩溃,则会中断(并显示调试提示)。这样我就可以让测试套件反复运行,拿起一杯咖啡,回来,并且提供一个很好的调试提示。

解决方案

这是一个小黑客,但你可以这样做:

  gdb --eval-command = run --eval-command = quit --args ./a.out 

如果a.out正常终止,它会让您退出GDB。但是,如果你崩溃了,程序仍然是活跃的,所以GDB通常会提示你是否真的想退出一个活跃的劣势:

 编程接收到的信号SIGABRT,中止。 
0x00007ffff72dad05 in raise(sig = ...)at ../nptl/sysdeps/unix/sysv/linux/raise.c:64
64 ../nptl/sysdeps/unix/sysv/linux /raise.c:没有这样的文件或目录。
在../nptl/sysdeps/unix/sysv/linux/raise.c
调试会话处于活动状态。

劣等1 [过程15126]将被杀死。

无论如何退出? (y或n)

像我说的,不漂亮,但它的作品,只要你避难没有关闭提示符以退出活动进程。可能还有一种方法可以使用gdb的 quit 命令:它需要一个数字参数,它是调试会话的退出代码。所以,也许你可以使用--eval-command =quit stuff,其中东西是一些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?

解决方案

This is a little hacky but you could do:

gdb --eval-command=run --eval-command=quit  --args ./a.out

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) 

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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆