blob: f819c49cee1d2a0fa484668acc9cb2842cc723e4 [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
David Srbecky15c19752015-03-31 14:53:55 +000028#include "base/unix_file/fd_file.h"
29#include "common_runtime_test.h"
30#include "elf_builder.h"
31#include "gtest/gtest.h"
32#include "os.h"
33
34namespace art {
35namespace dwarf {
36
37#define DW_CHECK(substring) Check(substring, false, __FILE__, __LINE__)
38#define DW_CHECK_NEXT(substring) Check(substring, true, __FILE__, __LINE__)
39
40class DwarfTest : public CommonRuntimeTest {
41 public:
42 static constexpr bool kPrintObjdumpOutput = false; // debugging.
43
44 struct ExpectedLine {
45 std::string substring;
46 bool next;
47 const char* at_file;
48 int at_line;
49 };
50
51 // Check that the objdump output contains given output.
52 // If next is true, it must be the next line. Otherwise lines are skipped.
53 void Check(const char* substr, bool next, const char* at_file, int at_line) {
54 expected_lines_.push_back(ExpectedLine {substr, next, at_file, at_line});
55 }
56
David Srbecky15c19752015-03-31 14:53:55 +000057 // Pretty-print the generated DWARF data using objdump.
David Srbecky533c2072015-04-22 12:20:22 +010058 template<typename ElfTypes>
David Srbeckybc90fd02015-04-22 19:40:27 +010059 std::vector<std::string> Objdump(const char* args) {
David Srbecky15c19752015-03-31 14:53:55 +000060 // Write simple elf file with just the DWARF sections.
David Srbeckybc90fd02015-04-22 19:40:27 +010061 InstructionSet isa = (sizeof(typename ElfTypes::Addr) == 8) ? kX86_64 : kX86;
David Srbecky15c19752015-03-31 14:53:55 +000062 class NoCode : public CodeOutput {
David Srbeckybc90fd02015-04-22 19:40:27 +010063 bool Write(OutputStream*) OVERRIDE { return true; } // NOLINT
64 } no_code;
65 ElfBuilder<ElfTypes> builder(isa, 0, &no_code, 0, &no_code, 0);
66 typedef typename ElfBuilder<ElfTypes>::RawSection RawSection;
67 RawSection debug_info(".debug_info", SHT_PROGBITS, 0, nullptr, 0, 1, 0);
68 RawSection debug_abbrev(".debug_abbrev", SHT_PROGBITS, 0, nullptr, 0, 1, 0);
69 RawSection debug_str(".debug_str", SHT_PROGBITS, 0, nullptr, 0, 1, 0);
70 RawSection debug_line(".debug_line", SHT_PROGBITS, 0, nullptr, 0, 1, 0);
David Srbeckyad5fa8c2015-05-06 18:27:35 +010071 RawSection debug_frame(".debug_frame", SHT_PROGBITS, 0, nullptr, 0, 8, 0);
David Srbecky15c19752015-03-31 14:53:55 +000072 if (!debug_info_data_.empty()) {
David Srbecky15c19752015-03-31 14:53:55 +000073 debug_info.SetBuffer(debug_info_data_);
David Srbeckybc90fd02015-04-22 19:40:27 +010074 builder.RegisterSection(&debug_info);
David Srbecky15c19752015-03-31 14:53:55 +000075 }
76 if (!debug_abbrev_data_.empty()) {
David Srbecky15c19752015-03-31 14:53:55 +000077 debug_abbrev.SetBuffer(debug_abbrev_data_);
David Srbeckybc90fd02015-04-22 19:40:27 +010078 builder.RegisterSection(&debug_abbrev);
David Srbecky15c19752015-03-31 14:53:55 +000079 }
80 if (!debug_str_data_.empty()) {
David Srbecky15c19752015-03-31 14:53:55 +000081 debug_str.SetBuffer(debug_str_data_);
David Srbeckybc90fd02015-04-22 19:40:27 +010082 builder.RegisterSection(&debug_str);
David Srbecky15c19752015-03-31 14:53:55 +000083 }
84 if (!debug_line_data_.empty()) {
David Srbecky15c19752015-03-31 14:53:55 +000085 debug_line.SetBuffer(debug_line_data_);
David Srbeckybc90fd02015-04-22 19:40:27 +010086 builder.RegisterSection(&debug_line);
David Srbecky15c19752015-03-31 14:53:55 +000087 }
David Srbeckyad5fa8c2015-05-06 18:27:35 +010088 if (!debug_frame_data_.empty()) {
89 debug_frame.SetBuffer(debug_frame_data_);
90 builder.RegisterSection(&debug_frame);
David Srbecky15c19752015-03-31 14:53:55 +000091 }
David Srbeckybc90fd02015-04-22 19:40:27 +010092 ScratchFile file;
93 builder.Write(file.GetFile());
David Srbecky15c19752015-03-31 14:53:55 +000094
95 // Read the elf file back using objdump.
96 std::vector<std::string> lines;
David Srbecky3e52aa42015-04-12 07:45:18 +010097 std::string cmd = GetAndroidHostToolsDir();
98 cmd = cmd + "objdump " + args + " " + file.GetFilename() + " 2>&1";
David Srbecky15c19752015-03-31 14:53:55 +000099 FILE* output = popen(cmd.data(), "r");
100 char buffer[1024];
101 const char* line;
102 while ((line = fgets(buffer, sizeof(buffer), output)) != nullptr) {
103 if (kPrintObjdumpOutput) {
104 printf("%s", line);
105 }
106 if (line[0] != '\0' && line[0] != '\n') {
107 EXPECT_TRUE(strstr(line, "objdump: Error:") == nullptr) << line;
108 EXPECT_TRUE(strstr(line, "objdump: Warning:") == nullptr) << line;
109 std::string str(line);
110 if (str.back() == '\n') {
111 str.pop_back();
112 }
113 lines.push_back(str);
114 }
115 }
116 pclose(output);
117 return lines;
118 }
119
120 std::vector<std::string> Objdump(bool is64bit, const char* args) {
121 if (is64bit) {
David Srbeckybc90fd02015-04-22 19:40:27 +0100122 return Objdump<ElfTypes64>(args);
David Srbecky15c19752015-03-31 14:53:55 +0000123 } else {
David Srbeckybc90fd02015-04-22 19:40:27 +0100124 return Objdump<ElfTypes32>(args);
David Srbecky15c19752015-03-31 14:53:55 +0000125 }
126 }
127
128 // Compare objdump output to the recorded checks.
129 void CheckObjdumpOutput(bool is64bit, const char* args) {
130 std::vector<std::string> actual_lines = Objdump(is64bit, args);
131 auto actual_line = actual_lines.begin();
132 for (const ExpectedLine& expected_line : expected_lines_) {
133 const std::string& substring = expected_line.substring;
134 if (actual_line == actual_lines.end()) {
135 ADD_FAILURE_AT(expected_line.at_file, expected_line.at_line) <<
136 "Expected '" << substring << "'.\n" <<
137 "Seen end of output.";
138 } else if (expected_line.next) {
139 if (actual_line->find(substring) == std::string::npos) {
140 ADD_FAILURE_AT(expected_line.at_file, expected_line.at_line) <<
141 "Expected '" << substring << "'.\n" <<
142 "Seen '" << actual_line->data() << "'.";
143 } else {
144 // printf("Found '%s' in '%s'.\n", substring.data(), actual_line->data());
145 }
146 actual_line++;
147 } else {
148 bool found = false;
149 for (auto it = actual_line; it < actual_lines.end(); it++) {
150 if (it->find(substring) != std::string::npos) {
151 actual_line = it;
152 found = true;
153 break;
154 }
155 }
156 if (!found) {
157 ADD_FAILURE_AT(expected_line.at_file, expected_line.at_line) <<
158 "Expected '" << substring << "'.\n" <<
159 "Not found anywhere in the rest of the output.";
160 } else {
161 // printf("Found '%s' in '%s'.\n", substring.data(), actual_line->data());
162 actual_line++;
163 }
164 }
165 }
166 }
167
168 // Buffers which are going to assembled into ELF file and passed to objdump.
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100169 std::vector<uint8_t> debug_frame_data_;
David Srbecky15c19752015-03-31 14:53:55 +0000170 std::vector<uint8_t> debug_info_data_;
171 std::vector<uint8_t> debug_abbrev_data_;
172 std::vector<uint8_t> debug_str_data_;
173 std::vector<uint8_t> debug_line_data_;
174
175 // The expected output of objdump.
176 std::vector<ExpectedLine> expected_lines_;
177};
178
179} // namespace dwarf
180} // namespace art
181
182#endif // ART_COMPILER_DWARF_DWARF_TEST_H_