blob: e9cd421da98875d7d47dda05226d9758b4619827 [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.
Andreas Gampec60e1b72015-07-30 08:57:50 -070030#ifndef __ANDROID__
David Srbecky15c19752015-03-31 14:53:55 +000031
Narayan Kamath97dabb72015-06-11 08:49:57 +010032constexpr CFIFormat kCFIFormat = DW_DEBUG_FRAME_FORMAT;
33
David Srbecky15c19752015-03-31 14:53:55 +000034TEST_F(DwarfTest, DebugFrame) {
35 const bool is64bit = false;
36
37 // Pick offset value which would catch Uleb vs Sleb errors.
38 const int offset = 40000;
39 ASSERT_EQ(UnsignedLeb128Size(offset / 4), 2u);
40 ASSERT_EQ(SignedLeb128Size(offset / 4), 3u);
41 DW_CHECK("Data alignment factor: -4");
42 const Reg reg(6);
43
44 // Test the opcodes in the order mentioned in the spec.
45 // There are usually several encoding variations of each opcode.
46 DebugFrameOpCodeWriter<> opcodes;
47 DW_CHECK("FDE");
48 int pc = 0;
49 for (int i : {0, 1, 0x3F, 0x40, 0xFF, 0x100, 0xFFFF, 0x10000}) {
50 pc += i;
51 opcodes.AdvancePC(pc);
52 }
53 DW_CHECK_NEXT("DW_CFA_advance_loc: 1 to 01000001");
54 DW_CHECK_NEXT("DW_CFA_advance_loc: 63 to 01000040");
55 DW_CHECK_NEXT("DW_CFA_advance_loc1: 64 to 01000080");
56 DW_CHECK_NEXT("DW_CFA_advance_loc1: 255 to 0100017f");
57 DW_CHECK_NEXT("DW_CFA_advance_loc2: 256 to 0100027f");
58 DW_CHECK_NEXT("DW_CFA_advance_loc2: 65535 to 0101027e");
59 DW_CHECK_NEXT("DW_CFA_advance_loc4: 65536 to 0102027e");
60 opcodes.DefCFA(reg, offset);
61 DW_CHECK_NEXT("DW_CFA_def_cfa: r6 (esi) ofs 40000");
62 opcodes.DefCFA(reg, -offset);
63 DW_CHECK_NEXT("DW_CFA_def_cfa_sf: r6 (esi) ofs -40000");
64 opcodes.DefCFARegister(reg);
65 DW_CHECK_NEXT("DW_CFA_def_cfa_register: r6 (esi)");
66 opcodes.DefCFAOffset(offset);
67 DW_CHECK_NEXT("DW_CFA_def_cfa_offset: 40000");
68 opcodes.DefCFAOffset(-offset);
69 DW_CHECK_NEXT("DW_CFA_def_cfa_offset_sf: -40000");
70 uint8_t expr[] = { 0 };
71 opcodes.DefCFAExpression(expr, arraysize(expr));
72 DW_CHECK_NEXT("DW_CFA_def_cfa_expression");
73 opcodes.Undefined(reg);
74 DW_CHECK_NEXT("DW_CFA_undefined: r6 (esi)");
75 opcodes.SameValue(reg);
76 DW_CHECK_NEXT("DW_CFA_same_value: r6 (esi)");
77 opcodes.Offset(Reg(0x3F), -offset);
78 // Bad register likely means that it does not exist on x86,
79 // but we want to test high register numbers anyway.
80 DW_CHECK_NEXT("DW_CFA_offset: bad register: r63 at cfa-40000");
81 opcodes.Offset(Reg(0x40), -offset);
82 DW_CHECK_NEXT("DW_CFA_offset_extended: bad register: r64 at cfa-40000");
83 opcodes.Offset(Reg(0x40), offset);
84 DW_CHECK_NEXT("DW_CFA_offset_extended_sf: bad register: r64 at cfa+40000");
85 opcodes.ValOffset(reg, -offset);
86 DW_CHECK_NEXT("DW_CFA_val_offset: r6 (esi) at cfa-40000");
87 opcodes.ValOffset(reg, offset);
88 DW_CHECK_NEXT("DW_CFA_val_offset_sf: r6 (esi) at cfa+40000");
89 opcodes.Register(reg, Reg(1));
90 DW_CHECK_NEXT("DW_CFA_register: r6 (esi) in r1 (ecx)");
91 opcodes.Expression(reg, expr, arraysize(expr));
92 DW_CHECK_NEXT("DW_CFA_expression: r6 (esi)");
93 opcodes.ValExpression(reg, expr, arraysize(expr));
94 DW_CHECK_NEXT("DW_CFA_val_expression: r6 (esi)");
95 opcodes.Restore(Reg(0x3F));
96 DW_CHECK_NEXT("DW_CFA_restore: bad register: r63");
97 opcodes.Restore(Reg(0x40));
98 DW_CHECK_NEXT("DW_CFA_restore_extended: bad register: r64");
99 opcodes.Restore(reg);
100 DW_CHECK_NEXT("DW_CFA_restore: r6 (esi)");
101 opcodes.RememberState();
102 DW_CHECK_NEXT("DW_CFA_remember_state");
103 opcodes.RestoreState();
104 DW_CHECK_NEXT("DW_CFA_restore_state");
105 opcodes.Nop();
106 DW_CHECK_NEXT("DW_CFA_nop");
107
108 // Also test helpers.
109 opcodes.DefCFA(Reg(4), 100); // ESP
110 DW_CHECK_NEXT("DW_CFA_def_cfa: r4 (esp) ofs 100");
111 opcodes.AdjustCFAOffset(8);
112 DW_CHECK_NEXT("DW_CFA_def_cfa_offset: 108");
113 opcodes.RelOffset(Reg(0), 0); // push R0
114 DW_CHECK_NEXT("DW_CFA_offset: r0 (eax) at cfa-108");
115 opcodes.RelOffset(Reg(1), 4); // push R1
116 DW_CHECK_NEXT("DW_CFA_offset: r1 (ecx) at cfa-104");
117 opcodes.RelOffsetForMany(Reg(2), 8, 1 | (1 << 3), 4); // push R2 and R5
118 DW_CHECK_NEXT("DW_CFA_offset: r2 (edx) at cfa-100");
119 DW_CHECK_NEXT("DW_CFA_offset: r5 (ebp) at cfa-96");
120 opcodes.RestoreMany(Reg(2), 1 | (1 << 3)); // pop R2 and R5
121 DW_CHECK_NEXT("DW_CFA_restore: r2 (edx)");
122 DW_CHECK_NEXT("DW_CFA_restore: r5 (ebp)");
123
David Srbecky15c19752015-03-31 14:53:55 +0000124 DebugFrameOpCodeWriter<> initial_opcodes;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000125 WriteCIE(is64bit, Reg(is64bit ? 16 : 8),
126 initial_opcodes, kCFIFormat, &debug_frame_data_);
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100127 std::vector<uintptr_t> debug_frame_patches;
David Srbecky2f6cdb02015-04-11 00:17:53 +0100128 std::vector<uintptr_t> expected_patches { 28 }; // NOLINT
David Srbecky6d8c8f02015-10-26 10:57:09 +0000129 WriteFDE(is64bit, 0, 0, 0x01000000, 0x01000000, ArrayRef<const uint8_t>(*opcodes.data()),
130 kCFIFormat, 0, &debug_frame_data_, &debug_frame_patches);
David Srbecky2f6cdb02015-04-11 00:17:53 +0100131
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100132 EXPECT_EQ(expected_patches, debug_frame_patches);
David Srbecky15c19752015-03-31 14:53:55 +0000133 CheckObjdumpOutput(is64bit, "-W");
134}
135
David Srbecky2f6cdb02015-04-11 00:17:53 +0100136TEST_F(DwarfTest, DebugFrame64) {
David Srbeckyb5362472015-04-08 19:37:39 +0100137 constexpr bool is64bit = true;
138 DebugFrameOpCodeWriter<> initial_opcodes;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000139 WriteCIE(is64bit, Reg(16),
140 initial_opcodes, kCFIFormat, &debug_frame_data_);
David Srbeckyb5362472015-04-08 19:37:39 +0100141 DebugFrameOpCodeWriter<> opcodes;
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100142 std::vector<uintptr_t> debug_frame_patches;
David Srbecky2f6cdb02015-04-11 00:17:53 +0100143 std::vector<uintptr_t> expected_patches { 32 }; // NOLINT
David Srbecky6d8c8f02015-10-26 10:57:09 +0000144 WriteFDE(is64bit, 0, 0, 0x0100000000000000, 0x0200000000000000,
145 ArrayRef<const uint8_t>(*opcodes.data()),
146 kCFIFormat, 0, &debug_frame_data_, &debug_frame_patches);
David Srbecky15c19752015-03-31 14:53:55 +0000147 DW_CHECK("FDE cie=00000000 pc=100000000000000..300000000000000");
David Srbecky2f6cdb02015-04-11 00:17:53 +0100148
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100149 EXPECT_EQ(expected_patches, debug_frame_patches);
David Srbecky15c19752015-03-31 14:53:55 +0000150 CheckObjdumpOutput(is64bit, "-W");
151}
152
David Srbeckyc2055cf2015-04-12 14:58:39 +0100153// Test x86_64 register mapping. It is the only non-trivial architecture.
154// ARM, X86, and Mips have: dwarf_reg = art_reg + constant.
155TEST_F(DwarfTest, x86_64_RegisterMapping) {
156 constexpr bool is64bit = true;
157 DebugFrameOpCodeWriter<> opcodes;
158 for (int i = 0; i < 16; i++) {
159 opcodes.RelOffset(Reg::X86_64Core(i), 0);
160 }
161 DW_CHECK("FDE");
162 DW_CHECK_NEXT("DW_CFA_offset: r0 (rax)");
163 DW_CHECK_NEXT("DW_CFA_offset: r2 (rcx)");
164 DW_CHECK_NEXT("DW_CFA_offset: r1 (rdx)");
165 DW_CHECK_NEXT("DW_CFA_offset: r3 (rbx)");
166 DW_CHECK_NEXT("DW_CFA_offset: r7 (rsp)");
167 DW_CHECK_NEXT("DW_CFA_offset: r6 (rbp)");
168 DW_CHECK_NEXT("DW_CFA_offset: r4 (rsi)");
169 DW_CHECK_NEXT("DW_CFA_offset: r5 (rdi)");
170 DW_CHECK_NEXT("DW_CFA_offset: r8 (r8)");
171 DW_CHECK_NEXT("DW_CFA_offset: r9 (r9)");
172 DW_CHECK_NEXT("DW_CFA_offset: r10 (r10)");
173 DW_CHECK_NEXT("DW_CFA_offset: r11 (r11)");
174 DW_CHECK_NEXT("DW_CFA_offset: r12 (r12)");
175 DW_CHECK_NEXT("DW_CFA_offset: r13 (r13)");
176 DW_CHECK_NEXT("DW_CFA_offset: r14 (r14)");
177 DW_CHECK_NEXT("DW_CFA_offset: r15 (r15)");
178 DebugFrameOpCodeWriter<> initial_opcodes;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000179 WriteCIE(is64bit, Reg(16),
180 initial_opcodes, kCFIFormat, &debug_frame_data_);
David Srbeckyad5fa8c2015-05-06 18:27:35 +0100181 std::vector<uintptr_t> debug_frame_patches;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000182 WriteFDE(is64bit, 0, 0, 0x0100000000000000, 0x0200000000000000,
183 ArrayRef<const uint8_t>(*opcodes.data()),
184 kCFIFormat, 0, &debug_frame_data_, &debug_frame_patches);
David Srbeckyc2055cf2015-04-12 14:58:39 +0100185
186 CheckObjdumpOutput(is64bit, "-W");
187}
188
David Srbecky15c19752015-03-31 14:53:55 +0000189TEST_F(DwarfTest, DebugLine) {
190 const bool is64bit = false;
191 const int code_factor_bits = 1;
192 DebugLineOpCodeWriter<> opcodes(is64bit, code_factor_bits);
193
194 std::vector<std::string> include_directories;
195 include_directories.push_back("/path/to/source");
196 DW_CHECK("/path/to/source");
197
David Srbeckyb5362472015-04-08 19:37:39 +0100198 std::vector<FileEntry> files {
David Srbecky15c19752015-03-31 14:53:55 +0000199 { "file0.c", 0, 1000, 2000 },
200 { "file1.c", 1, 1000, 2000 },
201 { "file2.c", 1, 1000, 2000 },
202 };
203 DW_CHECK("1\t0\t1000\t2000\tfile0.c");
204 DW_CHECK_NEXT("2\t1\t1000\t2000\tfile1.c");
205 DW_CHECK_NEXT("3\t1\t1000\t2000\tfile2.c");
206
207 DW_CHECK("Line Number Statements");
208 opcodes.SetAddress(0x01000000);
209 DW_CHECK_NEXT("Extended opcode 2: set Address to 0x1000000");
210 opcodes.AddRow();
211 DW_CHECK_NEXT("Copy");
212 opcodes.AdvancePC(0x01000100);
213 DW_CHECK_NEXT("Advance PC by 256 to 0x1000100");
214 opcodes.SetFile(2);
215 DW_CHECK_NEXT("Set File Name to entry 2 in the File Name Table");
216 opcodes.AdvanceLine(3);
217 DW_CHECK_NEXT("Advance Line by 2 to 3");
218 opcodes.SetColumn(4);
219 DW_CHECK_NEXT("Set column to 4");
220 opcodes.NegateStmt();
221 DW_CHECK_NEXT("Set is_stmt to 0");
222 opcodes.SetBasicBlock();
223 DW_CHECK_NEXT("Set basic block");
224 opcodes.SetPrologueEnd();
225 DW_CHECK_NEXT("Set prologue_end to true");
226 opcodes.SetEpilogueBegin();
227 DW_CHECK_NEXT("Set epilogue_begin to true");
228 opcodes.SetISA(5);
229 DW_CHECK_NEXT("Set ISA to 5");
230 opcodes.EndSequence();
231 DW_CHECK_NEXT("Extended opcode 1: End of Sequence");
232 opcodes.DefineFile("file.c", 0, 1000, 2000);
233 DW_CHECK_NEXT("Extended opcode 3: define new File Table entry");
234 DW_CHECK_NEXT("Entry\tDir\tTime\tSize\tName");
235 DW_CHECK_NEXT("1\t0\t1000\t2000\tfile.c");
236
David Srbecky2f6cdb02015-04-11 00:17:53 +0100237 std::vector<uintptr_t> debug_line_patches;
238 std::vector<uintptr_t> expected_patches { 87 }; // NOLINT
239 WriteDebugLineTable(include_directories, files, opcodes,
David Srbeckyb851b492015-11-11 20:19:38 +0000240 0, &debug_line_data_, &debug_line_patches);
David Srbecky2f6cdb02015-04-11 00:17:53 +0100241
242 EXPECT_EQ(expected_patches, debug_line_patches);
David Srbecky15c19752015-03-31 14:53:55 +0000243 CheckObjdumpOutput(is64bit, "-W");
244}
245
246// DWARF has special one byte codes which advance PC and line at the same time.
247TEST_F(DwarfTest, DebugLineSpecialOpcodes) {
248 const bool is64bit = false;
249 const int code_factor_bits = 1;
250 uint32_t pc = 0x01000000;
251 int line = 1;
252 DebugLineOpCodeWriter<> opcodes(is64bit, code_factor_bits);
253 opcodes.SetAddress(pc);
254 size_t num_rows = 0;
255 DW_CHECK("Line Number Statements:");
256 DW_CHECK("Special opcode");
257 DW_CHECK("Advance PC by constant");
258 DW_CHECK("Decoded dump of debug contents of section .debug_line:");
259 DW_CHECK("Line number Starting address");
260 for (int addr_delta = 0; addr_delta < 80; addr_delta += 2) {
261 for (int line_delta = 16; line_delta >= -16; --line_delta) {
262 pc += addr_delta;
263 line += line_delta;
264 opcodes.AddRow(pc, line);
265 num_rows++;
266 ASSERT_EQ(opcodes.CurrentAddress(), pc);
267 ASSERT_EQ(opcodes.CurrentLine(), line);
268 char expected[1024];
269 sprintf(expected, "%i 0x%x", line, pc);
270 DW_CHECK_NEXT(expected);
271 }
272 }
273 EXPECT_LT(opcodes.data()->size(), num_rows * 3);
274
275 std::vector<std::string> directories;
David Srbeckyb5362472015-04-08 19:37:39 +0100276 std::vector<FileEntry> files { { "file.c", 0, 1000, 2000 } }; // NOLINT
David Srbecky2f6cdb02015-04-11 00:17:53 +0100277 std::vector<uintptr_t> debug_line_patches;
278 WriteDebugLineTable(directories, files, opcodes,
David Srbeckyb851b492015-11-11 20:19:38 +0000279 0, &debug_line_data_, &debug_line_patches);
David Srbecky2f6cdb02015-04-11 00:17:53 +0100280
David Srbecky15c19752015-03-31 14:53:55 +0000281 CheckObjdumpOutput(is64bit, "-W -WL");
282}
283
David Srbeckyb5362472015-04-08 19:37:39 +0100284TEST_F(DwarfTest, DebugInfo) {
285 constexpr bool is64bit = false;
286 DebugInfoEntryWriter<> info(is64bit, &debug_abbrev_data_);
287 DW_CHECK("Contents of the .debug_info section:");
David Srbecky04b05262015-11-09 18:05:48 +0000288 info.StartTag(dwarf::DW_TAG_compile_unit);
David Srbeckyb5362472015-04-08 19:37:39 +0100289 DW_CHECK("Abbrev Number: 1 (DW_TAG_compile_unit)");
290 info.WriteStrp(dwarf::DW_AT_producer, "Compiler name", &debug_str_data_);
291 DW_CHECK_NEXT("DW_AT_producer : (indirect string, offset: 0x0): Compiler name");
292 info.WriteAddr(dwarf::DW_AT_low_pc, 0x01000000);
293 DW_CHECK_NEXT("DW_AT_low_pc : 0x1000000");
294 info.WriteAddr(dwarf::DW_AT_high_pc, 0x02000000);
295 DW_CHECK_NEXT("DW_AT_high_pc : 0x2000000");
David Srbecky04b05262015-11-09 18:05:48 +0000296 info.StartTag(dwarf::DW_TAG_subprogram);
David Srbeckyb5362472015-04-08 19:37:39 +0100297 DW_CHECK("Abbrev Number: 2 (DW_TAG_subprogram)");
298 info.WriteStrp(dwarf::DW_AT_name, "Foo", &debug_str_data_);
299 DW_CHECK_NEXT("DW_AT_name : (indirect string, offset: 0xe): Foo");
300 info.WriteAddr(dwarf::DW_AT_low_pc, 0x01010000);
301 DW_CHECK_NEXT("DW_AT_low_pc : 0x1010000");
302 info.WriteAddr(dwarf::DW_AT_high_pc, 0x01020000);
303 DW_CHECK_NEXT("DW_AT_high_pc : 0x1020000");
304 info.EndTag(); // DW_TAG_subprogram
David Srbecky04b05262015-11-09 18:05:48 +0000305 info.StartTag(dwarf::DW_TAG_subprogram);
David Srbeckyb5362472015-04-08 19:37:39 +0100306 DW_CHECK("Abbrev Number: 2 (DW_TAG_subprogram)");
307 info.WriteStrp(dwarf::DW_AT_name, "Bar", &debug_str_data_);
308 DW_CHECK_NEXT("DW_AT_name : (indirect string, offset: 0x12): Bar");
309 info.WriteAddr(dwarf::DW_AT_low_pc, 0x01020000);
310 DW_CHECK_NEXT("DW_AT_low_pc : 0x1020000");
311 info.WriteAddr(dwarf::DW_AT_high_pc, 0x01030000);
312 DW_CHECK_NEXT("DW_AT_high_pc : 0x1030000");
313 info.EndTag(); // DW_TAG_subprogram
314 info.EndTag(); // DW_TAG_compile_unit
315 // Test that previous list was properly terminated and empty children.
David Srbecky04b05262015-11-09 18:05:48 +0000316 info.StartTag(dwarf::DW_TAG_compile_unit);
David Srbeckyb5362472015-04-08 19:37:39 +0100317 info.EndTag(); // DW_TAG_compile_unit
318
319 // The abbrev table is just side product, but check it as well.
320 DW_CHECK("Abbrev Number: 3 (DW_TAG_compile_unit)");
321 DW_CHECK("Contents of the .debug_abbrev section:");
322 DW_CHECK("1 DW_TAG_compile_unit [has children]");
323 DW_CHECK_NEXT("DW_AT_producer 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("2 DW_TAG_subprogram [no children]");
327 DW_CHECK_NEXT("DW_AT_name DW_FORM_strp");
328 DW_CHECK_NEXT("DW_AT_low_pc DW_FORM_addr");
329 DW_CHECK_NEXT("DW_AT_high_pc DW_FORM_addr");
David Srbecky04b05262015-11-09 18:05:48 +0000330 DW_CHECK("3 DW_TAG_compile_unit [no children]");
David Srbeckyb5362472015-04-08 19:37:39 +0100331
David Srbecky2f6cdb02015-04-11 00:17:53 +0100332 std::vector<uintptr_t> debug_info_patches;
333 std::vector<uintptr_t> expected_patches { 16, 20, 29, 33, 42, 46 }; // NOLINT
334 dwarf::WriteDebugInfoCU(0 /* debug_abbrev_offset */, info,
David Srbeckyb851b492015-11-11 20:19:38 +0000335 0, &debug_info_data_, &debug_info_patches);
David Srbecky2f6cdb02015-04-11 00:17:53 +0100336
337 EXPECT_EQ(expected_patches, debug_info_patches);
David Srbeckyb5362472015-04-08 19:37:39 +0100338 CheckObjdumpOutput(is64bit, "-W");
339}
340
Andreas Gampec60e1b72015-07-30 08:57:50 -0700341#endif // __ANDROID__
David Srbecky15c19752015-03-31 14:53:55 +0000342
343} // namespace dwarf
344} // namespace art