• 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 #include <errno.h>
18 #include <fcntl.h>
19 #include <inttypes.h>
20 #include <stdint.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <sys/mman.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26 
27 #include <android-base/unique_fd.h>
28 #include <procinfo/process_map.h>
29 
30 #include <algorithm>
31 #include <cctype>
32 #include <memory>
33 #include <string>
34 #include <vector>
35 
36 #include <unwindstack/Elf.h>
37 #include <unwindstack/Maps.h>
38 #include <unwindstack/Memory.h>
39 
40 namespace unwindstack {
41 
Find(uint64_t pc)42 MapInfo* Maps::Find(uint64_t pc) {
43   if (maps_.empty()) {
44     return nullptr;
45   }
46   size_t first = 0;
47   size_t last = maps_.size();
48   while (first < last) {
49     size_t index = (first + last) / 2;
50     const auto& cur = maps_[index];
51     if (pc >= cur->start && pc < cur->end) {
52       return cur.get();
53     } else if (pc < cur->start) {
54       last = index;
55     } else {
56       first = index + 1;
57     }
58   }
59   return nullptr;
60 }
61 
Parse()62 bool Maps::Parse() {
63   return android::procinfo::ReadMapFile(
64       GetMapsFile(),
65       [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, ino_t, const char* name) {
66         // Mark a device map in /dev/ and not in /dev/ashmem/ specially.
67         if (strncmp(name, "/dev/", 5) == 0 && strncmp(name + 5, "ashmem/", 7) != 0) {
68           flags |= unwindstack::MAPS_FLAGS_DEVICE_MAP;
69         }
70         maps_.emplace_back(
71             new MapInfo(maps_.empty() ? nullptr : maps_.back().get(), start, end, pgoff,
72                         flags, name));
73       });
74 }
75 
Add(uint64_t start,uint64_t end,uint64_t offset,uint64_t flags,const std::string & name,uint64_t load_bias)76 void Maps::Add(uint64_t start, uint64_t end, uint64_t offset, uint64_t flags,
77                const std::string& name, uint64_t load_bias) {
78   auto map_info =
79       std::make_unique<MapInfo>(maps_.empty() ? nullptr : maps_.back().get(), start, end, offset,
80                                 flags, name);
81   map_info->load_bias = load_bias;
82   maps_.emplace_back(std::move(map_info));
83 }
84 
Sort()85 void Maps::Sort() {
86   std::sort(maps_.begin(), maps_.end(),
87             [](const std::unique_ptr<MapInfo>& a, const std::unique_ptr<MapInfo>& b) {
88               return a->start < b->start; });
89 
90   // Set the prev_map values on the info objects.
91   MapInfo* prev_map = nullptr;
92   for (const auto& map_info : maps_) {
93     map_info->prev_map = prev_map;
94     prev_map = map_info.get();
95   }
96 }
97 
Parse()98 bool BufferMaps::Parse() {
99   std::string content(buffer_);
100   return android::procinfo::ReadMapFileContent(
101       &content[0],
102       [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, ino_t, const char* name) {
103         // Mark a device map in /dev/ and not in /dev/ashmem/ specially.
104         if (strncmp(name, "/dev/", 5) == 0 && strncmp(name + 5, "ashmem/", 7) != 0) {
105           flags |= unwindstack::MAPS_FLAGS_DEVICE_MAP;
106         }
107         maps_.emplace_back(
108             new MapInfo(maps_.empty() ? nullptr : maps_.back().get(), start, end, pgoff,
109                         flags, name));
110       });
111 }
112 
GetMapsFile() const113 const std::string RemoteMaps::GetMapsFile() const {
114   return "/proc/" + std::to_string(pid_) + "/maps";
115 }
116 
GetMapsFile() const117 const std::string LocalUpdatableMaps::GetMapsFile() const {
118   return "/proc/self/maps";
119 }
120 
Reparse()121 bool LocalUpdatableMaps::Reparse() {
122   // New maps will be added at the end without deleting the old ones.
123   size_t last_map_idx = maps_.size();
124   if (!Parse()) {
125     maps_.resize(last_map_idx);
126     return false;
127   }
128 
129   size_t total_entries = maps_.size();
130   size_t search_map_idx = 0;
131   for (size_t new_map_idx = last_map_idx; new_map_idx < maps_.size(); new_map_idx++) {
132     auto& new_map_info = maps_[new_map_idx];
133     uint64_t start = new_map_info->start;
134     uint64_t end = new_map_info->end;
135     uint64_t flags = new_map_info->flags;
136     std::string* name = &new_map_info->name;
137     for (size_t old_map_idx = search_map_idx; old_map_idx < last_map_idx; old_map_idx++) {
138       auto& info = maps_[old_map_idx];
139       if (start == info->start && end == info->end && flags == info->flags && *name == info->name) {
140         // No need to check
141         search_map_idx = old_map_idx + 1;
142         maps_[new_map_idx] = nullptr;
143         total_entries--;
144         break;
145       } else if (info->start > start) {
146         // Stop, there isn't going to be a match.
147         search_map_idx = old_map_idx;
148         break;
149       }
150 
151       // Never delete these maps, they may be in use. The assumption is
152       // that there will only every be a handfull of these so waiting
153       // to destroy them is not too expensive.
154       saved_maps_.emplace_back(std::move(info));
155       maps_[old_map_idx] = nullptr;
156       total_entries--;
157     }
158     if (search_map_idx >= last_map_idx) {
159       break;
160     }
161   }
162 
163   // Now move out any of the maps that never were found.
164   for (size_t i = search_map_idx; i < last_map_idx; i++) {
165     saved_maps_.emplace_back(std::move(maps_[i]));
166     maps_[i] = nullptr;
167     total_entries--;
168   }
169 
170   // Sort all of the values such that the nullptrs wind up at the end, then
171   // resize them away.
172   std::sort(maps_.begin(), maps_.end(), [](const auto& a, const auto& b) {
173     if (a == nullptr) {
174       return false;
175     } else if (b == nullptr) {
176       return true;
177     }
178     return a->start < b->start;
179   });
180   maps_.resize(total_entries);
181 
182   return true;
183 }
184 
185 }  // namespace unwindstack
186