Skip to content

Commit

Permalink
Create gcov_preload.c
Browse files Browse the repository at this point in the history
  • Loading branch information
pettershao-ragilenetworks authored Jul 1, 2022
1 parent ab433a1 commit e2fbf90
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/preload/gcov_preload/gcov_preload.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <signal.h>
#define SIMPLE_WAY

void sighandler(int signo)
{
#ifdef SIMPLE_WAY
exit(signo);
#else
extern void __gcov_flush();
__gcov_flush(); /* flush out gcov stats data */
raise(signo); /* raise the signal again to crash process */
#endif
}

/**
* The code for cracking the preloaded dynamic library gcov_preload.so is as follows, where __attribute__((constructor)) is the symbol of gcc,
* The modified function will be called before the main function is executed. We use it to intercept the exception signal to our own function, and then call __gcov_flush() to output the error message
*/

__attribute__ ((constructor))

void ctor()
{
int sigs[] = {
SIGILL, SIGFPE, SIGABRT, SIGBUS,
SIGSEGV, SIGHUP, SIGINT, SIGQUIT,
SIGTERM
};
int i;
struct sigaction sa;
sa.sa_handler = sighandler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESETHAND;

for(i = 0; i < sizeof(sigs)/sizeof(sigs[0]); ++i) {
if (sigaction(sigs[i], &sa, NULL) == -1) {
perror("Could not set signal handler");
}
}
}

0 comments on commit e2fbf90

Please sign in to comment.