blob: 85f24361e1be5a0d18bc08fd1338c427af1ed154 [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>
Christopher Ferris2c43cff2015-03-26 19:18:36 -070018#include <stdint.h>
Christopher Ferris46756822014-01-14 20:16:30 -080019#include <sys/types.h>
20#include <unistd.h>
21
Christopher Ferrisdf290612014-01-22 19:21:07 -080022#include <backtrace/backtrace_constants.h>
Christopher Ferris46756822014-01-14 20:16:30 -080023#include <backtrace/BacktraceMap.h>
24#include <log/log.h>
25
Christopher Ferrisdf290612014-01-22 19:21:07 -080026#include "thread_utils.h"
Christopher Ferrisdf290612014-01-22 19:21:07 -080027
Christopher Ferris46756822014-01-14 20:16:30 -080028BacktraceMap::BacktraceMap(pid_t pid) : pid_(pid) {
29 if (pid_ < 0) {
30 pid_ = getpid();
31 }
32}
33
34BacktraceMap::~BacktraceMap() {
35}
36
Christopher Ferris12385e32015-02-06 13:22:01 -080037void BacktraceMap::FillIn(uintptr_t addr, backtrace_map_t* map) {
Christopher Ferris0c572d02016-06-15 15:49:50 -070038 ScopedBacktraceMapIteratorLock lock(this);
39 for (BacktraceMap::const_iterator it = begin(); it != end(); ++it) {
Christopher Ferris46756822014-01-14 20:16:30 -080040 if (addr >= it->start && addr < it->end) {
Christopher Ferris12385e32015-02-06 13:22:01 -080041 *map = *it;
42 return;
Christopher Ferris46756822014-01-14 20:16:30 -080043 }
44 }
Christopher Ferris12385e32015-02-06 13:22:01 -080045 *map = {};
Christopher Ferris46756822014-01-14 20:16:30 -080046}
47
48bool BacktraceMap::ParseLine(const char* line, backtrace_map_t* map) {
49 unsigned long int start;
50 unsigned long int end;
51 char permissions[5];
52 int name_pos;
53
54#if defined(__APPLE__)
55// Mac OS vmmap(1) output:
56// __TEXT 0009f000-000a1000 [ 8K 8K] r-x/rwx SM=COW /Volumes/android/dalvik-dev/out/host/darwin-x86/bin/libcorkscrew_test\n
57// 012345678901234567890123456789012345678901234567890123456789
58// 0 1 2 3 4 5
59 if (sscanf(line, "%*21c %lx-%lx [%*13c] %3c/%*3c SM=%*3c %n",
60 &start, &end, permissions, &name_pos) != 3) {
61#else
62// Linux /proc/<pid>/maps lines:
63// 6f000000-6f01e000 rwxp 00000000 00:0c 16389419 /system/lib/libcomposer.so\n
64// 012345678901234567890123456789012345678901234567890123456789
65// 0 1 2 3 4 5
Elliott Hughesfe3593d2015-09-22 17:26:24 -070066 if (sscanf(line, "%lx-%lx %4s %*x %*x:%*x %*d %n",
Christopher Ferris46756822014-01-14 20:16:30 -080067 &start, &end, permissions, &name_pos) != 3) {
68#endif
69 return false;
70 }
71
72 map->start = start;
73 map->end = end;
74 map->flags = PROT_NONE;
75 if (permissions[0] == 'r') {
76 map->flags |= PROT_READ;
77 }
78 if (permissions[1] == 'w') {
79 map->flags |= PROT_WRITE;
80 }
81 if (permissions[2] == 'x') {
82 map->flags |= PROT_EXEC;
83 }
84
Christopher Ferris46756822014-01-14 20:16:30 -080085 map->name = line+name_pos;
86 if (!map->name.empty() && map->name[map->name.length()-1] == '\n') {
87 map->name.erase(map->name.length()-1);
88 }
89
90 ALOGV("Parsed map: start=%p, end=%p, flags=%x, name=%s",
Colin Crossf4b0b792014-02-06 20:07:15 -080091 reinterpret_cast<void*>(map->start), reinterpret_cast<void*>(map->end),
92 map->flags, map->name.c_str());
Christopher Ferris46756822014-01-14 20:16:30 -080093 return true;
94}
95
96bool BacktraceMap::Build() {
97#if defined(__APPLE__)
98 char cmd[sizeof(pid_t)*3 + sizeof("vmmap -w -resident -submap -allSplitLibs -interleaved ") + 1];
99#else
100 char path[sizeof(pid_t)*3 + sizeof("/proc//maps") + 1];
101#endif
102 char line[1024];
103
104#if defined(__APPLE__)
105 // cmd is guaranteed to always be big enough to hold this string.
Christopher Ferrisb8c72952014-04-18 14:12:35 -0700106 snprintf(cmd, sizeof(cmd), "vmmap -w -resident -submap -allSplitLibs -interleaved %d", pid_);
Christopher Ferris46756822014-01-14 20:16:30 -0800107 FILE* fp = popen(cmd, "r");
108#else
109 // path is guaranteed to always be big enough to hold this string.
Christopher Ferrisb8c72952014-04-18 14:12:35 -0700110 snprintf(path, sizeof(path), "/proc/%d/maps", pid_);
Christopher Ferris46756822014-01-14 20:16:30 -0800111 FILE* fp = fopen(path, "r");
112#endif
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700113 if (fp == nullptr) {
Christopher Ferris46756822014-01-14 20:16:30 -0800114 return false;
115 }
116
117 while(fgets(line, sizeof(line), fp)) {
118 backtrace_map_t map;
119 if (ParseLine(line, &map)) {
120 maps_.push_back(map);
121 }
122 }
123#if defined(__APPLE__)
124 pclose(fp);
125#else
126 fclose(fp);
127#endif
128
129 return true;
130}
Christopher Ferrisdf290612014-01-22 19:21:07 -0800131
132#if defined(__APPLE__)
133// Corkscrew and libunwind don't compile on the mac, so create a generic
134// map object.
Colin Cross5b439ea2015-04-30 15:11:34 -0700135BacktraceMap* BacktraceMap::Create(pid_t pid, bool /*uncached*/) {
Christopher Ferrisdf290612014-01-22 19:21:07 -0800136 BacktraceMap* map = new BacktraceMap(pid);
137 if (!map->Build()) {
138 delete map;
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700139 return nullptr;
Christopher Ferrisdf290612014-01-22 19:21:07 -0800140 }
141 return map;
142}
143#endif
Yabin Cui9e402bb2015-09-22 04:46:57 +0000144
145BacktraceMap* BacktraceMap::Create(pid_t pid, const std::vector<backtrace_map_t>& maps) {
146 BacktraceMap* backtrace_map = new BacktraceMap(pid);
147 backtrace_map->maps_.insert(backtrace_map->maps_.begin(), maps.begin(), maps.end());
148 std::sort(backtrace_map->maps_.begin(), backtrace_map->maps_.end(),
149 [](const backtrace_map_t& map1, const backtrace_map_t& map2) {
150 return map1.start < map2.start;
151 });
152 return backtrace_map;
153}