blob: 9755ef12d0188f78736546b274a32e19bc9e58a1 [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
David Srbecky1109fb32015-04-07 20:21:06 +010020#include <memory>
21#include <sstream>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070022#include <vector>
David Srbecky1109fb32015-04-07 20:21:06 +010023
24#include "arch/instruction_set.h"
Andreas Gampe372f3a32016-08-19 10:49:06 -070025#include "base/enums.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000026#include "debug/dwarf/dwarf_test.h"
Andreas Gampe2c30e4a2017-08-23 11:31:32 -070027#include "disassembler.h"
David Srbecky2faab002019-02-12 16:35:48 +000028#include "dwarf/dwarf_constants.h"
29#include "dwarf/headers.h"
David Srbecky1109fb32015-04-07 20:21:06 +010030#include "gtest/gtest.h"
Andreas Gampe372f3a32016-08-19 10:49:06 -070031#include "thread.h"
David Srbecky1109fb32015-04-07 20:21:06 +010032
33namespace art {
34
35class CFITest : public dwarf::DwarfTest {
36 public:
37 void GenerateExpected(FILE* f, InstructionSet isa, const char* isa_str,
Vladimir Markoca1e0382018-04-11 09:58:41 +000038 ArrayRef<const uint8_t> actual_asm,
39 ArrayRef<const uint8_t> actual_cfi) {
David Srbecky1109fb32015-04-07 20:21:06 +010040 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 Srbecky91b29002019-02-08 15:51:31 +000051 dwarf::WriteCIE(is64bit, dwarf::Reg(8), initial_opcodes, &debug_frame_data_);
David Srbeckyad5fa8c2015-05-06 18:27:35 +010052 std::vector<uintptr_t> debug_frame_patches;
Vladimir Markoca1e0382018-04-11 09:58:41 +000053 dwarf::WriteFDE(is64bit,
David Srbecky7370d922019-02-12 14:00:30 +000054 /* cie_pointer= */ 0,
Andreas Gampe3db70682018-12-26 15:12:03 -080055 /* code_address= */ 0,
Vladimir Markoca1e0382018-04-11 09:58:41 +000056 actual_asm.size(),
57 actual_cfi,
David Srbecky7370d922019-02-12 14:00:30 +000058 &debug_frame_data_);
David Srbecky1109fb32015-04-07 20:21:06 +010059 ReformatCfi(Objdump(false, "-W"), &lines);
60 // Pretty-print assembly.
Aart Bikd3059e72016-05-11 10:30:47 -070061 const uint8_t* asm_base = actual_asm.data();
62 const uint8_t* asm_end = asm_base + actual_asm.size();
Andreas Gampe372f3a32016-08-19 10:49:06 -070063 auto* opts = new DisassemblerOptions(false,
64 asm_base,
65 asm_end,
66 true,
67 is64bit
68 ? &Thread::DumpThreadOffset<PointerSize::k64>
69 : &Thread::DumpThreadOffset<PointerSize::k32>);
David Srbecky1109fb32015-04-07 20:21:06 +010070 std::unique_ptr<Disassembler> disasm(Disassembler::Create(isa, opts));
71 std::stringstream stream;
Vladimir Marko33bff252017-11-01 14:35:42 +000072 const uint8_t* base = actual_asm.data() + (isa == InstructionSet::kThumb2 ? 1 : 0);
David Srbecky1109fb32015-04-07 20:21:06 +010073 disasm->Dump(stream, base, base + actual_asm.size());
74 ReformatAsm(&stream, &lines);
75 // Print CFI and assembly interleaved.
76 std::stable_sort(lines.begin(), lines.end(), CompareByAddress);
77 for (const std::string& line : lines) {
78 fprintf(f, "// %s\n", line.c_str());
79 }
80 fprintf(f, "\n");
81 }
82
83 private:
84 // Helper - get offset just past the end of given string.
85 static size_t FindEndOf(const std::string& str, const char* substr) {
86 size_t pos = str.find(substr);
87 CHECK_NE(std::string::npos, pos);
88 return pos + strlen(substr);
89 }
90
91 // Spit to lines and remove raw instruction bytes.
92 static void ReformatAsm(std::stringstream* stream,
93 std::vector<std::string>* output) {
94 std::string line;
95 while (std::getline(*stream, line)) {
96 line = line.substr(0, FindEndOf(line, ": ")) +
97 line.substr(FindEndOf(line, "\t"));
98 size_t pos;
99 while ((pos = line.find(" ")) != std::string::npos) {
100 line = line.replace(pos, 2, " ");
101 }
102 while (!line.empty() && line.back() == ' ') {
103 line.pop_back();
104 }
105 output->push_back(line);
106 }
107 }
108
109 // Find interesting parts of objdump output and prefix the lines with address.
110 static void ReformatCfi(const std::vector<std::string>& lines,
111 std::vector<std::string>* output) {
112 std::string address;
113 for (const std::string& line : lines) {
114 if (line.find("DW_CFA_nop") != std::string::npos) {
115 // Ignore.
116 } else if (line.find("DW_CFA_advance_loc") != std::string::npos) {
117 // The last 8 characters are the address.
118 address = "0x" + line.substr(line.size() - 8);
119 } else if (line.find("DW_CFA_") != std::string::npos) {
120 std::string new_line(line);
121 // "bad register" warning is caused by always using host (x86) objdump.
122 const char* bad_reg = "bad register: ";
123 size_t pos;
124 if ((pos = new_line.find(bad_reg)) != std::string::npos) {
125 new_line = new_line.replace(pos, strlen(bad_reg), "");
126 }
127 // Remove register names in parentheses since they have x86 names.
128 if ((pos = new_line.find(" (")) != std::string::npos) {
129 new_line = new_line.replace(pos, FindEndOf(new_line, ")") - pos, "");
130 }
131 // Use the .cfi_ prefix.
132 new_line = ".cfi_" + new_line.substr(FindEndOf(new_line, "DW_CFA_"));
133 output->push_back(address + ": " + new_line);
134 }
135 }
136 }
137
138 // Compare strings by the address prefix.
139 static bool CompareByAddress(const std::string& lhs, const std::string& rhs) {
140 EXPECT_EQ(lhs[10], ':');
141 EXPECT_EQ(rhs[10], ':');
142 return strncmp(lhs.c_str(), rhs.c_str(), 10) < 0;
143 }
144
145 // Pretty-print byte array. 12 bytes per line.
Vladimir Markoca1e0382018-04-11 09:58:41 +0000146 static void HexDump(FILE* f, ArrayRef<const uint8_t> data) {
David Srbecky1109fb32015-04-07 20:21:06 +0100147 for (size_t i = 0; i < data.size(); i++) {
148 fprintf(f, i % 12 == 0 ? "\n " : " "); // Whitespace.
149 fprintf(f, "0x%02X,", data[i]);
150 }
151 }
152};
153
154} // namespace art
155
156#endif // ART_COMPILER_CFI_TEST_H_