diff options
author | ladderbreaker <ladderbreaker@4c0a9323-5329-0410-9bdc-e9ce6186880e> | 2007-05-02 21:04:57 +0000 |
---|---|---|
committer | ladderbreaker <ladderbreaker@4c0a9323-5329-0410-9bdc-e9ce6186880e> | 2007-05-02 21:04:57 +0000 |
commit | c455a76c03de07b31220af13138fa9187244fdfd (patch) | |
tree | ee899b3957bde076d4b75201e595b7b7199d1c3d /src/client/mac/handler | |
parent | Merge Ted's patch for writing symbols to stdout for linux. (diff) | |
download | breakpad-c455a76c03de07b31220af13138fa9187244fdfd.tar.xz |
Issue 159: reviewer Waylonis
git-svn-id: http://google-breakpad.googlecode.com/svn/trunk@150 4c0a9323-5329-0410-9bdc-e9ce6186880e
Diffstat (limited to 'src/client/mac/handler')
-rw-r--r-- | src/client/mac/handler/dynamic_images.cc | 239 | ||||
-rw-r--r-- | src/client/mac/handler/dynamic_images.h | 301 |
2 files changed, 540 insertions, 0 deletions
diff --git a/src/client/mac/handler/dynamic_images.cc b/src/client/mac/handler/dynamic_images.cc new file mode 100644 index 00000000..06ef6dd3 --- /dev/null +++ b/src/client/mac/handler/dynamic_images.cc @@ -0,0 +1,239 @@ +// Copyright (c) 2007, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include <mach-o/nlist.h> +#include <stdlib.h> +#include <stdio.h> +#include <algorithm> + +#include "client/mac/handler/dynamic_images.h" + +namespace google_breakpad { + +//============================================================================== +// Reads an address range from another task. A block of memory is malloced +// and should be freed by the caller. +void* ReadTaskMemory(task_port_t target_task, + const void* address, + size_t length) { + void* result = NULL; + mach_vm_address_t page_address = (uint32_t)address & (-4096); + mach_vm_address_t last_page_address = + ((uint32_t)address + length + 4095) & (-4096); + mach_vm_size_t page_size = last_page_address - page_address; + uint8_t* local_start; + uint32_t local_length; + + kern_return_t r = vm_read(target_task, + page_address, + page_size, + reinterpret_cast<vm_offset_t*>(&local_start), + &local_length); + + if (r == KERN_SUCCESS) { + result = malloc(length); + if (result != NULL) { + memcpy(result, &local_start[(uint32_t)address - page_address], length); + } + vm_deallocate(mach_task_self(), (uintptr_t)local_start, local_length); + } + + return result; +} + +#pragma mark - + +//============================================================================== +// Initializes vmaddr_, vmsize_, and slide_ +void DynamicImage::CalculateMemoryInfo() { + mach_header *header = GetMachHeader(); + + const struct load_command *cmd = + reinterpret_cast<const struct load_command *>(header + 1); + + for (unsigned int i = 0; cmd && (i < header->ncmds); ++i) { + if (cmd->cmd == LC_SEGMENT) { + const struct segment_command *seg = + reinterpret_cast<const struct segment_command *>(cmd); + + if (!strcmp(seg->segname, "__TEXT")) { + vmaddr_ = seg->vmaddr; + vmsize_ = seg->vmsize; + slide_ = 0; + + if (seg->fileoff == 0 && seg->filesize != 0) { + slide_ = (uintptr_t)GetLoadAddress() - (uintptr_t)seg->vmaddr; + } + return; + } + } + + cmd = reinterpret_cast<const struct load_command *> + (reinterpret_cast<const char *>(cmd) + cmd->cmdsize); + } + + // we failed - a call to IsValid() will return false + vmaddr_ = 0; + vmsize_ = 0; + slide_ = 0; +} + +#pragma mark - + +//============================================================================== +// Loads information about dynamically loaded code in the given task. +DynamicImages::DynamicImages(mach_port_t task) + : task_(task) { + ReadImageInfoForTask(); +} + +//============================================================================== +// This code was written using dyld_debug.c (from Darwin) as a guide. +void DynamicImages::ReadImageInfoForTask() { + struct nlist l[8]; + memset(l, 0, sizeof(l) ); + + // First we lookup the address of the "_dyld_all_image_infos" struct + // which lives in "dyld". This structure contains information about all + // of the loaded dynamic images. + struct nlist &list = l[0]; + list.n_un.n_name = "_dyld_all_image_infos"; + nlist("/usr/lib/dyld", &list); + + if (list.n_value) { + // Read the structure inside of dyld that contains information about + // loaded images. We're reading from the desired task's address space. + + // Here we make the assumption that dyld loaded at the same address in + // the crashed process vs. this one. This is an assumption made in + // "dyld_debug.c" and is said to be nearly always valid. + dyld_all_image_infos *dyldInfo = reinterpret_cast<dyld_all_image_infos*> + (ReadTaskMemory(task_, + reinterpret_cast<void*>(list.n_value), + sizeof(dyld_all_image_infos))); + + if (dyldInfo) { + // number of loaded images + int count = dyldInfo->infoArrayCount; + + // Read an array of dyld_image_info structures each containing + // information about a loaded image. + dyld_image_info *infoArray = reinterpret_cast<dyld_image_info*> + (ReadTaskMemory(task_, + dyldInfo->infoArray, + count*sizeof(dyld_image_info))); + + image_list_.reserve(count); + + for (int i = 0; i < count; ++i) { + dyld_image_info &info = infoArray[i]; + + // First read just the mach_header from the image in the task. + mach_header *header = reinterpret_cast<mach_header*> + (ReadTaskMemory(task_, info.load_address_, sizeof(mach_header))); + + if (!header) + break; // bail on this dynamic image + + // Now determine the total amount we really want to read based on the + // size of the load commands. We need the header plus all of the + // load commands. + unsigned int header_size = sizeof(mach_header) + header->sizeofcmds; + free(header); + + header = reinterpret_cast<mach_header*> + (ReadTaskMemory(task_, info.load_address_, header_size)); + + // Read the file name from the task's memory space. + char *file_path = NULL; + if (info.file_path_) { + // Although we're reading 0x2000 bytes, this is copied in the + // the DynamicImage constructor below with the correct string length, + // so it's not really wasting memory. + file_path = reinterpret_cast<char*> + (ReadTaskMemory(task_, + info.file_path_, + 0x2000)); + } + + // Create an object representing this image and add it to our list. + DynamicImage *new_image = new DynamicImage(header, + header_size, + info.load_address_, + file_path, + info.file_mod_date_, + task_); + + if (new_image->IsValid()) { + image_list_.push_back(DynamicImageRef(new_image)); + } else { + delete new_image; + } + + if (file_path) { + free(file_path); + } + } + + free(dyldInfo); + free(infoArray); + + // sorts based on loading address + sort(image_list_.begin(), image_list_.end() ); + } + } +} + +//============================================================================== +DynamicImage *DynamicImages::GetExecutableImage() { + int executable_index = GetExecutableImageIndex(); + + if (executable_index >= 0) { + return GetImage(executable_index); + } + + return NULL; +} + +//============================================================================== +// returns -1 if failure to find executable +int DynamicImages::GetExecutableImageIndex() { + int image_count = GetImageCount(); + + for (int i = 0; i < image_count; ++i) { + DynamicImage *image = GetImage(i); + if (image->GetMachHeader()->filetype == MH_EXECUTE) { + return i; + } + } + + return -1; +} + +} // namespace google_breakpad diff --git a/src/client/mac/handler/dynamic_images.h b/src/client/mac/handler/dynamic_images.h new file mode 100644 index 00000000..545ae6cc --- /dev/null +++ b/src/client/mac/handler/dynamic_images.h @@ -0,0 +1,301 @@ +// Copyright (c) 2007, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// dynamic_images.h +// +// Implements most of the function of the dyld API, but allowing an +// arbitrary task to be introspected, unlike the dyld API which +// only allows operation on the current task. The current implementation +// is limited to use by 32-bit tasks. + +#ifndef CLIENT_MAC_HANDLER_DYNAMIC_IMAGES_H__ +#define CLIENT_MAC_HANDLER_DYNAMIC_IMAGES_H__ + +#include <mach/mach.h> +#include <mach-o/dyld.h> +#include <mach-o/loader.h> +#include <sys/types.h> +#include <vector> + +namespace google_breakpad { + +using std::vector; + +//============================================================================== +// The memory layout of this struct matches the dyld_image_info struct +// defined in "dyld_gdb.h" in the darwin source. +typedef struct dyld_image_info { + struct mach_header *load_address_; + char *file_path_; + uintptr_t file_mod_date_; +} dyld_image_info; + +//============================================================================== +// This is as defined in "dyld_gdb.h" in the darwin source. +// _dyld_all_image_infos (in dyld) is a structure of this type +// which will be used to determine which dynamic code has been loaded. +typedef struct dyld_all_image_infos { + uint32_t version; // == 1 in Mac OS X 10.4 + uint32_t infoArrayCount; + const struct dyld_image_info *infoArray; + void* notification; + bool processDetachedFromSharedRegion; +} dyld_all_image_infos; + +//============================================================================== +// A simple wrapper for a mach_header +// +// This could be fleshed out with some more interesting methods. +class MachHeader { + public: + explicit MachHeader(const mach_header &header) : header_(header) {} + + void Print() { + printf("magic\t\t: %4x\n", header_.magic); + printf("cputype\t\t: %d\n", header_.cputype); + printf("cpusubtype\t: %d\n", header_.cpusubtype); + printf("filetype\t: %d\n", header_.filetype); + printf("ncmds\t\t: %d\n", header_.ncmds); + printf("sizeofcmds\t: %d\n", header_.sizeofcmds); + printf("flags\t\t: %d\n", header_.flags); + } + + mach_header header_; +}; + +//============================================================================== +// Represents a single dynamically loaded mach-o image +class DynamicImage { + public: + DynamicImage(mach_header *header, // we take ownership + int header_size, // includes load commands + mach_header *load_address, + char *inFilePath, + uintptr_t image_mod_date, + mach_port_t task) + : header_(header), + header_size_(header_size), + load_address_(load_address), + file_mod_date_(image_mod_date), + task_(task) { + InitializeFilePath(inFilePath); + CalculateMemoryInfo(); + } + + ~DynamicImage() { + if (file_path_) { + free(file_path_); + } + free(header_); + } + + // Returns pointer to a local copy of the mach_header plus load commands + mach_header *GetMachHeader() {return header_;} + + // Size of mach_header plus load commands + int GetHeaderSize() const {return header_size_;} + + // Full path to mach-o binary + char *GetFilePath() {return file_path_;} + + uintptr_t GetModDate() const {return file_mod_date_;} + + // Actual address where the image was loaded + mach_header *GetLoadAddress() const {return load_address_;} + + // Address where the image should be loaded + uint32_t GetVMAddr() const {return vmaddr_;} + + // Difference between GetLoadAddress() and GetVMAddr() + ptrdiff_t GetVMAddrSlide() const {return slide_;} + + // Size of the image + uint32_t GetVMSize() const {return vmsize_;} + + // Task owning this loaded image + mach_port_t GetTask() {return task_;} + + // For sorting + bool operator<(const DynamicImage &inInfo) { + return GetLoadAddress() < inInfo.GetLoadAddress(); + } + + // Debugging + void Print() { + char *path = GetFilePath(); + if (!path) { + path = "(unknown)"; + } + printf("%p: %s\n", GetLoadAddress(), path); + MachHeader(*GetMachHeader()).Print(); + printf("vmaddr\t\t: %p\n", reinterpret_cast<void*>(GetVMAddr())); + printf("vmsize\t\t: %d\n", GetVMSize()); + printf("slide\t\t: %d\n", GetVMAddrSlide()); + } + + private: + friend class DynamicImages; + + // Sanity checking + bool IsValid() {return GetVMAddr() != 0;} + + // Makes local copy of file path to mach-o binary + void InitializeFilePath(char *inFilePath) { + if (inFilePath) { + file_path_ = reinterpret_cast<char*>(malloc(strlen(inFilePath))); + strcpy(file_path_, inFilePath); + } else { + file_path_ = NULL; + } + } + + // Initializes vmaddr_, vmsize_, and slide_ + void CalculateMemoryInfo(); + +#if 0 // currently not needed + // Copy constructor: we don't want this to be invoked, + // but here's the code in case we need to make it public some day. + DynamicImage(DynamicImage &inInfo) + : load_address_(inInfo.load_address_), + vmaddr_(inInfo.vmaddr_), + vmsize_(inInfo.vmsize_), + slide_(inInfo.slide_), + file_mod_date_(inInfo.file_mod_date_), + task_(inInfo.task_) { + // copy file path string + InitializeFilePath(inInfo.GetFilePath()); + + // copy mach_header and load commands + header_ = reinterpret_cast<mach_header*>(malloc(inInfo.header_size_)); + memcpy(header_, inInfo.header_, inInfo.header_size_); + header_size_ = inInfo.header_size_; + } +#endif + + mach_header *header_; // our local copy of the header + int header_size_; // mach_header plus load commands + mach_header *load_address_; // base address image is mapped into + uint32_t vmaddr_; + uint32_t vmsize_; + ptrdiff_t slide_; + + char *file_path_; // path dyld used to load the image + uintptr_t file_mod_date_; // time_t of image file + + mach_port_t task_; +}; + +//============================================================================== +// DynamicImageRef is just a simple wrapper for a pointer to +// DynamicImage. The reason we use it instead of a simple typedef is so +// that we can use stl::sort() on a vector of DynamicImageRefs +// and simple class pointers can't implement operator<(). +// +class DynamicImageRef { + public: + explicit DynamicImageRef(DynamicImage *inP) : p(inP) {} + DynamicImageRef(const DynamicImageRef &inRef) : p(inRef.p) {} // STL required + + bool operator<(const DynamicImageRef &inRef) const { + return (*const_cast<DynamicImageRef*>(this)->p) + < (*const_cast<DynamicImageRef&>(inRef).p); + } + + // Be just like DynamicImage* + DynamicImage *operator->() {return p;} + operator DynamicImage*() {return p;} + + private: + DynamicImage *p; +}; + +//============================================================================== +// An object of type DynamicImages may be created to allow introspection of +// an arbitrary task's dynamically loaded mach-o binaries. This makes the +// assumption that the current task has send rights to the target task. +class DynamicImages { + public: + explicit DynamicImages(mach_port_t task); + + ~DynamicImages() { + for (int i = 0; i < (int)image_list_.size(); ++i) { + delete image_list_[i]; + } + } + + // Returns the number of dynamically loaded mach-o images. + int GetImageCount() const {return image_list_.size();} + + // Returns an individual image. + DynamicImage *GetImage(int i) { + if (i < (int)image_list_.size()) { + return image_list_[i]; + } + return NULL; + } + + // Returns the image corresponding to the main executable. + DynamicImage *GetExecutableImage(); + int GetExecutableImageIndex(); + + // Returns the task which we're looking at. + mach_port_t GetTask() const {return task_;} + + // Debugging + void Print() { + for (int i = 0; i < (int)image_list_.size(); ++i) { + image_list_[i]->Print(); + } + } + + void TestPrint() { + for (int i = 0; i < (int)image_list_.size(); ++i) { + printf("dyld: %p: name = %s\n", _dyld_get_image_header(i), + _dyld_get_image_name(i) ); + MachHeader(*_dyld_get_image_header(i)).Print(); + } + } + + private: + bool IsOurTask() {return task_ == mach_task_self();} + + // Initialization + void ReadImageInfoForTask(); + + mach_port_t task_; + vector<DynamicImageRef> image_list_; +}; + +// Returns a malloced block containing the contents of memory at a particular +// location in another task. +void* ReadTaskMemory(task_port_t target_task, const void* address, size_t len); + +} // namespace google_breakpad + +#endif // CLIENT_MAC_HANDLER_DYNAMIC_IMAGES_H__ |