How to generate core dumps in Mac OS X?

纵然是瞬间 提交于 2019-11-27 03:59:01
TOC

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

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

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

  2. Restart your Mac.

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

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


In order to activate the full core dumps, make sure that /cores directory 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:

ulimit -c unlimited

The name of the core dump file is in format: 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 fault message.

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


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.

Apple list a number of ways to generate core dump files in their TN2124 or Mac 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

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;
}
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:

$ 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   $0x1000020, %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) $0 = 9223372036854775807
(lldb) expr $limit[1] = 0x7fffffffffffffff
(long long) $1 = 9223372036854775807
(lldb) expr (int)setrlimit(4, $limit)
(int) $2 = 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:

kill -ABRT 51918

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!