Commit 987fdcb6 authored by robertdavidgraham's avatar robertdavidgraham
Browse files

backtrace

parent 093cbc91
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -7,9 +7,9 @@ SYS := $(shell gcc -dumpmachine)
# environment where things likely will work -- as well as anything
# works on the bajillion of different Linux environments
ifneq (, $(findstring linux, $(SYS)))
LIBS = -lpcap -lm -lrt -ldl -rdynamic -lpthread
LIBS = -lpcap -lm -lrt -ldl -lpthread
INCLUDES = -I.
FLAGS2 = 
FLAGS2 = -fno-omit-frame-pointer -rdynamic
endif

# MAC OS X
+6 −1
Original line number Diff line number Diff line
@@ -48,7 +48,7 @@
#include "siphash24.h"
#include "proto-x509.h"
#include "crypto-base64.h"      /* base64 encode/decode */

#include "pixie-backtrace.h"

#include <assert.h>
#include <limits.h>
@@ -1305,6 +1305,8 @@ main_scan(struct Masscan *masscan)
}




/***************************************************************************
 ***************************************************************************/
int main(int argc, char *argv[])
@@ -1318,6 +1320,9 @@ int main(int argc, char *argv[])

    global_now = time(0);

    /* Set system to report debug information on crash */
    pixie_backtrace_init(argv[0]);
    
    /*
     * Initialize those defaults that aren't zero
     */

src/pixie-backtrace.c

0 → 100644
+96 −0
Original line number Diff line number Diff line
/*
    When program crashes, print backtrace with line numbers
*/
#include "pixie-backtrace.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>

char global_self[512] = "";


#if defined(__linux__)
#include <unistd.h>
#include <execinfo.h>
#include <dlfcn.h>




#define BACKTRACE_SIZE 256
static void
handle_segfault(int sig)
{
	void *func[BACKTRACE_SIZE];
	char **symb = NULL;
	int size;

	size = backtrace(func, BACKTRACE_SIZE);
	symb = backtrace_symbols(func, size);
	while (size > 0) {
        const char *symbol = symb[size - 1];
        char foo[1024];
		printf("%d: [%s]\n", size, symbol);
        if (strstr(symbol, "[0x")) {
            char *p = strstr(symbol, "[0x") + 1;
            char *pp = strchr(p, ']');

            snprintf(foo, sizeof(foo), "addr2line -p -i -f -e %s %.*s",
                global_self,
                (unsigned)(pp-p),
                p);
            system(foo);
        }
		size --;
	}
    exit(1);
    return;
}


/***************************************************************************
 ***************************************************************************/
void
pixie_backtrace_finish(void)
{
}

/***************************************************************************
 ***************************************************************************/
void
pixie_backtrace_init(const char *self)
{

    /* Need to get a handle to the currently executing program. On Linux,
     * we'll get this with /proc/self/exe, but on other platforms, we may
     * need to do other things */
    /* TODO: should we use readlink() to get the actual filename? */
#if defined(__linux__)
    readlink("/proc/self/exe", global_self, sizeof(global_self));
#elif defined(__FreeBSD__)
    readlink("/proc/curproc/file", global_self, sizeof(global_self));
#elif defined(__Solaris__)
    readlink("/proc/self/path/a.out", global_self, sizeof(global_self));
#else
    snprintf(global_self, sizeof(global_self), "%s", self);
#endif


	signal(SIGSEGV, handle_segfault);
}
#elif defined(WIN32)
#include <Windows.h>
void
pixie_backtrace_init(const char *self)
{

    GetModuleFileNameA(NULL, global_self, sizeof(global_self));

}
#else
void
pixie_backtrace_init(const char *self)
{
}
#endif
 No newline at end of file

src/pixie-backtrace.h

0 → 100644
+14 −0
Original line number Diff line number Diff line
#ifndef PIXIE_BACKTRACE_H
#define PIXIE_BACKTRACE_H

/**
 * Call this function at program startup in order to insert a signal handler
 * that will be caught when the program crashes. This signal handler will
 * print debug infromation to the console, such as the line numbers where
 * the program crashes.
 */
void
pixie_backtrace_init(const char *self);

#endif
+2 −0
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@
    <ClCompile Include="..\src\out-redis.c" />
    <ClCompile Include="..\src\out-text.c" />
    <ClCompile Include="..\src\out-xml.c" />
    <ClCompile Include="..\src\pixie-backtrace.c" />
    <ClCompile Include="..\src\pixie-file.c" />
    <ClCompile Include="..\src\proto-arp.c" />
    <ClCompile Include="..\src\proto-banner1.c" />
@@ -96,6 +97,7 @@
    <ClInclude Include="..\src\out-record.h" />
    <ClInclude Include="..\src\output.h" />
    <ClInclude Include="..\src\packet-queue.h" />
    <ClInclude Include="..\src\pixie-backtrace.h" />
    <ClInclude Include="..\src\pixie-file.h" />
    <ClInclude Include="..\src\pixie-sockets.h" />
    <ClInclude Include="..\src\pixie-threads.h" />
Loading