blob: e2f0a65ab70a46d7547319fb2e12235552e6cd50 [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
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"
Vladimir Marko131980f2015-12-03 18:29:23 +000032#include "linker/file_output_stream.h"
David Srbecky15c19752015-03-31 14:53:55 +000033#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 Srbeckybc90fd02015-04-22 19:40:27 +010060 std::vector<std::string> Objdump(const char* args) {
David Srbecky15c19752015-03-31 14:53:55 +000061 // Write simple elf file with just the DWARF sections.
David Srbeckybc90fd02015-04-22 19:40:27 +010062 InstructionSet isa = (sizeof(typename ElfTypes::Addr) == 8) ? kX86_64 : kX86;
David Srbecky6d8c8f02015-10-26 10:57:09 +000063 ScratchFile file;
64 FileOutputStream output_stream(file.GetFile());
David Srbecky5d811202016-03-08 13:21:22 +000065 ElfBuilder<ElfTypes> builder(isa, nullptr, &output_stream);
David Srbecky6d8c8f02015-10-26 10:57:09 +000066 builder.Start();
David Srbecky15c19752015-03-31 14:53:55 +000067 if (!debug_info_data_.empty()) {
David Srbecky6d8c8f02015-10-26 10:57:09 +000068 builder.WriteSection(".debug_info", &debug_info_data_);
David Srbecky15c19752015-03-31 14:53:55 +000069 }
70 if (!debug_abbrev_data_.empty()) {
David Srbecky6d8c8f02015-10-26 10:57:09 +000071 builder.WriteSection(".debug_abbrev", &debug_abbrev_data_);
David Srbecky15c19752015-03-31 14:53:55 +000072 }
73 if (!debug_str_data_.empty()) {
David Srbecky6d8c8f02015-10-26 10:57:09 +000074 builder.WriteSection(".debug_str", &debug_str_data_);
David Srbecky15c19752015-03-31 14:53:55 +000075 }
76 if (!debug_line_data_.empty()) {
David Srbecky6d8c8f02015-10-26 10:57:09 +000077 builder.WriteSection(".debug_line", &debug_line_data_);
David Srbecky15c19752015-03-31 14:53:55 +000078 }
David Srbeckyad5fa8c2015-05-06 18:27:35 +010079 if (!debug_frame_data_.empty()) {
David Srbecky6d8c8f02015-10-26 10:57:09 +000080 builder.WriteSection(".debug_frame", &debug_frame_data_);
David Srbecky15c19752015-03-31 14:53:55 +000081 }
David Srbecky6d8c8f02015-10-26 10:57:09 +000082 builder.End();
83 EXPECT_TRUE(builder.Good());
David Srbecky15c19752015-03-31 14:53:55 +000084
85 // Read the elf file back using objdump.
86 std::vector<std::string> lines;
David Srbecky3e52aa42015-04-12 07:45:18 +010087 std::string cmd = GetAndroidHostToolsDir();
88 cmd = cmd + "objdump " + args + " " + file.GetFilename() + " 2>&1";
David Srbecky15c19752015-03-31 14:53:55 +000089 FILE* output = popen(cmd.data(), "r");
90 char buffer[1024];
91 const char* line;
92 while ((line = fgets(buffer, sizeof(buffer), output)) != nullptr) {
93 if (kPrintObjdumpOutput) {
94 printf("%s", line);
95 }
96 if (line[0] != '\0' && line[0] != '\n') {
97 EXPECT_TRUE(strstr(line, "objdump: Error:") == nullptr) << line;
98 EXPECT_TRUE(strstr(line, "objdump: Warning:") == nullptr) << line;
99 std::string str(line);
100 if (str.back() == '\n') {
101 str.pop_back();
102 }
103 lines.push_back(str);
104 }
105 }
106 pclose(output);
107 return lines;
108 }
109
110 std::vector<std::string> Objdump(bool is64bit, const char* args) {
111 if (is64bit) {
David Srbeckybc90fd02015-04-22 19:40:27 +0100112 return Objdump<ElfTypes64>(args);
David Srbecky15c19752015-03-31 14:53:55 +0000113 } else {
David Srbeckybc90fd02015-04-22 19:40:27 +0100114 return Objdump<ElfTypes32>(args);
David Srbecky15c19752015-03-31 14:53:55 +0000115 }
116 }
117
118 // Compare objdump output to the recorded checks.
119 void CheckObjdumpOutput(bool is64bit, const char* args) {
120 std::vector<std::string> actual_lines = Objdump(is64bit, args);
121 auto actual_line = actual_lines.begin();
122 for (const ExpectedLine& expected_line : expected_lines_) {
123 const std::string& substring = expected_line.substring;
124 if (actual_line == actual_lines.end()) {
125 ADD_FAILURE_AT(expected_line.at_file, expected_line.at_line) <<
126 "Expected '" << substring << "'.\n" <<
127 "Seen end of output.";
128 } else if (expected_line.next) {
129 if (actual_line->find(substring) == std::string::npos) {
130 ADD_FAILURE_AT(expected_line.at_file, expected_line.at_line) <<
131 "Expected '" << substring << "'.\n" <<
132 "Seen '" << actual_line->data() << "'.";
133 } else {
134 // printf("Found '%s' in '%s'.\n", substring.data(), actual_line->data());
135 }
136 actual_line++;
137 } else {
138 bool found = false;
139 for (auto it = actual_line; it < actual_lines.end(); it++) {
140 if (it->find(substring) != std::string::npos) {
141 actual_line = it;
142 found = true;
143 break;
144 }
145 }
146 if (!found) {
147 ADD_FAILURE_AT(expected_line.at_file, expected_line.at_line) <<
148 "Expected '" << substring << "'.\n" <<
149 "Not found anywhere in the rest of the output.";
150 } else {
151 // printf("Found '%s' in '%s'.\n", substring.data(), actual_line->data());
152 actual_line++;
153 }
154 }
155 }
156 }
157
158 // Buffers which are going to assembled into ELF file and passed to objdump.
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100159 std::vector<uint8_t> debug_frame_data_;
David Srbecky15c19752015-03-31 14:53:55 +0000160 std::vector<uint8_t> debug_info_data_;
161 std::vector<uint8_t> debug_abbrev_data_;
162 std::vector<uint8_t> debug_str_data_;
163 std::vector<uint8_t> debug_line_data_;
164
165 // The expected output of objdump.
166 std::vector<ExpectedLine> expected_lines_;
167};
168
169} // namespace dwarf
170} // namespace art
171
David Srbecky4fda4eb2016-02-05 13:34:46 +0000172#endif // ART_COMPILER_DEBUG_DWARF_DWARF_TEST_H_