2
* Copyright (C) 2014 The Android Open Source Project
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
#ifndef _BACKTRACE_BACKTRACE_MAP_H
18
#define _BACKTRACE_BACKTRACE_MAP_H
21
#include <sys/types.h>
23
// MINGW does not define these constants.
26
#define PROT_WRITE 0x2
38
// Forward declaration.
39
struct backtrace_stackinfo_t;
41
// Special flag to indicate a map is in /dev/. However, a map in
42
// /dev/ashmem/... does not set this flag.
43
static constexpr int PROT_DEVICE_MAP = 0x8000;
44
// Special flag to indicate that this map represents an elf file
45
// created by ART for use with the gdb jit debug interface.
46
// This should only ever appear in offline maps data.
47
static constexpr int PROT_JIT_SYMFILE_MAP = 0x4000;
49
struct backtrace_map_t {
53
uint64_t load_bias = 0;
57
// Returns `name` if non-empty, or `<anonymous:0x...>` otherwise.
58
std::string Name() const;
61
namespace unwindstack {
67
// If uncached is true, then parse the current process map as of the call.
68
// Passing a map created with uncached set to true to Backtrace::Create()
70
static BacktraceMap* Create(pid_t pid, bool uncached = false);
72
virtual ~BacktraceMap();
74
class iterator : public std::iterator<std::bidirectional_iterator_tag, backtrace_map_t*> {
76
iterator(BacktraceMap* map, size_t index) : map_(map), index_(index) {}
78
iterator& operator++() {
82
const iterator operator++(int increment) {
86
iterator& operator--() {
90
const iterator operator--(int decrement) {
95
bool operator==(const iterator& rhs) { return this->index_ == rhs.index_; }
96
bool operator!=(const iterator& rhs) { return this->index_ != rhs.index_; }
98
const backtrace_map_t* operator*() {
99
if (index_ >= map_->size()) {
102
backtrace_map_t* map = &map_->maps_[index_];
103
if (map->load_bias == static_cast<uint64_t>(-1)) {
104
map->load_bias = map_->GetLoadBias(index_);
110
BacktraceMap* map_ = nullptr;
114
iterator begin() { return iterator(this, 0); }
115
iterator end() { return iterator(this, maps_.size()); }
117
// Fill in the map data structure for the given address.
118
virtual void FillIn(uint64_t addr, backtrace_map_t* map);
120
// Only supported with the new unwinder.
121
virtual std::string GetFunctionName(uint64_t /*pc*/, uint64_t* /*offset*/) { return ""; }
122
virtual std::shared_ptr<unwindstack::Memory> GetProcessMemory() { return nullptr; }
124
// The flags returned are the same flags as used by the mmap call.
125
// The values are PROT_*.
126
int GetFlags(uint64_t pc) {
135
bool IsReadable(uint64_t pc) { return GetFlags(pc) & PROT_READ; }
136
bool IsWritable(uint64_t pc) { return GetFlags(pc) & PROT_WRITE; }
137
bool IsExecutable(uint64_t pc) { return GetFlags(pc) & PROT_EXEC; }
139
// In order to use the iterators on this object, a caller must
140
// call the LockIterator and UnlockIterator function to guarantee
141
// that the data does not change while it's being used.
142
virtual void LockIterator() {}
143
virtual void UnlockIterator() {}
145
size_t size() const { return maps_.size(); }
147
virtual bool Build();
149
static inline bool IsValid(const backtrace_map_t& map) {
153
void SetSuffixesToIgnore(std::vector<std::string> suffixes) {
154
suffixes_to_ignore_.insert(suffixes_to_ignore_.end(), suffixes.begin(), suffixes.end());
157
const std::vector<std::string>& GetSuffixesToIgnore() { return suffixes_to_ignore_; }
159
// Disabling the resolving of names results in the function name being
160
// set to an empty string and the function offset being set to zero
161
// in the frame data when unwinding.
162
void SetResolveNames(bool resolve) { resolve_names_ = resolve; }
164
bool ResolveNames() { return resolve_names_; }
167
BacktraceMap(pid_t pid);
169
virtual uint64_t GetLoadBias(size_t /* index */) { return 0; }
172
std::deque<backtrace_map_t> maps_;
173
std::vector<std::string> suffixes_to_ignore_;
174
bool resolve_names_ = true;
177
class ScopedBacktraceMapIteratorLock {
179
explicit ScopedBacktraceMapIteratorLock(BacktraceMap* map) : map_(map) {
183
~ScopedBacktraceMapIteratorLock() {
184
map_->UnlockIterator();
191
#endif // _BACKTRACE_BACKTRACE_MAP_H