| Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [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 |  | 
| Yabin Cui | 3841acc | 2018-05-10 17:19:12 -0700 | [diff] [blame] | 17 | #include <errno.h> | 
| Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 18 | #include <fcntl.h> | 
| Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 19 | #include <inttypes.h> | 
| Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 20 | #include <string.h> | 
| Sandeep Patil | f31c709 | 2019-01-30 17:43:22 -0800 | [diff] [blame] | 21 | #include <sys/types.h> | 
| Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 22 | #include <unistd.h> | 
|  | 23 |  | 
|  | 24 | #include <android-base/unique_fd.h> | 
| Yabin Cui | 3841acc | 2018-05-10 17:19:12 -0700 | [diff] [blame] | 25 | #include <procinfo/process_map.h> | 
| Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 26 |  | 
| Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 27 | #include "ProcessMappings.h" | 
| Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 28 |  | 
| Colin Cross | a9939e9 | 2017-06-21 13:13:00 -0700 | [diff] [blame] | 29 | namespace android { | 
|  | 30 |  | 
| Yabin Cui | 3841acc | 2018-05-10 17:19:12 -0700 | [diff] [blame] | 31 | struct ReadMapCallback { | 
|  | 32 | ReadMapCallback(allocator::vector<Mapping>& mappings) : mappings_(mappings) {} | 
|  | 33 |  | 
| Sandeep Patil | f31c709 | 2019-01-30 17:43:22 -0800 | [diff] [blame] | 34 | void operator()(uint64_t start, uint64_t end, uint16_t flags, uint64_t, ino_t, | 
|  | 35 | const char* name) const { | 
| Yabin Cui | 3841acc | 2018-05-10 17:19:12 -0700 | [diff] [blame] | 36 | mappings_.emplace_back(start, end, flags & PROT_READ, flags & PROT_WRITE, flags & PROT_EXEC, | 
|  | 37 | name); | 
|  | 38 | } | 
|  | 39 |  | 
|  | 40 | allocator::vector<Mapping>& mappings_; | 
|  | 41 | }; | 
|  | 42 |  | 
| Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 43 | bool ProcessMappings(pid_t pid, allocator::vector<Mapping>& mappings) { | 
|  | 44 | char map_buffer[1024]; | 
|  | 45 | snprintf(map_buffer, sizeof(map_buffer), "/proc/%d/maps", pid); | 
| Elliott Hughes | 2c5d1d7 | 2016-03-28 12:15:36 -0700 | [diff] [blame] | 46 | android::base::unique_fd fd(open(map_buffer, O_RDONLY)); | 
|  | 47 | if (fd == -1) { | 
| Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 48 | return false; | 
|  | 49 | } | 
| Yabin Cui | 3841acc | 2018-05-10 17:19:12 -0700 | [diff] [blame] | 50 | allocator::string content(mappings.get_allocator()); | 
|  | 51 | ssize_t n; | 
|  | 52 | while ((n = TEMP_FAILURE_RETRY(read(fd, map_buffer, sizeof(map_buffer)))) > 0) { | 
|  | 53 | content.append(map_buffer, n); | 
| Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 54 | } | 
| Yabin Cui | 3841acc | 2018-05-10 17:19:12 -0700 | [diff] [blame] | 55 | ReadMapCallback callback(mappings); | 
|  | 56 | return android::procinfo::ReadMapFileContent(&content[0], callback); | 
| Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 57 | } | 
| Colin Cross | a9939e9 | 2017-06-21 13:13:00 -0700 | [diff] [blame] | 58 |  | 
|  | 59 | }  // namespace android |