1 // Copyright (c) 2010 Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
30 #ifndef CLIENT_LINUX_HANDLER_EXCEPTION_HANDLER_H_
31 #define CLIENT_LINUX_HANDLER_EXCEPTION_HANDLER_H_
40 #include <sys/ucontext.h>
42 #include "client/linux/crash_generation/crash_generation_client.h"
43 #include "client/linux/handler/minidump_descriptor.h"
44 #include "client/linux/minidump_writer/minidump_writer.h"
45 #include "common/scoped_ptr.h"
46 #include "common/using_std_string.h"
47 #include "google_breakpad/common/minidump_format.h"
49 namespace google_breakpad {
53 // ExceptionHandler can write a minidump file when an exception occurs,
54 // or when WriteMinidump() is called explicitly by your program.
56 // To have the exception handler write minidumps when an uncaught exception
57 // (crash) occurs, you should create an instance early in the execution
58 // of your program, and keep it around for the entire time you want to
59 // have crash handling active (typically, until shutdown).
60 // (NOTE): There should be only be one this kind of exception handler
61 // object per process.
63 // If you want to write minidumps without installing the exception handler,
64 // you can create an ExceptionHandler with install_handler set to false,
65 // then call WriteMinidump. You can also use this technique if you want to
66 // use different minidump callbacks for different call sites.
68 // In either case, a callback function is called when a minidump is written,
69 // which receives the full path or file descriptor of the minidump. The
70 // caller can collect and write additional application state to that minidump,
71 // and launch an external crash-reporting application.
73 // Caller should try to make the callbacks as crash-friendly as possible,
74 // it should avoid use heap memory allocation as much as possible.
76 class ExceptionHandler {
78 // A callback function to run before Breakpad performs any substantial
79 // processing of an exception. A FilterCallback is called before writing
80 // a minidump. |context| is the parameter supplied by the user as
81 // callback_context when the handler was created.
83 // If a FilterCallback returns true, Breakpad will continue processing,
84 // attempting to write a minidump. If a FilterCallback returns false,
85 // Breakpad will immediately report the exception as unhandled without
86 // writing a minidump, allowing another handler the opportunity to handle it.
87 typedef bool (*FilterCallback)(void *context);
89 // A callback function to run after the minidump has been written.
90 // |descriptor| contains the file descriptor or file path containing the
91 // minidump. |context| is the parameter supplied by the user as
92 // callback_context when the handler was created. |succeeded| indicates
93 // whether a minidump file was successfully written.
95 // If an exception occurred and the callback returns true, Breakpad will
96 // treat the exception as fully-handled, suppressing any other handlers from
97 // being notified of the exception. If the callback returns false, Breakpad
98 // will treat the exception as unhandled, and allow another handler to handle
99 // it. If there are no other handlers, Breakpad will report the exception to
100 // the system as unhandled, allowing a debugger or native crash dialog the
101 // opportunity to handle the exception. Most callback implementations
102 // should normally return the value of |succeeded|, or when they wish to
103 // not report an exception of handled, false. Callbacks will rarely want to
104 // return true directly (unless |succeeded| is true).
105 typedef bool (*MinidumpCallback)(const MinidumpDescriptor& descriptor,
109 // In certain cases, a user may wish to handle the generation of the minidump
110 // themselves. In this case, they can install a handler callback which is
111 // called when a crash has occurred. If this function returns true, no other
112 // processing of occurs and the process will shortly be crashed. If this
113 // returns false, the normal processing continues.
114 typedef bool (*HandlerCallback)(const void* crash_context,
115 size_t crash_context_size,
118 // Creates a new ExceptionHandler instance to handle writing minidumps.
119 // Before writing a minidump, the optional |filter| callback will be called.
120 // Its return value determines whether or not Breakpad should write a
121 // minidump. The minidump content will be written to the file path or file
122 // descriptor from |descriptor|, and the optional |callback| is called after
123 // writing the dump file, as described above.
124 // If install_handler is true, then a minidump will be written whenever
125 // an unhandled exception occurs. If it is false, minidumps will only
126 // be written when WriteMinidump is called.
127 // If |server_fd| is valid, the minidump is generated out-of-process. If it
128 // is -1, in-process generation will always be used.
129 ExceptionHandler(const MinidumpDescriptor& descriptor,
130 FilterCallback filter,
131 MinidumpCallback callback,
132 void *callback_context,
133 bool install_handler,
134 const int server_fd);
137 const MinidumpDescriptor& minidump_descriptor() const {
138 return minidump_descriptor_;
141 void set_minidump_descriptor(const MinidumpDescriptor& descriptor) {
142 minidump_descriptor_ = descriptor;
145 void set_crash_handler(HandlerCallback callback) {
146 crash_handler_ = callback;
149 void set_crash_generation_client(CrashGenerationClient* client) {
150 crash_generation_client_.reset(client);
153 // Writes a minidump immediately. This can be used to capture the execution
154 // state independently of a crash.
155 // Returns true on success.
156 // If the ExceptionHandler has been created with a path, a new file is
157 // generated for each minidump. The file path can be retrieved in the
158 // MinidumpDescriptor passed to the MinidumpCallback or by accessing the
159 // MinidumpDescriptor directly from the ExceptionHandler (with
160 // minidump_descriptor()).
161 // If the ExceptionHandler has been created with a file descriptor, the file
162 // descriptor is repositioned to its beginning and the previous generated
163 // minidump is overwritten.
164 // Note that this method is not supposed to be called from a compromised
165 // context as it uses the heap.
166 bool WriteMinidump();
168 // Convenience form of WriteMinidump which does not require an
169 // ExceptionHandler instance.
170 static bool WriteMinidump(const string& dump_path,
171 MinidumpCallback callback,
172 void* callback_context);
174 // Write a minidump of |child| immediately. This can be used to
175 // capture the execution state of |child| independently of a crash.
176 // Pass a meaningful |child_blamed_thread| to make that thread in
177 // the child process the one from which a crash signature is
180 // WARNING: the return of this function *must* happen before
181 // the code that will eventually reap |child| executes.
182 // Otherwise there's a pernicious race condition in which |child|
183 // exits, is reaped, another process created with its pid, then that
184 // new process dumped.
185 static bool WriteMinidumpForChild(pid_t child,
186 pid_t child_blamed_thread,
187 const string& dump_path,
188 MinidumpCallback callback,
189 void* callback_context);
191 // This structure is passed to minidump_writer.h:WriteMinidump via an opaque
192 // blob. It shouldn't be needed in any user code.
193 struct CrashContext {
195 pid_t tid; // the crashing thread.
196 struct ucontext context;
197 #if !defined(__ARM_EABI__) && !defined(__mips__)
198 // #ifdef this out because FP state is not part of user ABI for Linux ARM.
199 // In case of MIPS Linux FP state is already part of struct
200 // ucontext so 'float_state' is not required.
201 fpstate_t float_state;
205 // Returns whether out-of-process dump generation is used or not.
206 bool IsOutOfProcess() const {
207 return crash_generation_client_.get() != NULL;
210 // Add information about a memory mapping. This can be used if
211 // a custom library loader is used that maps things in a way
212 // that the linux dumper can't handle by reading the maps file.
213 void AddMappingInfo(const string& name,
214 const uint8_t identifier[sizeof(MDGUID)],
215 uintptr_t start_address,
219 // Register a block of memory of length bytes starting at address ptr
220 // to be copied to the minidump when a crash happens.
221 void RegisterAppMemory(void* ptr, size_t length);
223 // Unregister a block of memory that was registered with RegisterAppMemory.
224 void UnregisterAppMemory(void* ptr);
226 // Force signal handling for the specified signal.
227 bool SimulateSignalDelivery(int sig);
229 // Report a crash signal from an SA_SIGINFO signal handler.
230 bool HandleSignal(int sig, siginfo_t* info, void* uc);
232 // Save the old signal handlers and install new ones.
233 static bool InstallHandlersLocked();
234 // Restore the old signal handlers.
235 static void RestoreHandlersLocked();
237 void PreresolveSymbols();
238 bool GenerateDump(CrashContext *context);
239 void SendContinueSignalToChild();
240 void WaitForContinueSignal();
242 static void SignalHandler(int sig, siginfo_t* info, void* uc);
243 static int ThreadEntry(void* arg);
244 bool DoDump(pid_t crashing_process, const void* context,
245 size_t context_size);
247 const FilterCallback filter_;
248 const MinidumpCallback callback_;
249 void* const callback_context_;
251 scoped_ptr<CrashGenerationClient> crash_generation_client_;
253 MinidumpDescriptor minidump_descriptor_;
255 HandlerCallback crash_handler_;
257 // The global exception handler stack. This is need becuase there may exist
258 // multiple ExceptionHandler instances in a process. Each will have itself
259 // registered in this stack.
260 static std::vector<ExceptionHandler*> *handler_stack_;
261 static pthread_mutex_t handler_stack_mutex_;
263 // We need to explicitly enable ptrace of parent processes on some
264 // kernels, but we need to know the PID of the cloned process before we
265 // can do this. We create a pipe which we can use to block the
266 // cloned process after creating it, until we have explicitly enabled
267 // ptrace. This is used to store the file descriptors for the pipe
270 // Callers can add extra info about mappings for cases where the
271 // dumper code cannot extract enough information from /proc/<pid>/maps.
272 MappingList mapping_list_;
274 // Callers can request additional memory regions to be included in
276 AppMemoryList app_memory_list_;
279 } // namespace google_breakpad
281 #endif // CLIENT_LINUX_HANDLER_EXCEPTION_HANDLER_H_