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