blob: 6b039a7b5f5e3ed95938107261c5ae9c7157e812 [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
David Srbecky4fda4eb2016-02-05 13:34:46 +000017#ifndef ART_COMPILER_DEBUG_DWARF_DWARF_TEST_H_
18#define ART_COMPILER_DEBUG_DWARF_DWARF_TEST_H_
David Srbecky15c19752015-03-31 14:53:55 +000019
David Srbecky15c19752015-03-31 14:53:55 +000020#include <dirent.h>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070021#include <stdio.h>
22#include <sys/types.h>
23
24#include <cstring>
David Srbecky15c19752015-03-31 14:53:55 +000025#include <memory>
26#include <set>
David Srbecky15c19752015-03-31 14:53:55 +000027#include <string>
David Srbecky15c19752015-03-31 14:53:55 +000028
David Sehrc431b9d2018-03-02 12:01:51 -080029#include "base/os.h"
David Srbecky15c19752015-03-31 14:53:55 +000030#include "base/unix_file/fd_file.h"
Vladimir Markoa0431112018-06-25 09:32:54 +010031#include "common_compiler_test.h"
David Srbecky15c19752015-03-31 14:53:55 +000032#include "gtest/gtest.h"
Vladimir Marko74527972016-11-29 15:57:32 +000033#include "linker/elf_builder.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000034#include "linker/file_output_stream.h"
David Srbecky15c19752015-03-31 14:53:55 +000035
36namespace art {
37namespace dwarf {
38
39#define DW_CHECK(substring) Check(substring, false, __FILE__, __LINE__)
40#define DW_CHECK_NEXT(substring) Check(substring, true, __FILE__, __LINE__)
41
Vladimir Markoa0431112018-06-25 09:32:54 +010042class DwarfTest : public CommonCompilerTest {
David Srbecky15c19752015-03-31 14:53:55 +000043 public:
44 static constexpr bool kPrintObjdumpOutput = false; // debugging.
45
46 struct ExpectedLine {
47 std::string substring;
48 bool next;
49 const char* at_file;
50 int at_line;
51 };
52
53 // Check that the objdump output contains given output.
54 // If next is true, it must be the next line. Otherwise lines are skipped.
55 void Check(const char* substr, bool next, const char* at_file, int at_line) {
56 expected_lines_.push_back(ExpectedLine {substr, next, at_file, at_line});
57 }
58
David Srbecky15c19752015-03-31 14:53:55 +000059 // Pretty-print the generated DWARF data using objdump.
David Srbecky533c2072015-04-22 12:20:22 +010060 template<typename ElfTypes>
David Srbeckybc90fd02015-04-22 19:40:27 +010061 std::vector<std::string> Objdump(const char* args) {
David Srbecky15c19752015-03-31 14:53:55 +000062 // Write simple elf file with just the DWARF sections.
Vladimir Marko33bff252017-11-01 14:35:42 +000063 InstructionSet isa =
64 (sizeof(typename ElfTypes::Addr) == 8) ? InstructionSet::kX86_64 : InstructionSet::kX86;
David Srbecky6d8c8f02015-10-26 10:57:09 +000065 ScratchFile file;
Vladimir Marko74527972016-11-29 15:57:32 +000066 linker::FileOutputStream output_stream(file.GetFile());
67 linker::ElfBuilder<ElfTypes> builder(isa, nullptr, &output_stream);
David Srbecky6d8c8f02015-10-26 10:57:09 +000068 builder.Start();
David Srbecky15c19752015-03-31 14:53:55 +000069 if (!debug_info_data_.empty()) {
David Srbecky6d8c8f02015-10-26 10:57:09 +000070 builder.WriteSection(".debug_info", &debug_info_data_);
David Srbecky15c19752015-03-31 14:53:55 +000071 }
72 if (!debug_abbrev_data_.empty()) {
David Srbecky6d8c8f02015-10-26 10:57:09 +000073 builder.WriteSection(".debug_abbrev", &debug_abbrev_data_);
David Srbecky15c19752015-03-31 14:53:55 +000074 }
75 if (!debug_str_data_.empty()) {
David Srbecky6d8c8f02015-10-26 10:57:09 +000076 builder.WriteSection(".debug_str", &debug_str_data_);
David Srbecky15c19752015-03-31 14:53:55 +000077 }
78 if (!debug_line_data_.empty()) {
David Srbecky6d8c8f02015-10-26 10:57:09 +000079 builder.WriteSection(".debug_line", &debug_line_data_);
David Srbecky15c19752015-03-31 14:53:55 +000080 }
David Srbeckyad5fa8c2015-05-06 18:27:35 +010081 if (!debug_frame_data_.empty()) {
David Srbecky6d8c8f02015-10-26 10:57:09 +000082 builder.WriteSection(".debug_frame", &debug_frame_data_);
David Srbecky15c19752015-03-31 14:53:55 +000083 }
David Srbecky6d8c8f02015-10-26 10:57:09 +000084 builder.End();
85 EXPECT_TRUE(builder.Good());
David Srbecky15c19752015-03-31 14:53:55 +000086
87 // Read the elf file back using objdump.
88 std::vector<std::string> lines;
David Srbecky3e52aa42015-04-12 07:45:18 +010089 std::string cmd = GetAndroidHostToolsDir();
90 cmd = cmd + "objdump " + args + " " + file.GetFilename() + " 2>&1";
David Srbecky15c19752015-03-31 14:53:55 +000091 FILE* output = popen(cmd.data(), "r");
92 char buffer[1024];
93 const char* line;
94 while ((line = fgets(buffer, sizeof(buffer), output)) != nullptr) {
95 if (kPrintObjdumpOutput) {
96 printf("%s", line);
97 }
98 if (line[0] != '\0' && line[0] != '\n') {
99 EXPECT_TRUE(strstr(line, "objdump: Error:") == nullptr) << line;
100 EXPECT_TRUE(strstr(line, "objdump: Warning:") == nullptr) << line;
101 std::string str(line);
102 if (str.back() == '\n') {
103 str.pop_back();
104 }
105 lines.push_back(str);
106 }
107 }
108 pclose(output);
109 return lines;
110 }
111
112 std::vector<std::string> Objdump(bool is64bit, const char* args) {
113 if (is64bit) {
David Srbeckybc90fd02015-04-22 19:40:27 +0100114 return Objdump<ElfTypes64>(args);
David Srbecky15c19752015-03-31 14:53:55 +0000115 } else {
David Srbeckybc90fd02015-04-22 19:40:27 +0100116 return Objdump<ElfTypes32>(args);
David Srbecky15c19752015-03-31 14:53:55 +0000117 }
118 }
119
120 // Compare objdump output to the recorded checks.
121 void CheckObjdumpOutput(bool is64bit, const char* args) {
122 std::vector<std::string> actual_lines = Objdump(is64bit, args);
123 auto actual_line = actual_lines.begin();
124 for (const ExpectedLine& expected_line : expected_lines_) {
125 const std::string& substring = expected_line.substring;
126 if (actual_line == actual_lines.end()) {
127 ADD_FAILURE_AT(expected_line.at_file, expected_line.at_line) <<
128 "Expected '" << substring << "'.\n" <<
129 "Seen end of output.";
130 } else if (expected_line.next) {
131 if (actual_line->find(substring) == std::string::npos) {
132 ADD_FAILURE_AT(expected_line.at_file, expected_line.at_line) <<
133 "Expected '" << substring << "'.\n" <<
134 "Seen '" << actual_line->data() << "'.";
135 } else {
136 // printf("Found '%s' in '%s'.\n", substring.data(), actual_line->data());
137 }
138 actual_line++;
139 } else {
140 bool found = false;
141 for (auto it = actual_line; it < actual_lines.end(); it++) {
142 if (it->find(substring) != std::string::npos) {
143 actual_line = it;
144 found = true;
145 break;
146 }
147 }
148 if (!found) {
149 ADD_FAILURE_AT(expected_line.at_file, expected_line.at_line) <<
150 "Expected '" << substring << "'.\n" <<
151 "Not found anywhere in the rest of the output.";
152 } else {
153 // printf("Found '%s' in '%s'.\n", substring.data(), actual_line->data());
154 actual_line++;
155 }
156 }
157 }
158 }
159
160 // Buffers which are going to assembled into ELF file and passed to objdump.
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100161 std::vector<uint8_t> debug_frame_data_;
David Srbecky15c19752015-03-31 14:53:55 +0000162 std::vector<uint8_t> debug_info_data_;
163 std::vector<uint8_t> debug_abbrev_data_;
164 std::vector<uint8_t> debug_str_data_;
165 std::vector<uint8_t> debug_line_data_;
166
167 // The expected output of objdump.
168 std::vector<ExpectedLine> expected_lines_;
169};
170
171} // namespace dwarf
172} // namespace art
173
David Srbecky4fda4eb2016-02-05 13:34:46 +0000174#endif // ART_COMPILER_DEBUG_DWARF_DWARF_TEST_H_