toolkit/crashreporter/google-breakpad/src/client/linux/handler/exception_handler.h

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 // Copyright (c) 2010 Google Inc.
michael@0 2 // All rights reserved.
michael@0 3 //
michael@0 4 // Redistribution and use in source and binary forms, with or without
michael@0 5 // modification, are permitted provided that the following conditions are
michael@0 6 // met:
michael@0 7 //
michael@0 8 // * Redistributions of source code must retain the above copyright
michael@0 9 // notice, this list of conditions and the following disclaimer.
michael@0 10 // * Redistributions in binary form must reproduce the above
michael@0 11 // copyright notice, this list of conditions and the following disclaimer
michael@0 12 // in the documentation and/or other materials provided with the
michael@0 13 // distribution.
michael@0 14 // * Neither the name of Google Inc. nor the names of its
michael@0 15 // contributors may be used to endorse or promote products derived from
michael@0 16 // this software without specific prior written permission.
michael@0 17 //
michael@0 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 29
michael@0 30 #ifndef CLIENT_LINUX_HANDLER_EXCEPTION_HANDLER_H_
michael@0 31 #define CLIENT_LINUX_HANDLER_EXCEPTION_HANDLER_H_
michael@0 32
michael@0 33 #include <string>
michael@0 34 #include <vector>
michael@0 35
michael@0 36 #include <pthread.h>
michael@0 37 #include <signal.h>
michael@0 38 #include <stdint.h>
michael@0 39 #include <stdio.h>
michael@0 40 #include <sys/ucontext.h>
michael@0 41
michael@0 42 #include "client/linux/crash_generation/crash_generation_client.h"
michael@0 43 #include "client/linux/handler/minidump_descriptor.h"
michael@0 44 #include "client/linux/minidump_writer/minidump_writer.h"
michael@0 45 #include "common/scoped_ptr.h"
michael@0 46 #include "common/using_std_string.h"
michael@0 47 #include "google_breakpad/common/minidump_format.h"
michael@0 48
michael@0 49 namespace google_breakpad {
michael@0 50
michael@0 51 // ExceptionHandler
michael@0 52 //
michael@0 53 // ExceptionHandler can write a minidump file when an exception occurs,
michael@0 54 // or when WriteMinidump() is called explicitly by your program.
michael@0 55 //
michael@0 56 // To have the exception handler write minidumps when an uncaught exception
michael@0 57 // (crash) occurs, you should create an instance early in the execution
michael@0 58 // of your program, and keep it around for the entire time you want to
michael@0 59 // have crash handling active (typically, until shutdown).
michael@0 60 // (NOTE): There should be only be one this kind of exception handler
michael@0 61 // object per process.
michael@0 62 //
michael@0 63 // If you want to write minidumps without installing the exception handler,
michael@0 64 // you can create an ExceptionHandler with install_handler set to false,
michael@0 65 // then call WriteMinidump. You can also use this technique if you want to
michael@0 66 // use different minidump callbacks for different call sites.
michael@0 67 //
michael@0 68 // In either case, a callback function is called when a minidump is written,
michael@0 69 // which receives the full path or file descriptor of the minidump. The
michael@0 70 // caller can collect and write additional application state to that minidump,
michael@0 71 // and launch an external crash-reporting application.
michael@0 72 //
michael@0 73 // Caller should try to make the callbacks as crash-friendly as possible,
michael@0 74 // it should avoid use heap memory allocation as much as possible.
michael@0 75
michael@0 76 class ExceptionHandler {
michael@0 77 public:
michael@0 78 // A callback function to run before Breakpad performs any substantial
michael@0 79 // processing of an exception. A FilterCallback is called before writing
michael@0 80 // a minidump. |context| is the parameter supplied by the user as
michael@0 81 // callback_context when the handler was created.
michael@0 82 //
michael@0 83 // If a FilterCallback returns true, Breakpad will continue processing,
michael@0 84 // attempting to write a minidump. If a FilterCallback returns false,
michael@0 85 // Breakpad will immediately report the exception as unhandled without
michael@0 86 // writing a minidump, allowing another handler the opportunity to handle it.
michael@0 87 typedef bool (*FilterCallback)(void *context);
michael@0 88
michael@0 89 // A callback function to run after the minidump has been written.
michael@0 90 // |descriptor| contains the file descriptor or file path containing the
michael@0 91 // minidump. |context| is the parameter supplied by the user as
michael@0 92 // callback_context when the handler was created. |succeeded| indicates
michael@0 93 // whether a minidump file was successfully written.
michael@0 94 //
michael@0 95 // If an exception occurred and the callback returns true, Breakpad will
michael@0 96 // treat the exception as fully-handled, suppressing any other handlers from
michael@0 97 // being notified of the exception. If the callback returns false, Breakpad
michael@0 98 // will treat the exception as unhandled, and allow another handler to handle
michael@0 99 // it. If there are no other handlers, Breakpad will report the exception to
michael@0 100 // the system as unhandled, allowing a debugger or native crash dialog the
michael@0 101 // opportunity to handle the exception. Most callback implementations
michael@0 102 // should normally return the value of |succeeded|, or when they wish to
michael@0 103 // not report an exception of handled, false. Callbacks will rarely want to
michael@0 104 // return true directly (unless |succeeded| is true).
michael@0 105 typedef bool (*MinidumpCallback)(const MinidumpDescriptor& descriptor,
michael@0 106 void* context,
michael@0 107 bool succeeded);
michael@0 108
michael@0 109 // In certain cases, a user may wish to handle the generation of the minidump
michael@0 110 // themselves. In this case, they can install a handler callback which is
michael@0 111 // called when a crash has occurred. If this function returns true, no other
michael@0 112 // processing of occurs and the process will shortly be crashed. If this
michael@0 113 // returns false, the normal processing continues.
michael@0 114 typedef bool (*HandlerCallback)(const void* crash_context,
michael@0 115 size_t crash_context_size,
michael@0 116 void* context);
michael@0 117
michael@0 118 // Creates a new ExceptionHandler instance to handle writing minidumps.
michael@0 119 // Before writing a minidump, the optional |filter| callback will be called.
michael@0 120 // Its return value determines whether or not Breakpad should write a
michael@0 121 // minidump. The minidump content will be written to the file path or file
michael@0 122 // descriptor from |descriptor|, and the optional |callback| is called after
michael@0 123 // writing the dump file, as described above.
michael@0 124 // If install_handler is true, then a minidump will be written whenever
michael@0 125 // an unhandled exception occurs. If it is false, minidumps will only
michael@0 126 // be written when WriteMinidump is called.
michael@0 127 // If |server_fd| is valid, the minidump is generated out-of-process. If it
michael@0 128 // is -1, in-process generation will always be used.
michael@0 129 ExceptionHandler(const MinidumpDescriptor& descriptor,
michael@0 130 FilterCallback filter,
michael@0 131 MinidumpCallback callback,
michael@0 132 void *callback_context,
michael@0 133 bool install_handler,
michael@0 134 const int server_fd);
michael@0 135 ~ExceptionHandler();
michael@0 136
michael@0 137 const MinidumpDescriptor& minidump_descriptor() const {
michael@0 138 return minidump_descriptor_;
michael@0 139 }
michael@0 140
michael@0 141 void set_minidump_descriptor(const MinidumpDescriptor& descriptor) {
michael@0 142 minidump_descriptor_ = descriptor;
michael@0 143 }
michael@0 144
michael@0 145 void set_crash_handler(HandlerCallback callback) {
michael@0 146 crash_handler_ = callback;
michael@0 147 }
michael@0 148
michael@0 149 // Writes a minidump immediately. This can be used to capture the execution
michael@0 150 // state independently of a crash.
michael@0 151 // Returns true on success.
michael@0 152 // If the ExceptionHandler has been created with a path, a new file is
michael@0 153 // generated for each minidump. The file path can be retrieved in the
michael@0 154 // MinidumpDescriptor passed to the MinidumpCallback or by accessing the
michael@0 155 // MinidumpDescriptor directly from the ExceptionHandler (with
michael@0 156 // minidump_descriptor()).
michael@0 157 // If the ExceptionHandler has been created with a file descriptor, the file
michael@0 158 // descriptor is repositioned to its beginning and the previous generated
michael@0 159 // minidump is overwritten.
michael@0 160 // Note that this method is not supposed to be called from a compromised
michael@0 161 // context as it uses the heap.
michael@0 162 bool WriteMinidump();
michael@0 163
michael@0 164 // Convenience form of WriteMinidump which does not require an
michael@0 165 // ExceptionHandler instance.
michael@0 166 static bool WriteMinidump(const string& dump_path,
michael@0 167 MinidumpCallback callback,
michael@0 168 void* callback_context);
michael@0 169
michael@0 170 // Write a minidump of |child| immediately. This can be used to
michael@0 171 // capture the execution state of |child| independently of a crash.
michael@0 172 // Pass a meaningful |child_blamed_thread| to make that thread in
michael@0 173 // the child process the one from which a crash signature is
michael@0 174 // extracted.
michael@0 175 //
michael@0 176 // WARNING: the return of this function *must* happen before
michael@0 177 // the code that will eventually reap |child| executes.
michael@0 178 // Otherwise there's a pernicious race condition in which |child|
michael@0 179 // exits, is reaped, another process created with its pid, then that
michael@0 180 // new process dumped.
michael@0 181 static bool WriteMinidumpForChild(pid_t child,
michael@0 182 pid_t child_blamed_thread,
michael@0 183 const string& dump_path,
michael@0 184 MinidumpCallback callback,
michael@0 185 void* callback_context);
michael@0 186
michael@0 187 // This structure is passed to minidump_writer.h:WriteMinidump via an opaque
michael@0 188 // blob. It shouldn't be needed in any user code.
michael@0 189 struct CrashContext {
michael@0 190 siginfo_t siginfo;
michael@0 191 pid_t tid; // the crashing thread.
michael@0 192 struct ucontext context;
michael@0 193 #if !defined(__ARM_EABI__)
michael@0 194 // #ifdef this out because FP state is not part of user ABI for Linux ARM.
michael@0 195 struct _libc_fpstate float_state;
michael@0 196 #endif
michael@0 197 };
michael@0 198
michael@0 199 // Returns whether out-of-process dump generation is used or not.
michael@0 200 bool IsOutOfProcess() const {
michael@0 201 return crash_generation_client_.get() != NULL;
michael@0 202 }
michael@0 203
michael@0 204 // Add information about a memory mapping. This can be used if
michael@0 205 // a custom library loader is used that maps things in a way
michael@0 206 // that the linux dumper can't handle by reading the maps file.
michael@0 207 void AddMappingInfo(const string& name,
michael@0 208 const uint8_t identifier[sizeof(MDGUID)],
michael@0 209 uintptr_t start_address,
michael@0 210 size_t mapping_size,
michael@0 211 size_t file_offset);
michael@0 212
michael@0 213 // Register a block of memory of length bytes starting at address ptr
michael@0 214 // to be copied to the minidump when a crash happens.
michael@0 215 void RegisterAppMemory(void* ptr, size_t length);
michael@0 216
michael@0 217 // Unregister a block of memory that was registered with RegisterAppMemory.
michael@0 218 void UnregisterAppMemory(void* ptr);
michael@0 219
michael@0 220 // Force signal handling for the specified signal.
michael@0 221 bool SimulateSignalDelivery(int sig);
michael@0 222
michael@0 223 // Report a crash signal from an SA_SIGINFO signal handler.
michael@0 224 bool HandleSignal(int sig, siginfo_t* info, void* uc);
michael@0 225 private:
michael@0 226 // Save the old signal handlers and install new ones.
michael@0 227 static bool InstallHandlersLocked();
michael@0 228 // Restore the old signal handlers.
michael@0 229 static void RestoreHandlersLocked();
michael@0 230
michael@0 231 void PreresolveSymbols();
michael@0 232 bool GenerateDump(CrashContext *context);
michael@0 233 void SendContinueSignalToChild();
michael@0 234 void WaitForContinueSignal();
michael@0 235
michael@0 236 static void SignalHandler(int sig, siginfo_t* info, void* uc);
michael@0 237 static int ThreadEntry(void* arg);
michael@0 238 bool DoDump(pid_t crashing_process, const void* context,
michael@0 239 size_t context_size);
michael@0 240
michael@0 241 const FilterCallback filter_;
michael@0 242 const MinidumpCallback callback_;
michael@0 243 void* const callback_context_;
michael@0 244
michael@0 245 scoped_ptr<CrashGenerationClient> crash_generation_client_;
michael@0 246
michael@0 247 MinidumpDescriptor minidump_descriptor_;
michael@0 248
michael@0 249 HandlerCallback crash_handler_;
michael@0 250
michael@0 251 // The global exception handler stack. This is need becuase there may exist
michael@0 252 // multiple ExceptionHandler instances in a process. Each will have itself
michael@0 253 // registered in this stack.
michael@0 254 static std::vector<ExceptionHandler*> *handler_stack_;
michael@0 255 static pthread_mutex_t handler_stack_mutex_;
michael@0 256
michael@0 257 // We need to explicitly enable ptrace of parent processes on some
michael@0 258 // kernels, but we need to know the PID of the cloned process before we
michael@0 259 // can do this. We create a pipe which we can use to block the
michael@0 260 // cloned process after creating it, until we have explicitly enabled
michael@0 261 // ptrace. This is used to store the file descriptors for the pipe
michael@0 262 int fdes[2];
michael@0 263
michael@0 264 // Callers can add extra info about mappings for cases where the
michael@0 265 // dumper code cannot extract enough information from /proc/<pid>/maps.
michael@0 266 MappingList mapping_list_;
michael@0 267
michael@0 268 // Callers can request additional memory regions to be included in
michael@0 269 // the dump.
michael@0 270 AppMemoryList app_memory_list_;
michael@0 271 };
michael@0 272
michael@0 273 } // namespace google_breakpad
michael@0 274
michael@0 275 #endif // CLIENT_LINUX_HANDLER_EXCEPTION_HANDLER_H_

mercurial