Planeshift

minidump_generator.h

Go to the documentation of this file.
00001 // Copyright (c) 2006, Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 
00030 // minidump_generator.h:  Create a minidump of the current MacOS process.
00031 
00032 #ifndef CLIENT_MAC_GENERATOR_MINIDUMP_GENERATOR_H__
00033 #define CLIENT_MAC_GENERATOR_MINIDUMP_GENERATOR_H__
00034 
00035 #include <mach/mach.h>
00036 #include <TargetConditionals.h>
00037 
00038 #include <string>
00039 
00040 #include "client/minidump_file_writer.h"
00041 #include "common/memory.h"
00042 #include "common/mac/macho_utilities.h"
00043 #include "google_breakpad/common/minidump_format.h"
00044 
00045 #include "dynamic_images.h"
00046 #include "mach_vm_compat.h"
00047 
00048 #if !TARGET_OS_IPHONE && (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7)
00049   #define HAS_PPC_SUPPORT
00050 #endif
00051 #if defined(__arm__)
00052   #define HAS_ARM_SUPPORT
00053 #elif defined(__i386__) || defined(__x86_64__)
00054   #define HAS_X86_SUPPORT
00055 #endif
00056 
00057 namespace google_breakpad {
00058 
00059 using std::string;
00060 
00061 // Use the REGISTER_FROM_THREADSTATE to access a register name from the
00062 // breakpad_thread_state_t structure.
00063 #if __DARWIN_UNIX03 || TARGET_CPU_X86_64 || TARGET_CPU_PPC64 || TARGET_CPU_ARM
00064 // In The 10.5 SDK Headers Apple prepended __ to the variable names in the
00065 // i386_thread_state_t structure.  There's no good way to tell what version of
00066 // the SDK we're compiling against so we just toggle on the same preprocessor
00067 // symbol Apple's headers use.
00068 #define REGISTER_FROM_THREADSTATE(a, b) ((a)->__ ## b)
00069 #else
00070 #define REGISTER_FROM_THREADSTATE(a, b) (a->b)
00071 #endif
00072 
00073 // Creates a minidump file of the current process.  If there is exception data,
00074 // use SetExceptionInformation() to add this to the minidump.  The minidump
00075 // file is generated by the Write() function.
00076 // Usage:
00077 // MinidumpGenerator minidump();
00078 // minidump.Write("/tmp/minidump");
00079 //
00080 class MinidumpGenerator {
00081  public:
00082   MinidumpGenerator();
00083   MinidumpGenerator(mach_port_t crashing_task, mach_port_t handler_thread);
00084 
00085   virtual ~MinidumpGenerator();
00086 
00087   // Return <dir>/<unique_name>.dmp
00088   // Sets |unique_name| (if requested) to the unique name for the minidump
00089   static string UniqueNameInDirectory(const string &dir, string *unique_name);
00090 
00091   // Write out the minidump into |path|
00092   // All of the components of |path| must exist and be writable
00093   // Return true if successful, false otherwise
00094   bool Write(const char *path);
00095 
00096   // Specify some exception information, if applicable
00097   void SetExceptionInformation(int type, int code, int subcode,
00098                                mach_port_t thread_name) {
00099     exception_type_ = type;
00100     exception_code_ = code;
00101     exception_subcode_ = subcode;
00102     exception_thread_ = thread_name;
00103   }
00104 
00105   // Specify the task context. If |task_context| is not NULL, it will be used
00106   // to retrieve the context of the current thread, instead of using
00107   // |thread_get_state|.
00108   void SetTaskContext(ucontext_t *task_context);
00109 
00110   // Gather system information.  This should be call at least once before using
00111   // the MinidumpGenerator class.
00112   static void GatherSystemInformation();
00113 
00114  protected:
00115   // Overridable Stream writers
00116   virtual bool WriteExceptionStream(MDRawDirectory *exception_stream);
00117 
00118   // Overridable Helper
00119   virtual bool WriteThreadStream(mach_port_t thread_id, MDRawThread *thread);
00120 
00121  private:
00122   typedef bool (MinidumpGenerator::*WriteStreamFN)(MDRawDirectory *);
00123 
00124   // Stream writers
00125   bool WriteThreadListStream(MDRawDirectory *thread_list_stream);
00126   bool WriteMemoryListStream(MDRawDirectory *memory_list_stream);
00127   bool WriteSystemInfoStream(MDRawDirectory *system_info_stream);
00128   bool WriteModuleListStream(MDRawDirectory *module_list_stream);
00129   bool WriteMiscInfoStream(MDRawDirectory *misc_info_stream);
00130   bool WriteBreakpadInfoStream(MDRawDirectory *breakpad_info_stream);
00131 
00132   // Helpers
00133   uint64_t CurrentPCForStack(breakpad_thread_state_data_t state);
00134   bool GetThreadState(thread_act_t target_thread, thread_state_t state,
00135                       mach_msg_type_number_t *count);
00136   bool WriteStackFromStartAddress(mach_vm_address_t start_addr,
00137                                   MDMemoryDescriptor *stack_location);
00138   bool WriteStack(breakpad_thread_state_data_t state,
00139                   MDMemoryDescriptor *stack_location);
00140   bool WriteContext(breakpad_thread_state_data_t state,
00141                     MDLocationDescriptor *register_location);
00142   bool WriteCVRecord(MDRawModule *module, int cpu_type,
00143                      const char *module_path, bool in_memory);
00144   bool WriteModuleStream(unsigned int index, MDRawModule *module);
00145   size_t CalculateStackSize(mach_vm_address_t start_addr);
00146   int  FindExecutableModule();
00147 
00148   // Per-CPU implementations of these methods
00149 #ifdef HAS_ARM_SUPPORT
00150   bool WriteStackARM(breakpad_thread_state_data_t state,
00151                      MDMemoryDescriptor *stack_location);
00152   bool WriteContextARM(breakpad_thread_state_data_t state,
00153                        MDLocationDescriptor *register_location);
00154   uint64_t CurrentPCForStackARM(breakpad_thread_state_data_t state);
00155 #endif
00156 #ifdef HAS_PPC_SUPPORT
00157   bool WriteStackPPC(breakpad_thread_state_data_t state,
00158                      MDMemoryDescriptor *stack_location);
00159   bool WriteContextPPC(breakpad_thread_state_data_t state,
00160                        MDLocationDescriptor *register_location);
00161   uint64_t CurrentPCForStackPPC(breakpad_thread_state_data_t state);
00162   bool WriteStackPPC64(breakpad_thread_state_data_t state,
00163                        MDMemoryDescriptor *stack_location);
00164   bool WriteContextPPC64(breakpad_thread_state_data_t state,
00165                        MDLocationDescriptor *register_location);
00166   uint64_t CurrentPCForStackPPC64(breakpad_thread_state_data_t state);
00167 #endif
00168 #ifdef HAS_X86_SUPPORT
00169   bool WriteStackX86(breakpad_thread_state_data_t state,
00170                        MDMemoryDescriptor *stack_location);
00171   bool WriteContextX86(breakpad_thread_state_data_t state,
00172                        MDLocationDescriptor *register_location);
00173   uint64_t CurrentPCForStackX86(breakpad_thread_state_data_t state);
00174   bool WriteStackX86_64(breakpad_thread_state_data_t state,
00175                         MDMemoryDescriptor *stack_location);
00176   bool WriteContextX86_64(breakpad_thread_state_data_t state,
00177                           MDLocationDescriptor *register_location);
00178   uint64_t CurrentPCForStackX86_64(breakpad_thread_state_data_t state);
00179 #endif
00180 
00181   // disallow copy ctor and operator=
00182   explicit MinidumpGenerator(const MinidumpGenerator &);
00183   void operator=(const MinidumpGenerator &);
00184 
00185  protected:
00186   // Use this writer to put the data to disk
00187   MinidumpFileWriter writer_;
00188 
00189  private:
00190   // Exception information
00191   int exception_type_;
00192   int exception_code_;
00193   int exception_subcode_;
00194   mach_port_t exception_thread_;
00195   mach_port_t crashing_task_;
00196   mach_port_t handler_thread_;
00197 
00198   // CPU type of the task being dumped.
00199   cpu_type_t cpu_type_;
00200   
00201   // System information
00202   static char build_string_[16];
00203   static int os_major_version_;
00204   static int os_minor_version_;
00205   static int os_build_number_;
00206 
00207   // Context of the task to dump.
00208   ucontext_t *task_context_;
00209 
00210   // Information about dynamically loaded code
00211   DynamicImages *dynamic_images_;
00212 
00213   // PageAllocator makes it possible to allocate memory
00214   // directly from the system, even while handling an exception.
00215   mutable PageAllocator allocator_;
00216 
00217  protected:
00218   // Blocks of memory written to the dump. These are all currently
00219   // written while writing the thread list stream, but saved here
00220   // so a memory list stream can be written afterwards.
00221   wasteful_vector<MDMemoryDescriptor> memory_blocks_;
00222 };
00223 
00224 }  // namespace google_breakpad
00225 
00226 #endif  // CLIENT_MAC_GENERATOR_MINIDUMP_GENERATOR_H__