tools/profiler/shared-libraries-linux.cc

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #include "shared-libraries.h"
michael@0 6
michael@0 7 #define PATH_MAX_TOSTRING(x) #x
michael@0 8 #define PATH_MAX_STRING(x) PATH_MAX_TOSTRING(x)
michael@0 9 #include <stdlib.h>
michael@0 10 #include <stdio.h>
michael@0 11 #include <string.h>
michael@0 12 #include <limits.h>
michael@0 13 #include <unistd.h>
michael@0 14 #include <fstream>
michael@0 15 #include "platform.h"
michael@0 16 #include "shared-libraries.h"
michael@0 17
michael@0 18 #if !defined(MOZ_WIDGET_GONK)
michael@0 19 // TODO fix me with proper include
michael@0 20 #include "nsDebug.h"
michael@0 21 #ifdef ANDROID
michael@0 22 #include "ElfLoader.h" // dl_phdr_info
michael@0 23 #else
michael@0 24 #include <link.h> // dl_phdr_info
michael@0 25 #endif
michael@0 26 #include <features.h>
michael@0 27 #include <dlfcn.h>
michael@0 28 #include <sys/types.h>
michael@0 29
michael@0 30 #ifdef ANDROID
michael@0 31 extern "C" __attribute__((weak))
michael@0 32 int dl_iterate_phdr(
michael@0 33 int (*callback) (struct dl_phdr_info *info,
michael@0 34 size_t size, void *data),
michael@0 35 void *data);
michael@0 36 #endif
michael@0 37
michael@0 38 int dl_iterate_callback(struct dl_phdr_info *dl_info, size_t size, void *data)
michael@0 39 {
michael@0 40 SharedLibraryInfo& info = *reinterpret_cast<SharedLibraryInfo*>(data);
michael@0 41
michael@0 42 if (dl_info->dlpi_phnum <= 0)
michael@0 43 return 0;
michael@0 44
michael@0 45 unsigned long libStart = -1;
michael@0 46 unsigned long libEnd = 0;
michael@0 47
michael@0 48 for (size_t i = 0; i < dl_info->dlpi_phnum; i++) {
michael@0 49 if (dl_info->dlpi_phdr[i].p_type != PT_LOAD) {
michael@0 50 continue;
michael@0 51 }
michael@0 52 unsigned long start = dl_info->dlpi_addr + dl_info->dlpi_phdr[i].p_vaddr;
michael@0 53 unsigned long end = start + dl_info->dlpi_phdr[i].p_memsz;
michael@0 54 if (start < libStart)
michael@0 55 libStart = start;
michael@0 56 if (end > libEnd)
michael@0 57 libEnd = end;
michael@0 58 }
michael@0 59 SharedLibrary shlib(libStart, libEnd, 0, "", dl_info->dlpi_name);
michael@0 60 info.AddSharedLibrary(shlib);
michael@0 61
michael@0 62 return 0;
michael@0 63 }
michael@0 64 #endif
michael@0 65
michael@0 66 SharedLibraryInfo SharedLibraryInfo::GetInfoForSelf()
michael@0 67 {
michael@0 68 SharedLibraryInfo info;
michael@0 69
michael@0 70 #if !defined(MOZ_WIDGET_GONK)
michael@0 71 dl_iterate_phdr(dl_iterate_callback, &info);
michael@0 72 #ifndef ANDROID
michael@0 73 return info;
michael@0 74 #endif
michael@0 75 #endif
michael@0 76
michael@0 77 pid_t pid = getpid();
michael@0 78 char path[PATH_MAX];
michael@0 79 snprintf(path, PATH_MAX, "/proc/%d/maps", pid);
michael@0 80 std::ifstream maps(path);
michael@0 81 std::string line;
michael@0 82 int count = 0;
michael@0 83 while (std::getline(maps, line)) {
michael@0 84 int ret;
michael@0 85 //XXX: needs input sanitizing
michael@0 86 unsigned long start;
michael@0 87 unsigned long end;
michael@0 88 char perm[6] = "";
michael@0 89 unsigned long offset;
michael@0 90 char name[PATH_MAX] = "";
michael@0 91 ret = sscanf(line.c_str(),
michael@0 92 "%lx-%lx %6s %lx %*s %*x %" PATH_MAX_STRING(PATH_MAX) "s\n",
michael@0 93 &start, &end, perm, &offset, name);
michael@0 94 if (!strchr(perm, 'x')) {
michael@0 95 // Ignore non executable entries
michael@0 96 continue;
michael@0 97 }
michael@0 98 if (ret != 5 && ret != 4) {
michael@0 99 LOG("Get maps line failed");
michael@0 100 continue;
michael@0 101 }
michael@0 102 #if defined(ANDROID) && !defined(MOZ_WIDGET_GONK)
michael@0 103 // Use proc/pid/maps to get the dalvik-jit section since it has
michael@0 104 // no associated phdrs
michael@0 105 if (strcmp(name, "/dev/ashmem/dalvik-jit-code-cache") != 0)
michael@0 106 continue;
michael@0 107 #else
michael@0 108 if (strcmp(perm, "r-xp") != 0) {
michael@0 109 // Ignore entries that are writable and/or shared.
michael@0 110 // At least one graphics driver uses short-lived "rwxs" mappings
michael@0 111 // (see bug 926734 comment 5), so just checking for 'x' isn't enough.
michael@0 112 continue;
michael@0 113 }
michael@0 114 #endif
michael@0 115 SharedLibrary shlib(start, end, offset, "", name);
michael@0 116 info.AddSharedLibrary(shlib);
michael@0 117 if (count > 10000) {
michael@0 118 LOG("Get maps failed");
michael@0 119 break;
michael@0 120 }
michael@0 121 count++;
michael@0 122 }
michael@0 123 return info;
michael@0 124 }

mercurial