macos 如何在 Mac OS X 中生成核心转储?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9412156/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 08:54:42  来源:igfitidea点击:

How to generate core dumps in Mac OS X?

macoscoredump

提问by alexpirine

It seems like I can not generate core dumps in Mac OS X 10.6.8.

似乎我无法在 Mac OS X 10.6.8 中生成核心转储。

$ ulimit -c unlimited
$ ./a.out 
Hello world!
Segmentation fault
$ find ~/ -type f -name core 

# ls -la /cores/
total 0
drwxrwxr-t@  2 root  admin    68 24 jui  2010 .
drwxrwxr-t  31 root  admin  1122 17 oct 15:52 ..

My current directory, my HOME and /cores/ remain empty…

我的当前目录,我的 HOME 和 /cores/ 保持空...

回答by kenorb

By default, crashes are reported into .crashfiles which can be found in /Library/Logs/DiagnosticReports(system-wide) and ~/Library/Logs/DiagnosticReports(user). These files can be opened by using Consoleapp, in Useror System Reports. The .crashfiles are in plain text format and should include relevant information about the crash.

默认情况下,崩溃报告到.crash可以在/Library/Logs/DiagnosticReports(系统范围)和~/Library/Logs/DiagnosticReports(用户)中找到的文件中。这些文件可以通过使用控制台应用程序在用户系统报告中打开。这些.crash文件为纯文本格式,并应包含有关崩溃的相关信息。



In order to activate the full core dumps, make sure that /coresdirectory has write permissions for the current user (test by: touch /cores/test && rm /cores/test). In addition, make sure that you don't have any limits on core file sizes by:

为了激活完整的核心转储,请确保该/cores目录具有当前用户的写权限(测试者:)touch /cores/test && rm /cores/test。此外,请确保您对核心文件大小没有任何限制:

ulimit -c unlimited

The name of the core dump file is in format: core.PID.

核心转储文件的名称格式为:core. PID.

If the directory is hidden, you can show the hidden files by:

如果目录是隐藏的,您可以通过以下方式显示隐藏文件:

defaults write com.apple.finder AppleShowAllFiles TRUE

You can test that by the following commands:

您可以通过以下命令进行测试:

sleep 100 &
killall -SIGSEGV sleep

which should say extra (core dumped), after Segmentation faultmessage.

(core dumped)Segmentation 错误消息之后,应该说额外的。

The core dump files should be found by default in /coresdirectory.

默认情况下,应该在/cores目录中找到核心转储文件。



Example by commands:

命令示例:

$ ulimit -c unlimited
$ sleep 100 &
$ killall -SIGSEGV sleep # Then press Enter few times till below message is shown
[1]+  Segmentation fault: 11  (core dumped) sleep 100
$ ls /cores
core.13652
$ lldb -c /cores/core.*
(lldb) target create --core "/cores/core.13652"
Core file '/cores/core.13652' (x86_64) was loaded.
(lldb) bt
* thread #1, stop reason = signal SIGSTOP
  * frame #0: 0x00007fffa7d13fde libsystem_kernel.dylib`__semwait_signal + 10
    frame #1: 0x00007fffa7c9ab92 libsystem_c.dylib`nanosleep + 199
    frame #2: 0x000000010c090002 sleep`rpl_nanosleep + 128


See also: Technical Note TN2118 - Kernel Core Dumps.

另请参阅:技术说明 TN2118 - 内核核心转储

回答by TOC

You can generate core dump files on Mac Os X like this:

您可以像这样在 Mac Os X 上生成核心转储文件:

  1. Create the file : /etc/launchd.conf, then :

    echo "limit core unlimited" | sudo tee -a /etc/launchd.conf

  2. Restart your Mac.

  1. 创建文件 : /etc/launchd.conf,然后:

    echo "limit core unlimited" | sudo tee -a /etc/launchd.conf

  2. 重新启动您的 Mac。

And that's it, the core dump files are generated in the /coresdirectory. Be careful the core dump files are large filesso when you finishing troubleshooting your code, remove them.

就是这样,核心转储文件在/cores目录中生成。请注意核心转储文件是大文件,因此在完成代码故障排除后,请将其删除。

回答by jww

Apple list a number of ways to generate core dump files in their TN2124or Mac OS X Debugging Magic.

Apple 在其TN2124Mac OS X Debugging Magic 中列出了多种生成核心转储文件的方法。

Here's a couple of extracts:

这里有几个摘录:

Prior to Mac OS X 10.4, you would enable core dumps on a system-wide basis by changing the line "COREDUMPS=-NO-" in /etc/hostconfig to "COREDUMPS=-YES-" and then restarting

在 Mac OS X 10.4 之前,您可以通过将 /etc/hostconfig 中的“COREDUMPS=-NO-”行更改为“COREDUMPS=-YES-”然后重新启动来在系统范围的基础上启用核心转储

And

# BSH
$ ulimit -c unlimited

# CSH
% limit coredumpsize unlimited

You can even do it programatically:

您甚至可以以编程方式执行此操作:

#include <sys/resource.h>

static bool EnableCoreDumps(void)
{
    struct rlimit   limit;

    limit.rlim_cur = RLIM_INFINITY;
    limit.rlim_max = RLIM_INFINITY;
    return setrlimit(RLIMIT_CORE, &limit) == 0;
}

回答by adamretter

On the Mac OS X Yosemite, you can enable the core dump on a per-process basis using LLDB. Assuming your process id is 51918, run the following from bash:

在 Mac OS X Yosemite 上,您可以使用 LLDB 在每个进程的基础上启用核心转储。假设您的进程 ID 是51918,请从 bash 运行以下命令:

$ lldb
(lldb) attach 51918
Process 51918 stopped
* thread #1: tid = 0x6bf50, 0x00007fff927c14de libsystem_kernel.dylib`mach_msg_trap + 10, queue = 'com.apple.main-thread', stop reason = signal SIGSTOP
    frame #0: 0x00007fff927c14de libsystem_kernel.dylib`mach_msg_trap + 10
libsystem_kernel.dylib`mach_msg_trap:
->  0x7fff927c14de <+10>: retq   
    0x7fff927c14df <+11>: nop    

libsystem_kernel.dylib`mach_msg_overwrite_trap:
    0x7fff927c14e0 <+0>:  movq   %rcx, %r10
    0x7fff927c14e3 <+3>:  movl   
kill -ABRT 51918
x1000020, %eax Executable module set to "/Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home/bin/java". Architecture set to: x86_64h-apple-macosx. (lldb) expr long long * $limit = (long long *)malloc(16) (lldb) expr $limit[0] = 0x7fffffffffffffff (long long) ##代码## = 9223372036854775807 (lldb) expr $limit[1] = 0x7fffffffffffffff (long long) = 9223372036854775807 (lldb) expr (int)setrlimit(4, $limit) (int) = 0 (lldb) detach Process 51918 detached

If you process causes a segmentation fault, you will now find a core in /cores. You can check this be sending a SIGABRT to your process after running the above:

如果您的进程导致分段错误,您现在将在/cores. 运行上述命令后,您可以检查是否向您的进程发送 SIGABRT:

##代码##

Please note that attaching to process owned by root won't work on recent macOSes (El Capitan and above) by default due to System Integrity Protection.

请注意,由于系统完整性保护,默认情况下附加到 root 拥有的进程在最近的 macOS(El Capitan 及更高版本)上不起作用。