/thirdparty/breakpad/client/windows/crash_generation/crash_generation_server.h

http://github.com/tomahawk-player/tomahawk · C Header · 300 lines · 103 code · 63 blank · 134 comment · 0 complexity · bc6f36f965887f99ead570f0e544118f MD5 · raw file

  1. // Copyright (c) 2008, 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. #ifndef CLIENT_WINDOWS_CRASH_GENERATION_CRASH_GENERATION_SERVER_H__
  30. #define CLIENT_WINDOWS_CRASH_GENERATION_CRASH_GENERATION_SERVER_H__
  31. #include <list>
  32. #include <string>
  33. #include "client/windows/common/ipc_protocol.h"
  34. #include "client/windows/crash_generation/minidump_generator.h"
  35. #include "processor/scoped_ptr.h"
  36. namespace google_breakpad {
  37. class ClientInfo;
  38. // Abstraction for server side implementation of out-of-process crash
  39. // generation protocol for Windows platform only. It generates Windows
  40. // minidump files for client processes that request dump generation. When
  41. // the server is requested to start listening for clients (by calling the
  42. // Start method), it creates a named pipe and waits for the clients to
  43. // register. In response, it hands them event handles that the client can
  44. // signal to request dump generation. When the clients request dump
  45. // generation in this way, the server generates Windows minidump files.
  46. class CrashGenerationServer {
  47. public:
  48. typedef void (*OnClientConnectedCallback)(void* context,
  49. const ClientInfo* client_info);
  50. typedef void (*OnClientDumpRequestCallback)(void* context,
  51. const ClientInfo* client_info,
  52. const std::wstring* file_path);
  53. typedef void (*OnClientExitedCallback)(void* context,
  54. const ClientInfo* client_info);
  55. typedef void (*OnClientUploadRequestCallback)(void* context,
  56. const DWORD crash_id);
  57. // Creates an instance with the given parameters.
  58. //
  59. // Parameter pipe_name: Name of the Windows named pipe
  60. // Parameter pipe_sec_attrs Security attributes to set on the pipe. Pass
  61. // NULL to use default security on the pipe. By default, the pipe created
  62. // allows Local System, Administrators and the Creator full control and
  63. // the Everyone group read access on the pipe.
  64. // Parameter connect_callback: Callback for a new client connection.
  65. // Parameter connect_context: Context for client connection callback.
  66. // Parameter crash_callback: Callback for a client crash dump request.
  67. // Parameter crash_context: Context for client crash dump request callback.
  68. // Parameter exit_callback: Callback for client process exit.
  69. // Parameter exit_context: Context for client exit callback.
  70. // Parameter generate_dumps: Whether to automatically generate dumps.
  71. // Client code of this class might want to generate dumps explicitly in the
  72. // crash dump request callback. In that case, false can be passed for this
  73. // parameter.
  74. // Parameter dump_path: Path for generating dumps; required only if true is
  75. // passed for generateDumps parameter; NULL can be passed otherwise.
  76. CrashGenerationServer(const std::wstring& pipe_name,
  77. SECURITY_ATTRIBUTES* pipe_sec_attrs,
  78. OnClientConnectedCallback connect_callback,
  79. void* connect_context,
  80. OnClientDumpRequestCallback dump_callback,
  81. void* dump_context,
  82. OnClientExitedCallback exit_callback,
  83. void* exit_context,
  84. OnClientUploadRequestCallback upload_request_callback,
  85. void* upload_context,
  86. bool generate_dumps,
  87. const std::wstring* dump_path);
  88. ~CrashGenerationServer();
  89. // Performs initialization steps needed to start listening to clients. Upon
  90. // successful return clients may connect to this server's pipe.
  91. //
  92. // Returns true if initialization is successful; false otherwise.
  93. bool Start();
  94. private:
  95. // Various states the client can be in during the handshake with
  96. // the server.
  97. enum IPCServerState {
  98. // Server starts in this state.
  99. IPC_SERVER_STATE_UNINITIALIZED,
  100. // Server is in error state and it cannot serve any clients.
  101. IPC_SERVER_STATE_ERROR,
  102. // Server starts in this state.
  103. IPC_SERVER_STATE_INITIAL,
  104. // Server has issued an async connect to the pipe and it is waiting
  105. // for the connection to be established.
  106. IPC_SERVER_STATE_CONNECTING,
  107. // Server is connected successfully.
  108. IPC_SERVER_STATE_CONNECTED,
  109. // Server has issued an async read from the pipe and it is waiting for
  110. // the read to finish.
  111. IPC_SERVER_STATE_READING,
  112. // Server is done reading from the pipe.
  113. IPC_SERVER_STATE_READ_DONE,
  114. // Server has issued an async write to the pipe and it is waiting for
  115. // the write to finish.
  116. IPC_SERVER_STATE_WRITING,
  117. // Server is done writing to the pipe.
  118. IPC_SERVER_STATE_WRITE_DONE,
  119. // Server has issued an async read from the pipe for an ack and it
  120. // is waiting for the read to finish.
  121. IPC_SERVER_STATE_READING_ACK,
  122. // Server is done writing to the pipe and it is now ready to disconnect
  123. // and reconnect.
  124. IPC_SERVER_STATE_DISCONNECTING
  125. };
  126. //
  127. // Helper methods to handle various server IPC states.
  128. //
  129. void HandleErrorState();
  130. void HandleInitialState();
  131. void HandleConnectingState();
  132. void HandleConnectedState();
  133. void HandleReadingState();
  134. void HandleReadDoneState();
  135. void HandleWritingState();
  136. void HandleWriteDoneState();
  137. void HandleReadingAckState();
  138. void HandleDisconnectingState();
  139. // Prepares reply for a client from the given parameters.
  140. bool PrepareReply(const ClientInfo& client_info,
  141. ProtocolMessage* reply) const;
  142. // Duplicates various handles in the ClientInfo object for the client
  143. // process and stores them in the given ProtocolMessage instance. If
  144. // creating any handle fails, ProtocolMessage will contain the handles
  145. // already created successfully, which should be closed by the caller.
  146. bool CreateClientHandles(const ClientInfo& client_info,
  147. ProtocolMessage* reply) const;
  148. // Response to the given client. Return true if all steps of
  149. // responding to the client succeed, false otherwise.
  150. bool RespondToClient(ClientInfo* client_info);
  151. // Handles a connection request from the client.
  152. void HandleConnectionRequest();
  153. // Handles a dump request from the client.
  154. void HandleDumpRequest(const ClientInfo& client_info);
  155. // Callback for pipe connected event.
  156. static void CALLBACK OnPipeConnected(void* context, BOOLEAN timer_or_wait);
  157. // Callback for a dump request.
  158. static void CALLBACK OnDumpRequest(void* context, BOOLEAN timer_or_wait);
  159. // Callback for client process exit event.
  160. static void CALLBACK OnClientEnd(void* context, BOOLEAN timer_or_wait);
  161. // Releases resources for a client.
  162. static DWORD WINAPI CleanupClient(void* context);
  163. // Cleans up for the given client.
  164. void DoCleanup(ClientInfo* client_info);
  165. // Adds the given client to the list of registered clients.
  166. bool AddClient(ClientInfo* client_info);
  167. // Generates dump for the given client.
  168. bool GenerateDump(const ClientInfo& client, std::wstring* dump_path);
  169. // Puts the server in a permanent error state and sets a signal such that
  170. // the state will be immediately entered after the current state transition
  171. // is complete.
  172. void EnterErrorState();
  173. // Puts the server in the specified state and sets a signal such that the
  174. // state is immediately entered after the current state transition is
  175. // complete.
  176. void EnterStateImmediately(IPCServerState state);
  177. // Puts the server in the specified state. No signal will be set, so the state
  178. // transition will only occur when signaled manually or by completion of an
  179. // asynchronous IO operation.
  180. void EnterStateWhenSignaled(IPCServerState state);
  181. // Sync object for thread-safe access to the shared list of clients and
  182. // the server's state.
  183. CRITICAL_SECTION sync_;
  184. // List of clients.
  185. std::list<ClientInfo*> clients_;
  186. // Pipe name.
  187. std::wstring pipe_name_;
  188. // Pipe security attributes
  189. SECURITY_ATTRIBUTES* pipe_sec_attrs_;
  190. // Handle to the pipe used for handshake with clients.
  191. HANDLE pipe_;
  192. // Pipe wait handle.
  193. HANDLE pipe_wait_handle_;
  194. // Handle to server-alive mutex.
  195. HANDLE server_alive_handle_;
  196. // Callback for a successful client connection.
  197. OnClientConnectedCallback connect_callback_;
  198. // Context for client connected callback.
  199. void* connect_context_;
  200. // Callback for a client dump request.
  201. OnClientDumpRequestCallback dump_callback_;
  202. // Context for client dump request callback.
  203. void* dump_context_;
  204. // Callback for client process exit.
  205. OnClientExitedCallback exit_callback_;
  206. // Context for client process exit callback.
  207. void* exit_context_;
  208. // Callback for upload request.
  209. OnClientUploadRequestCallback upload_request_callback_;
  210. // Context for upload request callback.
  211. void* upload_context_;
  212. // Whether to generate dumps.
  213. bool generate_dumps_;
  214. // Instance of a mini dump generator.
  215. scoped_ptr<MinidumpGenerator> dump_generator_;
  216. // State of the server in performing the IPC with the client.
  217. // Note that since we restrict the pipe to one instance, we
  218. // only need to keep one state of the server. Otherwise, server
  219. // would have one state per client it is talking to.
  220. IPCServerState server_state_;
  221. // Whether the server is shutting down.
  222. bool shutting_down_;
  223. // Overlapped instance for async I/O on the pipe.
  224. OVERLAPPED overlapped_;
  225. // Message object used in IPC with the client.
  226. ProtocolMessage msg_;
  227. // Client Info for the client that's connecting to the server.
  228. ClientInfo* client_info_;
  229. // Count of clean-up work items that are currently running or are
  230. // already queued to run.
  231. volatile LONG cleanup_item_count_;
  232. // Disable copy ctor and operator=.
  233. CrashGenerationServer(const CrashGenerationServer& crash_server);
  234. CrashGenerationServer& operator=(const CrashGenerationServer& crash_server);
  235. };
  236. } // namespace google_breakpad
  237. #endif // CLIENT_WINDOWS_CRASH_GENERATION_CRASH_GENERATION_SERVER_H__