00001 // Copyright 2018 The Abseil Authors. 00002 // 00003 // Licensed under the Apache License, Version 2.0 (the "License"); 00004 // you may not use this file except in compliance with the License. 00005 // You may obtain a copy of the License at 00006 // 00007 // https://www.apache.org/licenses/LICENSE-2.0 00008 // 00009 // Unless required by applicable law or agreed to in writing, software 00010 // distributed under the License is distributed on an "AS IS" BASIS, 00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00012 // See the License for the specific language governing permissions and 00013 // limitations under the License. 00014 // 00015 // ----------------------------------------------------------------------------- 00016 // File: failure_signal_handler.h 00017 // ----------------------------------------------------------------------------- 00018 // 00019 // This file configures the Abseil *failure signal handler* to capture and dump 00020 // useful debugging information (such as a stacktrace) upon program failure. 00021 // 00022 // To use the failure signal handler, call `absl::InstallFailureSignalHandler()` 00023 // very early in your program, usually in the first few lines of main(): 00024 // 00025 // int main(int argc, char** argv) { 00026 // // Initialize the symbolizer to get a human-readable stack trace 00027 // absl::InitializeSymbolizer(argv[0]); 00028 // 00029 // absl::FailureSignalHandlerOptions options; 00030 // absl::InstallFailureSignalHandler(options); 00031 // DoSomethingInteresting(); 00032 // return 0; 00033 // } 00034 // 00035 // Any program that raises a fatal signal (such as `SIGSEGV`, `SIGILL`, 00036 // `SIGFPE`, `SIGABRT`, `SIGTERM`, `SIGBUG`, and `SIGTRAP`) will call the 00037 // installed failure signal handler and provide debugging information to stderr. 00038 // 00039 // Note that you should *not* install the Abseil failure signal handler more 00040 // than once. You may, of course, have another (non-Abseil) failure signal 00041 // handler installed (which would be triggered if Abseil's failure signal 00042 // handler sets `call_previous_handler` to `true`). 00043 00044 #ifndef ABSL_DEBUGGING_FAILURE_SIGNAL_HANDLER_H_ 00045 #define ABSL_DEBUGGING_FAILURE_SIGNAL_HANDLER_H_ 00046 00047 namespace absl { 00048 00049 // FailureSignalHandlerOptions 00050 // 00051 // Struct for holding `absl::InstallFailureSignalHandler()` configuration 00052 // options. 00053 struct FailureSignalHandlerOptions { 00054 // If true, try to symbolize the stacktrace emitted on failure, provided that 00055 // you have initialized a symbolizer for that purpose. (See symbolize.h for 00056 // more information.) 00057 bool symbolize_stacktrace = true; 00058 00059 // If true, try to run signal handlers on an alternate stack (if supported on 00060 // the given platform). An alternate stack is useful for program crashes due 00061 // to a stack overflow; by running on a alternate stack, the signal handler 00062 // may run even when normal stack space has been exausted. The downside of 00063 // using an alternate stack is that extra memory for the alternate stack needs 00064 // to be pre-allocated. 00065 bool use_alternate_stack = true; 00066 00067 // If positive, indicates the number of seconds after which the failure signal 00068 // handler is invoked to abort the program. Setting such an alarm is useful in 00069 // cases where the failure signal handler itself may become hung or 00070 // deadlocked. 00071 int alarm_on_failure_secs = 3; 00072 00073 // If true, call the previously registered signal handler for the signal that 00074 // was received (if one was registered) after the existing signal handler 00075 // runs. This mechanism can be used to chain signal handlers together. 00076 // 00077 // If false, the signal is raised to the default handler for that signal 00078 // (which normally terminates the program). 00079 // 00080 // IMPORTANT: If true, the chained fatal signal handlers must not try to 00081 // recover from the fatal signal. Instead, they should terminate the program 00082 // via some mechanism, like raising the default handler for the signal, or by 00083 // calling `_exit()`. Note that the failure signal handler may put parts of 00084 // the Abseil library into a state from which they cannot recover. 00085 bool call_previous_handler = false; 00086 00087 // If non-null, indicates a pointer to a callback function that will be called 00088 // upon failure, with a std::string argument containing failure data. This function 00089 // may be used as a hook to write failure data to a secondary location, such 00090 // as a log file. This function may also be called with null data, as a hint 00091 // to flush any buffered data before the program may be terminated. Consider 00092 // flushing any buffered data in all calls to this function. 00093 // 00094 // Since this function runs within a signal handler, it should be 00095 // async-signal-safe if possible. 00096 // See http://man7.org/linux/man-pages/man7/signal-safety.7.html 00097 void (*writerfn)(const char*) = nullptr; 00098 }; 00099 00100 // InstallFailureSignalHandler() 00101 // 00102 // Installs a signal handler for the common failure signals `SIGSEGV`, `SIGILL`, 00103 // `SIGFPE`, `SIGABRT`, `SIGTERM`, `SIGBUG`, and `SIGTRAP` (provided they exist 00104 // on the given platform). The failure signal handler dumps program failure data 00105 // useful for debugging in an unspecified format to stderr. This data may 00106 // include the program counter, a stacktrace, and register information on some 00107 // systems; do not rely on an exact format for the output, as it is subject to 00108 // change. 00109 void InstallFailureSignalHandler(const FailureSignalHandlerOptions& options); 00110 00111 namespace debugging_internal { 00112 const char* FailureSignalToString(int signo); 00113 } // namespace debugging_internal 00114 00115 } // namespace absl 00116 00117 #endif // ABSL_DEBUGGING_FAILURE_SIGNAL_HANDLER_H_