1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/tools/profiler/shared-libraries-linux.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,124 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include "shared-libraries.h" 1.9 + 1.10 +#define PATH_MAX_TOSTRING(x) #x 1.11 +#define PATH_MAX_STRING(x) PATH_MAX_TOSTRING(x) 1.12 +#include <stdlib.h> 1.13 +#include <stdio.h> 1.14 +#include <string.h> 1.15 +#include <limits.h> 1.16 +#include <unistd.h> 1.17 +#include <fstream> 1.18 +#include "platform.h" 1.19 +#include "shared-libraries.h" 1.20 + 1.21 +#if !defined(MOZ_WIDGET_GONK) 1.22 +// TODO fix me with proper include 1.23 +#include "nsDebug.h" 1.24 +#ifdef ANDROID 1.25 +#include "ElfLoader.h" // dl_phdr_info 1.26 +#else 1.27 +#include <link.h> // dl_phdr_info 1.28 +#endif 1.29 +#include <features.h> 1.30 +#include <dlfcn.h> 1.31 +#include <sys/types.h> 1.32 + 1.33 +#ifdef ANDROID 1.34 +extern "C" __attribute__((weak)) 1.35 +int dl_iterate_phdr( 1.36 + int (*callback) (struct dl_phdr_info *info, 1.37 + size_t size, void *data), 1.38 + void *data); 1.39 +#endif 1.40 + 1.41 +int dl_iterate_callback(struct dl_phdr_info *dl_info, size_t size, void *data) 1.42 +{ 1.43 + SharedLibraryInfo& info = *reinterpret_cast<SharedLibraryInfo*>(data); 1.44 + 1.45 + if (dl_info->dlpi_phnum <= 0) 1.46 + return 0; 1.47 + 1.48 + unsigned long libStart = -1; 1.49 + unsigned long libEnd = 0; 1.50 + 1.51 + for (size_t i = 0; i < dl_info->dlpi_phnum; i++) { 1.52 + if (dl_info->dlpi_phdr[i].p_type != PT_LOAD) { 1.53 + continue; 1.54 + } 1.55 + unsigned long start = dl_info->dlpi_addr + dl_info->dlpi_phdr[i].p_vaddr; 1.56 + unsigned long end = start + dl_info->dlpi_phdr[i].p_memsz; 1.57 + if (start < libStart) 1.58 + libStart = start; 1.59 + if (end > libEnd) 1.60 + libEnd = end; 1.61 + } 1.62 + SharedLibrary shlib(libStart, libEnd, 0, "", dl_info->dlpi_name); 1.63 + info.AddSharedLibrary(shlib); 1.64 + 1.65 + return 0; 1.66 +} 1.67 +#endif 1.68 + 1.69 +SharedLibraryInfo SharedLibraryInfo::GetInfoForSelf() 1.70 +{ 1.71 + SharedLibraryInfo info; 1.72 + 1.73 +#if !defined(MOZ_WIDGET_GONK) 1.74 + dl_iterate_phdr(dl_iterate_callback, &info); 1.75 +#ifndef ANDROID 1.76 + return info; 1.77 +#endif 1.78 +#endif 1.79 + 1.80 + pid_t pid = getpid(); 1.81 + char path[PATH_MAX]; 1.82 + snprintf(path, PATH_MAX, "/proc/%d/maps", pid); 1.83 + std::ifstream maps(path); 1.84 + std::string line; 1.85 + int count = 0; 1.86 + while (std::getline(maps, line)) { 1.87 + int ret; 1.88 + //XXX: needs input sanitizing 1.89 + unsigned long start; 1.90 + unsigned long end; 1.91 + char perm[6] = ""; 1.92 + unsigned long offset; 1.93 + char name[PATH_MAX] = ""; 1.94 + ret = sscanf(line.c_str(), 1.95 + "%lx-%lx %6s %lx %*s %*x %" PATH_MAX_STRING(PATH_MAX) "s\n", 1.96 + &start, &end, perm, &offset, name); 1.97 + if (!strchr(perm, 'x')) { 1.98 + // Ignore non executable entries 1.99 + continue; 1.100 + } 1.101 + if (ret != 5 && ret != 4) { 1.102 + LOG("Get maps line failed"); 1.103 + continue; 1.104 + } 1.105 +#if defined(ANDROID) && !defined(MOZ_WIDGET_GONK) 1.106 + // Use proc/pid/maps to get the dalvik-jit section since it has 1.107 + // no associated phdrs 1.108 + if (strcmp(name, "/dev/ashmem/dalvik-jit-code-cache") != 0) 1.109 + continue; 1.110 +#else 1.111 + if (strcmp(perm, "r-xp") != 0) { 1.112 + // Ignore entries that are writable and/or shared. 1.113 + // At least one graphics driver uses short-lived "rwxs" mappings 1.114 + // (see bug 926734 comment 5), so just checking for 'x' isn't enough. 1.115 + continue; 1.116 + } 1.117 +#endif 1.118 + SharedLibrary shlib(start, end, offset, "", name); 1.119 + info.AddSharedLibrary(shlib); 1.120 + if (count > 10000) { 1.121 + LOG("Get maps failed"); 1.122 + break; 1.123 + } 1.124 + count++; 1.125 + } 1.126 + return info; 1.127 +}