Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / breakpad / src / client / linux / handler / exception_handler.h
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
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"
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
76 class ExceptionHandler {
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);
135   ~ExceptionHandler();
136
137   const MinidumpDescriptor& minidump_descriptor() const {
138     return minidump_descriptor_;
139   }
140
141   void set_minidump_descriptor(const MinidumpDescriptor& descriptor) {
142     minidump_descriptor_ = descriptor;
143   }
144
145   void set_crash_handler(HandlerCallback callback) {
146     crash_handler_ = callback;
147   }
148
149   void set_crash_generation_client(CrashGenerationClient* client) {
150     crash_generation_client_.reset(client);
151   }
152
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();
167
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);
173
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
178   // extracted.
179   //
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);
190
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 {
194     siginfo_t siginfo;
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;
202 #endif
203   };
204
205   // Returns whether out-of-process dump generation is used or not.
206   bool IsOutOfProcess() const {
207     return crash_generation_client_.get() != NULL;
208   }
209
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,
216                       size_t mapping_size,
217                       size_t file_offset);
218
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);
222
223   // Unregister a block of memory that was registered with RegisterAppMemory.
224   void UnregisterAppMemory(void* ptr);
225
226   // Force signal handling for the specified signal.
227   bool SimulateSignalDelivery(int sig);
228
229   // Report a crash signal from an SA_SIGINFO signal handler.
230   bool HandleSignal(int sig, siginfo_t* info, void* uc);
231  private:
232   // Save the old signal handlers and install new ones.
233   static bool InstallHandlersLocked();
234   // Restore the old signal handlers.
235   static void RestoreHandlersLocked();
236
237   void PreresolveSymbols();
238   bool GenerateDump(CrashContext *context);
239   void SendContinueSignalToChild();
240   void WaitForContinueSignal();
241
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);
246
247   const FilterCallback filter_;
248   const MinidumpCallback callback_;
249   void* const callback_context_;
250
251   scoped_ptr<CrashGenerationClient> crash_generation_client_;
252
253   MinidumpDescriptor minidump_descriptor_;
254
255   HandlerCallback crash_handler_;
256
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_;
262
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
268   int fdes[2];
269
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_;
273
274   // Callers can request additional memory regions to be included in
275   // the dump.
276   AppMemoryList app_memory_list_;
277 };
278
279 }  // namespace google_breakpad
280
281 #endif  // CLIENT_LINUX_HANDLER_EXCEPTION_HANDLER_H_