分段错误(核心已转储)-到哪里?它是什么?为什么呢?


16

在Linux中发生分段错误时,错误消息Segmentation fault (core dumped)将打印到终端(如果有),并且程序将终止。作为C / C ++开发人员,这种情况经常发生在我身上,我通常忽略它并继续前进gdb,重新创建我以前的操作,以便再次触发无效的内存引用。取而代之的是,我认为我也许可以改用这个“核心”,因为一直运行gdb很繁琐,而且我不能总是重新创建分段错误。

我的问题是三个:

  • 这个难以捉摸的“核心”在哪里倾销?
  • 它包含什么?
  • 我该怎么办?

通常您只需要命令gdb path-to-your-binary path-to-corefile,然后info stackCtrl-d。唯一令人担心的是,核心转储对您来说是很平常的事情。
ott--

并不是那么平常,只是偶尔 -在大多数情况下,这是由于错别字或我改变了一些而没有抢占结果。

Answers:


16

如果其他人清理...

...您通常什么也找不到。但是幸运的是,Linux为此提供了一个处理程序,您可以在运行时指定该处理程序。在/usr/src/linux/Documentation/sysctl/kernel.txt中,您将找到:

[/ proc / sys / kernel /] core_pattern用于指定核心转储文件模式名称。

  • 如果模式的第一个字符是'|',则内核会将模式的其余部分视为要运行的命令。核心转储将被写入该程序的标准输入,而不是文件。

谢谢

根据消息来源,这是由abrt程序处理的(即自动Bug报告工具,不会中止),但是在我的Arch Linux上,它是由systemd处理的。您可能需要编写自己的处理程序或使用当前目录。

但是里面有什么?

现在它包含的内容是特定于系统的,但是根据所有已知的百科全书

[核心转储]由计算机程序在特定时间的工作存储器的记录状态组成。实际上,通常会同时转储程序状态的其他关键部分,包括处理器寄存器,这些寄存器可能包括程序计数器和堆栈指针,内存管理信息以及其他处理器和操作系统标志和信息。

...因此它基本上包含了所有gdb想要的东西,以及更多。

是的,但我希望我能快乐而不是gdb

两者都会很高兴,因为gdb只要您拥有可执行文件的精确副本,它就会加载任何核心转储gdb path/to/binary my/core.dump。然后,您应该能够像往常一样继续开展业务,并因尝试并未能修复错误而不是尝试并未能再现错误而烦恼。



4

通常会调用核心文件,该文件core位于进程的当前工作目录中。但是,有很多原因导致无法生成核心文件,并且它可能完全以其他名称位于其他位置。有关详细信息,请参见core.5手册页

描述

某些信号的默认操作是使进程终止并产生一个核心转储文件该文件是一个磁盘文件,包含终止时进程内存的映像。该映像可以在调试器(例如gdb(1))中使用,以在程序终止时检查程序的状态。 导致进程转储内核的信号列表可以在signal(7)中找到。

...

在多种情况下不会生成核心转储文件:

   *  The process does not have permission to write the core file.  (By
      default, the core file is called core or core.pid, where pid is
      the ID of the process that dumped core, and is created in the
      current working directory.  See below for details on naming.) 
      Writing the core file will fail if the directory in which it is to
      be created is nonwritable, or if a file with the same name exists
      and is not writable or is not a regular file (e.g., it is a
      directory or a symbolic link).
   *  A (writable, regular) file with the same name as would be used for
      the core dump already exists, but there is more than one hard link
      to that file.
   *  The filesystem where the core dump file would be created is full;
      or has run out of inodes; or is mounted read-only; or the user has
      reached their quota for the filesystem.
   *  The directory in which the core dump file is to be created does
      not exist.
   *  The RLIMIT_CORE (core file size) or RLIMIT_FSIZE (file size)
      resource limits for the process are set to zero; see getrlimit(2)
      and the documentation of the shell's ulimit command (limit in
      csh(1)).
   *  The binary being executed by the process does not have read
      permission enabled.
   *  The process is executing a set-user-ID (set-group-ID) program that
      is owned by a user (group) other than the real user (group) ID of
      the process, or the process is executing a program that has file
      capabilities (see capabilities(7)).  (However, see the description
      of the prctl(2) PR_SET_DUMPABLE operation, and the description of
      the /proc/sys/fs/suid_dumpable file in proc(5).)
   *  (Since Linux 3.7) The kernel was configured without the
      CONFIG_COREDUMP option.

此外,如果使用了madvise(2)MADV_DONTDUMP标志,则核心转储可能会排除进程的部分地址空间。

核心转储文件的命名

默认情况下,核心转储文件名为core,但是/ proc / sys / kernel / core_pattern文件(自Linux 2.6和2.4.21起)可以设置为定义用于命名核心转储文件的模板。模板可以包含%说明符,在创建核心文件时,这些说明符将由以下值替换:

       %%  a single % character
       %c  core file size soft resource limit of crashing process (since
           Linux 2.6.24)
       %d  dump mode—same as value returned by prctl(2) PR_GET_DUMPABLE
           (since Linux 3.7)
       %e  executable filename (without path prefix)
       %E  pathname of executable, with slashes ('/') replaced by
           exclamation marks ('!') (since Linux 3.0).
       %g  (numeric) real GID of dumped process
       %h  hostname (same as nodename returned by uname(2))
       %i  TID of thread that triggered core dump, as seen in the PID
           namespace in which the thread resides (since Linux 3.18)
       %I  TID of thread that triggered core dump, as seen in the
           initial PID namespace (since Linux 3.18)
       %p  PID of dumped process, as seen in the PID namespace in which
           the process resides
       %P  PID of dumped process, as seen in the initial PID namespace
           (since Linux 3.12)
       %s  number of signal causing dump
       %t  time of dump, expressed as seconds since the Epoch,
           1970-01-01 00:00:00 +0000 (UTC)
       %u  (numeric) real UID of dumped process

2

在Ubuntu中,发生的任何崩溃都将登录到/ var / crash。生成的崩溃报告可以使用工具apport解压缩

apport-unpack /var/crash/_crash_file.crash'解压路径

然后可以使用以下命令读取解压缩报告中的核心转储

gdb'cat ExecutablePath'CoreDump

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.