blob: 049efa5f714ee058ac3d8dc3761f88d4bbed4df4 [file] [log] [blame]
Austin Schuh906616c2019-01-21 20:25:11 -08001// Copyright (c) 2008, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29//
30// Author: Satoru Takabayashi
31//
32// Implementation of InstallFailureSignalHandler().
33
34#include "utilities.h"
35#include "stacktrace.h"
36#include "symbolize.h"
37#include "glog/logging.h"
38
39#include <signal.h>
40#include <time.h>
41#ifdef HAVE_UCONTEXT_H
42# include <ucontext.h>
43#endif
44#ifdef HAVE_SYS_UCONTEXT_H
45# include <sys/ucontext.h>
46#endif
47#include <algorithm>
48
49_START_GOOGLE_NAMESPACE_
50
51namespace {
52
53// We'll install the failure signal handler for these signals. We could
54// use strsignal() to get signal names, but we don't use it to avoid
55// introducing yet another #ifdef complication.
56//
57// The list should be synced with the comment in signalhandler.h.
58const struct {
59 int number;
60 const char *name;
61} kFailureSignals[] = {
62 { SIGSEGV, "SIGSEGV" },
63 { SIGILL, "SIGILL" },
64 { SIGFPE, "SIGFPE" },
65 { SIGABRT, "SIGABRT" },
66#if !defined(OS_WINDOWS)
67 { SIGBUS, "SIGBUS" },
68#endif
69 { SIGTERM, "SIGTERM" },
70};
71
72static bool kFailureSignalHandlerInstalled = false;
73
74// Returns the program counter from signal context, NULL if unknown.
75void* GetPC(void* ucontext_in_void) {
76#if (defined(HAVE_UCONTEXT_H) || defined(HAVE_SYS_UCONTEXT_H)) && defined(PC_FROM_UCONTEXT)
77 if (ucontext_in_void != NULL) {
78 ucontext_t *context = reinterpret_cast<ucontext_t *>(ucontext_in_void);
79 return (void*)context->PC_FROM_UCONTEXT;
80 }
Austin Schuh10358f22019-01-21 20:25:11 -080081#else
82 (void)ucontext_in_void;
Austin Schuh906616c2019-01-21 20:25:11 -080083#endif
84 return NULL;
85}
86
87// The class is used for formatting error messages. We don't use printf()
88// as it's not async signal safe.
89class MinimalFormatter {
90 public:
91 MinimalFormatter(char *buffer, int size)
92 : buffer_(buffer),
93 cursor_(buffer),
94 end_(buffer + size) {
95 }
96
97 // Returns the number of bytes written in the buffer.
98 int num_bytes_written() const { return (int) (cursor_ - buffer_); }
99
100 // Appends string from "str" and updates the internal cursor.
101 void AppendString(const char* str) {
102 int i = 0;
103 while (str[i] != '\0' && cursor_ + i < end_) {
104 cursor_[i] = str[i];
105 ++i;
106 }
107 cursor_ += i;
108 }
109
110 // Formats "number" in "radix" and updates the internal cursor.
111 // Lowercase letters are used for 'a' - 'z'.
112 void AppendUint64(uint64 number, int radix) {
113 int i = 0;
114 while (cursor_ + i < end_) {
115 const int tmp = number % radix;
116 number /= radix;
117 cursor_[i] = (tmp < 10 ? '0' + tmp : 'a' + tmp - 10);
118 ++i;
119 if (number == 0) {
120 break;
121 }
122 }
123 // Reverse the bytes written.
124 std::reverse(cursor_, cursor_ + i);
125 cursor_ += i;
126 }
127
128 // Formats "number" as hexadecimal number, and updates the internal
129 // cursor. Padding will be added in front if needed.
130 void AppendHexWithPadding(uint64 number, int width) {
131 char* start = cursor_;
132 AppendString("0x");
133 AppendUint64(number, 16);
134 // Move to right and add padding in front if needed.
135 if (cursor_ < start + width) {
136 const int64 delta = start + width - cursor_;
137 std::copy(start, cursor_, start + delta);
138 std::fill(start, start + delta, ' ');
139 cursor_ = start + width;
140 }
141 }
142
143 private:
144 char *buffer_;
145 char *cursor_;
146 const char * const end_;
147};
148
149// Writes the given data with the size to the standard error.
150void WriteToStderr(const char* data, int size) {
151 if (write(STDERR_FILENO, data, size) < 0) {
152 // Ignore errors.
153 }
154}
155
156// The writer function can be changed by InstallFailureWriter().
157void (*g_failure_writer)(const char* data, int size) = WriteToStderr;
158
159// Dumps time information. We don't dump human-readable time information
160// as localtime() is not guaranteed to be async signal safe.
161void DumpTimeInfo() {
162 time_t time_in_sec = time(NULL);
163 char buf[256]; // Big enough for time info.
164 MinimalFormatter formatter(buf, sizeof(buf));
165 formatter.AppendString("*** Aborted at ");
166 formatter.AppendUint64(time_in_sec, 10);
167 formatter.AppendString(" (unix time)");
168 formatter.AppendString(" try \"date -d @");
169 formatter.AppendUint64(time_in_sec, 10);
170 formatter.AppendString("\" if you are using GNU date ***\n");
171 g_failure_writer(buf, formatter.num_bytes_written());
172}
173
174// TOOD(hamaji): Use signal instead of sigaction?
175#ifdef HAVE_SIGACTION
176
177// Dumps information about the signal to STDERR.
178void DumpSignalInfo(int signal_number, siginfo_t *siginfo) {
179 // Get the signal name.
180 const char* signal_name = NULL;
181 for (size_t i = 0; i < ARRAYSIZE(kFailureSignals); ++i) {
182 if (signal_number == kFailureSignals[i].number) {
183 signal_name = kFailureSignals[i].name;
184 }
185 }
186
187 char buf[256]; // Big enough for signal info.
188 MinimalFormatter formatter(buf, sizeof(buf));
189
190 formatter.AppendString("*** ");
191 if (signal_name) {
192 formatter.AppendString(signal_name);
193 } else {
194 // Use the signal number if the name is unknown. The signal name
195 // should be known, but just in case.
196 formatter.AppendString("Signal ");
197 formatter.AppendUint64(signal_number, 10);
198 }
199 formatter.AppendString(" (@0x");
200 formatter.AppendUint64(reinterpret_cast<uintptr_t>(siginfo->si_addr), 16);
201 formatter.AppendString(")");
202 formatter.AppendString(" received by PID ");
203 formatter.AppendUint64(getpid(), 10);
204 formatter.AppendString(" (TID 0x");
205 // We assume pthread_t is an integral number or a pointer, rather
206 // than a complex struct. In some environments, pthread_self()
207 // returns an uint64 but in some other environments pthread_self()
208 // returns a pointer. Hence we use C-style cast here, rather than
209 // reinterpret/static_cast, to support both types of environments.
210 formatter.AppendUint64((uintptr_t)pthread_self(), 16);
211 formatter.AppendString(") ");
212 // Only linux has the PID of the signal sender in si_pid.
213#ifdef OS_LINUX
214 formatter.AppendString("from PID ");
215 formatter.AppendUint64(siginfo->si_pid, 10);
216 formatter.AppendString("; ");
217#endif
218 formatter.AppendString("stack trace: ***\n");
219 g_failure_writer(buf, formatter.num_bytes_written());
220}
221
222#endif // HAVE_SIGACTION
223
224// Dumps information about the stack frame to STDERR.
225void DumpStackFrameInfo(const char* prefix, void* pc) {
226 // Get the symbol name.
227 const char *symbol = "(unknown)";
228 char symbolized[1024]; // Big enough for a sane symbol.
229 // Symbolizes the previous address of pc because pc may be in the
230 // next function.
231 if (Symbolize(reinterpret_cast<char *>(pc) - 1,
232 symbolized, sizeof(symbolized))) {
233 symbol = symbolized;
234 }
235
236 char buf[1024]; // Big enough for stack frame info.
237 MinimalFormatter formatter(buf, sizeof(buf));
238
239 formatter.AppendString(prefix);
240 formatter.AppendString("@ ");
241 const int width = 2 * sizeof(void*) + 2; // + 2 for "0x".
242 formatter.AppendHexWithPadding(reinterpret_cast<uintptr_t>(pc), width);
243 formatter.AppendString(" ");
244 formatter.AppendString(symbol);
245 formatter.AppendString("\n");
246 g_failure_writer(buf, formatter.num_bytes_written());
247}
248
249// Invoke the default signal handler.
250void InvokeDefaultSignalHandler(int signal_number) {
251#ifdef HAVE_SIGACTION
252 struct sigaction sig_action;
253 memset(&sig_action, 0, sizeof(sig_action));
254 sigemptyset(&sig_action.sa_mask);
255 sig_action.sa_handler = SIG_DFL;
256 sigaction(signal_number, &sig_action, NULL);
257 kill(getpid(), signal_number);
258#elif defined(OS_WINDOWS)
259 signal(signal_number, SIG_DFL);
260 raise(signal_number);
261#endif
262}
263
264// This variable is used for protecting FailureSignalHandler() from
265// dumping stuff while another thread is doing it. Our policy is to let
266// the first thread dump stuff and let other threads wait.
267// See also comments in FailureSignalHandler().
268static pthread_t* g_entered_thread_id_pointer = NULL;
269
270// Dumps signal and stack frame information, and invokes the default
271// signal handler once our job is done.
272#if defined(OS_WINDOWS)
273void FailureSignalHandler(int signal_number)
274#else
275void FailureSignalHandler(int signal_number,
276 siginfo_t *signal_info,
277 void *ucontext)
278#endif
279{
280 // First check if we've already entered the function. We use an atomic
281 // compare and swap operation for platforms that support it. For other
282 // platforms, we use a naive method that could lead to a subtle race.
283
284 // We assume pthread_self() is async signal safe, though it's not
285 // officially guaranteed.
286 pthread_t my_thread_id = pthread_self();
287 // NOTE: We could simply use pthread_t rather than pthread_t* for this,
288 // if pthread_self() is guaranteed to return non-zero value for thread
289 // ids, but there is no such guarantee. We need to distinguish if the
290 // old value (value returned from __sync_val_compare_and_swap) is
291 // different from the original value (in this case NULL).
292 pthread_t* old_thread_id_pointer =
293 glog_internal_namespace_::sync_val_compare_and_swap(
294 &g_entered_thread_id_pointer,
295 static_cast<pthread_t*>(NULL),
296 &my_thread_id);
297 if (old_thread_id_pointer != NULL) {
298 // We've already entered the signal handler. What should we do?
299 if (pthread_equal(my_thread_id, *g_entered_thread_id_pointer)) {
300 // It looks the current thread is reentering the signal handler.
301 // Something must be going wrong (maybe we are reentering by another
302 // type of signal?). Kill ourself by the default signal handler.
303 InvokeDefaultSignalHandler(signal_number);
304 }
305 // Another thread is dumping stuff. Let's wait until that thread
306 // finishes the job and kills the process.
307 while (true) {
308 sleep(1);
309 }
310 }
Austin Schuha8faf282020-03-08 14:49:53 -0700311
312 {
313 // Put this back on SCHED_OTHER by default.
314 struct sched_param param;
315 param.sched_priority = 0;
316 sched_setscheduler(0, SCHED_OTHER, &param);
317 }
318
Austin Schuh906616c2019-01-21 20:25:11 -0800319 // This is the first time we enter the signal handler. We are going to
320 // do some interesting stuff from here.
321 // TODO(satorux): We might want to set timeout here using alarm(), but
322 // mixing alarm() and sleep() can be a bad idea.
323
324 // First dump time info.
325 DumpTimeInfo();
326
327#if !defined(OS_WINDOWS)
328 // Get the program counter from ucontext.
329 void *pc = GetPC(ucontext);
330 DumpStackFrameInfo("PC: ", pc);
Austin Schuh10358f22019-01-21 20:25:11 -0800331#else
332 (void)ucontext;
Austin Schuh906616c2019-01-21 20:25:11 -0800333#endif
334
335#ifdef HAVE_STACKTRACE
336 // Get the stack traces.
337 void *stack[32];
338 // +1 to exclude this function.
339 const int depth = GetStackTrace(stack, ARRAYSIZE(stack), 1);
340# ifdef HAVE_SIGACTION
341 DumpSignalInfo(signal_number, signal_info);
Austin Schuh10358f22019-01-21 20:25:11 -0800342# else
343 (void)signal_info;
Austin Schuh906616c2019-01-21 20:25:11 -0800344# endif
345 // Dump the stack traces.
346 for (int i = 0; i < depth; ++i) {
347 DumpStackFrameInfo(" ", stack[i]);
348 }
Austin Schuh10358f22019-01-21 20:25:11 -0800349#else
350 (void)signal_info;
Austin Schuh906616c2019-01-21 20:25:11 -0800351#endif
352
353 // *** TRANSITION ***
354 //
355 // BEFORE this point, all code must be async-termination-safe!
356 // (See WARNING above.)
357 //
358 // AFTER this point, we do unsafe things, like using LOG()!
359 // The process could be terminated or hung at any time. We try to
360 // do more useful things first and riskier things later.
361
362 // Flush the logs before we do anything in case 'anything'
363 // causes problems.
364 FlushLogFilesUnsafe(0);
365
366 // Kill ourself by the default signal handler.
367 InvokeDefaultSignalHandler(signal_number);
368}
369
370} // namespace
371
372namespace glog_internal_namespace_ {
373
374bool IsFailureSignalHandlerInstalled() {
375#ifdef HAVE_SIGACTION
376 // TODO(andschwa): Return kFailureSignalHandlerInstalled?
377 struct sigaction sig_action;
378 memset(&sig_action, 0, sizeof(sig_action));
379 sigemptyset(&sig_action.sa_mask);
380 sigaction(SIGABRT, NULL, &sig_action);
381 if (sig_action.sa_sigaction == &FailureSignalHandler)
382 return true;
383#elif defined(OS_WINDOWS)
384 return kFailureSignalHandlerInstalled;
385#endif // HAVE_SIGACTION
386 return false;
387}
388
389} // namespace glog_internal_namespace_
390
391void InstallFailureSignalHandler() {
392#ifdef HAVE_SIGACTION
393 // Build the sigaction struct.
394 struct sigaction sig_action;
395 memset(&sig_action, 0, sizeof(sig_action));
396 sigemptyset(&sig_action.sa_mask);
397 sig_action.sa_flags |= SA_SIGINFO;
398 sig_action.sa_sigaction = &FailureSignalHandler;
399
400 for (size_t i = 0; i < ARRAYSIZE(kFailureSignals); ++i) {
401 CHECK_ERR(sigaction(kFailureSignals[i].number, &sig_action, NULL));
402 }
403 kFailureSignalHandlerInstalled = true;
404#elif defined(OS_WINDOWS)
405 for (size_t i = 0; i < ARRAYSIZE(kFailureSignals); ++i) {
406 CHECK_NE(signal(kFailureSignals[i].number, &FailureSignalHandler),
407 SIG_ERR);
408 }
409 kFailureSignalHandlerInstalled = true;
410#endif // HAVE_SIGACTION
411}
412
413void InstallFailureWriter(void (*writer)(const char* data, int size)) {
414#if defined(HAVE_SIGACTION) || defined(OS_WINDOWS)
415 g_failure_writer = writer;
416#endif // HAVE_SIGACTION
417}
418
419_END_GOOGLE_NAMESPACE_