title: Getting fault address that generated a UNIX signal
body: <p>I am interested in a signal handler which can identify the address of the instruction which caused the problem.</p>
  
  <p>I know about <code>siginfo_t</code> and <code>__builtin_return_address</code> and neither seem to work:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;signal.h&gt;
  
  void handler (int, siginfo_t *, void *);
  
  int main ()
  {
  begin:
      std :: cerr &lt;&lt; &amp;&amp;begin &lt;&lt; " ~ " &lt;&lt; &amp;&amp;before &lt;&lt; " ~ " &lt;&lt; &amp;&amp;after &lt;&lt; "\n";
  
      struct sigaction s;
      s .sa_flags = SA_SIGINFO;
      sigemptyset (&amp; s .sa_mask);
      s .sa_sigaction = handler;
      sigaction (SIGSEGV, &amp;s, NULL);
  
      int * i = NULL;
  before:
      *i = 0;
  after:
      std :: cout &lt;&lt; "End.\n";
  }
  
  void handler (int, siginfo_t *si, void *)
  {
      std :: cerr &lt;&lt; "si:" &lt;&lt; si -&gt; si_addr &lt;&lt; "\n";
      std :: cerr &lt;&lt; "At: " &lt;&lt; __builtin_return_address (0) &lt;&lt; "\n";
      std :: cerr &lt;&lt; "At: " &lt;&lt; __builtin_return_address (1) &lt;&lt; "\n";
      std :: cerr &lt;&lt; "At: " &lt;&lt; __builtin_return_address (2) &lt;&lt; "\n";
      std :: cerr &lt;&lt; "At: " &lt;&lt; __builtin_return_address (3) &lt;&lt; "\n";
      std :: cerr &lt;&lt; "At: " &lt;&lt; __builtin_return_address (4) &lt;&lt; "\n";
      std :: cerr &lt;&lt; "At: " &lt;&lt; __builtin_return_address (5) &lt;&lt; "\n";
  }
  </code></pre>
  
  <p>This outputs something like:</p>
  
  <pre><code>0x10978 ~ 0x10a4c ~ 0x10a54
  si:0
  At: 0xfb945364
  At: 0xfb939e64
  At: 0x10a40
  At: 0x10740
  At: 0
  At: Segmentation Fault
  </code></pre>
  
  <p>So <code>siginfo_t</code> is NULL and <code>__builtin_return_address</code> is yielding values somewhere <em>in between</em> the named labels.</p>
  
  <p>I was expecting both of these to return the value of <code>&amp;&amp;before</code>. Am I using these functions correctly?</p>
  
  <p>Tested on Linux 2.6.9-89.0.9.Elsmp and SunOS.</p>