David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 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_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 | |
| 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. |
| 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 Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 58 | // Pretty-print the generated DWARF data using objdump. |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 59 | template<typename ElfTypes> |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 60 | std::vector<std::string> Objdump(bool is64bit, const char* args) { |
| 61 | // Write simple elf file with just the DWARF sections. |
| 62 | class NoCode : public CodeOutput { |
| 63 | virtual void SetCodeOffset(size_t) { } |
| 64 | virtual bool Write(OutputStream*) { return true; } |
| 65 | } code; |
| 66 | ScratchFile file; |
| 67 | InstructionSet isa = is64bit ? kX86_64 : kX86; |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 68 | ElfBuilder<ElfTypes> builder( |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 69 | &code, file.GetFile(), isa, 0, 0, 0, 0, 0, 0, false, false); |
David Srbecky | 0c5bbc1 | 2015-04-28 17:54:52 +0100 | [diff] [blame^] | 70 | typedef typename ElfBuilder<ElfTypes>::ElfRawSectionBuilder Section; |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 71 | Section debug_info(".debug_info", SHT_PROGBITS, 0, nullptr, 0, 1, 0); |
| 72 | Section debug_abbrev(".debug_abbrev", SHT_PROGBITS, 0, nullptr, 0, 1, 0); |
| 73 | Section debug_str(".debug_str", SHT_PROGBITS, 0, nullptr, 0, 1, 0); |
| 74 | Section debug_line(".debug_line", SHT_PROGBITS, 0, nullptr, 0, 1, 0); |
| 75 | Section eh_frame(".eh_frame", SHT_PROGBITS, SHF_ALLOC, nullptr, 0, 4, 0); |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 76 | if (!debug_info_data_.empty()) { |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 77 | debug_info.SetBuffer(debug_info_data_); |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 78 | builder.RegisterRawSection(&debug_info); |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 79 | } |
| 80 | if (!debug_abbrev_data_.empty()) { |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 81 | debug_abbrev.SetBuffer(debug_abbrev_data_); |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 82 | builder.RegisterRawSection(&debug_abbrev); |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 83 | } |
| 84 | if (!debug_str_data_.empty()) { |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 85 | debug_str.SetBuffer(debug_str_data_); |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 86 | builder.RegisterRawSection(&debug_str); |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 87 | } |
| 88 | if (!debug_line_data_.empty()) { |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 89 | debug_line.SetBuffer(debug_line_data_); |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 90 | builder.RegisterRawSection(&debug_line); |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 91 | } |
| 92 | if (!eh_frame_data_.empty()) { |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 93 | eh_frame.SetBuffer(eh_frame_data_); |
David Srbecky | 527c9c7 | 2015-04-17 21:14:10 +0100 | [diff] [blame] | 94 | builder.RegisterRawSection(&eh_frame); |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 95 | } |
| 96 | builder.Init(); |
| 97 | builder.Write(); |
| 98 | |
| 99 | // Read the elf file back using objdump. |
| 100 | std::vector<std::string> lines; |
David Srbecky | 3e52aa4 | 2015-04-12 07:45:18 +0100 | [diff] [blame] | 101 | std::string cmd = GetAndroidHostToolsDir(); |
| 102 | cmd = cmd + "objdump " + args + " " + file.GetFilename() + " 2>&1"; |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 103 | FILE* output = popen(cmd.data(), "r"); |
| 104 | char buffer[1024]; |
| 105 | const char* line; |
| 106 | while ((line = fgets(buffer, sizeof(buffer), output)) != nullptr) { |
| 107 | if (kPrintObjdumpOutput) { |
| 108 | printf("%s", line); |
| 109 | } |
| 110 | if (line[0] != '\0' && line[0] != '\n') { |
| 111 | EXPECT_TRUE(strstr(line, "objdump: Error:") == nullptr) << line; |
| 112 | EXPECT_TRUE(strstr(line, "objdump: Warning:") == nullptr) << line; |
| 113 | std::string str(line); |
| 114 | if (str.back() == '\n') { |
| 115 | str.pop_back(); |
| 116 | } |
| 117 | lines.push_back(str); |
| 118 | } |
| 119 | } |
| 120 | pclose(output); |
| 121 | return lines; |
| 122 | } |
| 123 | |
| 124 | std::vector<std::string> Objdump(bool is64bit, const char* args) { |
| 125 | if (is64bit) { |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 126 | return Objdump<ElfTypes64>(is64bit, args); |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 127 | } else { |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 128 | return Objdump<ElfTypes32>(is64bit, args); |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
| 132 | // Compare objdump output to the recorded checks. |
| 133 | void CheckObjdumpOutput(bool is64bit, const char* args) { |
| 134 | std::vector<std::string> actual_lines = Objdump(is64bit, args); |
| 135 | auto actual_line = actual_lines.begin(); |
| 136 | for (const ExpectedLine& expected_line : expected_lines_) { |
| 137 | const std::string& substring = expected_line.substring; |
| 138 | if (actual_line == actual_lines.end()) { |
| 139 | ADD_FAILURE_AT(expected_line.at_file, expected_line.at_line) << |
| 140 | "Expected '" << substring << "'.\n" << |
| 141 | "Seen end of output."; |
| 142 | } else if (expected_line.next) { |
| 143 | if (actual_line->find(substring) == std::string::npos) { |
| 144 | ADD_FAILURE_AT(expected_line.at_file, expected_line.at_line) << |
| 145 | "Expected '" << substring << "'.\n" << |
| 146 | "Seen '" << actual_line->data() << "'."; |
| 147 | } else { |
| 148 | // printf("Found '%s' in '%s'.\n", substring.data(), actual_line->data()); |
| 149 | } |
| 150 | actual_line++; |
| 151 | } else { |
| 152 | bool found = false; |
| 153 | for (auto it = actual_line; it < actual_lines.end(); it++) { |
| 154 | if (it->find(substring) != std::string::npos) { |
| 155 | actual_line = it; |
| 156 | found = true; |
| 157 | break; |
| 158 | } |
| 159 | } |
| 160 | if (!found) { |
| 161 | ADD_FAILURE_AT(expected_line.at_file, expected_line.at_line) << |
| 162 | "Expected '" << substring << "'.\n" << |
| 163 | "Not found anywhere in the rest of the output."; |
| 164 | } else { |
| 165 | // printf("Found '%s' in '%s'.\n", substring.data(), actual_line->data()); |
| 166 | actual_line++; |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | // Buffers which are going to assembled into ELF file and passed to objdump. |
| 173 | std::vector<uint8_t> eh_frame_data_; |
| 174 | std::vector<uint8_t> debug_info_data_; |
| 175 | std::vector<uint8_t> debug_abbrev_data_; |
| 176 | std::vector<uint8_t> debug_str_data_; |
| 177 | std::vector<uint8_t> debug_line_data_; |
| 178 | |
| 179 | // The expected output of objdump. |
| 180 | std::vector<ExpectedLine> expected_lines_; |
| 181 | }; |
| 182 | |
| 183 | } // namespace dwarf |
| 184 | } // namespace art |
| 185 | |
| 186 | #endif // ART_COMPILER_DWARF_DWARF_TEST_H_ |