-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfail.c
84 lines (71 loc) · 1.56 KB
/
fail.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#ifndef _FAIL_C
#define _FAIL_C
#include <stdio.h>
#include <stdlib.h>
#ifdef __linux
# include <sys/types.h>
# include <unistd.h>
static const char *emptystring = "";
static const char *myname(void)
{
# define n 0x29a
//const int n = 0x29a;
static char buf[n];
pid_t p = getpid();
snprintf(buf, n, "/proc/%d/cmdline", p);
FILE *f = fopen(buf, "r");
if (!f) return emptystring;
int c, i = 0;
while ((c = fgetc(f)) != EOF && i < n) {
# undef n
buf[i] = c ? c : ' ';
i += 1;
}
if (i) buf[i-1] = '\0';
fclose(f);
return buf;
}
#else
static const char *myname(void) { return ""; }
#endif//__linux
#ifdef DOTRACE
#include <execinfo.h>
#endif
#ifndef BACKTRACE_SYMBOLS
#define BACKTRACE_SYMBOLS 50
#endif
static void print_trace(FILE *f)
{
(void)f;
#ifdef DOTRACE
void *array[BACKTRACE_SYMBOLS];
size_t size, i;
char **strings;
size = backtrace (array, BACKTRACE_SYMBOLS);
strings = backtrace_symbols (array, size);
fprintf (f, "Obtained %zu stack frames.\n", size);
for (i = 0; i < size; i++)
fprintf (f, "%s\n", strings[i]);
free (strings);
#endif
}
#include <stdarg.h>
//static void fail(const char *fmt, ...) __attribute__((noreturn,format(printf,1,2)));
static void fail(const char *fmt, ...) __attribute__((noreturn));
static void fail(const char *fmt, ...)
{
va_list argp;
fprintf(stderr, "\nFAIL(\"%s\"): ", myname());
va_start(argp, fmt);
vfprintf(stderr, fmt, argp);
va_end(argp);
fprintf(stderr, "\n\n");
fflush(NULL);
print_trace(stderr);
#ifdef NDEBUG
exit(-1);
#else//NDEBUG
exit(*(volatile int *)0x43);
#endif//NDEBUG
}
#endif//_FAIL_C