blob: 449637553bd4b8bc1284faf11a980b2a6d38a0fc [file] [log] [blame]
Christopher Ferris46756822014-01-14 20:16:30 -08001/*
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 Salyzyncfd5b082016-10-17 14:28:00 -070017#define LOG_TAG "backtrace-map"
18
Christopher Ferris46756822014-01-14 20:16:30 -080019#include <ctype.h>
Chih-Hung Hsiehec2ff8c2016-04-18 14:45:46 -070020#include <inttypes.h>
Christopher Ferris2c43cff2015-03-26 19:18:36 -070021#include <stdint.h>
Christopher Ferris46756822014-01-14 20:16:30 -080022#include <sys/types.h>
23#include <unistd.h>
24
Mark Salyzynff2dcd92016-09-28 15:54:45 -070025#include <android/log.h>
Christopher Ferrisdf290612014-01-22 19:21:07 -080026#include <backtrace/backtrace_constants.h>
Christopher Ferris46756822014-01-14 20:16:30 -080027#include <backtrace/BacktraceMap.h>
Christopher Ferris46756822014-01-14 20:16:30 -080028
Christopher Ferrisdf290612014-01-22 19:21:07 -080029#include "thread_utils.h"
Christopher Ferrisdf290612014-01-22 19:21:07 -080030
Christopher Ferris46756822014-01-14 20:16:30 -080031BacktraceMap::BacktraceMap(pid_t pid) : pid_(pid) {
32 if (pid_ < 0) {
33 pid_ = getpid();
34 }
35}
36
37BacktraceMap::~BacktraceMap() {
38}
39
Christopher Ferris12385e32015-02-06 13:22:01 -080040void BacktraceMap::FillIn(uintptr_t addr, backtrace_map_t* map) {
Christopher Ferris3a140042016-06-15 15:49:50 -070041 ScopedBacktraceMapIteratorLock lock(this);
42 for (BacktraceMap::const_iterator it = begin(); it != end(); ++it) {
Christopher Ferris46756822014-01-14 20:16:30 -080043 if (addr >= it->start && addr < it->end) {
Christopher Ferris12385e32015-02-06 13:22:01 -080044 *map = *it;
45 return;
Christopher Ferris46756822014-01-14 20:16:30 -080046 }
47 }
Christopher Ferris12385e32015-02-06 13:22:01 -080048 *map = {};
Christopher Ferris46756822014-01-14 20:16:30 -080049}
50
51bool BacktraceMap::ParseLine(const char* line, backtrace_map_t* map) {
Chih-Hung Hsiehec2ff8c2016-04-18 14:45:46 -070052 uint64_t start;
53 uint64_t end;
Christopher Ferris46756822014-01-14 20:16:30 -080054 char permissions[5];
55 int name_pos;
56
57#if defined(__APPLE__)
58// Mac OS vmmap(1) output:
59// __TEXT 0009f000-000a1000 [ 8K 8K] r-x/rwx SM=COW /Volumes/android/dalvik-dev/out/host/darwin-x86/bin/libcorkscrew_test\n
60// 012345678901234567890123456789012345678901234567890123456789
61// 0 1 2 3 4 5
Chih-Hung Hsiehec2ff8c2016-04-18 14:45:46 -070062 if (sscanf(line, "%*21c %" SCNx64 "-%" SCNx64 " [%*13c] %3c/%*3c SM=%*3c %n",
Christopher Ferris46756822014-01-14 20:16:30 -080063 &start, &end, permissions, &name_pos) != 3) {
64#else
65// Linux /proc/<pid>/maps lines:
66// 6f000000-6f01e000 rwxp 00000000 00:0c 16389419 /system/lib/libcomposer.so\n
67// 012345678901234567890123456789012345678901234567890123456789
68// 0 1 2 3 4 5
Chih-Hung Hsiehec2ff8c2016-04-18 14:45:46 -070069 if (sscanf(line, "%" SCNx64 "-%" SCNx64 " %4s %*x %*x:%*x %*d %n",
Christopher Ferris46756822014-01-14 20:16:30 -080070 &start, &end, permissions, &name_pos) != 3) {
71#endif
72 return false;
73 }
74
75 map->start = start;
76 map->end = end;
77 map->flags = PROT_NONE;
78 if (permissions[0] == 'r') {
79 map->flags |= PROT_READ;
80 }
81 if (permissions[1] == 'w') {
82 map->flags |= PROT_WRITE;
83 }
84 if (permissions[2] == 'x') {
85 map->flags |= PROT_EXEC;
86 }
87
Christopher Ferris46756822014-01-14 20:16:30 -080088 map->name = line+name_pos;
89 if (!map->name.empty() && map->name[map->name.length()-1] == '\n') {
90 map->name.erase(map->name.length()-1);
91 }
92
93 ALOGV("Parsed map: start=%p, end=%p, flags=%x, name=%s",
Colin Crossf4b0b792014-02-06 20:07:15 -080094 reinterpret_cast<void*>(map->start), reinterpret_cast<void*>(map->end),
95 map->flags, map->name.c_str());
Christopher Ferris46756822014-01-14 20:16:30 -080096 return true;
97}
98
99bool BacktraceMap::Build() {
100#if defined(__APPLE__)
101 char cmd[sizeof(pid_t)*3 + sizeof("vmmap -w -resident -submap -allSplitLibs -interleaved ") + 1];
102#else
103 char path[sizeof(pid_t)*3 + sizeof("/proc//maps") + 1];
104#endif
105 char line[1024];
106
107#if defined(__APPLE__)
108 // cmd is guaranteed to always be big enough to hold this string.
Christopher Ferrisb8c72952014-04-18 14:12:35 -0700109 snprintf(cmd, sizeof(cmd), "vmmap -w -resident -submap -allSplitLibs -interleaved %d", pid_);
Christopher Ferris46756822014-01-14 20:16:30 -0800110 FILE* fp = popen(cmd, "r");
111#else
112 // path is guaranteed to always be big enough to hold this string.
Christopher Ferrisb8c72952014-04-18 14:12:35 -0700113 snprintf(path, sizeof(path), "/proc/%d/maps", pid_);
Christopher Ferris46756822014-01-14 20:16:30 -0800114 FILE* fp = fopen(path, "r");
115#endif
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700116 if (fp == nullptr) {
Christopher Ferris46756822014-01-14 20:16:30 -0800117 return false;
118 }
119
120 while(fgets(line, sizeof(line), fp)) {
121 backtrace_map_t map;
122 if (ParseLine(line, &map)) {
123 maps_.push_back(map);
124 }
125 }
126#if defined(__APPLE__)
127 pclose(fp);
128#else
129 fclose(fp);
130#endif
131
132 return true;
133}
Christopher Ferrisdf290612014-01-22 19:21:07 -0800134
135#if defined(__APPLE__)
136// Corkscrew and libunwind don't compile on the mac, so create a generic
137// map object.
Colin Cross5b439ea2015-04-30 15:11:34 -0700138BacktraceMap* BacktraceMap::Create(pid_t pid, bool /*uncached*/) {
Christopher Ferrisdf290612014-01-22 19:21:07 -0800139 BacktraceMap* map = new BacktraceMap(pid);
140 if (!map->Build()) {
141 delete map;
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700142 return nullptr;
Christopher Ferrisdf290612014-01-22 19:21:07 -0800143 }
144 return map;
145}
146#endif
Yabin Cui9e402bb2015-09-22 04:46:57 +0000147
148BacktraceMap* BacktraceMap::Create(pid_t pid, const std::vector<backtrace_map_t>& maps) {
149 BacktraceMap* backtrace_map = new BacktraceMap(pid);
150 backtrace_map->maps_.insert(backtrace_map->maps_.begin(), maps.begin(), maps.end());
151 std::sort(backtrace_map->maps_.begin(), backtrace_map->maps_.end(),
152 [](const backtrace_map_t& map1, const backtrace_map_t& map2) {
153 return map1.start < map2.start;
154 });
155 return backtrace_map;
156}