Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_signals.c
Go to the documentation of this file.
1
9#include <stdio.h>
10#include <stdlib.h>
11#include <stdbool.h>
12#include <stdint.h>
13#include <string.h>
14#include <stdbool.h>
15#include <errno.h>
16#include <signal.h>
17#include <assert.h>
18
19#include "nilorea/n_signals.h"
20#include "nilorea/n_log.h"
21
22#include <libgen.h>
23
24int divide_by_zero(void);
25void cause_segfault(void);
26int stack_overflow(void);
27void infinite_loop(void);
28void illegal_instruction(void);
29void cause_calamity(void);
30
31char* progname = NULL;
32
33int main(int argc, char* argv[]) {
34 (void)argc;
35 progname = argv[0];
36
37 // set_log_file( "ex_signals.log" );
40 // set_log_level( LOG_FILE );
41
42 n_log(LOG_DEBUG, "set_signal_handler");
44
45 n_log(LOG_DEBUG, "cause_calamity");
47
48 return 0;
49}
50
51void cause_calamity(void) {
52 /* uncomment one of the following error conditions to cause a calamity of
53 your choosing! */
55 (void)divide_by_zero();
57 assert(false);
60}
61
62int divide_by_zero(void) {
63 int a = 1;
64 int b = 0;
65 return a / b;
66}
67
68void cause_segfault(void) {
69 int* p = (int*)0x12345678;
70 *p = 0;
71}
72
73// that function was made to generate an error. Temporary disabling infinite recursion detection
74#pragma GCC diagnostic push
75#if defined(__GNUC__) && (__GNUC__ >= 12)
76#pragma GCC diagnostic ignored "-Winfinite-recursion"
77#endif
78int stack_overflow(void) {
79 int foo = 0;
80 (void)foo;
81 foo = stack_overflow();
82 return rand() % 1000;
83}
84#pragma GCC diagnostic pop
85
86/* break out with ctrl+c to test SIGINT handling */
87void infinite_loop(void) {
88 int a = 1;
89 int b = 1;
90 int c = 1;
91 while (a == 1) {
92 b = c;
93 c = a;
94 a = b;
95 }
96}
97
99 /* I couldn't find an easy way to cause this one, so I'm cheating */
100 raise(SIGILL);
101}
int main(void)
void infinite_loop(void)
Definition ex_signals.c:87
void cause_calamity(void)
Definition ex_signals.c:51
void illegal_instruction(void)
Definition ex_signals.c:98
int divide_by_zero(void)
Definition ex_signals.c:62
char * progname
Definition ex_signals.c:31
void cause_segfault(void)
Definition ex_signals.c:68
int stack_overflow(void)
Definition ex_signals.c:78
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:70
#define LOG_DEBUG
debug-level messages
Definition n_log.h:65
#define LOG_STDERR
internal, default LOG_TYPE
Definition n_log.h:31
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:104
void set_signal_handler(const char *progname)
Install a signal handler for progname.
Definition n_signals.c:368
Generic log system.
Signals general handling with stack printing, from https://gist.github.com/jvranish/4441299.