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 GOOGLE_BREAKPAD_CLIENT_MAC_CRASH_GENERATION_CRASH_GENERATION_SERVER_H_ 31 #define GOOGLE_BREAKPAD_CLIENT_MAC_CRASH_GENERATION_CRASH_GENERATION_SERVER_H_ 32 33 #include <stdint.h> 34 35 #include <string> 36 37 #include "common/mac/MachIPC.h" 38 39 namespace google_breakpad { 40 41 class ClientInfo; 42 43 // Messages the server can read via its mach port 44 enum { 45 kDumpRequestMessage = 1, 46 kAcknowledgementMessage = 2, 47 kQuitMessage = 3 48 }; 49 50 // Exception details sent by the client when requesting a dump. 51 struct ExceptionInfo { 52 int32_t exception_type; 53 int32_t exception_code; 54 int64_t exception_subcode; 55 int32_t child_pid; 56 }; 57 58 class CrashGenerationServer { 59 public: 60 // WARNING: callbacks may be invoked on a different thread 61 // than that which creates the CrashGenerationServer. They must 62 // be thread safe. 63 typedef void (*OnClientDumpRequestCallback)(void *context, 64 const ClientInfo &client_info, 65 const std::string &file_path); 66 67 typedef void (*OnClientExitingCallback)(void *context, 68 const ClientInfo &client_info); 69 // If a FilterCallback returns false, the dump will not be written. 70 typedef bool (*FilterCallback)(void *context); 71 72 // Create an instance with the given parameters. 73 // 74 // mach_port_name: Named server port to listen on. 75 // filter: Callback for a client to cancel writing a dump. 76 // filter_context: Context for the filter callback. 77 // dump_callback: Callback for a client crash dump request. 78 // dump_context: Context for client crash dump request callback. 79 // exit_callback: Callback for client process exit. 80 // exit_context: Context for client exit callback. 81 // generate_dumps: Whether to automatically generate dumps. 82 // Client code of this class might want to generate dumps explicitly 83 // in the crash dump request callback. In that case, false can be 84 // passed for this parameter. 85 // dump_path: Path for generating dumps; required only if true is 86 // passed for generateDumps parameter; NULL can be passed otherwise. 87 CrashGenerationServer(const char *mach_port_name, 88 FilterCallback filter, 89 void *filter_context, 90 OnClientDumpRequestCallback dump_callback, 91 void *dump_context, 92 OnClientExitingCallback exit_callback, 93 void *exit_context, 94 bool generate_dumps, 95 const std::string &dump_path); 96 97 ~CrashGenerationServer(); 98 99 // Perform initialization steps needed to start listening to clients. 100 // 101 // Return true if initialization is successful; false otherwise. 102 bool Start(); 103 104 // Stop the server. 105 bool Stop(); 106 107 private: 108 // Return a unique filename at which a minidump can be written. 109 bool MakeMinidumpFilename(std::string &outFilename); 110 111 // Loop reading client messages and responding to them until 112 // a quit message is received. 113 static void *WaitForMessages(void *server); 114 115 // Wait for a single client message and respond to it. Returns false 116 // if a quit message was received or if an error occurred. 117 bool WaitForOneMessage(); 118 119 FilterCallback filter_; 120 void *filter_context_; 121 122 OnClientDumpRequestCallback dump_callback_; 123 void *dump_context_; 124 125 OnClientExitingCallback exit_callback_; 126 void *exit_context_; 127 128 bool generate_dumps_; 129 130 std::string dump_dir_; 131 132 bool started_; 133 134 // The mach port that receives requests to dump from child processes. 135 ReceivePort receive_port_; 136 137 // The name of the mach port. Stored so the Stop method can message 138 // the background thread to shut it down. 139 std::string mach_port_name_; 140 141 // The thread that waits on the receive port. 142 pthread_t server_thread_; 143 144 // Disable copy constructor and operator=. 145 CrashGenerationServer(const CrashGenerationServer&); 146 CrashGenerationServer& operator=(const CrashGenerationServer&); 147 }; 148 149 } // namespace google_breakpad 150 151 #endif // GOOGLE_BREAKPAD_CLIENT_MAC_CRASH_GENERATION_CRASH_GENERATION_SERVER_H_ 152