Nilorea Library signals api test.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include <errno.h>
#include <signal.h>
#include <assert.h>
#include <libgen.h>
int divide_by_zero(void);
void cause_segfault(void);
int stack_overflow(void);
void infinite_loop(void);
void illegal_instruction(void);
void cause_calamity(void);
char* progname = NULL;
int main(int argc, char* argv[]) {
(void)argc;
progname = argv[0];
cause_calamity();
return 0;
}
void cause_calamity(void) {
stack_overflow();
(void)divide_by_zero();
cause_segfault();
assert(false);
infinite_loop();
illegal_instruction();
}
int divide_by_zero(void) {
int a = 1;
int b = 0;
return a / b;
}
void cause_segfault(void) {
int* p = (int*)0x12345678;
*p = 0;
}
#pragma GCC diagnostic push
#if defined(__GNUC__) && (__GNUC__ >= 12)
#pragma GCC diagnostic ignored "-Winfinite-recursion"
#endif
int stack_overflow(void) {
int foo = 0;
(void)foo;
foo = stack_overflow();
return rand() % 1000;
}
#pragma GCC diagnostic pop
void infinite_loop(void) {
int a = 1;
int b = 1;
int c = 1;
while (a == 1) {
b = c;
c = a;
a = b;
}
}
void illegal_instruction(void) {
raise(SIGILL);
}
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_DEBUG
debug-level messages
#define LOG_STDERR
internal, default LOG_TYPE
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
void set_signal_handler(const char *progname)
Install a signal handler for progname.
Signals general handling with stack printing, from https://gist.github.com/jvranish/4441299.