Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | #ifndef __TEST_UTILS_H |
| 18 | #define __TEST_UTILS_H |
| 19 | #include <inttypes.h> |
| 20 | #include <sys/mman.h> |
Yabin Cui | 76144aa | 2015-11-19 13:52:16 -0800 | [diff] [blame] | 21 | #include <unistd.h> |
| 22 | |
| 23 | #include <atomic> |
| 24 | #include <string> |
| 25 | #include <regex> |
| 26 | |
| 27 | #include <base/file.h> |
| 28 | #include <base/stringprintf.h> |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 29 | |
| 30 | #include "private/ScopeGuard.h" |
| 31 | |
| 32 | struct map_record { |
| 33 | uintptr_t addr_start; |
| 34 | uintptr_t addr_end; |
| 35 | |
| 36 | int perms; |
| 37 | |
| 38 | size_t offset; |
| 39 | |
| 40 | dev_t device; |
| 41 | ino_t inode; |
| 42 | |
| 43 | std::string pathname; |
| 44 | }; |
| 45 | |
| 46 | class Maps { |
| 47 | public: |
| 48 | static bool parse_maps(std::vector<map_record>* maps) { |
Elliott Hughes | 15dfd63 | 2015-09-22 16:40:14 -0700 | [diff] [blame] | 49 | FILE* fp = fopen("/proc/self/maps", "re"); |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 50 | if (fp == nullptr) { |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | auto fp_guard = make_scope_guard([&]() { |
| 55 | fclose(fp); |
| 56 | }); |
| 57 | |
| 58 | char line[BUFSIZ]; |
| 59 | while (fgets(line, sizeof(line), fp) != nullptr) { |
| 60 | map_record record; |
Dmitriy Ivanov | 1dce3ed | 2015-04-06 19:05:58 -0700 | [diff] [blame] | 61 | uint32_t dev_major, dev_minor; |
Elliott Hughes | 15dfd63 | 2015-09-22 16:40:14 -0700 | [diff] [blame] | 62 | int path_offset; |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 63 | char prot[5]; // sizeof("rwxp") |
Elliott Hughes | 15dfd63 | 2015-09-22 16:40:14 -0700 | [diff] [blame] | 64 | if (sscanf(line, "%" SCNxPTR "-%" SCNxPTR " %4s %" SCNxPTR " %x:%x %lu %n", |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 65 | &record.addr_start, &record.addr_end, prot, &record.offset, |
Elliott Hughes | 15dfd63 | 2015-09-22 16:40:14 -0700 | [diff] [blame] | 66 | &dev_major, &dev_minor, &record.inode, &path_offset) == 7) { |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 67 | record.perms = 0; |
| 68 | if (prot[0] == 'r') { |
| 69 | record.perms |= PROT_READ; |
| 70 | } |
| 71 | if (prot[1] == 'w') { |
| 72 | record.perms |= PROT_WRITE; |
| 73 | } |
| 74 | if (prot[2] == 'x') { |
| 75 | record.perms |= PROT_EXEC; |
| 76 | } |
| 77 | |
| 78 | // TODO: parse shared/private? |
| 79 | |
| 80 | record.device = makedev(dev_major, dev_minor); |
Elliott Hughes | 15dfd63 | 2015-09-22 16:40:14 -0700 | [diff] [blame] | 81 | record.pathname = line + path_offset; |
| 82 | if (!record.pathname.empty() && record.pathname.back() == '\n') { |
| 83 | record.pathname.pop_back(); |
| 84 | } |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 85 | maps->push_back(record); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | return true; |
| 90 | } |
| 91 | }; |
| 92 | |
Yabin Cui | 76144aa | 2015-11-19 13:52:16 -0800 | [diff] [blame] | 93 | extern "C" pid_t gettid(); |
| 94 | |
| 95 | static inline void WaitUntilThreadSleep(std::atomic<pid_t>& tid) { |
| 96 | while (tid == 0) { |
| 97 | usleep(1000); |
| 98 | } |
| 99 | std::string filename = android::base::StringPrintf("/proc/%d/stat", tid.load()); |
| 100 | std::regex regex {R"(\s+S\s+)"}; |
| 101 | |
| 102 | while (true) { |
| 103 | std::string content; |
| 104 | ASSERT_TRUE(android::base::ReadFileToString(filename, &content)); |
| 105 | if (std::regex_search(content, regex)) { |
| 106 | break; |
| 107 | } |
| 108 | usleep(1000); |
| 109 | } |
| 110 | } |
| 111 | |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 112 | #endif |