title: How does CoffeeCatch jump back to the COFFEE_CATCH clause?
body: <p>I recently discovered <a href="https://github.com/xroche/coffeecatch" rel="nofollow noreferrer">CoffeeCatch</a>, which I want to use to log the C/C++ native crashes on Android. I haven't managed, but still I am curious about how it works internally.</p>
  <p>My understanding is that it basically catches and emitted signal (e.g. SIGSEGV) and allows the user to do something with it; in my case I would like save the stack trace and crash.</p>
  <p>It is used like this:</p>
  <pre><code>COFFEE_TRY_JNI(env, *retcode = call_dangerous_function(env, object));
  </code></pre>
  <p>Where <code>call_dangerous_function()</code> is a function that could crash and therefore emit a signal.</p>
  <p>The macro is defined <a href="https://github.com/xroche/coffeecatch/blob/master/coffeejni.h#L117-L124" rel="nofollow noreferrer">here</a>:</p>
  <pre class="lang-c prettyprint-override"><code>#define COFFEE_TRY_JNI(ENV, CODE)       \
    do {                                  \
      COFFEE_TRY() {                      \
        CODE;                             \
      } COFFEE_CATCH() {                  \
        coffeecatch_throw_exception(ENV); \
      } COFFEE_END();                     \
    } while(0)
  </code></pre>
  <p>Which <a href="https://github.com/xroche/coffeecatch/blob/master/coffeecatch.h#L215-L220" rel="nofollow noreferrer">resolves to</a> something like this:</p>
  <pre class="lang-c prettyprint-override"><code>  do {                                  
        if (coffeecatch_inside() || 
        (coffeecatch_setup() == 0 
         &amp;&amp; sigsetjmp(*coffeecatch_get_ctx(), 1) == 0)) {                      \
        call_dangerous_function(env, object);                             
      } else {                  
        coffeecatch_throw_exception(ENV); 
      } coffeecatch_cleanup();                     
    } while(0)
  </code></pre>
  <p>Here, my understanding is that <code>sigsetjmp(*coffeecatch_get_ctx(), 1) == 0)</code> somehow sets a pointer to the current &quot;context&quot;, which I guess is something like the execution state of this thread at this moment.</p>
  <p>So somehow, later, <a href="https://github.com/xroche/coffeecatch/blob/master/coffeecatch.c#L498" rel="nofollow noreferrer">siglongjmp(t-&gt;ctx, code);</a> will be called (after the environment has been somehow prepared for it) to jump back here.</p>
  <p>What's not clear to me is if we then jump at the <code>if</code> statement (and evaluate the statement again), or somehow end up into the <code>else</code> statement for some reason.</p>
  <p>My expectation is that <code>siglongjmp</code> now results in going into the <code>else</code> statement (where in my case a Java exception could be thrown). But:</p>
  <ul>
  <li>First, that's not what I see (<a href="https://stackoverflow.com/questions/72600920/how-can-i-catch-sigsegv-using-coffeecatch">my program</a> emits the SIGSEGV signal right after <code>siglongjmp</code> is called without ever reaching the <code>else</code> statement).</li>
  <li>Second, I don't get where <code>siglongjmp</code> should actually jump. If it goes to the <code>if</code> statement again and evaluates the condition again, it will evaluate to <code>true</code> again and that's a loop. How would it go to <code>else</code>?</li>
  </ul>