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 #pragma once
18 
19 #include <pthread.h>
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   virtual std::shared_ptr<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   void Add(uint64_t start, uint64_t end, uint64_t offset, uint64_t flags, const std::string& name,
60            uint64_t load_bias);
61 
62   void Sort();
63 
64   typedef std::vector<std::shared_ptr<MapInfo>>::iterator iterator;
begin()65   iterator begin() { return maps_.begin(); }
end()66   iterator end() { return maps_.end(); }
67 
68   typedef std::vector<std::shared_ptr<MapInfo>>::const_iterator const_iterator;
begin()69   const_iterator begin() const { return maps_.begin(); }
end()70   const_iterator end() const { return maps_.end(); }
71 
Total()72   size_t Total() { return maps_.size(); }
73 
Get(size_t index)74   std::shared_ptr<MapInfo> Get(size_t index) {
75     if (index >= maps_.size()) return nullptr;
76     return maps_[index];
77   }
78 
79  protected:
80   std::vector<std::shared_ptr<MapInfo>> maps_;
81 };
82 
83 class RemoteMaps : public Maps {
84  public:
RemoteMaps(pid_t pid)85   RemoteMaps(pid_t pid) : pid_(pid) {}
86   virtual ~RemoteMaps() = default;
87 
88   virtual const std::string GetMapsFile() const override;
89 
90  private:
91   pid_t pid_;
92 };
93 
94 class LocalMaps : public RemoteMaps {
95  public:
LocalMaps()96   LocalMaps() : RemoteMaps(getpid()) {}
97   virtual ~LocalMaps() = default;
98 };
99 
100 class LocalUpdatableMaps : public Maps {
101  public:
102   LocalUpdatableMaps();
103   virtual ~LocalUpdatableMaps() = default;
104 
105   std::shared_ptr<MapInfo> Find(uint64_t pc) override;
106 
107   bool Parse() override;
108 
109   const std::string GetMapsFile() const override;
110 
111   bool Reparse(/*out*/ bool* any_changed = nullptr);
112 
113  private:
114   pthread_rwlock_t maps_rwlock_;
115 };
116 
117 class BufferMaps : public Maps {
118  public:
BufferMaps(const char * buffer)119   BufferMaps(const char* buffer) : buffer_(buffer) {}
120   virtual ~BufferMaps() = default;
121 
122   bool Parse() override;
123 
124  private:
125   const char* buffer_;
126 };
127 
128 class FileMaps : public Maps {
129  public:
FileMaps(const std::string & file)130   FileMaps(const std::string& file) : file_(file) {}
131   virtual ~FileMaps() = default;
132 
GetMapsFile()133   const std::string GetMapsFile() const override { return file_; }
134 
135  protected:
136   const std::string file_;
137 };
138 
139 }  // namespace unwindstack
140