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