BacktraceMap.cpp revision 3a14004c7f521cf2ca6dfea182fa7441e77c97e7
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>
18ec2ff8c176d795656e69aecfce9650db40bef60bChih-Hung Hsieh#include <inttypes.h>
192c43cff01d1271be451671567955158629b23670Christopher Ferris#include <stdint.h>
2046756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#include <sys/types.h>
2146756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#include <unistd.h>
2246756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris
23df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris#include <backtrace/backtrace_constants.h>
2446756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#include <backtrace/BacktraceMap.h>
2546756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#include <log/log.h>
2646756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris
27df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris#include "thread_utils.h"
28df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris
2946756821c4fe238f12a6e5ea18c356398f8d8795Christopher FerrisBacktraceMap::BacktraceMap(pid_t pid) : pid_(pid) {
3046756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  if (pid_ < 0) {
3146756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris    pid_ = getpid();
3246756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  }
3346756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris}
3446756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris
3546756821c4fe238f12a6e5ea18c356398f8d8795Christopher FerrisBacktraceMap::~BacktraceMap() {
3646756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris}
3746756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris
3812385e3ad085aa1ac06c26529b32b688503a9fcfChristopher Ferrisvoid BacktraceMap::FillIn(uintptr_t addr, backtrace_map_t* map) {
393a14004c7f521cf2ca6dfea182fa7441e77c97e7Christopher Ferris  ScopedBacktraceMapIteratorLock lock(this);
403a14004c7f521cf2ca6dfea182fa7441e77c97e7Christopher Ferris  for (BacktraceMap::const_iterator it = begin(); it != end(); ++it) {
4146756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris    if (addr >= it->start && addr < it->end) {
4212385e3ad085aa1ac06c26529b32b688503a9fcfChristopher Ferris      *map = *it;
4312385e3ad085aa1ac06c26529b32b688503a9fcfChristopher Ferris      return;
4446756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris    }
4546756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  }
4612385e3ad085aa1ac06c26529b32b688503a9fcfChristopher Ferris  *map = {};
4746756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris}
4846756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris
4946756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferrisbool BacktraceMap::ParseLine(const char* line, backtrace_map_t* map) {
50ec2ff8c176d795656e69aecfce9650db40bef60bChih-Hung Hsieh  uint64_t start;
51ec2ff8c176d795656e69aecfce9650db40bef60bChih-Hung Hsieh  uint64_t end;
5246756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  char permissions[5];
5346756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  int name_pos;
5446756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris
5546756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#if defined(__APPLE__)
5646756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris// Mac OS vmmap(1) output:
5746756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris// __TEXT                 0009f000-000a1000 [    8K     8K] r-x/rwx SM=COW  /Volumes/android/dalvik-dev/out/host/darwin-x86/bin/libcorkscrew_test\n
5846756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris// 012345678901234567890123456789012345678901234567890123456789
5946756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris// 0         1         2         3         4         5
60ec2ff8c176d795656e69aecfce9650db40bef60bChih-Hung Hsieh  if (sscanf(line, "%*21c %" SCNx64 "-%" SCNx64 " [%*13c] %3c/%*3c SM=%*3c  %n",
6146756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris             &start, &end, permissions, &name_pos) != 3) {
6246756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#else
6346756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris// Linux /proc/<pid>/maps lines:
6446756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris// 6f000000-6f01e000 rwxp 00000000 00:0c 16389419   /system/lib/libcomposer.so\n
6546756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris// 012345678901234567890123456789012345678901234567890123456789
6646756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris// 0         1         2         3         4         5
67ec2ff8c176d795656e69aecfce9650db40bef60bChih-Hung Hsieh  if (sscanf(line, "%" SCNx64 "-%" SCNx64 " %4s %*x %*x:%*x %*d %n",
6846756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris             &start, &end, permissions, &name_pos) != 3) {
6946756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#endif
7046756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris    return false;
7146756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  }
7246756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris
7346756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  map->start = start;
7446756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  map->end = end;
7546756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  map->flags = PROT_NONE;
7646756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  if (permissions[0] == 'r') {
7746756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris    map->flags |= PROT_READ;
7846756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  }
7946756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  if (permissions[1] == 'w') {
8046756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris    map->flags |= PROT_WRITE;
8146756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  }
8246756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  if (permissions[2] == 'x') {
8346756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris    map->flags |= PROT_EXEC;
8446756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  }
8546756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris
8646756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  map->name = line+name_pos;
8746756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  if (!map->name.empty() && map->name[map->name.length()-1] == '\n') {
8846756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris    map->name.erase(map->name.length()-1);
8946756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  }
9046756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris
9146756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  ALOGV("Parsed map: start=%p, end=%p, flags=%x, name=%s",
92f4b0b7971c0333b7331c2f54384af5de0260ae75Colin Cross        reinterpret_cast<void*>(map->start), reinterpret_cast<void*>(map->end),
93f4b0b7971c0333b7331c2f54384af5de0260ae75Colin Cross        map->flags, map->name.c_str());
9446756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  return true;
9546756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris}
9646756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris
9746756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferrisbool BacktraceMap::Build() {
9846756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#if defined(__APPLE__)
9946756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  char cmd[sizeof(pid_t)*3 + sizeof("vmmap -w -resident -submap -allSplitLibs -interleaved ") + 1];
10046756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#else
10146756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  char path[sizeof(pid_t)*3 + sizeof("/proc//maps") + 1];
10246756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#endif
10346756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  char line[1024];
10446756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris
10546756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#if defined(__APPLE__)
10646756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  // cmd is guaranteed to always be big enough to hold this string.
107b8c72957f072a687a4fb99dd7f2423d3f86e70d2Christopher Ferris  snprintf(cmd, sizeof(cmd), "vmmap -w -resident -submap -allSplitLibs -interleaved %d", pid_);
10846756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  FILE* fp = popen(cmd, "r");
10946756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#else
11046756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  // path is guaranteed to always be big enough to hold this string.
111b8c72957f072a687a4fb99dd7f2423d3f86e70d2Christopher Ferris  snprintf(path, sizeof(path), "/proc/%d/maps", pid_);
11246756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  FILE* fp = fopen(path, "r");
11346756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#endif
1142c43cff01d1271be451671567955158629b23670Christopher Ferris  if (fp == nullptr) {
11546756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris    return false;
11646756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  }
11746756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris
11846756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  while(fgets(line, sizeof(line), fp)) {
11946756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris    backtrace_map_t map;
12046756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris    if (ParseLine(line, &map)) {
12146756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris      maps_.push_back(map);
12246756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris    }
12346756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  }
12446756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#if defined(__APPLE__)
12546756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  pclose(fp);
12646756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#else
12746756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  fclose(fp);
12846756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#endif
12946756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris
13046756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris  return true;
13146756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris}
132df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris
133df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris#if defined(__APPLE__)
134df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris// Corkscrew and libunwind don't compile on the mac, so create a generic
135df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris// map object.
1365b439eaf938aa27b7db04225694be7d2a25af477Colin CrossBacktraceMap* BacktraceMap::Create(pid_t pid, bool /*uncached*/) {
137df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris  BacktraceMap* map = new BacktraceMap(pid);
138df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris  if (!map->Build()) {
139df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris    delete map;
1402c43cff01d1271be451671567955158629b23670Christopher Ferris    return nullptr;
141df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris  }
142df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris  return map;
143df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris}
144df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris#endif
1459e402bb20cb868577e5588d8323363411655291bYabin Cui
1469e402bb20cb868577e5588d8323363411655291bYabin CuiBacktraceMap* BacktraceMap::Create(pid_t pid, const std::vector<backtrace_map_t>& maps) {
1479e402bb20cb868577e5588d8323363411655291bYabin Cui    BacktraceMap* backtrace_map = new BacktraceMap(pid);
1489e402bb20cb868577e5588d8323363411655291bYabin Cui    backtrace_map->maps_.insert(backtrace_map->maps_.begin(), maps.begin(), maps.end());
1499e402bb20cb868577e5588d8323363411655291bYabin Cui    std::sort(backtrace_map->maps_.begin(), backtrace_map->maps_.end(),
1509e402bb20cb868577e5588d8323363411655291bYabin Cui            [](const backtrace_map_t& map1, const backtrace_map_t& map2) {
1519e402bb20cb868577e5588d8323363411655291bYabin Cui              return map1.start < map2.start;
1529e402bb20cb868577e5588d8323363411655291bYabin Cui            });
1539e402bb20cb868577e5588d8323363411655291bYabin Cui    return backtrace_map;
1549e402bb20cb868577e5588d8323363411655291bYabin Cui}
155