2014-01-23 11:21:07 +08: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-06-16 06:49:50 +08:00
|
|
|
#include <pthread.h>
|
2015-03-27 10:18:36 +08:00
|
|
|
#include <stdint.h>
|
2014-05-22 09:21:02 +08:00
|
|
|
#include <stdlib.h>
|
2014-01-23 11:21:07 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <backtrace/BacktraceMap.h>
|
|
|
|
|
|
|
|
#include <libunwind.h>
|
|
|
|
|
2014-03-08 11:42:19 +08:00
|
|
|
#include "BacktraceLog.h"
|
2014-01-23 11:21:07 +08:00
|
|
|
#include "UnwindMap.h"
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// libunwind has a single shared address space for the current process
|
|
|
|
// aka local. If multiple maps are created for the current pid, then
|
|
|
|
// only update the local address space once, and keep a reference count
|
|
|
|
// of maps using the same map cursor.
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
UnwindMap::UnwindMap(pid_t pid) : BacktraceMap(pid) {
|
2016-02-06 03:07:12 +08:00
|
|
|
unw_map_cursor_clear(&map_cursor_);
|
|
|
|
}
|
|
|
|
|
|
|
|
UnwindMapRemote::UnwindMapRemote(pid_t pid) : UnwindMap(pid) {
|
2014-01-23 11:21:07 +08:00
|
|
|
}
|
|
|
|
|
2016-02-06 03:07:12 +08:00
|
|
|
UnwindMapRemote::~UnwindMapRemote() {
|
2014-03-08 11:42:19 +08:00
|
|
|
unw_map_cursor_destroy(&map_cursor_);
|
|
|
|
unw_map_cursor_clear(&map_cursor_);
|
2014-01-23 11:21:07 +08:00
|
|
|
}
|
|
|
|
|
2016-02-06 03:07:12 +08:00
|
|
|
bool UnwindMapRemote::GenerateMap() {
|
2014-01-23 11:21:07 +08:00
|
|
|
// Use the map_cursor information to construct the BacktraceMap data
|
|
|
|
// rather than reparsing /proc/self/maps.
|
|
|
|
unw_map_cursor_reset(&map_cursor_);
|
2014-03-08 11:42:19 +08:00
|
|
|
|
2014-01-23 11:21:07 +08:00
|
|
|
unw_map_t unw_map;
|
2014-03-08 11:42:19 +08:00
|
|
|
while (unw_map_cursor_get_next(&map_cursor_, &unw_map)) {
|
2014-01-23 11:21:07 +08:00
|
|
|
backtrace_map_t map;
|
|
|
|
|
|
|
|
map.start = unw_map.start;
|
|
|
|
map.end = unw_map.end;
|
2015-05-07 03:50:09 +08:00
|
|
|
map.offset = unw_map.offset;
|
2015-05-02 06:02:03 +08:00
|
|
|
map.load_base = unw_map.load_base;
|
2014-01-23 11:21:07 +08:00
|
|
|
map.flags = unw_map.flags;
|
|
|
|
map.name = unw_map.path;
|
|
|
|
|
|
|
|
// The maps are in descending order, but we want them in ascending order.
|
|
|
|
maps_.push_front(map);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-06 03:07:12 +08:00
|
|
|
bool UnwindMapRemote::Build() {
|
2014-03-08 11:42:19 +08:00
|
|
|
return (unw_map_cursor_create(&map_cursor_, pid_) == 0) && GenerateMap();
|
|
|
|
}
|
|
|
|
|
|
|
|
UnwindMapLocal::UnwindMapLocal() : UnwindMap(getpid()), map_created_(false) {
|
2016-06-16 06:49:50 +08:00
|
|
|
pthread_rwlock_init(&map_lock_, nullptr);
|
2014-03-08 11:42:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
UnwindMapLocal::~UnwindMapLocal() {
|
|
|
|
if (map_created_) {
|
|
|
|
unw_map_local_destroy();
|
|
|
|
unw_map_cursor_clear(&map_cursor_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UnwindMapLocal::GenerateMap() {
|
2016-06-16 06:49:50 +08:00
|
|
|
// Lock so that multiple threads cannot modify the maps data at the
|
|
|
|
// same time.
|
|
|
|
pthread_rwlock_wrlock(&map_lock_);
|
|
|
|
|
2014-03-08 11:42:19 +08:00
|
|
|
// It's possible for the map to be regenerated while this loop is occurring.
|
|
|
|
// If that happens, get the map again, but only try at most three times
|
|
|
|
// before giving up.
|
2016-06-16 06:49:50 +08:00
|
|
|
bool generated = false;
|
2014-03-08 11:42:19 +08:00
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
maps_.clear();
|
|
|
|
|
2016-02-06 03:07:12 +08:00
|
|
|
// Save the map data retrieved so we can tell if it changes.
|
2014-03-08 11:42:19 +08:00
|
|
|
unw_map_local_cursor_get(&map_cursor_);
|
|
|
|
|
|
|
|
unw_map_t unw_map;
|
|
|
|
int ret;
|
|
|
|
while ((ret = unw_map_local_cursor_get_next(&map_cursor_, &unw_map)) > 0) {
|
|
|
|
backtrace_map_t map;
|
|
|
|
|
|
|
|
map.start = unw_map.start;
|
|
|
|
map.end = unw_map.end;
|
2015-05-07 03:50:09 +08:00
|
|
|
map.offset = unw_map.offset;
|
2015-05-02 06:02:03 +08:00
|
|
|
map.load_base = unw_map.load_base;
|
2014-03-08 11:42:19 +08:00
|
|
|
map.flags = unw_map.flags;
|
|
|
|
map.name = unw_map.path;
|
|
|
|
|
|
|
|
free(unw_map.path);
|
|
|
|
|
|
|
|
// The maps are in descending order, but we want them in ascending order.
|
|
|
|
maps_.push_front(map);
|
|
|
|
}
|
|
|
|
// Check to see if the map changed while getting the data.
|
|
|
|
if (ret != -UNW_EINVAL) {
|
2016-06-16 06:49:50 +08:00
|
|
|
generated = true;
|
|
|
|
break;
|
2014-03-08 11:42:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-16 06:49:50 +08:00
|
|
|
pthread_rwlock_unlock(&map_lock_);
|
|
|
|
|
|
|
|
if (!generated) {
|
|
|
|
BACK_LOGW("Unable to generate the map.");
|
|
|
|
}
|
|
|
|
return generated;
|
2014-03-08 11:42:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool UnwindMapLocal::Build() {
|
|
|
|
return (map_created_ = (unw_map_local_create() == 0)) && GenerateMap();;
|
|
|
|
}
|
|
|
|
|
2015-02-07 05:22:01 +08:00
|
|
|
void UnwindMapLocal::FillIn(uintptr_t addr, backtrace_map_t* map) {
|
|
|
|
BacktraceMap::FillIn(addr, map);
|
|
|
|
if (!IsValid(*map)) {
|
2014-03-08 11:42:19 +08:00
|
|
|
// Check to see if the underlying map changed and regenerate the map
|
|
|
|
// if it did.
|
|
|
|
if (unw_map_local_cursor_valid(&map_cursor_) < 0) {
|
|
|
|
if (GenerateMap()) {
|
2015-02-07 05:22:01 +08:00
|
|
|
BacktraceMap::FillIn(addr, map);
|
2014-03-08 11:42:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-23 11:21:07 +08:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// BacktraceMap create function.
|
|
|
|
//-------------------------------------------------------------------------
|
2014-08-05 08:08:46 +08:00
|
|
|
BacktraceMap* BacktraceMap::Create(pid_t pid, bool uncached) {
|
2014-03-08 11:42:19 +08:00
|
|
|
BacktraceMap* map;
|
2014-08-05 08:08:46 +08:00
|
|
|
|
|
|
|
if (uncached) {
|
|
|
|
// Force use of the base class to parse the maps when this call is made.
|
|
|
|
map = new BacktraceMap(pid);
|
|
|
|
} else if (pid == getpid()) {
|
2014-03-08 11:42:19 +08:00
|
|
|
map = new UnwindMapLocal();
|
|
|
|
} else {
|
2016-02-06 03:07:12 +08:00
|
|
|
map = new UnwindMapRemote(pid);
|
2014-03-08 11:42:19 +08:00
|
|
|
}
|
2014-01-23 11:21:07 +08:00
|
|
|
if (!map->Build()) {
|
|
|
|
delete map;
|
2015-03-27 10:18:36 +08:00
|
|
|
return nullptr;
|
2014-01-23 11:21:07 +08:00
|
|
|
}
|
|
|
|
return map;
|
|
|
|
}
|