crashpad_client.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. // Copyright 2014 The Crashpad Authors. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef CRASHPAD_CLIENT_CRASHPAD_CLIENT_H_
  15. #define CRASHPAD_CLIENT_CRASHPAD_CLIENT_H_
  16. #include <map>
  17. #include <string>
  18. #include <vector>
  19. #include <stdint.h>
  20. #include "base/files/file_path.h"
  21. #include "base/macros.h"
  22. #include "build/build_config.h"
  23. #if defined(OS_MACOSX)
  24. #include "base/mac/scoped_mach_port.h"
  25. #elif defined(OS_WIN)
  26. #include <windows.h>
  27. #include "util/win/scoped_handle.h"
  28. #endif
  29. namespace crashpad {
  30. //! \brief The primary interface for an application to have Crashpad monitor
  31. //! it for crashes.
  32. class CrashpadClient {
  33. public:
  34. CrashpadClient();
  35. ~CrashpadClient();
  36. //! \brief Starts a Crashpad handler process, performing any necessary
  37. //! handshake to configure it.
  38. //!
  39. //! This method directs crashes to the Crashpad handler. On macOS, this is
  40. //! applicable to this process and all subsequent child processes. On Windows,
  41. //! child processes must also register by using SetHandlerIPCPipe().
  42. //!
  43. //! On macOS, this method starts a Crashpad handler and obtains a Mach send
  44. //! right corresponding to a receive right held by the handler process. The
  45. //! handler process runs an exception server on this port. This method sets
  46. //! the task’s exception port for `EXC_CRASH`, `EXC_RESOURCE`, and `EXC_GUARD`
  47. //! exceptions to the Mach send right obtained. The handler will be installed
  48. //! with behavior `EXCEPTION_STATE_IDENTITY | MACH_EXCEPTION_CODES` and thread
  49. //! state flavor `MACHINE_THREAD_STATE`. Exception ports are inherited, so a
  50. //! Crashpad handler started here will remain the handler for any child
  51. //! processes created after StartHandler() is called. These child processes do
  52. //! not need to call StartHandler() or be aware of Crashpad in any way. The
  53. //! Crashpad handler will receive crashes from child processes that have
  54. //! inherited it as their exception handler even after the process that called
  55. //! StartHandler() exits.
  56. //!
  57. //! On Windows, if \a asynchronous_start is `true`, this function will not
  58. //! directly call `CreateProcess()`, making it suitable for use in a
  59. //! `DllMain()`. In that case, the handler is started from a background
  60. //! thread, deferring the handler's startup. Nevertheless, regardless of the
  61. //! value of \a asynchronous_start, after calling this method, the global
  62. //! unhandled exception filter is set up, and all crashes will be handled by
  63. //! Crashpad. Optionally, use WaitForHandlerStart() to join with the
  64. //! background thread and retrieve the status of handler startup.
  65. //!
  66. //! \param[in] handler The path to a Crashpad handler executable.
  67. //! \param[in] database The path to a Crashpad database. The handler will be
  68. //! started with this path as its `--database` argument.
  69. //! \param[in] metrics_dir The path to an already existing directory where
  70. //! metrics files can be stored. The handler will be started with this
  71. //! path as its `--metrics-dir` argument.
  72. //! \param[in] url The URL of an upload server. The handler will be started
  73. //! with this URL as its `--url` argument.
  74. //! \param[in] annotations Process annotations to set in each crash report.
  75. //! The handler will be started with an `--annotation` argument for each
  76. //! element in this map.
  77. //! \param[in] arguments Additional arguments to pass to the Crashpad handler.
  78. //! Arguments passed in other parameters and arguments required to perform
  79. //! the handshake are the responsibility of this method, and must not be
  80. //! specified in this parameter.
  81. //! \param[in] restartable If `true`, the handler will be restarted if it
  82. //! dies, if this behavior is supported. This option is not available on
  83. //! all platforms, and does not function on all OS versions. If it is
  84. //! not supported, it will be ignored.
  85. //! \param[out] asynchronous_start If `true`, the handler will be started from
  86. //! a background thread. Optionally, WaitForHandlerStart() can be used at
  87. //! a suitable time to retreive the result of background startup. This
  88. //! option is only used on Windows.
  89. //!
  90. //! \return `true` on success, `false` on failure with a message logged.
  91. bool StartHandler(const base::FilePath& handler,
  92. const base::FilePath& database,
  93. const base::FilePath& metrics_dir,
  94. const std::string& url,
  95. const std::map<std::string, std::string>& annotations,
  96. const std::vector<std::string>& arguments,
  97. bool restartable,
  98. bool asynchronous_start);
  99. #if defined(OS_MACOSX) || DOXYGEN
  100. //! \brief Sets the process’ crash handler to a Mach service registered with
  101. //! the bootstrap server.
  102. //!
  103. //! This method is only defined on macOS.
  104. //!
  105. //! See StartHandler() for more detail on how the port and handler are
  106. //! configured.
  107. //!
  108. //! \param[in] service_name The service name of a Crashpad exception handler
  109. //! service previously registered with the bootstrap server.
  110. //!
  111. //! \return `true` on success, `false` on failure with a message logged.
  112. bool SetHandlerMachService(const std::string& service_name);
  113. //! \brief Sets the process’ crash handler to a Mach port.
  114. //!
  115. //! This method is only defined on macOS.
  116. //!
  117. //! See StartHandler() for more detail on how the port and handler are
  118. //! configured.
  119. //!
  120. //! \param[in] exception_port An `exception_port_t` corresponding to a
  121. //! Crashpad exception handler service.
  122. //!
  123. //! \return `true` on success, `false` on failure with a message logged.
  124. bool SetHandlerMachPort(base::mac::ScopedMachSendRight exception_port);
  125. //! \brief Retrieves a send right to the process’ crash handler Mach port.
  126. //!
  127. //! This method is only defined on macOS.
  128. //!
  129. //! This method can be used to obtain the crash handler Mach port when a
  130. //! Crashpad client process wishes to provide a send right to this port to
  131. //! another process. The IPC mechanism used to convey the right is under the
  132. //! application’s control. If the other process wishes to become a client of
  133. //! the same crash handler, it can provide the transferred right to
  134. //! SetHandlerMachPort().
  135. //!
  136. //! See StartHandler() for more detail on how the port and handler are
  137. //! configured.
  138. //!
  139. //! \return The Mach port set by SetHandlerMachPort(), possibly indirectly by
  140. //! a call to another method such as StartHandler() or
  141. //! SetHandlerMachService(). This method must only be called after a
  142. //! successful call to one of those methods. `MACH_PORT_NULL` on failure
  143. //! with a message logged.
  144. base::mac::ScopedMachSendRight GetHandlerMachPort() const;
  145. #endif
  146. #if defined(OS_WIN) || DOXYGEN
  147. //! \brief Sets the IPC pipe of a presumably-running Crashpad handler process
  148. //! which was started with StartHandler() or by other compatible means
  149. //! and does an IPC message exchange to register this process with the
  150. //! handler. Crashes will be serviced once this method returns.
  151. //!
  152. //! This method is only defined on Windows.
  153. //!
  154. //! This method sets the unhandled exception handler to a local
  155. //! function that when reached will "signal and wait" for the crash handler
  156. //! process to create the dump.
  157. //!
  158. //! \param[in] ipc_pipe The full name of the crash handler IPC pipe. This is
  159. //! a string of the form `&quot;\\.\pipe\NAME&quot;`.
  160. //!
  161. //! \return `true` on success and `false` on failure.
  162. bool SetHandlerIPCPipe(const std::wstring& ipc_pipe);
  163. //! \brief Retrieves the IPC pipe name used to register with the Crashpad
  164. //! handler.
  165. //!
  166. //! This method is only defined on Windows.
  167. //!
  168. //! This method retrieves the IPC pipe name set by SetHandlerIPCPipe(), or a
  169. //! suitable IPC pipe name chosen by StartHandler(). It must only be called
  170. //! after a successful call to one of those methods. It is intended to be used
  171. //! to obtain the IPC pipe name so that it may be passed to other processes,
  172. //! so that they may register with an existing Crashpad handler by calling
  173. //! SetHandlerIPCPipe().
  174. //!
  175. //! \return The full name of the crash handler IPC pipe, a string of the form
  176. //! `&quot;\\.\pipe\NAME&quot;`.
  177. std::wstring GetHandlerIPCPipe() const;
  178. //! \brief When `asynchronous_start` is used with StartHandler(), this method
  179. //! can be used to block until the handler launch has been completed to
  180. //! retrieve status information.
  181. //!
  182. //! This method should not be used unless `asynchronous_start` was `true`.
  183. //!
  184. //! \param[in] timeout_ms The number of milliseconds to wait for a result from
  185. //! the background launch, or `0xffffffff` to block indefinitely.
  186. //!
  187. //! \return `true` if the hander startup succeeded, `false` otherwise, and an
  188. //! error message will have been logged.
  189. bool WaitForHandlerStart(unsigned int timeout_ms);
  190. //! \brief Requests that the handler capture a dump even though there hasn't
  191. //! been a crash.
  192. //!
  193. //! \param[in] context A `CONTEXT`, generally captured by CaptureContext() or
  194. //! similar.
  195. static void DumpWithoutCrash(const CONTEXT& context);
  196. //! \brief Requests that the handler capture a dump using the given \a
  197. //! exception_pointers to get the `EXCEPTION_RECORD` and `CONTEXT`.
  198. //!
  199. //! This function is not necessary in general usage as an unhandled exception
  200. //! filter is installed by StartHandler() or SetHandlerIPCPipe().
  201. //!
  202. //! \param[in] exception_pointers An `EXCEPTION_POINTERS`, as would generally
  203. //! passed to an unhandled exception filter.
  204. static void DumpAndCrash(EXCEPTION_POINTERS* exception_pointers);
  205. //! \brief Requests that the handler capture a dump of a different process.
  206. //!
  207. //! The target process must be an already-registered Crashpad client. An
  208. //! exception will be triggered in the target process, and the regular dump
  209. //! mechanism used. This function will block until the exception in the target
  210. //! process has been handled by the Crashpad handler.
  211. //!
  212. //! This function is unavailable when running on Windows XP and will return
  213. //! `false`.
  214. //!
  215. //! \param[in] process A `HANDLE` identifying the process to be dumped.
  216. //! \param[in] blame_thread If non-null, a `HANDLE` valid in the caller's
  217. //! process, referring to a thread in the target process. If this is
  218. //! supplied, instead of the exception referring to the location where the
  219. //! exception was injected, an exception record will be fabricated that
  220. //! refers to the current location of the given thread.
  221. //! \param[in] exception_code If \a blame_thread is non-null, this will be
  222. //! used as the exception code in the exception record.
  223. //!
  224. //! \return `true` if the exception was triggered successfully.
  225. bool DumpAndCrashTargetProcess(HANDLE process,
  226. HANDLE blame_thread,
  227. DWORD exception_code) const;
  228. enum : uint32_t {
  229. //! \brief The exception code (roughly "Client called") used when
  230. //! DumpAndCrashTargetProcess() triggers an exception in a target
  231. //! process.
  232. //!
  233. //! \note This value does not have any bits of the top nibble set, to avoid
  234. //! confusion with real exception codes which tend to have those bits
  235. //! set.
  236. kTriggeredExceptionCode = 0xcca11ed,
  237. };
  238. #endif
  239. #if defined(OS_MACOSX) || DOXYGEN
  240. //! \brief Configures the process to direct its crashes to the default handler
  241. //! for the operating system.
  242. //!
  243. //! On macOS, this sets the task’s exception port as in SetHandlerMachPort(),
  244. //! but the exception handler used is obtained from
  245. //! SystemCrashReporterHandler(). If the system’s crash reporter handler
  246. //! cannot be determined or set, the task’s exception ports for crash-type
  247. //! exceptions are cleared.
  248. //!
  249. //! Use of this function is strongly discouraged.
  250. //!
  251. //! \warning After a call to this function, Crashpad will no longer monitor
  252. //! the process for crashes until a subsequent call to
  253. //! SetHandlerMachPort().
  254. //!
  255. //! \note This is provided as a static function to allow it to be used in
  256. //! situations where a CrashpadClient object is not otherwise available.
  257. //! This may be useful when a child process inherits its parent’s Crashpad
  258. //! handler, but wants to sever this tie.
  259. static void UseSystemDefaultHandler();
  260. #endif
  261. private:
  262. #if defined(OS_MACOSX)
  263. base::mac::ScopedMachSendRight exception_port_;
  264. #elif defined(OS_WIN)
  265. std::wstring ipc_pipe_;
  266. ScopedKernelHANDLE handler_start_thread_;
  267. #endif // OS_MACOSX
  268. DISALLOW_COPY_AND_ASSIGN(CrashpadClient);
  269. };
  270. } // namespace crashpad
  271. #endif // CRASHPAD_CLIENT_CRASHPAD_CLIENT_H_