Skip to content

Commit

Permalink
added stacktrace print on segmentation fault
Browse files Browse the repository at this point in the history
  • Loading branch information
havraji6 committed Jul 14, 2021
1 parent 0cb1586 commit 8ab1a1b
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 2 deletions.
6 changes: 4 additions & 2 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ bin_PROGRAMS=ipfixprobe
DISTCHECK_CONFIGURE_FLAGS="--with-systemdsystemunitdir=$$dc_install_base/$(systemdsystemunitdir)"

if HAVE_NDP
ipfixprobe_LDFLAGS=-lpthread -lnuma -lnfb -lndpRI -L${srcdir}/nfbCInterface/
ipfixprobe_LDFLAGS=-lpthread -lunwind -lnuma -lnfb -lndpRI -L${srcdir}/nfbCInterface/
else
ipfixprobe_LDFLAGS=-lpthread
ipfixprobe_LDFLAGS=-lpthread -lunwind
endif

ipfixprobe_CXXFLAGS=-std=gnu++11 -Wno-write-strings -I./nfbCInterface/include/


ipfixprobe_src=main.cpp \
ipfixprobe.h \
stacktrace.cpp \
stacktrace.h \
packet.h \
packetreceiver.h \
flowexporter.h \
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ This NEMEA module creates biflows from input PCAP file / network interface and e
## Requirements
- To compile this module you will need [libpcap](http://www.tcpdump.org/) development library installed,
OR netcope-common to support high-speed transfers from [COMBO cards](https://www.liberouter.org/technologies/cards/) (NDP interface).
- and libunwind-devel package

## Build & Installation

Expand Down Expand Up @@ -58,6 +59,7 @@ Install CygWin and the following packages:
- binutils
- gcc-core
- gcc-g++
- libunwind-devel

Download npcap SDK [https://nmap.org/npcap/dist/npcap-sdk-1.07.zip](https://nmap.org/npcap/dist/npcap-sdk-1.07.zip) and copy content of the `Include` folder to `/usr/include` folder in your cygwin root installation folder (`C:\cygwin64\usr\include` for example). Then copy files of the `Lib` folder to `/lib` folder (or `Lib/x64/` based on your architecture).

Expand Down
4 changes: 4 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ if [[ -z "$HAVE_NDP_TRUE" ]]; then
RPM_BUILDREQ+=" netcope-common-devel"
fi

AC_CHECK_HEADER(libunwind.h,
AC_CHECK_LIB(unwind, unw_getcontext, AC_MSG_ERROR([libunwind not found. The ipfixprobe flow exporter will not be compiled.])),
AC_MSG_ERROR([libunwind.h not found. The ipfixprobe flow exporter will not be compiled.]))

if test x${withndp} = xno; then
AC_CHECK_HEADER(pcap.h,
AC_CHECK_LIB(pcap, pcap_open_live, [libpcap=yes],
Expand Down
6 changes: 6 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
#include "stats.h"
#include "conversion.h"
#include "ring.h"
#include "stacktrace.h"

#include "httpplugin.h"
#include "rtspplugin.h"
Expand Down Expand Up @@ -498,6 +499,10 @@ inline int error(const string &e)
*/
void signal_handler(int sig)
{
if (sig == SIGSEGV) {
stacktrace_print(sig);
abort();
}
stop = 1;
}

Expand Down Expand Up @@ -656,6 +661,7 @@ int main(int argc, char *argv[])

signal(SIGTERM, signal_handler);
signal(SIGINT, signal_handler);
signal(SIGSEGV, signal_handler);
signal(SIGPIPE, SIG_IGN);

signed char opt;
Expand Down
115 changes: 115 additions & 0 deletions stacktrace.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/**
* \file stacktrace.cpp
* \brief Stack trace functions source file
* \author Jiri Havranek <[email protected]>
* \date 2021
*/
/*
* Copyright (C) 2021 CESNET
*
* LICENSE TERMS
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name of the Company nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* ALTERNATIVELY, provided that this notice is retained in full, this
* product may be distributed under the terms of the GNU General Public
* License (GPL) version 2 or later, in which case the provisions
* of the GPL apply INSTEAD OF those given above.
*
* This software is provided ``as is'', and any express or implied
* warranties, including, but not limited to, the implied warranties of
* merchantability and fitness for a particular purpose are disclaimed.
* In no event shall the company or contributors be liable for any
* direct, indirect, incidental, special, exemplary, or consequential
* damages (including, but not limited to, procurement of substitute
* goods or services; loss of use, data, or profits; or business
* interruption) however caused and on any theory of liability, whether
* in contract, strict liability, or tort (including negligence or
* otherwise) arising in any way out of the use of this software, even
* if advised of the possibility of such damage.
*
*/

#include <config.h>
#include <cxxabi.h>
#include <cstdio>
#include <cstdlib>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/syscall.h>

#define UNW_LOCAL_ONLY
#include <libunwind.h>

#include "stacktrace.h"

void stacktrace_print(int sig)
{
int framenum = 0;
unw_context_t ctx;
unw_cursor_t cursor;

unw_getcontext(&ctx);
unw_init_local(&cursor, &ctx);

fprintf(stderr, "stacktrace dump of %s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
#ifdef SYS_gettid
fprintf(stderr, "pid: %d uid: %d\n", getpid(), getuid());
#else
fprintf(stderr, "pid: %d tid: %d uid: %d\n", getpid(), gettid(), getuid());
#endif
fprintf(stderr, "received signal: %d\n", sig);

while (unw_step(&cursor) > 0) {
unw_word_t offset;
unw_word_t pc;
unw_word_t sp;
if (unw_get_reg(&cursor, UNW_REG_IP, &pc) == 0) {
if (pc == 0) {
break;
}
fprintf(stderr, "#%-3d %#016lx", framenum, pc);
} else {
fprintf(stderr, "#%-3d ???", framenum);
}
if (unw_get_reg(&cursor, UNW_REG_SP, &sp) == 0) {
fprintf(stderr, " %#016lx", sp);
} else {
fprintf(stderr, " ???");
}

framenum++;
printf(":");

char sym[256];
if (unw_get_proc_name(&cursor, sym, sizeof(sym), &offset) == 0) {
int status;
char *nameptr = sym;
char *demangled = abi::__cxa_demangle(sym, nullptr, nullptr, &status);
if (status == 0) {
nameptr = demangled;
}
fprintf(stderr, " %s+%#lx", nameptr, offset);
if (unw_is_signal_frame(&cursor)) {
fprintf(stderr, " <-");
}
fprintf(stderr, "\n");
free(demangled);
} else {
fprintf(stderr, " ???\n");
}
}
}

49 changes: 49 additions & 0 deletions stacktrace.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* \file stacktrace.h
* \brief Stack trace functions header file
* \author Jiri Havranek <[email protected]>
* \date 2021
*/
/*
* Copyright (C) 2021 CESNET
*
* LICENSE TERMS
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name of the Company nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* ALTERNATIVELY, provided that this notice is retained in full, this
* product may be distributed under the terms of the GNU General Public
* License (GPL) version 2 or later, in which case the provisions
* of the GPL apply INSTEAD OF those given above.
*
* This software is provided ``as is'', and any express or implied
* warranties, including, but not limited to, the implied warranties of
* merchantability and fitness for a particular purpose are disclaimed.
* In no event shall the company or contributors be liable for any
* direct, indirect, incidental, special, exemplary, or consequential
* damages (including, but not limited to, procurement of substitute
* goods or services; loss of use, data, or profits; or business
* interruption) however caused and on any theory of liability, whether
* in contract, strict liability, or tort (including negligence or
* otherwise) arising in any way out of the use of this software, even
* if advised of the possibility of such damage.
*
*/

#ifndef STACKTRACE_H
#define STACKTRACE_H

void stacktrace_print(int sig);

#endif /* STACKTRACE_H */

0 comments on commit 8ab1a1b

Please sign in to comment.