blob: 781819af57b4eaea3fd36598e88d5e826bde1d70 [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
Sandeep Patilf31c7092019-01-30 17:43:22 -080049BacktraceMap::~BacktraceMap() {}
Christopher Ferris46756822014-01-14 20:16:30 -080050
Christopher Ferris7937a362018-01-18 11:15:49 -080051void BacktraceMap::FillIn(uint64_t addr, backtrace_map_t* map) {
Christopher Ferris3a140042016-06-15 15:49:50 -070052 ScopedBacktraceMapIteratorLock lock(this);
Christopher Ferrisb7de5f52017-12-01 21:37:37 -080053 for (auto it = begin(); it != end(); ++it) {
54 const backtrace_map_t* entry = *it;
55 if (addr >= entry->start && addr < entry->end) {
56 *map = *entry;
Christopher Ferris12385e32015-02-06 13:22:01 -080057 return;
Christopher Ferris46756822014-01-14 20:16:30 -080058 }
59 }
Christopher Ferris12385e32015-02-06 13:22:01 -080060 *map = {};
Christopher Ferris46756822014-01-14 20:16:30 -080061}
62
Yabin Cui3841acc2018-05-10 17:19:12 -070063#if defined(__APPLE__)
64static bool ParseLine(const char* line, backtrace_map_t* map) {
Chih-Hung Hsiehec2ff8c2016-04-18 14:45:46 -070065 uint64_t start;
66 uint64_t end;
Christopher Ferris46756822014-01-14 20:16:30 -080067 char permissions[5];
68 int name_pos;
69
Sandeep Patilf31c7092019-01-30 17:43:22 -080070 // Mac OS vmmap(1) output:
71 // __TEXT 0009f000-000a1000 [ 8K 8K] r-x/rwx SM=COW
72 // /Volumes/android/dalvik-dev/out/host/darwin-x86/bin/libcorkscrew_test\n
73 // 012345678901234567890123456789012345678901234567890123456789
74 // 0 1 2 3 4 5
75 if (sscanf(line, "%*21c %" SCNx64 "-%" SCNx64 " [%*13c] %3c/%*3c SM=%*3c %n", &start, &end,
76 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
Sandeep Patilf31c7092019-01-30 17:43:22 -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);
Christopher Ferris46756822014-01-14 20:16:30 -080096 }
97
Sandeep Patilf31c7092019-01-30 17:43:22 -080098 ALOGV("Parsed map: start=%p, end=%p, flags=%x, name=%s", reinterpret_cast<void*>(map->start),
99 reinterpret_cast<void*>(map->end), map->flags, map->name.c_str());
Christopher Ferris46756822014-01-14 20:16:30 -0800100 return true;
101}
Yabin Cui3841acc2018-05-10 17:19:12 -0700102#endif // defined(__APPLE__)
Christopher Ferris46756822014-01-14 20:16:30 -0800103
104bool BacktraceMap::Build() {
105#if defined(__APPLE__)
Sandeep Patilf31c7092019-01-30 17:43:22 -0800106 char
107 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
Sandeep Patilf31c7092019-01-30 17:43:22 -0800116 while (fgets(line, sizeof(line), fp)) {
Christopher Ferris46756822014-01-14 20:16:30 -0800117 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(
Sandeep Patilf31c7092019-01-30 17:43:22 -0800126 pid_, [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t, ino_t, const char* name) {
Yabin Cui3841acc2018-05-10 17:19:12 -0700127 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