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 | |
| 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 | |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 28 | #include "base/unix_file/fd_file.h" |
| 29 | #include "common_runtime_test.h" |
| 30 | #include "elf_builder.h" |
| 31 | #include "gtest/gtest.h" |
Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 32 | #include "linker/file_output_stream.h" |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 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 | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 60 | std::vector<std::string> Objdump(const char* args) { |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 61 | // Write simple elf file with just the DWARF sections. |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 62 | InstructionSet isa = (sizeof(typename ElfTypes::Addr) == 8) ? kX86_64 : kX86; |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 63 | ScratchFile file; |
| 64 | FileOutputStream output_stream(file.GetFile()); |
David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 65 | ElfBuilder<ElfTypes> builder(isa, nullptr, &output_stream); |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 66 | builder.Start(); |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 67 | if (!debug_info_data_.empty()) { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 68 | builder.WriteSection(".debug_info", &debug_info_data_); |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 69 | } |
| 70 | if (!debug_abbrev_data_.empty()) { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 71 | builder.WriteSection(".debug_abbrev", &debug_abbrev_data_); |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 72 | } |
| 73 | if (!debug_str_data_.empty()) { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 74 | builder.WriteSection(".debug_str", &debug_str_data_); |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 75 | } |
| 76 | if (!debug_line_data_.empty()) { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 77 | builder.WriteSection(".debug_line", &debug_line_data_); |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 78 | } |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 79 | if (!debug_frame_data_.empty()) { |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 80 | builder.WriteSection(".debug_frame", &debug_frame_data_); |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 81 | } |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 82 | builder.End(); |
| 83 | EXPECT_TRUE(builder.Good()); |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 84 | |
| 85 | // Read the elf file back using objdump. |
| 86 | std::vector<std::string> lines; |
David Srbecky | 3e52aa4 | 2015-04-12 07:45:18 +0100 | [diff] [blame] | 87 | std::string cmd = GetAndroidHostToolsDir(); |
| 88 | cmd = cmd + "objdump " + args + " " + file.GetFilename() + " 2>&1"; |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 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 | |
| 110 | std::vector<std::string> Objdump(bool is64bit, const char* args) { |
| 111 | if (is64bit) { |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 112 | return Objdump<ElfTypes64>(args); |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 113 | } else { |
David Srbecky | bc90fd0 | 2015-04-22 19:40:27 +0100 | [diff] [blame] | 114 | return Objdump<ElfTypes32>(args); |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
| 118 | // Compare objdump output to the recorded checks. |
| 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. |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 159 | std::vector<uint8_t> debug_frame_data_; |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 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 | |
David Srbecky | 4fda4eb | 2016-02-05 13:34:46 +0000 | [diff] [blame] | 172 | #endif // ART_COMPILER_DEBUG_DWARF_DWARF_TEST_H_ |