crashpad_info.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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_INFO_H_
  15. #define CRASHPAD_CLIENT_CRASHPAD_INFO_H_
  16. #include <stdint.h>
  17. #include "base/macros.h"
  18. #include "build/build_config.h"
  19. #include "client/annotation_list.h"
  20. #include "client/simple_address_range_bag.h"
  21. #include "client/simple_string_dictionary.h"
  22. #include "util/misc/tri_state.h"
  23. #if defined(OS_WIN)
  24. #include <windows.h>
  25. #endif // OS_WIN
  26. namespace crashpad {
  27. namespace internal {
  28. //! \brief A linked list of blocks representing custom streams in the minidump,
  29. //! with addresses (and size) stored as uint64_t to simplify reading from
  30. //! the handler process.
  31. struct UserDataMinidumpStreamListEntry {
  32. //! \brief The address of the next entry in the linked list.
  33. uint64_t next;
  34. //! \brief The base address of the memory block in the target process' address
  35. //! space that represents the user data stream.
  36. uint64_t base_address;
  37. //! \brief The size of memory block in the target process' address space that
  38. //! represents the user data stream.
  39. uint64_t size;
  40. //! \brief The stream type identifier.
  41. uint32_t stream_type;
  42. };
  43. } // namespace internal
  44. //! \brief A structure that can be used by a Crashpad-enabled program to
  45. //! provide information to the Crashpad crash handler.
  46. //!
  47. //! It is possible for one CrashpadInfo structure to appear in each loaded code
  48. //! module in a process, but from the perspective of the user of the client
  49. //! interface, there is only one global CrashpadInfo structure, located in the
  50. //! module that contains the client interface code.
  51. struct CrashpadInfo {
  52. public:
  53. //! \brief Returns the global CrashpadInfo structure.
  54. static CrashpadInfo* GetCrashpadInfo();
  55. CrashpadInfo();
  56. //! \brief Sets the bag of extra memory ranges to be included in the snapshot.
  57. //!
  58. //! Extra memory ranges may exist in \a address_range_bag at the time that
  59. //! this method is called, or they may be added, removed, or modified in \a
  60. //! address_range_bag after this method is called.
  61. //!
  62. //! TODO(scottmg) This is currently only supported on Windows.
  63. //!
  64. //! \param[in] address_range_bag A bag of address ranges. The CrashpadInfo
  65. //! object does not take ownership of the SimpleAddressRangeBag object.
  66. //! It is the caller’s responsibility to ensure that this pointer remains
  67. //! valid while it is in effect for a CrashpadInfo object.
  68. void set_extra_memory_ranges(SimpleAddressRangeBag* address_range_bag) {
  69. extra_memory_ranges_ = address_range_bag;
  70. }
  71. //! \brief Sets the simple annotations dictionary.
  72. //!
  73. //! Simple annotations set on a CrashpadInfo structure are interpreted by
  74. //! Crashpad as module-level annotations.
  75. //!
  76. //! Annotations may exist in \a simple_annotations at the time that this
  77. //! method is called, or they may be added, removed, or modified in \a
  78. //! simple_annotations after this method is called.
  79. //!
  80. //! \param[in] simple_annotations A dictionary that maps string keys to string
  81. //! values. The CrashpadInfo object does not take ownership of the
  82. //! SimpleStringDictionary object. It is the caller’s responsibility to
  83. //! ensure that this pointer remains valid while it is in effect for a
  84. //! CrashpadInfo object.
  85. //!
  86. //! \sa simple_annotations()
  87. void set_simple_annotations(SimpleStringDictionary* simple_annotations) {
  88. simple_annotations_ = simple_annotations;
  89. }
  90. //! \return The simple annotations dictionary.
  91. //!
  92. //! \sa set_simple_annotations()
  93. SimpleStringDictionary* simple_annotations() const {
  94. return simple_annotations_;
  95. }
  96. //! \brief Sets the annotations list.
  97. //!
  98. //! Unlike the \a simple_annotations structure, the \a annotations can
  99. //! typed data and it is not limited to a dictionary form. Annotations are
  100. //! interpreted by Crashpad as module-level annotations.
  101. //!
  102. //! Annotations may exist in \a list at the time that this method is called,
  103. //! or they may be added, removed, or modified in \a list after this method is
  104. //! called.
  105. //!
  106. //! \param[in] list A list of set Annotation objects that maintain arbitrary,
  107. //! typed key-value state. The CrashpadInfo object does not take ownership
  108. //! of the AnnotationsList object. It is the caller’s responsibility to
  109. //! ensure that this pointer remains valid while it is in effect for a
  110. //! CrashpadInfo object.
  111. //!
  112. //! \sa annotations_list()
  113. //! \sa AnnotationList::Register()
  114. void set_annotations_list(AnnotationList* list) { annotations_list_ = list; }
  115. //! \return The annotations list.
  116. //!
  117. //! \sa set_annotations_list()
  118. //! \sa AnnotationList::Get()
  119. //! \sa AnnotationList::Register()
  120. AnnotationList* annotations_list() const { return annotations_list_; }
  121. //! \brief Enables or disables Crashpad handler processing.
  122. //!
  123. //! When handling an exception, the Crashpad handler will scan all modules in
  124. //! a process. The first one that has a CrashpadInfo structure populated with
  125. //! a value other than #kUnset for this field will dictate whether the handler
  126. //! is functional or not. If all modules with a CrashpadInfo structure specify
  127. //! #kUnset, the handler will be enabled. If disabled, the Crashpad handler
  128. //! will still run and receive exceptions, but will not take any action on an
  129. //! exception on its own behalf, except for the action necessary to determine
  130. //! that it has been disabled.
  131. //!
  132. //! The Crashpad handler should not normally be disabled. More commonly, it
  133. //! is appropriate to disable crash report upload by calling
  134. //! Settings::SetUploadsEnabled().
  135. void set_crashpad_handler_behavior(TriState crashpad_handler_behavior) {
  136. crashpad_handler_behavior_ = crashpad_handler_behavior;
  137. }
  138. //! \brief Enables or disables Crashpad forwarding of exceptions to the
  139. //! system’s crash reporter.
  140. //!
  141. //! When handling an exception, the Crashpad handler will scan all modules in
  142. //! a process. The first one that has a CrashpadInfo structure populated with
  143. //! a value other than #kUnset for this field will dictate whether the
  144. //! exception is forwarded to the system’s crash reporter. If all modules with
  145. //! a CrashpadInfo structure specify #kUnset, forwarding will be enabled.
  146. //! Unless disabled, forwarding may still occur if the Crashpad handler is
  147. //! disabled by SetCrashpadHandlerState(). Even when forwarding is enabled,
  148. //! the Crashpad handler may choose not to forward all exceptions to the
  149. //! system’s crash reporter in cases where it has reason to believe that the
  150. //! system’s crash reporter would not normally have handled the exception in
  151. //! Crashpad’s absence.
  152. void set_system_crash_reporter_forwarding(
  153. TriState system_crash_reporter_forwarding) {
  154. system_crash_reporter_forwarding_ = system_crash_reporter_forwarding;
  155. }
  156. //! \brief Enables or disables Crashpad capturing indirectly referenced memory
  157. //! in the minidump.
  158. //!
  159. //! When handling an exception, the Crashpad handler will scan all modules in
  160. //! a process. The first one that has a CrashpadInfo structure populated with
  161. //! a value other than #kUnset for this field will dictate whether the extra
  162. //! memory is captured.
  163. //!
  164. //! This causes Crashpad to include pages of data referenced by locals or
  165. //! other stack memory. Turning this on can increase the size of the minidump
  166. //! significantly.
  167. //!
  168. //! \param[in] gather_indirectly_referenced_memory Whether extra memory should
  169. //! be gathered.
  170. //! \param[in] limit The amount of memory in bytes after which no more
  171. //! indirectly gathered memory should be captured. This value is only used
  172. //! when \a gather_indirectly_referenced_memory is TriState::kEnabled.
  173. void set_gather_indirectly_referenced_memory(
  174. TriState gather_indirectly_referenced_memory,
  175. uint32_t limit) {
  176. gather_indirectly_referenced_memory_ = gather_indirectly_referenced_memory;
  177. indirectly_referenced_memory_cap_ = limit;
  178. }
  179. //! \brief Adds a custom stream to the minidump.
  180. //!
  181. //! The memory block referenced by \a data and \a size will added to the
  182. //! minidump as separate stream with type \a stream_type. The memory referred
  183. //! to by \a data and \a size is owned by the caller and must remain valid
  184. //! while it is in effect for the CrashpadInfo object.
  185. //!
  186. //! Note that streams will appear in the minidump in the reverse order to
  187. //! which they are added.
  188. //!
  189. //! TODO(scottmg) This is currently only supported on Windows.
  190. //!
  191. //! \param[in] stream_type The stream type identifier to use. This should be
  192. //! normally be larger than `MINIDUMP_STREAM_TYPE::LastReservedStream`
  193. //! which is `0xffff`.
  194. //! \param[in] data The base pointer of the stream data.
  195. //! \param[in] size The size of the stream data.
  196. void AddUserDataMinidumpStream(uint32_t stream_type,
  197. const void* data,
  198. size_t size);
  199. enum : uint32_t {
  200. kSignature = 'CPad',
  201. };
  202. private:
  203. // The compiler won’t necessarily see anyone using these fields, but it
  204. // shouldn’t warn about that. These fields aren’t intended for use by the
  205. // process they’re found in, they’re supposed to be read by the crash
  206. // reporting process.
  207. #if defined(__clang__)
  208. #pragma clang diagnostic push
  209. #pragma clang diagnostic ignored "-Wunused-private-field"
  210. #endif
  211. // Fields present in version 1, subject to a check of the size_ field:
  212. uint32_t signature_; // kSignature
  213. uint32_t size_; // The size of the entire CrashpadInfo structure.
  214. uint32_t version_; // kCrashpadInfoVersion
  215. uint32_t indirectly_referenced_memory_cap_;
  216. uint32_t padding_0_;
  217. TriState crashpad_handler_behavior_;
  218. TriState system_crash_reporter_forwarding_;
  219. TriState gather_indirectly_referenced_memory_;
  220. uint8_t padding_1_;
  221. SimpleAddressRangeBag* extra_memory_ranges_; // weak
  222. SimpleStringDictionary* simple_annotations_; // weak
  223. internal::UserDataMinidumpStreamListEntry* user_data_minidump_stream_head_;
  224. AnnotationList* annotations_list_; // weak
  225. // It’s generally safe to add new fields without changing
  226. // kCrashpadInfoVersion, because readers should check size_ and ignore fields
  227. // that aren’t present, as well as unknown fields.
  228. //
  229. // Adding fields? Consider snapshot/crashpad_info_size_test_module.cc too.
  230. #if defined(__clang__)
  231. #pragma clang diagnostic pop
  232. #endif
  233. DISALLOW_COPY_AND_ASSIGN(CrashpadInfo);
  234. };
  235. } // namespace crashpad
  236. #endif // CRASHPAD_CLIENT_CRASHPAD_INFO_H_