• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   static BacktraceMap* CreateOffline(pid_t pid, const std::vector<backtrace_map_t>& maps);
73 
74   virtual ~BacktraceMap();
75 
76   class iterator : public std::iterator<std::bidirectional_iterator_tag, backtrace_map_t*> {
77    public:
iterator(BacktraceMap * map,size_t index)78     iterator(BacktraceMap* map, size_t index) : map_(map), index_(index) {}
79 
80     iterator& operator++() {
81       index_++;
82       return *this;
83     }
84     iterator& operator++(int increment) {
85       index_ += increment;
86       return *this;
87     }
88     iterator& operator--() {
89       index_--;
90       return *this;
91     }
92     iterator& operator--(int decrement) {
93       index_ -= decrement;
94       return *this;
95     }
96 
97     bool operator==(const iterator& rhs) { return this->index_ == rhs.index_; }
98     bool operator!=(const iterator& rhs) { return this->index_ != rhs.index_; }
99 
100     const backtrace_map_t* operator*() {
101       if (index_ >= map_->size()) {
102         return nullptr;
103       }
104       backtrace_map_t* map = &map_->maps_[index_];
105       if (map->load_bias == static_cast<uint64_t>(-1)) {
106         map->load_bias = map_->GetLoadBias(index_);
107       }
108       return map;
109     }
110 
111    private:
112     BacktraceMap* map_ = nullptr;
113     size_t index_ = 0;
114   };
115 
begin()116   iterator begin() { return iterator(this, 0); }
end()117   iterator end() { return iterator(this, maps_.size()); }
118 
119   // Fill in the map data structure for the given address.
120   virtual void FillIn(uint64_t addr, backtrace_map_t* map);
121 
122   // Only supported with the new unwinder.
GetFunctionName(uint64_t,uint64_t *)123   virtual std::string GetFunctionName(uint64_t /*pc*/, uint64_t* /*offset*/) { return ""; }
GetProcessMemory()124   virtual std::shared_ptr<unwindstack::Memory> GetProcessMemory() { return nullptr; }
125 
126   // The flags returned are the same flags as used by the mmap call.
127   // The values are PROT_*.
GetFlags(uint64_t pc)128   int GetFlags(uint64_t pc) {
129     backtrace_map_t map;
130     FillIn(pc, &map);
131     if (IsValid(map)) {
132       return map.flags;
133     }
134     return PROT_NONE;
135   }
136 
IsReadable(uint64_t pc)137   bool IsReadable(uint64_t pc) { return GetFlags(pc) & PROT_READ; }
IsWritable(uint64_t pc)138   bool IsWritable(uint64_t pc) { return GetFlags(pc) & PROT_WRITE; }
IsExecutable(uint64_t pc)139   bool IsExecutable(uint64_t pc) { return GetFlags(pc) & PROT_EXEC; }
140 
141   // In order to use the iterators on this object, a caller must
142   // call the LockIterator and UnlockIterator function to guarantee
143   // that the data does not change while it's being used.
LockIterator()144   virtual void LockIterator() {}
UnlockIterator()145   virtual void UnlockIterator() {}
146 
size()147   size_t size() const { return maps_.size(); }
148 
149   virtual bool Build();
150 
IsValid(const backtrace_map_t & map)151   static inline bool IsValid(const backtrace_map_t& map) {
152     return map.end > 0;
153   }
154 
SetSuffixesToIgnore(std::vector<std::string> suffixes)155   void SetSuffixesToIgnore(std::vector<std::string> suffixes) {
156     suffixes_to_ignore_.insert(suffixes_to_ignore_.end(), suffixes.begin(), suffixes.end());
157   }
158 
GetSuffixesToIgnore()159   const std::vector<std::string>& GetSuffixesToIgnore() { return suffixes_to_ignore_; }
160 
161   // Disabling the resolving of names results in the function name being
162   // set to an empty string and the function offset being set to zero
163   // in the frame data when unwinding.
SetResolveNames(bool resolve)164   void SetResolveNames(bool resolve) { resolve_names_ = resolve; }
165 
ResolveNames()166   bool ResolveNames() { return resolve_names_; }
167 
168  protected:
169   BacktraceMap(pid_t pid);
170 
GetLoadBias(size_t)171   virtual uint64_t GetLoadBias(size_t /* index */) { return 0; }
172 
173   pid_t pid_;
174   std::deque<backtrace_map_t> maps_;
175   std::vector<std::string> suffixes_to_ignore_;
176   bool resolve_names_ = true;
177 };
178 
179 class ScopedBacktraceMapIteratorLock {
180 public:
ScopedBacktraceMapIteratorLock(BacktraceMap * map)181   explicit ScopedBacktraceMapIteratorLock(BacktraceMap* map) : map_(map) {
182     map->LockIterator();
183   }
184 
~ScopedBacktraceMapIteratorLock()185   ~ScopedBacktraceMapIteratorLock() {
186     map_->UnlockIterator();
187   }
188 
189 private:
190   BacktraceMap* map_;
191 };
192 
193 #endif // _BACKTRACE_BACKTRACE_MAP_H
194