Linux 为什么会生成核心转储文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/775872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why are core dump files generated?
提问by Chaithra
Sometimes when I run my code, a core dump file is generated when I terminate the program by Ctrl+\. The file name is of the form core.*
. The program is not terminating abruptly, and there is no segmentation fault. I believe it is SIGQUIT
and not SIGABRT
or SIGSEGV
. If I try Ctrl+C, or Ctrl+Z, then it is not generated.
有时当我运行我的代码时,当我通过Ctrl+终止程序时会生成一个核心转储文件\。文件名的格式为core.*
. 程序不会突然终止,也没有分段错误。我相信它是SIGQUIT
和不是SIGABRT
或SIGSEGV
。如果我尝试Ctrl+C或Ctrl+ Z,则不会生成它。
Can anyone tell why it is generated only when Ctrl+\is pressed? How can I avoid this core dump file from being generated? Is there any use for the core dumped file?
谁能说出为什么只有在按下Ctrl+时才会生成它\?如何避免生成此核心转储文件?核心转储文件有什么用吗?
采纳答案by JaredPar
A process dumps core when it is terminated by the operating system due to a fault in the program. The most typical reason this occurs is because the program accessed an invalid pointer value. Given that you have a sporadic dump, it's likely that you are using an uninitialized pointer.
当一个进程由于程序错误而被操作系统终止时,它会转储核心。发生这种情况的最典型原因是程序访问了无效的指针值。鉴于您有一个零星的转储,很可能您正在使用未初始化的指针。
Can you post the code that is causing the fault? Other than vague generalizations it's hard to guess what's wrong without actually seeing code.
你能贴出导致错误的代码吗?除了模糊的概括之外,如果没有实际看到代码,很难猜测出了什么问题。
As for what a core dump actually is, check out this Wikipedia article:
至于核心转储实际上是什么,请查看这篇维基百科文章:
回答by Jason Coco
It's a tool to aid in debugging an application that's behaving badly. It's large because it contains the contents of all the applications physical memory at the time it died as well as the register states and stacks of all its threads.
它是一种帮助调试运行不良的应用程序的工具。它很大是因为它包含了所有应用程序在它死亡时物理内存的内容以及所有线程的寄存器状态和堆栈。
They get generated when the kernel kills an application for doing something evil, like generating a segmentation violation or a bus error.
当内核杀死应用程序做坏事时会生成它们,例如生成分段违规或总线错误。
回答by Jason Coco
Core dumps are generated when the process receives certain signals, such as SIGSEGV, which the kernels sends it when it accesses memory outside its address space. Typically that happens because of errors in how pointers are used. That means there's a bug in the program.
当进程接收到某些信号(例如 SIGSEGV)时,会生成核心转储,内核在访问其地址空间之外的内存时会发送该信号。通常发生这种情况是因为指针的使用方式错误。这意味着程序中存在错误。
The core dump is useful for finding the bug. It is an image of the the process's memory at the time of the problem, so a debugger such as gdb can be used to see what the program was doing then. The debugger can even access (sometimes) the values of variables in the program.
核心转储对于查找错误很有用。它是出现问题时进程内存的映像,因此可以使用诸如 gdb 之类的调试器来查看程序当时在做什么。调试器甚至可以访问(有时)程序中变量的值。
You can prevent core dumps from happening using the ulimit command.
您可以使用 ulimit 命令防止发生核心转储。
回答by lothar
As said by others before the core dump is the result of a fault in the program.
正如之前其他人所说,核心转储是程序错误的结果。
You can configure if a core dump is to be generated with the ulimitcommand. Entering
您可以配置是否使用ulimit命令生成核心转储。进入
ulimit -c 0
disables core file generation in the active shell.
在活动 shell 中禁用核心文件生成。
If the program that generated the core was built with symbol information you can do a post mortem debugging sessionlike this:
如果生成核心的程序是使用符号信息构建的,您可以像这样进行事后调试会话:
gdb <pathto/executable> --core <corefilename>
回答by MarkR
You can avoid creating a core dump file by writing code that doesn't crash :)
您可以通过编写不会崩溃的代码来避免创建核心转储文件:)
Seriously, core dumps are useful because you can see the state of the program when it crashed, for "post mortem" debugging. You can open them in gdb and inspect the state of your program (especially if it was built with debugging).
说真的,核心转储很有用,因为您可以看到程序崩溃时的状态,用于“事后”调试。您可以在 gdb 中打开它们并检查程序的状态(特别是如果它是通过调试构建的)。
Core dumps usually get made if the program has a SIGSEGV (usually caused by invalid pointer dereferencing), SIGABRT (which would happen if you called abort(), or in C++ by the default terminate() handler for exceptions in destructors etc) or some other fault. You can also trigger them explicitly with the debugger or programmatically.
如果程序有 SIGSEGV(通常由无效的指针解除引用引起)、SIGABRT(如果你调用 abort() 或在 C++ 中通过默认的 terminate() 处理程序在析构函数等中调用异常)或某些其他故障。您还可以使用调试器或以编程方式显式触发它们。
If you've fixed all the bugs and it's perfect, you can delete them. Also, if you've changed your program in any way (and recompiled it) then they will become useless as the debug info now won't match what's in the core dump, so you can delete them then too.
如果您已修复所有错误并且完美无缺,则可以删除它们。此外,如果您以任何方式更改了程序(并重新编译了它),那么它们将变得无用,因为现在调试信息与核心转储中的内容不匹配,因此您也可以删除它们。
回答by Onkar
ctrl + \sends signal SIGQUIT to the process. According to POSIX.1 standard, the default action for this signal is to generate a core.
ctrl + \向进程发送信号 SIGQUIT。根据 POSIX.1 标准,此信号的默认操作是生成核心。
SIGILL, SIGABRT, SIGFPE, SIGSEGV are other cases when system will generate a core.
SIGILL、SIGABRT、SIGFPE、SIGSEGV 是系统将生成核心的其他情况。
Please refer "man 7 signal" on your system for more details.
有关详细信息,请参阅系统上的“man 7 信号”。
回答by Zenexer
The point of Ctrl+ \is to generate a core dump. That's what SIGQUIT
does. If you don't want it to be generated, use Ctrl+ C(SIGINT
) instead. If the program in question is not responding to SIGINT
but you need to kill it from the terminal, either you or the developer is doing something wrong.
点Ctrl+\是生成一个核心转储。这就是SIGQUIT
它的作用。如果您不想生成它,请改用Ctrl+ C( SIGINT
)。如果有问题的程序没有响应,SIGINT
但您需要从终端杀死它,那么您或开发人员做错了什么。
Programs designed notto be killed from the terminal with Ctrl+ Cshould still respond gracefully to SIGTERM
, which can be triggered in another terminal via kill -TERM ...
. If all else fails, SIGKILL
will force an immediate termination.
设计为不会从终端被Ctrl+杀死的程序C仍然应该优雅地响应SIGTERM
,这可以在另一个终端中通过kill -TERM ...
. 如果一切都失败了,SIGKILL
将强制立即终止。