blob: 004defe5e05edb04ef83b0d94e5c7bc3c817160f [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 Chartier8e2c5622016-06-21 15:14:20 -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 Chartier8e2c5622016-06-21 15:14:20 -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 Chartier8e2c5622016-06-21 15:14:20 -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 Chartier8e2c5622016-06-21 15:14:20 -070070 // ScratchFile scratch;
Mathieu Chartier19510f02015-05-26 14:44:35 -070071 std::vector<std::string> exec_argv = { file_path };
Mathieu Chartier8e2c5622016-06-21 15:14:20 -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 Chartier8e2c5622016-06-21 15:14:20 -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 Chartier8e2c5622016-06-21 15:14:20 -0700102
103 bool result = true;
104 // We must set --android-root.
105 int link[2];
106 if (pipe(link) == -1) {
107 return false;
108 }
109
110 const pid_t pid = fork();
111 if (pid == -1) {
112 return false;
113 }
114
115 if (pid == 0) {
116 dup2(link[1], STDOUT_FILENO);
117 close(link[0]);
118 close(link[1]);
119 exit(::art::Exec(exec_argv, error_msg) ? 0 : 1);
120 } else {
121 close(link[1]);
122 static const size_t kLineMax = 256;
123 char line[kLineMax] = {};
124 size_t line_len = 0;
125 size_t total = 0;
126 std::vector<bool> found(expected_prefixes.size(), false);
127 while (true) {
128 while (true) {
129 size_t spaces = 0;
130 // Trim spaces at the start of the line.
131 for (; spaces < line_len && isspace(line[spaces]); ++spaces) {}
132 if (spaces > 0) {
133 line_len -= spaces;
134 memmove(&line[0], &line[spaces], line_len);
135 }
136 ssize_t bytes_read =
137 TEMP_FAILURE_RETRY(read(link[0], &line[line_len], kLineMax - line_len));
138 if (bytes_read <= 0) {
139 break;
140 }
141 line_len += bytes_read;
142 total += bytes_read;
143 }
144 if (line_len == 0) {
145 break;
146 }
147 // Check contents.
148 for (size_t i = 0; i < expected_prefixes.size(); ++i) {
149 const std::string& expected = expected_prefixes[i];
150 if (!found[i] &&
151 line_len >= expected.length() &&
152 memcmp(line, expected.c_str(), expected.length()) == 0) {
153 found[i] = true;
154 }
155 }
156 // Skip to next line.
157 size_t next_line = 0;
158 for (; next_line + 1 < line_len && line[next_line] != '\n'; ++next_line) {}
159 line_len -= next_line + 1;
160 memmove(&line[0], &line[next_line + 1], line_len);
161 }
162 if (mode == kModeSymbolize) {
163 EXPECT_EQ(total, 0u);
164 } else {
165 EXPECT_GT(total, 0u);
166 }
167 LOG(INFO) << "Processed bytes " << total;
168 close(link[0]);
169 int status = 0;
170 if (waitpid(pid, &status, 0) != -1) {
171 result = (status == 0);
172 }
173
174 for (size_t i = 0; i < expected_prefixes.size(); ++i) {
175 if (!found[i]) {
176 LOG(ERROR) << "Did not find prefix " << expected_prefixes[i];
177 result = false;
178 }
179 }
180 }
181
182 return result;
Mathieu Chartier19510f02015-05-26 14:44:35 -0700183 }
184
185 private:
186 std::string core_art_location_;
187 std::string core_oat_location_;
188};
189
Vladimir Marko7da31702016-03-29 18:42:21 +0100190// Disable tests on arm and mips as they are taking too long to run. b/27824283.
191#if !defined(__arm__) && !defined(__mips__)
Mathieu Chartier19510f02015-05-26 14:44:35 -0700192TEST_F(OatDumpTest, TestImage) {
193 std::string error_msg;
Mathieu Chartier8e2c5622016-06-21 15:14:20 -0700194 ASSERT_TRUE(Exec(kModeArt, {}, /*list_only*/ false, &error_msg)) << error_msg;
Mathieu Chartier19510f02015-05-26 14:44:35 -0700195}
196
197TEST_F(OatDumpTest, TestOatImage) {
198 std::string error_msg;
Mathieu Chartier8e2c5622016-06-21 15:14:20 -0700199 ASSERT_TRUE(Exec(kModeOat, {}, /*list_only*/ false, &error_msg)) << error_msg;
Mathieu Chartier19510f02015-05-26 14:44:35 -0700200}
201
Mathieu Chartier19510f02015-05-26 14:44:35 -0700202TEST_F(OatDumpTest, TestNoDumpVmap) {
203 std::string error_msg;
Mathieu Chartier8e2c5622016-06-21 15:14:20 -0700204 ASSERT_TRUE(Exec(kModeArt, {"--no-dump:vmap"}, /*list_only*/ false, &error_msg)) << error_msg;
Mathieu Chartier19510f02015-05-26 14:44:35 -0700205}
206
207TEST_F(OatDumpTest, TestNoDisassemble) {
208 std::string error_msg;
Mathieu Chartier8e2c5622016-06-21 15:14:20 -0700209 ASSERT_TRUE(Exec(kModeArt, {"--no-disassemble"}, /*list_only*/ false, &error_msg)) << error_msg;
Mathieu Chartier19510f02015-05-26 14:44:35 -0700210}
211
212TEST_F(OatDumpTest, TestListClasses) {
213 std::string error_msg;
Mathieu Chartier8e2c5622016-06-21 15:14:20 -0700214 ASSERT_TRUE(Exec(kModeArt, {"--list-classes"}, /*list_only*/ true, &error_msg)) << error_msg;
Mathieu Chartier19510f02015-05-26 14:44:35 -0700215}
216
217TEST_F(OatDumpTest, TestListMethods) {
218 std::string error_msg;
Mathieu Chartier8e2c5622016-06-21 15:14:20 -0700219 ASSERT_TRUE(Exec(kModeArt, {"--list-methods"}, /*list_only*/ true, &error_msg)) << error_msg;
Mathieu Chartier19510f02015-05-26 14:44:35 -0700220}
221
222TEST_F(OatDumpTest, TestSymbolize) {
223 std::string error_msg;
Mathieu Chartier8e2c5622016-06-21 15:14:20 -0700224 ASSERT_TRUE(Exec(kModeSymbolize, {}, /*list_only*/ true, &error_msg)) << error_msg;
Mathieu Chartier19510f02015-05-26 14:44:35 -0700225}
Nicolas Geoffray973ce7c2016-03-24 09:23:04 +0000226#endif
Mathieu Chartier19510f02015-05-26 14:44:35 -0700227} // namespace art