Move ABRThandler func out of internal lib

This commit is contained in:
Casey McGinty 2018-09-11 11:39:49 -07:00
parent 2a3967b7c4
commit d4337b6f1d
No known key found for this signature in database
GPG Key ID: BA0229620046D260
6 changed files with 71 additions and 71 deletions

View File

@ -16,10 +16,6 @@
#include <stdlib.h> /* malloc, calloc, free */
#include <string.h> /* memset */
#include <stdio.h> /* fprintf(), stderr */
#include <signal.h> /* signal() */
#ifndef _WIN32
#include <execinfo.h> /* backtrace, backtrace_symbols, symbollist */
#endif
#include "error_private.h"
#include "zstd_internal.h"
@ -90,64 +86,3 @@ void ZSTD_free(void* ptr, ZSTD_customMem customMem)
free(ptr);
}
}
/*-*********************************************************
* Termination signal trapping (Print debug stack trace)
***********************************************************/
#define MAX_STACK_FRAMES 50
#ifndef _WIN32
#ifdef __linux__
#define START_STACK_FRAME 2
#elif defined __APPLE__
#define START_STACK_FRAME 4
#endif
static void ABRThandler(int sig)
{
const char* name;
void* addrlist[MAX_STACK_FRAMES + 1];
char** symbollist;
U32 addrlen, i;
switch (sig) {
case SIGABRT: name = "SIGABRT"; break;
case SIGFPE: name = "SIGFPE"; break;
case SIGILL: name = "SIGILL"; break;
case SIGINT: name = "SIGINT"; break;
case SIGSEGV: name = "SIGSEGV"; break;
default: name = "UNKNOWN"; break;
}
DISPLAY("Caught %s signal, printing stack:\n", name);
// Retrieve current stack addresses.
addrlen = backtrace(addrlist, sizeof(addrlist) / sizeof(void*));
if (addrlen == 0) {
DISPLAY("\n");
return;
}
// Create readable strings to each frame.
symbollist = backtrace_symbols(addrlist, addrlen);
// Print the stack trace, excluding calls handling the signal.
for (i = START_STACK_FRAME; i < addrlen; i++) {
DISPLAY("%s\n", symbollist[i]);
}
free(symbollist);
// Reset and raise the signal so default handler runs.
signal(sig, SIG_DFL);
raise(sig);
}
#endif
void ZSTD_addAbortHandler()
{
#ifndef _WIN32
signal(SIGABRT, ABRThandler);
signal(SIGFPE, ABRThandler);
signal(SIGILL, ABRThandler);
signal(SIGSEGV, ABRThandler);
signal(SIGBUS, ABRThandler);
#endif
}

View File

@ -206,9 +206,6 @@ void* ZSTD_malloc(size_t size, ZSTD_customMem customMem);
void* ZSTD_calloc(size_t size, ZSTD_customMem customMem);
void ZSTD_free(void* ptr, ZSTD_customMem customMem);
/* custom crash signal handler */
void ZSTD_addAbortHandler(void);
MEM_STATIC U32 ZSTD_highbit32(U32 val) /* compress, dictBuilder, decodeCorpus */
{

View File

@ -30,6 +30,10 @@
#include <stdlib.h> /* malloc, free */
#include <string.h> /* strcmp, strlen */
#include <errno.h> /* errno */
#include <signal.h>
#ifndef _WIN32
#include <execinfo.h> /* backtrace, backtrace_symbols */
#endif
#if defined (_MSC_VER)
# include <sys/stat.h>
@ -124,8 +128,6 @@ static UTIL_time_t g_displayClock = UTIL_TIME_INITIALIZER;
/*-************************************
* Signal (Ctrl-C trapping)
**************************************/
#include <signal.h>
static const char* g_artefact = NULL;
static void INThandler(int sig)
{
@ -157,6 +159,59 @@ static void clearHandler(void)
}
/*-*********************************************************
* Termination signal trapping (Print debug stack trace)
***********************************************************/
#define MAX_STACK_FRAMES 50
#ifndef _WIN32
static void ABRThandler(int sig) {
const char* name;
void* addrlist[MAX_STACK_FRAMES + 1];
char** symbollist;
U32 addrlen, i;
switch (sig) {
case SIGABRT: name = "SIGABRT"; break;
case SIGFPE: name = "SIGFPE"; break;
case SIGILL: name = "SIGILL"; break;
case SIGINT: name = "SIGINT"; break;
case SIGSEGV: name = "SIGSEGV"; break;
default: name = "UNKNOWN";
}
DISPLAY("Caught %s signal, printing stack:\n", name);
// Retrieve current stack addresses.
addrlen = backtrace(addrlist, sizeof(addrlist) / sizeof(void*));
if (addrlen == 0) {
DISPLAY("\n");
return;
}
// Create readable strings to each frame.
symbollist = backtrace_symbols(addrlist, addrlen);
// Print the stack trace, excluding calls handling the signal.
for (i = ZSTD_START_SYMBOLLIST_FRAME; i < addrlen; i++) {
DISPLAY("%s\n", symbollist[i]);
}
free(symbollist);
// Reset and raise the signal so default handler runs.
signal(sig, SIG_DFL);
raise(sig);
}
#endif
void FIO_addAbortHandler()
{
#ifndef _WIN32
signal(SIGABRT, ABRThandler);
signal(SIGFPE, ABRThandler);
signal(SIGILL, ABRThandler);
signal(SIGSEGV, ABRThandler);
signal(SIGBUS, ABRThandler);
#endif
}
/*-************************************************************
* Avoid fseek()'s 2GiB barrier with MSVC, macOS, *BSD, MinGW
***************************************************************/

View File

@ -95,6 +95,9 @@ int FIO_decompressMultipleFilenames(const char** srcNamesTable, unsigned nbFiles
const char* dictFileName);
/* custom crash signal handler */
void FIO_addAbortHandler(void);
#if defined (__cplusplus)
}
#endif

View File

@ -148,6 +148,17 @@ static __inline int IS_CONSOLE(FILE* stdStream) {
#endif
#ifndef ZSTD_START_SYMBOLLIST_FRAME
# ifdef __linux__
# define ZSTD_START_SYMBOLLIST_FRAME 2
# elif defined __APPLE__
# define ZSTD_START_SYMBOLLIST_FRAME 4
# else
# define ZSTD_START_SYMBOLLIST_FRAME 0
# endif
#endif
#if defined (__cplusplus)
}
#endif

View File

@ -39,7 +39,6 @@
#endif
#define ZSTD_STATIC_LINKING_ONLY /* ZSTD_maxCLevel */
#include "zstd.h" /* ZSTD_VERSION_STRING */
#include "zstd_internal.h" /* ZSTD_addAbortHandler */
/*-************************************