blob: c717f098cbb4d03a3679bbbc3b727ad2913bd6de [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 {
36 uintptr_t start;
37 uintptr_t end;
38 int flags;
39 std::string name;
40};
41
42class BacktraceMap {
43public:
Christopher Ferrisdf290612014-01-22 19:21:07 -080044 static BacktraceMap* Create(pid_t pid);
45
Christopher Ferris46756822014-01-14 20:16:30 -080046 virtual ~BacktraceMap();
47
48 // Get the map data structure for the given address.
Christopher Ferrise2960912014-03-07 19:42:19 -080049 virtual const backtrace_map_t* Find(uintptr_t addr);
Christopher Ferris46756822014-01-14 20:16:30 -080050
51 // The flags returned are the same flags as used by the mmap call.
52 // The values are PROT_*.
53 int GetFlags(uintptr_t pc) {
54 const backtrace_map_t* map = Find(pc);
55 if (map) {
56 return map->flags;
57 }
58 return PROT_NONE;
59 }
60
61 bool IsReadable(uintptr_t pc) { return GetFlags(pc) & PROT_READ; }
62 bool IsWritable(uintptr_t pc) { return GetFlags(pc) & PROT_WRITE; }
63 bool IsExecutable(uintptr_t pc) { return GetFlags(pc) & PROT_EXEC; }
64
Christopher Ferrisdf290612014-01-22 19:21:07 -080065 typedef std::deque<backtrace_map_t>::iterator iterator;
Christopher Ferris46756822014-01-14 20:16:30 -080066 iterator begin() { return maps_.begin(); }
67 iterator end() { return maps_.end(); }
68
Christopher Ferrisdf290612014-01-22 19:21:07 -080069 typedef std::deque<backtrace_map_t>::const_iterator const_iterator;
Christopher Ferris46756822014-01-14 20:16:30 -080070 const_iterator begin() const { return maps_.begin(); }
71 const_iterator end() const { return maps_.end(); }
72
73 virtual bool Build();
74
75protected:
Christopher Ferrisdf290612014-01-22 19:21:07 -080076 BacktraceMap(pid_t pid);
77
Christopher Ferris46756822014-01-14 20:16:30 -080078 virtual bool ParseLine(const char* line, backtrace_map_t* map);
79
Christopher Ferrisdf290612014-01-22 19:21:07 -080080 std::deque<backtrace_map_t> maps_;
Christopher Ferris46756822014-01-14 20:16:30 -080081 pid_t pid_;
82};
83
84#endif // _BACKTRACE_BACKTRACE_MAP_H