/thirdparty/breakpad/client/mac/handler/minidump_generator.h

http://github.com/tomahawk-player/tomahawk · C Header · 218 lines · 120 code · 32 blank · 66 comment · 5 complexity · b3b0a250dca35f4729c9e5ce7914a065 MD5 · raw file

  1. // Copyright (c) 2006, 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. // minidump_generator.h: Create a minidump of the current MacOS process.
  30. #ifndef CLIENT_MAC_GENERATOR_MINIDUMP_GENERATOR_H__
  31. #define CLIENT_MAC_GENERATOR_MINIDUMP_GENERATOR_H__
  32. #include <mach/mach.h>
  33. #include <TargetConditionals.h>
  34. #include <string>
  35. #include "client/minidump_file_writer.h"
  36. #include "common/memory.h"
  37. #include "common/mac/macho_utilities.h"
  38. #include "google_breakpad/common/minidump_format.h"
  39. #include "dynamic_images.h"
  40. #include "mach_vm_compat.h"
  41. #if !TARGET_OS_IPHONE && (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_7)
  42. #define HAS_PPC_SUPPORT
  43. #endif
  44. #if defined(__arm__)
  45. #define HAS_ARM_SUPPORT
  46. #elif defined(__i386__) || defined(__x86_64__)
  47. #define HAS_X86_SUPPORT
  48. #endif
  49. namespace google_breakpad {
  50. using std::string;
  51. // Use the REGISTER_FROM_THREADSTATE to access a register name from the
  52. // breakpad_thread_state_t structure.
  53. #if __DARWIN_UNIX03 || TARGET_CPU_X86_64 || TARGET_CPU_PPC64 || TARGET_CPU_ARM
  54. // In The 10.5 SDK Headers Apple prepended __ to the variable names in the
  55. // i386_thread_state_t structure. There's no good way to tell what version of
  56. // the SDK we're compiling against so we just toggle on the same preprocessor
  57. // symbol Apple's headers use.
  58. #define REGISTER_FROM_THREADSTATE(a, b) ((a)->__ ## b)
  59. #else
  60. #define REGISTER_FROM_THREADSTATE(a, b) (a->b)
  61. #endif
  62. // Creates a minidump file of the current process. If there is exception data,
  63. // use SetExceptionInformation() to add this to the minidump. The minidump
  64. // file is generated by the Write() function.
  65. // Usage:
  66. // MinidumpGenerator minidump();
  67. // minidump.Write("/tmp/minidump");
  68. //
  69. class MinidumpGenerator {
  70. public:
  71. MinidumpGenerator();
  72. MinidumpGenerator(mach_port_t crashing_task, mach_port_t handler_thread);
  73. virtual ~MinidumpGenerator();
  74. // Return <dir>/<unique_name>.dmp
  75. // Sets |unique_name| (if requested) to the unique name for the minidump
  76. static string UniqueNameInDirectory(const string &dir, string *unique_name);
  77. // Write out the minidump into |path|
  78. // All of the components of |path| must exist and be writable
  79. // Return true if successful, false otherwise
  80. bool Write(const char *path);
  81. // Specify some exception information, if applicable
  82. void SetExceptionInformation(int type, int code, int subcode,
  83. mach_port_t thread_name) {
  84. exception_type_ = type;
  85. exception_code_ = code;
  86. exception_subcode_ = subcode;
  87. exception_thread_ = thread_name;
  88. }
  89. // Gather system information. This should be call at least once before using
  90. // the MinidumpGenerator class.
  91. static void GatherSystemInformation();
  92. protected:
  93. // Overridable Stream writers
  94. virtual bool WriteExceptionStream(MDRawDirectory *exception_stream);
  95. // Overridable Helper
  96. virtual bool WriteThreadStream(mach_port_t thread_id, MDRawThread *thread);
  97. private:
  98. typedef bool (MinidumpGenerator::*WriteStreamFN)(MDRawDirectory *);
  99. // Stream writers
  100. bool WriteThreadListStream(MDRawDirectory *thread_list_stream);
  101. bool WriteMemoryListStream(MDRawDirectory *memory_list_stream);
  102. bool WriteSystemInfoStream(MDRawDirectory *system_info_stream);
  103. bool WriteModuleListStream(MDRawDirectory *module_list_stream);
  104. bool WriteMiscInfoStream(MDRawDirectory *misc_info_stream);
  105. bool WriteBreakpadInfoStream(MDRawDirectory *breakpad_info_stream);
  106. // Helpers
  107. u_int64_t CurrentPCForStack(breakpad_thread_state_data_t state);
  108. bool GetThreadState(thread_act_t target_thread, thread_state_t state,
  109. mach_msg_type_number_t *count);
  110. bool WriteStackFromStartAddress(mach_vm_address_t start_addr,
  111. MDMemoryDescriptor *stack_location);
  112. bool WriteStack(breakpad_thread_state_data_t state,
  113. MDMemoryDescriptor *stack_location);
  114. bool WriteContext(breakpad_thread_state_data_t state,
  115. MDLocationDescriptor *register_location);
  116. bool WriteCVRecord(MDRawModule *module, int cpu_type,
  117. const char *module_path, bool in_memory);
  118. bool WriteModuleStream(unsigned int index, MDRawModule *module);
  119. size_t CalculateStackSize(mach_vm_address_t start_addr);
  120. int FindExecutableModule();
  121. // Per-CPU implementations of these methods
  122. #ifdef HAS_ARM_SUPPORT
  123. bool WriteStackARM(breakpad_thread_state_data_t state,
  124. MDMemoryDescriptor *stack_location);
  125. bool WriteContextARM(breakpad_thread_state_data_t state,
  126. MDLocationDescriptor *register_location);
  127. u_int64_t CurrentPCForStackARM(breakpad_thread_state_data_t state);
  128. #endif
  129. #ifdef HAS_PPC_SUPPORT
  130. bool WriteStackPPC(breakpad_thread_state_data_t state,
  131. MDMemoryDescriptor *stack_location);
  132. bool WriteContextPPC(breakpad_thread_state_data_t state,
  133. MDLocationDescriptor *register_location);
  134. u_int64_t CurrentPCForStackPPC(breakpad_thread_state_data_t state);
  135. bool WriteStackPPC64(breakpad_thread_state_data_t state,
  136. MDMemoryDescriptor *stack_location);
  137. bool WriteContextPPC64(breakpad_thread_state_data_t state,
  138. MDLocationDescriptor *register_location);
  139. u_int64_t CurrentPCForStackPPC64(breakpad_thread_state_data_t state);
  140. #endif
  141. #ifdef HAS_X86_SUPPORT
  142. bool WriteStackX86(breakpad_thread_state_data_t state,
  143. MDMemoryDescriptor *stack_location);
  144. bool WriteContextX86(breakpad_thread_state_data_t state,
  145. MDLocationDescriptor *register_location);
  146. u_int64_t CurrentPCForStackX86(breakpad_thread_state_data_t state);
  147. bool WriteStackX86_64(breakpad_thread_state_data_t state,
  148. MDMemoryDescriptor *stack_location);
  149. bool WriteContextX86_64(breakpad_thread_state_data_t state,
  150. MDLocationDescriptor *register_location);
  151. u_int64_t CurrentPCForStackX86_64(breakpad_thread_state_data_t state);
  152. #endif
  153. // disallow copy ctor and operator=
  154. explicit MinidumpGenerator(const MinidumpGenerator &);
  155. void operator=(const MinidumpGenerator &);
  156. protected:
  157. // Use this writer to put the data to disk
  158. MinidumpFileWriter writer_;
  159. private:
  160. // Exception information
  161. int exception_type_;
  162. int exception_code_;
  163. int exception_subcode_;
  164. mach_port_t exception_thread_;
  165. mach_port_t crashing_task_;
  166. mach_port_t handler_thread_;
  167. // CPU type of the task being dumped.
  168. cpu_type_t cpu_type_;
  169. // System information
  170. static char build_string_[16];
  171. static int os_major_version_;
  172. static int os_minor_version_;
  173. static int os_build_number_;
  174. // Information about dynamically loaded code
  175. DynamicImages *dynamic_images_;
  176. // PageAllocator makes it possible to allocate memory
  177. // directly from the system, even while handling an exception.
  178. mutable PageAllocator allocator_;
  179. protected:
  180. // Blocks of memory written to the dump. These are all currently
  181. // written while writing the thread list stream, but saved here
  182. // so a memory list stream can be written afterwards.
  183. wasteful_vector<MDMemoryDescriptor> memory_blocks_;
  184. };
  185. } // namespace google_breakpad
  186. #endif // CLIENT_MAC_GENERATOR_MINIDUMP_GENERATOR_H__