blob: a8f3441da2b8835a79c76aacb06f4b5b909724d5 [file] [log] [blame]
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001/*
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 Cui76144aa2015-11-19 13:52:16 -080021#include <unistd.h>
22
23#include <atomic>
24#include <string>
25#include <regex>
26
Elliott Hughes939a7e02015-12-04 15:27:46 -080027#include <android-base/file.h>
28#include <android-base/stringprintf.h>
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070029
30#include "private/ScopeGuard.h"
31
32struct 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
46class Maps {
47 public:
48 static bool parse_maps(std::vector<map_record>* maps) {
Elliott Hughes15dfd632015-09-22 16:40:14 -070049 FILE* fp = fopen("/proc/self/maps", "re");
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070050 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 Ivanov1dce3ed2015-04-06 19:05:58 -070061 uint32_t dev_major, dev_minor;
Elliott Hughes15dfd632015-09-22 16:40:14 -070062 int path_offset;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070063 char prot[5]; // sizeof("rwxp")
Elliott Hughes15dfd632015-09-22 16:40:14 -070064 if (sscanf(line, "%" SCNxPTR "-%" SCNxPTR " %4s %" SCNxPTR " %x:%x %lu %n",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070065 &record.addr_start, &record.addr_end, prot, &record.offset,
Elliott Hughes15dfd632015-09-22 16:40:14 -070066 &dev_major, &dev_minor, &record.inode, &path_offset) == 7) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070067 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 Hughes15dfd632015-09-22 16:40:14 -070081 record.pathname = line + path_offset;
82 if (!record.pathname.empty() && record.pathname.back() == '\n') {
83 record.pathname.pop_back();
84 }
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070085 maps->push_back(record);
86 }
87 }
88
89 return true;
90 }
91};
92
Yabin Cui76144aa2015-11-19 13:52:16 -080093extern "C" pid_t gettid();
94
95static 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 Ivanovaae859c2015-03-31 11:14:03 -0700112#endif