如何以编程方式在 C/C 中引发核心转储++
我想在我的 C++ 应用程序中的特定位置强制进行核心转储。
我知道我可以通过执行以下操作来做到这一点:
int * crash = NULL;
*crash = 1;
但我想知道是否有更干净的方法?
顺便说一句,我正在使用Linux。
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?
I am using Linux by the way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
提高信号编号 6(Linux 中的
SIGABRT
)是实现此目的的一种方法(但请记住,在所有 POSIX 实现中,SIGABRT 并不要求为 6,因此您可以如果这不是快速的脏调试代码,则希望使用 SIGABRT 值本身)。调用
abort()
也会导致核心转储,您甚至可以在不终止进程的情况下执行此操作,只需调用fork()
后跟 < code>abort() 仅在子进程中 - 有关详细信息,请参阅此答案。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 theSIGABRT
value itself if this is anything other than quick'n'dirty debug code).Calling
abort()
will also cause a core dump, and you can even do this without terminating your process by callingfork()
followed byabort()
in the child only - see this answer for details.几年前,Google 发布了 coredumper 库。
这不是你想要的,但也许更好:)
A few years ago, Google released the coredumper library.
It's not what you were asking for, but maybe it's even better :)
如信号手册页中列出的,具有列出的操作的任何信号因为“核心”将强制进行核心转储。 一些示例是:
确保启用核心转储:
As listed in the signal manpage, any signal with the action listed as 'core' will force a core dump. Some examples are:
Make sure that you enable core dumps:
调用
相关,有时您希望在没有实际核心转储的情况下进行回溯跟踪,并允许程序继续运行:查看 glibc backtrace() 和 backtrace_symbols() 函数:
http://www.gnu.org/s/libc/manual/ html_node/Backtraces.html
Invoke
Related, sometimes you'd like a back trace without an actual core dump, and allow the program to continue running: check out glibc backtrace() and backtrace_symbols() functions:
http://www.gnu.org/s/libc/manual/html_node/Backtraces.html
生成核心转储的另一种方法:
只需创建 bash 的新实例并使用指定信号终止它。
$$
是 PID贝壳。 否则,您将终止当前的 bash,并将被注销、终端关闭或断开连接。
Another way of generating a core dump:
Just create a new instance of the bash and kill it with specified signal. The
$$
is the PID ofthe shell. Otherwise you are killing your current bash and will be logged out, terminal closed or disconnected.
您可以使用 kill(2) 发送信号。
所以,
You can use kill(2) to send signal.
So,
有时,这样做可能是合适的:
这种简单方法的一个问题是只有一个线程将被核心转储。
Sometimes it may be appropriate to do something like this:
One problem with this simple approach is that only one thread will be coredumped.
任何你想要的地方都可以使用这种方法:)
use this approach wherever you want :)