Browse Source

for jackdbus, setup SIGSEGV magic that prints nice stack in logfile

git-svn-id: http://subversion.jackaudio.org/jack/jack2/trunk/jackmp@2378 0c269be4-1314-0410-8aa9-9f06e86f4224
tags/1.90
nedko 17 years ago
parent
commit
712d6c66b6
4 changed files with 154 additions and 0 deletions
  1. +1
    -0
      linux/dbus/SConscript
  2. +4
    -0
      linux/dbus/jackdbus.c
  3. +140
    -0
      linux/dbus/sigsegv.c
  4. +9
    -0
      linux/dbus/sigsegv.h

+ 1
- 0
linux/dbus/SConscript View File

@@ -71,6 +71,7 @@ jackenv.Install(env['BINDIR'], jackenv.Program('jackdbus', [
# 'xml_libxml.c', # 'xml_libxml.c',
# 'xml_nop.c', # 'xml_nop.c',
'xml_write_raw.c', 'xml_write_raw.c',
'sigsegv.c',
])) ]))
jackenv.Alias('install', env['BINDIR']) jackenv.Alias('install', env['BINDIR'])




+ 4
- 0
linux/dbus/jackdbus.c View File

@@ -37,6 +37,7 @@
#include "jack/jack.h" #include "jack/jack.h"
#include "jack/jslist.h" #include "jack/jslist.h"
#include "jack/control.h" #include "jack/control.h"
#include "sigsegv.h"


FILE *g_logfile; FILE *g_logfile;
char *g_jackdbus_config_dir; char *g_jackdbus_config_dir;
@@ -744,6 +745,9 @@ main (int argc, char **argv)
jack_set_error_function(jack_dbus_error_callback); jack_set_error_function(jack_dbus_error_callback);
jack_set_info_function(jack_dbus_info_callback); jack_set_info_function(jack_dbus_info_callback);


/* setup our SIGSEGV magic that prints nice stack in our logfile */
setup_sigsegv();

jack_info("------------------"); jack_info("------------------");
jack_info("Controller activated. Version %s (%s)", jack_get_version_string(), JACK_SVNREVISION); jack_info("Controller activated. Version %s (%s)", jack_get_version_string(), JACK_SVNREVISION);




+ 140
- 0
linux/dbus/sigsegv.c View File

@@ -0,0 +1,140 @@
/**
* This source file is used to print out a stack-trace when your program
* segfaults. It is relatively reliable and spot-on accurate.
*
* This code is in the public domain. Use it as you see fit, some credit
* would be appreciated, but is not a prerequisite for usage. Feedback
* on it's use would encourage further development and maintenance.
*
* Author: Jaco Kroon <jaco@kroon.co.za>
*
* Copyright (C) 2005 - 2008 Jaco Kroon
*/

//#define NO_CPP_DEMANGLE
#define SIGSEGV_NO_AUTO_INIT

#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif

#include <memory.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <ucontext.h>
#include <dlfcn.h>
#include <execinfo.h>
#include <errno.h>
#ifndef NO_CPP_DEMANGLE
//#include <cxxabi.h>
char * __cxa_demangle(const char * __mangled_name, char * __output_buffer, size_t * __length, int * __status);
#endif

#if defined(REG_RIP)
# define SIGSEGV_STACK_IA64
# define REGFORMAT "%016lx"
#elif defined(REG_EIP)
# define SIGSEGV_STACK_X86
# define REGFORMAT "%08x"
#else
# define SIGSEGV_STACK_GENERIC
# define REGFORMAT "%x"
#endif

static void signal_segv(int signum, siginfo_t* info, void*ptr) {
static const char *si_codes[3] = {"", "SEGV_MAPERR", "SEGV_ACCERR"};

size_t i;
ucontext_t *ucontext = (ucontext_t*)ptr;

#if defined(SIGSEGV_STACK_X86) || defined(SIGSEGV_STACK_IA64)
int f = 0;
Dl_info dlinfo;
void **bp = 0;
void *ip = 0;
#else
void *bt[20];
char **strings;
size_t sz;
#endif

jack_error("Segmentation Fault!");
jack_error("info.si_signo = %d", signum);
jack_error("info.si_errno = %d", info->si_errno);
jack_error("info.si_code = %d (%s)", info->si_code, si_codes[info->si_code]);
jack_error("info.si_addr = %p", info->si_addr);
for(i = 0; i < NGREG; i++)
jack_error("reg[%02d] = 0x" REGFORMAT, i, ucontext->uc_mcontext.gregs[i]);

#if defined(SIGSEGV_STACK_X86) || defined(SIGSEGV_STACK_IA64)
# if defined(SIGSEGV_STACK_IA64)
ip = (void*)ucontext->uc_mcontext.gregs[REG_RIP];
bp = (void**)ucontext->uc_mcontext.gregs[REG_RBP];
# elif defined(SIGSEGV_STACK_X86)
ip = (void*)ucontext->uc_mcontext.gregs[REG_EIP];
bp = (void**)ucontext->uc_mcontext.gregs[REG_EBP];
# endif

jack_error("Stack trace:");
while(bp && ip) {
if(!dladdr(ip, &dlinfo))
break;

const char *symname = dlinfo.dli_sname;
#ifndef NO_CPP_DEMANGLE
int status;
char *tmp = __cxa_demangle(symname, NULL, 0, &status);

if(status == 0 && tmp)
symname = tmp;
#endif

jack_error("% 2d: %p <%s+%u> (%s)",
++f,
ip,
symname,
(unsigned)(ip - dlinfo.dli_saddr),
dlinfo.dli_fname);

#ifndef NO_CPP_DEMANGLE
if(tmp)
free(tmp);
#endif

if(dlinfo.dli_sname && !strcmp(dlinfo.dli_sname, "main"))
break;

ip = bp[1];
bp = (void**)bp[0];
}
#else
jack_error("Stack trace (non-dedicated):");
sz = backtrace(bt, 20);
strings = backtrace_symbols(bt, sz);

for(i = 0; i < sz; ++i)
jack_error("%s", strings[i]);
#endif
jack_error("End of stack trace");
exit (-1);
}

int setup_sigsegv() {
struct sigaction action;
memset(&action, 0, sizeof(action));
action.sa_sigaction = signal_segv;
action.sa_flags = SA_SIGINFO;
if(sigaction(SIGSEGV, &action, NULL) < 0) {
jack_error("sigaction failed. errno is %d (%s)", errno, strerror(errno));
return 0;
}

return 1;
}

#ifndef SIGSEGV_NO_AUTO_INIT
static void __attribute((constructor)) init(void) {
setup_sigsegv();
}
#endif

+ 9
- 0
linux/dbus/sigsegv.h View File

@@ -0,0 +1,9 @@
#ifndef __SIGSEGV_H__
#define __SIGSEGV_H__

#ifdef __cplusplus
extern "C"
#endif
int setup_sigsegv();

#endif

Loading…
Cancel
Save