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