146756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris/*
246756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris * Copyright (C) 2014 The Android Open Source Project
346756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris *
446756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris * Licensed under the Apache License, Version 2.0 (the "License");
546756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris * you may not use this file except in compliance with the License.
646756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris * You may obtain a copy of the License at
746756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris *
846756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris *      http://www.apache.org/licenses/LICENSE-2.0
946756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris *
1046756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris * Unless required by applicable law or agreed to in writing, software
1146756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris * distributed under the License is distributed on an "AS IS" BASIS,
1246756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1346756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris * See the License for the specific language governing permissions and
1446756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris * limitations under the License.
1546756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris */
1646756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris
1746756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#include <ctype.h>
182c43cff01d1271be451671567955158629b23670Christopher Ferris#include <stdint.h>
1946756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#include <sys/types.h>
2046756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#include <unistd.h>
2146756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris
22df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris#include <backtrace/backtrace_constants.h>
2346756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#include <backtrace/BacktraceMap.h>
2446756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#include <log/log.h>
2546756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris
26df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris#include "thread_utils.h"
27df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris
2846756821c4fe238f12a6e5ea18c356398f8d8795Christopher FerrisBacktraceMap::BacktraceMap(pid_t pid) : pid_(pid) {
2946756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  if (pid_ < 0) {
3046756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris    pid_ = getpid();
3146756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  }
3246756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris}
3346756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris
3446756821c4fe238f12a6e5ea18c356398f8d8795Christopher FerrisBacktraceMap::~BacktraceMap() {
3546756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris}
3646756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris
3712385e3ad085aa1ac06c26529b32b688503a9fcfChristopher Ferrisvoid BacktraceMap::FillIn(uintptr_t addr, backtrace_map_t* map) {
3846756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  for (BacktraceMap::const_iterator it = begin();
3946756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris       it != end(); ++it) {
4046756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris    if (addr >= it->start && addr < it->end) {
4112385e3ad085aa1ac06c26529b32b688503a9fcfChristopher Ferris      *map = *it;
4212385e3ad085aa1ac06c26529b32b688503a9fcfChristopher Ferris      return;
4346756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris    }
4446756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  }
4512385e3ad085aa1ac06c26529b32b688503a9fcfChristopher Ferris  *map = {};
4646756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris}
4746756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris
4846756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferrisbool BacktraceMap::ParseLine(const char* line, backtrace_map_t* map) {
4946756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  unsigned long int start;
5046756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  unsigned long int end;
5146756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  char permissions[5];
5246756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  int name_pos;
5346756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris
5446756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#if defined(__APPLE__)
5546756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris// Mac OS vmmap(1) output:
5646756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris// __TEXT                 0009f000-000a1000 [    8K     8K] r-x/rwx SM=COW  /Volumes/android/dalvik-dev/out/host/darwin-x86/bin/libcorkscrew_test\n
5746756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris// 012345678901234567890123456789012345678901234567890123456789
5846756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris// 0         1         2         3         4         5
5946756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  if (sscanf(line, "%*21c %lx-%lx [%*13c] %3c/%*3c SM=%*3c  %n",
6046756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris             &start, &end, permissions, &name_pos) != 3) {
6146756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#else
6246756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris// Linux /proc/<pid>/maps lines:
6346756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris// 6f000000-6f01e000 rwxp 00000000 00:0c 16389419   /system/lib/libcomposer.so\n
6446756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris// 012345678901234567890123456789012345678901234567890123456789
6546756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris// 0         1         2         3         4         5
66fe3593dc2af46fdaf9c9a710133fce3e4a219ccaElliott Hughes  if (sscanf(line, "%lx-%lx %4s %*x %*x:%*x %*d %n",
6746756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris             &start, &end, permissions, &name_pos) != 3) {
6846756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#endif
6946756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris    return false;
7046756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  }
7146756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris
7246756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  map->start = start;
7346756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  map->end = end;
7446756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  map->flags = PROT_NONE;
7546756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  if (permissions[0] == 'r') {
7646756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris    map->flags |= PROT_READ;
7746756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  }
7846756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  if (permissions[1] == 'w') {
7946756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris    map->flags |= PROT_WRITE;
8046756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  }
8146756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  if (permissions[2] == 'x') {
8246756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris    map->flags |= PROT_EXEC;
8346756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  }
8446756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris
8546756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  map->name = line+name_pos;
8646756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  if (!map->name.empty() && map->name[map->name.length()-1] == '\n') {
8746756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris    map->name.erase(map->name.length()-1);
8846756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  }
8946756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris
9046756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  ALOGV("Parsed map: start=%p, end=%p, flags=%x, name=%s",
91f4b0b7971c0333b7331c2f54384af5de0260ae75Colin Cross        reinterpret_cast<void*>(map->start), reinterpret_cast<void*>(map->end),
92f4b0b7971c0333b7331c2f54384af5de0260ae75Colin Cross        map->flags, map->name.c_str());
9346756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  return true;
9446756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris}
9546756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris
9646756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferrisbool BacktraceMap::Build() {
9746756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#if defined(__APPLE__)
9846756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  char cmd[sizeof(pid_t)*3 + sizeof("vmmap -w -resident -submap -allSplitLibs -interleaved ") + 1];
9946756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#else
10046756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  char path[sizeof(pid_t)*3 + sizeof("/proc//maps") + 1];
10146756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#endif
10246756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  char line[1024];
10346756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris
10446756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#if defined(__APPLE__)
10546756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  // cmd is guaranteed to always be big enough to hold this string.
106b8c72957f072a687a4fb99dd7f2423d3f86e70d2Christopher Ferris  snprintf(cmd, sizeof(cmd), "vmmap -w -resident -submap -allSplitLibs -interleaved %d", pid_);
10746756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  FILE* fp = popen(cmd, "r");
10846756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#else
10946756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  // path is guaranteed to always be big enough to hold this string.
110b8c72957f072a687a4fb99dd7f2423d3f86e70d2Christopher Ferris  snprintf(path, sizeof(path), "/proc/%d/maps", pid_);
11146756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  FILE* fp = fopen(path, "r");
11246756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#endif
1132c43cff01d1271be451671567955158629b23670Christopher Ferris  if (fp == nullptr) {
11446756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris    return false;
11546756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  }
11646756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris
11746756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  while(fgets(line, sizeof(line), fp)) {
11846756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris    backtrace_map_t map;
11946756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris    if (ParseLine(line, &map)) {
12046756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris      maps_.push_back(map);
12146756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris    }
12246756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  }
12346756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#if defined(__APPLE__)
12446756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  pclose(fp);
12546756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#else
12646756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  fclose(fp);
12746756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#endif
12846756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris
12946756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  return true;
13046756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris}
131df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris
132df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris#if defined(__APPLE__)
133df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris// Corkscrew and libunwind don't compile on the mac, so create a generic
134df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris// map object.
1355b439eaf938aa27b7db04225694be7d2a25af477Colin CrossBacktraceMap* BacktraceMap::Create(pid_t pid, bool /*uncached*/) {
136df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris  BacktraceMap* map = new BacktraceMap(pid);
137df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris  if (!map->Build()) {
138df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris    delete map;
1392c43cff01d1271be451671567955158629b23670Christopher Ferris    return nullptr;
140df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris  }
141df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris  return map;
142df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris}
143df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris#endif
1449e402bb20cb868577e5588d8323363411655291bYabin Cui
1459e402bb20cb868577e5588d8323363411655291bYabin CuiBacktraceMap* BacktraceMap::Create(pid_t pid, const std::vector<backtrace_map_t>& maps) {
1469e402bb20cb868577e5588d8323363411655291bYabin Cui    BacktraceMap* backtrace_map = new BacktraceMap(pid);
1479e402bb20cb868577e5588d8323363411655291bYabin Cui    backtrace_map->maps_.insert(backtrace_map->maps_.begin(), maps.begin(), maps.end());
1489e402bb20cb868577e5588d8323363411655291bYabin Cui    std::sort(backtrace_map->maps_.begin(), backtrace_map->maps_.end(),
1499e402bb20cb868577e5588d8323363411655291bYabin Cui            [](const backtrace_map_t& map1, const backtrace_map_t& map2) {
1509e402bb20cb868577e5588d8323363411655291bYabin Cui              return map1.start < map2.start;
1519e402bb20cb868577e5588d8323363411655291bYabin Cui            });
1529e402bb20cb868577e5588d8323363411655291bYabin Cui    return backtrace_map;
1539e402bb20cb868577e5588d8323363411655291bYabin Cui}
154