Home | History | Annotate | Download | only in libbacktrace
      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 
     32 #include "thread_utils.h"
     33 
     34 using android::base::StringPrintf;
     35 
     36 std::string backtrace_map_t::Name() const {
     37   if (!name.empty()) return name;
     38   if (start == 0 && end == 0) return "";
     39   return StringPrintf("<anonymous:%" PRIPTR ">", start);
     40 }
     41 
     42 BacktraceMap::BacktraceMap(pid_t pid) : pid_(pid) {
     43   if (pid_ < 0) {
     44     pid_ = getpid();
     45   }
     46 }
     47 
     48 BacktraceMap::~BacktraceMap() {
     49 }
     50 
     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 bool BacktraceMap::ParseLine(const char* line, backtrace_map_t* map) {
     64   uint64_t start;
     65   uint64_t end;
     66   char permissions[5];
     67   int name_pos;
     68 
     69 #if defined(__APPLE__)
     70 // Mac OS vmmap(1) output:
     71 // __TEXT                 0009f000-000a1000 [    8K     8K] r-x/rwx SM=COW  /Volumes/android/dalvik-dev/out/host/darwin-x86/bin/libcorkscrew_test\n
     72 // 012345678901234567890123456789012345678901234567890123456789
     73 // 0         1         2         3         4         5
     74   if (sscanf(line, "%*21c %" SCNx64 "-%" SCNx64 " [%*13c] %3c/%*3c SM=%*3c  %n",
     75              &start, &end, permissions, &name_pos) != 3) {
     76 #else
     77 // Linux /proc/<pid>/maps lines:
     78 // 6f000000-6f01e000 rwxp 00000000 00:0c 16389419   /system/lib/libcomposer.so\n
     79 // 012345678901234567890123456789012345678901234567890123456789
     80 // 0         1         2         3         4         5
     81   if (sscanf(line, "%" SCNx64 "-%" SCNx64 " %4s %*x %*x:%*x %*d %n",
     82              &start, &end, permissions, &name_pos) != 3) {
     83 #endif
     84     return false;
     85   }
     86 
     87   map->start = start;
     88   map->end = end;
     89   map->flags = PROT_NONE;
     90   if (permissions[0] == 'r') {
     91     map->flags |= PROT_READ;
     92   }
     93   if (permissions[1] == 'w') {
     94     map->flags |= PROT_WRITE;
     95   }
     96   if (permissions[2] == 'x') {
     97     map->flags |= PROT_EXEC;
     98   }
     99 
    100   map->name = line+name_pos;
    101   if (!map->name.empty() && map->name[map->name.length()-1] == '\n') {
    102     map->name.erase(map->name.length()-1);
    103   }
    104 
    105   ALOGV("Parsed map: start=%p, end=%p, flags=%x, name=%s",
    106         reinterpret_cast<void*>(map->start), reinterpret_cast<void*>(map->end),
    107         map->flags, map->name.c_str());
    108   return true;
    109 }
    110 
    111 bool BacktraceMap::Build() {
    112 #if defined(__APPLE__)
    113   char cmd[sizeof(pid_t)*3 + sizeof("vmmap -w -resident -submap -allSplitLibs -interleaved ") + 1];
    114 #else
    115   char path[sizeof(pid_t)*3 + sizeof("/proc//maps") + 1];
    116 #endif
    117   char line[1024];
    118 
    119 #if defined(__APPLE__)
    120   // cmd is guaranteed to always be big enough to hold this string.
    121   snprintf(cmd, sizeof(cmd), "vmmap -w -resident -submap -allSplitLibs -interleaved %d", pid_);
    122   FILE* fp = popen(cmd, "r");
    123 #else
    124   // path is guaranteed to always be big enough to hold this string.
    125   snprintf(path, sizeof(path), "/proc/%d/maps", pid_);
    126   FILE* fp = fopen(path, "r");
    127 #endif
    128   if (fp == nullptr) {
    129     return false;
    130   }
    131 
    132   while(fgets(line, sizeof(line), fp)) {
    133     backtrace_map_t map;
    134     if (ParseLine(line, &map)) {
    135       maps_.push_back(map);
    136     }
    137   }
    138 #if defined(__APPLE__)
    139   pclose(fp);
    140 #else
    141   fclose(fp);
    142 #endif
    143 
    144   return true;
    145 }
    146 
    147 #if defined(__APPLE__)
    148 // Corkscrew and libunwind don't compile on the mac, so create a generic
    149 // map object.
    150 BacktraceMap* BacktraceMap::Create(pid_t pid, bool /*uncached*/) {
    151   BacktraceMap* map = new BacktraceMap(pid);
    152   if (!map->Build()) {
    153     delete map;
    154     return nullptr;
    155   }
    156   return map;
    157 }
    158 #endif
    159