1 /*
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"
20 #include "dwarf/debug_info_entry_writer.h"
21 #include "dwarf/debug_line_opcode_writer.h"
22 #include "dwarf/dwarf_constants.h"
23 #include "dwarf/headers.h"
24 #include "gtest/gtest.h"
25
26 namespace art {
27 namespace dwarf {
28
29 // Run the tests only on host since we need objdump.
30 #ifndef ART_TARGET_ANDROID
31
TEST_F(DwarfTest,DebugFrame)32 TEST_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 DW_CHECK_NEXT("DW_CFA_offset: r63 at cfa-40000");
77 opcodes.Offset(Reg(0x40), -offset);
78 DW_CHECK_NEXT("DW_CFA_offset_extended: r64 at cfa-40000");
79 opcodes.Offset(Reg(0x40), offset);
80 DW_CHECK_NEXT("DW_CFA_offset_extended_sf: r64 at cfa+40000");
81 opcodes.ValOffset(reg, -offset);
82 DW_CHECK_NEXT("DW_CFA_val_offset: r6 (esi) at cfa-40000");
83 opcodes.ValOffset(reg, offset);
84 DW_CHECK_NEXT("DW_CFA_val_offset_sf: r6 (esi) at cfa+40000");
85 opcodes.Register(reg, Reg(1));
86 DW_CHECK_NEXT("DW_CFA_register: r6 (esi) in r1 (ecx)");
87 opcodes.Expression(reg, expr, arraysize(expr));
88 DW_CHECK_NEXT("DW_CFA_expression: r6 (esi)");
89 opcodes.ValExpression(reg, expr, arraysize(expr));
90 DW_CHECK_NEXT("DW_CFA_val_expression: r6 (esi)");
91 opcodes.Restore(Reg(0x3F));
92 DW_CHECK_NEXT("DW_CFA_restore: bad register: r63");
93 opcodes.Restore(Reg(0x40));
94 DW_CHECK_NEXT("DW_CFA_restore_extended: bad register: r64");
95 opcodes.Restore(reg);
96 DW_CHECK_NEXT("DW_CFA_restore: r6 (esi)");
97 opcodes.RememberState();
98 DW_CHECK_NEXT("DW_CFA_remember_state");
99 opcodes.RestoreState();
100 DW_CHECK_NEXT("DW_CFA_restore_state");
101 opcodes.Nop();
102 DW_CHECK_NEXT("DW_CFA_nop");
103
104 // Also test helpers.
105 opcodes.DefCFA(Reg(4), 100); // ESP
106 DW_CHECK_NEXT("DW_CFA_def_cfa: r4 (esp) ofs 100");
107 opcodes.AdjustCFAOffset(8);
108 DW_CHECK_NEXT("DW_CFA_def_cfa_offset: 108");
109 opcodes.RelOffset(Reg(0), 0); // push R0
110 DW_CHECK_NEXT("DW_CFA_offset: r0 (eax) at cfa-108");
111 opcodes.RelOffset(Reg(1), 4); // push R1
112 DW_CHECK_NEXT("DW_CFA_offset: r1 (ecx) at cfa-104");
113 opcodes.RelOffsetForMany(Reg(2), 8, 1 | (1 << 3), 4); // push R2 and R5
114 DW_CHECK_NEXT("DW_CFA_offset: r2 (edx) at cfa-100");
115 DW_CHECK_NEXT("DW_CFA_offset: r5 (ebp) at cfa-96");
116 opcodes.RestoreMany(Reg(2), 1 | (1 << 3)); // pop R2 and R5
117 DW_CHECK_NEXT("DW_CFA_restore: r2 (edx)");
118 DW_CHECK_NEXT("DW_CFA_restore: r5 (ebp)");
119
120 DebugFrameOpCodeWriter<> initial_opcodes;
121 WriteCIE(is64bit, Reg(is64bit ? 16 : 8), initial_opcodes, &debug_frame_data_);
122 WriteFDE(is64bit,
123 /* cie_pointer= */ 0,
124 0x01000000,
125 0x01000000,
126 ArrayRef<const uint8_t>(*opcodes.data()),
127 &debug_frame_data_);
128
129 CheckObjdumpOutput(is64bit, "-W");
130 }
131
TEST_F(DwarfTest,DISABLED_DebugFrame64)132 TEST_F(DwarfTest, DISABLED_DebugFrame64) {
133 constexpr bool is64bit = true;
134 DebugFrameOpCodeWriter<> initial_opcodes;
135 WriteCIE(is64bit, Reg(16), initial_opcodes, &debug_frame_data_);
136 DebugFrameOpCodeWriter<> opcodes;
137 WriteFDE(is64bit,
138 /* cie_pointer= */ 0,
139 0x0100000000000000,
140 0x0200000000000000,
141 ArrayRef<const uint8_t>(*opcodes.data()),
142 &debug_frame_data_);
143 DW_CHECK("FDE cie=00000000 pc=100000000000000..300000000000000");
144
145 CheckObjdumpOutput(is64bit, "-W");
146 }
147
148 // Test x86_64 register mapping. It is the only non-trivial architecture.
149 // ARM, X86, and Mips have: dwarf_reg = art_reg + constant.
TEST_F(DwarfTest,x86_64_RegisterMapping)150 TEST_F(DwarfTest, x86_64_RegisterMapping) {
151 constexpr bool is64bit = true;
152 DebugFrameOpCodeWriter<> opcodes;
153 for (int i = 0; i < 16; i++) {
154 opcodes.RelOffset(Reg::X86_64Core(i), 0);
155 }
156 DW_CHECK("FDE");
157 DW_CHECK_NEXT("DW_CFA_offset: r0 (rax)");
158 DW_CHECK_NEXT("DW_CFA_offset: r2 (rcx)");
159 DW_CHECK_NEXT("DW_CFA_offset: r1 (rdx)");
160 DW_CHECK_NEXT("DW_CFA_offset: r3 (rbx)");
161 DW_CHECK_NEXT("DW_CFA_offset: r7 (rsp)");
162 DW_CHECK_NEXT("DW_CFA_offset: r6 (rbp)");
163 DW_CHECK_NEXT("DW_CFA_offset: r4 (rsi)");
164 DW_CHECK_NEXT("DW_CFA_offset: r5 (rdi)");
165 DW_CHECK_NEXT("DW_CFA_offset: r8 (r8)");
166 DW_CHECK_NEXT("DW_CFA_offset: r9 (r9)");
167 DW_CHECK_NEXT("DW_CFA_offset: r10 (r10)");
168 DW_CHECK_NEXT("DW_CFA_offset: r11 (r11)");
169 DW_CHECK_NEXT("DW_CFA_offset: r12 (r12)");
170 DW_CHECK_NEXT("DW_CFA_offset: r13 (r13)");
171 DW_CHECK_NEXT("DW_CFA_offset: r14 (r14)");
172 DW_CHECK_NEXT("DW_CFA_offset: r15 (r15)");
173 DebugFrameOpCodeWriter<> initial_opcodes;
174 WriteCIE(is64bit, Reg(16), initial_opcodes, &debug_frame_data_);
175 WriteFDE(is64bit,
176 /* cie_pointer= */ 0,
177 0x0100000000000000,
178 0x0200000000000000,
179 ArrayRef<const uint8_t>(*opcodes.data()),
180 &debug_frame_data_);
181
182 CheckObjdumpOutput(is64bit, "-W");
183 }
184
TEST_F(DwarfTest,DISABLED_DebugLine)185 TEST_F(DwarfTest, DISABLED_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
194 std::vector<FileEntry> files {
195 { "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.SetIsStmt(true);
217 DW_CHECK_NEXT("Set is_stmt to 1");
218 opcodes.SetIsStmt(false);
219 DW_CHECK_NEXT("Set is_stmt to 0");
220 opcodes.SetBasicBlock();
221 DW_CHECK_NEXT("Set basic block");
222 opcodes.SetPrologueEnd();
223 DW_CHECK_NEXT("Set prologue_end to true");
224 opcodes.SetEpilogueBegin();
225 DW_CHECK_NEXT("Set epilogue_begin to true");
226 opcodes.SetISA(5);
227 DW_CHECK_NEXT("Set ISA to 5");
228 opcodes.EndSequence();
229 DW_CHECK_NEXT("Extended opcode 1: End of Sequence");
230 opcodes.DefineFile("file.c", 0, 1000, 2000);
231 DW_CHECK_NEXT("Extended opcode 3: define new File Table entry");
232 DW_CHECK_NEXT("Entry\tDir\tTime\tSize\tName");
233 DW_CHECK_NEXT("1\t0\t1000\t2000\tfile.c");
234
235 WriteDebugLineTable(include_directories, files, opcodes, &debug_line_data_);
236
237 CheckObjdumpOutput(is64bit, "-W");
238 }
239
240 // DWARF has special one byte codes which advance PC and line at the same time.
TEST_F(DwarfTest,DebugLineSpecialOpcodes)241 TEST_F(DwarfTest, DebugLineSpecialOpcodes) {
242 const bool is64bit = false;
243 const int code_factor_bits = 1;
244 uint32_t pc = 0x01000000;
245 int line = 1;
246 DebugLineOpCodeWriter<> opcodes(is64bit, code_factor_bits);
247 opcodes.SetAddress(pc);
248 size_t num_rows = 0;
249 DW_CHECK("Line Number Statements:");
250 DW_CHECK("Special opcode");
251 DW_CHECK("Advance PC by constant");
252 DW_CHECK("Decoded dump of debug contents of section .debug_line:");
253 DW_CHECK("Line number Starting address");
254 for (int addr_delta = 0; addr_delta < 80; addr_delta += 2) {
255 for (int line_delta = 16; line_delta >= -16; --line_delta) {
256 pc += addr_delta;
257 line += line_delta;
258 opcodes.AddRow(pc, line);
259 num_rows++;
260 ASSERT_EQ(opcodes.CurrentAddress(), pc);
261 ASSERT_EQ(opcodes.CurrentLine(), line);
262 char expected[1024];
263 sprintf(expected, "%i 0x%x", line, pc);
264 DW_CHECK_NEXT(expected);
265 }
266 }
267 EXPECT_LT(opcodes.data()->size(), num_rows * 3);
268
269 std::vector<std::string> directories;
270 std::vector<FileEntry> files = { { "file.c", 0, 1000, 2000 } };
271 WriteDebugLineTable(directories, files, opcodes, &debug_line_data_);
272
273 CheckObjdumpOutput(is64bit, "-W -WL");
274 }
275
TEST_F(DwarfTest,DebugInfo)276 TEST_F(DwarfTest, DebugInfo) {
277 constexpr bool is64bit = false;
278 DebugAbbrevWriter<> debug_abbrev(&debug_abbrev_data_);
279 DebugInfoEntryWriter<> info(is64bit, &debug_abbrev);
280 DW_CHECK("Contents of the .debug_info section:");
281 info.StartTag(dwarf::DW_TAG_compile_unit);
282 DW_CHECK("Abbrev Number: 1 (DW_TAG_compile_unit)");
283 info.WriteStrp(dwarf::DW_AT_producer, "Compiler name", &debug_str_data_);
284 DW_CHECK_NEXT("DW_AT_producer : (indirect string, offset: 0x0): Compiler name");
285 info.WriteAddr(dwarf::DW_AT_low_pc, 0x01000000);
286 DW_CHECK_NEXT("DW_AT_low_pc : 0x1000000");
287 info.WriteAddr(dwarf::DW_AT_high_pc, 0x02000000);
288 DW_CHECK_NEXT("DW_AT_high_pc : 0x2000000");
289 info.StartTag(dwarf::DW_TAG_subprogram);
290 DW_CHECK("Abbrev Number: 2 (DW_TAG_subprogram)");
291 info.WriteStrp(dwarf::DW_AT_name, "Foo", &debug_str_data_);
292 DW_CHECK_NEXT("DW_AT_name : (indirect string, offset: 0xe): Foo");
293 info.WriteAddr(dwarf::DW_AT_low_pc, 0x01010000);
294 DW_CHECK_NEXT("DW_AT_low_pc : 0x1010000");
295 info.WriteAddr(dwarf::DW_AT_high_pc, 0x01020000);
296 DW_CHECK_NEXT("DW_AT_high_pc : 0x1020000");
297 info.EndTag(); // DW_TAG_subprogram
298 info.StartTag(dwarf::DW_TAG_subprogram);
299 DW_CHECK("Abbrev Number: 2 (DW_TAG_subprogram)");
300 info.WriteStrp(dwarf::DW_AT_name, "Bar", &debug_str_data_);
301 DW_CHECK_NEXT("DW_AT_name : (indirect string, offset: 0x12): Bar");
302 info.WriteAddr(dwarf::DW_AT_low_pc, 0x01020000);
303 DW_CHECK_NEXT("DW_AT_low_pc : 0x1020000");
304 info.WriteAddr(dwarf::DW_AT_high_pc, 0x01030000);
305 DW_CHECK_NEXT("DW_AT_high_pc : 0x1030000");
306 info.EndTag(); // DW_TAG_subprogram
307 info.EndTag(); // DW_TAG_compile_unit
308 // Test that previous list was properly terminated and empty children.
309 info.StartTag(dwarf::DW_TAG_compile_unit);
310 info.EndTag(); // DW_TAG_compile_unit
311
312 // The abbrev table is just side product, but check it as well.
313 DW_CHECK("Abbrev Number: 3 (DW_TAG_compile_unit)");
314 DW_CHECK("Contents of the .debug_abbrev section:");
315 DW_CHECK("1 DW_TAG_compile_unit [has children]");
316 DW_CHECK_NEXT("DW_AT_producer DW_FORM_strp");
317 DW_CHECK_NEXT("DW_AT_low_pc DW_FORM_addr");
318 DW_CHECK_NEXT("DW_AT_high_pc DW_FORM_addr");
319 DW_CHECK("2 DW_TAG_subprogram [no children]");
320 DW_CHECK_NEXT("DW_AT_name DW_FORM_strp");
321 DW_CHECK_NEXT("DW_AT_low_pc DW_FORM_addr");
322 DW_CHECK_NEXT("DW_AT_high_pc DW_FORM_addr");
323 DW_CHECK("3 DW_TAG_compile_unit [no children]");
324
325 dwarf::WriteDebugInfoCU(/* debug_abbrev_offset= */ 0, info, &debug_info_data_);
326
327 CheckObjdumpOutput(is64bit, "-W");
328 }
329
330 #endif // ART_TARGET_ANDROID
331
332 } // namespace dwarf
333 } // namespace art
334