BacktraceMap.h revision 46756821c4fe238f12a6e5ea18c356398f8d8795
1/*
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>
21#include <sys/mman.h>
22
23#include <string>
24#include <vector>
25
26struct backtrace_map_t {
27  uintptr_t start;
28  uintptr_t end;
29  int flags;
30  std::string name;
31};
32
33class BacktraceMap {
34public:
35  BacktraceMap(pid_t pid);
36  virtual ~BacktraceMap();
37
38  // Get the map data structure for the given address.
39  const backtrace_map_t* Find(uintptr_t addr);
40
41  // The flags returned are the same flags as used by the mmap call.
42  // The values are PROT_*.
43  int GetFlags(uintptr_t pc) {
44    const backtrace_map_t* map = Find(pc);
45    if (map) {
46      return map->flags;
47    }
48    return PROT_NONE;
49  }
50
51  bool IsReadable(uintptr_t pc) { return GetFlags(pc) & PROT_READ; }
52  bool IsWritable(uintptr_t pc) { return GetFlags(pc) & PROT_WRITE; }
53  bool IsExecutable(uintptr_t pc) { return GetFlags(pc) & PROT_EXEC; }
54
55  typedef std::vector<backtrace_map_t>::iterator iterator;
56  iterator begin() { return maps_.begin(); }
57  iterator end() { return maps_.end(); }
58
59  typedef std::vector<backtrace_map_t>::const_iterator const_iterator;
60  const_iterator begin() const { return maps_.begin(); }
61  const_iterator end() const { return maps_.end(); }
62
63  virtual bool Build();
64
65protected:
66  virtual bool ParseLine(const char* line, backtrace_map_t* map);
67
68  std::vector<backtrace_map_t> maps_;
69  pid_t pid_;
70};
71
72#endif // _BACKTRACE_BACKTRACE_MAP_H
73