BacktraceMap.cpp revision 2c43cff01d1271be451671567955158629b23670
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#include <ctype.h>
18#include <stdint.h>
19#include <sys/types.h>
20#include <unistd.h>
21
22#include <backtrace/backtrace_constants.h>
23#include <backtrace/BacktraceMap.h>
24#include <log/log.h>
25
26#include "thread_utils.h"
27
28BacktraceMap::BacktraceMap(pid_t pid) : pid_(pid) {
29  if (pid_ < 0) {
30    pid_ = getpid();
31  }
32}
33
34BacktraceMap::~BacktraceMap() {
35}
36
37void BacktraceMap::FillIn(uintptr_t addr, backtrace_map_t* map) {
38  for (BacktraceMap::const_iterator it = begin();
39       it != end(); ++it) {
40    if (addr >= it->start && addr < it->end) {
41      *map = *it;
42      return;
43    }
44  }
45  *map = {};
46}
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
66  if (sscanf(line, "%lx-%lx %4s %*x %*x:%*x %*d%n",
67             &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
85  while (isspace(line[name_pos])) {
86    name_pos += 1;
87  }
88  map->name = line+name_pos;
89  if (!map->name.empty() && map->name[map->name.length()-1] == '\n') {
90    map->name.erase(map->name.length()-1);
91  }
92
93  ALOGV("Parsed map: start=%p, end=%p, flags=%x, name=%s",
94        reinterpret_cast<void*>(map->start), reinterpret_cast<void*>(map->end),
95        map->flags, map->name.c_str());
96  return true;
97}
98
99bool BacktraceMap::Build() {
100#if defined(__APPLE__)
101  char cmd[sizeof(pid_t)*3 + sizeof("vmmap -w -resident -submap -allSplitLibs -interleaved ") + 1];
102#else
103  char path[sizeof(pid_t)*3 + sizeof("/proc//maps") + 1];
104#endif
105  char line[1024];
106
107#if defined(__APPLE__)
108  // cmd is guaranteed to always be big enough to hold this string.
109  snprintf(cmd, sizeof(cmd), "vmmap -w -resident -submap -allSplitLibs -interleaved %d", pid_);
110  FILE* fp = popen(cmd, "r");
111#else
112  // path is guaranteed to always be big enough to hold this string.
113  snprintf(path, sizeof(path), "/proc/%d/maps", pid_);
114  FILE* fp = fopen(path, "r");
115#endif
116  if (fp == nullptr) {
117    return false;
118  }
119
120  while(fgets(line, sizeof(line), fp)) {
121    backtrace_map_t map;
122    if (ParseLine(line, &map)) {
123      maps_.push_back(map);
124    }
125  }
126#if defined(__APPLE__)
127  pclose(fp);
128#else
129  fclose(fp);
130#endif
131
132  return true;
133}
134
135#if defined(__APPLE__)
136// Corkscrew and libunwind don't compile on the mac, so create a generic
137// map object.
138BacktraceMap* BacktraceMap::Create(pid_t pid, bool uncached) {
139  BacktraceMap* map = new BacktraceMap(pid);
140  if (!map->Build()) {
141    delete map;
142    return nullptr;
143  }
144  return map;
145}
146#endif
147