blob: b3b4300a7a23e28d7a1d44958d4746c646cb7f5b [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>
18#include <sys/types.h>
19#include <unistd.h>
20
21#include <string>
22#include <vector>
23
24#include <backtrace/BacktraceMap.h>
25#include <log/log.h>
26
27BacktraceMap::BacktraceMap(pid_t pid) : pid_(pid) {
28 if (pid_ < 0) {
29 pid_ = getpid();
30 }
31}
32
33BacktraceMap::~BacktraceMap() {
34}
35
36const backtrace_map_t* BacktraceMap::Find(uintptr_t addr) {
37 for (BacktraceMap::const_iterator it = begin();
38 it != end(); ++it) {
39 if (addr >= it->start && addr < it->end) {
40 return &*it;
41 }
42 }
43 return NULL;
44}
45
46bool BacktraceMap::ParseLine(const char* line, backtrace_map_t* map) {
47 unsigned long int start;
48 unsigned long int end;
49 char permissions[5];
50 int name_pos;
51
52#if defined(__APPLE__)
53// Mac OS vmmap(1) output:
54// __TEXT 0009f000-000a1000 [ 8K 8K] r-x/rwx SM=COW /Volumes/android/dalvik-dev/out/host/darwin-x86/bin/libcorkscrew_test\n
55// 012345678901234567890123456789012345678901234567890123456789
56// 0 1 2 3 4 5
57 if (sscanf(line, "%*21c %lx-%lx [%*13c] %3c/%*3c SM=%*3c %n",
58 &start, &end, permissions, &name_pos) != 3) {
59#else
60// Linux /proc/<pid>/maps lines:
61// 6f000000-6f01e000 rwxp 00000000 00:0c 16389419 /system/lib/libcomposer.so\n
62// 012345678901234567890123456789012345678901234567890123456789
63// 0 1 2 3 4 5
64 if (sscanf(line, "%lx-%lx %4s %*x %*x:%*x %*d%n",
65 &start, &end, permissions, &name_pos) != 3) {
66#endif
67 return false;
68 }
69
70 map->start = start;
71 map->end = end;
72 map->flags = PROT_NONE;
73 if (permissions[0] == 'r') {
74 map->flags |= PROT_READ;
75 }
76 if (permissions[1] == 'w') {
77 map->flags |= PROT_WRITE;
78 }
79 if (permissions[2] == 'x') {
80 map->flags |= PROT_EXEC;
81 }
82
83 while (isspace(line[name_pos])) {
84 name_pos += 1;
85 }
86 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",
92 map->start, map->end, map->flags, map->name.c_str());
93 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.
106 sprintf(cmd, "vmmap -w -resident -submap -allSplitLibs -interleaved %d", pid);
107 FILE* fp = popen(cmd, "r");
108#else
109 // path is guaranteed to always be big enough to hold this string.
110 sprintf(path, "/proc/%d/maps", pid_);
111 FILE* fp = fopen(path, "r");
112#endif
113 if (fp == NULL) {
114 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}