blob: 1d5c53693279d4f2bf47cccf177b6469363d8497 [file] [log] [blame]
Vladimir Marko1352f132017-04-28 15:28:29 +01001/*
2 * Copyright (C) 2015 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 ART_OATDUMP_OATDUMP_TEST_H_
18#define ART_OATDUMP_OATDUMP_TEST_H_
19
20#include <sstream>
21#include <string>
22#include <vector>
23
24#include "android-base/strings.h"
25
Andreas Gampe2c30e4a2017-08-23 11:31:32 -070026#include "arch/instruction_set.h"
Vladimir Marko1352f132017-04-28 15:28:29 +010027#include "base/unix_file/fd_file.h"
Andreas Gampe2c30e4a2017-08-23 11:31:32 -070028#include "common_runtime_test.h"
29#include "exec_utils.h"
30#include "gc/heap.h"
31#include "gc/space/image_space.h"
32#include "os.h"
Vladimir Marko1352f132017-04-28 15:28:29 +010033#include "utils.h"
34
35#include <sys/types.h>
36#include <unistd.h>
37
38namespace art {
39
40class OatDumpTest : public CommonRuntimeTest {
41 protected:
42 virtual void SetUp() {
43 CommonRuntimeTest::SetUp();
44 core_art_location_ = GetCoreArtLocation();
45 core_oat_location_ = GetSystemImageFilename(GetCoreOatLocation().c_str(), kRuntimeISA);
46 }
47
48 // Linking flavor.
49 enum Flavor {
50 kDynamic, // oatdump(d)
51 kStatic, // oatdump(d)s
52 };
53
54 // Returns path to the oatdump binary.
55 std::string GetOatDumpFilePath(Flavor flavor) {
56 std::string root = GetTestAndroidRoot();
57 root += "/bin/oatdump";
58 if (kIsDebugBuild) {
59 root += "d";
60 }
61 if (flavor == kStatic) {
62 root += "s";
63 }
64 return root;
65 }
66
67 enum Mode {
68 kModeOat,
69 kModeArt,
70 kModeSymbolize,
71 };
72
73 // Display style.
74 enum Display {
75 kListOnly,
76 kListAndCode
77 };
78
79 // Run the test with custom arguments.
80 bool Exec(Flavor flavor,
81 Mode mode,
82 const std::vector<std::string>& args,
83 Display display,
84 std::string* error_msg) {
85 std::string file_path = GetOatDumpFilePath(flavor);
86
87 EXPECT_TRUE(OS::FileExists(file_path.c_str())) << file_path << " should be a valid file path";
88
89 // ScratchFile scratch;
90 std::vector<std::string> exec_argv = { file_path };
91 std::vector<std::string> expected_prefixes;
92 if (mode == kModeSymbolize) {
93 exec_argv.push_back("--symbolize=" + core_oat_location_);
94 exec_argv.push_back("--output=" + core_oat_location_ + ".symbolize");
95 } else {
96 expected_prefixes.push_back("Dex file data for");
97 expected_prefixes.push_back("Num string ids:");
98 expected_prefixes.push_back("Num field ids:");
99 expected_prefixes.push_back("Num method ids:");
100 expected_prefixes.push_back("LOCATION:");
101 expected_prefixes.push_back("MAGIC:");
102 expected_prefixes.push_back("DEX FILE COUNT:");
103 if (display == kListAndCode) {
104 // Code and dex code do not show up if list only.
105 expected_prefixes.push_back("DEX CODE:");
106 expected_prefixes.push_back("CODE:");
107 expected_prefixes.push_back("CodeInfoEncoding");
108 expected_prefixes.push_back("CodeInfoInlineInfo");
109 }
110 if (mode == kModeArt) {
111 exec_argv.push_back("--image=" + core_art_location_);
112 exec_argv.push_back("--instruction-set=" + std::string(
113 GetInstructionSetString(kRuntimeISA)));
114 expected_prefixes.push_back("IMAGE LOCATION:");
115 expected_prefixes.push_back("IMAGE BEGIN:");
116 expected_prefixes.push_back("kDexCaches:");
117 } else {
118 CHECK_EQ(static_cast<size_t>(mode), static_cast<size_t>(kModeOat));
119 exec_argv.push_back("--oat-file=" + core_oat_location_);
120 }
121 }
122 exec_argv.insert(exec_argv.end(), args.begin(), args.end());
123
124 bool result = true;
125 // We must set --android-root.
126 int link[2];
127 if (pipe(link) == -1) {
128 *error_msg = strerror(errno);
129 return false;
130 }
131
132 const pid_t pid = fork();
133 if (pid == -1) {
134 *error_msg = strerror(errno);
135 return false;
136 }
137
138 if (pid == 0) {
139 dup2(link[1], STDOUT_FILENO);
140 close(link[0]);
141 close(link[1]);
142 // change process groups, so we don't get reaped by ProcessManager
143 setpgid(0, 0);
144 // Use execv here rather than art::Exec to avoid blocking on waitpid here.
145 std::vector<char*> argv;
146 for (size_t i = 0; i < exec_argv.size(); ++i) {
147 argv.push_back(const_cast<char*>(exec_argv[i].c_str()));
148 }
149 argv.push_back(nullptr);
150 UNUSED(execv(argv[0], &argv[0]));
151 const std::string command_line(android::base::Join(exec_argv, ' '));
152 PLOG(ERROR) << "Failed to execv(" << command_line << ")";
153 // _exit to avoid atexit handlers in child.
154 _exit(1);
155 } else {
156 close(link[1]);
157 static const size_t kLineMax = 256;
158 char line[kLineMax] = {};
159 size_t line_len = 0;
160 size_t total = 0;
161 std::vector<bool> found(expected_prefixes.size(), false);
162 while (true) {
163 while (true) {
164 size_t spaces = 0;
165 // Trim spaces at the start of the line.
166 for (; spaces < line_len && isspace(line[spaces]); ++spaces) {}
167 if (spaces > 0) {
168 line_len -= spaces;
169 memmove(&line[0], &line[spaces], line_len);
170 }
171 ssize_t bytes_read =
172 TEMP_FAILURE_RETRY(read(link[0], &line[line_len], kLineMax - line_len));
173 if (bytes_read <= 0) {
174 break;
175 }
176 line_len += bytes_read;
177 total += bytes_read;
178 }
179 if (line_len == 0) {
180 break;
181 }
182 // Check contents.
183 for (size_t i = 0; i < expected_prefixes.size(); ++i) {
184 const std::string& expected = expected_prefixes[i];
185 if (!found[i] &&
186 line_len >= expected.length() &&
187 memcmp(line, expected.c_str(), expected.length()) == 0) {
188 found[i] = true;
189 }
190 }
191 // Skip to next line.
192 size_t next_line = 0;
193 for (; next_line + 1 < line_len && line[next_line] != '\n'; ++next_line) {}
194 line_len -= next_line + 1;
195 memmove(&line[0], &line[next_line + 1], line_len);
196 }
197 if (mode == kModeSymbolize) {
198 EXPECT_EQ(total, 0u);
199 } else {
200 EXPECT_GT(total, 0u);
201 }
202 LOG(INFO) << "Processed bytes " << total;
203 close(link[0]);
204 int status = 0;
205 if (waitpid(pid, &status, 0) != -1) {
206 result = (status == 0);
207 }
208
209 for (size_t i = 0; i < expected_prefixes.size(); ++i) {
210 if (!found[i]) {
211 LOG(ERROR) << "Did not find prefix " << expected_prefixes[i];
212 result = false;
213 }
214 }
215 }
216
217 return result;
218 }
219
220 private:
221 std::string core_art_location_;
222 std::string core_oat_location_;
223};
224
225} // namespace art
226
227#endif // ART_OATDUMP_OATDUMP_TEST_H_