Nilorea Library
C utilities for networking, threading, graphics
n_signals.c File Reference

signals general handling with stack printing More...

#include <signal.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <errno.h>
#include <string.h>
#include "nilorea/n_log.h"
#include "nilorea/n_signals.h"
#include "nilorea/n_str.h"
#include <err.h>
#include <execinfo.h>
+ Include dependency graph for n_signals.c:

Go to the source code of this file.

Macros

#define LOGFPRT(...)   fprintf( stderr , "Error: " __VA_ARGS__ )
 internal: output to stderr More...
 
#define LOGNLOG(...)   n_log( LOG_ERR , __VA_ARGS__ )
 internal: output to syslog More...
 
#define LOGSIG   LOGNLOG
 internal: output to syslog More...
 

Functions

int addr2line (char const *const program_name, void const *const addr)
 Resolve symbol name and source location given the path to the executable and an address. More...
 
void posix_print_stack_trace ()
 print current stack More...
 
void posix_signal_handler (int sig, siginfo_t *siginfo, void *context)
 decode a signal and call stack print More...
 
void set_signal_handler (const char *progname)
 Install a signal handler for progname. More...
 

Variables

static const char * __n_stack_traced_progam_name
 name of program to debug (addr2line & co) More...
 
static uint8_t alternate_stack [SIGALTSTACK_SIZE]
 static block for alternate stack More...
 
static void * stack_traces [MAX_STACK_FRAMES]
 static frame list More...
 

Detailed Description

signals general handling with stack printing

Author
Castagnier Mickael, adapted from https://gist.github.com/jvranish/4441299
Version
1.0
Date
08/11/2018

Definition in file n_signals.c.

Macro Definition Documentation

◆ LOGFPRT

#define LOGFPRT (   ...)    fprintf( stderr , "Error: " __VA_ARGS__ )

internal: output to stderr

Definition at line 24 of file n_signals.c.

◆ LOGNLOG

#define LOGNLOG (   ...)    n_log( LOG_ERR , __VA_ARGS__ )

internal: output to syslog

Definition at line 27 of file n_signals.c.

◆ LOGSIG

#define LOGSIG   LOGNLOG

internal: output to syslog

Definition at line 30 of file n_signals.c.

Function Documentation

◆ addr2line()

int addr2line ( char const *const  program_name,
void const *const  addr 
)

Resolve symbol name and source location given the path to the executable and an address.

Parameters
program_nametarget filename
addraddress to peek
Returns
addr2line command return code

Definition at line 88 of file n_signals.c.

References free_nstr, LOGSIG, and n_popen().

Referenced by posix_print_stack_trace().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ posix_print_stack_trace()

void posix_print_stack_trace ( )

print current stack

Definition at line 266 of file n_signals.c.

References __n_stack_traced_progam_name, addr2line(), LOGSIG, MAX_STACK_FRAMES, and stack_traces.

Referenced by posix_signal_handler().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ posix_signal_handler()

void posix_signal_handler ( int  sig,
siginfo_t *  siginfo,
void *  context 
)

decode a signal and call stack print

Parameters
sigsignal to decode
siginfodetails of the signal
contextthe context to unroll

Definition at line 298 of file n_signals.c.

References LOGSIG, and posix_print_stack_trace().

Referenced by set_signal_handler().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ __n_stack_traced_progam_name

const char* __n_stack_traced_progam_name
static

name of program to debug (addr2line & co)

Definition at line 33 of file n_signals.c.

Referenced by posix_print_stack_trace(), and set_signal_handler().

◆ alternate_stack

uint8_t alternate_stack[SIGALTSTACK_SIZE]
static

static block for alternate stack

Definition at line 387 of file n_signals.c.

Referenced by set_signal_handler().

◆ stack_traces

void* stack_traces[MAX_STACK_FRAMES]
static

static frame list

Definition at line 261 of file n_signals.c.

Referenced by posix_print_stack_trace().