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. |
Aart Bik | a6e95b3 | 2016-05-11 10:30:47 -0700 | [diff] [blame] | 58 | const uint8_t* asm_base = actual_asm.data(); |
| 59 | const uint8_t* asm_end = asm_base + actual_asm.size(); |
| 60 | auto* opts = new DisassemblerOptions(false, asm_base, asm_end, true); |
David Srbecky | 1109fb3 | 2015-04-07 20:21:06 +0100 | [diff] [blame] | 61 | std::unique_ptr<Disassembler> disasm(Disassembler::Create(isa, opts)); |
| 62 | std::stringstream stream; |
| 63 | const uint8_t* base = actual_asm.data() + (isa == kThumb2 ? 1 : 0); |
| 64 | disasm->Dump(stream, base, base + actual_asm.size()); |
| 65 | ReformatAsm(&stream, &lines); |
| 66 | // Print CFI and assembly interleaved. |
| 67 | std::stable_sort(lines.begin(), lines.end(), CompareByAddress); |
| 68 | for (const std::string& line : lines) { |
| 69 | fprintf(f, "// %s\n", line.c_str()); |
| 70 | } |
| 71 | fprintf(f, "\n"); |
| 72 | } |
| 73 | |
| 74 | private: |
| 75 | // Helper - get offset just past the end of given string. |
| 76 | static size_t FindEndOf(const std::string& str, const char* substr) { |
| 77 | size_t pos = str.find(substr); |
| 78 | CHECK_NE(std::string::npos, pos); |
| 79 | return pos + strlen(substr); |
| 80 | } |
| 81 | |
| 82 | // Spit to lines and remove raw instruction bytes. |
| 83 | static void ReformatAsm(std::stringstream* stream, |
| 84 | std::vector<std::string>* output) { |
| 85 | std::string line; |
| 86 | while (std::getline(*stream, line)) { |
| 87 | line = line.substr(0, FindEndOf(line, ": ")) + |
| 88 | line.substr(FindEndOf(line, "\t")); |
| 89 | size_t pos; |
| 90 | while ((pos = line.find(" ")) != std::string::npos) { |
| 91 | line = line.replace(pos, 2, " "); |
| 92 | } |
| 93 | while (!line.empty() && line.back() == ' ') { |
| 94 | line.pop_back(); |
| 95 | } |
| 96 | output->push_back(line); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // Find interesting parts of objdump output and prefix the lines with address. |
| 101 | static void ReformatCfi(const std::vector<std::string>& lines, |
| 102 | std::vector<std::string>* output) { |
| 103 | std::string address; |
| 104 | for (const std::string& line : lines) { |
| 105 | if (line.find("DW_CFA_nop") != std::string::npos) { |
| 106 | // Ignore. |
| 107 | } else if (line.find("DW_CFA_advance_loc") != std::string::npos) { |
| 108 | // The last 8 characters are the address. |
| 109 | address = "0x" + line.substr(line.size() - 8); |
| 110 | } else if (line.find("DW_CFA_") != std::string::npos) { |
| 111 | std::string new_line(line); |
| 112 | // "bad register" warning is caused by always using host (x86) objdump. |
| 113 | const char* bad_reg = "bad register: "; |
| 114 | size_t pos; |
| 115 | if ((pos = new_line.find(bad_reg)) != std::string::npos) { |
| 116 | new_line = new_line.replace(pos, strlen(bad_reg), ""); |
| 117 | } |
| 118 | // Remove register names in parentheses since they have x86 names. |
| 119 | if ((pos = new_line.find(" (")) != std::string::npos) { |
| 120 | new_line = new_line.replace(pos, FindEndOf(new_line, ")") - pos, ""); |
| 121 | } |
| 122 | // Use the .cfi_ prefix. |
| 123 | new_line = ".cfi_" + new_line.substr(FindEndOf(new_line, "DW_CFA_")); |
| 124 | output->push_back(address + ": " + new_line); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | // Compare strings by the address prefix. |
| 130 | static bool CompareByAddress(const std::string& lhs, const std::string& rhs) { |
| 131 | EXPECT_EQ(lhs[10], ':'); |
| 132 | EXPECT_EQ(rhs[10], ':'); |
| 133 | return strncmp(lhs.c_str(), rhs.c_str(), 10) < 0; |
| 134 | } |
| 135 | |
| 136 | // Pretty-print byte array. 12 bytes per line. |
| 137 | static void HexDump(FILE* f, const std::vector<uint8_t>& data) { |
| 138 | for (size_t i = 0; i < data.size(); i++) { |
| 139 | fprintf(f, i % 12 == 0 ? "\n " : " "); // Whitespace. |
| 140 | fprintf(f, "0x%02X,", data[i]); |
| 141 | } |
| 142 | } |
| 143 | }; |
| 144 | |
| 145 | } // namespace art |
| 146 | |
| 147 | #endif // ART_COMPILER_CFI_TEST_H_ |