1 // Copyright 2010 Google LLC 2 // 3 // Redistribution and use in source and binary forms, with or without 4 // modification, are permitted provided that the following conditions are 5 // met: 6 // 7 // * Redistributions of source code must retain the above copyright 8 // notice, this list of conditions and the following disclaimer. 9 // * Redistributions in binary form must reproduce the above 10 // copyright notice, this list of conditions and the following disclaimer 11 // in the documentation and/or other materials provided with the 12 // distribution. 13 // * Neither the name of Google LLC nor the names of its 14 // contributors may be used to endorse or promote products derived from 15 // this software without specific prior written permission. 16 // 17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 29 #ifndef GOOGLE_BREAKPAD_CLIENT_MAC_CRASH_GENERATION_CRASH_GENERATION_SERVER_H_ 30 #define GOOGLE_BREAKPAD_CLIENT_MAC_CRASH_GENERATION_CRASH_GENERATION_SERVER_H_ 31 32 #include <stdint.h> 33 34 #include <string> 35 36 #include "common/mac/MachIPC.h" 37 38 namespace google_breakpad { 39 40 class ClientInfo; 41 42 // Messages the server can read via its mach port 43 enum { 44 kDumpRequestMessage = 1, 45 kAcknowledgementMessage = 2, 46 kQuitMessage = 3 47 }; 48 49 // Exception details sent by the client when requesting a dump. 50 struct ExceptionInfo { 51 int32_t exception_type; 52 int32_t exception_code; 53 int32_t exception_subcode; 54 }; 55 56 class CrashGenerationServer { 57 public: 58 // WARNING: callbacks may be invoked on a different thread 59 // than that which creates the CrashGenerationServer. They must 60 // be thread safe. 61 typedef void (*OnClientDumpRequestCallback)(void* context, 62 const ClientInfo& client_info, 63 const std::string& file_path); 64 65 typedef void (*OnClientExitingCallback)(void* context, 66 const ClientInfo& client_info); 67 // If a FilterCallback returns false, the dump will not be written. 68 typedef bool (*FilterCallback)(void* context); 69 70 // Create an instance with the given parameters. 71 // 72 // mach_port_name: Named server port to listen on. 73 // filter: Callback for a client to cancel writing a dump. 74 // filter_context: Context for the filter callback. 75 // dump_callback: Callback for a client crash dump request. 76 // dump_context: Context for client crash dump request callback. 77 // exit_callback: Callback for client process exit. 78 // exit_context: Context for client exit callback. 79 // generate_dumps: Whether to automatically generate dumps. 80 // Client code of this class might want to generate dumps explicitly 81 // in the crash dump request callback. In that case, false can be 82 // passed for this parameter. 83 // dump_path: Path for generating dumps; required only if true is 84 // passed for generateDumps parameter; NULL can be passed otherwise. 85 CrashGenerationServer(const char* mach_port_name, 86 FilterCallback filter, 87 void* filter_context, 88 OnClientDumpRequestCallback dump_callback, 89 void* dump_context, 90 OnClientExitingCallback exit_callback, 91 void* exit_context, 92 bool generate_dumps, 93 const std::string& dump_path); 94 95 ~CrashGenerationServer(); 96 97 // Perform initialization steps needed to start listening to clients. 98 // 99 // Return true if initialization is successful; false otherwise. 100 bool Start(); 101 102 // Stop the server. 103 bool Stop(); 104 105 private: 106 // Return a unique filename at which a minidump can be written. 107 bool MakeMinidumpFilename(std::string& outFilename); 108 109 // Loop reading client messages and responding to them until 110 // a quit message is received. 111 static void* WaitForMessages(void* server); 112 113 // Wait for a single client message and respond to it. Returns false 114 // if a quit message was received or if an error occurred. 115 bool WaitForOneMessage(); 116 117 FilterCallback filter_; 118 void* filter_context_; 119 120 OnClientDumpRequestCallback dump_callback_; 121 void* dump_context_; 122 123 OnClientExitingCallback exit_callback_; 124 void* exit_context_; 125 126 bool generate_dumps_; 127 128 std::string dump_dir_; 129 130 bool started_; 131 132 // The mach port that receives requests to dump from child processes. 133 ReceivePort receive_port_; 134 135 // The name of the mach port. Stored so the Stop method can message 136 // the background thread to shut it down. 137 std::string mach_port_name_; 138 139 // The thread that waits on the receive port. 140 pthread_t server_thread_; 141 142 // Disable copy constructor and operator=. 143 CrashGenerationServer(const CrashGenerationServer&); 144 CrashGenerationServer& operator=(const CrashGenerationServer&); 145 }; 146 147 } // namespace google_breakpad 148 149 #endif // GOOGLE_BREAKPAD_CLIENT_MAC_CRASH_GENERATION_CRASH_GENERATION_SERVER_H_ 150