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 #include <TargetConditionals.h>
37 
38 #include <string>
39 
40 #include "mac/handler/ucontext_compat.h"
41 #include "minidump_file_writer.h"
42 #include "common/memory_allocator.h"
43 #include "common/mac/macho_utilities.h"
44 #include "google_breakpad/common/minidump_format.h"
45 
46 #include "dynamic_images.h"
47 #include "mach_vm_compat.h"
48 
49 #if !TARGET_OS_IPHONE && (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7)
50   #define HAS_PPC_SUPPORT
51 #endif
52 #if defined(__arm__)
53 #define HAS_ARM_SUPPORT
54 #elif defined(__aarch64__)
55 #define HAS_ARM64_SUPPORT
56 #elif defined(__i386__) || defined(__x86_64__)
57   #define HAS_X86_SUPPORT
58 #endif
59 
60 namespace google_breakpad {
61 
62 using std::string;
63 
64 // Use the REGISTER_FROM_THREADSTATE to access a register name from the
65 // breakpad_thread_state_t structure.
66 #if __DARWIN_OPAQUE_ARM_THREAD_STATE64
67 #define ARRAY_REGISTER_FROM_THREADSTATE(a, b, i) ((a)->__##b[i])
68 #define GET_REGISTER_FROM_THREADSTATE_fp(a)                                    \
69   (reinterpret_cast<uintptr_t>((a)->__opaque_fp))
70 #define GET_REGISTER_FROM_THREADSTATE_lr(a)                                    \
71   (reinterpret_cast<uintptr_t>((a)->__opaque_lr))
72 #define GET_REGISTER_FROM_THREADSTATE_sp(a)                                    \
73   (reinterpret_cast<uintptr_t>((a)->__opaque_sp))
74 #define GET_REGISTER_FROM_THREADSTATE_pc(a)                                    \
75   (reinterpret_cast<uintptr_t>((a)->__opaque_pc))
76 #define GET_REGISTER_FROM_THREADSTATE_cpsr(a) ((a)->__cpsr)
77 #define GET_REGISTER_FROM_THREADSTATE_flags(a) ((a)->__opaque_flags)
78 #define REGISTER_FROM_THREADSTATE(a, b) (GET_REGISTER_FROM_THREADSTATE_##b(a))
79 #elif __DARWIN_UNIX03 || TARGET_CPU_X86_64 || TARGET_CPU_PPC64 || TARGET_CPU_ARM
80 // In The 10.5 SDK Headers Apple prepended __ to the variable names in the
81 // i386_thread_state_t structure.  There's no good way to tell what version of
82 // the SDK we're compiling against so we just toggle on the same preprocessor
83 // symbol Apple's headers use.
84 #define REGISTER_FROM_THREADSTATE(a, b) ((a)->__ ## b)
85 #define ARRAY_REGISTER_FROM_THREADSTATE(a, b, i)                               \
86   REGISTER_FROM_THREADSTATE(a, b[i])
87 #else
88 #define REGISTER_FROM_THREADSTATE(a, b) (a->b)
89 #define ARRAY_REGISTER_FROM_THREADSTATE(a, b, i)                               \
90   REGISTER_FROM_THREADSTATE(a, b[i])
91 #endif
92 
93 // Creates a minidump file of the current process.  If there is exception data,
94 // use SetExceptionInformation() to add this to the minidump.  The minidump
95 // file is generated by the Write() function.
96 // Usage:
97 // MinidumpGenerator minidump();
98 // minidump.Write("/tmp/minidump");
99 //
100 class MinidumpGenerator {
101  public:
102   MinidumpGenerator();
103   MinidumpGenerator(mach_port_t crashing_task, mach_port_t handler_thread);
104 
105   virtual ~MinidumpGenerator();
106 
107   // Return <dir>/<unique_name>.dmp
108   // Sets |unique_name| (if requested) to the unique name for the minidump
109   static string UniqueNameInDirectory(const string &dir, string *unique_name);
110 
111   // Write out the minidump into |path|
112   // All of the components of |path| must exist and be writable
113   // Return true if successful, false otherwise
114   bool Write(const char *path);
115 
116   // Specify some exception information, if applicable
SetExceptionInformation(int type,int code,int64_t subcode,mach_port_t thread_name)117   void SetExceptionInformation(int type, int code, int64_t subcode,
118                                mach_port_t thread_name) {
119     exception_type_ = type;
120     exception_code_ = code;
121     exception_subcode_ = subcode;
122     exception_thread_ = thread_name;
123   }
124 
125   // Specify the task context. If |task_context| is not NULL, it will be used
126   // to retrieve the context of the current thread, instead of using
127   // |thread_get_state|.
128   void SetTaskContext(breakpad_ucontext_t *task_context);
129 
130   // Gather system information.  This should be call at least once before using
131   // the MinidumpGenerator class.
132   static void GatherSystemInformation();
133 
134  protected:
135   // Overridable Stream writers
136   virtual bool WriteExceptionStream(MDRawDirectory *exception_stream);
137 
138   // Overridable Helper
139   virtual bool WriteThreadStream(mach_port_t thread_id, MDRawThread *thread);
140 
141  private:
142   typedef bool (MinidumpGenerator::*WriteStreamFN)(MDRawDirectory *);
143 
144   // Stream writers
145   bool WriteThreadListStream(MDRawDirectory *thread_list_stream);
146   bool WriteMemoryListStream(MDRawDirectory *memory_list_stream);
147   bool WriteSystemInfoStream(MDRawDirectory *system_info_stream);
148   bool WriteModuleListStream(MDRawDirectory *module_list_stream);
149   bool WriteMiscInfoStream(MDRawDirectory *misc_info_stream);
150   bool WriteBreakpadInfoStream(MDRawDirectory *breakpad_info_stream);
151   bool WriteCrashInfoStream(MDRawDirectory *crash_info_stream);
152   bool WriteThreadNamesStream(MDRawDirectory *thread_names_stream);
153 
154   // Helpers
155   uint64_t CurrentPCForStack(breakpad_thread_state_data_t state);
156   bool GetThreadState(thread_act_t target_thread, thread_state_t state,
157                       mach_msg_type_number_t *count);
158   bool WriteStackFromStartAddress(mach_vm_address_t start_addr,
159                                   MDMemoryDescriptor *stack_location);
160   bool WriteStack(breakpad_thread_state_data_t state,
161                   MDMemoryDescriptor *stack_location);
162   bool WriteContext(breakpad_thread_state_data_t state,
163                     MDLocationDescriptor *register_location);
164   bool WriteCVRecord(MDRawModule *module, int cpu_type, int cpu_subtype,
165                      const char *module_path, bool in_memory,
166                      bool out_of_process, bool in_dyld_shared_cache);
167   bool WriteModuleStream(unsigned int index, MDRawModule *module);
168   bool WriteCrashInfoRecord(MDLocationDescriptor *location,
169                             const char *module_path,
170                             const char *crash_info,
171                             unsigned long crash_info_size,
172                             bool out_of_process,
173                             bool in_dyld_shared_cache);
174   bool WriteThreadName(mach_port_t thread_id,
175                        MDRawThreadName *thread_name);
176   size_t CalculateStackSize(mach_vm_address_t start_addr);
177   int  FindExecutableModule();
178 
179   // Per-CPU implementations of these methods
180 #ifdef HAS_ARM_SUPPORT
181   bool WriteStackARM(breakpad_thread_state_data_t state,
182                      MDMemoryDescriptor *stack_location);
183   bool WriteContextARM(breakpad_thread_state_data_t state,
184                        MDLocationDescriptor *register_location);
185   uint64_t CurrentPCForStackARM(breakpad_thread_state_data_t state);
186 #endif
187 #ifdef HAS_ARM64_SUPPORT
188   bool WriteStackARM64(breakpad_thread_state_data_t state,
189                        MDMemoryDescriptor *stack_location);
190   bool WriteContextARM64(breakpad_thread_state_data_t state,
191                          MDLocationDescriptor *register_location);
192   uint64_t CurrentPCForStackARM64(breakpad_thread_state_data_t state);
193 #endif
194 #ifdef HAS_PPC_SUPPORT
195   bool WriteStackPPC(breakpad_thread_state_data_t state,
196                      MDMemoryDescriptor *stack_location);
197   bool WriteContextPPC(breakpad_thread_state_data_t state,
198                        MDLocationDescriptor *register_location);
199   uint64_t CurrentPCForStackPPC(breakpad_thread_state_data_t state);
200   bool WriteStackPPC64(breakpad_thread_state_data_t state,
201                        MDMemoryDescriptor *stack_location);
202   bool WriteContextPPC64(breakpad_thread_state_data_t state,
203                        MDLocationDescriptor *register_location);
204   uint64_t CurrentPCForStackPPC64(breakpad_thread_state_data_t state);
205 #endif
206 #ifdef HAS_X86_SUPPORT
207   bool WriteStackX86(breakpad_thread_state_data_t state,
208                        MDMemoryDescriptor *stack_location);
209   bool WriteContextX86(breakpad_thread_state_data_t state,
210                        MDLocationDescriptor *register_location);
211   uint64_t CurrentPCForStackX86(breakpad_thread_state_data_t state);
212   bool WriteStackX86_64(breakpad_thread_state_data_t state,
213                         MDMemoryDescriptor *stack_location);
214   bool WriteContextX86_64(breakpad_thread_state_data_t state,
215                           MDLocationDescriptor *register_location);
216   uint64_t CurrentPCForStackX86_64(breakpad_thread_state_data_t state);
217 #endif
218 
219   // disallow copy ctor and operator=
220   explicit MinidumpGenerator(const MinidumpGenerator &);
221   void operator=(const MinidumpGenerator &);
222 
223  protected:
224   // Use this writer to put the data to disk
225   MinidumpFileWriter writer_;
226 
227  private:
228   // Exception information
229   int exception_type_;
230   int exception_code_;
231   int64_t exception_subcode_;
232   mach_port_t exception_thread_;
233   mach_port_t crashing_task_;
234   mach_port_t handler_thread_;
235 
236   // CPU type of the task being dumped.
237   cpu_type_t cpu_type_;
238 
239   // System information
240   static char build_string_[16];
241   static int os_major_version_;
242   static int os_minor_version_;
243   static int os_build_number_;
244 
245   // Context of the task to dump.
246   breakpad_ucontext_t *task_context_;
247 
248   // Information about dynamically loaded code
249   DynamicImages *dynamic_images_;
250 
251   // PageAllocator makes it possible to allocate memory
252   // directly from the system, even while handling an exception.
253   mutable PageAllocator allocator_;
254 
255  protected:
256   // Blocks of memory written to the dump. These are all currently
257   // written while writing the thread list stream, but saved here
258   // so a memory list stream can be written afterwards.
259   wasteful_vector<MDMemoryDescriptor> memory_blocks_;
260 };
261 
262 }  // namespace google_breakpad
263 
264 #endif  // CLIENT_MAC_GENERATOR_MINIDUMP_GENERATOR_H__
265