Planeshift
exception_handler.h
Go to the documentation of this file.
1 // Copyright (c) 2010 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 #ifndef CLIENT_LINUX_HANDLER_EXCEPTION_HANDLER_H_
31 #define CLIENT_LINUX_HANDLER_EXCEPTION_HANDLER_H_
32 
33 #include <string>
34 #include <vector>
35 
36 #include <pthread.h>
37 #include <signal.h>
38 #include <stdint.h>
39 #include <stdio.h>
40 #include <sys/ucontext.h>
41 
45 #include "common/scoped_ptr.h"
48 
49 namespace google_breakpad {
50 
51 // ExceptionHandler
52 //
53 // ExceptionHandler can write a minidump file when an exception occurs,
54 // or when WriteMinidump() is called explicitly by your program.
55 //
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.
62 //
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.
67 //
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.
72 //
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.
75 
77  public:
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.
82  //
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);
88 
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.
94  //
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,
106  void* context,
107  bool succeeded);
108 
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,
116  void* context);
117 
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);
136 
138  return minidump_descriptor_;
139  }
140 
141  void set_minidump_descriptor(const MinidumpDescriptor& descriptor) {
142  minidump_descriptor_ = descriptor;
143  }
144 
146  crash_handler_ = callback;
147  }
148 
149  // Writes a minidump immediately. This can be used to capture the execution
150  // state independently of a crash.
151  // Returns true on success.
152  // If the ExceptionHandler has been created with a path, a new file is
153  // generated for each minidump. The file path can be retrieved in the
154  // MinidumpDescriptor passed to the MinidumpCallback or by accessing the
155  // MinidumpDescriptor directly from the ExceptionHandler (with
156  // minidump_descriptor()).
157  // If the ExceptionHandler has been created with a file descriptor, the file
158  // descriptor is repositioned to its beginning and the previous generated
159  // minidump is overwritten.
160  // Note that this method is not supposed to be called from a compromised
161  // context as it uses the heap.
162  bool WriteMinidump();
163 
164  // Convenience form of WriteMinidump which does not require an
165  // ExceptionHandler instance.
166  static bool WriteMinidump(const string& dump_path,
167  MinidumpCallback callback,
168  void* callback_context);
169 
170  // Write a minidump of |child| immediately. This can be used to
171  // capture the execution state of |child| independently of a crash.
172  // Pass a meaningful |child_blamed_thread| to make that thread in
173  // the child process the one from which a crash signature is
174  // extracted.
175  //
176  // WARNING: the return of this function *must* happen before
177  // the code that will eventually reap |child| executes.
178  // Otherwise there's a pernicious race condition in which |child|
179  // exits, is reaped, another process created with its pid, then that
180  // new process dumped.
181  static bool WriteMinidumpForChild(pid_t child,
182  pid_t child_blamed_thread,
183  const string& dump_path,
184  MinidumpCallback callback,
185  void* callback_context);
186 
187  // This structure is passed to minidump_writer.h:WriteMinidump via an opaque
188  // blob. It shouldn't be needed in any user code.
189  struct CrashContext {
190  siginfo_t siginfo;
191  pid_t tid; // the crashing thread.
192  struct ucontext context;
193 #if !defined(__ARM_EABI__)
194  // #ifdef this out because FP state is not part of user ABI for Linux ARM.
195  struct _libc_fpstate float_state;
196 #endif
197  };
198 
199  // Returns whether out-of-process dump generation is used or not.
200  bool IsOutOfProcess() const {
201  return crash_generation_client_.get() != NULL;
202  }
203 
204  // Add information about a memory mapping. This can be used if
205  // a custom library loader is used that maps things in a way
206  // that the linux dumper can't handle by reading the maps file.
207  void AddMappingInfo(const string& name,
208  const uint8_t identifier[sizeof(MDGUID)],
209  uintptr_t start_address,
210  size_t mapping_size,
211  size_t file_offset);
212 
213  // Register a block of memory of length bytes starting at address ptr
214  // to be copied to the minidump when a crash happens.
215  void RegisterAppMemory(void* ptr, size_t length);
216 
217  // Unregister a block of memory that was registered with RegisterAppMemory.
218  void UnregisterAppMemory(void* ptr);
219 
220  // Force signal handling for the specified signal.
221  bool SimulateSignalDelivery(int sig);
222  private:
223  // Save the old signal handlers and install new ones.
224  static bool InstallHandlersLocked();
225  // Restore the old signal handlers.
226  static void RestoreHandlersLocked();
227 
228  void PreresolveSymbols();
229  bool GenerateDump(CrashContext *context);
230  void SendContinueSignalToChild();
231  void WaitForContinueSignal();
232 
233  static void SignalHandler(int sig, siginfo_t* info, void* uc);
234  bool HandleSignal(int sig, siginfo_t* info, void* uc);
235  static int ThreadEntry(void* arg);
236  bool DoDump(pid_t crashing_process, const void* context,
237  size_t context_size);
238 
239  const FilterCallback filter_;
240  const MinidumpCallback callback_;
241  void* const callback_context_;
242 
243  scoped_ptr<CrashGenerationClient> crash_generation_client_;
244 
245  MinidumpDescriptor minidump_descriptor_;
246 
247  HandlerCallback crash_handler_;
248 
249  // The global exception handler stack. This is need becuase there may exist
250  // multiple ExceptionHandler instances in a process. Each will have itself
251  // registered in this stack.
252  static std::vector<ExceptionHandler*> *handler_stack_;
253  static pthread_mutex_t handler_stack_mutex_;
254 
255  // We need to explicitly enable ptrace of parent processes on some
256  // kernels, but we need to know the PID of the cloned process before we
257  // can do this. We create a pipe which we can use to block the
258  // cloned process after creating it, until we have explicitly enabled
259  // ptrace. This is used to store the file descriptors for the pipe
260  int fdes[2];
261 
262  // Callers can add extra info about mappings for cases where the
263  // dumper code cannot extract enough information from /proc/<pid>/maps.
264  MappingList mapping_list_;
265 
266  // Callers can request additional memory regions to be included in
267  // the dump.
268  AppMemoryList app_memory_list_;
269 };
270 
271 } // namespace google_breakpad
272 
273 #endif // CLIENT_LINUX_HANDLER_EXCEPTION_HANDLER_H_
void RegisterAppMemory(void *ptr, size_t length)
void AddMappingInfo(const string &name, const uint8_t identifier[sizeof(MDGUID)], uintptr_t start_address, size_t mapping_size, size_t file_offset)
std::list< MappingEntry > MappingList
bool(* HandlerCallback)(const void *crash_context, size_t crash_context_size, void *context)
ExceptionHandler(const MinidumpDescriptor &descriptor, FilterCallback filter, MinidumpCallback callback, void *callback_context, bool install_handler, const int server_fd)
static bool WriteMinidumpForChild(pid_t child, pid_t child_blamed_thread, const string &dump_path, MinidumpCallback callback, void *callback_context)
bool(* MinidumpCallback)(const MinidumpDescriptor &descriptor, void *context, bool succeeded)
bool(* FilterCallback)(void *context)
void set_minidump_descriptor(const MinidumpDescriptor &descriptor)
const MinidumpDescriptor & minidump_descriptor() const
void set_crash_handler(HandlerCallback callback)
std::list< AppMemory > AppMemoryList