blob: b30ff143d3282a4815e58838b5f7da8bfe22f177 [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 Srbecky15c19752015-03-31 14:53:55 +000029#include "base/unix_file/fd_file.h"
30#include "common_runtime_test.h"
David Srbecky15c19752015-03-31 14:53:55 +000031#include "gtest/gtest.h"
Vladimir Marko74527972016-11-29 15:57:32 +000032#include "linker/elf_builder.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000033#include "linker/file_output_stream.h"
David Srbecky15c19752015-03-31 14:53:55 +000034#include "os.h"
35
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
42class DwarfTest : public CommonRuntimeTest {
43 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.
David Srbeckybc90fd02015-04-22 19:40:27 +010063 InstructionSet isa = (sizeof(typename ElfTypes::Addr) == 8) ? kX86_64 : kX86;
David Srbecky6d8c8f02015-10-26 10:57:09 +000064 ScratchFile file;
Vladimir Marko74527972016-11-29 15:57:32 +000065 linker::FileOutputStream output_stream(file.GetFile());
66 linker::ElfBuilder<ElfTypes> builder(isa, nullptr, &output_stream);
David Srbecky6d8c8f02015-10-26 10:57:09 +000067 builder.Start();
David Srbecky15c19752015-03-31 14:53:55 +000068 if (!debug_info_data_.empty()) {
David Srbecky6d8c8f02015-10-26 10:57:09 +000069 builder.WriteSection(".debug_info", &debug_info_data_);
David Srbecky15c19752015-03-31 14:53:55 +000070 }
71 if (!debug_abbrev_data_.empty()) {
David Srbecky6d8c8f02015-10-26 10:57:09 +000072 builder.WriteSection(".debug_abbrev", &debug_abbrev_data_);
David Srbecky15c19752015-03-31 14:53:55 +000073 }
74 if (!debug_str_data_.empty()) {
David Srbecky6d8c8f02015-10-26 10:57:09 +000075 builder.WriteSection(".debug_str", &debug_str_data_);
David Srbecky15c19752015-03-31 14:53:55 +000076 }
77 if (!debug_line_data_.empty()) {
David Srbecky6d8c8f02015-10-26 10:57:09 +000078 builder.WriteSection(".debug_line", &debug_line_data_);
David Srbecky15c19752015-03-31 14:53:55 +000079 }
David Srbeckyad5fa8c2015-05-06 18:27:35 +010080 if (!debug_frame_data_.empty()) {
David Srbecky6d8c8f02015-10-26 10:57:09 +000081 builder.WriteSection(".debug_frame", &debug_frame_data_);
David Srbecky15c19752015-03-31 14:53:55 +000082 }
David Srbecky6d8c8f02015-10-26 10:57:09 +000083 builder.End();
84 EXPECT_TRUE(builder.Good());
David Srbecky15c19752015-03-31 14:53:55 +000085
86 // Read the elf file back using objdump.
87 std::vector<std::string> lines;
David Srbecky3e52aa42015-04-12 07:45:18 +010088 std::string cmd = GetAndroidHostToolsDir();
89 cmd = cmd + "objdump " + args + " " + file.GetFilename() + " 2>&1";
David Srbecky15c19752015-03-31 14:53:55 +000090 FILE* output = popen(cmd.data(), "r");
91 char buffer[1024];
92 const char* line;
93 while ((line = fgets(buffer, sizeof(buffer), output)) != nullptr) {
94 if (kPrintObjdumpOutput) {
95 printf("%s", line);
96 }
97 if (line[0] != '\0' && line[0] != '\n') {
98 EXPECT_TRUE(strstr(line, "objdump: Error:") == nullptr) << line;
99 EXPECT_TRUE(strstr(line, "objdump: Warning:") == nullptr) << line;
100 std::string str(line);
101 if (str.back() == '\n') {
102 str.pop_back();
103 }
104 lines.push_back(str);
105 }
106 }
107 pclose(output);
108 return lines;
109 }
110
111 std::vector<std::string> Objdump(bool is64bit, const char* args) {
112 if (is64bit) {
David Srbeckybc90fd02015-04-22 19:40:27 +0100113 return Objdump<ElfTypes64>(args);
David Srbecky15c19752015-03-31 14:53:55 +0000114 } else {
David Srbeckybc90fd02015-04-22 19:40:27 +0100115 return Objdump<ElfTypes32>(args);
David Srbecky15c19752015-03-31 14:53:55 +0000116 }
117 }
118
119 // Compare objdump output to the recorded checks.
120 void CheckObjdumpOutput(bool is64bit, const char* args) {
121 std::vector<std::string> actual_lines = Objdump(is64bit, args);
122 auto actual_line = actual_lines.begin();
123 for (const ExpectedLine& expected_line : expected_lines_) {
124 const std::string& substring = expected_line.substring;
125 if (actual_line == actual_lines.end()) {
126 ADD_FAILURE_AT(expected_line.at_file, expected_line.at_line) <<
127 "Expected '" << substring << "'.\n" <<
128 "Seen end of output.";
129 } else if (expected_line.next) {
130 if (actual_line->find(substring) == std::string::npos) {
131 ADD_FAILURE_AT(expected_line.at_file, expected_line.at_line) <<
132 "Expected '" << substring << "'.\n" <<
133 "Seen '" << actual_line->data() << "'.";
134 } else {
135 // printf("Found '%s' in '%s'.\n", substring.data(), actual_line->data());
136 }
137 actual_line++;
138 } else {
139 bool found = false;
140 for (auto it = actual_line; it < actual_lines.end(); it++) {
141 if (it->find(substring) != std::string::npos) {
142 actual_line = it;
143 found = true;
144 break;
145 }
146 }
147 if (!found) {
148 ADD_FAILURE_AT(expected_line.at_file, expected_line.at_line) <<
149 "Expected '" << substring << "'.\n" <<
150 "Not found anywhere in the rest of the output.";
151 } else {
152 // printf("Found '%s' in '%s'.\n", substring.data(), actual_line->data());
153 actual_line++;
154 }
155 }
156 }
157 }
158
159 // Buffers which are going to assembled into ELF file and passed to objdump.
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100160 std::vector<uint8_t> debug_frame_data_;
David Srbecky15c19752015-03-31 14:53:55 +0000161 std::vector<uint8_t> debug_info_data_;
162 std::vector<uint8_t> debug_abbrev_data_;
163 std::vector<uint8_t> debug_str_data_;
164 std::vector<uint8_t> debug_line_data_;
165
166 // The expected output of objdump.
167 std::vector<ExpectedLine> expected_lines_;
168};
169
170} // namespace dwarf
171} // namespace art
172
David Srbecky4fda4eb2016-02-05 13:34:46 +0000173#endif // ART_COMPILER_DEBUG_DWARF_DWARF_TEST_H_