123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- #include <pthread.h>
- #include <stdint.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <backtrace/BacktraceMap.h>
- #include <libunwind.h>
- #include "BacktraceLog.h"
- #include "UnwindMap.h"
- UnwindMap::UnwindMap(pid_t pid) : BacktraceMap(pid) {
- unw_map_cursor_clear(&map_cursor_);
- }
- UnwindMapRemote::UnwindMapRemote(pid_t pid) : UnwindMap(pid) {
- }
- UnwindMapRemote::~UnwindMapRemote() {
- unw_map_cursor_destroy(&map_cursor_);
- unw_map_cursor_clear(&map_cursor_);
- }
- bool UnwindMapRemote::GenerateMap() {
-
-
- unw_map_cursor_reset(&map_cursor_);
- unw_map_t unw_map;
- while (unw_map_cursor_get_next(&map_cursor_, &unw_map)) {
- backtrace_map_t map;
- map.start = unw_map.start;
- map.end = unw_map.end;
- map.offset = unw_map.offset;
- map.load_bias = unw_map.load_base;
- map.flags = unw_map.flags;
- map.name = unw_map.path;
-
- maps_.push_front(map);
- }
- return true;
- }
- bool UnwindMapRemote::Build() {
- return (unw_map_cursor_create(&map_cursor_, pid_) == 0) && GenerateMap();
- }
- UnwindMapLocal::UnwindMapLocal() : UnwindMap(getpid()), map_created_(false) {
- pthread_rwlock_init(&map_lock_, nullptr);
- }
- UnwindMapLocal::~UnwindMapLocal() {
- if (map_created_) {
- unw_map_local_destroy();
- unw_map_cursor_clear(&map_cursor_);
- }
- }
- bool UnwindMapLocal::GenerateMap() {
-
-
- pthread_rwlock_wrlock(&map_lock_);
-
-
-
- bool generated = false;
- for (int i = 0; i < 3; i++) {
- maps_.clear();
-
- 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;
- map.offset = unw_map.offset;
- map.load_bias = unw_map.load_base;
- map.flags = unw_map.flags;
- map.name = unw_map.path;
- free(unw_map.path);
-
- maps_.push_front(map);
- }
-
- if (ret != -UNW_EINVAL) {
- generated = true;
- break;
- }
- }
- pthread_rwlock_unlock(&map_lock_);
- if (!generated) {
- BACK_LOGW("Unable to generate the map.");
- }
- return generated;
- }
- bool UnwindMapLocal::Build() {
- return (map_created_ = (unw_map_local_create() == 0)) && GenerateMap();;
- }
- void UnwindMapLocal::FillIn(uint64_t addr, backtrace_map_t* map) {
- BacktraceMap::FillIn(addr, map);
- if (!IsValid(*map)) {
-
-
- if (unw_map_local_cursor_valid(&map_cursor_) < 0) {
- if (GenerateMap()) {
- BacktraceMap::FillIn(addr, map);
- }
- }
- }
- }
|