blob: 8ab0dfabb56d835802e49238e3a8660144619525 [file] [log] [blame]
Christopher Ferris46756822014-01-14 20:16:30 -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
17#ifndef _BACKTRACE_BACKTRACE_MAP_H
18#define _BACKTRACE_BACKTRACE_MAP_H
19
20#include <stdint.h>
Dan Albertac2fe7e2014-05-21 18:21:02 -070021#include <sys/types.h>
Elliott Hughes34a4f0b2016-10-05 09:37:18 -070022#ifdef _WIN32
Christopher Ferris0dbce452014-01-17 08:25:26 -080023// 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
Christopher Ferris46756822014-01-14 20:16:30 -080029#include <sys/mman.h>
Christopher Ferris0dbce452014-01-17 08:25:26 -080030#endif
Christopher Ferris46756822014-01-14 20:16:30 -080031
Christopher Ferrisdf290612014-01-22 19:21:07 -080032#include <deque>
Christopher Ferris46756822014-01-14 20:16:30 -080033#include <string>
Yabin Cui9e402bb2015-09-22 04:46:57 +000034#include <vector>
Christopher Ferris46756822014-01-14 20:16:30 -080035
Christopher Ferrisf5e568e2017-03-22 13:18:31 -070036// Special flag to indicate a map is in /dev/. However, a map in
37// /dev/ashmem/... does not set this flag.
38static constexpr int PROT_DEVICE_MAP = 0x8000;
39
Christopher Ferris46756822014-01-14 20:16:30 -080040struct backtrace_map_t {
Christopher Ferris862fe022015-06-02 14:52:44 -070041 uintptr_t start = 0;
42 uintptr_t end = 0;
43 uintptr_t offset = 0;
44 uintptr_t load_base = 0;
45 int flags = 0;
Christopher Ferris46756822014-01-14 20:16:30 -080046 std::string name;
47};
48
49class BacktraceMap {
50public:
Christopher Ferrisdda47b72014-08-04 17:08:46 -070051 // If uncached is true, then parse the current process map as of the call.
52 // Passing a map created with uncached set to true to Backtrace::Create()
53 // is unsupported.
54 static BacktraceMap* Create(pid_t pid, bool uncached = false);
Christopher Ferrisdf290612014-01-22 19:21:07 -080055
Yabin Cui9e402bb2015-09-22 04:46:57 +000056 static BacktraceMap* Create(pid_t pid, const std::vector<backtrace_map_t>& maps);
57
Christopher Ferris46756822014-01-14 20:16:30 -080058 virtual ~BacktraceMap();
59
Christopher Ferris12385e32015-02-06 13:22:01 -080060 // Fill in the map data structure for the given address.
61 virtual void FillIn(uintptr_t addr, backtrace_map_t* map);
Christopher Ferris46756822014-01-14 20:16:30 -080062
63 // The flags returned are the same flags as used by the mmap call.
64 // The values are PROT_*.
65 int GetFlags(uintptr_t pc) {
Christopher Ferris12385e32015-02-06 13:22:01 -080066 backtrace_map_t map;
67 FillIn(pc, &map);
68 if (IsValid(map)) {
69 return map.flags;
Christopher Ferris46756822014-01-14 20:16:30 -080070 }
71 return PROT_NONE;
72 }
73
74 bool IsReadable(uintptr_t pc) { return GetFlags(pc) & PROT_READ; }
75 bool IsWritable(uintptr_t pc) { return GetFlags(pc) & PROT_WRITE; }
76 bool IsExecutable(uintptr_t pc) { return GetFlags(pc) & PROT_EXEC; }
77
Christopher Ferris3a140042016-06-15 15:49:50 -070078 // In order to use the iterators on this object, a caller must
79 // call the LockIterator and UnlockIterator function to guarantee
80 // that the data does not change while it's being used.
81 virtual void LockIterator() {}
82 virtual void UnlockIterator() {}
83
Christopher Ferrisdf290612014-01-22 19:21:07 -080084 typedef std::deque<backtrace_map_t>::iterator iterator;
Christopher Ferris46756822014-01-14 20:16:30 -080085 iterator begin() { return maps_.begin(); }
86 iterator end() { return maps_.end(); }
87
Christopher Ferrisdf290612014-01-22 19:21:07 -080088 typedef std::deque<backtrace_map_t>::const_iterator const_iterator;
Christopher Ferris46756822014-01-14 20:16:30 -080089 const_iterator begin() const { return maps_.begin(); }
90 const_iterator end() const { return maps_.end(); }
91
92 virtual bool Build();
93
Christopher Ferris12385e32015-02-06 13:22:01 -080094 static inline bool IsValid(const backtrace_map_t& map) {
95 return map.end > 0;
96 }
97
Christopher Ferris2106f4b2015-05-01 15:02:03 -070098 static uintptr_t GetRelativePc(const backtrace_map_t& map, uintptr_t pc) {
99 if (IsValid(map)) {
100 return pc - map.start + map.load_base;
101 } else {
102 return pc;
103 }
104 }
105
Christopher Ferris46756822014-01-14 20:16:30 -0800106protected:
Christopher Ferrisdf290612014-01-22 19:21:07 -0800107 BacktraceMap(pid_t pid);
108
Christopher Ferris46756822014-01-14 20:16:30 -0800109 virtual bool ParseLine(const char* line, backtrace_map_t* map);
110
Christopher Ferrisdf290612014-01-22 19:21:07 -0800111 std::deque<backtrace_map_t> maps_;
Christopher Ferris46756822014-01-14 20:16:30 -0800112 pid_t pid_;
113};
114
Christopher Ferris3a140042016-06-15 15:49:50 -0700115class ScopedBacktraceMapIteratorLock {
116public:
117 explicit ScopedBacktraceMapIteratorLock(BacktraceMap* map) : map_(map) {
118 map->LockIterator();
119 }
120
121 ~ScopedBacktraceMapIteratorLock() {
122 map_->UnlockIterator();
123 }
124
125private:
126 BacktraceMap* map_;
127};
128
Christopher Ferris46756822014-01-14 20:16:30 -0800129#endif // _BACKTRACE_BACKTRACE_MAP_H