何奇言 發表於 2021-7-18 14:32:00

ubuntu开启core dump

<h1 id="ubuntu开启core-dump">ubuntu开启core dump</h1>
<h2 id="1-ubuntu默认core-dump是关闭的">1. ubuntu默认core dump是关闭的</h2>
<p>通过命令<code>$ ulimit -a</code>查看:</p>
<p><img src="https://upload-images.jianshu.io/upload_images/6411513-45bfd351b9ec4e58.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240"></p>
<blockquote>
<p>core file size这一项为0,说明不生成core dump文件。</p>
</blockquote>
<h2 id="2-打开方法">2. 打开方法</h2>
<p>通过命令<code>$ ulimit -c unlimited</code>设置生成的core文件大小不限,也可以按自己的需求设置大小,设置完成后:</p>
<p><img src="https://upload-images.jianshu.io/upload_images/6411513-b651aa5bc42c27e1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240"></p>
<p>但是,这样设置会有一个问题,就是这个命令只在当前打开的shell中生效,关闭后就失效了。</p>
<h2 id="3-每次打开shell能够自动打开">3. 每次打开shell能够自动打开</h2>
<p>可以在<code>~/.bashrc</code>(只对当前用户生效)文件末尾添加<code>ulimit -c unlimited</code>,这样每次打开shell都会生效,可以使用编辑器或者输入命令<code>$ echo 'ulimit -c unlimited' &gt;&gt; ~/.bashrc</code>进行添加。</p>
<h2 id="4-测试">4. 测试</h2>
<p>源文件<code>test.cpp</code>:</p>
<pre><code class="language-c++">#include &lt;iostream&gt;
using namespace std;

int main() {
    int *p = nullptr;
    *p = 0; // 给空指针指向的地址赋值,引发core dump
    return 0;
}
</code></pre>
<p>编译:<code>$ g++ -g test.cpp -o test</code>(-g添加调试信息)<br>
运行:<code>$ ./test</code><br>
结果:</p>
<blockquote>
<p>Segmentation fault (core dumped)</p>
</blockquote>
<p>在与源文件相同目录下会生成名为<code>core</code>的core dump文件,使用gdb查看调用栈<code>$ gdb test core</code>:</p>
<p><img src="https://upload-images.jianshu.io/upload_images/6411513-f8de0043f505e077.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240"></p>
<p>通过gdb可以定位到发生core dump的位置为test.cpp文件的main()函数,具体在源文件的第6行,符合预期。</p>
<p><strong>2021.1.11更新:</strong></p>
<p>默认生成的core dump文件的名称为core,不够直观,可通过以下命令修改:<br>
<code>$ sudo sysctl -w kernel.core_pattern=core.%p.%s.%c.%d.%P.%E</code><br>
其中每个%开头的符号含义如下(来自man,命令:<code>man 5 core</code>):</p>
<pre><code class="language-shell">   Naming of core dump files
       By default, a core dump file is named core, but the /proc/sys/kernel/core_pattern file (sinceLinux2.6and
       2.4.21)canbesettodefine a template that is used to name core dump files.The template can contain %
       specifiers which are substituted by the following values when a core file is created:

         %%a single % character
         %ccore file size soft resource limit of crashing process (since Linux 2.6.24)
         %ddump mode—same as value returned by prctl(2) PR_GET_DUMPABLE (since Linux 3.7)
         %eexecutable filename (without path prefix)
         %Epathname of executable, with slashes ('/') replaced by exclamation marks ('!') (since Linux 3.0).
         %g(numeric) real GID of dumped process
         %hhostname (same as nodename returned by uname(2))
         %iTID of thread that triggered core dump, as seen in the PIDnamespaceinwhichthethreadresides
               (since Linux 3.18)
         %ITID of thread that triggered core dump, as seen in the initial PID namespace (since Linux 3.18)
         %pPID of dumped process, as seen in the PID namespace in which the process resides
         %PPID of dumped process, as seen in the initial PID namespace (since Linux 3.12)
         %snumber of signal causing dump
         %ttime of dump, expressed as seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC)
         %u(numeric) real UID of dumped process
</code></pre>
<blockquote>
<p>参考:https://stackoverflow.com/questions/17965/how-to-generate-a-core-dump-in-linux-on-a-segmentation-fault</p>
</blockquote><br><br>
来源:https://www.cnblogs.com/cong-wang/p/15026524.html
頁: [1]
查看完整版本: ubuntu开启core dump