blob: 658bdb35ae886e310010ad2a428d4d5b56e8b181 [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,
Vladimir Markoca1e0382018-04-11 09:58:41 +000040 ArrayRef<const uint8_t> actual_asm,
41 ArrayRef<const uint8_t> actual_cfi) {
David Srbecky1109fb32015-04-07 20:21:06 +010042 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;
Vladimir Markoca1e0382018-04-11 09:58:41 +000053 dwarf::WriteCIE(is64bit, dwarf::Reg(8), initial_opcodes, kCFIFormat, &debug_frame_data_);
David Srbeckyad5fa8c2015-05-06 18:27:35 +010054 std::vector<uintptr_t> debug_frame_patches;
Vladimir Markoca1e0382018-04-11 09:58:41 +000055 dwarf::WriteFDE(is64bit,
Andreas Gampe3db70682018-12-26 15:12:03 -080056 /* section_address= */ 0,
57 /* cie_address= */ 0,
58 /* code_address= */ 0,
Vladimir Markoca1e0382018-04-11 09:58:41 +000059 actual_asm.size(),
60 actual_cfi,
61 kCFIFormat,
Andreas Gampe3db70682018-12-26 15:12:03 -080062 /* buffer_address= */ 0,
Vladimir Markoca1e0382018-04-11 09:58:41 +000063 &debug_frame_data_,
64 &debug_frame_patches);
David Srbecky1109fb32015-04-07 20:21:06 +010065 ReformatCfi(Objdump(false, "-W"), &lines);
66 // Pretty-print assembly.
Aart Bikd3059e72016-05-11 10:30:47 -070067 const uint8_t* asm_base = actual_asm.data();
68 const uint8_t* asm_end = asm_base + actual_asm.size();
Andreas Gampe372f3a32016-08-19 10:49:06 -070069 auto* opts = new DisassemblerOptions(false,
70 asm_base,
71 asm_end,
72 true,
73 is64bit
74 ? &Thread::DumpThreadOffset<PointerSize::k64>
75 : &Thread::DumpThreadOffset<PointerSize::k32>);
David Srbecky1109fb32015-04-07 20:21:06 +010076 std::unique_ptr<Disassembler> disasm(Disassembler::Create(isa, opts));
77 std::stringstream stream;
Vladimir Marko33bff252017-11-01 14:35:42 +000078 const uint8_t* base = actual_asm.data() + (isa == InstructionSet::kThumb2 ? 1 : 0);
David Srbecky1109fb32015-04-07 20:21:06 +010079 disasm->Dump(stream, base, base + actual_asm.size());
80 ReformatAsm(&stream, &lines);
81 // Print CFI and assembly interleaved.
82 std::stable_sort(lines.begin(), lines.end(), CompareByAddress);
83 for (const std::string& line : lines) {
84 fprintf(f, "// %s\n", line.c_str());
85 }
86 fprintf(f, "\n");
87 }
88
89 private:
90 // Helper - get offset just past the end of given string.
91 static size_t FindEndOf(const std::string& str, const char* substr) {
92 size_t pos = str.find(substr);
93 CHECK_NE(std::string::npos, pos);
94 return pos + strlen(substr);
95 }
96
97 // Spit to lines and remove raw instruction bytes.
98 static void ReformatAsm(std::stringstream* stream,
99 std::vector<std::string>* output) {
100 std::string line;
101 while (std::getline(*stream, line)) {
102 line = line.substr(0, FindEndOf(line, ": ")) +
103 line.substr(FindEndOf(line, "\t"));
104 size_t pos;
105 while ((pos = line.find(" ")) != std::string::npos) {
106 line = line.replace(pos, 2, " ");
107 }
108 while (!line.empty() && line.back() == ' ') {
109 line.pop_back();
110 }
111 output->push_back(line);
112 }
113 }
114
115 // Find interesting parts of objdump output and prefix the lines with address.
116 static void ReformatCfi(const std::vector<std::string>& lines,
117 std::vector<std::string>* output) {
118 std::string address;
119 for (const std::string& line : lines) {
120 if (line.find("DW_CFA_nop") != std::string::npos) {
121 // Ignore.
122 } else if (line.find("DW_CFA_advance_loc") != std::string::npos) {
123 // The last 8 characters are the address.
124 address = "0x" + line.substr(line.size() - 8);
125 } else if (line.find("DW_CFA_") != std::string::npos) {
126 std::string new_line(line);
127 // "bad register" warning is caused by always using host (x86) objdump.
128 const char* bad_reg = "bad register: ";
129 size_t pos;
130 if ((pos = new_line.find(bad_reg)) != std::string::npos) {
131 new_line = new_line.replace(pos, strlen(bad_reg), "");
132 }
133 // Remove register names in parentheses since they have x86 names.
134 if ((pos = new_line.find(" (")) != std::string::npos) {
135 new_line = new_line.replace(pos, FindEndOf(new_line, ")") - pos, "");
136 }
137 // Use the .cfi_ prefix.
138 new_line = ".cfi_" + new_line.substr(FindEndOf(new_line, "DW_CFA_"));
139 output->push_back(address + ": " + new_line);
140 }
141 }
142 }
143
144 // Compare strings by the address prefix.
145 static bool CompareByAddress(const std::string& lhs, const std::string& rhs) {
146 EXPECT_EQ(lhs[10], ':');
147 EXPECT_EQ(rhs[10], ':');
148 return strncmp(lhs.c_str(), rhs.c_str(), 10) < 0;
149 }
150
151 // Pretty-print byte array. 12 bytes per line.
Vladimir Markoca1e0382018-04-11 09:58:41 +0000152 static void HexDump(FILE* f, ArrayRef<const uint8_t> data) {
David Srbecky1109fb32015-04-07 20:21:06 +0100153 for (size_t i = 0; i < data.size(); i++) {
154 fprintf(f, i % 12 == 0 ? "\n " : " "); // Whitespace.
155 fprintf(f, "0x%02X,", data[i]);
156 }
157 }
158};
159
160} // namespace art
161
162#endif // ART_COMPILER_CFI_TEST_H_