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