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 | |
David Srbecky | 4fda4eb | 2016-02-05 13:34:46 +0000 | [diff] [blame] | 17 | #ifndef ART_COMPILER_DEBUG_DWARF_DWARF_TEST_H_ |
| 18 | #define ART_COMPILER_DEBUG_DWARF_DWARF_TEST_H_ |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 19 | |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 20 | #include <dirent.h> |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <sys/types.h> |
| 23 | |
| 24 | #include <cstring> |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 25 | #include <memory> |
| 26 | #include <set> |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 27 | #include <string> |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 28 | |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 29 | #include "base/os.h" |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 30 | #include "base/unix_file/fd_file.h" |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame^] | 31 | #include "common_compiler_test.h" |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 32 | #include "gtest/gtest.h" |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 33 | #include "linker/elf_builder.h" |
Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 34 | #include "linker/file_output_stream.h" |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 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 | |
Vladimir Marko | a043111 | 2018-06-25 09:32:54 +0100 | [diff] [blame^] | 42 | class DwarfTest : public CommonCompilerTest { |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 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 Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 59 | // Pretty-print the generated DWARF data using objdump. |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 60 | template<typename ElfTypes> |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 61 | std::vector<std::string> Objdump(const char* args) { |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 62 | // Write simple elf file with just the DWARF sections. |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 63 | InstructionSet isa = |
| 64 | (sizeof(typename ElfTypes::Addr) == 8) ? InstructionSet::kX86_64 : InstructionSet::kX86; |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 65 | ScratchFile file; |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 66 | linker::FileOutputStream output_stream(file.GetFile()); |
| 67 | linker::ElfBuilder<ElfTypes> builder(isa, nullptr, &output_stream); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 68 | builder.Start(); |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 69 | if (!debug_info_data_.empty()) { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 70 | builder.WriteSection(".debug_info", &debug_info_data_); |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 71 | } |
| 72 | if (!debug_abbrev_data_.empty()) { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 73 | builder.WriteSection(".debug_abbrev", &debug_abbrev_data_); |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 74 | } |
| 75 | if (!debug_str_data_.empty()) { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 76 | builder.WriteSection(".debug_str", &debug_str_data_); |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 77 | } |
| 78 | if (!debug_line_data_.empty()) { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 79 | builder.WriteSection(".debug_line", &debug_line_data_); |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 80 | } |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 81 | if (!debug_frame_data_.empty()) { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 82 | builder.WriteSection(".debug_frame", &debug_frame_data_); |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 83 | } |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 84 | builder.End(); |
| 85 | EXPECT_TRUE(builder.Good()); |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 86 | |
| 87 | // Read the elf file back using objdump. |
| 88 | std::vector<std::string> lines; |
David Srbecky | 3e52aa4 | 2015-04-12 07:45:18 +0100 | [diff] [blame] | 89 | std::string cmd = GetAndroidHostToolsDir(); |
| 90 | cmd = cmd + "objdump " + args + " " + file.GetFilename() + " 2>&1"; |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 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 | |
| 112 | std::vector<std::string> Objdump(bool is64bit, const char* args) { |
| 113 | if (is64bit) { |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 114 | return Objdump<ElfTypes64>(args); |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 115 | } else { |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 116 | return Objdump<ElfTypes32>(args); |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
| 120 | // Compare objdump output to the recorded checks. |
| 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. |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 161 | std::vector<uint8_t> debug_frame_data_; |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 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 | |
David Srbecky | 4fda4eb | 2016-02-05 13:34:46 +0000 | [diff] [blame] | 174 | #endif // ART_COMPILER_DEBUG_DWARF_DWARF_TEST_H_ |