2014-01-15 05:16:30 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-10-17 23:28:00 +02:00
|
|
|
#define LOG_TAG "backtrace-map"
|
|
|
|
|
2014-01-15 05:16:30 +01:00
|
|
|
#include <ctype.h>
|
2016-04-18 23:45:46 +02:00
|
|
|
#include <inttypes.h>
|
2015-03-27 03:18:36 +01:00
|
|
|
#include <stdint.h>
|
2014-01-15 05:16:30 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2017-01-10 22:19:54 +01:00
|
|
|
#include <log/log.h>
|
|
|
|
|
2018-02-15 18:18:21 +01:00
|
|
|
#include <android-base/stringprintf.h>
|
|
|
|
#include <backtrace/Backtrace.h>
|
2014-01-15 05:16:30 +01:00
|
|
|
#include <backtrace/BacktraceMap.h>
|
2018-02-15 18:18:21 +01:00
|
|
|
#include <backtrace/backtrace_constants.h>
|
2018-05-11 02:19:12 +02:00
|
|
|
#if defined(__linux__)
|
|
|
|
#include <procinfo/process_map.h>
|
|
|
|
#endif
|
2014-01-15 05:16:30 +01:00
|
|
|
|
2018-02-15 18:18:21 +01:00
|
|
|
using android::base::StringPrintf;
|
|
|
|
|
|
|
|
std::string backtrace_map_t::Name() const {
|
|
|
|
if (!name.empty()) return name;
|
|
|
|
if (start == 0 && end == 0) return "";
|
|
|
|
return StringPrintf("<anonymous:%" PRIPTR ">", start);
|
|
|
|
}
|
|
|
|
|
2014-01-15 05:16:30 +01:00
|
|
|
BacktraceMap::BacktraceMap(pid_t pid) : pid_(pid) {
|
|
|
|
if (pid_ < 0) {
|
|
|
|
pid_ = getpid();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-31 02:43:22 +01:00
|
|
|
BacktraceMap::~BacktraceMap() {}
|
2014-01-15 05:16:30 +01:00
|
|
|
|
2018-01-18 20:15:49 +01:00
|
|
|
void BacktraceMap::FillIn(uint64_t addr, backtrace_map_t* map) {
|
2016-06-16 00:49:50 +02:00
|
|
|
ScopedBacktraceMapIteratorLock lock(this);
|
2017-12-02 06:37:37 +01:00
|
|
|
for (auto it = begin(); it != end(); ++it) {
|
|
|
|
const backtrace_map_t* entry = *it;
|
|
|
|
if (addr >= entry->start && addr < entry->end) {
|
|
|
|
*map = *entry;
|
2015-02-06 22:22:01 +01:00
|
|
|
return;
|
2014-01-15 05:16:30 +01:00
|
|
|
}
|
|
|
|
}
|
2015-02-06 22:22:01 +01:00
|
|
|
*map = {};
|
2014-01-15 05:16:30 +01:00
|
|
|
}
|
|
|
|
|
2018-05-11 02:19:12 +02:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
static bool ParseLine(const char* line, backtrace_map_t* map) {
|
2016-04-18 23:45:46 +02:00
|
|
|
uint64_t start;
|
|
|
|
uint64_t end;
|
2014-01-15 05:16:30 +01:00
|
|
|
char permissions[5];
|
|
|
|
int name_pos;
|
|
|
|
|
2019-01-31 02:43:22 +01:00
|
|
|
// Mac OS vmmap(1) output:
|
|
|
|
// __TEXT 0009f000-000a1000 [ 8K 8K] r-x/rwx SM=COW
|
|
|
|
// /Volumes/android/dalvik-dev/out/host/darwin-x86/bin/libcorkscrew_test\n
|
|
|
|
// 012345678901234567890123456789012345678901234567890123456789
|
|
|
|
// 0 1 2 3 4 5
|
|
|
|
if (sscanf(line, "%*21c %" SCNx64 "-%" SCNx64 " [%*13c] %3c/%*3c SM=%*3c %n", &start, &end,
|
|
|
|
permissions, &name_pos) != 3) {
|
2014-01-15 05:16:30 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
map->start = start;
|
|
|
|
map->end = end;
|
|
|
|
map->flags = PROT_NONE;
|
|
|
|
if (permissions[0] == 'r') {
|
|
|
|
map->flags |= PROT_READ;
|
|
|
|
}
|
|
|
|
if (permissions[1] == 'w') {
|
|
|
|
map->flags |= PROT_WRITE;
|
|
|
|
}
|
|
|
|
if (permissions[2] == 'x') {
|
|
|
|
map->flags |= PROT_EXEC;
|
|
|
|
}
|
|
|
|
|
2019-01-31 02:43:22 +01:00
|
|
|
map->name = line + name_pos;
|
|
|
|
if (!map->name.empty() && map->name[map->name.length() - 1] == '\n') {
|
|
|
|
map->name.erase(map->name.length() - 1);
|
2014-01-15 05:16:30 +01:00
|
|
|
}
|
|
|
|
|
2019-01-31 02:43:22 +01:00
|
|
|
ALOGV("Parsed map: start=%p, end=%p, flags=%x, name=%s", reinterpret_cast<void*>(map->start),
|
|
|
|
reinterpret_cast<void*>(map->end), map->flags, map->name.c_str());
|
2014-01-15 05:16:30 +01:00
|
|
|
return true;
|
|
|
|
}
|
2018-05-11 02:19:12 +02:00
|
|
|
#endif // defined(__APPLE__)
|
2014-01-15 05:16:30 +01:00
|
|
|
|
|
|
|
bool BacktraceMap::Build() {
|
|
|
|
#if defined(__APPLE__)
|
2019-01-31 02:43:22 +01:00
|
|
|
char
|
|
|
|
cmd[sizeof(pid_t) * 3 + sizeof("vmmap -w -resident -submap -allSplitLibs -interleaved ") + 1];
|
2014-01-15 05:16:30 +01:00
|
|
|
char line[1024];
|
|
|
|
// cmd is guaranteed to always be big enough to hold this string.
|
2014-04-18 23:12:35 +02:00
|
|
|
snprintf(cmd, sizeof(cmd), "vmmap -w -resident -submap -allSplitLibs -interleaved %d", pid_);
|
2014-01-15 05:16:30 +01:00
|
|
|
FILE* fp = popen(cmd, "r");
|
2015-03-27 03:18:36 +01:00
|
|
|
if (fp == nullptr) {
|
2014-01-15 05:16:30 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-31 02:43:22 +01:00
|
|
|
while (fgets(line, sizeof(line), fp)) {
|
2014-01-15 05:16:30 +01:00
|
|
|
backtrace_map_t map;
|
|
|
|
if (ParseLine(line, &map)) {
|
|
|
|
maps_.push_back(map);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pclose(fp);
|
2018-05-11 02:19:12 +02:00
|
|
|
return true;
|
2014-01-15 05:16:30 +01:00
|
|
|
#else
|
2018-05-11 02:19:12 +02:00
|
|
|
return android::procinfo::ReadProcessMaps(
|
2019-01-31 02:43:22 +01:00
|
|
|
pid_, [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t, ino_t, const char* name) {
|
2018-05-11 02:19:12 +02:00
|
|
|
maps_.resize(maps_.size() + 1);
|
|
|
|
backtrace_map_t& map = maps_.back();
|
|
|
|
map.start = start;
|
|
|
|
map.end = end;
|
|
|
|
map.flags = flags;
|
|
|
|
map.name = name;
|
|
|
|
});
|
2014-01-15 05:16:30 +01:00
|
|
|
#endif
|
|
|
|
}
|
2014-01-23 04:21:07 +01:00
|
|
|
|
|
|
|
#if defined(__APPLE__)
|
|
|
|
// Corkscrew and libunwind don't compile on the mac, so create a generic
|
|
|
|
// map object.
|
2015-05-01 00:11:34 +02:00
|
|
|
BacktraceMap* BacktraceMap::Create(pid_t pid, bool /*uncached*/) {
|
2014-01-23 04:21:07 +01:00
|
|
|
BacktraceMap* map = new BacktraceMap(pid);
|
|
|
|
if (!map->Build()) {
|
|
|
|
delete map;
|
2015-03-27 03:18:36 +01:00
|
|
|
return nullptr;
|
2014-01-23 04:21:07 +01:00
|
|
|
}
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
#endif
|