blob: 0f1ae11f74f10b79dcc45c4a24ab306954599e9c [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
Christopher Ferrisdf290612014-01-22 19:21:07 -080027#include <backtrace/backtrace_constants.h>
Christopher Ferris46756822014-01-14 20:16:30 -080028#include <backtrace/BacktraceMap.h>
Christopher Ferris46756822014-01-14 20:16:30 -080029
Christopher Ferrisdf290612014-01-22 19:21:07 -080030#include "thread_utils.h"
Christopher Ferrisdf290612014-01-22 19:21:07 -080031
Christopher Ferris46756822014-01-14 20:16:30 -080032BacktraceMap::BacktraceMap(pid_t pid) : pid_(pid) {
33 if (pid_ < 0) {
34 pid_ = getpid();
35 }
36}
37
38BacktraceMap::~BacktraceMap() {
39}
40
Christopher Ferris12385e32015-02-06 13:22:01 -080041void BacktraceMap::FillIn(uintptr_t addr, backtrace_map_t* map) {
Christopher Ferris3a140042016-06-15 15:49:50 -070042 ScopedBacktraceMapIteratorLock lock(this);
Christopher Ferrisb7de5f52017-12-01 21:37:37 -080043 for (auto it = begin(); it != end(); ++it) {
44 const backtrace_map_t* entry = *it;
45 if (addr >= entry->start && addr < entry->end) {
46 *map = *entry;
Christopher Ferris12385e32015-02-06 13:22:01 -080047 return;
Christopher Ferris46756822014-01-14 20:16:30 -080048 }
49 }
Christopher Ferris12385e32015-02-06 13:22:01 -080050 *map = {};
Christopher Ferris46756822014-01-14 20:16:30 -080051}
52
53bool BacktraceMap::ParseLine(const char* line, backtrace_map_t* map) {
Chih-Hung Hsiehec2ff8c2016-04-18 14:45:46 -070054 uint64_t start;
55 uint64_t end;
Christopher Ferris46756822014-01-14 20:16:30 -080056 char permissions[5];
57 int name_pos;
58
59#if defined(__APPLE__)
60// Mac OS vmmap(1) output:
61// __TEXT 0009f000-000a1000 [ 8K 8K] r-x/rwx SM=COW /Volumes/android/dalvik-dev/out/host/darwin-x86/bin/libcorkscrew_test\n
62// 012345678901234567890123456789012345678901234567890123456789
63// 0 1 2 3 4 5
Chih-Hung Hsiehec2ff8c2016-04-18 14:45:46 -070064 if (sscanf(line, "%*21c %" SCNx64 "-%" SCNx64 " [%*13c] %3c/%*3c SM=%*3c %n",
Christopher Ferris46756822014-01-14 20:16:30 -080065 &start, &end, permissions, &name_pos) != 3) {
66#else
67// Linux /proc/<pid>/maps lines:
68// 6f000000-6f01e000 rwxp 00000000 00:0c 16389419 /system/lib/libcomposer.so\n
69// 012345678901234567890123456789012345678901234567890123456789
70// 0 1 2 3 4 5
Chih-Hung Hsiehec2ff8c2016-04-18 14:45:46 -070071 if (sscanf(line, "%" SCNx64 "-%" SCNx64 " %4s %*x %*x:%*x %*d %n",
Christopher Ferris46756822014-01-14 20:16:30 -080072 &start, &end, permissions, &name_pos) != 3) {
73#endif
74 return false;
75 }
76
77 map->start = start;
78 map->end = end;
79 map->flags = PROT_NONE;
80 if (permissions[0] == 'r') {
81 map->flags |= PROT_READ;
82 }
83 if (permissions[1] == 'w') {
84 map->flags |= PROT_WRITE;
85 }
86 if (permissions[2] == 'x') {
87 map->flags |= PROT_EXEC;
88 }
89
Christopher Ferris46756822014-01-14 20:16:30 -080090 map->name = line+name_pos;
91 if (!map->name.empty() && map->name[map->name.length()-1] == '\n') {
92 map->name.erase(map->name.length()-1);
93 }
94
95 ALOGV("Parsed map: start=%p, end=%p, flags=%x, name=%s",
Colin Crossf4b0b792014-02-06 20:07:15 -080096 reinterpret_cast<void*>(map->start), reinterpret_cast<void*>(map->end),
97 map->flags, map->name.c_str());
Christopher Ferris46756822014-01-14 20:16:30 -080098 return true;
99}
100
101bool BacktraceMap::Build() {
102#if defined(__APPLE__)
103 char cmd[sizeof(pid_t)*3 + sizeof("vmmap -w -resident -submap -allSplitLibs -interleaved ") + 1];
104#else
105 char path[sizeof(pid_t)*3 + sizeof("/proc//maps") + 1];
106#endif
107 char line[1024];
108
109#if defined(__APPLE__)
110 // cmd is guaranteed to always be big enough to hold this string.
Christopher Ferrisb8c72952014-04-18 14:12:35 -0700111 snprintf(cmd, sizeof(cmd), "vmmap -w -resident -submap -allSplitLibs -interleaved %d", pid_);
Christopher Ferris46756822014-01-14 20:16:30 -0800112 FILE* fp = popen(cmd, "r");
113#else
114 // path is guaranteed to always be big enough to hold this string.
Christopher Ferrisb8c72952014-04-18 14:12:35 -0700115 snprintf(path, sizeof(path), "/proc/%d/maps", pid_);
Christopher Ferris46756822014-01-14 20:16:30 -0800116 FILE* fp = fopen(path, "r");
117#endif
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700118 if (fp == nullptr) {
Christopher Ferris46756822014-01-14 20:16:30 -0800119 return false;
120 }
121
122 while(fgets(line, sizeof(line), fp)) {
123 backtrace_map_t map;
124 if (ParseLine(line, &map)) {
125 maps_.push_back(map);
126 }
127 }
128#if defined(__APPLE__)
129 pclose(fp);
130#else
131 fclose(fp);
132#endif
133
134 return true;
135}
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
Yabin Cui9e402bb2015-09-22 04:46:57 +0000149
150BacktraceMap* BacktraceMap::Create(pid_t pid, const std::vector<backtrace_map_t>& maps) {
151 BacktraceMap* backtrace_map = new BacktraceMap(pid);
152 backtrace_map->maps_.insert(backtrace_map->maps_.begin(), maps.begin(), maps.end());
153 std::sort(backtrace_map->maps_.begin(), backtrace_map->maps_.end(),
154 [](const backtrace_map_t& map1, const backtrace_map_t& map2) {
155 return map1.start < map2.start;
156 });
157 return backtrace_map;
158}