blob: 784bc035c5d24d4c8c197f49daee066ed85a5a7b [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>
Christopher Ferris0dbce452014-01-17 08:25:26 -080022#ifdef USE_MINGW
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
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>
Christopher Ferris46756822014-01-14 20:16:30 -080034
35struct backtrace_map_t {
Christopher Ferris12385e32015-02-06 13:22:01 -080036 backtrace_map_t(): start(0), end(0), flags(0) {}
37
Christopher Ferris46756822014-01-14 20:16:30 -080038 uintptr_t start;
39 uintptr_t end;
Christopher Ferris5c885682015-05-06 12:50:09 -070040 uintptr_t offset;
Christopher Ferris2106f4b2015-05-01 15:02:03 -070041 uintptr_t load_base;
Christopher Ferris46756822014-01-14 20:16:30 -080042 int flags;
43 std::string name;
44};
45
46class BacktraceMap {
47public:
Christopher Ferrisdda47b72014-08-04 17:08:46 -070048 // If uncached is true, then parse the current process map as of the call.
49 // Passing a map created with uncached set to true to Backtrace::Create()
50 // is unsupported.
51 static BacktraceMap* Create(pid_t pid, bool uncached = false);
Christopher Ferrisdf290612014-01-22 19:21:07 -080052
Christopher Ferris46756822014-01-14 20:16:30 -080053 virtual ~BacktraceMap();
54
Christopher Ferris12385e32015-02-06 13:22:01 -080055 // Fill in the map data structure for the given address.
56 virtual void FillIn(uintptr_t addr, backtrace_map_t* map);
Christopher Ferris46756822014-01-14 20:16:30 -080057
58 // The flags returned are the same flags as used by the mmap call.
59 // The values are PROT_*.
60 int GetFlags(uintptr_t pc) {
Christopher Ferris12385e32015-02-06 13:22:01 -080061 backtrace_map_t map;
62 FillIn(pc, &map);
63 if (IsValid(map)) {
64 return map.flags;
Christopher Ferris46756822014-01-14 20:16:30 -080065 }
66 return PROT_NONE;
67 }
68
69 bool IsReadable(uintptr_t pc) { return GetFlags(pc) & PROT_READ; }
70 bool IsWritable(uintptr_t pc) { return GetFlags(pc) & PROT_WRITE; }
71 bool IsExecutable(uintptr_t pc) { return GetFlags(pc) & PROT_EXEC; }
72
Christopher Ferrisdf290612014-01-22 19:21:07 -080073 typedef std::deque<backtrace_map_t>::iterator iterator;
Christopher Ferris46756822014-01-14 20:16:30 -080074 iterator begin() { return maps_.begin(); }
75 iterator end() { return maps_.end(); }
76
Christopher Ferrisdf290612014-01-22 19:21:07 -080077 typedef std::deque<backtrace_map_t>::const_iterator const_iterator;
Christopher Ferris46756822014-01-14 20:16:30 -080078 const_iterator begin() const { return maps_.begin(); }
79 const_iterator end() const { return maps_.end(); }
80
81 virtual bool Build();
82
Christopher Ferris12385e32015-02-06 13:22:01 -080083 static inline bool IsValid(const backtrace_map_t& map) {
84 return map.end > 0;
85 }
86
Christopher Ferris2106f4b2015-05-01 15:02:03 -070087 static uintptr_t GetRelativePc(const backtrace_map_t& map, uintptr_t pc) {
88 if (IsValid(map)) {
89 return pc - map.start + map.load_base;
90 } else {
91 return pc;
92 }
93 }
94
Christopher Ferris46756822014-01-14 20:16:30 -080095protected:
Christopher Ferrisdf290612014-01-22 19:21:07 -080096 BacktraceMap(pid_t pid);
97
Christopher Ferris46756822014-01-14 20:16:30 -080098 virtual bool ParseLine(const char* line, backtrace_map_t* map);
99
Christopher Ferrisdf290612014-01-22 19:21:07 -0800100 std::deque<backtrace_map_t> maps_;
Christopher Ferris46756822014-01-14 20:16:30 -0800101 pid_t pid_;
102};
103
104#endif // _BACKTRACE_BACKTRACE_MAP_H