blob: 82a4085ed7f2febc65c05855c9f98bf495009520 [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
Christopher Ferrisdf290612014-01-22 19:21:07 -080024#include <backtrace/backtrace_constants.h>
Christopher Ferris46756822014-01-14 20:16:30 -080025#include <backtrace/BacktraceMap.h>
26#include <log/log.h>
27
Christopher Ferrisdf290612014-01-22 19:21:07 -080028#include "thread_utils.h"
29#include "BacktraceImpl.h"
30
Christopher Ferris46756822014-01-14 20:16:30 -080031BacktraceMap::BacktraceMap(pid_t pid) : pid_(pid) {
32 if (pid_ < 0) {
33 pid_ = getpid();
34 }
35}
36
37BacktraceMap::~BacktraceMap() {
38}
39
Christopher Ferris12385e32015-02-06 13:22:01 -080040void BacktraceMap::FillIn(uintptr_t addr, backtrace_map_t* map) {
Christopher Ferris46756822014-01-14 20:16:30 -080041 for (BacktraceMap::const_iterator it = begin();
42 it != end(); ++it) {
43 if (addr >= it->start && addr < it->end) {
Christopher Ferris12385e32015-02-06 13:22:01 -080044 *map = *it;
45 return;
Christopher Ferris46756822014-01-14 20:16:30 -080046 }
47 }
Christopher Ferris12385e32015-02-06 13:22:01 -080048 *map = {};
Christopher Ferris46756822014-01-14 20:16:30 -080049}
50
51bool BacktraceMap::ParseLine(const char* line, backtrace_map_t* map) {
52 unsigned long int start;
53 unsigned long int end;
54 char permissions[5];
55 int name_pos;
56
57#if defined(__APPLE__)
58// Mac OS vmmap(1) output:
59// __TEXT 0009f000-000a1000 [ 8K 8K] r-x/rwx SM=COW /Volumes/android/dalvik-dev/out/host/darwin-x86/bin/libcorkscrew_test\n
60// 012345678901234567890123456789012345678901234567890123456789
61// 0 1 2 3 4 5
62 if (sscanf(line, "%*21c %lx-%lx [%*13c] %3c/%*3c SM=%*3c %n",
63 &start, &end, permissions, &name_pos) != 3) {
64#else
65// Linux /proc/<pid>/maps lines:
66// 6f000000-6f01e000 rwxp 00000000 00:0c 16389419 /system/lib/libcomposer.so\n
67// 012345678901234567890123456789012345678901234567890123456789
68// 0 1 2 3 4 5
69 if (sscanf(line, "%lx-%lx %4s %*x %*x:%*x %*d%n",
70 &start, &end, permissions, &name_pos) != 3) {
71#endif
72 return false;
73 }
74
75 map->start = start;
76 map->end = end;
77 map->flags = PROT_NONE;
78 if (permissions[0] == 'r') {
79 map->flags |= PROT_READ;
80 }
81 if (permissions[1] == 'w') {
82 map->flags |= PROT_WRITE;
83 }
84 if (permissions[2] == 'x') {
85 map->flags |= PROT_EXEC;
86 }
87
88 while (isspace(line[name_pos])) {
89 name_pos += 1;
90 }
91 map->name = line+name_pos;
92 if (!map->name.empty() && map->name[map->name.length()-1] == '\n') {
93 map->name.erase(map->name.length()-1);
94 }
95
96 ALOGV("Parsed map: start=%p, end=%p, flags=%x, name=%s",
Colin Crossf4b0b792014-02-06 20:07:15 -080097 reinterpret_cast<void*>(map->start), reinterpret_cast<void*>(map->end),
98 map->flags, map->name.c_str());
Christopher Ferris46756822014-01-14 20:16:30 -080099 return true;
100}
101
102bool BacktraceMap::Build() {
103#if defined(__APPLE__)
104 char cmd[sizeof(pid_t)*3 + sizeof("vmmap -w -resident -submap -allSplitLibs -interleaved ") + 1];
105#else
106 char path[sizeof(pid_t)*3 + sizeof("/proc//maps") + 1];
107#endif
108 char line[1024];
109
110#if defined(__APPLE__)
111 // cmd is guaranteed to always be big enough to hold this string.
Christopher Ferrisb8c72952014-04-18 14:12:35 -0700112 snprintf(cmd, sizeof(cmd), "vmmap -w -resident -submap -allSplitLibs -interleaved %d", pid_);
Christopher Ferris46756822014-01-14 20:16:30 -0800113 FILE* fp = popen(cmd, "r");
114#else
115 // path is guaranteed to always be big enough to hold this string.
Christopher Ferrisb8c72952014-04-18 14:12:35 -0700116 snprintf(path, sizeof(path), "/proc/%d/maps", pid_);
Christopher Ferris46756822014-01-14 20:16:30 -0800117 FILE* fp = fopen(path, "r");
118#endif
119 if (fp == NULL) {
120 return false;
121 }
122
123 while(fgets(line, sizeof(line), fp)) {
124 backtrace_map_t map;
125 if (ParseLine(line, &map)) {
126 maps_.push_back(map);
127 }
128 }
129#if defined(__APPLE__)
130 pclose(fp);
131#else
132 fclose(fp);
133#endif
134
135 return true;
136}
Christopher Ferrisdf290612014-01-22 19:21:07 -0800137
138#if defined(__APPLE__)
139// Corkscrew and libunwind don't compile on the mac, so create a generic
140// map object.
Brian Carlstromb978a322014-08-05 00:15:49 -0700141BacktraceMap* BacktraceMap::Create(pid_t pid, bool uncached) {
Christopher Ferrisdf290612014-01-22 19:21:07 -0800142 BacktraceMap* map = new BacktraceMap(pid);
143 if (!map->Build()) {
144 delete map;
145 return NULL;
146 }
147 return map;
148}
149#endif