1 /* 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_DEBUG_DWARF_DWARF_TEST_H_ 18 #define ART_COMPILER_DEBUG_DWARF_DWARF_TEST_H_ 19 20 #include <dirent.h> 21 #include <stdio.h> 22 #include <sys/types.h> 23 24 #include <cstring> 25 #include <memory> 26 #include <set> 27 #include <string> 28 29 #include "base/os.h" 30 #include "base/unix_file/fd_file.h" 31 #include "common_compiler_test.h" 32 #include "elf/elf_builder.h" 33 #include "gtest/gtest.h" 34 #include "stream/file_output_stream.h" 35 36 namespace art { 37 namespace dwarf { 38 39 #define DW_CHECK(substring) Check(substring, false, __FILE__, __LINE__) 40 #define DW_CHECK_NEXT(substring) Check(substring, true, __FILE__, __LINE__) 41 42 class DwarfTest : public CommonCompilerTest { 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. Check(const char * substr,bool next,const char * at_file,int at_line)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 59 // Pretty-print the generated DWARF data using objdump. 60 template<typename ElfTypes> Objdump(const char * args)61 std::vector<std::string> Objdump(const char* args) { 62 // Write simple elf file with just the DWARF sections. 63 InstructionSet isa = 64 (sizeof(typename ElfTypes::Addr) == 8) ? InstructionSet::kX86_64 : InstructionSet::kX86; 65 ScratchFile file; 66 FileOutputStream output_stream(file.GetFile()); 67 ElfBuilder<ElfTypes> builder(isa, &output_stream); 68 builder.Start(); 69 if (!debug_info_data_.empty()) { 70 builder.WriteSection(".debug_info", &debug_info_data_); 71 } 72 if (!debug_abbrev_data_.empty()) { 73 builder.WriteSection(".debug_abbrev", &debug_abbrev_data_); 74 } 75 if (!debug_str_data_.empty()) { 76 builder.WriteSection(".debug_str", &debug_str_data_); 77 } 78 if (!debug_line_data_.empty()) { 79 builder.WriteSection(".debug_line", &debug_line_data_); 80 } 81 if (!debug_frame_data_.empty()) { 82 builder.WriteSection(".debug_frame", &debug_frame_data_); 83 } 84 builder.End(); 85 EXPECT_TRUE(builder.Good()); 86 87 // Read the elf file back using objdump. 88 std::vector<std::string> lines; 89 std::string cmd = GetAndroidHostToolsDir(); 90 cmd = cmd + "objdump " + args + " " + file.GetFilename() + " 2>&1"; 91 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 Objdump(bool is64bit,const char * args)112 std::vector<std::string> Objdump(bool is64bit, const char* args) { 113 if (is64bit) { 114 return Objdump<ElfTypes64>(args); 115 } else { 116 return Objdump<ElfTypes32>(args); 117 } 118 } 119 120 // Compare objdump output to the recorded checks. CheckObjdumpOutput(bool is64bit,const char * args)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. 161 std::vector<uint8_t> debug_frame_data_; 162 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 174 #endif // ART_COMPILER_DEBUG_DWARF_DWARF_TEST_H_ 175