BacktraceMap.cpp revision b978a329efbe0e4489e8556f77bba689065f0499
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> 1846756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#include <sys/types.h> 1946756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#include <unistd.h> 2046756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris 2146756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#include <string> 2246756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#include <vector> 2346756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris 24df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris#include <backtrace/backtrace_constants.h> 2546756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#include <backtrace/BacktraceMap.h> 2646756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#include <log/log.h> 2746756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris 28df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris#include "thread_utils.h" 29df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris#include "BacktraceImpl.h" 30df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris 3146756821c4fe238f12a6e5ea18c356398f8d8795Christopher FerrisBacktraceMap::BacktraceMap(pid_t pid) : pid_(pid) { 3246756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris if (pid_ < 0) { 3346756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris pid_ = getpid(); 3446756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris } 3546756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris} 3646756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris 3746756821c4fe238f12a6e5ea18c356398f8d8795Christopher FerrisBacktraceMap::~BacktraceMap() { 3846756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris} 3946756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris 4046756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferrisconst backtrace_map_t* BacktraceMap::Find(uintptr_t addr) { 4146756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris for (BacktraceMap::const_iterator it = begin(); 4246756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris it != end(); ++it) { 4346756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris if (addr >= it->start && addr < it->end) { 4446756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris return &*it; 4546756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris } 4646756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris } 4746756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris return NULL; 4846756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris} 4946756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris 5046756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferrisbool BacktraceMap::ParseLine(const char* line, backtrace_map_t* map) { 5146756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris unsigned long int start; 5246756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris unsigned long int end; 5346756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris char permissions[5]; 5446756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris int name_pos; 5546756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris 5646756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#if defined(__APPLE__) 5746756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris// Mac OS vmmap(1) output: 5846756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris// __TEXT 0009f000-000a1000 [ 8K 8K] r-x/rwx SM=COW /Volumes/android/dalvik-dev/out/host/darwin-x86/bin/libcorkscrew_test\n 5946756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris// 012345678901234567890123456789012345678901234567890123456789 6046756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris// 0 1 2 3 4 5 6146756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris if (sscanf(line, "%*21c %lx-%lx [%*13c] %3c/%*3c SM=%*3c %n", 6246756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris &start, &end, permissions, &name_pos) != 3) { 6346756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#else 6446756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris// Linux /proc/<pid>/maps lines: 6546756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris// 6f000000-6f01e000 rwxp 00000000 00:0c 16389419 /system/lib/libcomposer.so\n 6646756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris// 012345678901234567890123456789012345678901234567890123456789 6746756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris// 0 1 2 3 4 5 6846756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris if (sscanf(line, "%lx-%lx %4s %*x %*x:%*x %*d%n", 6946756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris &start, &end, permissions, &name_pos) != 3) { 7046756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#endif 7146756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris return false; 7246756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris } 7346756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris 7446756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris map->start = start; 7546756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris map->end = end; 7646756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris map->flags = PROT_NONE; 7746756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris if (permissions[0] == 'r') { 7846756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris map->flags |= PROT_READ; 7946756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris } 8046756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris if (permissions[1] == 'w') { 8146756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris map->flags |= PROT_WRITE; 8246756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris } 8346756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris if (permissions[2] == 'x') { 8446756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris map->flags |= PROT_EXEC; 8546756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris } 8646756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris 8746756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris while (isspace(line[name_pos])) { 8846756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris name_pos += 1; 8946756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris } 9046756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris map->name = line+name_pos; 9146756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris if (!map->name.empty() && map->name[map->name.length()-1] == '\n') { 9246756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris map->name.erase(map->name.length()-1); 9346756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris } 9446756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris 9546756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris ALOGV("Parsed map: start=%p, end=%p, flags=%x, name=%s", 96f4b0b7971c0333b7331c2f54384af5de0260ae75Colin Cross reinterpret_cast<void*>(map->start), reinterpret_cast<void*>(map->end), 97f4b0b7971c0333b7331c2f54384af5de0260ae75Colin Cross map->flags, map->name.c_str()); 9846756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris return true; 9946756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris} 10046756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris 10146756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferrisbool BacktraceMap::Build() { 10246756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#if defined(__APPLE__) 10346756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris char cmd[sizeof(pid_t)*3 + sizeof("vmmap -w -resident -submap -allSplitLibs -interleaved ") + 1]; 10446756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#else 10546756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris char path[sizeof(pid_t)*3 + sizeof("/proc//maps") + 1]; 10646756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#endif 10746756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris char line[1024]; 10846756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris 10946756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#if defined(__APPLE__) 11046756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris // cmd is guaranteed to always be big enough to hold this string. 111b8c72957f072a687a4fb99dd7f2423d3f86e70d2Christopher Ferris snprintf(cmd, sizeof(cmd), "vmmap -w -resident -submap -allSplitLibs -interleaved %d", pid_); 11246756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris FILE* fp = popen(cmd, "r"); 11346756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#else 11446756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris // path is guaranteed to always be big enough to hold this string. 115b8c72957f072a687a4fb99dd7f2423d3f86e70d2Christopher Ferris snprintf(path, sizeof(path), "/proc/%d/maps", pid_); 11646756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris FILE* fp = fopen(path, "r"); 11746756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#endif 11846756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris if (fp == NULL) { 11946756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris return false; 12046756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris } 12146756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris 12246756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris while(fgets(line, sizeof(line), fp)) { 12346756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris backtrace_map_t map; 12446756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris if (ParseLine(line, &map)) { 12546756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris maps_.push_back(map); 12646756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris } 12746756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris } 12846756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#if defined(__APPLE__) 12946756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris pclose(fp); 13046756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#else 13146756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris fclose(fp); 13246756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris#endif 13346756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris 13446756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris return true; 13546756821c4fe238f12a6e5ea18c356398f8d8795Christopher Ferris} 136df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris 137df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris#if defined(__APPLE__) 138df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris// Corkscrew and libunwind don't compile on the mac, so create a generic 139df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris// map object. 140b978a329efbe0e4489e8556f77bba689065f0499Brian CarlstromBacktraceMap* BacktraceMap::Create(pid_t pid, bool uncached) { 141df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris BacktraceMap* map = new BacktraceMap(pid); 142df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris if (!map->Build()) { 143df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris delete map; 144df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris return NULL; 145df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris } 146df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris return map; 147df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris} 148df2906186b6952c57b1f662bfef0b65c9f8c2e0dChristopher Ferris#endif 149