blob: 5a97c3b07e1e42e078baa3b2c88bd658dd5481c1 [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
David Srbecky15c19752015-03-31 14:53:55 +000058 // Pretty-print the generated DWARF data using objdump.
59 template<typename Elf_Word, typename Elf_Sword, typename Elf_Addr, typename Elf_Dyn,
60 typename Elf_Sym, typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr>
61 std::vector<std::string> Objdump(bool is64bit, const char* args) {
62 // Write simple elf file with just the DWARF sections.
63 class NoCode : public CodeOutput {
64 virtual void SetCodeOffset(size_t) { }
65 virtual bool Write(OutputStream*) { return true; }
66 } code;
67 ScratchFile file;
68 InstructionSet isa = is64bit ? kX86_64 : kX86;
69 ElfBuilder<Elf_Word, Elf_Sword, Elf_Addr, Elf_Dyn,
70 Elf_Sym, Elf_Ehdr, Elf_Phdr, Elf_Shdr> builder(
71 &code, file.GetFile(), isa, 0, 0, 0, 0, 0, 0, false, false);
72 typedef ElfRawSectionBuilder<Elf_Word, Elf_Sword, Elf_Shdr> Section;
73 if (!debug_info_data_.empty()) {
74 Section debug_info(".debug_info", SHT_PROGBITS, 0, nullptr, 0, 1, 0);
75 debug_info.SetBuffer(debug_info_data_);
76 builder.RegisterRawSection(debug_info);
77 }
78 if (!debug_abbrev_data_.empty()) {
79 Section debug_abbrev(".debug_abbrev", SHT_PROGBITS, 0, nullptr, 0, 1, 0);
80 debug_abbrev.SetBuffer(debug_abbrev_data_);
81 builder.RegisterRawSection(debug_abbrev);
82 }
83 if (!debug_str_data_.empty()) {
84 Section debug_str(".debug_str", SHT_PROGBITS, 0, nullptr, 0, 1, 0);
85 debug_str.SetBuffer(debug_str_data_);
86 builder.RegisterRawSection(debug_str);
87 }
88 if (!debug_line_data_.empty()) {
89 Section debug_line(".debug_line", SHT_PROGBITS, 0, nullptr, 0, 1, 0);
90 debug_line.SetBuffer(debug_line_data_);
91 builder.RegisterRawSection(debug_line);
92 }
93 if (!eh_frame_data_.empty()) {
94 Section eh_frame(".eh_frame", SHT_PROGBITS, SHF_ALLOC, nullptr, 0, 4, 0);
95 eh_frame.SetBuffer(eh_frame_data_);
96 builder.RegisterRawSection(eh_frame);
97 }
98 builder.Init();
99 builder.Write();
100
101 // Read the elf file back using objdump.
102 std::vector<std::string> lines;
David Srbecky3e52aa42015-04-12 07:45:18 +0100103 std::string cmd = GetAndroidHostToolsDir();
104 cmd = cmd + "objdump " + args + " " + file.GetFilename() + " 2>&1";
David Srbecky15c19752015-03-31 14:53:55 +0000105 FILE* output = popen(cmd.data(), "r");
106 char buffer[1024];
107 const char* line;
108 while ((line = fgets(buffer, sizeof(buffer), output)) != nullptr) {
109 if (kPrintObjdumpOutput) {
110 printf("%s", line);
111 }
112 if (line[0] != '\0' && line[0] != '\n') {
113 EXPECT_TRUE(strstr(line, "objdump: Error:") == nullptr) << line;
114 EXPECT_TRUE(strstr(line, "objdump: Warning:") == nullptr) << line;
115 std::string str(line);
116 if (str.back() == '\n') {
117 str.pop_back();
118 }
119 lines.push_back(str);
120 }
121 }
122 pclose(output);
123 return lines;
124 }
125
126 std::vector<std::string> Objdump(bool is64bit, const char* args) {
127 if (is64bit) {
128 return Objdump<Elf64_Word, Elf64_Sword, Elf64_Addr, Elf64_Dyn,
129 Elf64_Sym, Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr>(is64bit, args);
130 } else {
131 return Objdump<Elf32_Word, Elf32_Sword, Elf32_Addr, Elf32_Dyn,
132 Elf32_Sym, Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr>(is64bit, args);
133 }
134 }
135
136 // Compare objdump output to the recorded checks.
137 void CheckObjdumpOutput(bool is64bit, const char* args) {
138 std::vector<std::string> actual_lines = Objdump(is64bit, args);
139 auto actual_line = actual_lines.begin();
140 for (const ExpectedLine& expected_line : expected_lines_) {
141 const std::string& substring = expected_line.substring;
142 if (actual_line == actual_lines.end()) {
143 ADD_FAILURE_AT(expected_line.at_file, expected_line.at_line) <<
144 "Expected '" << substring << "'.\n" <<
145 "Seen end of output.";
146 } else if (expected_line.next) {
147 if (actual_line->find(substring) == std::string::npos) {
148 ADD_FAILURE_AT(expected_line.at_file, expected_line.at_line) <<
149 "Expected '" << substring << "'.\n" <<
150 "Seen '" << actual_line->data() << "'.";
151 } else {
152 // printf("Found '%s' in '%s'.\n", substring.data(), actual_line->data());
153 }
154 actual_line++;
155 } else {
156 bool found = false;
157 for (auto it = actual_line; it < actual_lines.end(); it++) {
158 if (it->find(substring) != std::string::npos) {
159 actual_line = it;
160 found = true;
161 break;
162 }
163 }
164 if (!found) {
165 ADD_FAILURE_AT(expected_line.at_file, expected_line.at_line) <<
166 "Expected '" << substring << "'.\n" <<
167 "Not found anywhere in the rest of the output.";
168 } else {
169 // printf("Found '%s' in '%s'.\n", substring.data(), actual_line->data());
170 actual_line++;
171 }
172 }
173 }
174 }
175
176 // Buffers which are going to assembled into ELF file and passed to objdump.
177 std::vector<uint8_t> eh_frame_data_;
178 std::vector<uint8_t> debug_info_data_;
179 std::vector<uint8_t> debug_abbrev_data_;
180 std::vector<uint8_t> debug_str_data_;
181 std::vector<uint8_t> debug_line_data_;
182
183 // The expected output of objdump.
184 std::vector<ExpectedLine> expected_lines_;
185};
186
187} // namespace dwarf
188} // namespace art
189
190#endif // ART_COMPILER_DWARF_DWARF_TEST_H_