blob: 230ebe3a79e3688b72c2ab04b8faf9b0aae1e31a [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 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 Srbecky15c19752015-03-31 14:53:55 +000063 class NoCode : public CodeOutput {
David Srbeckybc90fd02015-04-22 19:40:27 +010064 bool Write(OutputStream*) OVERRIDE { return true; } // NOLINT
65 } no_code;
66 ElfBuilder<ElfTypes> builder(isa, 0, &no_code, 0, &no_code, 0);
67 typedef typename ElfBuilder<ElfTypes>::RawSection RawSection;
68 RawSection debug_info(".debug_info", SHT_PROGBITS, 0, nullptr, 0, 1, 0);
69 RawSection debug_abbrev(".debug_abbrev", SHT_PROGBITS, 0, nullptr, 0, 1, 0);
70 RawSection debug_str(".debug_str", SHT_PROGBITS, 0, nullptr, 0, 1, 0);
71 RawSection debug_line(".debug_line", SHT_PROGBITS, 0, nullptr, 0, 1, 0);
72 RawSection eh_frame(".eh_frame", SHT_PROGBITS, SHF_ALLOC, nullptr, 0, kPageSize, 0);
David Srbecky15c19752015-03-31 14:53:55 +000073 if (!debug_info_data_.empty()) {
David Srbecky15c19752015-03-31 14:53:55 +000074 debug_info.SetBuffer(debug_info_data_);
David Srbeckybc90fd02015-04-22 19:40:27 +010075 builder.RegisterSection(&debug_info);
David Srbecky15c19752015-03-31 14:53:55 +000076 }
77 if (!debug_abbrev_data_.empty()) {
David Srbecky15c19752015-03-31 14:53:55 +000078 debug_abbrev.SetBuffer(debug_abbrev_data_);
David Srbeckybc90fd02015-04-22 19:40:27 +010079 builder.RegisterSection(&debug_abbrev);
David Srbecky15c19752015-03-31 14:53:55 +000080 }
81 if (!debug_str_data_.empty()) {
David Srbecky15c19752015-03-31 14:53:55 +000082 debug_str.SetBuffer(debug_str_data_);
David Srbeckybc90fd02015-04-22 19:40:27 +010083 builder.RegisterSection(&debug_str);
David Srbecky15c19752015-03-31 14:53:55 +000084 }
85 if (!debug_line_data_.empty()) {
David Srbecky15c19752015-03-31 14:53:55 +000086 debug_line.SetBuffer(debug_line_data_);
David Srbeckybc90fd02015-04-22 19:40:27 +010087 builder.RegisterSection(&debug_line);
David Srbecky15c19752015-03-31 14:53:55 +000088 }
89 if (!eh_frame_data_.empty()) {
David Srbecky15c19752015-03-31 14:53:55 +000090 eh_frame.SetBuffer(eh_frame_data_);
David Srbeckybc90fd02015-04-22 19:40:27 +010091 builder.RegisterSection(&eh_frame);
David Srbecky15c19752015-03-31 14:53:55 +000092 }
David Srbeckybc90fd02015-04-22 19:40:27 +010093 ScratchFile file;
94 builder.Write(file.GetFile());
David Srbecky15c19752015-03-31 14:53:55 +000095
96 // Read the elf file back using objdump.
97 std::vector<std::string> lines;
David Srbecky3e52aa42015-04-12 07:45:18 +010098 std::string cmd = GetAndroidHostToolsDir();
99 cmd = cmd + "objdump " + args + " " + file.GetFilename() + " 2>&1";
David Srbecky15c19752015-03-31 14:53:55 +0000100 FILE* output = popen(cmd.data(), "r");
101 char buffer[1024];
102 const char* line;
103 while ((line = fgets(buffer, sizeof(buffer), output)) != nullptr) {
104 if (kPrintObjdumpOutput) {
105 printf("%s", line);
106 }
107 if (line[0] != '\0' && line[0] != '\n') {
108 EXPECT_TRUE(strstr(line, "objdump: Error:") == nullptr) << line;
109 EXPECT_TRUE(strstr(line, "objdump: Warning:") == nullptr) << line;
110 std::string str(line);
111 if (str.back() == '\n') {
112 str.pop_back();
113 }
114 lines.push_back(str);
115 }
116 }
117 pclose(output);
118 return lines;
119 }
120
121 std::vector<std::string> Objdump(bool is64bit, const char* args) {
122 if (is64bit) {
David Srbeckybc90fd02015-04-22 19:40:27 +0100123 return Objdump<ElfTypes64>(args);
David Srbecky15c19752015-03-31 14:53:55 +0000124 } else {
David Srbeckybc90fd02015-04-22 19:40:27 +0100125 return Objdump<ElfTypes32>(args);
David Srbecky15c19752015-03-31 14:53:55 +0000126 }
127 }
128
129 // Compare objdump output to the recorded checks.
130 void CheckObjdumpOutput(bool is64bit, const char* args) {
131 std::vector<std::string> actual_lines = Objdump(is64bit, args);
132 auto actual_line = actual_lines.begin();
133 for (const ExpectedLine& expected_line : expected_lines_) {
134 const std::string& substring = expected_line.substring;
135 if (actual_line == actual_lines.end()) {
136 ADD_FAILURE_AT(expected_line.at_file, expected_line.at_line) <<
137 "Expected '" << substring << "'.\n" <<
138 "Seen end of output.";
139 } else if (expected_line.next) {
140 if (actual_line->find(substring) == std::string::npos) {
141 ADD_FAILURE_AT(expected_line.at_file, expected_line.at_line) <<
142 "Expected '" << substring << "'.\n" <<
143 "Seen '" << actual_line->data() << "'.";
144 } else {
145 // printf("Found '%s' in '%s'.\n", substring.data(), actual_line->data());
146 }
147 actual_line++;
148 } else {
149 bool found = false;
150 for (auto it = actual_line; it < actual_lines.end(); it++) {
151 if (it->find(substring) != std::string::npos) {
152 actual_line = it;
153 found = true;
154 break;
155 }
156 }
157 if (!found) {
158 ADD_FAILURE_AT(expected_line.at_file, expected_line.at_line) <<
159 "Expected '" << substring << "'.\n" <<
160 "Not found anywhere in the rest of the output.";
161 } else {
162 // printf("Found '%s' in '%s'.\n", substring.data(), actual_line->data());
163 actual_line++;
164 }
165 }
166 }
167 }
168
169 // Buffers which are going to assembled into ELF file and passed to objdump.
170 std::vector<uint8_t> eh_frame_data_;
171 std::vector<uint8_t> debug_info_data_;
172 std::vector<uint8_t> debug_abbrev_data_;
173 std::vector<uint8_t> debug_str_data_;
174 std::vector<uint8_t> debug_line_data_;
175
176 // The expected output of objdump.
177 std::vector<ExpectedLine> expected_lines_;
178};
179
180} // namespace dwarf
181} // namespace art
182
183#endif // ART_COMPILER_DWARF_DWARF_TEST_H_