blob: 879fea5eb13d1921986391865c40d6b785665b50 [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 Ferris2c43cff2015-03-26 19:18:36 -070017#include <stdint.h>
Dan Albertac2fe7e2014-05-21 18:21:02 -070018#include <stdlib.h>
Christopher Ferrisdf290612014-01-22 19:21:07 -080019#include <sys/types.h>
20#include <unistd.h>
21
22#include <backtrace/BacktraceMap.h>
23
24#include <libunwind.h>
25
Christopher Ferrise2960912014-03-07 19:42:19 -080026#include "BacktraceLog.h"
Christopher Ferrisdf290612014-01-22 19:21:07 -080027#include "UnwindMap.h"
28
29//-------------------------------------------------------------------------
30// libunwind has a single shared address space for the current process
31// aka local. If multiple maps are created for the current pid, then
32// only update the local address space once, and keep a reference count
33// of maps using the same map cursor.
34//-------------------------------------------------------------------------
Christopher Ferrisdf290612014-01-22 19:21:07 -080035UnwindMap::UnwindMap(pid_t pid) : BacktraceMap(pid) {
Christopher Ferrisdf290612014-01-22 19:21:07 -080036}
37
38UnwindMap::~UnwindMap() {
Christopher Ferrise2960912014-03-07 19:42:19 -080039 unw_map_cursor_destroy(&map_cursor_);
40 unw_map_cursor_clear(&map_cursor_);
Christopher Ferrisdf290612014-01-22 19:21:07 -080041}
42
Christopher Ferrise2960912014-03-07 19:42:19 -080043bool UnwindMap::GenerateMap() {
Christopher Ferrisdf290612014-01-22 19:21:07 -080044 // Use the map_cursor information to construct the BacktraceMap data
45 // rather than reparsing /proc/self/maps.
46 unw_map_cursor_reset(&map_cursor_);
Christopher Ferrise2960912014-03-07 19:42:19 -080047
Christopher Ferrisdf290612014-01-22 19:21:07 -080048 unw_map_t unw_map;
Christopher Ferrise2960912014-03-07 19:42:19 -080049 while (unw_map_cursor_get_next(&map_cursor_, &unw_map)) {
Christopher Ferrisdf290612014-01-22 19:21:07 -080050 backtrace_map_t map;
51
52 map.start = unw_map.start;
53 map.end = unw_map.end;
Christopher Ferris5c885682015-05-06 12:50:09 -070054 map.offset = unw_map.offset;
Christopher Ferris2106f4b2015-05-01 15:02:03 -070055 map.load_base = unw_map.load_base;
Christopher Ferrisdf290612014-01-22 19:21:07 -080056 map.flags = unw_map.flags;
57 map.name = unw_map.path;
58
59 // The maps are in descending order, but we want them in ascending order.
60 maps_.push_front(map);
61 }
62
63 return true;
64}
65
Christopher Ferrise2960912014-03-07 19:42:19 -080066bool UnwindMap::Build() {
67 return (unw_map_cursor_create(&map_cursor_, pid_) == 0) && GenerateMap();
68}
69
70UnwindMapLocal::UnwindMapLocal() : UnwindMap(getpid()), map_created_(false) {
71}
72
73UnwindMapLocal::~UnwindMapLocal() {
74 if (map_created_) {
75 unw_map_local_destroy();
76 unw_map_cursor_clear(&map_cursor_);
77 }
78}
79
80bool UnwindMapLocal::GenerateMap() {
81 // It's possible for the map to be regenerated while this loop is occurring.
82 // If that happens, get the map again, but only try at most three times
83 // before giving up.
84 for (int i = 0; i < 3; i++) {
85 maps_.clear();
86
87 unw_map_local_cursor_get(&map_cursor_);
88
89 unw_map_t unw_map;
90 int ret;
91 while ((ret = unw_map_local_cursor_get_next(&map_cursor_, &unw_map)) > 0) {
92 backtrace_map_t map;
93
94 map.start = unw_map.start;
95 map.end = unw_map.end;
Christopher Ferris5c885682015-05-06 12:50:09 -070096 map.offset = unw_map.offset;
Christopher Ferris2106f4b2015-05-01 15:02:03 -070097 map.load_base = unw_map.load_base;
Christopher Ferrise2960912014-03-07 19:42:19 -080098 map.flags = unw_map.flags;
99 map.name = unw_map.path;
100
101 free(unw_map.path);
102
103 // The maps are in descending order, but we want them in ascending order.
104 maps_.push_front(map);
105 }
106 // Check to see if the map changed while getting the data.
107 if (ret != -UNW_EINVAL) {
108 return true;
109 }
110 }
111
112 BACK_LOGW("Unable to generate the map.");
113 return false;
114}
115
116bool UnwindMapLocal::Build() {
117 return (map_created_ = (unw_map_local_create() == 0)) && GenerateMap();;
118}
119
Christopher Ferris12385e32015-02-06 13:22:01 -0800120void UnwindMapLocal::FillIn(uintptr_t addr, backtrace_map_t* map) {
121 BacktraceMap::FillIn(addr, map);
122 if (!IsValid(*map)) {
Christopher Ferrise2960912014-03-07 19:42:19 -0800123 // Check to see if the underlying map changed and regenerate the map
124 // if it did.
125 if (unw_map_local_cursor_valid(&map_cursor_) < 0) {
126 if (GenerateMap()) {
Christopher Ferris12385e32015-02-06 13:22:01 -0800127 BacktraceMap::FillIn(addr, map);
Christopher Ferrise2960912014-03-07 19:42:19 -0800128 }
129 }
130 }
Christopher Ferrise2960912014-03-07 19:42:19 -0800131}
132
Christopher Ferrisdf290612014-01-22 19:21:07 -0800133//-------------------------------------------------------------------------
134// BacktraceMap create function.
135//-------------------------------------------------------------------------
Christopher Ferrisdda47b72014-08-04 17:08:46 -0700136BacktraceMap* BacktraceMap::Create(pid_t pid, bool uncached) {
Christopher Ferrise2960912014-03-07 19:42:19 -0800137 BacktraceMap* map;
Christopher Ferrisdda47b72014-08-04 17:08:46 -0700138
139 if (uncached) {
140 // Force use of the base class to parse the maps when this call is made.
141 map = new BacktraceMap(pid);
142 } else if (pid == getpid()) {
Christopher Ferrise2960912014-03-07 19:42:19 -0800143 map = new UnwindMapLocal();
144 } else {
145 map = new UnwindMap(pid);
146 }
Christopher Ferrisdf290612014-01-22 19:21:07 -0800147 if (!map->Build()) {
148 delete map;
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700149 return nullptr;
Christopher Ferrisdf290612014-01-22 19:21:07 -0800150 }
151 return map;
152}