blob: 508b04a16feeb7e49ccdea071e5ce24b46e41f0c [file] [log] [blame]
David Srbecky1109fb32015-04-07 20:21:06 +01001/*
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 Srbecky527c9c72015-04-17 21:14:10 +010025#include "dwarf/dwarf_constants.h"
David Srbecky1109fb32015-04-07 20:21:06 +010026#include "dwarf/dwarf_test.h"
David Srbeckyb5362472015-04-08 19:37:39 +010027#include "dwarf/headers.h"
David Srbecky1109fb32015-04-07 20:21:06 +010028#include "disassembler/disassembler.h"
29#include "gtest/gtest.h"
30
31namespace art {
32
David Srbeckyad5fa8c2015-05-06 18:27:35 +010033constexpr dwarf::CFIFormat kCFIFormat = dwarf::DW_DEBUG_FRAME_FORMAT;
34
David Srbecky1109fb32015-04-07 20:21:06 +010035class 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 Srbeckyb5362472015-04-08 19:37:39 +010049 constexpr bool is64bit = false;
50 dwarf::DebugFrameOpCodeWriter<> initial_opcodes;
David Srbecky6d8c8f02015-10-26 10:57:09 +000051 dwarf::WriteCIE(is64bit, dwarf::Reg(8),
52 initial_opcodes, kCFIFormat, &debug_frame_data_);
David Srbeckyad5fa8c2015-05-06 18:27:35 +010053 std::vector<uintptr_t> debug_frame_patches;
David Srbecky6d8c8f02015-10-26 10:57:09 +000054 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 Srbecky1109fb32015-04-07 20:21:06 +010056 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_