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