blob: 022fac27e17caf928e0bb6217092631f768d6630 [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.
David Srbecky533c2072015-04-22 12:20:22 +010059 template<typename ElfTypes>
David Srbecky15c19752015-03-31 14:53:55 +000060 std::vector<std::string> Objdump(bool is64bit, const char* args) {
61 // Write simple elf file with just the DWARF sections.
62 class NoCode : public CodeOutput {
63 virtual void SetCodeOffset(size_t) { }
64 virtual bool Write(OutputStream*) { return true; }
65 } code;
66 ScratchFile file;
67 InstructionSet isa = is64bit ? kX86_64 : kX86;
David Srbecky533c2072015-04-22 12:20:22 +010068 ElfBuilder<ElfTypes> builder(
David Srbecky15c19752015-03-31 14:53:55 +000069 &code, file.GetFile(), isa, 0, 0, 0, 0, 0, 0, false, false);
David Srbecky0c5bbc12015-04-28 17:54:52 +010070 typedef typename ElfBuilder<ElfTypes>::ElfRawSectionBuilder Section;
David Srbecky527c9c72015-04-17 21:14:10 +010071 Section debug_info(".debug_info", SHT_PROGBITS, 0, nullptr, 0, 1, 0);
72 Section debug_abbrev(".debug_abbrev", SHT_PROGBITS, 0, nullptr, 0, 1, 0);
73 Section debug_str(".debug_str", SHT_PROGBITS, 0, nullptr, 0, 1, 0);
74 Section debug_line(".debug_line", SHT_PROGBITS, 0, nullptr, 0, 1, 0);
75 Section eh_frame(".eh_frame", SHT_PROGBITS, SHF_ALLOC, nullptr, 0, 4, 0);
David Srbecky15c19752015-03-31 14:53:55 +000076 if (!debug_info_data_.empty()) {
David Srbecky15c19752015-03-31 14:53:55 +000077 debug_info.SetBuffer(debug_info_data_);
David Srbecky527c9c72015-04-17 21:14:10 +010078 builder.RegisterRawSection(&debug_info);
David Srbecky15c19752015-03-31 14:53:55 +000079 }
80 if (!debug_abbrev_data_.empty()) {
David Srbecky15c19752015-03-31 14:53:55 +000081 debug_abbrev.SetBuffer(debug_abbrev_data_);
David Srbecky527c9c72015-04-17 21:14:10 +010082 builder.RegisterRawSection(&debug_abbrev);
David Srbecky15c19752015-03-31 14:53:55 +000083 }
84 if (!debug_str_data_.empty()) {
David Srbecky15c19752015-03-31 14:53:55 +000085 debug_str.SetBuffer(debug_str_data_);
David Srbecky527c9c72015-04-17 21:14:10 +010086 builder.RegisterRawSection(&debug_str);
David Srbecky15c19752015-03-31 14:53:55 +000087 }
88 if (!debug_line_data_.empty()) {
David Srbecky15c19752015-03-31 14:53:55 +000089 debug_line.SetBuffer(debug_line_data_);
David Srbecky527c9c72015-04-17 21:14:10 +010090 builder.RegisterRawSection(&debug_line);
David Srbecky15c19752015-03-31 14:53:55 +000091 }
92 if (!eh_frame_data_.empty()) {
David Srbecky15c19752015-03-31 14:53:55 +000093 eh_frame.SetBuffer(eh_frame_data_);
David Srbecky527c9c72015-04-17 21:14:10 +010094 builder.RegisterRawSection(&eh_frame);
David Srbecky15c19752015-03-31 14:53:55 +000095 }
96 builder.Init();
97 builder.Write();
98
99 // Read the elf file back using objdump.
100 std::vector<std::string> lines;
David Srbecky3e52aa42015-04-12 07:45:18 +0100101 std::string cmd = GetAndroidHostToolsDir();
102 cmd = cmd + "objdump " + args + " " + file.GetFilename() + " 2>&1";
David Srbecky15c19752015-03-31 14:53:55 +0000103 FILE* output = popen(cmd.data(), "r");
104 char buffer[1024];
105 const char* line;
106 while ((line = fgets(buffer, sizeof(buffer), output)) != nullptr) {
107 if (kPrintObjdumpOutput) {
108 printf("%s", line);
109 }
110 if (line[0] != '\0' && line[0] != '\n') {
111 EXPECT_TRUE(strstr(line, "objdump: Error:") == nullptr) << line;
112 EXPECT_TRUE(strstr(line, "objdump: Warning:") == nullptr) << line;
113 std::string str(line);
114 if (str.back() == '\n') {
115 str.pop_back();
116 }
117 lines.push_back(str);
118 }
119 }
120 pclose(output);
121 return lines;
122 }
123
124 std::vector<std::string> Objdump(bool is64bit, const char* args) {
125 if (is64bit) {
David Srbecky533c2072015-04-22 12:20:22 +0100126 return Objdump<ElfTypes64>(is64bit, args);
David Srbecky15c19752015-03-31 14:53:55 +0000127 } else {
David Srbecky533c2072015-04-22 12:20:22 +0100128 return Objdump<ElfTypes32>(is64bit, args);
David Srbecky15c19752015-03-31 14:53:55 +0000129 }
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_