blob: 798c769c8484da279c0d4c22091615851fbdbf20 [file] [log] [blame]
Christopher Ferrisdf290612014-01-22 19:21:07 -08001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Christopher Ferris3a140042016-06-15 15:49:50 -070017#include <pthread.h>
Christopher Ferris2c43cff2015-03-26 19:18:36 -070018#include <stdint.h>
Dan Albertac2fe7e2014-05-21 18:21:02 -070019#include <stdlib.h>
Christopher Ferrisdf290612014-01-22 19:21:07 -080020#include <sys/types.h>
21#include <unistd.h>
22
23#include <backtrace/BacktraceMap.h>
24
25#include <libunwind.h>
26
Christopher Ferrise2960912014-03-07 19:42:19 -080027#include "BacktraceLog.h"
Christopher Ferrisdf290612014-01-22 19:21:07 -080028#include "UnwindMap.h"
29
30//-------------------------------------------------------------------------
31// libunwind has a single shared address space for the current process
32// aka local. If multiple maps are created for the current pid, then
33// only update the local address space once, and keep a reference count
34// of maps using the same map cursor.
35//-------------------------------------------------------------------------
Christopher Ferrisdf290612014-01-22 19:21:07 -080036UnwindMap::UnwindMap(pid_t pid) : BacktraceMap(pid) {
Christopher Ferrisd4c88432016-02-05 11:07:12 -080037 unw_map_cursor_clear(&map_cursor_);
Christopher Ferrisdf290612014-01-22 19:21:07 -080038}
39
Christopher Ferrisd4c88432016-02-05 11:07:12 -080040UnwindMapRemote::UnwindMapRemote(pid_t pid) : UnwindMap(pid) {
41}
42
43UnwindMapRemote::~UnwindMapRemote() {
Christopher Ferrise2960912014-03-07 19:42:19 -080044 unw_map_cursor_destroy(&map_cursor_);
45 unw_map_cursor_clear(&map_cursor_);
Christopher Ferrisdf290612014-01-22 19:21:07 -080046}
47
Christopher Ferrisd4c88432016-02-05 11:07:12 -080048bool UnwindMapRemote::GenerateMap() {
Christopher Ferrisdf290612014-01-22 19:21:07 -080049 // Use the map_cursor information to construct the BacktraceMap data
50 // rather than reparsing /proc/self/maps.
51 unw_map_cursor_reset(&map_cursor_);
Christopher Ferrise2960912014-03-07 19:42:19 -080052
Christopher Ferrisdf290612014-01-22 19:21:07 -080053 unw_map_t unw_map;
Christopher Ferrise2960912014-03-07 19:42:19 -080054 while (unw_map_cursor_get_next(&map_cursor_, &unw_map)) {
Christopher Ferrisdf290612014-01-22 19:21:07 -080055 backtrace_map_t map;
56
57 map.start = unw_map.start;
58 map.end = unw_map.end;
Christopher Ferrisaf67fb22015-05-06 12:50:09 -070059 map.offset = unw_map.offset;
Christopher Ferris96722b02017-07-19 14:20:46 -070060 map.load_bias = unw_map.load_base;
Christopher Ferrisdf290612014-01-22 19:21:07 -080061 map.flags = unw_map.flags;
62 map.name = unw_map.path;
63
64 // The maps are in descending order, but we want them in ascending order.
65 maps_.push_front(map);
66 }
67
68 return true;
69}
70
Christopher Ferrisd4c88432016-02-05 11:07:12 -080071bool UnwindMapRemote::Build() {
Christopher Ferrise2960912014-03-07 19:42:19 -080072 return (unw_map_cursor_create(&map_cursor_, pid_) == 0) && GenerateMap();
73}
74
75UnwindMapLocal::UnwindMapLocal() : UnwindMap(getpid()), map_created_(false) {
Christopher Ferris3a140042016-06-15 15:49:50 -070076 pthread_rwlock_init(&map_lock_, nullptr);
Christopher Ferrise2960912014-03-07 19:42:19 -080077}
78
79UnwindMapLocal::~UnwindMapLocal() {
80 if (map_created_) {
81 unw_map_local_destroy();
82 unw_map_cursor_clear(&map_cursor_);
83 }
84}
85
86bool UnwindMapLocal::GenerateMap() {
Christopher Ferris3a140042016-06-15 15:49:50 -070087 // Lock so that multiple threads cannot modify the maps data at the
88 // same time.
89 pthread_rwlock_wrlock(&map_lock_);
90
Christopher Ferrise2960912014-03-07 19:42:19 -080091 // It's possible for the map to be regenerated while this loop is occurring.
92 // If that happens, get the map again, but only try at most three times
93 // before giving up.
Christopher Ferris3a140042016-06-15 15:49:50 -070094 bool generated = false;
Christopher Ferrise2960912014-03-07 19:42:19 -080095 for (int i = 0; i < 3; i++) {
96 maps_.clear();
97
Christopher Ferrisd4c88432016-02-05 11:07:12 -080098 // Save the map data retrieved so we can tell if it changes.
Christopher Ferrise2960912014-03-07 19:42:19 -080099 unw_map_local_cursor_get(&map_cursor_);
100
101 unw_map_t unw_map;
102 int ret;
103 while ((ret = unw_map_local_cursor_get_next(&map_cursor_, &unw_map)) > 0) {
104 backtrace_map_t map;
105
106 map.start = unw_map.start;
107 map.end = unw_map.end;
Christopher Ferrisaf67fb22015-05-06 12:50:09 -0700108 map.offset = unw_map.offset;
Christopher Ferris96722b02017-07-19 14:20:46 -0700109 map.load_bias = unw_map.load_base;
Christopher Ferrise2960912014-03-07 19:42:19 -0800110 map.flags = unw_map.flags;
111 map.name = unw_map.path;
112
113 free(unw_map.path);
114
115 // The maps are in descending order, but we want them in ascending order.
116 maps_.push_front(map);
117 }
118 // Check to see if the map changed while getting the data.
119 if (ret != -UNW_EINVAL) {
Christopher Ferris3a140042016-06-15 15:49:50 -0700120 generated = true;
121 break;
Christopher Ferrise2960912014-03-07 19:42:19 -0800122 }
123 }
124
Christopher Ferris3a140042016-06-15 15:49:50 -0700125 pthread_rwlock_unlock(&map_lock_);
126
127 if (!generated) {
128 BACK_LOGW("Unable to generate the map.");
129 }
130 return generated;
Christopher Ferrise2960912014-03-07 19:42:19 -0800131}
132
133bool UnwindMapLocal::Build() {
134 return (map_created_ = (unw_map_local_create() == 0)) && GenerateMap();;
135}
136
Christopher Ferris7937a362018-01-18 11:15:49 -0800137void UnwindMapLocal::FillIn(uint64_t addr, backtrace_map_t* map) {
Christopher Ferris12385e32015-02-06 13:22:01 -0800138 BacktraceMap::FillIn(addr, map);
139 if (!IsValid(*map)) {
Christopher Ferrise2960912014-03-07 19:42:19 -0800140 // Check to see if the underlying map changed and regenerate the map
141 // if it did.
142 if (unw_map_local_cursor_valid(&map_cursor_) < 0) {
143 if (GenerateMap()) {
Christopher Ferris12385e32015-02-06 13:22:01 -0800144 BacktraceMap::FillIn(addr, map);
Christopher Ferrise2960912014-03-07 19:42:19 -0800145 }
146 }
147 }
Christopher Ferrise2960912014-03-07 19:42:19 -0800148}