blob: 4c3aef4cd9a08d91f6277e676ea07cf4f2bef6ee [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
Elliott Hughes33697a02016-01-26 13:04:57 -080019
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070020#include <inttypes.h>
21#include <sys/mman.h>
Elliott Hughes33697a02016-01-26 13:04:57 -080022#include <sys/types.h>
23#include <sys/wait.h>
Yabin Cui76144aa2015-11-19 13:52:16 -080024#include <unistd.h>
25
26#include <atomic>
27#include <string>
28#include <regex>
29
Elliott Hughes939a7e02015-12-04 15:27:46 -080030#include <android-base/file.h>
31#include <android-base/stringprintf.h>
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070032
33#include "private/ScopeGuard.h"
34
Elliott Hughesffbb0f82016-10-10 18:34:27 -070035#if defined(__linux__)
36
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070037struct map_record {
38 uintptr_t addr_start;
39 uintptr_t addr_end;
40
41 int perms;
42
43 size_t offset;
44
45 dev_t device;
46 ino_t inode;
47
48 std::string pathname;
49};
50
51class Maps {
52 public:
53 static bool parse_maps(std::vector<map_record>* maps) {
Elliott Hughes15dfd632015-09-22 16:40:14 -070054 FILE* fp = fopen("/proc/self/maps", "re");
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070055 if (fp == nullptr) {
56 return false;
57 }
58
59 auto fp_guard = make_scope_guard([&]() {
60 fclose(fp);
61 });
62
63 char line[BUFSIZ];
64 while (fgets(line, sizeof(line), fp) != nullptr) {
65 map_record record;
Dmitriy Ivanov1dce3ed2015-04-06 19:05:58 -070066 uint32_t dev_major, dev_minor;
Elliott Hughes15dfd632015-09-22 16:40:14 -070067 int path_offset;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070068 char prot[5]; // sizeof("rwxp")
Elliott Hughes15dfd632015-09-22 16:40:14 -070069 if (sscanf(line, "%" SCNxPTR "-%" SCNxPTR " %4s %" SCNxPTR " %x:%x %lu %n",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070070 &record.addr_start, &record.addr_end, prot, &record.offset,
Elliott Hughes15dfd632015-09-22 16:40:14 -070071 &dev_major, &dev_minor, &record.inode, &path_offset) == 7) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070072 record.perms = 0;
73 if (prot[0] == 'r') {
74 record.perms |= PROT_READ;
75 }
76 if (prot[1] == 'w') {
77 record.perms |= PROT_WRITE;
78 }
79 if (prot[2] == 'x') {
80 record.perms |= PROT_EXEC;
81 }
82
83 // TODO: parse shared/private?
84
85 record.device = makedev(dev_major, dev_minor);
Elliott Hughes15dfd632015-09-22 16:40:14 -070086 record.pathname = line + path_offset;
87 if (!record.pathname.empty() && record.pathname.back() == '\n') {
88 record.pathname.pop_back();
89 }
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070090 maps->push_back(record);
91 }
92 }
93
94 return true;
95 }
96};
97
Yabin Cui76144aa2015-11-19 13:52:16 -080098extern "C" pid_t gettid();
99
Elliott Hughesffbb0f82016-10-10 18:34:27 -0700100#endif
101
Yabin Cui76144aa2015-11-19 13:52:16 -0800102static inline void WaitUntilThreadSleep(std::atomic<pid_t>& tid) {
103 while (tid == 0) {
104 usleep(1000);
105 }
106 std::string filename = android::base::StringPrintf("/proc/%d/stat", tid.load());
107 std::regex regex {R"(\s+S\s+)"};
108
109 while (true) {
110 std::string content;
111 ASSERT_TRUE(android::base::ReadFileToString(filename, &content));
112 if (std::regex_search(content, regex)) {
113 break;
114 }
115 usleep(1000);
116 }
117}
118
Elliott Hughes33697a02016-01-26 13:04:57 -0800119static inline void AssertChildExited(int pid, int expected_exit_status) {
120 int status;
121 ASSERT_EQ(pid, waitpid(pid, &status, 0));
122 ASSERT_TRUE(WIFEXITED(status));
123 ASSERT_EQ(expected_exit_status, WEXITSTATUS(status));
124}
125
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -0700126// The absolute path to the executable
127const std::string& get_executable_path();
Dimitry Ivanovd17a3772016-03-01 13:11:28 -0800128
Dimitry Ivanova36e59b2016-09-01 11:37:39 -0700129// Get realpath
130bool get_realpath(const std::string& path, std::string* realpath);
Dimitry Ivanovd0b5c3a2016-11-25 12:23:11 -0800131// Get dirname
132std::string get_dirname(const char* path);
Dimitry Ivanova36e59b2016-09-01 11:37:39 -0700133
Dimitry Ivanov55437462016-07-20 15:33:07 -0700134// Access to argc/argv/envp
135int get_argc();
136char** get_argv();
137char** get_envp();
138
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700139#endif