blob: 98f691a7a1cc275cf7f6a488080648e3bd982666 [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
David Srbeckyc2055cf2015-04-12 14:58:39 +0100147// Test x86_64 register mapping. It is the only non-trivial architecture.
148// ARM, X86, and Mips have: dwarf_reg = art_reg + constant.
149TEST_F(DwarfTest, x86_64_RegisterMapping) {
150 constexpr bool is64bit = true;
151 DebugFrameOpCodeWriter<> opcodes;
152 for (int i = 0; i < 16; i++) {
153 opcodes.RelOffset(Reg::X86_64Core(i), 0);
154 }
155 DW_CHECK("FDE");
156 DW_CHECK_NEXT("DW_CFA_offset: r0 (rax)");
157 DW_CHECK_NEXT("DW_CFA_offset: r2 (rcx)");
158 DW_CHECK_NEXT("DW_CFA_offset: r1 (rdx)");
159 DW_CHECK_NEXT("DW_CFA_offset: r3 (rbx)");
160 DW_CHECK_NEXT("DW_CFA_offset: r7 (rsp)");
161 DW_CHECK_NEXT("DW_CFA_offset: r6 (rbp)");
162 DW_CHECK_NEXT("DW_CFA_offset: r4 (rsi)");
163 DW_CHECK_NEXT("DW_CFA_offset: r5 (rdi)");
164 DW_CHECK_NEXT("DW_CFA_offset: r8 (r8)");
165 DW_CHECK_NEXT("DW_CFA_offset: r9 (r9)");
166 DW_CHECK_NEXT("DW_CFA_offset: r10 (r10)");
167 DW_CHECK_NEXT("DW_CFA_offset: r11 (r11)");
168 DW_CHECK_NEXT("DW_CFA_offset: r12 (r12)");
169 DW_CHECK_NEXT("DW_CFA_offset: r13 (r13)");
170 DW_CHECK_NEXT("DW_CFA_offset: r14 (r14)");
171 DW_CHECK_NEXT("DW_CFA_offset: r15 (r15)");
172 DebugFrameOpCodeWriter<> initial_opcodes;
173 WriteEhFrameCIE(is64bit, Reg(16), initial_opcodes, &eh_frame_data_);
174 std::vector<uintptr_t> eh_frame_patches;
175 WriteEhFrameFDE(is64bit, 0, 0x0100000000000000, 0x0200000000000000,
176 opcodes.data(), &eh_frame_data_, &eh_frame_patches);
177
178 CheckObjdumpOutput(is64bit, "-W");
179}
180
David Srbecky15c19752015-03-31 14:53:55 +0000181TEST_F(DwarfTest, DebugLine) {
182 const bool is64bit = false;
183 const int code_factor_bits = 1;
184 DebugLineOpCodeWriter<> opcodes(is64bit, code_factor_bits);
185
186 std::vector<std::string> include_directories;
187 include_directories.push_back("/path/to/source");
188 DW_CHECK("/path/to/source");
189
David Srbeckyb5362472015-04-08 19:37:39 +0100190 std::vector<FileEntry> files {
David Srbecky15c19752015-03-31 14:53:55 +0000191 { "file0.c", 0, 1000, 2000 },
192 { "file1.c", 1, 1000, 2000 },
193 { "file2.c", 1, 1000, 2000 },
194 };
195 DW_CHECK("1\t0\t1000\t2000\tfile0.c");
196 DW_CHECK_NEXT("2\t1\t1000\t2000\tfile1.c");
197 DW_CHECK_NEXT("3\t1\t1000\t2000\tfile2.c");
198
199 DW_CHECK("Line Number Statements");
200 opcodes.SetAddress(0x01000000);
201 DW_CHECK_NEXT("Extended opcode 2: set Address to 0x1000000");
202 opcodes.AddRow();
203 DW_CHECK_NEXT("Copy");
204 opcodes.AdvancePC(0x01000100);
205 DW_CHECK_NEXT("Advance PC by 256 to 0x1000100");
206 opcodes.SetFile(2);
207 DW_CHECK_NEXT("Set File Name to entry 2 in the File Name Table");
208 opcodes.AdvanceLine(3);
209 DW_CHECK_NEXT("Advance Line by 2 to 3");
210 opcodes.SetColumn(4);
211 DW_CHECK_NEXT("Set column to 4");
212 opcodes.NegateStmt();
213 DW_CHECK_NEXT("Set is_stmt to 0");
214 opcodes.SetBasicBlock();
215 DW_CHECK_NEXT("Set basic block");
216 opcodes.SetPrologueEnd();
217 DW_CHECK_NEXT("Set prologue_end to true");
218 opcodes.SetEpilogueBegin();
219 DW_CHECK_NEXT("Set epilogue_begin to true");
220 opcodes.SetISA(5);
221 DW_CHECK_NEXT("Set ISA to 5");
222 opcodes.EndSequence();
223 DW_CHECK_NEXT("Extended opcode 1: End of Sequence");
224 opcodes.DefineFile("file.c", 0, 1000, 2000);
225 DW_CHECK_NEXT("Extended opcode 3: define new File Table entry");
226 DW_CHECK_NEXT("Entry\tDir\tTime\tSize\tName");
227 DW_CHECK_NEXT("1\t0\t1000\t2000\tfile.c");
228
David Srbecky2f6cdb02015-04-11 00:17:53 +0100229 std::vector<uintptr_t> debug_line_patches;
230 std::vector<uintptr_t> expected_patches { 87 }; // NOLINT
231 WriteDebugLineTable(include_directories, files, opcodes,
232 &debug_line_data_, &debug_line_patches);
233
234 EXPECT_EQ(expected_patches, debug_line_patches);
David Srbecky15c19752015-03-31 14:53:55 +0000235 CheckObjdumpOutput(is64bit, "-W");
236}
237
238// DWARF has special one byte codes which advance PC and line at the same time.
239TEST_F(DwarfTest, DebugLineSpecialOpcodes) {
240 const bool is64bit = false;
241 const int code_factor_bits = 1;
242 uint32_t pc = 0x01000000;
243 int line = 1;
244 DebugLineOpCodeWriter<> opcodes(is64bit, code_factor_bits);
245 opcodes.SetAddress(pc);
246 size_t num_rows = 0;
247 DW_CHECK("Line Number Statements:");
248 DW_CHECK("Special opcode");
249 DW_CHECK("Advance PC by constant");
250 DW_CHECK("Decoded dump of debug contents of section .debug_line:");
251 DW_CHECK("Line number Starting address");
252 for (int addr_delta = 0; addr_delta < 80; addr_delta += 2) {
253 for (int line_delta = 16; line_delta >= -16; --line_delta) {
254 pc += addr_delta;
255 line += line_delta;
256 opcodes.AddRow(pc, line);
257 num_rows++;
258 ASSERT_EQ(opcodes.CurrentAddress(), pc);
259 ASSERT_EQ(opcodes.CurrentLine(), line);
260 char expected[1024];
261 sprintf(expected, "%i 0x%x", line, pc);
262 DW_CHECK_NEXT(expected);
263 }
264 }
265 EXPECT_LT(opcodes.data()->size(), num_rows * 3);
266
267 std::vector<std::string> directories;
David Srbeckyb5362472015-04-08 19:37:39 +0100268 std::vector<FileEntry> files { { "file.c", 0, 1000, 2000 } }; // NOLINT
David Srbecky2f6cdb02015-04-11 00:17:53 +0100269 std::vector<uintptr_t> debug_line_patches;
270 WriteDebugLineTable(directories, files, opcodes,
271 &debug_line_data_, &debug_line_patches);
272
David Srbecky15c19752015-03-31 14:53:55 +0000273 CheckObjdumpOutput(is64bit, "-W -WL");
274}
275
David Srbeckyb5362472015-04-08 19:37:39 +0100276TEST_F(DwarfTest, DebugInfo) {
277 constexpr bool is64bit = false;
278 DebugInfoEntryWriter<> info(is64bit, &debug_abbrev_data_);
279 DW_CHECK("Contents of the .debug_info section:");
280 info.StartTag(dwarf::DW_TAG_compile_unit, dwarf::DW_CHILDREN_yes);
281 DW_CHECK("Abbrev Number: 1 (DW_TAG_compile_unit)");
282 info.WriteStrp(dwarf::DW_AT_producer, "Compiler name", &debug_str_data_);
283 DW_CHECK_NEXT("DW_AT_producer : (indirect string, offset: 0x0): Compiler name");
284 info.WriteAddr(dwarf::DW_AT_low_pc, 0x01000000);
285 DW_CHECK_NEXT("DW_AT_low_pc : 0x1000000");
286 info.WriteAddr(dwarf::DW_AT_high_pc, 0x02000000);
287 DW_CHECK_NEXT("DW_AT_high_pc : 0x2000000");
288 info.StartTag(dwarf::DW_TAG_subprogram, dwarf::DW_CHILDREN_no);
289 DW_CHECK("Abbrev Number: 2 (DW_TAG_subprogram)");
290 info.WriteStrp(dwarf::DW_AT_name, "Foo", &debug_str_data_);
291 DW_CHECK_NEXT("DW_AT_name : (indirect string, offset: 0xe): Foo");
292 info.WriteAddr(dwarf::DW_AT_low_pc, 0x01010000);
293 DW_CHECK_NEXT("DW_AT_low_pc : 0x1010000");
294 info.WriteAddr(dwarf::DW_AT_high_pc, 0x01020000);
295 DW_CHECK_NEXT("DW_AT_high_pc : 0x1020000");
296 info.EndTag(); // DW_TAG_subprogram
297 info.StartTag(dwarf::DW_TAG_subprogram, dwarf::DW_CHILDREN_no);
298 DW_CHECK("Abbrev Number: 2 (DW_TAG_subprogram)");
299 info.WriteStrp(dwarf::DW_AT_name, "Bar", &debug_str_data_);
300 DW_CHECK_NEXT("DW_AT_name : (indirect string, offset: 0x12): Bar");
301 info.WriteAddr(dwarf::DW_AT_low_pc, 0x01020000);
302 DW_CHECK_NEXT("DW_AT_low_pc : 0x1020000");
303 info.WriteAddr(dwarf::DW_AT_high_pc, 0x01030000);
304 DW_CHECK_NEXT("DW_AT_high_pc : 0x1030000");
305 info.EndTag(); // DW_TAG_subprogram
306 info.EndTag(); // DW_TAG_compile_unit
307 // Test that previous list was properly terminated and empty children.
308 info.StartTag(dwarf::DW_TAG_compile_unit, dwarf::DW_CHILDREN_yes);
309 info.EndTag(); // DW_TAG_compile_unit
310
311 // The abbrev table is just side product, but check it as well.
312 DW_CHECK("Abbrev Number: 3 (DW_TAG_compile_unit)");
313 DW_CHECK("Contents of the .debug_abbrev section:");
314 DW_CHECK("1 DW_TAG_compile_unit [has children]");
315 DW_CHECK_NEXT("DW_AT_producer DW_FORM_strp");
316 DW_CHECK_NEXT("DW_AT_low_pc DW_FORM_addr");
317 DW_CHECK_NEXT("DW_AT_high_pc DW_FORM_addr");
318 DW_CHECK("2 DW_TAG_subprogram [no children]");
319 DW_CHECK_NEXT("DW_AT_name DW_FORM_strp");
320 DW_CHECK_NEXT("DW_AT_low_pc DW_FORM_addr");
321 DW_CHECK_NEXT("DW_AT_high_pc DW_FORM_addr");
322 DW_CHECK("3 DW_TAG_compile_unit [has children]");
323
David Srbecky2f6cdb02015-04-11 00:17:53 +0100324 std::vector<uintptr_t> debug_info_patches;
325 std::vector<uintptr_t> expected_patches { 16, 20, 29, 33, 42, 46 }; // NOLINT
326 dwarf::WriteDebugInfoCU(0 /* debug_abbrev_offset */, info,
327 &debug_info_data_, &debug_info_patches);
328
329 EXPECT_EQ(expected_patches, debug_info_patches);
David Srbeckyb5362472015-04-08 19:37:39 +0100330 CheckObjdumpOutput(is64bit, "-W");
331}
332
David Srbecky15c19752015-03-31 14:53:55 +0000333#endif // HAVE_ANDROID_OS
334
335} // namespace dwarf
336} // namespace art