Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:40:50

0001 // Copyright 2018 The Abseil Authors.
0002 //
0003 // Licensed under the Apache License, Version 2.0 (the "License");
0004 // you may not use this file except in compliance with the License.
0005 // You may obtain a copy of the License at
0006 //
0007 //      https://www.apache.org/licenses/LICENSE-2.0
0008 //
0009 // Unless required by applicable law or agreed to in writing, software
0010 // distributed under the License is distributed on an "AS IS" BASIS,
0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0012 // See the License for the specific language governing permissions and
0013 // limitations under the License.
0014 //
0015 // -----------------------------------------------------------------------------
0016 // File: failure_signal_handler.h
0017 // -----------------------------------------------------------------------------
0018 //
0019 // This file configures the Abseil *failure signal handler* to capture and dump
0020 // useful debugging information (such as a stacktrace) upon program failure.
0021 //
0022 // To use the failure signal handler, call `absl::InstallFailureSignalHandler()`
0023 // very early in your program, usually in the first few lines of main():
0024 //
0025 // int main(int argc, char** argv) {
0026 //   // Initialize the symbolizer to get a human-readable stack trace
0027 //   absl::InitializeSymbolizer(argv[0]);
0028 //
0029 //   absl::FailureSignalHandlerOptions options;
0030 //   absl::InstallFailureSignalHandler(options);
0031 //   DoSomethingInteresting();
0032 //   return 0;
0033 // }
0034 //
0035 // Any program that raises a fatal signal (such as `SIGSEGV`, `SIGILL`,
0036 // `SIGFPE`, `SIGABRT`, `SIGTERM`, `SIGBUS`, and `SIGTRAP`) will call the
0037 // installed failure signal handler and provide debugging information to stderr.
0038 //
0039 // Note that you should *not* install the Abseil failure signal handler more
0040 // than once. You may, of course, have another (non-Abseil) failure signal
0041 // handler installed (which would be triggered if Abseil's failure signal
0042 // handler sets `call_previous_handler` to `true`).
0043 
0044 #ifndef ABSL_DEBUGGING_FAILURE_SIGNAL_HANDLER_H_
0045 #define ABSL_DEBUGGING_FAILURE_SIGNAL_HANDLER_H_
0046 
0047 #include "absl/base/config.h"
0048 
0049 namespace absl {
0050 ABSL_NAMESPACE_BEGIN
0051 
0052 // FailureSignalHandlerOptions
0053 //
0054 // Struct for holding `absl::InstallFailureSignalHandler()` configuration
0055 // options.
0056 struct FailureSignalHandlerOptions {
0057   // If true, try to symbolize the stacktrace emitted on failure, provided that
0058   // you have initialized a symbolizer for that purpose. (See symbolize.h for
0059   // more information.)
0060   bool symbolize_stacktrace = true;
0061 
0062   // If true, try to run signal handlers on an alternate stack (if supported on
0063   // the given platform). An alternate stack is useful for program crashes due
0064   // to a stack overflow; by running on a alternate stack, the signal handler
0065   // may run even when normal stack space has been exhausted. The downside of
0066   // using an alternate stack is that extra memory for the alternate stack needs
0067   // to be pre-allocated.
0068   bool use_alternate_stack = true;
0069 
0070   // If positive, indicates the number of seconds after which the failure signal
0071   // handler is invoked to abort the program. Setting such an alarm is useful in
0072   // cases where the failure signal handler itself may become hung or
0073   // deadlocked.
0074   int alarm_on_failure_secs = 3;
0075 
0076   // If true, call the previously registered signal handler for the signal that
0077   // was received (if one was registered) after the existing signal handler
0078   // runs. This mechanism can be used to chain signal handlers together.
0079   //
0080   // If false, the signal is raised to the default handler for that signal
0081   // (which normally terminates the program).
0082   //
0083   // IMPORTANT: If true, the chained fatal signal handlers must not try to
0084   // recover from the fatal signal. Instead, they should terminate the program
0085   // via some mechanism, like raising the default handler for the signal, or by
0086   // calling `_exit()`. Note that the failure signal handler may put parts of
0087   // the Abseil library into a state from which they cannot recover.
0088   bool call_previous_handler = false;
0089 
0090   // If non-null, indicates a pointer to a callback function that will be called
0091   // upon failure, with a string argument containing failure data. This function
0092   // may be used as a hook to write failure data to a secondary location, such
0093   // as a log file. This function will also be called with null data, as a hint
0094   // to flush any buffered data before the program may be terminated. Consider
0095   // flushing any buffered data in all calls to this function.
0096   //
0097   // Since this function runs within a signal handler, it should be
0098   // async-signal-safe if possible.
0099   // See http://man7.org/linux/man-pages/man7/signal-safety.7.html
0100   void (*writerfn)(const char*) = nullptr;
0101 };
0102 
0103 // InstallFailureSignalHandler()
0104 //
0105 // Installs a signal handler for the common failure signals `SIGSEGV`, `SIGILL`,
0106 // `SIGFPE`, `SIGABRT`, `SIGTERM`, `SIGBUG`, and `SIGTRAP` (provided they exist
0107 // on the given platform). The failure signal handler dumps program failure data
0108 // useful for debugging in an unspecified format to stderr. This data may
0109 // include the program counter, a stacktrace, and register information on some
0110 // systems; do not rely on an exact format for the output, as it is subject to
0111 // change.
0112 void InstallFailureSignalHandler(const FailureSignalHandlerOptions& options);
0113 
0114 namespace debugging_internal {
0115 const char* FailureSignalToString(int signo);
0116 }  // namespace debugging_internal
0117 
0118 ABSL_NAMESPACE_END
0119 }  // namespace absl
0120 
0121 #endif  // ABSL_DEBUGGING_FAILURE_SIGNAL_HANDLER_H_