1/*
2 * Copyright (C) 2016 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 _LIBUNWINDSTACK_MAPS_H
18#define _LIBUNWINDSTACK_MAPS_H
19
20#include <sys/types.h>
21#include <unistd.h>
22
23#include <string>
24#include <vector>
25
26#include <unwindstack/MapInfo.h>
27
28namespace unwindstack {
29
30// Special flag to indicate a map is in /dev/. However, a map in
31// /dev/ashmem/... does not set this flag.
32static constexpr int MAPS_FLAGS_DEVICE_MAP = 0x8000;
33
34class Maps {
35 public:
36  Maps() = default;
37  virtual ~Maps();
38
39  MapInfo* Find(uint64_t pc);
40
41  virtual bool Parse();
42
43  virtual const std::string GetMapsFile() const { return ""; }
44
45  void Add(uint64_t start, uint64_t end, uint64_t offset, uint64_t flags, const std::string& name,
46           uint64_t load_bias);
47
48  typedef std::vector<MapInfo*>::iterator iterator;
49  iterator begin() { return maps_.begin(); }
50  iterator end() { return maps_.end(); }
51
52  typedef std::vector<MapInfo*>::const_iterator const_iterator;
53  const_iterator begin() const { return maps_.begin(); }
54  const_iterator end() const { return maps_.end(); }
55
56  size_t Total() { return maps_.size(); }
57
58  MapInfo* Get(size_t index) {
59    if (index >= maps_.size()) return nullptr;
60    return maps_[index];
61  }
62
63 protected:
64  std::vector<MapInfo*> maps_;
65};
66
67class RemoteMaps : public Maps {
68 public:
69  RemoteMaps(pid_t pid) : pid_(pid) {}
70  virtual ~RemoteMaps() = default;
71
72  virtual const std::string GetMapsFile() const override;
73
74 private:
75  pid_t pid_;
76};
77
78class LocalMaps : public RemoteMaps {
79 public:
80  LocalMaps() : RemoteMaps(getpid()) {}
81  virtual ~LocalMaps() = default;
82};
83
84class BufferMaps : public Maps {
85 public:
86  BufferMaps(const char* buffer) : buffer_(buffer) {}
87  virtual ~BufferMaps() = default;
88
89  bool Parse() override;
90
91 private:
92  const char* buffer_;
93};
94
95class FileMaps : public Maps {
96 public:
97  FileMaps(const std::string& file) : file_(file) {}
98  virtual ~FileMaps() = default;
99
100  const std::string GetMapsFile() const override { return file_; }
101
102 protected:
103  const std::string file_;
104};
105
106}  // namespace unwindstack
107
108#endif  // _LIBUNWINDSTACK_MAPS_H
109