Path: blob/21.2-virgl/include/android_stub/backtrace/BacktraceMap.h
7080 views
/*1* Copyright (C) 2014 The Android Open Source Project2*3* Licensed under the Apache License, Version 2.0 (the "License");4* you may not use this file except in compliance with the License.5* You may obtain a copy of the License at6*7* http://www.apache.org/licenses/LICENSE-2.08*9* Unless required by applicable law or agreed to in writing, software10* distributed under the License is distributed on an "AS IS" BASIS,11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12* See the License for the specific language governing permissions and13* limitations under the License.14*/1516#ifndef _BACKTRACE_BACKTRACE_MAP_H17#define _BACKTRACE_BACKTRACE_MAP_H1819#include <stdint.h>20#include <sys/types.h>21#ifdef _WIN3222// MINGW does not define these constants.23#define PROT_NONE 024#define PROT_READ 0x125#define PROT_WRITE 0x226#define PROT_EXEC 0x427#else28#include <sys/mman.h>29#endif3031#include <deque>32#include <iterator>33#include <memory>34#include <string>35#include <vector>3637// Forward declaration.38struct backtrace_stackinfo_t;3940// Special flag to indicate a map is in /dev/. However, a map in41// /dev/ashmem/... does not set this flag.42static constexpr int PROT_DEVICE_MAP = 0x8000;43// Special flag to indicate that this map represents an elf file44// created by ART for use with the gdb jit debug interface.45// This should only ever appear in offline maps data.46static constexpr int PROT_JIT_SYMFILE_MAP = 0x4000;4748struct backtrace_map_t {49uint64_t start = 0;50uint64_t end = 0;51uint64_t offset = 0;52uint64_t load_bias = 0;53int flags = 0;54std::string name;5556// Returns `name` if non-empty, or `<anonymous:0x...>` otherwise.57std::string Name() const;58};5960namespace unwindstack {61class Memory;62}6364class BacktraceMap {65public:66// If uncached is true, then parse the current process map as of the call.67// Passing a map created with uncached set to true to Backtrace::Create()68// is unsupported.69static BacktraceMap* Create(pid_t pid, bool uncached = false);7071virtual ~BacktraceMap();7273class iterator : public std::iterator<std::bidirectional_iterator_tag, backtrace_map_t*> {74public:75iterator(BacktraceMap* map, size_t index) : map_(map), index_(index) {}7677iterator& operator++() {78index_++;79return *this;80}81const iterator operator++(int increment) {82index_ += increment;83return *this;84}85iterator& operator--() {86index_--;87return *this;88}89const iterator operator--(int decrement) {90index_ -= decrement;91return *this;92}9394bool operator==(const iterator& rhs) { return this->index_ == rhs.index_; }95bool operator!=(const iterator& rhs) { return this->index_ != rhs.index_; }9697const backtrace_map_t* operator*() {98if (index_ >= map_->size()) {99return nullptr;100}101backtrace_map_t* map = &map_->maps_[index_];102if (map->load_bias == static_cast<uint64_t>(-1)) {103map->load_bias = map_->GetLoadBias(index_);104}105return map;106}107108private:109BacktraceMap* map_ = nullptr;110size_t index_ = 0;111};112113iterator begin() { return iterator(this, 0); }114iterator end() { return iterator(this, maps_.size()); }115116// Fill in the map data structure for the given address.117virtual void FillIn(uint64_t addr, backtrace_map_t* map);118119// Only supported with the new unwinder.120virtual std::string GetFunctionName(uint64_t /*pc*/, uint64_t* /*offset*/) { return ""; }121virtual std::shared_ptr<unwindstack::Memory> GetProcessMemory() { return nullptr; }122123// The flags returned are the same flags as used by the mmap call.124// The values are PROT_*.125int GetFlags(uint64_t pc) {126backtrace_map_t map;127FillIn(pc, &map);128if (IsValid(map)) {129return map.flags;130}131return PROT_NONE;132}133134bool IsReadable(uint64_t pc) { return GetFlags(pc) & PROT_READ; }135bool IsWritable(uint64_t pc) { return GetFlags(pc) & PROT_WRITE; }136bool IsExecutable(uint64_t pc) { return GetFlags(pc) & PROT_EXEC; }137138// In order to use the iterators on this object, a caller must139// call the LockIterator and UnlockIterator function to guarantee140// that the data does not change while it's being used.141virtual void LockIterator() {}142virtual void UnlockIterator() {}143144size_t size() const { return maps_.size(); }145146virtual bool Build();147148static inline bool IsValid(const backtrace_map_t& map) {149return map.end > 0;150}151152void SetSuffixesToIgnore(std::vector<std::string> suffixes) {153suffixes_to_ignore_.insert(suffixes_to_ignore_.end(), suffixes.begin(), suffixes.end());154}155156const std::vector<std::string>& GetSuffixesToIgnore() { return suffixes_to_ignore_; }157158// Disabling the resolving of names results in the function name being159// set to an empty string and the function offset being set to zero160// in the frame data when unwinding.161void SetResolveNames(bool resolve) { resolve_names_ = resolve; }162163bool ResolveNames() { return resolve_names_; }164165protected:166BacktraceMap(pid_t pid);167168virtual uint64_t GetLoadBias(size_t /* index */) { return 0; }169170pid_t pid_;171std::deque<backtrace_map_t> maps_;172std::vector<std::string> suffixes_to_ignore_;173bool resolve_names_ = true;174};175176class ScopedBacktraceMapIteratorLock {177public:178explicit ScopedBacktraceMapIteratorLock(BacktraceMap* map) : map_(map) {179map->LockIterator();180}181182~ScopedBacktraceMapIteratorLock() {183map_->UnlockIterator();184}185186private:187BacktraceMap* map_;188};189190#endif // _BACKTRACE_BACKTRACE_MAP_H191192193