Skip to content

Instantly share code, notes, and snippets.

@dev-area
Created November 12, 2017 17:53
Show Gist options
  • Save dev-area/108da57c2df7c9c46ecd77812031dca2 to your computer and use it in GitHub Desktop.
Save dev-area/108da57c2df7c9c46ecd77812031dca2 to your computer and use it in GitHub Desktop.
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <sys/ucontext.h>
#include <ucontext.h>
void setHandler(void (*handler)(int,siginfo_t *,void *))
{
struct sigaction action;
action.sa_flags = SA_SIGINFO;
action.sa_sigaction = handler;
if (sigaction(SIGFPE, &action, NULL) == -1) {
perror("sigusr: sigaction");
_exit(1);
}
if (sigaction(SIGSEGV, &action, NULL) == -1) {
perror("sigusr: sigaction");
_exit(1);
}
if (sigaction(SIGILL, &action, NULL) == -1) {
perror("sigusr: sigaction");
_exit(1);
}
if (sigaction(SIGBUS, &action, NULL) == -1) {
perror("sigusr: sigaction");
_exit(1);
}
}
void continue_after_crash(void)
{
printf("normal running now\n");
exit(0);
}
void catchit(int signo, siginfo_t *info, void *extra)
{
ucontext_t *p=(ucontext_t *)extra;
int x;
printf("Signal %d received from parent\n", signo);
printf("siginfo address=%x\n",info->si_addr);
x= p->uc_mcontext.gregs[REG_RIP];
// x= p->uc_mcontext.arm_pc;
printf("address = %x\n",x);
// uncomment one of the following lines
//p->uc_mcontext.gregs[REG_RIP] += 6;
//setHandler(SIG_DFL);
//p->uc_mcontext.gregs[REG_RIP] = (unsigned int)continue_after_crash;
//abort();
}
int main()
{
int x=9,y=0,z=10;
int *p=NULL;
setHandler(catchit);
p=0x90; // put a wrong address
*p=100;
printf("Alive???\n");
z=x/y;
printf("%d\n",z);
return 0;
}
@jonnygrant
Copy link

An interesting article and example. Gcc warns that those %x should be %p

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment