• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 #define LOG_TAG "backtrace-map"
18 
19 #include <ctype.h>
20 #include <inttypes.h>
21 #include <stdint.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24 
25 #include <log/log.h>
26 
27 #include <android-base/stringprintf.h>
28 #include <backtrace/Backtrace.h>
29 #include <backtrace/BacktraceMap.h>
30 #include <backtrace/backtrace_constants.h>
31 #if defined(__linux__)
32 #include <procinfo/process_map.h>
33 #endif
34 
35 using android::base::StringPrintf;
36 
Name() const37 std::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 
BacktraceMap(pid_t pid)43 BacktraceMap::BacktraceMap(pid_t pid) : pid_(pid) {
44   if (pid_ < 0) {
45     pid_ = getpid();
46   }
47 }
48 
~BacktraceMap()49 BacktraceMap::~BacktraceMap() {}
50 
FillIn(uint64_t addr,backtrace_map_t * map)51 void BacktraceMap::FillIn(uint64_t addr, backtrace_map_t* map) {
52   ScopedBacktraceMapIteratorLock lock(this);
53   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;
57       return;
58     }
59   }
60   *map = {};
61 }
62 
63 #if defined(__APPLE__)
ParseLine(const char * line,backtrace_map_t * map)64 static bool ParseLine(const char* line, backtrace_map_t* map) {
65   uint64_t start;
66   uint64_t end;
67   char permissions[5];
68   int name_pos;
69 
70   // 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) {
77     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 
93   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);
96   }
97 
98   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());
100   return true;
101 }
102 #endif  // defined(__APPLE__)
103 
Build()104 bool BacktraceMap::Build() {
105 #if defined(__APPLE__)
106   char
107       cmd[sizeof(pid_t) * 3 + sizeof("vmmap -w -resident -submap -allSplitLibs -interleaved ") + 1];
108   char line[1024];
109   // cmd is guaranteed to always be big enough to hold this string.
110   snprintf(cmd, sizeof(cmd), "vmmap -w -resident -submap -allSplitLibs -interleaved %d", pid_);
111   FILE* fp = popen(cmd, "r");
112   if (fp == nullptr) {
113     return false;
114   }
115 
116   while (fgets(line, sizeof(line), fp)) {
117     backtrace_map_t map;
118     if (ParseLine(line, &map)) {
119       maps_.push_back(map);
120     }
121   }
122   pclose(fp);
123   return true;
124 #else
125   return android::procinfo::ReadProcessMaps(
126       pid_, [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t, ino_t, const char* name) {
127         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
135 }
136 
137 #if defined(__APPLE__)
138 // Corkscrew and libunwind don't compile on the mac, so create a generic
139 // map object.
Create(pid_t pid,bool)140 BacktraceMap* BacktraceMap::Create(pid_t pid, bool /*uncached*/) {
141   BacktraceMap* map = new BacktraceMap(pid);
142   if (!map->Build()) {
143     delete map;
144     return nullptr;
145   }
146   return map;
147 }
148 #endif
149