BacktraceMap.cpp revision 2c43cff01d1271be451671567955158629b23670
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 6646756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris 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 while (isspace(line[name_pos])) { 8646756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris name_pos += 1; 8746756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris } 8846756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris map->name = line+name_pos; 8946756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris if (!map->name.empty() && map->name[map->name.length()-1] == '\n') { 9046756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris map->name.erase(map->name.length()-1); 9146756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris } 9246756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris 9346756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris ALOGV("Parsed map: start=%p, end=%p, flags=%x, name=%s", 94f4b0b7971c0333b7331c2f54384af5de0260ae75Colin Cross reinterpret_cast<void*>(map->start), reinterpret_cast<void*>(map->end), 95f4b0b7971c0333b7331c2f54384af5de0260ae75Colin Cross map->flags, map->name.c_str()); 9646756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris return true; 9746756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris} 9846756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris 9946756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferrisbool BacktraceMap::Build() { 10046756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#if defined(__APPLE__) 10146756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris char cmd[sizeof(pid_t)*3 + sizeof("vmmap -w -resident -submap -allSplitLibs -interleaved ") + 1]; 10246756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#else 10346756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris char path[sizeof(pid_t)*3 + sizeof("/proc//maps") + 1]; 10446756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#endif 10546756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris char line[1024]; 10646756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris 10746756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#if defined(__APPLE__) 10846756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris // cmd is guaranteed to always be big enough to hold this string. 109b8c72957f072a687a4fb99dd7f2423d3f86e70d2Christopher Ferris snprintf(cmd, sizeof(cmd), "vmmap -w -resident -submap -allSplitLibs -interleaved %d", pid_); 11046756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris FILE* fp = popen(cmd, "r"); 11146756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#else 11246756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris // path is guaranteed to always be big enough to hold this string. 113b8c72957f072a687a4fb99dd7f2423d3f86e70d2Christopher Ferris snprintf(path, sizeof(path), "/proc/%d/maps", pid_); 11446756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris FILE* fp = fopen(path, "r"); 11546756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#endif 1162c43cff01d1271be451671567955158629b23670Christopher Ferris if (fp == nullptr) { 11746756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris return false; 11846756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris } 11946756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris 12046756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris while(fgets(line, sizeof(line), fp)) { 12146756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris backtrace_map_t map; 12246756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris if (ParseLine(line, &map)) { 12346756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris maps_.push_back(map); 12446756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris } 12546756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris } 12646756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#if defined(__APPLE__) 12746756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris pclose(fp); 12846756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#else 12946756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris fclose(fp); 13046756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#endif 13146756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris 13246756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris return true; 13346756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris} 134df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris 135df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris#if defined(__APPLE__) 136df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris// Corkscrew and libunwind don't compile on the mac, so create a generic 137df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris// map object. 138b978a329efbe0e4489e8556f77bba689065f0499Brian CarlstromBacktraceMap* BacktraceMap::Create(pid_t pid, bool uncached) { 139df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris BacktraceMap* map = new BacktraceMap(pid); 140df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris if (!map->Build()) { 141df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris delete map; 1422c43cff01d1271be451671567955158629b23670Christopher Ferris return nullptr; 143df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris } 144df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris return map; 145df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris} 146df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris#endif 147