Last active
January 20, 2016 03:30
-
-
Save kalashnikov/613258687d491944f587 to your computer and use it in GitHub Desktop.
Signal handler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <sys/types.h> | |
#include <unistd.h> | |
#include <signal.h> | |
#include <stdlib.h> | |
#include <string.h> | |
void sighandler(int signum) | |
{ | |
printf("Process %d got signal %d\n", getpid(), signum); | |
// Get process executive path | |
// http://stackoverflow.com/questions/646241/c-run-a-system-command-and-get-output | |
char rdcom[1024] ={'\0'}; | |
sprintf(rdcom, "readlink -f /proc/%d/exe", getpid()); | |
FILE *fp; | |
char result[1024]; | |
/* Open the command for reading. */ | |
fp = popen(rdcom, "r"); | |
if (fp == NULL) { | |
printf("Failed to get the process executive path\n" ); | |
return; | |
} | |
fgets(result, sizeof(result)-1, fp); | |
pclose(fp); | |
// ================================================================================ // | |
// Trim newline | |
// http://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input | |
size_t ln = strlen(result) - 1; | |
if (*result && result[ln] == '\n') result[ln] = '\0'; | |
printf("Process %d path: %s\n", getpid(), result); | |
char command[1024] ={'\0'}; | |
sprintf(command, "gdb %s %d", result, getpid()); | |
printf("%s\n",command); | |
system(command); | |
exit(1); | |
} | |
void cause_SIGSEGV(char* null_ptr) | |
{ | |
*null_ptr = 's'; | |
} | |
int main() | |
{ | |
signal(SIGSEGV, sighandler); | |
char* null_ptr = NULL; | |
cause_SIGSEGV(null_ptr); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment