Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Mark Salyzyn | cfd5b08 | 2016-10-17 14:28:00 -0700 | [diff] [blame] | 17 | #define LOG_TAG "backtrace-map" |
| 18 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 19 | #include <ctype.h> |
Chih-Hung Hsieh | ec2ff8c | 2016-04-18 14:45:46 -0700 | [diff] [blame] | 20 | #include <inttypes.h> |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 21 | #include <stdint.h> |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 22 | #include <sys/types.h> |
| 23 | #include <unistd.h> |
| 24 | |
Mark Salyzyn | 30f991f | 2017-01-10 13:19:54 -0800 | [diff] [blame] | 25 | #include <log/log.h> |
| 26 | |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 27 | #include <backtrace/backtrace_constants.h> |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 28 | #include <backtrace/BacktraceMap.h> |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 29 | |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 30 | #include "thread_utils.h" |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 31 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 32 | BacktraceMap::BacktraceMap(pid_t pid) : pid_(pid) { |
| 33 | if (pid_ < 0) { |
| 34 | pid_ = getpid(); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | BacktraceMap::~BacktraceMap() { |
| 39 | } |
| 40 | |
Christopher Ferris | 12385e3 | 2015-02-06 13:22:01 -0800 | [diff] [blame] | 41 | void BacktraceMap::FillIn(uintptr_t addr, backtrace_map_t* map) { |
Christopher Ferris | 3a14004 | 2016-06-15 15:49:50 -0700 | [diff] [blame] | 42 | ScopedBacktraceMapIteratorLock lock(this); |
| 43 | for (BacktraceMap::const_iterator it = begin(); it != end(); ++it) { |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 44 | if (addr >= it->start && addr < it->end) { |
Christopher Ferris | 12385e3 | 2015-02-06 13:22:01 -0800 | [diff] [blame] | 45 | *map = *it; |
| 46 | return; |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 47 | } |
| 48 | } |
Christopher Ferris | 12385e3 | 2015-02-06 13:22:01 -0800 | [diff] [blame] | 49 | *map = {}; |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | bool BacktraceMap::ParseLine(const char* line, backtrace_map_t* map) { |
Chih-Hung Hsieh | ec2ff8c | 2016-04-18 14:45:46 -0700 | [diff] [blame] | 53 | uint64_t start; |
| 54 | uint64_t end; |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 55 | char permissions[5]; |
| 56 | int name_pos; |
| 57 | |
| 58 | #if defined(__APPLE__) |
| 59 | // Mac OS vmmap(1) output: |
| 60 | // __TEXT 0009f000-000a1000 [ 8K 8K] r-x/rwx SM=COW /Volumes/android/dalvik-dev/out/host/darwin-x86/bin/libcorkscrew_test\n |
| 61 | // 012345678901234567890123456789012345678901234567890123456789 |
| 62 | // 0 1 2 3 4 5 |
Chih-Hung Hsieh | ec2ff8c | 2016-04-18 14:45:46 -0700 | [diff] [blame] | 63 | if (sscanf(line, "%*21c %" SCNx64 "-%" SCNx64 " [%*13c] %3c/%*3c SM=%*3c %n", |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 64 | &start, &end, permissions, &name_pos) != 3) { |
| 65 | #else |
| 66 | // Linux /proc/<pid>/maps lines: |
| 67 | // 6f000000-6f01e000 rwxp 00000000 00:0c 16389419 /system/lib/libcomposer.so\n |
| 68 | // 012345678901234567890123456789012345678901234567890123456789 |
| 69 | // 0 1 2 3 4 5 |
Chih-Hung Hsieh | ec2ff8c | 2016-04-18 14:45:46 -0700 | [diff] [blame] | 70 | if (sscanf(line, "%" SCNx64 "-%" SCNx64 " %4s %*x %*x:%*x %*d %n", |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 71 | &start, &end, permissions, &name_pos) != 3) { |
| 72 | #endif |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | map->start = start; |
| 77 | map->end = end; |
| 78 | map->flags = PROT_NONE; |
| 79 | if (permissions[0] == 'r') { |
| 80 | map->flags |= PROT_READ; |
| 81 | } |
| 82 | if (permissions[1] == 'w') { |
| 83 | map->flags |= PROT_WRITE; |
| 84 | } |
| 85 | if (permissions[2] == 'x') { |
| 86 | map->flags |= PROT_EXEC; |
| 87 | } |
| 88 | |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 89 | map->name = line+name_pos; |
| 90 | if (!map->name.empty() && map->name[map->name.length()-1] == '\n') { |
| 91 | map->name.erase(map->name.length()-1); |
| 92 | } |
| 93 | |
| 94 | ALOGV("Parsed map: start=%p, end=%p, flags=%x, name=%s", |
Colin Cross | f4b0b79 | 2014-02-06 20:07:15 -0800 | [diff] [blame] | 95 | reinterpret_cast<void*>(map->start), reinterpret_cast<void*>(map->end), |
| 96 | map->flags, map->name.c_str()); |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 97 | return true; |
| 98 | } |
| 99 | |
| 100 | bool BacktraceMap::Build() { |
| 101 | #if defined(__APPLE__) |
| 102 | char cmd[sizeof(pid_t)*3 + sizeof("vmmap -w -resident -submap -allSplitLibs -interleaved ") + 1]; |
| 103 | #else |
| 104 | char path[sizeof(pid_t)*3 + sizeof("/proc//maps") + 1]; |
| 105 | #endif |
| 106 | char line[1024]; |
| 107 | |
| 108 | #if defined(__APPLE__) |
| 109 | // cmd is guaranteed to always be big enough to hold this string. |
Christopher Ferris | b8c7295 | 2014-04-18 14:12:35 -0700 | [diff] [blame] | 110 | snprintf(cmd, sizeof(cmd), "vmmap -w -resident -submap -allSplitLibs -interleaved %d", pid_); |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 111 | FILE* fp = popen(cmd, "r"); |
| 112 | #else |
| 113 | // path is guaranteed to always be big enough to hold this string. |
Christopher Ferris | b8c7295 | 2014-04-18 14:12:35 -0700 | [diff] [blame] | 114 | snprintf(path, sizeof(path), "/proc/%d/maps", pid_); |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 115 | FILE* fp = fopen(path, "r"); |
| 116 | #endif |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 117 | if (fp == nullptr) { |
Christopher Ferris | 4675682 | 2014-01-14 20:16:30 -0800 | [diff] [blame] | 118 | return false; |
| 119 | } |
| 120 | |
| 121 | while(fgets(line, sizeof(line), fp)) { |
| 122 | backtrace_map_t map; |
| 123 | if (ParseLine(line, &map)) { |
| 124 | maps_.push_back(map); |
| 125 | } |
| 126 | } |
| 127 | #if defined(__APPLE__) |
| 128 | pclose(fp); |
| 129 | #else |
| 130 | fclose(fp); |
| 131 | #endif |
| 132 | |
| 133 | return true; |
| 134 | } |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 135 | |
| 136 | #if defined(__APPLE__) |
| 137 | // Corkscrew and libunwind don't compile on the mac, so create a generic |
| 138 | // map object. |
Colin Cross | 5b439ea | 2015-04-30 15:11:34 -0700 | [diff] [blame] | 139 | BacktraceMap* BacktraceMap::Create(pid_t pid, bool /*uncached*/) { |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 140 | BacktraceMap* map = new BacktraceMap(pid); |
| 141 | if (!map->Build()) { |
| 142 | delete map; |
Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 143 | return nullptr; |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 144 | } |
| 145 | return map; |
| 146 | } |
| 147 | #endif |
Yabin Cui | 9e402bb | 2015-09-22 04:46:57 +0000 | [diff] [blame] | 148 | |
| 149 | BacktraceMap* BacktraceMap::Create(pid_t pid, const std::vector<backtrace_map_t>& maps) { |
| 150 | BacktraceMap* backtrace_map = new BacktraceMap(pid); |
| 151 | backtrace_map->maps_.insert(backtrace_map->maps_.begin(), maps.begin(), maps.end()); |
| 152 | std::sort(backtrace_map->maps_.begin(), backtrace_map->maps_.end(), |
| 153 | [](const backtrace_map_t& map1, const backtrace_map_t& map2) { |
| 154 | return map1.start < map2.start; |
| 155 | }); |
| 156 | return backtrace_map; |
| 157 | } |