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 CLIENT_LINUX_CRASH_GENERATION_CRASH_GENERATION_SERVER_H_ 30 #define CLIENT_LINUX_CRASH_GENERATION_CRASH_GENERATION_SERVER_H_ 31 32 #include <pthread.h> 33 34 #include <string> 35 36 #include "common/using_std_string.h" 37 38 namespace google_breakpad { 39 40 class ClientInfo; 41 42 class CrashGenerationServer { 43 public: 44 // WARNING: callbacks may be invoked on a different thread 45 // than that which creates the CrashGenerationServer. They must 46 // be thread safe. 47 typedef void (*OnClientDumpRequestCallback)(void* context, 48 const ClientInfo* client_info, 49 const string* file_path); 50 51 typedef void (*OnClientExitingCallback)(void* context, 52 const ClientInfo* client_info); 53 54 // Create an instance with the given parameters. 55 // 56 // Parameter listen_fd: The server fd created by CreateReportChannel(). 57 // Parameter dump_callback: Callback for a client crash dump request. 58 // Parameter dump_context: Context for client crash dump request callback. 59 // Parameter exit_callback: Callback for client process exit. 60 // Parameter exit_context: Context for client exit callback. 61 // Parameter generate_dumps: Whether to automatically generate dumps. 62 // Client code of this class might want to generate dumps explicitly 63 // in the crash dump request callback. In that case, false can be 64 // passed for this parameter. 65 // Parameter dump_path: Path for generating dumps; required only if true is 66 // passed for generateDumps parameter; NULL can be passed otherwise. 67 CrashGenerationServer(const int listen_fd, 68 OnClientDumpRequestCallback dump_callback, 69 void* dump_context, 70 OnClientExitingCallback exit_callback, 71 void* exit_context, 72 bool generate_dumps, 73 const string* dump_path); 74 75 ~CrashGenerationServer(); 76 77 // Perform initialization steps needed to start listening to clients. 78 // 79 // Return true if initialization is successful; false otherwise. 80 bool Start(); 81 82 // Stop the server. 83 void Stop(); 84 85 // Create a "channel" that can be used by clients to report crashes 86 // to a CrashGenerationServer. |*server_fd| should be passed to 87 // this class's constructor, and |*client_fd| should be passed to 88 // the ExceptionHandler constructor in the client process. 89 static bool CreateReportChannel(int* server_fd, int* client_fd); 90 91 private: 92 // Run the server's event loop 93 void Run(); 94 95 // Invoked when an child process (client) event occurs 96 // Returning true => "keep running", false => "exit loop" 97 bool ClientEvent(short revents); 98 99 // Invoked when the controlling thread (main) event occurs 100 // Returning true => "keep running", false => "exit loop" 101 bool ControlEvent(short revents); 102 103 // Return a unique filename at which a minidump can be written 104 bool MakeMinidumpFilename(string& outFilename); 105 106 // Trampoline to |Run()| 107 static void* ThreadMain(void* arg); 108 109 int server_fd_; 110 111 OnClientDumpRequestCallback dump_callback_; 112 void* dump_context_; 113 114 OnClientExitingCallback exit_callback_; 115 void* exit_context_; 116 117 bool generate_dumps_; 118 119 string dump_dir_; 120 121 bool started_; 122 123 pthread_t thread_; 124 int control_pipe_in_; 125 int control_pipe_out_; 126 127 // disable these 128 CrashGenerationServer(const CrashGenerationServer&); 129 CrashGenerationServer& operator=(const CrashGenerationServer&); 130 }; 131 132 } // namespace google_breakpad 133 134 #endif // CLIENT_LINUX_CRASH_GENERATION_CRASH_GENERATION_SERVER_H_ 135