blob: dd5e0c286ef7d5fbe01e8bea70a16485a949ace5 [file] [log] [blame]
David Srbecky15c19752015-03-31 14:53:55 +00001/*
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_COMPILER_DWARF_DWARF_TEST_H_
18#define ART_COMPILER_DWARF_DWARF_TEST_H_
19
20#include <cstring>
21#include <dirent.h>
22#include <memory>
23#include <set>
24#include <stdio.h>
25#include <string>
26#include <sys/types.h>
27
28#include "utils.h"
29#include "base/unix_file/fd_file.h"
30#include "common_runtime_test.h"
31#include "elf_builder.h"
32#include "gtest/gtest.h"
33#include "os.h"
34
35namespace art {
36namespace dwarf {
37
38#define DW_CHECK(substring) Check(substring, false, __FILE__, __LINE__)
39#define DW_CHECK_NEXT(substring) Check(substring, true, __FILE__, __LINE__)
40
41class DwarfTest : public CommonRuntimeTest {
42 public:
43 static constexpr bool kPrintObjdumpOutput = false; // debugging.
44
45 struct ExpectedLine {
46 std::string substring;
47 bool next;
48 const char* at_file;
49 int at_line;
50 };
51
52 // Check that the objdump output contains given output.
53 // If next is true, it must be the next line. Otherwise lines are skipped.
54 void Check(const char* substr, bool next, const char* at_file, int at_line) {
55 expected_lines_.push_back(ExpectedLine {substr, next, at_file, at_line});
56 }
57
58 static std::string GetObjdumpPath() {
59 const char* android_build_top = getenv("ANDROID_BUILD_TOP");
60 if (android_build_top != nullptr) {
61 std::string host_prebuilts = std::string(android_build_top) +
62 "/prebuilts/gcc/linux-x86/host/";
63 // Read the content of the directory.
64 std::set<std::string> entries;
65 DIR* dir = opendir(host_prebuilts.c_str());
66 if (dir != nullptr) {
67 struct dirent* entry;
68 while ((entry = readdir(dir)) != nullptr) {
69 if (strstr(entry->d_name, "linux-glibc")) {
70 entries.insert(host_prebuilts + entry->d_name);
71 }
72 }
73 closedir(dir);
74 }
75 // Strings are sorted so the last one should be the most recent version.
76 if (!entries.empty()) {
77 std::string path = *entries.rbegin() + "/x86_64-linux/bin/objdump";
78 struct stat st;
79 if (stat(path.c_str(), &st) == 0) {
80 return path; // File exists.
81 }
82 }
83 }
84 ADD_FAILURE() << "Can not find prebuild objdump.";
85 return "objdump"; // Use the system objdump as fallback.
86 }
87
88 // Pretty-print the generated DWARF data using objdump.
89 template<typename Elf_Word, typename Elf_Sword, typename Elf_Addr, typename Elf_Dyn,
90 typename Elf_Sym, typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr>
91 std::vector<std::string> Objdump(bool is64bit, const char* args) {
92 // Write simple elf file with just the DWARF sections.
93 class NoCode : public CodeOutput {
94 virtual void SetCodeOffset(size_t) { }
95 virtual bool Write(OutputStream*) { return true; }
96 } code;
97 ScratchFile file;
98 InstructionSet isa = is64bit ? kX86_64 : kX86;
99 ElfBuilder<Elf_Word, Elf_Sword, Elf_Addr, Elf_Dyn,
100 Elf_Sym, Elf_Ehdr, Elf_Phdr, Elf_Shdr> builder(
101 &code, file.GetFile(), isa, 0, 0, 0, 0, 0, 0, false, false);
102 typedef ElfRawSectionBuilder<Elf_Word, Elf_Sword, Elf_Shdr> Section;
103 if (!debug_info_data_.empty()) {
104 Section debug_info(".debug_info", SHT_PROGBITS, 0, nullptr, 0, 1, 0);
105 debug_info.SetBuffer(debug_info_data_);
106 builder.RegisterRawSection(debug_info);
107 }
108 if (!debug_abbrev_data_.empty()) {
109 Section debug_abbrev(".debug_abbrev", SHT_PROGBITS, 0, nullptr, 0, 1, 0);
110 debug_abbrev.SetBuffer(debug_abbrev_data_);
111 builder.RegisterRawSection(debug_abbrev);
112 }
113 if (!debug_str_data_.empty()) {
114 Section debug_str(".debug_str", SHT_PROGBITS, 0, nullptr, 0, 1, 0);
115 debug_str.SetBuffer(debug_str_data_);
116 builder.RegisterRawSection(debug_str);
117 }
118 if (!debug_line_data_.empty()) {
119 Section debug_line(".debug_line", SHT_PROGBITS, 0, nullptr, 0, 1, 0);
120 debug_line.SetBuffer(debug_line_data_);
121 builder.RegisterRawSection(debug_line);
122 }
123 if (!eh_frame_data_.empty()) {
124 Section eh_frame(".eh_frame", SHT_PROGBITS, SHF_ALLOC, nullptr, 0, 4, 0);
125 eh_frame.SetBuffer(eh_frame_data_);
126 builder.RegisterRawSection(eh_frame);
127 }
128 builder.Init();
129 builder.Write();
130
131 // Read the elf file back using objdump.
132 std::vector<std::string> lines;
133 std::string cmd = GetObjdumpPath();
134 cmd = cmd + " " + args + " " + file.GetFilename() + " 2>&1";
135 FILE* output = popen(cmd.data(), "r");
136 char buffer[1024];
137 const char* line;
138 while ((line = fgets(buffer, sizeof(buffer), output)) != nullptr) {
139 if (kPrintObjdumpOutput) {
140 printf("%s", line);
141 }
142 if (line[0] != '\0' && line[0] != '\n') {
143 EXPECT_TRUE(strstr(line, "objdump: Error:") == nullptr) << line;
144 EXPECT_TRUE(strstr(line, "objdump: Warning:") == nullptr) << line;
145 std::string str(line);
146 if (str.back() == '\n') {
147 str.pop_back();
148 }
149 lines.push_back(str);
150 }
151 }
152 pclose(output);
153 return lines;
154 }
155
156 std::vector<std::string> Objdump(bool is64bit, const char* args) {
157 if (is64bit) {
158 return Objdump<Elf64_Word, Elf64_Sword, Elf64_Addr, Elf64_Dyn,
159 Elf64_Sym, Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr>(is64bit, args);
160 } else {
161 return Objdump<Elf32_Word, Elf32_Sword, Elf32_Addr, Elf32_Dyn,
162 Elf32_Sym, Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr>(is64bit, args);
163 }
164 }
165
166 // Compare objdump output to the recorded checks.
167 void CheckObjdumpOutput(bool is64bit, const char* args) {
168 std::vector<std::string> actual_lines = Objdump(is64bit, args);
169 auto actual_line = actual_lines.begin();
170 for (const ExpectedLine& expected_line : expected_lines_) {
171 const std::string& substring = expected_line.substring;
172 if (actual_line == actual_lines.end()) {
173 ADD_FAILURE_AT(expected_line.at_file, expected_line.at_line) <<
174 "Expected '" << substring << "'.\n" <<
175 "Seen end of output.";
176 } else if (expected_line.next) {
177 if (actual_line->find(substring) == std::string::npos) {
178 ADD_FAILURE_AT(expected_line.at_file, expected_line.at_line) <<
179 "Expected '" << substring << "'.\n" <<
180 "Seen '" << actual_line->data() << "'.";
181 } else {
182 // printf("Found '%s' in '%s'.\n", substring.data(), actual_line->data());
183 }
184 actual_line++;
185 } else {
186 bool found = false;
187 for (auto it = actual_line; it < actual_lines.end(); it++) {
188 if (it->find(substring) != std::string::npos) {
189 actual_line = it;
190 found = true;
191 break;
192 }
193 }
194 if (!found) {
195 ADD_FAILURE_AT(expected_line.at_file, expected_line.at_line) <<
196 "Expected '" << substring << "'.\n" <<
197 "Not found anywhere in the rest of the output.";
198 } else {
199 // printf("Found '%s' in '%s'.\n", substring.data(), actual_line->data());
200 actual_line++;
201 }
202 }
203 }
204 }
205
206 // Buffers which are going to assembled into ELF file and passed to objdump.
207 std::vector<uint8_t> eh_frame_data_;
208 std::vector<uint8_t> debug_info_data_;
209 std::vector<uint8_t> debug_abbrev_data_;
210 std::vector<uint8_t> debug_str_data_;
211 std::vector<uint8_t> debug_line_data_;
212
213 // The expected output of objdump.
214 std::vector<ExpectedLine> expected_lines_;
215};
216
217} // namespace dwarf
218} // namespace art
219
220#endif // ART_COMPILER_DWARF_DWARF_TEST_H_