blob: 5464ed9c49c305f80785b1bf5649021d857eb741 [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 Srbecky6d8c8f02015-10-26 10:57:09 +000062 ScratchFile file;
63 FileOutputStream output_stream(file.GetFile());
64 ElfBuilder<ElfTypes> builder(isa, &output_stream);
65 builder.Start();
David Srbecky15c19752015-03-31 14:53:55 +000066 if (!debug_info_data_.empty()) {
David Srbecky6d8c8f02015-10-26 10:57:09 +000067 builder.WriteSection(".debug_info", &debug_info_data_);
David Srbecky15c19752015-03-31 14:53:55 +000068 }
69 if (!debug_abbrev_data_.empty()) {
David Srbecky6d8c8f02015-10-26 10:57:09 +000070 builder.WriteSection(".debug_abbrev", &debug_abbrev_data_);
David Srbecky15c19752015-03-31 14:53:55 +000071 }
72 if (!debug_str_data_.empty()) {
David Srbecky6d8c8f02015-10-26 10:57:09 +000073 builder.WriteSection(".debug_str", &debug_str_data_);
David Srbecky15c19752015-03-31 14:53:55 +000074 }
75 if (!debug_line_data_.empty()) {
David Srbecky6d8c8f02015-10-26 10:57:09 +000076 builder.WriteSection(".debug_line", &debug_line_data_);
David Srbecky15c19752015-03-31 14:53:55 +000077 }
David Srbeckyad5fa8c2015-05-06 18:27:35 +010078 if (!debug_frame_data_.empty()) {
David Srbecky6d8c8f02015-10-26 10:57:09 +000079 builder.WriteSection(".debug_frame", &debug_frame_data_);
David Srbecky15c19752015-03-31 14:53:55 +000080 }
David Srbecky6d8c8f02015-10-26 10:57:09 +000081 builder.End();
82 EXPECT_TRUE(builder.Good());
David Srbecky15c19752015-03-31 14:53:55 +000083
84 // Read the elf file back using objdump.
85 std::vector<std::string> lines;
David Srbecky3e52aa42015-04-12 07:45:18 +010086 std::string cmd = GetAndroidHostToolsDir();
87 cmd = cmd + "objdump " + args + " " + file.GetFilename() + " 2>&1";
David Srbecky15c19752015-03-31 14:53:55 +000088 FILE* output = popen(cmd.data(), "r");
89 char buffer[1024];
90 const char* line;
91 while ((line = fgets(buffer, sizeof(buffer), output)) != nullptr) {
92 if (kPrintObjdumpOutput) {
93 printf("%s", line);
94 }
95 if (line[0] != '\0' && line[0] != '\n') {
96 EXPECT_TRUE(strstr(line, "objdump: Error:") == nullptr) << line;
97 EXPECT_TRUE(strstr(line, "objdump: Warning:") == nullptr) << line;
98 std::string str(line);
99 if (str.back() == '\n') {
100 str.pop_back();
101 }
102 lines.push_back(str);
103 }
104 }
105 pclose(output);
106 return lines;
107 }
108
109 std::vector<std::string> Objdump(bool is64bit, const char* args) {
110 if (is64bit) {
David Srbeckybc90fd02015-04-22 19:40:27 +0100111 return Objdump<ElfTypes64>(args);
David Srbecky15c19752015-03-31 14:53:55 +0000112 } else {
David Srbeckybc90fd02015-04-22 19:40:27 +0100113 return Objdump<ElfTypes32>(args);
David Srbecky15c19752015-03-31 14:53:55 +0000114 }
115 }
116
117 // Compare objdump output to the recorded checks.
118 void CheckObjdumpOutput(bool is64bit, const char* args) {
119 std::vector<std::string> actual_lines = Objdump(is64bit, args);
120 auto actual_line = actual_lines.begin();
121 for (const ExpectedLine& expected_line : expected_lines_) {
122 const std::string& substring = expected_line.substring;
123 if (actual_line == actual_lines.end()) {
124 ADD_FAILURE_AT(expected_line.at_file, expected_line.at_line) <<
125 "Expected '" << substring << "'.\n" <<
126 "Seen end of output.";
127 } else if (expected_line.next) {
128 if (actual_line->find(substring) == std::string::npos) {
129 ADD_FAILURE_AT(expected_line.at_file, expected_line.at_line) <<
130 "Expected '" << substring << "'.\n" <<
131 "Seen '" << actual_line->data() << "'.";
132 } else {
133 // printf("Found '%s' in '%s'.\n", substring.data(), actual_line->data());
134 }
135 actual_line++;
136 } else {
137 bool found = false;
138 for (auto it = actual_line; it < actual_lines.end(); it++) {
139 if (it->find(substring) != std::string::npos) {
140 actual_line = it;
141 found = true;
142 break;
143 }
144 }
145 if (!found) {
146 ADD_FAILURE_AT(expected_line.at_file, expected_line.at_line) <<
147 "Expected '" << substring << "'.\n" <<
148 "Not found anywhere in the rest of the output.";
149 } else {
150 // printf("Found '%s' in '%s'.\n", substring.data(), actual_line->data());
151 actual_line++;
152 }
153 }
154 }
155 }
156
157 // Buffers which are going to assembled into ELF file and passed to objdump.
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100158 std::vector<uint8_t> debug_frame_data_;
David Srbecky15c19752015-03-31 14:53:55 +0000159 std::vector<uint8_t> debug_info_data_;
160 std::vector<uint8_t> debug_abbrev_data_;
161 std::vector<uint8_t> debug_str_data_;
162 std::vector<uint8_t> debug_line_data_;
163
164 // The expected output of objdump.
165 std::vector<ExpectedLine> expected_lines_;
166};
167
168} // namespace dwarf
169} // namespace art
170
171#endif // ART_COMPILER_DWARF_DWARF_TEST_H_