1 /*
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 
17 #ifndef _BACKTRACE_BACKTRACE_MAP_H
18 #define _BACKTRACE_BACKTRACE_MAP_H
19 
20 #include <stdint.h>
21 #include <sys/types.h>
22 #ifdef _WIN32
23 // MINGW does not define these constants.
24 #define PROT_NONE 0
25 #define PROT_READ 0x1
26 #define PROT_WRITE 0x2
27 #define PROT_EXEC 0x4
28 #else
29 #include <sys/mman.h>
30 #endif
31 
32 #include <deque>
33 #include <iterator>
34 #include <memory>
35 #include <string>
36 #include <vector>
37 
38 // Forward declaration.
39 struct backtrace_stackinfo_t;
40 
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;
48 
49 struct backtrace_map_t {
50   uint64_t start = 0;
51   uint64_t end = 0;
52   uint64_t offset = 0;
53   uint64_t load_bias = 0;
54   int flags = 0;
55   std::string name;
56 
57   // Returns `name` if non-empty, or `<anonymous:0x...>` otherwise.
58   std::string Name() const;
59 };
60 
61 namespace unwindstack {
62 class Memory;
63 }
64 
65 class BacktraceMap {
66 public:
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()
69   // is unsupported.
70   static BacktraceMap* Create(pid_t pid, bool uncached = false);
71 
72   virtual ~BacktraceMap();
73 
74   class iterator : public std::iterator<std::bidirectional_iterator_tag, backtrace_map_t*> {
75    public:
iterator(BacktraceMap * map,size_t index)76     iterator(BacktraceMap* map, size_t index) : map_(map), index_(index) {}
77 
78     iterator& operator++() {
79       index_++;
80       return *this;
81     }
82     const iterator operator++(int increment) {
83       index_ += increment;
84       return *this;
85     }
86     iterator& operator--() {
87       index_--;
88       return *this;
89     }
90     const iterator operator--(int decrement) {
91       index_ -= decrement;
92       return *this;
93     }
94 
95     bool operator==(const iterator& rhs) { return this->index_ == rhs.index_; }
96     bool operator!=(const iterator& rhs) { return this->index_ != rhs.index_; }
97 
98     const backtrace_map_t* operator*() {
99       if (index_ >= map_->size()) {
100         return nullptr;
101       }
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_);
105       }
106       return map;
107     }
108 
109    private:
110     BacktraceMap* map_ = nullptr;
111     size_t index_ = 0;
112   };
113 
begin()114   iterator begin() { return iterator(this, 0); }
end()115   iterator end() { return iterator(this, maps_.size()); }
116 
117   // Fill in the map data structure for the given address.
118   virtual void FillIn(uint64_t addr, backtrace_map_t* map);
119 
120   // Only supported with the new unwinder.
GetFunctionName(uint64_t,uint64_t *)121   virtual std::string GetFunctionName(uint64_t /*pc*/, uint64_t* /*offset*/) { return ""; }
GetProcessMemory()122   virtual std::shared_ptr<unwindstack::Memory> GetProcessMemory() { return nullptr; }
123 
124   // The flags returned are the same flags as used by the mmap call.
125   // The values are PROT_*.
GetFlags(uint64_t pc)126   int GetFlags(uint64_t pc) {
127     backtrace_map_t map;
128     FillIn(pc, &map);
129     if (IsValid(map)) {
130       return map.flags;
131     }
132     return PROT_NONE;
133   }
134 
IsReadable(uint64_t pc)135   bool IsReadable(uint64_t pc) { return GetFlags(pc) & PROT_READ; }
IsWritable(uint64_t pc)136   bool IsWritable(uint64_t pc) { return GetFlags(pc) & PROT_WRITE; }
IsExecutable(uint64_t pc)137   bool IsExecutable(uint64_t pc) { return GetFlags(pc) & PROT_EXEC; }
138 
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.
LockIterator()142   virtual void LockIterator() {}
UnlockIterator()143   virtual void UnlockIterator() {}
144 
size()145   size_t size() const { return maps_.size(); }
146 
147   virtual bool Build();
148 
IsValid(const backtrace_map_t & map)149   static inline bool IsValid(const backtrace_map_t& map) {
150     return map.end > 0;
151   }
152 
SetSuffixesToIgnore(std::vector<std::string> suffixes)153   void SetSuffixesToIgnore(std::vector<std::string> suffixes) {
154     suffixes_to_ignore_.insert(suffixes_to_ignore_.end(), suffixes.begin(), suffixes.end());
155   }
156 
GetSuffixesToIgnore()157   const std::vector<std::string>& GetSuffixesToIgnore() { return suffixes_to_ignore_; }
158 
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.
SetResolveNames(bool resolve)162   void SetResolveNames(bool resolve) { resolve_names_ = resolve; }
163 
ResolveNames()164   bool ResolveNames() { return resolve_names_; }
165 
166  protected:
167   BacktraceMap(pid_t pid);
168 
GetLoadBias(size_t)169   virtual uint64_t GetLoadBias(size_t /* index */) { return 0; }
170 
171   pid_t pid_;
172   std::deque<backtrace_map_t> maps_;
173   std::vector<std::string> suffixes_to_ignore_;
174   bool resolve_names_ = true;
175 };
176 
177 class ScopedBacktraceMapIteratorLock {
178 public:
ScopedBacktraceMapIteratorLock(BacktraceMap * map)179   explicit ScopedBacktraceMapIteratorLock(BacktraceMap* map) : map_(map) {
180     map->LockIterator();
181   }
182 
~ScopedBacktraceMapIteratorLock()183   ~ScopedBacktraceMapIteratorLock() {
184     map_->UnlockIterator();
185   }
186 
187 private:
188   BacktraceMap* map_;
189 };
190 
191 #endif // _BACKTRACE_BACKTRACE_MAP_H
192