blob: a53293a2570c32fa864d05094eee29f8d60c17b4 [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>
Christopher Ferris0dbce452014-01-17 08:25:26 -080021#ifdef USE_MINGW
22// MINGW does not define these constants.
23#define PROT_NONE 0
24#define PROT_READ 0x1
25#define PROT_WRITE 0x2
26#define PROT_EXEC 0x4
27#else
Christopher Ferris46756822014-01-14 20:16:30 -080028#include <sys/mman.h>
Christopher Ferris0dbce452014-01-17 08:25:26 -080029#endif
Christopher Ferris46756822014-01-14 20:16:30 -080030
31#include <string>
32#include <vector>
33
34struct backtrace_map_t {
35 uintptr_t start;
36 uintptr_t end;
37 int flags;
38 std::string name;
39};
40
41class BacktraceMap {
42public:
43 BacktraceMap(pid_t pid);
44 virtual ~BacktraceMap();
45
46 // Get the map data structure for the given address.
47 const backtrace_map_t* Find(uintptr_t addr);
48
49 // The flags returned are the same flags as used by the mmap call.
50 // The values are PROT_*.
51 int GetFlags(uintptr_t pc) {
52 const backtrace_map_t* map = Find(pc);
53 if (map) {
54 return map->flags;
55 }
56 return PROT_NONE;
57 }
58
59 bool IsReadable(uintptr_t pc) { return GetFlags(pc) & PROT_READ; }
60 bool IsWritable(uintptr_t pc) { return GetFlags(pc) & PROT_WRITE; }
61 bool IsExecutable(uintptr_t pc) { return GetFlags(pc) & PROT_EXEC; }
62
63 typedef std::vector<backtrace_map_t>::iterator iterator;
64 iterator begin() { return maps_.begin(); }
65 iterator end() { return maps_.end(); }
66
67 typedef std::vector<backtrace_map_t>::const_iterator const_iterator;
68 const_iterator begin() const { return maps_.begin(); }
69 const_iterator end() const { return maps_.end(); }
70
71 virtual bool Build();
72
73protected:
74 virtual bool ParseLine(const char* line, backtrace_map_t* map);
75
76 std::vector<backtrace_map_t> maps_;
77 pid_t pid_;
78};
79
80#endif // _BACKTRACE_BACKTRACE_MAP_H