blob: 19551dc577c7aedddcc9fb663fcde6c93b9aeacd [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
17#include <ctype.h>
Chih-Hung Hsiehec2ff8c2016-04-18 14:45:46 -070018#include <inttypes.h>
Christopher Ferris2c43cff2015-03-26 19:18:36 -070019#include <stdint.h>
Christopher Ferris46756822014-01-14 20:16:30 -080020#include <sys/types.h>
21#include <unistd.h>
22
Christopher Ferrisdf290612014-01-22 19:21:07 -080023#include <backtrace/backtrace_constants.h>
Christopher Ferris46756822014-01-14 20:16:30 -080024#include <backtrace/BacktraceMap.h>
25#include <log/log.h>
26
Christopher Ferrisdf290612014-01-22 19:21:07 -080027#include "thread_utils.h"
Christopher Ferrisdf290612014-01-22 19:21:07 -080028
Christopher Ferris46756822014-01-14 20:16:30 -080029BacktraceMap::BacktraceMap(pid_t pid) : pid_(pid) {
30 if (pid_ < 0) {
31 pid_ = getpid();
32 }
33}
34
35BacktraceMap::~BacktraceMap() {
36}
37
Christopher Ferris12385e32015-02-06 13:22:01 -080038void BacktraceMap::FillIn(uintptr_t addr, backtrace_map_t* map) {
Christopher Ferris46756822014-01-14 20:16:30 -080039 for (BacktraceMap::const_iterator it = begin();
40 it != end(); ++it) {
41 if (addr >= it->start && addr < it->end) {
Christopher Ferris12385e32015-02-06 13:22:01 -080042 *map = *it;
43 return;
Christopher Ferris46756822014-01-14 20:16:30 -080044 }
45 }
Christopher Ferris12385e32015-02-06 13:22:01 -080046 *map = {};
Christopher Ferris46756822014-01-14 20:16:30 -080047}
48
49bool BacktraceMap::ParseLine(const char* line, backtrace_map_t* map) {
Chih-Hung Hsiehec2ff8c2016-04-18 14:45:46 -070050 uint64_t start;
51 uint64_t end;
Christopher Ferris46756822014-01-14 20:16:30 -080052 char permissions[5];
53 int name_pos;
54
55#if defined(__APPLE__)
56// Mac OS vmmap(1) output:
57// __TEXT 0009f000-000a1000 [ 8K 8K] r-x/rwx SM=COW /Volumes/android/dalvik-dev/out/host/darwin-x86/bin/libcorkscrew_test\n
58// 012345678901234567890123456789012345678901234567890123456789
59// 0 1 2 3 4 5
Chih-Hung Hsiehec2ff8c2016-04-18 14:45:46 -070060 if (sscanf(line, "%*21c %" SCNx64 "-%" SCNx64 " [%*13c] %3c/%*3c SM=%*3c %n",
Christopher Ferris46756822014-01-14 20:16:30 -080061 &start, &end, permissions, &name_pos) != 3) {
62#else
63// Linux /proc/<pid>/maps lines:
64// 6f000000-6f01e000 rwxp 00000000 00:0c 16389419 /system/lib/libcomposer.so\n
65// 012345678901234567890123456789012345678901234567890123456789
66// 0 1 2 3 4 5
Chih-Hung Hsiehec2ff8c2016-04-18 14:45:46 -070067 if (sscanf(line, "%" SCNx64 "-%" SCNx64 " %4s %*x %*x:%*x %*d %n",
Christopher Ferris46756822014-01-14 20:16:30 -080068 &start, &end, permissions, &name_pos) != 3) {
69#endif
70 return false;
71 }
72
73 map->start = start;
74 map->end = end;
75 map->flags = PROT_NONE;
76 if (permissions[0] == 'r') {
77 map->flags |= PROT_READ;
78 }
79 if (permissions[1] == 'w') {
80 map->flags |= PROT_WRITE;
81 }
82 if (permissions[2] == 'x') {
83 map->flags |= PROT_EXEC;
84 }
85
Christopher Ferris46756822014-01-14 20:16:30 -080086 map->name = line+name_pos;
87 if (!map->name.empty() && map->name[map->name.length()-1] == '\n') {
88 map->name.erase(map->name.length()-1);
89 }
90
91 ALOGV("Parsed map: start=%p, end=%p, flags=%x, name=%s",
Colin Crossf4b0b792014-02-06 20:07:15 -080092 reinterpret_cast<void*>(map->start), reinterpret_cast<void*>(map->end),
93 map->flags, map->name.c_str());
Christopher Ferris46756822014-01-14 20:16:30 -080094 return true;
95}
96
97bool BacktraceMap::Build() {
98#if defined(__APPLE__)
99 char cmd[sizeof(pid_t)*3 + sizeof("vmmap -w -resident -submap -allSplitLibs -interleaved ") + 1];
100#else
101 char path[sizeof(pid_t)*3 + sizeof("/proc//maps") + 1];
102#endif
103 char line[1024];
104
105#if defined(__APPLE__)
106 // cmd is guaranteed to always be big enough to hold this string.
Christopher Ferrisb8c72952014-04-18 14:12:35 -0700107 snprintf(cmd, sizeof(cmd), "vmmap -w -resident -submap -allSplitLibs -interleaved %d", pid_);
Christopher Ferris46756822014-01-14 20:16:30 -0800108 FILE* fp = popen(cmd, "r");
109#else
110 // path is guaranteed to always be big enough to hold this string.
Christopher Ferrisb8c72952014-04-18 14:12:35 -0700111 snprintf(path, sizeof(path), "/proc/%d/maps", pid_);
Christopher Ferris46756822014-01-14 20:16:30 -0800112 FILE* fp = fopen(path, "r");
113#endif
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 }
124#if defined(__APPLE__)
125 pclose(fp);
126#else
127 fclose(fp);
128#endif
129
130 return true;
131}
Christopher Ferrisdf290612014-01-22 19:21:07 -0800132
133#if defined(__APPLE__)
134// Corkscrew and libunwind don't compile on the mac, so create a generic
135// map object.
Colin Cross5b439ea2015-04-30 15:11:34 -0700136BacktraceMap* BacktraceMap::Create(pid_t pid, bool /*uncached*/) {
Christopher Ferrisdf290612014-01-22 19:21:07 -0800137 BacktraceMap* map = new BacktraceMap(pid);
138 if (!map->Build()) {
139 delete map;
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700140 return nullptr;
Christopher Ferrisdf290612014-01-22 19:21:07 -0800141 }
142 return map;
143}
144#endif
Yabin Cui9e402bb2015-09-22 04:46:57 +0000145
146BacktraceMap* BacktraceMap::Create(pid_t pid, const std::vector<backtrace_map_t>& maps) {
147 BacktraceMap* backtrace_map = new BacktraceMap(pid);
148 backtrace_map->maps_.insert(backtrace_map->maps_.begin(), maps.begin(), maps.end());
149 std::sort(backtrace_map->maps_.begin(), backtrace_map->maps_.end(),
150 [](const backtrace_map_t& map1, const backtrace_map_t& map2) {
151 return map1.start < map2.start;
152 });
153 return backtrace_map;
154}