xref: /aosp_15_r20/external/abseil-cpp/absl/debugging/failure_signal_handler.h (revision 9356374a3709195abf420251b3e825997ff56c0f)
1*9356374aSAndroid Build Coastguard Worker // Copyright 2018 The Abseil Authors.
2*9356374aSAndroid Build Coastguard Worker //
3*9356374aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*9356374aSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*9356374aSAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*9356374aSAndroid Build Coastguard Worker //
7*9356374aSAndroid Build Coastguard Worker //      https://www.apache.org/licenses/LICENSE-2.0
8*9356374aSAndroid Build Coastguard Worker //
9*9356374aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*9356374aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*9356374aSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*9356374aSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*9356374aSAndroid Build Coastguard Worker // limitations under the License.
14*9356374aSAndroid Build Coastguard Worker //
15*9356374aSAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
16*9356374aSAndroid Build Coastguard Worker // File: failure_signal_handler.h
17*9356374aSAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
18*9356374aSAndroid Build Coastguard Worker //
19*9356374aSAndroid Build Coastguard Worker // This file configures the Abseil *failure signal handler* to capture and dump
20*9356374aSAndroid Build Coastguard Worker // useful debugging information (such as a stacktrace) upon program failure.
21*9356374aSAndroid Build Coastguard Worker //
22*9356374aSAndroid Build Coastguard Worker // To use the failure signal handler, call `absl::InstallFailureSignalHandler()`
23*9356374aSAndroid Build Coastguard Worker // very early in your program, usually in the first few lines of main():
24*9356374aSAndroid Build Coastguard Worker //
25*9356374aSAndroid Build Coastguard Worker // int main(int argc, char** argv) {
26*9356374aSAndroid Build Coastguard Worker //   // Initialize the symbolizer to get a human-readable stack trace
27*9356374aSAndroid Build Coastguard Worker //   absl::InitializeSymbolizer(argv[0]);
28*9356374aSAndroid Build Coastguard Worker //
29*9356374aSAndroid Build Coastguard Worker //   absl::FailureSignalHandlerOptions options;
30*9356374aSAndroid Build Coastguard Worker //   absl::InstallFailureSignalHandler(options);
31*9356374aSAndroid Build Coastguard Worker //   DoSomethingInteresting();
32*9356374aSAndroid Build Coastguard Worker //   return 0;
33*9356374aSAndroid Build Coastguard Worker // }
34*9356374aSAndroid Build Coastguard Worker //
35*9356374aSAndroid Build Coastguard Worker // Any program that raises a fatal signal (such as `SIGSEGV`, `SIGILL`,
36*9356374aSAndroid Build Coastguard Worker // `SIGFPE`, `SIGABRT`, `SIGTERM`, `SIGBUS`, and `SIGTRAP`) will call the
37*9356374aSAndroid Build Coastguard Worker // installed failure signal handler and provide debugging information to stderr.
38*9356374aSAndroid Build Coastguard Worker //
39*9356374aSAndroid Build Coastguard Worker // Note that you should *not* install the Abseil failure signal handler more
40*9356374aSAndroid Build Coastguard Worker // than once. You may, of course, have another (non-Abseil) failure signal
41*9356374aSAndroid Build Coastguard Worker // handler installed (which would be triggered if Abseil's failure signal
42*9356374aSAndroid Build Coastguard Worker // handler sets `call_previous_handler` to `true`).
43*9356374aSAndroid Build Coastguard Worker 
44*9356374aSAndroid Build Coastguard Worker #ifndef ABSL_DEBUGGING_FAILURE_SIGNAL_HANDLER_H_
45*9356374aSAndroid Build Coastguard Worker #define ABSL_DEBUGGING_FAILURE_SIGNAL_HANDLER_H_
46*9356374aSAndroid Build Coastguard Worker 
47*9356374aSAndroid Build Coastguard Worker #include "absl/base/config.h"
48*9356374aSAndroid Build Coastguard Worker 
49*9356374aSAndroid Build Coastguard Worker namespace absl {
50*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_BEGIN
51*9356374aSAndroid Build Coastguard Worker 
52*9356374aSAndroid Build Coastguard Worker // FailureSignalHandlerOptions
53*9356374aSAndroid Build Coastguard Worker //
54*9356374aSAndroid Build Coastguard Worker // Struct for holding `absl::InstallFailureSignalHandler()` configuration
55*9356374aSAndroid Build Coastguard Worker // options.
56*9356374aSAndroid Build Coastguard Worker struct FailureSignalHandlerOptions {
57*9356374aSAndroid Build Coastguard Worker   // If true, try to symbolize the stacktrace emitted on failure, provided that
58*9356374aSAndroid Build Coastguard Worker   // you have initialized a symbolizer for that purpose. (See symbolize.h for
59*9356374aSAndroid Build Coastguard Worker   // more information.)
60*9356374aSAndroid Build Coastguard Worker   bool symbolize_stacktrace = true;
61*9356374aSAndroid Build Coastguard Worker 
62*9356374aSAndroid Build Coastguard Worker   // If true, try to run signal handlers on an alternate stack (if supported on
63*9356374aSAndroid Build Coastguard Worker   // the given platform). An alternate stack is useful for program crashes due
64*9356374aSAndroid Build Coastguard Worker   // to a stack overflow; by running on a alternate stack, the signal handler
65*9356374aSAndroid Build Coastguard Worker   // may run even when normal stack space has been exhausted. The downside of
66*9356374aSAndroid Build Coastguard Worker   // using an alternate stack is that extra memory for the alternate stack needs
67*9356374aSAndroid Build Coastguard Worker   // to be pre-allocated.
68*9356374aSAndroid Build Coastguard Worker   bool use_alternate_stack = true;
69*9356374aSAndroid Build Coastguard Worker 
70*9356374aSAndroid Build Coastguard Worker   // If positive, indicates the number of seconds after which the failure signal
71*9356374aSAndroid Build Coastguard Worker   // handler is invoked to abort the program. Setting such an alarm is useful in
72*9356374aSAndroid Build Coastguard Worker   // cases where the failure signal handler itself may become hung or
73*9356374aSAndroid Build Coastguard Worker   // deadlocked.
74*9356374aSAndroid Build Coastguard Worker   int alarm_on_failure_secs = 3;
75*9356374aSAndroid Build Coastguard Worker 
76*9356374aSAndroid Build Coastguard Worker   // If true, call the previously registered signal handler for the signal that
77*9356374aSAndroid Build Coastguard Worker   // was received (if one was registered) after the existing signal handler
78*9356374aSAndroid Build Coastguard Worker   // runs. This mechanism can be used to chain signal handlers together.
79*9356374aSAndroid Build Coastguard Worker   //
80*9356374aSAndroid Build Coastguard Worker   // If false, the signal is raised to the default handler for that signal
81*9356374aSAndroid Build Coastguard Worker   // (which normally terminates the program).
82*9356374aSAndroid Build Coastguard Worker   //
83*9356374aSAndroid Build Coastguard Worker   // IMPORTANT: If true, the chained fatal signal handlers must not try to
84*9356374aSAndroid Build Coastguard Worker   // recover from the fatal signal. Instead, they should terminate the program
85*9356374aSAndroid Build Coastguard Worker   // via some mechanism, like raising the default handler for the signal, or by
86*9356374aSAndroid Build Coastguard Worker   // calling `_exit()`. Note that the failure signal handler may put parts of
87*9356374aSAndroid Build Coastguard Worker   // the Abseil library into a state from which they cannot recover.
88*9356374aSAndroid Build Coastguard Worker   bool call_previous_handler = false;
89*9356374aSAndroid Build Coastguard Worker 
90*9356374aSAndroid Build Coastguard Worker   // If non-null, indicates a pointer to a callback function that will be called
91*9356374aSAndroid Build Coastguard Worker   // upon failure, with a string argument containing failure data. This function
92*9356374aSAndroid Build Coastguard Worker   // may be used as a hook to write failure data to a secondary location, such
93*9356374aSAndroid Build Coastguard Worker   // as a log file. This function will also be called with null data, as a hint
94*9356374aSAndroid Build Coastguard Worker   // to flush any buffered data before the program may be terminated. Consider
95*9356374aSAndroid Build Coastguard Worker   // flushing any buffered data in all calls to this function.
96*9356374aSAndroid Build Coastguard Worker   //
97*9356374aSAndroid Build Coastguard Worker   // Since this function runs within a signal handler, it should be
98*9356374aSAndroid Build Coastguard Worker   // async-signal-safe if possible.
99*9356374aSAndroid Build Coastguard Worker   // See http://man7.org/linux/man-pages/man7/signal-safety.7.html
100*9356374aSAndroid Build Coastguard Worker   void (*writerfn)(const char*) = nullptr;
101*9356374aSAndroid Build Coastguard Worker };
102*9356374aSAndroid Build Coastguard Worker 
103*9356374aSAndroid Build Coastguard Worker // InstallFailureSignalHandler()
104*9356374aSAndroid Build Coastguard Worker //
105*9356374aSAndroid Build Coastguard Worker // Installs a signal handler for the common failure signals `SIGSEGV`, `SIGILL`,
106*9356374aSAndroid Build Coastguard Worker // `SIGFPE`, `SIGABRT`, `SIGTERM`, `SIGBUG`, and `SIGTRAP` (provided they exist
107*9356374aSAndroid Build Coastguard Worker // on the given platform). The failure signal handler dumps program failure data
108*9356374aSAndroid Build Coastguard Worker // useful for debugging in an unspecified format to stderr. This data may
109*9356374aSAndroid Build Coastguard Worker // include the program counter, a stacktrace, and register information on some
110*9356374aSAndroid Build Coastguard Worker // systems; do not rely on an exact format for the output, as it is subject to
111*9356374aSAndroid Build Coastguard Worker // change.
112*9356374aSAndroid Build Coastguard Worker void InstallFailureSignalHandler(const FailureSignalHandlerOptions& options);
113*9356374aSAndroid Build Coastguard Worker 
114*9356374aSAndroid Build Coastguard Worker namespace debugging_internal {
115*9356374aSAndroid Build Coastguard Worker const char* FailureSignalToString(int signo);
116*9356374aSAndroid Build Coastguard Worker }  // namespace debugging_internal
117*9356374aSAndroid Build Coastguard Worker 
118*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_END
119*9356374aSAndroid Build Coastguard Worker }  // namespace absl
120*9356374aSAndroid Build Coastguard Worker 
121*9356374aSAndroid Build Coastguard Worker #endif  // ABSL_DEBUGGING_FAILURE_SIGNAL_HANDLER_H_
122