blob: 29ff235cea70a8a3260c350e4406b386a01f7d23 [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_constants.h"
27#include "debug/dwarf/dwarf_test.h"
28#include "debug/dwarf/headers.h"
Andreas Gampe2c30e4a2017-08-23 11:31:32 -070029#include "disassembler.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
David Srbeckyad5fa8c2015-05-06 18:27:35 +010035constexpr dwarf::CFIFormat kCFIFormat = dwarf::DW_DEBUG_FRAME_FORMAT;
36
David Srbecky1109fb32015-04-07 20:21:06 +010037class CFITest : public dwarf::DwarfTest {
38 public:
39 void GenerateExpected(FILE* f, InstructionSet isa, const char* isa_str,
40 const std::vector<uint8_t>& actual_asm,
41 const std::vector<uint8_t>& actual_cfi) {
42 std::vector<std::string> lines;
43 // Print the raw bytes.
44 fprintf(f, "static constexpr uint8_t expected_asm_%s[] = {", isa_str);
45 HexDump(f, actual_asm);
46 fprintf(f, "\n};\n");
47 fprintf(f, "static constexpr uint8_t expected_cfi_%s[] = {", isa_str);
48 HexDump(f, actual_cfi);
49 fprintf(f, "\n};\n");
50 // Pretty-print CFI opcodes.
David Srbeckyb5362472015-04-08 19:37:39 +010051 constexpr bool is64bit = false;
52 dwarf::DebugFrameOpCodeWriter<> initial_opcodes;
David Srbecky6d8c8f02015-10-26 10:57:09 +000053 dwarf::WriteCIE(is64bit, dwarf::Reg(8),
54 initial_opcodes, kCFIFormat, &debug_frame_data_);
David Srbeckyad5fa8c2015-05-06 18:27:35 +010055 std::vector<uintptr_t> debug_frame_patches;
David Srbecky6d8c8f02015-10-26 10:57:09 +000056 dwarf::WriteFDE(is64bit, 0, 0, 0, actual_asm.size(), ArrayRef<const uint8_t>(actual_cfi),
57 kCFIFormat, 0, &debug_frame_data_, &debug_frame_patches);
David Srbecky1109fb32015-04-07 20:21:06 +010058 ReformatCfi(Objdump(false, "-W"), &lines);
59 // Pretty-print assembly.
Aart Bikd3059e72016-05-11 10:30:47 -070060 const uint8_t* asm_base = actual_asm.data();
61 const uint8_t* asm_end = asm_base + actual_asm.size();
Andreas Gampe372f3a32016-08-19 10:49:06 -070062 auto* opts = new DisassemblerOptions(false,
63 asm_base,
64 asm_end,
65 true,
66 is64bit
67 ? &Thread::DumpThreadOffset<PointerSize::k64>
68 : &Thread::DumpThreadOffset<PointerSize::k32>);
David Srbecky1109fb32015-04-07 20:21:06 +010069 std::unique_ptr<Disassembler> disasm(Disassembler::Create(isa, opts));
70 std::stringstream stream;
Vladimir Marko33bff252017-11-01 14:35:42 +000071 const uint8_t* base = actual_asm.data() + (isa == InstructionSet::kThumb2 ? 1 : 0);
David Srbecky1109fb32015-04-07 20:21:06 +010072 disasm->Dump(stream, base, base + actual_asm.size());
73 ReformatAsm(&stream, &lines);
74 // Print CFI and assembly interleaved.
75 std::stable_sort(lines.begin(), lines.end(), CompareByAddress);
76 for (const std::string& line : lines) {
77 fprintf(f, "// %s\n", line.c_str());
78 }
79 fprintf(f, "\n");
80 }
81
82 private:
83 // Helper - get offset just past the end of given string.
84 static size_t FindEndOf(const std::string& str, const char* substr) {
85 size_t pos = str.find(substr);
86 CHECK_NE(std::string::npos, pos);
87 return pos + strlen(substr);
88 }
89
90 // Spit to lines and remove raw instruction bytes.
91 static void ReformatAsm(std::stringstream* stream,
92 std::vector<std::string>* output) {
93 std::string line;
94 while (std::getline(*stream, line)) {
95 line = line.substr(0, FindEndOf(line, ": ")) +
96 line.substr(FindEndOf(line, "\t"));
97 size_t pos;
98 while ((pos = line.find(" ")) != std::string::npos) {
99 line = line.replace(pos, 2, " ");
100 }
101 while (!line.empty() && line.back() == ' ') {
102 line.pop_back();
103 }
104 output->push_back(line);
105 }
106 }
107
108 // Find interesting parts of objdump output and prefix the lines with address.
109 static void ReformatCfi(const std::vector<std::string>& lines,
110 std::vector<std::string>* output) {
111 std::string address;
112 for (const std::string& line : lines) {
113 if (line.find("DW_CFA_nop") != std::string::npos) {
114 // Ignore.
115 } else if (line.find("DW_CFA_advance_loc") != std::string::npos) {
116 // The last 8 characters are the address.
117 address = "0x" + line.substr(line.size() - 8);
118 } else if (line.find("DW_CFA_") != std::string::npos) {
119 std::string new_line(line);
120 // "bad register" warning is caused by always using host (x86) objdump.
121 const char* bad_reg = "bad register: ";
122 size_t pos;
123 if ((pos = new_line.find(bad_reg)) != std::string::npos) {
124 new_line = new_line.replace(pos, strlen(bad_reg), "");
125 }
126 // Remove register names in parentheses since they have x86 names.
127 if ((pos = new_line.find(" (")) != std::string::npos) {
128 new_line = new_line.replace(pos, FindEndOf(new_line, ")") - pos, "");
129 }
130 // Use the .cfi_ prefix.
131 new_line = ".cfi_" + new_line.substr(FindEndOf(new_line, "DW_CFA_"));
132 output->push_back(address + ": " + new_line);
133 }
134 }
135 }
136
137 // Compare strings by the address prefix.
138 static bool CompareByAddress(const std::string& lhs, const std::string& rhs) {
139 EXPECT_EQ(lhs[10], ':');
140 EXPECT_EQ(rhs[10], ':');
141 return strncmp(lhs.c_str(), rhs.c_str(), 10) < 0;
142 }
143
144 // Pretty-print byte array. 12 bytes per line.
145 static void HexDump(FILE* f, const std::vector<uint8_t>& data) {
146 for (size_t i = 0; i < data.size(); i++) {
147 fprintf(f, i % 12 == 0 ? "\n " : " "); // Whitespace.
148 fprintf(f, "0x%02X,", data[i]);
149 }
150 }
151};
152
153} // namespace art
154
155#endif // ART_COMPILER_CFI_TEST_H_