| Christopher Ferris | 09385e7 | 2017-04-05 13:25:04 -0700 | [diff] [blame] | 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 <sys/mman.h> | 
|  | 23 | #include <sys/types.h> | 
|  | 24 | #include <unistd.h> | 
|  | 25 |  | 
|  | 26 | #include <android-base/unique_fd.h> | 
| Yabin Cui | 3841acc | 2018-05-10 17:19:12 -0700 | [diff] [blame] | 27 | #include <procinfo/process_map.h> | 
| Christopher Ferris | 09385e7 | 2017-04-05 13:25:04 -0700 | [diff] [blame] | 28 |  | 
| Yabin Cui | d5b22c5 | 2018-02-22 17:11:31 -0800 | [diff] [blame] | 29 | #include <algorithm> | 
| Christopher Ferris | 60521c7 | 2017-08-18 15:10:53 -0700 | [diff] [blame] | 30 | #include <cctype> | 
| Christopher Ferris | 09385e7 | 2017-04-05 13:25:04 -0700 | [diff] [blame] | 31 | #include <memory> | 
|  | 32 | #include <string> | 
|  | 33 | #include <vector> | 
|  | 34 |  | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 35 | #include <unwindstack/Elf.h> | 
|  | 36 | #include <unwindstack/Maps.h> | 
|  | 37 | #include <unwindstack/Memory.h> | 
|  | 38 |  | 
|  | 39 | namespace unwindstack { | 
| Christopher Ferris | 09385e7 | 2017-04-05 13:25:04 -0700 | [diff] [blame] | 40 |  | 
|  | 41 | MapInfo* Maps::Find(uint64_t pc) { | 
|  | 42 | if (maps_.empty()) { | 
|  | 43 | return nullptr; | 
|  | 44 | } | 
|  | 45 | size_t first = 0; | 
|  | 46 | size_t last = maps_.size(); | 
|  | 47 | while (first < last) { | 
|  | 48 | size_t index = (first + last) / 2; | 
| Christopher Ferris | be788d8 | 2017-11-27 14:50:38 -0800 | [diff] [blame] | 49 | MapInfo* cur = maps_[index]; | 
| Christopher Ferris | 09385e7 | 2017-04-05 13:25:04 -0700 | [diff] [blame] | 50 | if (pc >= cur->start && pc < cur->end) { | 
|  | 51 | return cur; | 
|  | 52 | } else if (pc < cur->start) { | 
|  | 53 | last = index; | 
|  | 54 | } else { | 
|  | 55 | first = index + 1; | 
|  | 56 | } | 
|  | 57 | } | 
|  | 58 | return nullptr; | 
|  | 59 | } | 
|  | 60 |  | 
| Christopher Ferris | 09385e7 | 2017-04-05 13:25:04 -0700 | [diff] [blame] | 61 | bool Maps::Parse() { | 
| Yabin Cui | 3841acc | 2018-05-10 17:19:12 -0700 | [diff] [blame] | 62 | return android::procinfo::ReadMapFile( | 
|  | 63 | GetMapsFile(), | 
|  | 64 | [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, const char* name) { | 
|  | 65 | // Mark a device map in /dev/ and not in /dev/ashmem/ specially. | 
|  | 66 | if (strncmp(name, "/dev/", 5) == 0 && strncmp(name + 5, "ashmem/", 7) != 0) { | 
|  | 67 | flags |= unwindstack::MAPS_FLAGS_DEVICE_MAP; | 
|  | 68 | } | 
|  | 69 | maps_.push_back(new MapInfo(start, end, pgoff, flags, name)); | 
|  | 70 | }); | 
| Christopher Ferris | 09385e7 | 2017-04-05 13:25:04 -0700 | [diff] [blame] | 71 | } | 
|  | 72 |  | 
| Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 73 | void Maps::Add(uint64_t start, uint64_t end, uint64_t offset, uint64_t flags, | 
|  | 74 | const std::string& name, uint64_t load_bias) { | 
|  | 75 | MapInfo* map_info = new MapInfo(start, end, offset, flags, name); | 
|  | 76 | map_info->load_bias = load_bias; | 
|  | 77 | maps_.push_back(map_info); | 
|  | 78 | } | 
|  | 79 |  | 
| Yabin Cui | d5b22c5 | 2018-02-22 17:11:31 -0800 | [diff] [blame] | 80 | void Maps::Sort() { | 
|  | 81 | std::sort(maps_.begin(), maps_.end(), | 
|  | 82 | [](const MapInfo* a, const MapInfo* b) { return a->start < b->start; }); | 
|  | 83 | } | 
|  | 84 |  | 
| Christopher Ferris | 09385e7 | 2017-04-05 13:25:04 -0700 | [diff] [blame] | 85 | Maps::~Maps() { | 
|  | 86 | for (auto& map : maps_) { | 
| Christopher Ferris | be788d8 | 2017-11-27 14:50:38 -0800 | [diff] [blame] | 87 | delete map; | 
| Christopher Ferris | 09385e7 | 2017-04-05 13:25:04 -0700 | [diff] [blame] | 88 | } | 
|  | 89 | } | 
|  | 90 |  | 
|  | 91 | bool BufferMaps::Parse() { | 
| Yabin Cui | 3841acc | 2018-05-10 17:19:12 -0700 | [diff] [blame] | 92 | std::string content(buffer_); | 
|  | 93 | return android::procinfo::ReadMapFileContent( | 
|  | 94 | &content[0], | 
|  | 95 | [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, const char* name) { | 
|  | 96 | // Mark a device map in /dev/ and not in /dev/ashmem/ specially. | 
|  | 97 | if (strncmp(name, "/dev/", 5) == 0 && strncmp(name + 5, "ashmem/", 7) != 0) { | 
|  | 98 | flags |= unwindstack::MAPS_FLAGS_DEVICE_MAP; | 
|  | 99 | } | 
|  | 100 | maps_.push_back(new MapInfo(start, end, pgoff, flags, name)); | 
|  | 101 | }); | 
| Christopher Ferris | 09385e7 | 2017-04-05 13:25:04 -0700 | [diff] [blame] | 102 | } | 
|  | 103 |  | 
|  | 104 | const std::string RemoteMaps::GetMapsFile() const { | 
|  | 105 | return "/proc/" + std::to_string(pid_) + "/maps"; | 
|  | 106 | } | 
|  | 107 |  | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 108 | }  // namespace unwindstack |