Skip to content

Instantly share code, notes, and snippets.

@Frityet
Last active December 15, 2022 23:32
Show Gist options
  • Save Frityet/2c0de9bdb164bbf4d0eb0e8434fd757e to your computer and use it in GitHub Desktop.
Save Frityet/2c0de9bdb164bbf4d0eb0e8434fd757e to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <setjmp.h>
#include <signal.h>
static int sig_to_catch;
static void (^execption_handler)(int sig);
static jmp_buf exception_buffer;
typedef int Exception_t;
#define try void (^_exec_try_)(void) = ^
#define catch(ex) ; signal(ex, signal_handler); sig_to_catch = ex; execption_handler = ^
#define execute() ;if (setjmp(exception_buffer) == 0) _exec_try_();
#define throw(x) kill(getpid(), x)
static void signal_handler(int i)
{
if (i != sig_to_catch) return;
execption_handler(i);
sig_to_catch = 0;
longjmp(exception_buffer, i);
}
int main(void)
{
int *ptr;
int i = 10;
try {
throw(SIGSEGV);
} catch (SIGSEGV) (Exception_t signum) {
puts("oh noes!");
printf("Could not set to %d!\nReason: %s\n", i, strsignal(signum));
printf("Backtrace:\n");
void *trace[128] = {0};
int i = backtrace(trace, 128);
backtrace_symbols_fd(trace, i, STDERR_FILENO);
} execute();
puts("Done");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment