2 * Copyright (C) 2012 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef __TEST_UTILS_H
18 #define __TEST_UTILS_H
27 #include <android-base/file.h>
28 #include <android-base/stringprintf.h>
30 #include "private/ScopeGuard.h"
48 static bool parse_maps(std::vector<map_record>* maps) {
49 FILE* fp = fopen("/proc/self/maps", "re");
54 auto fp_guard = make_scope_guard([&]() {
59 while (fgets(line, sizeof(line), fp) != nullptr) {
61 uint32_t dev_major, dev_minor;
63 char prot[5]; // sizeof("rwxp")
64 if (sscanf(line, "%" SCNxPTR "-%" SCNxPTR " %4s %" SCNxPTR " %x:%x %lu %n",
65 &record.addr_start, &record.addr_end, prot, &record.offset,
66 &dev_major, &dev_minor, &record.inode, &path_offset) == 7) {
69 record.perms |= PROT_READ;
72 record.perms |= PROT_WRITE;
75 record.perms |= PROT_EXEC;
78 // TODO: parse shared/private?
80 record.device = makedev(dev_major, dev_minor);
81 record.pathname = line + path_offset;
82 if (!record.pathname.empty() && record.pathname.back() == '\n') {
83 record.pathname.pop_back();
85 maps->push_back(record);
93 extern "C" pid_t gettid();
95 static inline void WaitUntilThreadSleep(std::atomic<pid_t>& tid) {
99 std::string filename = android::base::StringPrintf("/proc/%d/stat", tid.load());
100 std::regex regex {R"(\s+S\s+)"};
104 ASSERT_TRUE(android::base::ReadFileToString(filename, &content));
105 if (std::regex_search(content, regex)) {