blob: fa12d7e5083d5f2c7c4fe2bd78fa5dc75ff182cd [file] [log] [blame]
David Srbecky15c19752015-03-31 14:53:55 +00001/*
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#include "dwarf_test.h"
18
19#include "dwarf/debug_frame_opcode_writer.h"
David Srbeckyb5362472015-04-08 19:37:39 +010020#include "dwarf/debug_info_entry_writer.h"
David Srbecky15c19752015-03-31 14:53:55 +000021#include "dwarf/debug_line_opcode_writer.h"
David Srbeckyb5362472015-04-08 19:37:39 +010022#include "dwarf/headers.h"
David Srbecky15c19752015-03-31 14:53:55 +000023#include "gtest/gtest.h"
24
25namespace art {
26namespace dwarf {
27
28// Run the tests only on host since we need objdump.
29#ifndef HAVE_ANDROID_OS
30
31TEST_F(DwarfTest, DebugFrame) {
32 const bool is64bit = false;
33
34 // Pick offset value which would catch Uleb vs Sleb errors.
35 const int offset = 40000;
36 ASSERT_EQ(UnsignedLeb128Size(offset / 4), 2u);
37 ASSERT_EQ(SignedLeb128Size(offset / 4), 3u);
38 DW_CHECK("Data alignment factor: -4");
39 const Reg reg(6);
40
41 // Test the opcodes in the order mentioned in the spec.
42 // There are usually several encoding variations of each opcode.
43 DebugFrameOpCodeWriter<> opcodes;
44 DW_CHECK("FDE");
45 int pc = 0;
46 for (int i : {0, 1, 0x3F, 0x40, 0xFF, 0x100, 0xFFFF, 0x10000}) {
47 pc += i;
48 opcodes.AdvancePC(pc);
49 }
50 DW_CHECK_NEXT("DW_CFA_advance_loc: 1 to 01000001");
51 DW_CHECK_NEXT("DW_CFA_advance_loc: 63 to 01000040");
52 DW_CHECK_NEXT("DW_CFA_advance_loc1: 64 to 01000080");
53 DW_CHECK_NEXT("DW_CFA_advance_loc1: 255 to 0100017f");
54 DW_CHECK_NEXT("DW_CFA_advance_loc2: 256 to 0100027f");
55 DW_CHECK_NEXT("DW_CFA_advance_loc2: 65535 to 0101027e");
56 DW_CHECK_NEXT("DW_CFA_advance_loc4: 65536 to 0102027e");
57 opcodes.DefCFA(reg, offset);
58 DW_CHECK_NEXT("DW_CFA_def_cfa: r6 (esi) ofs 40000");
59 opcodes.DefCFA(reg, -offset);
60 DW_CHECK_NEXT("DW_CFA_def_cfa_sf: r6 (esi) ofs -40000");
61 opcodes.DefCFARegister(reg);
62 DW_CHECK_NEXT("DW_CFA_def_cfa_register: r6 (esi)");
63 opcodes.DefCFAOffset(offset);
64 DW_CHECK_NEXT("DW_CFA_def_cfa_offset: 40000");
65 opcodes.DefCFAOffset(-offset);
66 DW_CHECK_NEXT("DW_CFA_def_cfa_offset_sf: -40000");
67 uint8_t expr[] = { 0 };
68 opcodes.DefCFAExpression(expr, arraysize(expr));
69 DW_CHECK_NEXT("DW_CFA_def_cfa_expression");
70 opcodes.Undefined(reg);
71 DW_CHECK_NEXT("DW_CFA_undefined: r6 (esi)");
72 opcodes.SameValue(reg);
73 DW_CHECK_NEXT("DW_CFA_same_value: r6 (esi)");
74 opcodes.Offset(Reg(0x3F), -offset);
75 // Bad register likely means that it does not exist on x86,
76 // but we want to test high register numbers anyway.
77 DW_CHECK_NEXT("DW_CFA_offset: bad register: r63 at cfa-40000");
78 opcodes.Offset(Reg(0x40), -offset);
79 DW_CHECK_NEXT("DW_CFA_offset_extended: bad register: r64 at cfa-40000");
80 opcodes.Offset(Reg(0x40), offset);
81 DW_CHECK_NEXT("DW_CFA_offset_extended_sf: bad register: r64 at cfa+40000");
82 opcodes.ValOffset(reg, -offset);
83 DW_CHECK_NEXT("DW_CFA_val_offset: r6 (esi) at cfa-40000");
84 opcodes.ValOffset(reg, offset);
85 DW_CHECK_NEXT("DW_CFA_val_offset_sf: r6 (esi) at cfa+40000");
86 opcodes.Register(reg, Reg(1));
87 DW_CHECK_NEXT("DW_CFA_register: r6 (esi) in r1 (ecx)");
88 opcodes.Expression(reg, expr, arraysize(expr));
89 DW_CHECK_NEXT("DW_CFA_expression: r6 (esi)");
90 opcodes.ValExpression(reg, expr, arraysize(expr));
91 DW_CHECK_NEXT("DW_CFA_val_expression: r6 (esi)");
92 opcodes.Restore(Reg(0x3F));
93 DW_CHECK_NEXT("DW_CFA_restore: bad register: r63");
94 opcodes.Restore(Reg(0x40));
95 DW_CHECK_NEXT("DW_CFA_restore_extended: bad register: r64");
96 opcodes.Restore(reg);
97 DW_CHECK_NEXT("DW_CFA_restore: r6 (esi)");
98 opcodes.RememberState();
99 DW_CHECK_NEXT("DW_CFA_remember_state");
100 opcodes.RestoreState();
101 DW_CHECK_NEXT("DW_CFA_restore_state");
102 opcodes.Nop();
103 DW_CHECK_NEXT("DW_CFA_nop");
104
105 // Also test helpers.
106 opcodes.DefCFA(Reg(4), 100); // ESP
107 DW_CHECK_NEXT("DW_CFA_def_cfa: r4 (esp) ofs 100");
108 opcodes.AdjustCFAOffset(8);
109 DW_CHECK_NEXT("DW_CFA_def_cfa_offset: 108");
110 opcodes.RelOffset(Reg(0), 0); // push R0
111 DW_CHECK_NEXT("DW_CFA_offset: r0 (eax) at cfa-108");
112 opcodes.RelOffset(Reg(1), 4); // push R1
113 DW_CHECK_NEXT("DW_CFA_offset: r1 (ecx) at cfa-104");
114 opcodes.RelOffsetForMany(Reg(2), 8, 1 | (1 << 3), 4); // push R2 and R5
115 DW_CHECK_NEXT("DW_CFA_offset: r2 (edx) at cfa-100");
116 DW_CHECK_NEXT("DW_CFA_offset: r5 (ebp) at cfa-96");
117 opcodes.RestoreMany(Reg(2), 1 | (1 << 3)); // pop R2 and R5
118 DW_CHECK_NEXT("DW_CFA_restore: r2 (edx)");
119 DW_CHECK_NEXT("DW_CFA_restore: r5 (ebp)");
120
David Srbecky15c19752015-03-31 14:53:55 +0000121 DebugFrameOpCodeWriter<> initial_opcodes;
David Srbeckyb5362472015-04-08 19:37:39 +0100122 WriteEhFrameCIE(is64bit, Reg(is64bit ? 16 : 8), initial_opcodes, &eh_frame_data_);
David Srbecky2f6cdb02015-04-11 00:17:53 +0100123 std::vector<uintptr_t> eh_frame_patches;
124 std::vector<uintptr_t> expected_patches { 28 }; // NOLINT
125 WriteEhFrameFDE(is64bit, 0, 0x01000000, 0x01000000, opcodes.data(),
126 &eh_frame_data_, &eh_frame_patches);
127
128 EXPECT_EQ(expected_patches, eh_frame_patches);
David Srbecky15c19752015-03-31 14:53:55 +0000129 CheckObjdumpOutput(is64bit, "-W");
130}
131
David Srbecky2f6cdb02015-04-11 00:17:53 +0100132TEST_F(DwarfTest, DebugFrame64) {
David Srbeckyb5362472015-04-08 19:37:39 +0100133 constexpr bool is64bit = true;
134 DebugFrameOpCodeWriter<> initial_opcodes;
135 WriteEhFrameCIE(is64bit, Reg(16), initial_opcodes, &eh_frame_data_);
136 DebugFrameOpCodeWriter<> opcodes;
David Srbecky2f6cdb02015-04-11 00:17:53 +0100137 std::vector<uintptr_t> eh_frame_patches;
138 std::vector<uintptr_t> expected_patches { 32 }; // NOLINT
David Srbeckyb5362472015-04-08 19:37:39 +0100139 WriteEhFrameFDE(is64bit, 0, 0x0100000000000000, 0x0200000000000000,
David Srbecky2f6cdb02015-04-11 00:17:53 +0100140 opcodes.data(), &eh_frame_data_, &eh_frame_patches);
David Srbecky15c19752015-03-31 14:53:55 +0000141 DW_CHECK("FDE cie=00000000 pc=100000000000000..300000000000000");
David Srbecky2f6cdb02015-04-11 00:17:53 +0100142
143 EXPECT_EQ(expected_patches, eh_frame_patches);
David Srbecky15c19752015-03-31 14:53:55 +0000144 CheckObjdumpOutput(is64bit, "-W");
145}
146
147TEST_F(DwarfTest, DebugLine) {
148 const bool is64bit = false;
149 const int code_factor_bits = 1;
150 DebugLineOpCodeWriter<> opcodes(is64bit, code_factor_bits);
151
152 std::vector<std::string> include_directories;
153 include_directories.push_back("/path/to/source");
154 DW_CHECK("/path/to/source");
155
David Srbeckyb5362472015-04-08 19:37:39 +0100156 std::vector<FileEntry> files {
David Srbecky15c19752015-03-31 14:53:55 +0000157 { "file0.c", 0, 1000, 2000 },
158 { "file1.c", 1, 1000, 2000 },
159 { "file2.c", 1, 1000, 2000 },
160 };
161 DW_CHECK("1\t0\t1000\t2000\tfile0.c");
162 DW_CHECK_NEXT("2\t1\t1000\t2000\tfile1.c");
163 DW_CHECK_NEXT("3\t1\t1000\t2000\tfile2.c");
164
165 DW_CHECK("Line Number Statements");
166 opcodes.SetAddress(0x01000000);
167 DW_CHECK_NEXT("Extended opcode 2: set Address to 0x1000000");
168 opcodes.AddRow();
169 DW_CHECK_NEXT("Copy");
170 opcodes.AdvancePC(0x01000100);
171 DW_CHECK_NEXT("Advance PC by 256 to 0x1000100");
172 opcodes.SetFile(2);
173 DW_CHECK_NEXT("Set File Name to entry 2 in the File Name Table");
174 opcodes.AdvanceLine(3);
175 DW_CHECK_NEXT("Advance Line by 2 to 3");
176 opcodes.SetColumn(4);
177 DW_CHECK_NEXT("Set column to 4");
178 opcodes.NegateStmt();
179 DW_CHECK_NEXT("Set is_stmt to 0");
180 opcodes.SetBasicBlock();
181 DW_CHECK_NEXT("Set basic block");
182 opcodes.SetPrologueEnd();
183 DW_CHECK_NEXT("Set prologue_end to true");
184 opcodes.SetEpilogueBegin();
185 DW_CHECK_NEXT("Set epilogue_begin to true");
186 opcodes.SetISA(5);
187 DW_CHECK_NEXT("Set ISA to 5");
188 opcodes.EndSequence();
189 DW_CHECK_NEXT("Extended opcode 1: End of Sequence");
190 opcodes.DefineFile("file.c", 0, 1000, 2000);
191 DW_CHECK_NEXT("Extended opcode 3: define new File Table entry");
192 DW_CHECK_NEXT("Entry\tDir\tTime\tSize\tName");
193 DW_CHECK_NEXT("1\t0\t1000\t2000\tfile.c");
194
David Srbecky2f6cdb02015-04-11 00:17:53 +0100195 std::vector<uintptr_t> debug_line_patches;
196 std::vector<uintptr_t> expected_patches { 87 }; // NOLINT
197 WriteDebugLineTable(include_directories, files, opcodes,
198 &debug_line_data_, &debug_line_patches);
199
200 EXPECT_EQ(expected_patches, debug_line_patches);
David Srbecky15c19752015-03-31 14:53:55 +0000201 CheckObjdumpOutput(is64bit, "-W");
202}
203
204// DWARF has special one byte codes which advance PC and line at the same time.
205TEST_F(DwarfTest, DebugLineSpecialOpcodes) {
206 const bool is64bit = false;
207 const int code_factor_bits = 1;
208 uint32_t pc = 0x01000000;
209 int line = 1;
210 DebugLineOpCodeWriter<> opcodes(is64bit, code_factor_bits);
211 opcodes.SetAddress(pc);
212 size_t num_rows = 0;
213 DW_CHECK("Line Number Statements:");
214 DW_CHECK("Special opcode");
215 DW_CHECK("Advance PC by constant");
216 DW_CHECK("Decoded dump of debug contents of section .debug_line:");
217 DW_CHECK("Line number Starting address");
218 for (int addr_delta = 0; addr_delta < 80; addr_delta += 2) {
219 for (int line_delta = 16; line_delta >= -16; --line_delta) {
220 pc += addr_delta;
221 line += line_delta;
222 opcodes.AddRow(pc, line);
223 num_rows++;
224 ASSERT_EQ(opcodes.CurrentAddress(), pc);
225 ASSERT_EQ(opcodes.CurrentLine(), line);
226 char expected[1024];
227 sprintf(expected, "%i 0x%x", line, pc);
228 DW_CHECK_NEXT(expected);
229 }
230 }
231 EXPECT_LT(opcodes.data()->size(), num_rows * 3);
232
233 std::vector<std::string> directories;
David Srbeckyb5362472015-04-08 19:37:39 +0100234 std::vector<FileEntry> files { { "file.c", 0, 1000, 2000 } }; // NOLINT
David Srbecky2f6cdb02015-04-11 00:17:53 +0100235 std::vector<uintptr_t> debug_line_patches;
236 WriteDebugLineTable(directories, files, opcodes,
237 &debug_line_data_, &debug_line_patches);
238
David Srbecky15c19752015-03-31 14:53:55 +0000239 CheckObjdumpOutput(is64bit, "-W -WL");
240}
241
David Srbeckyb5362472015-04-08 19:37:39 +0100242TEST_F(DwarfTest, DebugInfo) {
243 constexpr bool is64bit = false;
244 DebugInfoEntryWriter<> info(is64bit, &debug_abbrev_data_);
245 DW_CHECK("Contents of the .debug_info section:");
246 info.StartTag(dwarf::DW_TAG_compile_unit, dwarf::DW_CHILDREN_yes);
247 DW_CHECK("Abbrev Number: 1 (DW_TAG_compile_unit)");
248 info.WriteStrp(dwarf::DW_AT_producer, "Compiler name", &debug_str_data_);
249 DW_CHECK_NEXT("DW_AT_producer : (indirect string, offset: 0x0): Compiler name");
250 info.WriteAddr(dwarf::DW_AT_low_pc, 0x01000000);
251 DW_CHECK_NEXT("DW_AT_low_pc : 0x1000000");
252 info.WriteAddr(dwarf::DW_AT_high_pc, 0x02000000);
253 DW_CHECK_NEXT("DW_AT_high_pc : 0x2000000");
254 info.StartTag(dwarf::DW_TAG_subprogram, dwarf::DW_CHILDREN_no);
255 DW_CHECK("Abbrev Number: 2 (DW_TAG_subprogram)");
256 info.WriteStrp(dwarf::DW_AT_name, "Foo", &debug_str_data_);
257 DW_CHECK_NEXT("DW_AT_name : (indirect string, offset: 0xe): Foo");
258 info.WriteAddr(dwarf::DW_AT_low_pc, 0x01010000);
259 DW_CHECK_NEXT("DW_AT_low_pc : 0x1010000");
260 info.WriteAddr(dwarf::DW_AT_high_pc, 0x01020000);
261 DW_CHECK_NEXT("DW_AT_high_pc : 0x1020000");
262 info.EndTag(); // DW_TAG_subprogram
263 info.StartTag(dwarf::DW_TAG_subprogram, dwarf::DW_CHILDREN_no);
264 DW_CHECK("Abbrev Number: 2 (DW_TAG_subprogram)");
265 info.WriteStrp(dwarf::DW_AT_name, "Bar", &debug_str_data_);
266 DW_CHECK_NEXT("DW_AT_name : (indirect string, offset: 0x12): Bar");
267 info.WriteAddr(dwarf::DW_AT_low_pc, 0x01020000);
268 DW_CHECK_NEXT("DW_AT_low_pc : 0x1020000");
269 info.WriteAddr(dwarf::DW_AT_high_pc, 0x01030000);
270 DW_CHECK_NEXT("DW_AT_high_pc : 0x1030000");
271 info.EndTag(); // DW_TAG_subprogram
272 info.EndTag(); // DW_TAG_compile_unit
273 // Test that previous list was properly terminated and empty children.
274 info.StartTag(dwarf::DW_TAG_compile_unit, dwarf::DW_CHILDREN_yes);
275 info.EndTag(); // DW_TAG_compile_unit
276
277 // The abbrev table is just side product, but check it as well.
278 DW_CHECK("Abbrev Number: 3 (DW_TAG_compile_unit)");
279 DW_CHECK("Contents of the .debug_abbrev section:");
280 DW_CHECK("1 DW_TAG_compile_unit [has children]");
281 DW_CHECK_NEXT("DW_AT_producer DW_FORM_strp");
282 DW_CHECK_NEXT("DW_AT_low_pc DW_FORM_addr");
283 DW_CHECK_NEXT("DW_AT_high_pc DW_FORM_addr");
284 DW_CHECK("2 DW_TAG_subprogram [no children]");
285 DW_CHECK_NEXT("DW_AT_name DW_FORM_strp");
286 DW_CHECK_NEXT("DW_AT_low_pc DW_FORM_addr");
287 DW_CHECK_NEXT("DW_AT_high_pc DW_FORM_addr");
288 DW_CHECK("3 DW_TAG_compile_unit [has children]");
289
David Srbecky2f6cdb02015-04-11 00:17:53 +0100290 std::vector<uintptr_t> debug_info_patches;
291 std::vector<uintptr_t> expected_patches { 16, 20, 29, 33, 42, 46 }; // NOLINT
292 dwarf::WriteDebugInfoCU(0 /* debug_abbrev_offset */, info,
293 &debug_info_data_, &debug_info_patches);
294
295 EXPECT_EQ(expected_patches, debug_info_patches);
David Srbeckyb5362472015-04-08 19:37:39 +0100296 CheckObjdumpOutput(is64bit, "-W");
297}
298
David Srbecky15c19752015-03-31 14:53:55 +0000299#endif // HAVE_ANDROID_OS
300
301} // namespace dwarf
302} // namespace art