blob: 399721dc16b52e68d1dbf37dc93c2cb9dd148e92 [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
Christopher Ferrisdf290612014-01-22 19:21:07 -080035#include "thread_utils.h"
Christopher Ferrisdf290612014-01-22 19:21:07 -080036
Elliott Hughese1415a52018-02-15 09:18:21 -080037using android::base::StringPrintf;
38
39std::string backtrace_map_t::Name() const {
40 if (!name.empty()) return name;
41 if (start == 0 && end == 0) return "";
42 return StringPrintf("<anonymous:%" PRIPTR ">", start);
43}
44
Christopher Ferris46756822014-01-14 20:16:30 -080045BacktraceMap::BacktraceMap(pid_t pid) : pid_(pid) {
46 if (pid_ < 0) {
47 pid_ = getpid();
48 }
49}
50
51BacktraceMap::~BacktraceMap() {
52}
53
Christopher Ferris7937a362018-01-18 11:15:49 -080054void BacktraceMap::FillIn(uint64_t addr, backtrace_map_t* map) {
Christopher Ferris3a140042016-06-15 15:49:50 -070055 ScopedBacktraceMapIteratorLock lock(this);
Christopher Ferrisb7de5f52017-12-01 21:37:37 -080056 for (auto it = begin(); it != end(); ++it) {
57 const backtrace_map_t* entry = *it;
58 if (addr >= entry->start && addr < entry->end) {
59 *map = *entry;
Christopher Ferris12385e32015-02-06 13:22:01 -080060 return;
Christopher Ferris46756822014-01-14 20:16:30 -080061 }
62 }
Christopher Ferris12385e32015-02-06 13:22:01 -080063 *map = {};
Christopher Ferris46756822014-01-14 20:16:30 -080064}
65
Yabin Cui3841acc2018-05-10 17:19:12 -070066#if defined(__APPLE__)
67static bool ParseLine(const char* line, backtrace_map_t* map) {
Chih-Hung Hsiehec2ff8c2016-04-18 14:45:46 -070068 uint64_t start;
69 uint64_t end;
Christopher Ferris46756822014-01-14 20:16:30 -080070 char permissions[5];
71 int name_pos;
72
Christopher Ferris46756822014-01-14 20:16:30 -080073// Mac OS vmmap(1) output:
74// __TEXT 0009f000-000a1000 [ 8K 8K] r-x/rwx SM=COW /Volumes/android/dalvik-dev/out/host/darwin-x86/bin/libcorkscrew_test\n
75// 012345678901234567890123456789012345678901234567890123456789
76// 0 1 2 3 4 5
Chih-Hung Hsiehec2ff8c2016-04-18 14:45:46 -070077 if (sscanf(line, "%*21c %" SCNx64 "-%" SCNx64 " [%*13c] %3c/%*3c SM=%*3c %n",
Christopher Ferris46756822014-01-14 20:16:30 -080078 &start, &end, permissions, &name_pos) != 3) {
Christopher Ferris46756822014-01-14 20:16:30 -080079 return false;
80 }
81
82 map->start = start;
83 map->end = end;
84 map->flags = PROT_NONE;
85 if (permissions[0] == 'r') {
86 map->flags |= PROT_READ;
87 }
88 if (permissions[1] == 'w') {
89 map->flags |= PROT_WRITE;
90 }
91 if (permissions[2] == 'x') {
92 map->flags |= PROT_EXEC;
93 }
94
Christopher Ferris46756822014-01-14 20:16:30 -080095 map->name = line+name_pos;
96 if (!map->name.empty() && map->name[map->name.length()-1] == '\n') {
97 map->name.erase(map->name.length()-1);
98 }
99
100 ALOGV("Parsed map: start=%p, end=%p, flags=%x, name=%s",
Colin Crossf4b0b792014-02-06 20:07:15 -0800101 reinterpret_cast<void*>(map->start), reinterpret_cast<void*>(map->end),
102 map->flags, map->name.c_str());
Christopher Ferris46756822014-01-14 20:16:30 -0800103 return true;
104}
Yabin Cui3841acc2018-05-10 17:19:12 -0700105#endif // defined(__APPLE__)
Christopher Ferris46756822014-01-14 20:16:30 -0800106
107bool BacktraceMap::Build() {
108#if defined(__APPLE__)
109 char cmd[sizeof(pid_t)*3 + sizeof("vmmap -w -resident -submap -allSplitLibs -interleaved ") + 1];
Christopher Ferris46756822014-01-14 20:16:30 -0800110 char line[1024];
Christopher Ferris46756822014-01-14 20:16:30 -0800111 // cmd is guaranteed to always be big enough to hold this string.
Christopher Ferrisb8c72952014-04-18 14:12:35 -0700112 snprintf(cmd, sizeof(cmd), "vmmap -w -resident -submap -allSplitLibs -interleaved %d", pid_);
Christopher Ferris46756822014-01-14 20:16:30 -0800113 FILE* fp = popen(cmd, "r");
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700114 if (fp == nullptr) {
Christopher Ferris46756822014-01-14 20:16:30 -0800115 return false;
116 }
117
118 while(fgets(line, sizeof(line), fp)) {
119 backtrace_map_t map;
120 if (ParseLine(line, &map)) {
121 maps_.push_back(map);
122 }
123 }
Christopher Ferris46756822014-01-14 20:16:30 -0800124 pclose(fp);
Christopher Ferris46756822014-01-14 20:16:30 -0800125 return true;
Yabin Cui3841acc2018-05-10 17:19:12 -0700126#else
127 return android::procinfo::ReadProcessMaps(
128 pid_, [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t, const char* name) {
129 maps_.resize(maps_.size() + 1);
130 backtrace_map_t& map = maps_.back();
131 map.start = start;
132 map.end = end;
133 map.flags = flags;
134 map.name = name;
135 });
136#endif
Christopher Ferris46756822014-01-14 20:16:30 -0800137}
Christopher Ferrisdf290612014-01-22 19:21:07 -0800138
139#if defined(__APPLE__)
140// Corkscrew and libunwind don't compile on the mac, so create a generic
141// map object.
Colin Cross5b439ea2015-04-30 15:11:34 -0700142BacktraceMap* BacktraceMap::Create(pid_t pid, bool /*uncached*/) {
Christopher Ferrisdf290612014-01-22 19:21:07 -0800143 BacktraceMap* map = new BacktraceMap(pid);
144 if (!map->Build()) {
145 delete map;
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700146 return nullptr;
Christopher Ferrisdf290612014-01-22 19:21:07 -0800147 }
148 return map;
149}
150#endif