David Srbecky | 1109fb3 | 2015-04-07 20:21:06 +0100 | [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_CFI_TEST_H_ |
| 18 | #define ART_COMPILER_CFI_TEST_H_ |
| 19 | |
| 20 | #include <vector> |
| 21 | #include <memory> |
| 22 | #include <sstream> |
| 23 | |
| 24 | #include "arch/instruction_set.h" |
David Srbecky | 4fda4eb | 2016-02-05 13:34:46 +0000 | [diff] [blame] | 25 | #include "debug/dwarf/dwarf_constants.h" |
| 26 | #include "debug/dwarf/dwarf_test.h" |
| 27 | #include "debug/dwarf/headers.h" |
David Srbecky | 1109fb3 | 2015-04-07 20:21:06 +0100 | [diff] [blame] | 28 | #include "disassembler/disassembler.h" |
| 29 | #include "gtest/gtest.h" |
| 30 | |
| 31 | namespace art { |
| 32 | |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 33 | constexpr dwarf::CFIFormat kCFIFormat = dwarf::DW_DEBUG_FRAME_FORMAT; |
| 34 | |
David Srbecky | 1109fb3 | 2015-04-07 20:21:06 +0100 | [diff] [blame] | 35 | class CFITest : public dwarf::DwarfTest { |
| 36 | public: |
| 37 | void GenerateExpected(FILE* f, InstructionSet isa, const char* isa_str, |
| 38 | const std::vector<uint8_t>& actual_asm, |
| 39 | const std::vector<uint8_t>& actual_cfi) { |
| 40 | std::vector<std::string> lines; |
| 41 | // Print the raw bytes. |
| 42 | fprintf(f, "static constexpr uint8_t expected_asm_%s[] = {", isa_str); |
| 43 | HexDump(f, actual_asm); |
| 44 | fprintf(f, "\n};\n"); |
| 45 | fprintf(f, "static constexpr uint8_t expected_cfi_%s[] = {", isa_str); |
| 46 | HexDump(f, actual_cfi); |
| 47 | fprintf(f, "\n};\n"); |
| 48 | // Pretty-print CFI opcodes. |
David Srbecky | b536247 | 2015-04-08 19:37:39 +0100 | [diff] [blame] | 49 | constexpr bool is64bit = false; |
| 50 | dwarf::DebugFrameOpCodeWriter<> initial_opcodes; |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 51 | dwarf::WriteCIE(is64bit, dwarf::Reg(8), |
| 52 | initial_opcodes, kCFIFormat, &debug_frame_data_); |
David Srbecky | ad5fa8c | 2015-05-06 18:27:35 +0100 | [diff] [blame] | 53 | std::vector<uintptr_t> debug_frame_patches; |
David Srbecky | 6d8c8f0 | 2015-10-26 10:57:09 +0000 | [diff] [blame] | 54 | dwarf::WriteFDE(is64bit, 0, 0, 0, actual_asm.size(), ArrayRef<const uint8_t>(actual_cfi), |
| 55 | kCFIFormat, 0, &debug_frame_data_, &debug_frame_patches); |
David Srbecky | 1109fb3 | 2015-04-07 20:21:06 +0100 | [diff] [blame] | 56 | ReformatCfi(Objdump(false, "-W"), &lines); |
| 57 | // Pretty-print assembly. |
| 58 | auto* opts = new DisassemblerOptions(false, actual_asm.data(), true); |
| 59 | std::unique_ptr<Disassembler> disasm(Disassembler::Create(isa, opts)); |
| 60 | std::stringstream stream; |
| 61 | const uint8_t* base = actual_asm.data() + (isa == kThumb2 ? 1 : 0); |
| 62 | disasm->Dump(stream, base, base + actual_asm.size()); |
| 63 | ReformatAsm(&stream, &lines); |
| 64 | // Print CFI and assembly interleaved. |
| 65 | std::stable_sort(lines.begin(), lines.end(), CompareByAddress); |
| 66 | for (const std::string& line : lines) { |
| 67 | fprintf(f, "// %s\n", line.c_str()); |
| 68 | } |
| 69 | fprintf(f, "\n"); |
| 70 | } |
| 71 | |
| 72 | private: |
| 73 | // Helper - get offset just past the end of given string. |
| 74 | static size_t FindEndOf(const std::string& str, const char* substr) { |
| 75 | size_t pos = str.find(substr); |
| 76 | CHECK_NE(std::string::npos, pos); |
| 77 | return pos + strlen(substr); |
| 78 | } |
| 79 | |
| 80 | // Spit to lines and remove raw instruction bytes. |
| 81 | static void ReformatAsm(std::stringstream* stream, |
| 82 | std::vector<std::string>* output) { |
| 83 | std::string line; |
| 84 | while (std::getline(*stream, line)) { |
| 85 | line = line.substr(0, FindEndOf(line, ": ")) + |
| 86 | line.substr(FindEndOf(line, "\t")); |
| 87 | size_t pos; |
| 88 | while ((pos = line.find(" ")) != std::string::npos) { |
| 89 | line = line.replace(pos, 2, " "); |
| 90 | } |
| 91 | while (!line.empty() && line.back() == ' ') { |
| 92 | line.pop_back(); |
| 93 | } |
| 94 | output->push_back(line); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // Find interesting parts of objdump output and prefix the lines with address. |
| 99 | static void ReformatCfi(const std::vector<std::string>& lines, |
| 100 | std::vector<std::string>* output) { |
| 101 | std::string address; |
| 102 | for (const std::string& line : lines) { |
| 103 | if (line.find("DW_CFA_nop") != std::string::npos) { |
| 104 | // Ignore. |
| 105 | } else if (line.find("DW_CFA_advance_loc") != std::string::npos) { |
| 106 | // The last 8 characters are the address. |
| 107 | address = "0x" + line.substr(line.size() - 8); |
| 108 | } else if (line.find("DW_CFA_") != std::string::npos) { |
| 109 | std::string new_line(line); |
| 110 | // "bad register" warning is caused by always using host (x86) objdump. |
| 111 | const char* bad_reg = "bad register: "; |
| 112 | size_t pos; |
| 113 | if ((pos = new_line.find(bad_reg)) != std::string::npos) { |
| 114 | new_line = new_line.replace(pos, strlen(bad_reg), ""); |
| 115 | } |
| 116 | // Remove register names in parentheses since they have x86 names. |
| 117 | if ((pos = new_line.find(" (")) != std::string::npos) { |
| 118 | new_line = new_line.replace(pos, FindEndOf(new_line, ")") - pos, ""); |
| 119 | } |
| 120 | // Use the .cfi_ prefix. |
| 121 | new_line = ".cfi_" + new_line.substr(FindEndOf(new_line, "DW_CFA_")); |
| 122 | output->push_back(address + ": " + new_line); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | // Compare strings by the address prefix. |
| 128 | static bool CompareByAddress(const std::string& lhs, const std::string& rhs) { |
| 129 | EXPECT_EQ(lhs[10], ':'); |
| 130 | EXPECT_EQ(rhs[10], ':'); |
| 131 | return strncmp(lhs.c_str(), rhs.c_str(), 10) < 0; |
| 132 | } |
| 133 | |
| 134 | // Pretty-print byte array. 12 bytes per line. |
| 135 | static void HexDump(FILE* f, const std::vector<uint8_t>& data) { |
| 136 | for (size_t i = 0; i < data.size(); i++) { |
| 137 | fprintf(f, i % 12 == 0 ? "\n " : " "); // Whitespace. |
| 138 | fprintf(f, "0x%02X,", data[i]); |
| 139 | } |
| 140 | } |
| 141 | }; |
| 142 | |
| 143 | } // namespace art |
| 144 | |
| 145 | #endif // ART_COMPILER_CFI_TEST_H_ |