michael@0: // Copyright (c) 2007, Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: #include "client/mac/handler/dynamic_images.h" michael@0: michael@0: extern "C" { // needed to compile on Leopard michael@0: #include michael@0: #include michael@0: #include michael@0: } michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "breakpad_nlist_64.h" michael@0: michael@0: #if !TARGET_OS_IPHONE michael@0: #include michael@0: michael@0: #ifndef MAC_OS_X_VERSION_10_6 michael@0: #define MAC_OS_X_VERSION_10_6 1060 michael@0: #endif michael@0: michael@0: #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6 michael@0: michael@0: // Fallback declarations for TASK_DYLD_INFO and friends, introduced in michael@0: // in the Mac OS X 10.6 SDK. michael@0: #define TASK_DYLD_INFO 17 michael@0: struct task_dyld_info { michael@0: mach_vm_address_t all_image_info_addr; michael@0: mach_vm_size_t all_image_info_size; michael@0: }; michael@0: typedef struct task_dyld_info task_dyld_info_data_t; michael@0: typedef struct task_dyld_info *task_dyld_info_t; michael@0: #define TASK_DYLD_INFO_COUNT (sizeof(task_dyld_info_data_t) / sizeof(natural_t)) michael@0: michael@0: #endif michael@0: michael@0: #endif // !TARGET_OS_IPHONE michael@0: michael@0: namespace google_breakpad { michael@0: michael@0: using std::string; michael@0: using std::vector; michael@0: michael@0: //============================================================================== michael@0: // Returns the size of the memory region containing |address| and the michael@0: // number of bytes from |address| to the end of the region. michael@0: // We potentially, will extend the size of the original michael@0: // region by the size of the following region if it's contiguous with the michael@0: // first in order to handle cases when we're reading strings and they michael@0: // straddle two vm regions. michael@0: // michael@0: static mach_vm_size_t GetMemoryRegionSize(task_port_t target_task, michael@0: const uint64_t address, michael@0: mach_vm_size_t *size_to_end) { michael@0: mach_vm_address_t region_base = (mach_vm_address_t)address; michael@0: mach_vm_size_t region_size; michael@0: natural_t nesting_level = 0; michael@0: vm_region_submap_info_64 submap_info; michael@0: mach_msg_type_number_t info_count = VM_REGION_SUBMAP_INFO_COUNT_64; michael@0: michael@0: // Get information about the vm region containing |address| michael@0: vm_region_recurse_info_t region_info; michael@0: region_info = reinterpret_cast(&submap_info); michael@0: michael@0: kern_return_t result = michael@0: mach_vm_region_recurse(target_task, michael@0: ®ion_base, michael@0: ®ion_size, michael@0: &nesting_level, michael@0: region_info, michael@0: &info_count); michael@0: michael@0: if (result == KERN_SUCCESS) { michael@0: // Get distance from |address| to the end of this region michael@0: *size_to_end = region_base + region_size -(mach_vm_address_t)address; michael@0: michael@0: // If we want to handle strings as long as 4096 characters we may need michael@0: // to check if there's a vm region immediately following the first one. michael@0: // If so, we need to extend |*size_to_end| to go all the way to the end michael@0: // of the second region. michael@0: if (*size_to_end < 4096) { michael@0: // Second region starts where the first one ends michael@0: mach_vm_address_t region_base2 = michael@0: (mach_vm_address_t)(region_base + region_size); michael@0: mach_vm_size_t region_size2; michael@0: michael@0: // Get information about the following vm region michael@0: result = michael@0: mach_vm_region_recurse(target_task, michael@0: ®ion_base2, michael@0: ®ion_size2, michael@0: &nesting_level, michael@0: region_info, michael@0: &info_count); michael@0: michael@0: // Extend region_size to go all the way to the end of the 2nd region michael@0: if (result == KERN_SUCCESS michael@0: && region_base2 == region_base + region_size) { michael@0: region_size += region_size2; michael@0: } michael@0: } michael@0: michael@0: *size_to_end = region_base + region_size -(mach_vm_address_t)address; michael@0: } else { michael@0: region_size = 0; michael@0: *size_to_end = 0; michael@0: } michael@0: michael@0: return region_size; michael@0: } michael@0: michael@0: #define kMaxStringLength 8192 michael@0: //============================================================================== michael@0: // Reads a NULL-terminated string from another task. michael@0: // michael@0: // Warning! This will not read any strings longer than kMaxStringLength-1 michael@0: // michael@0: static string ReadTaskString(task_port_t target_task, michael@0: const uint64_t address) { michael@0: // The problem is we don't know how much to read until we know how long michael@0: // the string is. And we don't know how long the string is, until we've read michael@0: // the memory! So, we'll try to read kMaxStringLength bytes michael@0: // (or as many bytes as we can until we reach the end of the vm region). michael@0: mach_vm_size_t size_to_end; michael@0: GetMemoryRegionSize(target_task, address, &size_to_end); michael@0: michael@0: if (size_to_end > 0) { michael@0: mach_vm_size_t size_to_read = michael@0: size_to_end > kMaxStringLength ? kMaxStringLength : size_to_end; michael@0: michael@0: vector bytes; michael@0: if (ReadTaskMemory(target_task, address, (size_t)size_to_read, bytes) != michael@0: KERN_SUCCESS) michael@0: return string(); michael@0: michael@0: return string(reinterpret_cast(&bytes[0])); michael@0: } michael@0: michael@0: return string(); michael@0: } michael@0: michael@0: //============================================================================== michael@0: // Reads an address range from another task. The bytes read will be returned michael@0: // in bytes, which will be resized as necessary. michael@0: kern_return_t ReadTaskMemory(task_port_t target_task, michael@0: const uint64_t address, michael@0: size_t length, michael@0: vector &bytes) { michael@0: int systemPageSize = getpagesize(); michael@0: michael@0: // use the negative of the page size for the mask to find the page address michael@0: mach_vm_address_t page_address = address & (-systemPageSize); michael@0: michael@0: mach_vm_address_t last_page_address = michael@0: (address + length + (systemPageSize - 1)) & (-systemPageSize); michael@0: michael@0: mach_vm_size_t page_size = last_page_address - page_address; michael@0: uint8_t* local_start; michael@0: uint32_t local_length; michael@0: michael@0: kern_return_t r = mach_vm_read(target_task, michael@0: page_address, michael@0: page_size, michael@0: reinterpret_cast(&local_start), michael@0: &local_length); michael@0: michael@0: if (r != KERN_SUCCESS) michael@0: return r; michael@0: michael@0: bytes.resize(length); michael@0: memcpy(&bytes[0], michael@0: &local_start[(mach_vm_address_t)address - page_address], michael@0: length); michael@0: mach_vm_deallocate(mach_task_self(), (uintptr_t)local_start, local_length); michael@0: return KERN_SUCCESS; michael@0: } michael@0: michael@0: #pragma mark - michael@0: michael@0: //============================================================================== michael@0: // Traits structs for specializing function templates to handle michael@0: // 32-bit/64-bit Mach-O files. michael@0: struct MachO32 { michael@0: typedef mach_header mach_header_type; michael@0: typedef segment_command mach_segment_command_type; michael@0: typedef dyld_image_info32 dyld_image_info; michael@0: typedef dyld_all_image_infos32 dyld_all_image_infos; michael@0: typedef struct nlist nlist_type; michael@0: static const uint32_t magic = MH_MAGIC; michael@0: static const uint32_t segment_load_command = LC_SEGMENT; michael@0: }; michael@0: michael@0: struct MachO64 { michael@0: typedef mach_header_64 mach_header_type; michael@0: typedef segment_command_64 mach_segment_command_type; michael@0: typedef dyld_image_info64 dyld_image_info; michael@0: typedef dyld_all_image_infos64 dyld_all_image_infos; michael@0: typedef struct nlist_64 nlist_type; michael@0: static const uint32_t magic = MH_MAGIC_64; michael@0: static const uint32_t segment_load_command = LC_SEGMENT_64; michael@0: }; michael@0: michael@0: template michael@0: bool FindTextSection(DynamicImage& image) { michael@0: typedef typename MachBits::mach_header_type mach_header_type; michael@0: typedef typename MachBits::mach_segment_command_type michael@0: mach_segment_command_type; michael@0: michael@0: const mach_header_type* header = michael@0: reinterpret_cast(&image.header_[0]); michael@0: michael@0: if(header->magic != MachBits::magic) { michael@0: return false; michael@0: } michael@0: michael@0: const struct load_command *cmd = michael@0: reinterpret_cast(header + 1); michael@0: michael@0: bool found_text_section = false; michael@0: bool found_dylib_id_command = false; michael@0: for (unsigned int i = 0; cmd && (i < header->ncmds); ++i) { michael@0: if (!found_text_section) { michael@0: if (cmd->cmd == MachBits::segment_load_command) { michael@0: const mach_segment_command_type *seg = michael@0: reinterpret_cast(cmd); michael@0: michael@0: if (!strcmp(seg->segname, "__TEXT")) { michael@0: image.vmaddr_ = static_cast(seg->vmaddr); michael@0: image.vmsize_ = static_cast(seg->vmsize); michael@0: image.slide_ = 0; michael@0: michael@0: if (seg->fileoff == 0 && seg->filesize != 0) { michael@0: image.slide_ = michael@0: (uintptr_t)image.GetLoadAddress() - (uintptr_t)seg->vmaddr; michael@0: } michael@0: found_text_section = true; michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (!found_dylib_id_command) { michael@0: if (cmd->cmd == LC_ID_DYLIB) { michael@0: const struct dylib_command *dc = michael@0: reinterpret_cast(cmd); michael@0: michael@0: image.version_ = dc->dylib.current_version; michael@0: found_dylib_id_command = true; michael@0: } michael@0: } michael@0: michael@0: if (found_dylib_id_command && found_text_section) { michael@0: return true; michael@0: } michael@0: michael@0: cmd = reinterpret_cast michael@0: (reinterpret_cast(cmd) + cmd->cmdsize); michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: //============================================================================== michael@0: // Initializes vmaddr_, vmsize_, and slide_ michael@0: void DynamicImage::CalculateMemoryAndVersionInfo() { michael@0: // unless we can process the header, ensure that calls to michael@0: // IsValid() will return false michael@0: vmaddr_ = 0; michael@0: vmsize_ = 0; michael@0: slide_ = 0; michael@0: version_ = 0; michael@0: michael@0: // The function template above does all the real work. michael@0: if (Is64Bit()) michael@0: FindTextSection(*this); michael@0: else michael@0: FindTextSection(*this); michael@0: } michael@0: michael@0: //============================================================================== michael@0: // The helper function template abstracts the 32/64-bit differences. michael@0: template michael@0: uint32_t GetFileTypeFromHeader(DynamicImage& image) { michael@0: typedef typename MachBits::mach_header_type mach_header_type; michael@0: michael@0: const mach_header_type* header = michael@0: reinterpret_cast(&image.header_[0]); michael@0: return header->filetype; michael@0: } michael@0: michael@0: uint32_t DynamicImage::GetFileType() { michael@0: if (Is64Bit()) michael@0: return GetFileTypeFromHeader(*this); michael@0: michael@0: return GetFileTypeFromHeader(*this); michael@0: } michael@0: michael@0: #pragma mark - michael@0: michael@0: //============================================================================== michael@0: // Loads information about dynamically loaded code in the given task. michael@0: DynamicImages::DynamicImages(mach_port_t task) michael@0: : task_(task), michael@0: cpu_type_(DetermineTaskCPUType(task)), michael@0: image_list_() { michael@0: ReadImageInfoForTask(); michael@0: } michael@0: michael@0: template michael@0: static uint64_t LookupSymbol(const char* symbol_name, michael@0: const char* filename, michael@0: cpu_type_t cpu_type) { michael@0: typedef typename MachBits::nlist_type nlist_type; michael@0: michael@0: nlist_type symbol_info[8] = {}; michael@0: const char *symbolNames[2] = { symbol_name, "\0" }; michael@0: nlist_type &list = symbol_info[0]; michael@0: int invalidEntriesCount = breakpad_nlist(filename, michael@0: &list, michael@0: symbolNames, michael@0: cpu_type); michael@0: michael@0: if(invalidEntriesCount != 0) { michael@0: return 0; michael@0: } michael@0: michael@0: assert(list.n_value); michael@0: return list.n_value; michael@0: } michael@0: michael@0: #if TARGET_OS_IPHONE michael@0: static bool HasTaskDyldInfo() { michael@0: return true; michael@0: } michael@0: #else michael@0: static SInt32 GetOSVersionInternal() { michael@0: SInt32 os_version = 0; michael@0: Gestalt(gestaltSystemVersion, &os_version); michael@0: return os_version; michael@0: } michael@0: michael@0: static SInt32 GetOSVersion() { michael@0: static SInt32 os_version = GetOSVersionInternal(); michael@0: return os_version; michael@0: } michael@0: michael@0: static bool HasTaskDyldInfo() { michael@0: #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_6 michael@0: return true; michael@0: #else michael@0: return GetOSVersion() >= 0x1060; michael@0: #endif michael@0: } michael@0: #endif // TARGET_OS_IPHONE michael@0: michael@0: uint64_t DynamicImages::GetDyldAllImageInfosPointer() { michael@0: if (HasTaskDyldInfo()) { michael@0: task_dyld_info_data_t task_dyld_info; michael@0: mach_msg_type_number_t count = TASK_DYLD_INFO_COUNT; michael@0: if (task_info(task_, TASK_DYLD_INFO, (task_info_t)&task_dyld_info, michael@0: &count) != KERN_SUCCESS) { michael@0: return 0; michael@0: } michael@0: michael@0: return (uint64_t)task_dyld_info.all_image_info_addr; michael@0: } else { michael@0: const char *imageSymbolName = "_dyld_all_image_infos"; michael@0: const char *dyldPath = "/usr/lib/dyld"; michael@0: michael@0: if (Is64Bit()) michael@0: return LookupSymbol(imageSymbolName, dyldPath, cpu_type_); michael@0: return LookupSymbol(imageSymbolName, dyldPath, cpu_type_); michael@0: } michael@0: } michael@0: michael@0: //============================================================================== michael@0: // This code was written using dyld_debug.c (from Darwin) as a guide. michael@0: michael@0: template michael@0: void ReadImageInfo(DynamicImages& images, michael@0: uint64_t image_list_address) { michael@0: typedef typename MachBits::dyld_image_info dyld_image_info; michael@0: typedef typename MachBits::dyld_all_image_infos dyld_all_image_infos; michael@0: typedef typename MachBits::mach_header_type mach_header_type; michael@0: michael@0: // Read the structure inside of dyld that contains information about michael@0: // loaded images. We're reading from the desired task's address space. michael@0: michael@0: // Here we make the assumption that dyld loaded at the same address in michael@0: // the crashed process vs. this one. This is an assumption made in michael@0: // "dyld_debug.c" and is said to be nearly always valid. michael@0: vector dyld_all_info_bytes; michael@0: if (ReadTaskMemory(images.task_, michael@0: image_list_address, michael@0: sizeof(dyld_all_image_infos), michael@0: dyld_all_info_bytes) != KERN_SUCCESS) michael@0: return; michael@0: michael@0: dyld_all_image_infos *dyldInfo = michael@0: reinterpret_cast(&dyld_all_info_bytes[0]); michael@0: michael@0: // number of loaded images michael@0: int count = dyldInfo->infoArrayCount; michael@0: michael@0: // Read an array of dyld_image_info structures each containing michael@0: // information about a loaded image. michael@0: vector dyld_info_array_bytes; michael@0: if (ReadTaskMemory(images.task_, michael@0: dyldInfo->infoArray, michael@0: count * sizeof(dyld_image_info), michael@0: dyld_info_array_bytes) != KERN_SUCCESS) michael@0: return; michael@0: michael@0: dyld_image_info *infoArray = michael@0: reinterpret_cast(&dyld_info_array_bytes[0]); michael@0: images.image_list_.reserve(count); michael@0: michael@0: for (int i = 0; i < count; ++i) { michael@0: dyld_image_info &info = infoArray[i]; michael@0: michael@0: // First read just the mach_header from the image in the task. michael@0: vector mach_header_bytes; michael@0: if (ReadTaskMemory(images.task_, michael@0: info.load_address_, michael@0: sizeof(mach_header_type), michael@0: mach_header_bytes) != KERN_SUCCESS) michael@0: continue; // bail on this dynamic image michael@0: michael@0: mach_header_type *header = michael@0: reinterpret_cast(&mach_header_bytes[0]); michael@0: michael@0: // Now determine the total amount necessary to read the header michael@0: // plus all of the load commands. michael@0: size_t header_size = michael@0: sizeof(mach_header_type) + header->sizeofcmds; michael@0: michael@0: if (ReadTaskMemory(images.task_, michael@0: info.load_address_, michael@0: header_size, michael@0: mach_header_bytes) != KERN_SUCCESS) michael@0: continue; michael@0: michael@0: // Read the file name from the task's memory space. michael@0: string file_path; michael@0: if (info.file_path_) { michael@0: // Although we're reading kMaxStringLength bytes, it's copied in the michael@0: // the DynamicImage constructor below with the correct string length, michael@0: // so it's not really wasting memory. michael@0: file_path = ReadTaskString(images.task_, info.file_path_); michael@0: } michael@0: michael@0: // Create an object representing this image and add it to our list. michael@0: DynamicImage *new_image; michael@0: new_image = new DynamicImage(&mach_header_bytes[0], michael@0: header_size, michael@0: info.load_address_, michael@0: file_path, michael@0: static_cast(info.file_mod_date_), michael@0: images.task_, michael@0: images.cpu_type_); michael@0: michael@0: if (new_image->IsValid()) { michael@0: images.image_list_.push_back(DynamicImageRef(new_image)); michael@0: } else { michael@0: delete new_image; michael@0: } michael@0: } michael@0: michael@0: // sorts based on loading address michael@0: sort(images.image_list_.begin(), images.image_list_.end()); michael@0: // remove duplicates - this happens in certain strange cases michael@0: // You can see it in DashboardClient when Google Gadgets plugin michael@0: // is installed. Apple's crash reporter log and gdb "info shared" michael@0: // both show the same library multiple times at the same address michael@0: michael@0: vector::iterator it = unique(images.image_list_.begin(), michael@0: images.image_list_.end()); michael@0: images.image_list_.erase(it, images.image_list_.end()); michael@0: } michael@0: michael@0: void DynamicImages::ReadImageInfoForTask() { michael@0: uint64_t imageList = GetDyldAllImageInfosPointer(); michael@0: michael@0: if (imageList) { michael@0: if (Is64Bit()) michael@0: ReadImageInfo(*this, imageList); michael@0: else michael@0: ReadImageInfo(*this, imageList); michael@0: } michael@0: } michael@0: michael@0: //============================================================================== michael@0: DynamicImage *DynamicImages::GetExecutableImage() { michael@0: int executable_index = GetExecutableImageIndex(); michael@0: michael@0: if (executable_index >= 0) { michael@0: return GetImage(executable_index); michael@0: } michael@0: michael@0: return NULL; michael@0: } michael@0: michael@0: //============================================================================== michael@0: // returns -1 if failure to find executable michael@0: int DynamicImages::GetExecutableImageIndex() { michael@0: int image_count = GetImageCount(); michael@0: michael@0: for (int i = 0; i < image_count; ++i) { michael@0: DynamicImage *image = GetImage(i); michael@0: if (image->GetFileType() == MH_EXECUTE) { michael@0: return i; michael@0: } michael@0: } michael@0: michael@0: return -1; michael@0: } michael@0: michael@0: //============================================================================== michael@0: // static michael@0: cpu_type_t DynamicImages::DetermineTaskCPUType(task_t task) { michael@0: if (task == mach_task_self()) michael@0: return GetNativeCPUType(); michael@0: michael@0: int mib[CTL_MAXNAME]; michael@0: size_t mibLen = CTL_MAXNAME; michael@0: int err = sysctlnametomib("sysctl.proc_cputype", mib, &mibLen); michael@0: if (err == 0) { michael@0: assert(mibLen < CTL_MAXNAME); michael@0: pid_for_task(task, &mib[mibLen]); michael@0: mibLen += 1; michael@0: michael@0: cpu_type_t cpu_type; michael@0: size_t cpuTypeSize = sizeof(cpu_type); michael@0: sysctl(mib, mibLen, &cpu_type, &cpuTypeSize, 0, 0); michael@0: return cpu_type; michael@0: } michael@0: michael@0: return GetNativeCPUType(); michael@0: } michael@0: michael@0: } // namespace google_breakpad