生成核心转储文件

huangapple go评论97阅读模式
英文:

Generating core dumps

问题

有时候我的Go程序会崩溃。

为了生成该程序的核心转储,我尝试了几种方法:

  1. 在系统上定义ulimit,我尝试了ulimit -c unlimitedulimit -c 10000。在启动我的程序后,我没有得到任何核心转储。

  2. 我还在我的程序中添加了recover()支持,并添加了代码以在发生panic时记录到syslog,但是syslog中没有任何内容。

我现在已经没有更多的想法了。

我一定是忽略了什么,但我找不到是什么,任何帮助将不胜感激。

谢谢! 生成核心转储文件

英文:

From times to times my Go program crashes.

I tried a few things in order to get core dumps generated for this program:

  1. defining ulimit on the system, I tried both ulimit -c unlimited and ulimit -c 10000 just in case. After launching my panicking program, I get no core dump.

  2. I also added recover() support in my program and added code to log to syslog in case of panic but I get nothing in syslog.

I am running out of ideas right now.

I must have overlooked something but I do not find what, any help would be appreciated.

Thanks ! 生成核心转储文件

答案1

得分: 4

请注意,当满足某个特定条件时,操作系统会生成一个核心转储。这些条件是相当低级的,比如尝试访问未映射的内存或尝试执行CPU不知道的操作码等。在像Linux这样的POSIX操作系统下,当进程执行这些操作之一时,会向其发送一个适当的信号,如果进程没有处理,其中一些信号会有一个默认操作,即生成一个核心转储,如果没有通过设置某个限制来禁止的话,操作系统会执行这个操作。

现在请注意,这个机制在最低可能的级别(机器码)上处理进程,但是Go编译器生成的二进制文件比C编译器(或汇编器)生成的二进制文件更高级,这意味着由Go编译器生成的进程中的某些错误是由Go运行时处理而不是操作系统处理的。例如,由C编译器生成的进程中的典型空指针解引用通常会导致向进程发送SIGSEGV信号,然后通常会尝试转储进程的核心并终止它。相比之下,当这种情况发生在由Go编译器编译的进程中时,Go运行时会启动并发生恐慌,为调试目的生成一个漂亮的堆栈跟踪。

在了解这些事实的基础上,我会尝试做以下操作:

  1. 在一个shell脚本中包装你的程序,首先放宽核心转储的限制(但请参见下文),然后将程序的标准错误流重定向到一个文件(或管道到logger二进制文件等)。
  2. 用户可以调整的限制有一个层次结构:有软限制和硬限制,参见这个这个以获取解释。因此,请检查您的系统是否将核心转储大小设置为硬限制为0,因为这可能解释了为什么您尝试提高此限制没有效果。
  3. 至少在我的Debian系统上,当程序由于SIGSEGV而死亡时,内核会记录这个事实,并在syslog日志文件中可见,因此请尝试使用grep命令查找其中的提示。
英文:

Note that a core dump is generated by the OS when a condition from a certain set is met. These conditions are pretty low-level — like trying to access unmapped memory or trying to execute an opcode the CPU does not know etc. Under a POSIX operating system such as Linux when a process does one of these things, an appropriate signal is sent to it, and some of them, if not handled by the process, have a default action of generating a core dump, which is done by the OS if not prohibited by setting a certain limit.

Now observe that this machinery treats a process on the lowest possible level (machine code), but the binaries a Go compiler produces are more higher-level that those a C compiler (or assembler) produces, and this means certain errors in a process produced by a Go compiler are handled by the Go runtime rather than the OS. For instance, a typical NULL pointer dereference in a process produced by a C compiler usually results in sending the process the SIGSEGV signal which is then typically results in an attempt to dump the process' core and terminate it. In contrast, when this happens in a process compiled by a Go compiler, the Go runtime kicks in and panics, producing a nice stack trace for debugging purposes.

With these facts in mind, I would try to do this:

  1. Wrap your program in a shell script which first relaxes the limit for core dumps (but see below) and then runs your program with its standard error stream redirected to a file (or piped to the logger binary etc).
  2. The limits a user can tweak have a hierarchy: there are soft and hard limits — see this and this for an explanation. So try checking your system does not have 0 for the core dump size set as a hard limit as this would explain why your attempt to raise this limit has no effect.
  3. At least on my Debian systems, when a program dies due to SIGSEGV, this fact is logged by the kernel and is visible in the syslog log files, so try grepping them for hints.

答案2

得分: 0

  1. 首先,请确保所有错误都得到处理。

  2. 对于核心转储,您可以参考在Linux中生成核心转储

  3. 当程序崩溃时,您可以使用Supervisor来重新启动程序。

英文:
  1. First, please make sure all errors are handled.

  2. For core dump, you can refer generate a core dump in linux

  3. You can use supervisor to reboot the program when it crashes.

huangapple
  • 本文由 发表于 2013年2月19日 04:21:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/14944644.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定