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 <memory>
24 #include <string>
25 #include <vector>
26 
27 #include <unwindstack/MapInfo.h>
28 
29 namespace unwindstack {
30 
31 // Special flag to indicate a map is in /dev/. However, a map in
32 // /dev/ashmem/... does not set this flag.
33 static constexpr int MAPS_FLAGS_DEVICE_MAP = 0x8000;
34 // Special flag to indicate that this map represents an elf file
35 // created by ART for use with the gdb jit debug interface.
36 // This should only ever appear in offline maps data.
37 static constexpr int MAPS_FLAGS_JIT_SYMFILE_MAP = 0x4000;
38 
39 class Maps {
40  public:
41   virtual ~Maps() = default;
42 
43   Maps() = default;
44 
45   // Maps are not copyable but movable, because they own pointers to MapInfo
46   // objects.
47   Maps(const Maps&) = delete;
48   Maps& operator=(const Maps&) = delete;
49   Maps(Maps&&) = default;
50   Maps& operator=(Maps&&) = default;
51 
52   MapInfo* Find(uint64_t pc);
53 
54   virtual bool Parse();
55 
GetMapsFile()56   virtual const std::string GetMapsFile() const { return ""; }
57 
58   void Add(uint64_t start, uint64_t end, uint64_t offset, uint64_t flags, const std::string& name,
59            uint64_t load_bias);
60 
61   void Sort();
62 
63   typedef std::vector<std::unique_ptr<MapInfo>>::iterator iterator;
begin()64   iterator begin() { return maps_.begin(); }
end()65   iterator end() { return maps_.end(); }
66 
67   typedef std::vector<std::unique_ptr<MapInfo>>::const_iterator const_iterator;
begin()68   const_iterator begin() const { return maps_.begin(); }
end()69   const_iterator end() const { return maps_.end(); }
70 
Total()71   size_t Total() { return maps_.size(); }
72 
Get(size_t index)73   MapInfo* Get(size_t index) {
74     if (index >= maps_.size()) return nullptr;
75     return maps_[index].get();
76   }
77 
78  protected:
79   std::vector<std::unique_ptr<MapInfo>> maps_;
80 };
81 
82 class RemoteMaps : public Maps {
83  public:
RemoteMaps(pid_t pid)84   RemoteMaps(pid_t pid) : pid_(pid) {}
85   virtual ~RemoteMaps() = default;
86 
87   virtual const std::string GetMapsFile() const override;
88 
89  private:
90   pid_t pid_;
91 };
92 
93 class LocalMaps : public RemoteMaps {
94  public:
LocalMaps()95   LocalMaps() : RemoteMaps(getpid()) {}
96   virtual ~LocalMaps() = default;
97 };
98 
99 class LocalUpdatableMaps : public Maps {
100  public:
LocalUpdatableMaps()101   LocalUpdatableMaps() : Maps() {}
102   virtual ~LocalUpdatableMaps() = default;
103 
104   bool Reparse();
105 
106   const std::string GetMapsFile() const override;
107 
108  protected:
109   std::vector<std::unique_ptr<MapInfo>> saved_maps_;
110 };
111 
112 class BufferMaps : public Maps {
113  public:
BufferMaps(const char * buffer)114   BufferMaps(const char* buffer) : buffer_(buffer) {}
115   virtual ~BufferMaps() = default;
116 
117   bool Parse() override;
118 
119  private:
120   const char* buffer_;
121 };
122 
123 class FileMaps : public Maps {
124  public:
FileMaps(const std::string & file)125   FileMaps(const std::string& file) : file_(file) {}
126   virtual ~FileMaps() = default;
127 
GetMapsFile()128   const std::string GetMapsFile() const override { return file_; }
129 
130  protected:
131   const std::string file_;
132 };
133 
134 }  // namespace unwindstack
135 
136 #endif  // _LIBUNWINDSTACK_MAPS_H
137