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