title: Debugging segfault with no apparent cause in gdb?
body: <p>gdb was reporting that my C code was crashing somewhere in <code>malloc()</code>, so I linked my code with Electric Fence to pinpoint the actual source of the memory error. Now my code is segfaulting much earlier, but gdb's output is even more confusing:</p>
  
  <pre class="lang-none prettyprint-override"><code>Program received signal SIGSEGV, Segmentation fault.
  [Switching to Thread 0x30026b00 (LWP 4003)]
  0x10007c30 in simulated_status (axis=1, F=0x300e7fa8, B=0x1003a520, A=0x3013b000, p=0x1003b258, XS=0x3013b000)
      at ccp_gch.c:799
  </code></pre>
  
  <p>EDIT: The full backtrace:</p>
  
  <pre class="lang-none prettyprint-override"><code>(gdb) bt
  #0  0x10007c30 in simulated_status (axis=1, F=0x300e7fa8, B=0x1003a520, A=0x3013b000, p=0x1003b258, XS=0x3013b000)
      at ccp_gch.c:799
  #1  0x10007df8 in execute_QUERY (F=0x300e7fa8, B=0x1003a520, iData=0x7fb615c0) at ccp_gch.c:836
  #2  0x10009680 in execute_DATA_cmd (P=0x300e7fa8, B=0x7fb615cc, R_type=0x7fb615d0, iData=0x7fb615c0)
      at ccp_gch.c:1581
  #3  0x10015bd8 in do_volley (client=13) at session.c:76
  #4  0x10015ef4 in do_dialogue (v=12, port=2007) at session.c:149
  #5  0x10016350 in do_session (starting_port=2007, ports=1) at session.c:245
  #6  0x100056e4 in main (argc=2, argv=0x7fb618f4) at main.c:271
  </code></pre>
  
  <p>The relevant code (slightly modified due to reasons):</p>
  
  <pre><code>796  static uint32_t simulated_status(
  797      unsigned axis, struct foo *F, struct bar *B, struct Axis *A, BAZ *p, uint64_t *XS)
  798  {
  799      uint32_t result = A-&gt;status;
  800      *XS = get_status(axis);
  801      if (!some_function(p)) {
  802          ...
  </code></pre>
  
  <p>The obvious thing to check would be whether <code>A-&gt;status</code> is valid memory, but it is. Removing the assignment pushes the segfault to line 800, and removing <em>that</em> assignment causes some other assignment in the if-block to segfault. It looks as though either accessing an argument passed to the function or writing to a local variable is what's causing the segfault, but everything points to valid memory according to gdb.</p>
  
  <p>How am I to interpret this? I've never seen anything like this before, so any suggestions / pointers in the right direction would be appreciated. I'm using GNU gdb 6.8-debian, Electric Fence 2.1, and running on a PowerPC 405 (uname reports <code>Linux powerpmac 2.6.30.3 #24 [...] ppc GNU/Linux</code>).</p>