blob: 161551807557fae7967cfeafdcfbc20ff181cde1 [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 Ferrisdf290612014-01-22 19:21:07 -080017#include <pthread.h>
18#include <sys/types.h>
19#include <unistd.h>
20
21#include <backtrace/BacktraceMap.h>
22
23#include <libunwind.h>
24
Christopher Ferrise2960912014-03-07 19:42:19 -080025#include "BacktraceLog.h"
Christopher Ferrisdf290612014-01-22 19:21:07 -080026#include "UnwindMap.h"
27
28//-------------------------------------------------------------------------
29// libunwind has a single shared address space for the current process
30// aka local. If multiple maps are created for the current pid, then
31// only update the local address space once, and keep a reference count
32// of maps using the same map cursor.
33//-------------------------------------------------------------------------
Christopher Ferrisdf290612014-01-22 19:21:07 -080034UnwindMap::UnwindMap(pid_t pid) : BacktraceMap(pid) {
Christopher Ferrisdf290612014-01-22 19:21:07 -080035}
36
37UnwindMap::~UnwindMap() {
Christopher Ferrise2960912014-03-07 19:42:19 -080038 unw_map_cursor_destroy(&map_cursor_);
39 unw_map_cursor_clear(&map_cursor_);
Christopher Ferrisdf290612014-01-22 19:21:07 -080040}
41
Christopher Ferrise2960912014-03-07 19:42:19 -080042bool UnwindMap::GenerateMap() {
Christopher Ferrisdf290612014-01-22 19:21:07 -080043 // Use the map_cursor information to construct the BacktraceMap data
44 // rather than reparsing /proc/self/maps.
45 unw_map_cursor_reset(&map_cursor_);
Christopher Ferrise2960912014-03-07 19:42:19 -080046
Christopher Ferrisdf290612014-01-22 19:21:07 -080047 unw_map_t unw_map;
Christopher Ferrise2960912014-03-07 19:42:19 -080048 while (unw_map_cursor_get_next(&map_cursor_, &unw_map)) {
Christopher Ferrisdf290612014-01-22 19:21:07 -080049 backtrace_map_t map;
50
51 map.start = unw_map.start;
52 map.end = unw_map.end;
53 map.flags = unw_map.flags;
54 map.name = unw_map.path;
55
56 // The maps are in descending order, but we want them in ascending order.
57 maps_.push_front(map);
58 }
59
60 return true;
61}
62
Christopher Ferrise2960912014-03-07 19:42:19 -080063bool UnwindMap::Build() {
64 return (unw_map_cursor_create(&map_cursor_, pid_) == 0) && GenerateMap();
65}
66
67UnwindMapLocal::UnwindMapLocal() : UnwindMap(getpid()), map_created_(false) {
68}
69
70UnwindMapLocal::~UnwindMapLocal() {
71 if (map_created_) {
72 unw_map_local_destroy();
73 unw_map_cursor_clear(&map_cursor_);
74 }
75}
76
77bool UnwindMapLocal::GenerateMap() {
78 // It's possible for the map to be regenerated while this loop is occurring.
79 // If that happens, get the map again, but only try at most three times
80 // before giving up.
81 for (int i = 0; i < 3; i++) {
82 maps_.clear();
83
84 unw_map_local_cursor_get(&map_cursor_);
85
86 unw_map_t unw_map;
87 int ret;
88 while ((ret = unw_map_local_cursor_get_next(&map_cursor_, &unw_map)) > 0) {
89 backtrace_map_t map;
90
91 map.start = unw_map.start;
92 map.end = unw_map.end;
93 map.flags = unw_map.flags;
94 map.name = unw_map.path;
95
96 free(unw_map.path);
97
98 // The maps are in descending order, but we want them in ascending order.
99 maps_.push_front(map);
100 }
101 // Check to see if the map changed while getting the data.
102 if (ret != -UNW_EINVAL) {
103 return true;
104 }
105 }
106
107 BACK_LOGW("Unable to generate the map.");
108 return false;
109}
110
111bool UnwindMapLocal::Build() {
112 return (map_created_ = (unw_map_local_create() == 0)) && GenerateMap();;
113}
114
115const backtrace_map_t* UnwindMapLocal::Find(uintptr_t addr) {
116 const backtrace_map_t* map = BacktraceMap::Find(addr);
117 if (!map) {
118 // Check to see if the underlying map changed and regenerate the map
119 // if it did.
120 if (unw_map_local_cursor_valid(&map_cursor_) < 0) {
121 if (GenerateMap()) {
122 map = BacktraceMap::Find(addr);
123 }
124 }
125 }
126 return map;
127}
128
Christopher Ferrisdf290612014-01-22 19:21:07 -0800129//-------------------------------------------------------------------------
130// BacktraceMap create function.
131//-------------------------------------------------------------------------
132BacktraceMap* BacktraceMap::Create(pid_t pid) {
Christopher Ferrise2960912014-03-07 19:42:19 -0800133 BacktraceMap* map;
134 if (pid == getpid()) {
135 map = new UnwindMapLocal();
136 } else {
137 map = new UnwindMap(pid);
138 }
Christopher Ferrisdf290612014-01-22 19:21:07 -0800139 if (!map->Build()) {
140 delete map;
141 return NULL;
142 }
143 return map;
144}