forked from facebook/watchman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.c
214 lines (186 loc) · 5.57 KB
/
log.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* Copyright 2012-present Facebook, Inc.
* Licensed under the Apache License, Version 2.0 */
#include "watchman.h"
static void log_stack_trace(void)
{
#if defined(HAVE_BACKTRACE) && defined(HAVE_BACKTRACE_SYMBOLS)
void *array[24];
size_t size;
char **strings;
size_t i;
size = backtrace(array, sizeof(array)/sizeof(array[0]));
strings = backtrace_symbols(array, size);
w_log(W_LOG_ERR, "Fatal error detected at:\n");
for (i = 0; i < size; i++) {
w_log(W_LOG_ERR, "%s\n", strings[i]);
}
free(strings);
#endif
}
int log_level = W_LOG_ERR;
static pthread_key_t thread_name_key;
#ifndef _WIN32
static void crash_handler(int signo, siginfo_t *si, void *ucontext) {
const char *reason = "";
unused_parameter(ucontext);
if (si) {
switch (si->si_signo) {
case SIGILL:
switch (si->si_code) {
case ILL_ILLOPC: reason = "illegal opcode"; break;
case ILL_ILLOPN: reason = "illegal operand"; break;
case ILL_ILLADR: reason = "illegal addressing mode"; break;
case ILL_ILLTRP: reason = "illegal trap"; break;
case ILL_PRVOPC: reason = "privileged opcode"; break;
case ILL_PRVREG: reason = "privileged register"; break;
case ILL_COPROC: reason = "co-processor error"; break;
case ILL_BADSTK: reason = "internal stack error"; break;
}
break;
case SIGFPE:
switch (si->si_code) {
case FPE_INTDIV: reason = "integer divide by zero"; break;
case FPE_INTOVF: reason = "integer overflow"; break;
case FPE_FLTDIV: reason = "floating point divide by zero"; break;
case FPE_FLTOVF: reason = "floating point overflow"; break;
case FPE_FLTUND: reason = "floating point underflow"; break;
case FPE_FLTRES: reason = "floating point inexact result"; break;
case FPE_FLTINV: reason = "invalid floating point operation"; break;
case FPE_FLTSUB: reason = "subscript out of range"; break;
}
break;
case SIGSEGV:
switch (si->si_code) {
case SEGV_MAPERR: reason = "address not mapped to object"; break;
case SEGV_ACCERR: reason = "invalid permissions for mapped object";
break;
}
break;
#ifdef SIGBUS
case SIGBUS:
switch (si->si_code) {
case BUS_ADRALN: reason = "invalid address alignment"; break;
case BUS_ADRERR: reason = "non-existent physical address"; break;
}
break;
#endif
}
}
if (si) {
dprintf(STDERR_FILENO,
"Terminating due to signal %d %s "
"generated by pid=%d uid=%d. %s (%p)\n",
signo, w_strsignal(signo), si->si_pid, si->si_uid,
reason, si->si_value.sival_ptr);
} else {
dprintf(STDERR_FILENO, "Terminating due to signal %d %s. %s\n",
signo, w_strsignal(signo), reason);
}
#if defined(HAVE_BACKTRACE) && defined(HAVE_BACKTRACE_SYMBOLS_FD)
{
void *array[24];
size_t size = backtrace(array, sizeof(array)/sizeof(array[0]));
backtrace_symbols_fd(array, size, STDERR_FILENO);
}
#endif
if (signo == SIGTERM) {
w_request_shutdown();
return;
}
abort();
}
#endif
void w_setup_signal_handlers(void) {
#ifndef _WIN32
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_sigaction = crash_handler;
sa.sa_flags = SA_SIGINFO|SA_RESETHAND;
sigaction(SIGSEGV, &sa, NULL);
#ifdef SIGBUS
sigaction(SIGBUS, &sa, NULL);
#endif
sigaction(SIGFPE, &sa, NULL);
sigaction(SIGILL, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
#endif
}
static w_ctor_fn_type(register_thread_name) {
pthread_key_create(&thread_name_key, free);
}
w_ctor_fn_reg(register_thread_name);
const char *w_get_thread_name(void) {
const char *name = pthread_getspecific(thread_name_key);
if (name) {
return name;
}
return w_set_thread_name("%" PRIu32, (uint32_t)(uintptr_t)pthread_self());
}
const char *w_set_thread_name(const char *fmt, ...) {
char *name = NULL;
va_list ap;
free(pthread_getspecific(thread_name_key));
va_start(ap, fmt);
ignore_result(vasprintf(&name, fmt, ap));
va_end(ap);
pthread_setspecific(thread_name_key, name);
return name;
}
void w_log(int level, WATCHMAN_FMT_STRING(const char *fmt), ...)
{
char buf[4096];
va_list ap;
int len, len2;
bool fatal = false;
struct timeval tv;
char timebuf[64];
struct tm tm;
bool should_log_to_stderr = level <= log_level;
bool should_log_to_clients = w_should_log_to_clients(level);
if (!(should_log_to_stderr || should_log_to_clients)) {
// Don't bother formatting the log message if nobody's listening.
return;
}
if (level == W_LOG_FATAL) {
level = W_LOG_ERR;
fatal = true;
}
gettimeofday(&tv, NULL);
#ifdef _WIN32
tm = *localtime(&tv.tv_sec);
#else
localtime_r(&tv.tv_sec, &tm);
#endif
strftime(timebuf, sizeof(timebuf), "%Y-%m-%dT%H:%M:%S", &tm);
len = snprintf(buf, sizeof(buf), "%s,%03d: [%s] ",
timebuf, (int)tv.tv_usec / 1000, w_get_thread_name());
va_start(ap, fmt);
len2 = vsnprintf(buf + len, sizeof(buf) - len, fmt, ap);
va_end(ap);
if (len2 == -1) {
// Truncated. Ensure that we have a NUL terminator
buf[sizeof(buf)-1] = 0;
}
len = (int)strlen(buf);
if (buf[len - 1] != '\n') {
if (len < (int)sizeof(buf) - 1) {
buf[len] = '\n';
buf[len + 1] = 0;
len++;
} else {
buf[len - 1] = '\n';
}
}
if (should_log_to_stderr) {
ignore_result(write(STDERR_FILENO, buf, (unsigned int)len));
}
if (should_log_to_clients) {
w_log_to_clients(level, buf);
}
if (fatal) {
log_stack_trace();
abort();
}
}
/* vim:ts=2:sw=2:et:
*/