1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/tools/profiler/shared-libraries-macos.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,132 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include <AvailabilityMacros.h> 1.10 +#include <mach-o/loader.h> 1.11 +#include <mach-o/dyld_images.h> 1.12 +#include <mach/task_info.h> 1.13 +#include <mach/task.h> 1.14 +#include <mach/mach_init.h> 1.15 +#include <mach/mach_traps.h> 1.16 +#include <string.h> 1.17 +#include <stdlib.h> 1.18 +#include <vector> 1.19 +#include <sstream> 1.20 + 1.21 +#include "shared-libraries.h" 1.22 + 1.23 +#ifndef MAC_OS_X_VERSION_10_6 1.24 +#define MAC_OS_X_VERSION_10_6 1060 1.25 +#endif 1.26 + 1.27 +#if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6 1.28 +// borrowed from Breakpad 1.29 +// Fallback declarations for TASK_DYLD_INFO and friends, introduced in 1.30 +// <mach/task_info.h> in the Mac OS X 10.6 SDK. 1.31 +#define TASK_DYLD_INFO 17 1.32 +struct task_dyld_info { 1.33 + mach_vm_address_t all_image_info_addr; 1.34 + mach_vm_size_t all_image_info_size; 1.35 + }; 1.36 +typedef struct task_dyld_info task_dyld_info_data_t; 1.37 +typedef struct task_dyld_info *task_dyld_info_t; 1.38 +#define TASK_DYLD_INFO_COUNT (sizeof(task_dyld_info_data_t) / sizeof(natural_t)) 1.39 + 1.40 +#endif 1.41 + 1.42 +// Architecture specific abstraction. 1.43 +#ifdef __i386__ 1.44 +typedef mach_header platform_mach_header; 1.45 +typedef segment_command mach_segment_command_type; 1.46 +#define MACHO_MAGIC_NUMBER MH_MAGIC 1.47 +#define CMD_SEGMENT LC_SEGMENT 1.48 +#define seg_size uint32_t 1.49 +#else 1.50 +typedef mach_header_64 platform_mach_header; 1.51 +typedef segment_command_64 mach_segment_command_type; 1.52 +#define MACHO_MAGIC_NUMBER MH_MAGIC_64 1.53 +#define CMD_SEGMENT LC_SEGMENT_64 1.54 +#define seg_size uint64_t 1.55 +#endif 1.56 + 1.57 +static 1.58 +void addSharedLibrary(const platform_mach_header* header, char *name, SharedLibraryInfo &info) { 1.59 + const struct load_command *cmd = 1.60 + reinterpret_cast<const struct load_command *>(header + 1); 1.61 + 1.62 + seg_size size = 0; 1.63 + unsigned long long start = reinterpret_cast<unsigned long long>(header); 1.64 + // Find the cmd segment in the macho image. It will contain the offset we care about. 1.65 + const uint8_t *uuid_bytes = nullptr; 1.66 + for (unsigned int i = 0; 1.67 + cmd && (i < header->ncmds) && (uuid_bytes == nullptr || size == 0); 1.68 + ++i) { 1.69 + if (cmd->cmd == CMD_SEGMENT) { 1.70 + const mach_segment_command_type *seg = 1.71 + reinterpret_cast<const mach_segment_command_type *>(cmd); 1.72 + 1.73 + if (!strcmp(seg->segname, "__TEXT")) { 1.74 + size = seg->vmsize; 1.75 + } 1.76 + } else if (cmd->cmd == LC_UUID) { 1.77 + const uuid_command *ucmd = reinterpret_cast<const uuid_command *>(cmd); 1.78 + uuid_bytes = ucmd->uuid; 1.79 + } 1.80 + 1.81 + cmd = reinterpret_cast<const struct load_command *> 1.82 + (reinterpret_cast<const char *>(cmd) + cmd->cmdsize); 1.83 + } 1.84 + 1.85 + std::stringstream uuid; 1.86 + uuid << std::hex << std::uppercase; 1.87 + if (uuid_bytes != nullptr) { 1.88 + for (int i = 0; i < 16; ++i) { 1.89 + uuid << ((uuid_bytes[i] & 0xf0) >> 4); 1.90 + uuid << (uuid_bytes[i] & 0xf); 1.91 + } 1.92 + uuid << '0'; 1.93 + } 1.94 + 1.95 + info.AddSharedLibrary(SharedLibrary(start, start + size, 0, uuid.str(), 1.96 + name)); 1.97 +} 1.98 + 1.99 +// Use dyld to inspect the macho image information. We can build the SharedLibraryEntry structure 1.100 +// giving us roughtly the same info as /proc/PID/maps in Linux. 1.101 +SharedLibraryInfo SharedLibraryInfo::GetInfoForSelf() 1.102 +{ 1.103 + SharedLibraryInfo sharedLibraryInfo; 1.104 + 1.105 + task_dyld_info_data_t task_dyld_info; 1.106 + mach_msg_type_number_t count = TASK_DYLD_INFO_COUNT; 1.107 + if (task_info(mach_task_self (), TASK_DYLD_INFO, (task_info_t)&task_dyld_info, 1.108 + &count) != KERN_SUCCESS) { 1.109 + return sharedLibraryInfo; 1.110 + } 1.111 + 1.112 + struct dyld_all_image_infos* aii = (struct dyld_all_image_infos*)task_dyld_info.all_image_info_addr; 1.113 + size_t infoCount = aii->infoArrayCount; 1.114 + 1.115 + // Iterate through all dyld images (loaded libraries) to get their names 1.116 + // and offests. 1.117 + for (size_t i = 0; i < infoCount; ++i) { 1.118 + const dyld_image_info *info = &aii->infoArray[i]; 1.119 + 1.120 + // If the magic number doesn't match then go no further 1.121 + // since we're not pointing to where we think we are. 1.122 + if (info->imageLoadAddress->magic != MACHO_MAGIC_NUMBER) { 1.123 + continue; 1.124 + } 1.125 + 1.126 + const platform_mach_header* header = 1.127 + reinterpret_cast<const platform_mach_header*>(info->imageLoadAddress); 1.128 + 1.129 + // Add the entry for this image. 1.130 + addSharedLibrary(header, (char*)info->imageFilePath, sharedLibraryInfo); 1.131 + 1.132 + } 1.133 + return sharedLibraryInfo; 1.134 +} 1.135 +