<p>I have a process in Linux that's getting a segmentation fault.  How can I tell it to generate a core dump when it fails?</p>

[{'answer_id': 17994, 'author': 'Eli Courtwright', 'author_id': 1694, 'author_profile': 'https://Stackoverflow.com/users/1694', 'pm_score': 9, 'selected': True, 'text': "<p>This depends on what shell you are using.  If you are using bash, then the ulimit command controls several settings relating to program execution, such as whether you should dump core.  If you type</p>\n\n<pre><code>ulimit -c unlimited\n</code></pre>\n\n<p>then that will tell bash that its programs can dump cores of any size.  You can specify a size such as 52M instead of unlimited if you want, but in practice this shouldn't be necessary since the size of core files will probably never be an issue for you.</p>\n\n<p>In tcsh, you'd type</p>\n\n<pre><code>limit coredumpsize unlimited\n</code></pre>\n"}, {'answer_id': 18117, 'author': 'Mark Harrison', 'author_id': 116, 'author_profile': 'https://Stackoverflow.com/users/116', 'pm_score': 2, 'selected': False, 'text': '<p>By default you will get a core file.   Check to see that the current directory of the process is writable, or no core file will be created.</p>\n'}, {'answer_id': 18400, 'author': 'Nathan Fellman', 'author_id': 1084, 'author_profile': 'https://Stackoverflow.com/users/1084', 'pm_score': 5, 'selected': False, 'text': '<p>What I did at the end was attach gdb to the process before it crashed, and then when it got the segfault I executed the <code>generate-core-file</code> command.  That forced generation of a core dump.</p>\n'}, {'answer_id': 2139719, 'author': 't0mm13b', 'author_id': 206367, 'author_profile': 'https://Stackoverflow.com/users/206367', 'pm_score': 4, 'selected': False, 'text': '<p>Maybe you could do it this way, this program is a demonstration of how to trap a segmentation fault and shells out to a debugger (this is the original code used under <code>AIX</code>) and prints the stack trace up to the point of a segmentation fault. You will need to change the <code>sprintf</code> variable to use <code>gdb</code> in the case of Linux.</p>\n\n<pre><code>#include &lt;stdio.h&gt;\n#include &lt;signal.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;stdarg.h&gt;\n\nstatic void signal_handler(int);\nstatic void dumpstack(void);\nstatic void cleanup(void);\nvoid init_signals(void);\nvoid panic(const char *, ...);\n\nstruct sigaction sigact;\nchar *progname;\n\nint main(int argc, char **argv) {\n    char *s;\n    progname = *(argv);\n    atexit(cleanup);\n    init_signals();\n    printf("About to seg fault by assigning zero to *s\\n");\n    *s = 0;\n    sigemptyset(&amp;sigact.sa_mask);\n    return 0;\n}\n\nvoid init_signals(void) {\n    sigact.sa_handler = signal_handler;\n    sigemptyset(&amp;sigact.sa_mask);\n    sigact.sa_flags = 0;\n    sigaction(SIGINT, &amp;sigact, (struct sigaction *)NULL);\n\n    sigaddset(&amp;sigact.sa_mask, SIGSEGV);\n    sigaction(SIGSEGV, &amp;sigact, (struct sigaction *)NULL);\n\n    sigaddset(&amp;sigact.sa_mask, SIGBUS);\n    sigaction(SIGBUS, &amp;sigact, (struct sigaction *)NULL);\n\n    sigaddset(&amp;sigact.sa_mask, SIGQUIT);\n    sigaction(SIGQUIT, &amp;sigact, (struct sigaction *)NULL);\n\n    sigaddset(&amp;sigact.sa_mask, SIGHUP);\n    sigaction(SIGHUP, &amp;sigact, (struct sigaction *)NULL);\n\n    sigaddset(&amp;sigact.sa_mask, SIGKILL);\n    sigaction(SIGKILL, &amp;sigact, (struct sigaction *)NULL);\n}\n\nstatic void signal_handler(int sig) {\n    if (sig == SIGHUP) panic("FATAL: Program hanged up\\n");\n    if (sig == SIGSEGV || sig == SIGBUS){\n        dumpstack();\n        panic("FATAL: %s Fault. Logged StackTrace\\n", (sig == SIGSEGV) ? "Segmentation" : ((sig == SIGBUS) ? "Bus" : "Unknown"));\n    }\n    if (sig == SIGQUIT) panic("QUIT signal ended program\\n");\n    if (sig == SIGKILL) panic("KILL signal ended program\\n");\n    if (sig == SIGINT) ;\n}\n\nvoid panic(const char *fmt, ...) {\n    char buf[50];\n    va_list argptr;\n    va_start(argptr, fmt);\n    vsprintf(buf, fmt, argptr);\n    va_end(argptr);\n    fprintf(stderr, buf);\n    exit(-1);\n}\n\nstatic void dumpstack(void) {\n    /* Got this routine from http://www.whitefang.com/unix/faq_toc.html\n    ** Section 6.5. Modified to redirect to file to prevent clutter\n    */\n    /* This needs to be changed... */\n    char dbx[160];\n\n    sprintf(dbx, "echo \'where\\ndetach\' | dbx -a %d &gt; %s.dump", getpid(), progname);\n    /* Change the dbx to gdb */\n\n    system(dbx);\n    return;\n}\n\nvoid cleanup(void) {\n    sigemptyset(&amp;sigact.sa_mask);\n    /* Do any cleaning up chores here */\n}\n</code></pre>\n\n<p>You may have to additionally add a parameter to get gdb to dump the core as shown here in this blog <a href="http://prefetch.net/blog/index.php/2006/12/21/generating-core-files-from-gdb/" rel="noreferrer">here</a>.</p>\n'}, {'answer_id': 9191175, 'author': 'mlutescu', 'author_id': 1196859, 'author_profile': 'https://Stackoverflow.com/users/1196859', 'pm_score': 4, 'selected': False, 'text': '<p>There are more things that may influence the generation of a core dump.  I encountered these:</p>\n\n<ul>\n<li>the directory for the dump must be writable.  By default this is the current directory of the process, but that may be changed by setting <code>/proc/sys/kernel/core_pattern</code>.  </li>\n<li>in some conditions, the kernel value in <code>/proc/sys/fs/suid_dumpable</code> may prevent the core to be generated.</li>\n</ul>\n\n<p>There are more situations which may prevent the generation that are described in the man page - try <code>man core</code>.</p>\n'}, {'answer_id': 12968632, 'author': 'Edgar Jordi', 'author_id': 1758419, 'author_profile': 'https://Stackoverflow.com/users/1758419', 'pm_score': 3, 'selected': False, 'text': '<p>In order to activate the core dump do the following:</p>\n\n<ol>\n<li><p>In  <code>/etc/profile</code> comment the line:</p>\n\n<pre><code># ulimit -S -c 0 &gt; /dev/null 2&gt;&amp;1\n</code></pre></li>\n<li><p>In <code>/etc/security/limits.conf</code> comment out the line:</p>\n\n<pre><code>*               soft    core            0\n</code></pre></li>\n<li><p>execute the cmd <code>limit coredumpsize unlimited</code> and check it with cmd <code>limit</code>:</p>\n\n<pre><code># limit coredumpsize unlimited\n# limit\ncputime      unlimited\nfilesize     unlimited\ndatasize     unlimited\nstacksize    10240 kbytes\ncoredumpsize unlimited\nmemoryuse    unlimited\nvmemoryuse   unlimited\ndescriptors  1024\nmemorylocked 32 kbytes\nmaxproc      528383\n#\n</code></pre></li>\n<li><p>to check if the corefile gets written you can kill the relating process with cmd <code>kill -s SEGV &lt;PID&gt;</code> (should not be needed, just in case no core file gets written this can be used as a check):</p>\n\n<pre><code># kill -s SEGV &lt;PID&gt;\n</code></pre></li>\n</ol>\n\n<p>Once the corefile has been written make sure to deactivate the coredump settings again in the relating files (1./2./3.) !</p>\n'}, {'answer_id': 14709836, 'author': 'George Co', 'author_id': 893982, 'author_profile': 'https://Stackoverflow.com/users/893982', 'pm_score': 6, 'selected': False, 'text': "<p>As explained above the real question being asked here is how to enable core dumps on a system where they are not enabled. That question is answered here.</p>\n\n<p>If you've come here hoping to learn how to generate a core dump for a hung process, the answer is  </p>\n\n<pre><code>gcore &lt;pid&gt;\n</code></pre>\n\n<p>if gcore is not available on your system then  </p>\n\n<pre><code>kill -ABRT &lt;pid&gt;\n</code></pre>\n\n<p>Don't use kill -SEGV as that will often invoke a signal handler making it harder to diagnose the stuck process</p>\n"}, {'answer_id': 32461658, 'author': 'kenorb', 'author_id': 55075, 'author_profile': 'https://Stackoverflow.com/users/55075', 'pm_score': 5, 'selected': False, 'text': '<p>To check where the core dumps are generated, run:</p>\n\n<pre><code>sysctl kernel.core_pattern\n</code></pre>\n\n<p>or:</p>\n\n<pre><code>cat /proc/sys/kernel/core_pattern\n</code></pre>\n\n<p>where <code>%e</code> is the process name and <code>%t</code> the system time. You can change it in <code>/etc/sysctl.conf</code> and reloading by <code>sysctl -p</code>.</p>\n\n<p>If the core files are not generated (test it by: <code>sleep 10 &amp;</code> and <code>killall -SIGSEGV sleep</code>), check the limits by: <code>ulimit -a</code>.</p>\n\n<p>If your core file size is limited, run:</p>\n\n<pre><code>ulimit -c unlimited\n</code></pre>\n\n<p>to make it unlimited.</p>\n\n<p>Then test again, if the core dumping is successful, you will see “(core dumped)” after the segmentation fault indication as below:</p>\n\n<blockquote>\n  <p>Segmentation fault: 11  (core dumped)</p>\n</blockquote>\n\n<p>See also: <a href="https://stackoverflow.com/q/2065912/55075">core dumped - but core file is not in current directory?</a></p>\n\n<hr>\n\n<h3>Ubuntu</h3>\n\n<p>In Ubuntu the core dumps are handled by <a href="https://wiki.ubuntu.com/Apport" rel="noreferrer"><em>Apport</em></a> and can be located in <code>/var/crash/</code>. However, it is disabled by default in stable releases.</p>\n\n<p>For more details, please check: <a href="https://askubuntu.com/q/966407/78223">Where do I find the core dump in Ubuntu?</a>.</p>\n\n<h3>macOS</h3>\n\n<p>For macOS, see: <a href="https://stackoverflow.com/q/9412156/55075">How to generate core dumps in Mac OS X?</a></p>\n'}, {'answer_id': 35747215, 'author': 'mrgloom', 'author_id': 1179925, 'author_profile': 'https://Stackoverflow.com/users/1179925', 'pm_score': 4, 'selected': False, 'text': '<p>For Ubuntu 14.04</p>\n\n<ol>\n<li><p>Check  core dump enabled:</p>\n\n<pre><code>ulimit -a\n</code></pre></li>\n<li><p>One of the lines should be : </p>\n\n<pre><code>core file size          (blocks, -c) unlimited\n</code></pre></li>\n<li><p>If not :</p>\n\n<p><code>gedit ~/.bashrc</code> and add <code>ulimit -c unlimited</code> to end of file and save, re-run terminal.</p></li>\n<li><p>Build your application with debug information :</p>\n\n<p>In Makefile <code>-O0  -g</code></p></li>\n<li><p>Run application that create core dump (core dump file with name ‘core’ should be created near application_name file):</p>\n\n<pre><code>./application_name\n</code></pre></li>\n<li><p>Run under gdb:</p>\n\n<pre><code>gdb application_name core\n</code></pre></li>\n</ol>\n'}, {'answer_id': 52000790, 'author': 'kgbook', 'author_id': 5393174, 'author_profile': 'https://Stackoverflow.com/users/5393174', 'pm_score': 2, 'selected': False, 'text': '<p>Better to turn on core dump programmatically using system call <code>setrlimit</code>.</p>\n\n<p>example:</p>\n\n<pre><code>#include &lt;sys/resource.h&gt;\n\nbool enable_core_dump(){    \n    struct rlimit corelim;\n\n    corelim.rlim_cur = RLIM_INFINITY;\n    corelim.rlim_max = RLIM_INFINITY;\n\n    return (0 == setrlimit(RLIMIT_CORE, &amp;corelim));\n}\n</code></pre>\n'}, {'answer_id': 54275787, 'author': 'Pawel Veselov', 'author_id': 622266, 'author_profile': 'https://Stackoverflow.com/users/622266', 'pm_score': 2, 'selected': False, 'text': '<p>It\'s worth mentioning that if you have a <a href="https://www.freedesktop.org/wiki/Software/systemd/" rel="nofollow noreferrer">systemd</a> set up, then things are a little bit different. The set up typically would have the core files be piped, by means of <code>core_pattern</code> sysctl value, through <code>systemd-coredump(8)</code>. The core file size rlimit would typically be configured as "unlimited" already.</p>\n\n<p>It is then possible to retrieve the core dumps using <code>coredumpctl(1)</code>.</p>\n\n<p>The storage of core dumps, etc. is configured by <code>coredump.conf(5)</code>. There are examples of how to get the core files in the coredumpctl man page, but in short, it would look like this:</p>\n\n<p>Find the core file:</p>\n\n<pre><code>[vps@phoenix]~$ coredumpctl list test_me | tail -1\nSun 2019-01-20 11:17:33 CET   16163  1224  1224  11 present /home/vps/test_me\n</code></pre>\n\n<p>Get the core file:</p>\n\n<pre><code>[vps@phoenix]~$ coredumpctl -o test_me.core dump 16163\n</code></pre>\n'}, {'answer_id': 58593462, 'author': 'DarkTrick', 'author_id': 6702598, 'author_profile': 'https://Stackoverflow.com/users/6702598', 'pm_score': 3, 'selected': False, 'text': '<p><strong>Ubuntu 19.04</strong></p>\n\n<p>All other answers themselves didn\'t help me. But the following sum up did the job</p>\n\n<p>Create <code>~/.config/apport/settings</code> with the following content:</p>\n\n<pre><code>[main]\nunpackaged=true\n</code></pre>\n\n<p>(This tells apport to also write core dumps for custom apps)</p>\n\n<p>check: <code>ulimit -c</code>. If it outputs 0, fix it with</p>\n\n<pre><code>ulimit -c unlimited\n</code></pre>\n\n<p>Just for in case restart apport:    </p>\n\n<pre><code>sudo systemctl restart apport\n</code></pre>\n\n<p>Crash files are now written in <code>/var/crash/</code>. But you <em>cannot</em> use them with gdb. To use them with gdb, use</p>\n\n<pre><code>apport-unpack &lt;location_of_report&gt; &lt;target_directory&gt;\n</code></pre>\n\n<p>Further information:</p>\n\n<ul>\n<li>Some answers suggest changing <code>core_pattern</code>. Be aware, that that file might get overwritten by the apport service on restarting.</li>\n<li>Simply stopping apport did not do the job</li>\n<li>The <code>ulimit -c</code> value might get changed automatically while you\'re trying other answers of the web. Be sure to check it regularly during setting up your core dump creation.</li>\n</ul>\n\n<p>References:</p>\n\n<ul>\n<li><a href="https://stackoverflow.com/a/47481884/6702598">https://stackoverflow.com/a/47481884/6702598</a></li>\n</ul>\n'}, {'answer_id': 70872068, 'author': 'theicfire', 'author_id': 1394731, 'author_profile': 'https://Stackoverflow.com/users/1394731', 'pm_score': 2, 'selected': False, 'text': "<p>This is typically sufficient:</p>\n<pre><code>ulimit -c unlimited\n</code></pre>\n<p>Note this will <strong>not persist between ssh sections</strong>! To add persistence:</p>\n<pre><code>echo '* soft core unlimited' &gt;&gt; /etc/security/limits.conf\n</code></pre>\n<p>Now, if you're using Ubuntu, &quot;apport&quot; is probably running. Here's how to check:</p>\n<pre><code>sudo systemctl status apport.service\n</code></pre>\n<p>If it is, you'll probably find core dumps in one of these places:</p>\n<pre><code>/var/lib/apport/coredump \n/var/crash\n</code></pre>\n<h1>If you want to change the location of core dumps</h1>\n<p>Make sure that you have the <strong>permissions</strong> to create files and the <strong>directory exists</strong> in the directory you're sending a core dump to!</p>\n<p>Here's an example. Note this will <em>not persist across reboots</em>:</p>\n<pre><code>sysctl -w kernel.core_pattern=/coredumps/core-%e-%s-%u-%g-%p-%t\nmkdir /coredumps\n</code></pre>\n<p>Make sure that the process that's crashing has access to write to this. The easiest way would be an example like this:</p>\n<pre><code>chmod 777 /coredumps\n</code></pre>\n<h1>Test that core dumps works</h1>\n<pre><code>&gt; crash.c\ngcc -Wl,--defsym=main=0 crash.c\n./a.out\n==output== Segmentation fault (core dumped)\n</code></pre>\n<p>If it doesn't say &quot;core dumped&quot; above, something isn't working.</p>\n"}]
['https://Stackoverflow.com/questions/17965', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1084/']