blob: 6a967f7d65d45576542bfc496ca70a000af42063 [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 Salyzyn30f991f2017-01-10 13:19:54 -080025#include <log/log.h>
26
Elliott Hughese1415a52018-02-15 09:18:21 -080027#include <android-base/stringprintf.h>
28#include <backtrace/Backtrace.h>
Christopher Ferris46756822014-01-14 20:16:30 -080029#include <backtrace/BacktraceMap.h>
Elliott Hughese1415a52018-02-15 09:18:21 -080030#include <backtrace/backtrace_constants.h>
Yabin Cui3841acc2018-05-10 17:19:12 -070031#if defined(__linux__)
32#include <procinfo/process_map.h>
33#endif
Christopher Ferris46756822014-01-14 20:16:30 -080034
Elliott Hughese1415a52018-02-15 09:18:21 -080035using android::base::StringPrintf;
36
37std::string backtrace_map_t::Name() const {
38 if (!name.empty()) return name;
39 if (start == 0 && end == 0) return "";
40 return StringPrintf("<anonymous:%" PRIPTR ">", start);
41}
42
Christopher Ferris46756822014-01-14 20:16:30 -080043BacktraceMap::BacktraceMap(pid_t pid) : pid_(pid) {
44 if (pid_ < 0) {
45 pid_ = getpid();
46 }
47}
48
49BacktraceMap::~BacktraceMap() {
50}
51
Christopher Ferris7937a362018-01-18 11:15:49 -080052void BacktraceMap::FillIn(uint64_t addr, backtrace_map_t* map) {
Christopher Ferris3a140042016-06-15 15:49:50 -070053 ScopedBacktraceMapIteratorLock lock(this);
Christopher Ferrisb7de5f52017-12-01 21:37:37 -080054 for (auto it = begin(); it != end(); ++it) {
55 const backtrace_map_t* entry = *it;
56 if (addr >= entry->start && addr < entry->end) {
57 *map = *entry;
Christopher Ferris12385e32015-02-06 13:22:01 -080058 return;
Christopher Ferris46756822014-01-14 20:16:30 -080059 }
60 }
Christopher Ferris12385e32015-02-06 13:22:01 -080061 *map = {};
Christopher Ferris46756822014-01-14 20:16:30 -080062}
63
Yabin Cui3841acc2018-05-10 17:19:12 -070064#if defined(__APPLE__)
65static bool ParseLine(const char* line, backtrace_map_t* map) {
Chih-Hung Hsiehec2ff8c2016-04-18 14:45:46 -070066 uint64_t start;
67 uint64_t end;
Christopher Ferris46756822014-01-14 20:16:30 -080068 char permissions[5];
69 int name_pos;
70
Christopher Ferris46756822014-01-14 20:16:30 -080071// Mac OS vmmap(1) output:
72// __TEXT 0009f000-000a1000 [ 8K 8K] r-x/rwx SM=COW /Volumes/android/dalvik-dev/out/host/darwin-x86/bin/libcorkscrew_test\n
73// 012345678901234567890123456789012345678901234567890123456789
74// 0 1 2 3 4 5
Chih-Hung Hsiehec2ff8c2016-04-18 14:45:46 -070075 if (sscanf(line, "%*21c %" SCNx64 "-%" SCNx64 " [%*13c] %3c/%*3c SM=%*3c %n",
Christopher Ferris46756822014-01-14 20:16:30 -080076 &start, &end, permissions, &name_pos) != 3) {
Christopher Ferris46756822014-01-14 20:16:30 -080077 return false;
78 }
79
80 map->start = start;
81 map->end = end;
82 map->flags = PROT_NONE;
83 if (permissions[0] == 'r') {
84 map->flags |= PROT_READ;
85 }
86 if (permissions[1] == 'w') {
87 map->flags |= PROT_WRITE;
88 }
89 if (permissions[2] == 'x') {
90 map->flags |= PROT_EXEC;
91 }
92
Christopher Ferris46756822014-01-14 20:16:30 -080093 map->name = line+name_pos;
94 if (!map->name.empty() && map->name[map->name.length()-1] == '\n') {
95 map->name.erase(map->name.length()-1);
96 }
97
98 ALOGV("Parsed map: start=%p, end=%p, flags=%x, name=%s",
Colin Crossf4b0b792014-02-06 20:07:15 -080099 reinterpret_cast<void*>(map->start), reinterpret_cast<void*>(map->end),
100 map->flags, map->name.c_str());
Christopher Ferris46756822014-01-14 20:16:30 -0800101 return true;
102}
Yabin Cui3841acc2018-05-10 17:19:12 -0700103#endif // defined(__APPLE__)
Christopher Ferris46756822014-01-14 20:16:30 -0800104
105bool BacktraceMap::Build() {
106#if defined(__APPLE__)
107 char cmd[sizeof(pid_t)*3 + sizeof("vmmap -w -resident -submap -allSplitLibs -interleaved ") + 1];
Christopher Ferris46756822014-01-14 20:16:30 -0800108 char line[1024];
Christopher Ferris46756822014-01-14 20:16:30 -0800109 // cmd is guaranteed to always be big enough to hold this string.
Christopher Ferrisb8c72952014-04-18 14:12:35 -0700110 snprintf(cmd, sizeof(cmd), "vmmap -w -resident -submap -allSplitLibs -interleaved %d", pid_);
Christopher Ferris46756822014-01-14 20:16:30 -0800111 FILE* fp = popen(cmd, "r");
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700112 if (fp == nullptr) {
Christopher Ferris46756822014-01-14 20:16:30 -0800113 return false;
114 }
115
116 while(fgets(line, sizeof(line), fp)) {
117 backtrace_map_t map;
118 if (ParseLine(line, &map)) {
119 maps_.push_back(map);
120 }
121 }
Christopher Ferris46756822014-01-14 20:16:30 -0800122 pclose(fp);
Christopher Ferris46756822014-01-14 20:16:30 -0800123 return true;
Yabin Cui3841acc2018-05-10 17:19:12 -0700124#else
125 return android::procinfo::ReadProcessMaps(
126 pid_, [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t, const char* name) {
127 maps_.resize(maps_.size() + 1);
128 backtrace_map_t& map = maps_.back();
129 map.start = start;
130 map.end = end;
131 map.flags = flags;
132 map.name = name;
133 });
134#endif
Christopher Ferris46756822014-01-14 20:16:30 -0800135}
Christopher Ferrisdf290612014-01-22 19:21:07 -0800136
137#if defined(__APPLE__)
138// Corkscrew and libunwind don't compile on the mac, so create a generic
139// map object.
Colin Cross5b439ea2015-04-30 15:11:34 -0700140BacktraceMap* BacktraceMap::Create(pid_t pid, bool /*uncached*/) {
Christopher Ferrisdf290612014-01-22 19:21:07 -0800141 BacktraceMap* map = new BacktraceMap(pid);
142 if (!map->Build()) {
143 delete map;
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700144 return nullptr;
Christopher Ferrisdf290612014-01-22 19:21:07 -0800145 }
146 return map;
147}
148#endif