PageRenderTime 52ms CodeModel.GetById 7ms app.highlight 40ms RepoModel.GetById 1ms app.codeStats 0ms

/thirdparty/breakpad/client/mac/handler/dynamic_images.cc

http://github.com/tomahawk-player/tomahawk
C++ | 578 lines | 379 code | 99 blank | 100 comment | 60 complexity | fc976ab3be307d339fa7b31cbd70513c MD5 | raw file
  1// Copyright (c) 2007, 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#include "client/mac/handler/dynamic_images.h"
 31
 32extern "C" { // needed to compile on Leopard
 33  #include <mach-o/nlist.h>
 34  #include <stdlib.h>
 35  #include <stdio.h>
 36}
 37
 38#include <assert.h>
 39#include <AvailabilityMacros.h>
 40#include <dlfcn.h>
 41#include <mach/task_info.h>
 42#include <sys/sysctl.h>
 43#include <TargetConditionals.h>
 44
 45#include <algorithm>
 46#include <string>
 47#include <vector>
 48
 49#include "breakpad_nlist_64.h"
 50
 51#if !TARGET_OS_IPHONE
 52#include <CoreServices/CoreServices.h>
 53
 54#ifndef MAC_OS_X_VERSION_10_6
 55#define MAC_OS_X_VERSION_10_6 1060
 56#endif
 57
 58#if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6
 59
 60// Fallback declarations for TASK_DYLD_INFO and friends, introduced in
 61// <mach/task_info.h> in the Mac OS X 10.6 SDK.
 62#define TASK_DYLD_INFO 17
 63struct task_dyld_info {
 64  mach_vm_address_t all_image_info_addr;
 65  mach_vm_size_t all_image_info_size;
 66};
 67typedef struct task_dyld_info task_dyld_info_data_t;
 68typedef struct task_dyld_info *task_dyld_info_t;
 69#define TASK_DYLD_INFO_COUNT (sizeof(task_dyld_info_data_t) / sizeof(natural_t))
 70
 71#endif
 72
 73#endif  // !TARGET_OS_IPHONE
 74
 75namespace google_breakpad {
 76
 77using std::string;
 78using std::vector;
 79
 80//==============================================================================
 81// Returns the size of the memory region containing |address| and the
 82// number of bytes from |address| to the end of the region.
 83// We potentially, will extend the size of the original
 84// region by the size of the following region if it's contiguous with the
 85// first in order to handle cases when we're reading strings and they
 86// straddle two vm regions.
 87//
 88static mach_vm_size_t GetMemoryRegionSize(task_port_t target_task,
 89                                          const uint64_t address,
 90                                          mach_vm_size_t *size_to_end) {
 91  mach_vm_address_t region_base = (mach_vm_address_t)address;
 92  mach_vm_size_t region_size;
 93  natural_t nesting_level = 0;
 94  vm_region_submap_info_64 submap_info;
 95  mach_msg_type_number_t info_count = VM_REGION_SUBMAP_INFO_COUNT_64;
 96
 97  // Get information about the vm region containing |address|
 98  vm_region_recurse_info_t region_info;
 99  region_info = reinterpret_cast<vm_region_recurse_info_t>(&submap_info);
100
101  kern_return_t result =
102    mach_vm_region_recurse(target_task,
103                           &region_base,
104                           &region_size,
105                           &nesting_level,
106                           region_info,
107                           &info_count);
108
109  if (result == KERN_SUCCESS) {
110    // Get distance from |address| to the end of this region
111    *size_to_end = region_base + region_size -(mach_vm_address_t)address;
112
113    // If we want to handle strings as long as 4096 characters we may need
114    // to check if there's a vm region immediately following the first one.
115    // If so, we need to extend |*size_to_end| to go all the way to the end
116    // of the second region.
117    if (*size_to_end < 4096) {
118      // Second region starts where the first one ends
119      mach_vm_address_t region_base2 =
120        (mach_vm_address_t)(region_base + region_size);
121      mach_vm_size_t region_size2;
122
123      // Get information about the following vm region
124      result =
125        mach_vm_region_recurse(target_task,
126                               &region_base2,
127                               &region_size2,
128                               &nesting_level,
129                               region_info,
130                               &info_count);
131
132      // Extend region_size to go all the way to the end of the 2nd region
133      if (result == KERN_SUCCESS
134          && region_base2 == region_base + region_size) {
135        region_size += region_size2;
136      }
137    }
138
139    *size_to_end = region_base + region_size -(mach_vm_address_t)address;
140  } else {
141    region_size = 0;
142    *size_to_end = 0;
143  }
144
145  return region_size;
146}
147
148#define kMaxStringLength 8192
149//==============================================================================
150// Reads a NULL-terminated string from another task.
151//
152// Warning!  This will not read any strings longer than kMaxStringLength-1
153//
154static string ReadTaskString(task_port_t target_task,
155                             const uint64_t address) {
156  // The problem is we don't know how much to read until we know how long
157  // the string is. And we don't know how long the string is, until we've read
158  // the memory!  So, we'll try to read kMaxStringLength bytes
159  // (or as many bytes as we can until we reach the end of the vm region).
160  mach_vm_size_t size_to_end;
161  GetMemoryRegionSize(target_task, address, &size_to_end);
162
163  if (size_to_end > 0) {
164    mach_vm_size_t size_to_read =
165      size_to_end > kMaxStringLength ? kMaxStringLength : size_to_end;
166
167    vector<uint8_t> bytes;
168    if (ReadTaskMemory(target_task, address, (size_t)size_to_read, bytes) !=
169        KERN_SUCCESS)
170      return string();
171
172    return string(reinterpret_cast<const char*>(&bytes[0]));
173  }
174
175  return string();
176}
177
178//==============================================================================
179// Reads an address range from another task. The bytes read will be returned
180// in bytes, which will be resized as necessary.
181kern_return_t ReadTaskMemory(task_port_t target_task,
182                             const uint64_t address,
183                             size_t length,
184                             vector<uint8_t> &bytes) {
185  int systemPageSize = getpagesize();
186
187  // use the negative of the page size for the mask to find the page address
188  mach_vm_address_t page_address = address & (-systemPageSize);
189
190  mach_vm_address_t last_page_address =
191      (address + length + (systemPageSize - 1)) & (-systemPageSize);
192
193  mach_vm_size_t page_size = last_page_address - page_address;
194  uint8_t* local_start;
195  uint32_t local_length;
196
197  kern_return_t r = mach_vm_read(target_task,
198                                 page_address,
199                                 page_size,
200                                 reinterpret_cast<vm_offset_t*>(&local_start),
201                                 &local_length);
202
203  if (r != KERN_SUCCESS)
204    return r;
205
206  bytes.resize(length);
207  memcpy(&bytes[0],
208         &local_start[(mach_vm_address_t)address - page_address],
209         length);
210  mach_vm_deallocate(mach_task_self(), (uintptr_t)local_start, local_length);
211  return KERN_SUCCESS;
212}
213
214#pragma mark -
215
216//==============================================================================
217// Traits structs for specializing function templates to handle
218// 32-bit/64-bit Mach-O files.
219struct MachO32 {
220  typedef mach_header mach_header_type;
221  typedef segment_command mach_segment_command_type;
222  typedef dyld_image_info32 dyld_image_info;
223  typedef dyld_all_image_infos32 dyld_all_image_infos;
224  typedef struct nlist nlist_type;
225  static const uint32_t magic = MH_MAGIC;
226  static const uint32_t segment_load_command = LC_SEGMENT;
227};
228
229struct MachO64 {
230  typedef mach_header_64 mach_header_type;
231  typedef segment_command_64 mach_segment_command_type;
232  typedef dyld_image_info64 dyld_image_info;
233  typedef dyld_all_image_infos64 dyld_all_image_infos;
234  typedef struct nlist_64 nlist_type;
235  static const uint32_t magic = MH_MAGIC_64;
236  static const uint32_t segment_load_command = LC_SEGMENT_64;
237};
238
239template<typename MachBits>
240bool FindTextSection(DynamicImage& image) {
241  typedef typename MachBits::mach_header_type mach_header_type;
242  typedef typename MachBits::mach_segment_command_type
243      mach_segment_command_type;
244  
245  const mach_header_type* header =
246      reinterpret_cast<const mach_header_type*>(&image.header_[0]);
247
248  if(header->magic != MachBits::magic) {
249    return false;
250  }
251
252  const struct load_command *cmd =
253      reinterpret_cast<const struct load_command *>(header + 1);
254
255  bool found_text_section = false;
256  bool found_dylib_id_command = false;
257  for (unsigned int i = 0; cmd && (i < header->ncmds); ++i) {
258    if (!found_text_section) {
259      if (cmd->cmd == MachBits::segment_load_command) {
260        const mach_segment_command_type *seg =
261            reinterpret_cast<const mach_segment_command_type *>(cmd);
262
263        if (!strcmp(seg->segname, "__TEXT")) {
264          image.vmaddr_ = seg->vmaddr;
265          image.vmsize_ = seg->vmsize;
266          image.slide_ = 0;
267
268          if (seg->fileoff == 0 && seg->filesize != 0) {
269            image.slide_ =
270                (uintptr_t)image.GetLoadAddress() - (uintptr_t)seg->vmaddr;
271          }
272          found_text_section = true;
273        }
274      }
275    }
276
277    if (!found_dylib_id_command) {
278      if (cmd->cmd == LC_ID_DYLIB) {
279        const struct dylib_command *dc =
280            reinterpret_cast<const struct dylib_command *>(cmd);
281
282        image.version_ = dc->dylib.current_version;
283        found_dylib_id_command = true;
284      }
285    }
286
287    if (found_dylib_id_command && found_text_section) {
288      return true;
289    }
290
291    cmd = reinterpret_cast<const struct load_command *>
292        (reinterpret_cast<const char *>(cmd) + cmd->cmdsize);
293  }
294
295  return false;
296}
297
298//==============================================================================
299// Initializes vmaddr_, vmsize_, and slide_
300void DynamicImage::CalculateMemoryAndVersionInfo() {
301  // unless we can process the header, ensure that calls to
302  // IsValid() will return false
303  vmaddr_ = 0;
304  vmsize_ = 0;
305  slide_ = 0;
306  version_ = 0;
307
308  // The function template above does all the real work.
309  if (Is64Bit())
310    FindTextSection<MachO64>(*this);
311  else
312    FindTextSection<MachO32>(*this);
313}
314
315//==============================================================================
316// The helper function template abstracts the 32/64-bit differences.
317template<typename MachBits>
318uint32_t GetFileTypeFromHeader(DynamicImage& image) {
319  typedef typename MachBits::mach_header_type mach_header_type;
320
321  const mach_header_type* header =
322      reinterpret_cast<const mach_header_type*>(&image.header_[0]);
323  return header->filetype;
324}
325
326uint32_t DynamicImage::GetFileType() {
327  if (Is64Bit())
328    return GetFileTypeFromHeader<MachO64>(*this);
329
330  return GetFileTypeFromHeader<MachO32>(*this);
331}
332
333#pragma mark -
334
335//==============================================================================
336// Loads information about dynamically loaded code in the given task.
337DynamicImages::DynamicImages(mach_port_t task)
338    : task_(task),
339      cpu_type_(DetermineTaskCPUType(task)),
340      image_list_() {
341  ReadImageInfoForTask();
342}
343
344template<typename MachBits>
345static uint64_t LookupSymbol(const char* symbol_name,
346                             const char* filename,
347                             cpu_type_t cpu_type) {
348  typedef typename MachBits::nlist_type nlist_type;
349
350  nlist_type symbol_info[8] = {};
351  const char *symbolNames[2] = { symbol_name, "\0" };
352  nlist_type &list = symbol_info[0];
353  int invalidEntriesCount = breakpad_nlist(filename,
354                                           &list,
355                                           symbolNames,
356                                           cpu_type);
357
358  if(invalidEntriesCount != 0) {
359    return 0;
360  }
361
362  assert(list.n_value);
363  return list.n_value;
364}
365
366#if TARGET_OS_IPHONE
367static bool HasTaskDyldInfo() {
368  return true;
369}
370#else
371static SInt32 GetOSVersionInternal() {
372  SInt32 os_version = 0;
373  Gestalt(gestaltSystemVersion, &os_version);
374  return os_version;
375}
376
377static SInt32 GetOSVersion() {
378  static SInt32 os_version = GetOSVersionInternal();
379  return os_version;
380}
381
382static bool HasTaskDyldInfo() {
383#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_6
384  return true;
385#else
386  return GetOSVersion() >= 0x1060;
387#endif
388}
389#endif  // TARGET_OS_IPHONE
390
391uint64_t DynamicImages::GetDyldAllImageInfosPointer() {
392  if (HasTaskDyldInfo()) {
393    task_dyld_info_data_t task_dyld_info;
394    mach_msg_type_number_t count = TASK_DYLD_INFO_COUNT;
395    if (task_info(task_, TASK_DYLD_INFO, (task_info_t)&task_dyld_info,
396                  &count) != KERN_SUCCESS) {
397      return 0;
398    }
399
400    return (uint64_t)task_dyld_info.all_image_info_addr;
401  } else {
402    const char *imageSymbolName = "_dyld_all_image_infos";
403    const char *dyldPath = "/usr/lib/dyld";
404
405    if (Is64Bit())
406      return LookupSymbol<MachO64>(imageSymbolName, dyldPath, cpu_type_);
407    return LookupSymbol<MachO32>(imageSymbolName, dyldPath, cpu_type_);
408  }
409}
410
411//==============================================================================
412// This code was written using dyld_debug.c (from Darwin) as a guide.
413
414template<typename MachBits>
415void ReadImageInfo(DynamicImages& images,
416                   uint64_t image_list_address) {
417  typedef typename MachBits::dyld_image_info dyld_image_info;
418  typedef typename MachBits::dyld_all_image_infos dyld_all_image_infos;
419  typedef typename MachBits::mach_header_type mach_header_type;
420
421  // Read the structure inside of dyld that contains information about
422  // loaded images.  We're reading from the desired task's address space.
423
424  // Here we make the assumption that dyld loaded at the same address in
425  // the crashed process vs. this one.  This is an assumption made in
426  // "dyld_debug.c" and is said to be nearly always valid.
427  vector<uint8_t> dyld_all_info_bytes;
428  if (ReadTaskMemory(images.task_,
429                     image_list_address,
430                     sizeof(dyld_all_image_infos),
431                     dyld_all_info_bytes) != KERN_SUCCESS)
432    return;
433
434  dyld_all_image_infos *dyldInfo =
435    reinterpret_cast<dyld_all_image_infos*>(&dyld_all_info_bytes[0]);
436
437  // number of loaded images
438  int count = dyldInfo->infoArrayCount;
439
440  // Read an array of dyld_image_info structures each containing
441  // information about a loaded image.
442  vector<uint8_t> dyld_info_array_bytes;
443    if (ReadTaskMemory(images.task_,
444                       dyldInfo->infoArray,
445                       count * sizeof(dyld_image_info),
446                       dyld_info_array_bytes) != KERN_SUCCESS)
447      return;
448
449    dyld_image_info *infoArray =
450        reinterpret_cast<dyld_image_info*>(&dyld_info_array_bytes[0]);
451    images.image_list_.reserve(count);
452
453    for (int i = 0; i < count; ++i) {
454      dyld_image_info &info = infoArray[i];
455
456      // First read just the mach_header from the image in the task.
457      vector<uint8_t> mach_header_bytes;
458      if (ReadTaskMemory(images.task_,
459                         info.load_address_,
460                         sizeof(mach_header_type),
461                         mach_header_bytes) != KERN_SUCCESS)
462        continue;  // bail on this dynamic image
463
464      mach_header_type *header =
465          reinterpret_cast<mach_header_type*>(&mach_header_bytes[0]);
466
467      // Now determine the total amount necessary to read the header
468      // plus all of the load commands.
469      size_t header_size =
470          sizeof(mach_header_type) + header->sizeofcmds;
471
472      if (ReadTaskMemory(images.task_,
473                         info.load_address_,
474                         header_size,
475                         mach_header_bytes) != KERN_SUCCESS)
476        continue;
477
478      header = reinterpret_cast<mach_header_type*>(&mach_header_bytes[0]);
479
480      // Read the file name from the task's memory space.
481      string file_path;
482      if (info.file_path_) {
483        // Although we're reading kMaxStringLength bytes, it's copied in the
484        // the DynamicImage constructor below with the correct string length,
485        // so it's not really wasting memory.
486        file_path = ReadTaskString(images.task_, info.file_path_);
487      }
488
489      // Create an object representing this image and add it to our list.
490      DynamicImage *new_image;
491      new_image = new DynamicImage(&mach_header_bytes[0],
492                                   header_size,
493                                   info.load_address_,
494                                   file_path,
495                                   info.file_mod_date_,
496                                   images.task_,
497                                   images.cpu_type_);
498
499      if (new_image->IsValid()) {
500        images.image_list_.push_back(DynamicImageRef(new_image));
501      } else {
502        delete new_image;
503      }
504    }
505
506    // sorts based on loading address
507    sort(images.image_list_.begin(), images.image_list_.end());
508    // remove duplicates - this happens in certain strange cases
509    // You can see it in DashboardClient when Google Gadgets plugin
510    // is installed.  Apple's crash reporter log and gdb "info shared"
511    // both show the same library multiple times at the same address
512
513    vector<DynamicImageRef>::iterator it = unique(images.image_list_.begin(),
514                                                  images.image_list_.end());
515    images.image_list_.erase(it, images.image_list_.end());
516}
517
518void DynamicImages::ReadImageInfoForTask() {
519  uint64_t imageList = GetDyldAllImageInfosPointer();
520
521  if (imageList) {
522    if (Is64Bit())
523      ReadImageInfo<MachO64>(*this, imageList);
524    else
525      ReadImageInfo<MachO32>(*this, imageList);
526  }
527}
528
529//==============================================================================
530DynamicImage  *DynamicImages::GetExecutableImage() {
531  int executable_index = GetExecutableImageIndex();
532
533  if (executable_index >= 0) {
534    return GetImage(executable_index);
535  }
536
537  return NULL;
538}
539
540//==============================================================================
541// returns -1 if failure to find executable
542int DynamicImages::GetExecutableImageIndex() {
543  int image_count = GetImageCount();
544
545  for (int i = 0; i < image_count; ++i) {
546    DynamicImage  *image = GetImage(i);
547    if (image->GetFileType() == MH_EXECUTE) {
548      return i;
549    }
550  }
551
552  return -1;
553}
554
555//==============================================================================
556// static
557cpu_type_t DynamicImages::DetermineTaskCPUType(task_t task) {
558  if (task == mach_task_self())
559    return GetNativeCPUType();
560
561  int mib[CTL_MAXNAME];
562  size_t mibLen = CTL_MAXNAME;
563  int err = sysctlnametomib("sysctl.proc_cputype", mib, &mibLen);
564  if (err == 0) {
565    assert(mibLen < CTL_MAXNAME);
566    pid_for_task(task, &mib[mibLen]);
567    mibLen += 1;
568
569    cpu_type_t cpu_type;
570    size_t cpuTypeSize = sizeof(cpu_type);
571    sysctl(mib, mibLen, &cpu_type, &cpuTypeSize, 0, 0);
572    return cpu_type;
573  }
574
575  return GetNativeCPUType();
576}
577
578}  // namespace google_breakpad