如何以编程方式在C/C ++中引起核心转储 [英] How to programmatically cause a core dump in C/C++
问题描述
我想在我的C ++应用程序的特定位置强制进行核心转储.
I would like to force a core dump at a specific location in my C++ application.
我知道我可以通过以下方式做到这一点:
I know I can do it by doing something like:
int * crash = NULL;
*crash = 1;
但是我想知道是否有一种更清洁的方法?
But I would like to know if there is a cleaner way?
我正在使用Linux.
I am using Linux by the way.
推荐答案
提高信号编号6(在Linux中为SIGABRT
)是实现此目的的一种方法(尽管请注意,不需要SIGABRT 在所有POSIX实现中都为6,因此,如果这不是quick'n'dirty调试代码,则可能需要使用SIGABRT
值本身.
Raising of signal number 6 (SIGABRT
in Linux) is one way to do it (though keep in mind that SIGABRT is not required to be 6 in all POSIX implementations so you may want to use the SIGABRT
value itself if this is anything other than quick'n'dirty debug code).
#include <signal.h>
: : :
raise (SIGABRT);
调用abort()
也会导致核心转储,您甚至可以执行此操作而无需通过仅在子级中调用fork()
后跟abort()
来终止进程-请参见此答案以了解详情.
Calling abort()
will also cause a core dump, and you can even do this without terminating your process by calling fork()
followed by abort()
in the child only - see this answer for details.
这篇关于如何以编程方式在C/C ++中引起核心转储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!