blob: db970556e6adddeaa28585f0c3b16ed0faf1ecab [file] [log] [blame]
Mathieu Chartier19510f02015-05-26 14:44:35 -07001/*
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
Mathieu Chartierdc00f182016-07-14 10:10:44 -070017#include <sstream>
Mathieu Chartier19510f02015-05-26 14:44:35 -070018#include <string>
19#include <vector>
Mathieu Chartier19510f02015-05-26 14:44:35 -070020
21#include "common_runtime_test.h"
22
23#include "base/stringprintf.h"
Mathieu Chartierdc00f182016-07-14 10:10:44 -070024#include "base/unix_file/fd_file.h"
Mathieu Chartier19510f02015-05-26 14:44:35 -070025#include "runtime/arch/instruction_set.h"
26#include "runtime/gc/heap.h"
27#include "runtime/gc/space/image_space.h"
28#include "runtime/os.h"
29#include "runtime/utils.h"
30#include "utils.h"
31
32#include <sys/types.h>
33#include <unistd.h>
34
35namespace art {
36
37class OatDumpTest : public CommonRuntimeTest {
38 protected:
39 virtual void SetUp() {
40 CommonRuntimeTest::SetUp();
41 core_art_location_ = GetCoreArtLocation();
42 core_oat_location_ = GetSystemImageFilename(GetCoreOatLocation().c_str(), kRuntimeISA);
43 }
44
45 // Returns path to the oatdump binary.
46 std::string GetOatDumpFilePath() {
47 std::string root = GetTestAndroidRoot();
48 root += "/bin/oatdump";
49 if (kIsDebugBuild) {
50 root += "d";
51 }
52 return root;
53 }
54
55 enum Mode {
56 kModeOat,
57 kModeArt,
58 kModeSymbolize,
59 };
60
61 // Run the test with custom arguments.
Mathieu Chartierdc00f182016-07-14 10:10:44 -070062 bool Exec(Mode mode,
63 const std::vector<std::string>& args,
64 bool list_only,
65 std::string* error_msg) {
Mathieu Chartier19510f02015-05-26 14:44:35 -070066 std::string file_path = GetOatDumpFilePath();
67
68 EXPECT_TRUE(OS::FileExists(file_path.c_str())) << file_path << " should be a valid file path";
69
Mathieu Chartierdc00f182016-07-14 10:10:44 -070070 // ScratchFile scratch;
Mathieu Chartier19510f02015-05-26 14:44:35 -070071 std::vector<std::string> exec_argv = { file_path };
Mathieu Chartierdc00f182016-07-14 10:10:44 -070072 std::vector<std::string> expected_prefixes;
Mathieu Chartier19510f02015-05-26 14:44:35 -070073 if (mode == kModeSymbolize) {
74 exec_argv.push_back("--symbolize=" + core_oat_location_);
75 exec_argv.push_back("--output=" + core_oat_location_ + ".symbolize");
Mathieu Chartier19510f02015-05-26 14:44:35 -070076 } else {
Mathieu Chartierdc00f182016-07-14 10:10:44 -070077 expected_prefixes.push_back("Dex file data for");
78 expected_prefixes.push_back("Num string ids:");
79 expected_prefixes.push_back("Num field ids:");
80 expected_prefixes.push_back("Num method ids:");
81 expected_prefixes.push_back("LOCATION:");
82 expected_prefixes.push_back("MAGIC:");
83 expected_prefixes.push_back("DEX FILE COUNT:");
84 if (!list_only) {
85 // Code and dex code do not show up if list only.
86 expected_prefixes.push_back("DEX CODE:");
87 expected_prefixes.push_back("CODE:");
88 }
89 if (mode == kModeArt) {
90 exec_argv.push_back("--image=" + core_art_location_);
91 exec_argv.push_back("--instruction-set=" + std::string(
92 GetInstructionSetString(kRuntimeISA)));
93 expected_prefixes.push_back("IMAGE LOCATION:");
94 expected_prefixes.push_back("IMAGE BEGIN:");
95 expected_prefixes.push_back("kDexCaches:");
96 } else {
97 CHECK_EQ(static_cast<size_t>(mode), static_cast<size_t>(kModeOat));
98 exec_argv.push_back("--oat-file=" + core_oat_location_);
99 }
Mathieu Chartier19510f02015-05-26 14:44:35 -0700100 }
101 exec_argv.insert(exec_argv.end(), args.begin(), args.end());
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700102
103 bool result = true;
104 // We must set --android-root.
105 int link[2];
106 if (pipe(link) == -1) {
David Sehrda820e92016-08-04 09:41:55 -0700107 *error_msg = strerror(errno);
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700108 return false;
109 }
110
111 const pid_t pid = fork();
112 if (pid == -1) {
David Sehrda820e92016-08-04 09:41:55 -0700113 *error_msg = strerror(errno);
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700114 return false;
115 }
116
117 if (pid == 0) {
118 dup2(link[1], STDOUT_FILENO);
119 close(link[0]);
120 close(link[1]);
David Sehrda820e92016-08-04 09:41:55 -0700121 // change process groups, so we don't get reaped by ProcessManager
122 setpgid(0, 0);
123 // Use execv here rather than art::Exec to avoid blocking on waitpid here.
124 std::vector<char*> argv;
125 for (size_t i = 0; i < exec_argv.size(); ++i) {
126 argv.push_back(const_cast<char*>(exec_argv[i].c_str()));
127 }
128 argv.push_back(nullptr);
129 UNUSED(execv(argv[0], &argv[0]));
130 const std::string command_line(Join(exec_argv, ' '));
131 PLOG(ERROR) << "Failed to execv(" << command_line << ")";
132 // _exit to avoid atexit handlers in child.
133 _exit(1);
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700134 } else {
135 close(link[1]);
136 static const size_t kLineMax = 256;
137 char line[kLineMax] = {};
138 size_t line_len = 0;
139 size_t total = 0;
140 std::vector<bool> found(expected_prefixes.size(), false);
141 while (true) {
142 while (true) {
143 size_t spaces = 0;
144 // Trim spaces at the start of the line.
145 for (; spaces < line_len && isspace(line[spaces]); ++spaces) {}
146 if (spaces > 0) {
147 line_len -= spaces;
148 memmove(&line[0], &line[spaces], line_len);
149 }
150 ssize_t bytes_read =
151 TEMP_FAILURE_RETRY(read(link[0], &line[line_len], kLineMax - line_len));
152 if (bytes_read <= 0) {
153 break;
154 }
155 line_len += bytes_read;
156 total += bytes_read;
157 }
158 if (line_len == 0) {
159 break;
160 }
161 // Check contents.
162 for (size_t i = 0; i < expected_prefixes.size(); ++i) {
163 const std::string& expected = expected_prefixes[i];
164 if (!found[i] &&
165 line_len >= expected.length() &&
166 memcmp(line, expected.c_str(), expected.length()) == 0) {
167 found[i] = true;
168 }
169 }
170 // Skip to next line.
171 size_t next_line = 0;
172 for (; next_line + 1 < line_len && line[next_line] != '\n'; ++next_line) {}
173 line_len -= next_line + 1;
174 memmove(&line[0], &line[next_line + 1], line_len);
175 }
176 if (mode == kModeSymbolize) {
177 EXPECT_EQ(total, 0u);
178 } else {
179 EXPECT_GT(total, 0u);
180 }
181 LOG(INFO) << "Processed bytes " << total;
182 close(link[0]);
183 int status = 0;
184 if (waitpid(pid, &status, 0) != -1) {
185 result = (status == 0);
186 }
187
188 for (size_t i = 0; i < expected_prefixes.size(); ++i) {
189 if (!found[i]) {
190 LOG(ERROR) << "Did not find prefix " << expected_prefixes[i];
191 result = false;
192 }
193 }
194 }
195
196 return result;
Mathieu Chartier19510f02015-05-26 14:44:35 -0700197 }
198
199 private:
200 std::string core_art_location_;
201 std::string core_oat_location_;
202};
203
Vladimir Marko7da31702016-03-29 18:42:21 +0100204// Disable tests on arm and mips as they are taking too long to run. b/27824283.
205#if !defined(__arm__) && !defined(__mips__)
Mathieu Chartier19510f02015-05-26 14:44:35 -0700206TEST_F(OatDumpTest, TestImage) {
207 std::string error_msg;
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700208 ASSERT_TRUE(Exec(kModeArt, {}, /*list_only*/ false, &error_msg)) << error_msg;
Mathieu Chartier19510f02015-05-26 14:44:35 -0700209}
210
211TEST_F(OatDumpTest, TestOatImage) {
212 std::string error_msg;
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700213 ASSERT_TRUE(Exec(kModeOat, {}, /*list_only*/ false, &error_msg)) << error_msg;
Mathieu Chartier19510f02015-05-26 14:44:35 -0700214}
215
Mathieu Chartier19510f02015-05-26 14:44:35 -0700216TEST_F(OatDumpTest, TestNoDumpVmap) {
217 std::string error_msg;
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700218 ASSERT_TRUE(Exec(kModeArt, {"--no-dump:vmap"}, /*list_only*/ false, &error_msg)) << error_msg;
Mathieu Chartier19510f02015-05-26 14:44:35 -0700219}
220
221TEST_F(OatDumpTest, TestNoDisassemble) {
222 std::string error_msg;
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700223 ASSERT_TRUE(Exec(kModeArt, {"--no-disassemble"}, /*list_only*/ false, &error_msg)) << error_msg;
Mathieu Chartier19510f02015-05-26 14:44:35 -0700224}
225
226TEST_F(OatDumpTest, TestListClasses) {
227 std::string error_msg;
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700228 ASSERT_TRUE(Exec(kModeArt, {"--list-classes"}, /*list_only*/ true, &error_msg)) << error_msg;
Mathieu Chartier19510f02015-05-26 14:44:35 -0700229}
230
231TEST_F(OatDumpTest, TestListMethods) {
232 std::string error_msg;
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700233 ASSERT_TRUE(Exec(kModeArt, {"--list-methods"}, /*list_only*/ true, &error_msg)) << error_msg;
Mathieu Chartier19510f02015-05-26 14:44:35 -0700234}
235
236TEST_F(OatDumpTest, TestSymbolize) {
237 std::string error_msg;
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700238 ASSERT_TRUE(Exec(kModeSymbolize, {}, /*list_only*/ true, &error_msg)) << error_msg;
Mathieu Chartier19510f02015-05-26 14:44:35 -0700239}
Nicolas Geoffray973ce7c2016-03-24 09:23:04 +0000240#endif
Mathieu Chartier19510f02015-05-26 14:44:35 -0700241} // namespace art