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