<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community:  Carlos</title>
    <description>The latest articles on DEV Community by  Carlos (@jcarlosv).</description>
    <link>https://dev.to/jcarlosv</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F677209%2F5136408c-4f06-47d8-b893-164f01eb53e3.png</url>
      <title>DEV Community:  Carlos</title>
      <link>https://dev.to/jcarlosv</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jcarlosv"/>
    <language>en</language>
    <item>
      <title>Core dump for Rust</title>
      <dc:creator> Carlos</dc:creator>
      <pubDate>Mon, 02 Aug 2021 18:04:18 +0000</pubDate>
      <link>https://dev.to/jcarlosv/core-dump-for-rust-10nm</link>
      <guid>https://dev.to/jcarlosv/core-dump-for-rust-10nm</guid>
      <description>&lt;h2&gt;
  
  
  Intro
&lt;/h2&gt;

&lt;p&gt;From Wikipedia:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In computing, a core dump,[a] memory dump, crash dump, system dump, or ABEND dump[1] consists of the recorded state of the working memory of a computer program at a specific time, generally when the program has crashed or otherwise terminated abnormally.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In GNU/Linux core dump generation is handle by the kernel. And it can pipe the generation to a user-space program like &lt;code&gt;systemd-coredump&lt;/code&gt; or  &lt;code&gt;apport&lt;/code&gt;. You can see if is handle by an external program looking into:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cat&lt;/span&gt; /proc/sys/kernel/core_pattern
|/usr/share/apport/apport %p %s %c
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From &lt;code&gt;core(5)&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The default action of certain signals is to cause a process to&lt;br&gt;
terminate and produce a core dump file, a file containing an&lt;br&gt;
image of the process's memory at the time of termination.  This&lt;br&gt;
image can be used in a debugger (e.g., gdb(1)) to inspect the&lt;br&gt;
state of the program at the time that it terminated.  A list of&lt;br&gt;
the signals which cause a process to dump core can be found in&lt;br&gt;
signal(7).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Signals that generate a core dump.
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;SIGABRT Abort signal from abort(3)&lt;/li&gt;
&lt;li&gt;SIGBUS  Bus error (bad memory access)&lt;/li&gt;
&lt;li&gt;SIGFPE  Floating-point exception&lt;/li&gt;
&lt;li&gt;SIGILL  Illegal Instruction&lt;/li&gt;
&lt;li&gt;SIGSEGV Invalid memory reference
See the full documentation for a complete list.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Generate a core dump on Rust panics.
&lt;/h2&gt;

&lt;p&gt;While rust panics itself help with the great information they provide. Sometimes it is really useful debug using a core dump specially if your binary has debug symbols plus the gdb wrappers provided by &lt;code&gt;rust-gdb&lt;/code&gt;. &lt;/p&gt;

&lt;h3&gt;
  
  
  Instrument panic handler to generate coredump.
&lt;/h3&gt;

&lt;p&gt;Most of fatal errors in Rust comes from a panic, &lt;code&gt;unwraps&lt;/code&gt;  that we considered safe (probably?).&lt;br&gt;
Based in a really helpful gist I found:&lt;br&gt;
&lt;a href="https://gist.github.com/epilys/a6caba03cb02cfd2880fd80755cd08b8"&gt;https://gist.github.com/epilys/a6caba03cb02cfd2880fd80755cd08b8&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;First modify how the panic is handled by printing the panic trace as usual, but then send a signal that generates a  core to our own process. In the example gist, the signal  is &lt;code&gt;SIGQUIT&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;panic_handler_generate_coredump&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;default_panic&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;panic&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;take_hook&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;panic&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;set_hook&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;move&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt;&lt;span class="n"&gt;panic_info&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;default_panic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;panic_info&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;pid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;process&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;id&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;libc&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;kill&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;libc&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;SIGQUIT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;convert&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;TryInto&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;unsafe&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;kill&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="nf"&gt;.try_into&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.unwrap&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;SIGQUIT&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="p"&gt;}));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="nf"&gt;panic_handler_generate_coredump&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nd"&gt;panic!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"don't Panic!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Compile config
&lt;/h3&gt;

&lt;p&gt;Optional:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight toml"&gt;&lt;code&gt;&lt;span class="nn"&gt;[profile.dev]&lt;/span&gt;
&lt;span class="py"&gt;opt-level&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;  &lt;span class="c"&gt;# Controls the --opt-level the compiler builds with&lt;/span&gt;
&lt;span class="py"&gt;debug&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;   &lt;span class="c"&gt;# Controls whether the compiler passes `-g`&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Enable core dump collection.
&lt;/h1&gt;

&lt;p&gt;Optional: Change the path where coredump is generated&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;sysctl &lt;span class="nt"&gt;-w&lt;/span&gt; kernel.core_pattern&lt;span class="o"&gt;=&lt;/span&gt;/tmp/core.%u.%p.%t
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or check where is generated:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cat /proc/sys/kernel/core_pattern
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Modify core file size:
&lt;/h2&gt;

&lt;p&gt;From : &lt;a href="https://community.perforce.com/s/article/2979"&gt;https://community.perforce.com/s/article/2979&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;For most distributions the core file size limitation is set to 0 to produce no core files at all. To find out the limit on the system in question, issue the command:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ulimit -c
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;If it is set to 0 then no core files are produced. To allow core files to be produced, set a core file size that is not 0:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ulimit -c 100000000000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ulimit -c unlimited
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Generate core
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;cargo run
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Debug core
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rust-gdb &amp;lt;path-to-binary&amp;gt; /tmp/&amp;lt;core_path&amp;gt;
thread apply all bt full
#or
where
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>rust</category>
    </item>
  </channel>
</rss>
