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 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(".debug_frame contents:");
45 DW_CHECK("FDE");
46 DW_CHECK_NEXT("DW_CFA_nop:"); // TODO: Why is a nop here.
47 int pc = 0;
48 for (int i : {0, 1, 0x3F, 0x40, 0xFF, 0x100, 0xFFFF, 0x10000}) {
49 pc += i;
50 opcodes.AdvancePC(pc);
51 }
52 DW_CHECK_NEXT("DW_CFA_advance_loc: 1");
53 DW_CHECK_NEXT("DW_CFA_advance_loc: 63");
54 DW_CHECK_NEXT("DW_CFA_advance_loc1: 64");
55 DW_CHECK_NEXT("DW_CFA_advance_loc1: 255");
56 DW_CHECK_NEXT("DW_CFA_advance_loc2: 256");
57 DW_CHECK_NEXT("DW_CFA_advance_loc2: 65535");
58 DW_CHECK_NEXT("DW_CFA_advance_loc4: 65536");
59 opcodes.DefCFA(reg, offset);
60 DW_CHECK_NEXT("DW_CFA_def_cfa: reg6 +40000");
61 opcodes.DefCFA(reg, -offset);
62 DW_CHECK_NEXT("DW_CFA_def_cfa_sf: reg6 -40000");
63 opcodes.DefCFARegister(reg);
64 DW_CHECK_NEXT("DW_CFA_def_cfa_register: reg6");
65 opcodes.DefCFAOffset(offset);
66 DW_CHECK_NEXT("DW_CFA_def_cfa_offset: +40000");
67 opcodes.DefCFAOffset(-offset);
68 DW_CHECK_NEXT("DW_CFA_def_cfa_offset_sf: -40000");
69 uint8_t expr[] = { /*nop*/ 0x96 };
70 opcodes.DefCFAExpression(expr, arraysize(expr));
71 DW_CHECK_NEXT("DW_CFA_def_cfa_expression: DW_OP_nop");
72 opcodes.Undefined(reg);
73 DW_CHECK_NEXT("DW_CFA_undefined: reg6");
74 opcodes.SameValue(reg);
75 DW_CHECK_NEXT("DW_CFA_same_value: reg6");
76 opcodes.Offset(Reg(0x3F), -offset);
77 DW_CHECK_NEXT("DW_CFA_offset: reg63 -40000");
78 opcodes.Offset(Reg(0x40), -offset);
79 DW_CHECK_NEXT("DW_CFA_offset_extended: reg64 -40000");
80 opcodes.Offset(Reg(0x40), offset);
81 DW_CHECK_NEXT("DW_CFA_offset_extended_sf: reg64 40000");
82 opcodes.ValOffset(reg, -offset);
83 DW_CHECK_NEXT("DW_CFA_val_offset: reg6 -40000");
84 opcodes.ValOffset(reg, offset);
85 DW_CHECK_NEXT("DW_CFA_val_offset_sf: reg6 40000");
86 opcodes.Register(reg, Reg(1));
87 DW_CHECK_NEXT("DW_CFA_register: reg6 reg1");
88 opcodes.Expression(reg, expr, arraysize(expr));
89 DW_CHECK_NEXT("DW_CFA_expression: reg6 DW_OP_nop");
90 opcodes.ValExpression(reg, expr, arraysize(expr));
91 DW_CHECK_NEXT("DW_CFA_val_expression: reg6 DW_OP_nop");
92 opcodes.Restore(Reg(0x3F));
93 DW_CHECK_NEXT("DW_CFA_restore: reg63");
94 opcodes.Restore(Reg(0x40));
95 DW_CHECK_NEXT("DW_CFA_restore_extended: reg64");
96 opcodes.Restore(reg);
97 DW_CHECK_NEXT("DW_CFA_restore: reg6");
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: reg4 +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: reg0 -108");
112 opcodes.RelOffset(Reg(1), 4); // push R1
113 DW_CHECK_NEXT("DW_CFA_offset: reg1 -104");
114 opcodes.RelOffsetForMany(Reg(2), 8, 1 | (1 << 3), 4); // push R2 and R5
115 DW_CHECK_NEXT("DW_CFA_offset: reg2 -100");
116 DW_CHECK_NEXT("DW_CFA_offset: reg5 -96");
117 opcodes.RestoreMany(Reg(2), 1 | (1 << 3)); // pop R2 and R5
118 DW_CHECK_NEXT("DW_CFA_restore: reg2");
119 DW_CHECK_NEXT("DW_CFA_restore: reg5");
120
121 DebugFrameOpCodeWriter<> initial_opcodes;
122 WriteCIE(is64bit, Reg(is64bit ? 16 : 8), initial_opcodes, &debug_frame_data_);
123 WriteFDE(is64bit,
124 /* cie_pointer= */ 0,
125 0x01000000,
126 0x01000000,
127 ArrayRef<const uint8_t>(*opcodes.data()),
128 &debug_frame_data_);
129
130 CheckObjdumpOutput(is64bit, "-debug-frame");
131 }
132
TEST_F(DwarfTest,DISABLED_DebugFrame64)133 TEST_F(DwarfTest, DISABLED_DebugFrame64) {
134 constexpr bool is64bit = true;
135 DebugFrameOpCodeWriter<> initial_opcodes;
136 WriteCIE(is64bit, Reg(16), initial_opcodes, &debug_frame_data_);
137 DebugFrameOpCodeWriter<> opcodes;
138 DW_CHECK(".debug_frame contents:");
139 WriteFDE(is64bit,
140 /* cie_pointer= */ 0,
141 0x0100000000000000,
142 0x0200000000000000,
143 ArrayRef<const uint8_t>(*opcodes.data()),
144 &debug_frame_data_);
145 DW_CHECK("FDE cie=00000000 pc=100000000000000..300000000000000");
146
147 CheckObjdumpOutput(is64bit, "-debug-frame");
148 }
149
150 // Test x86_64 register mapping. It is the only non-trivial architecture.
151 // ARM and X86 have: dwarf_reg = art_reg + constant.
TEST_F(DwarfTest,x86_64_RegisterMapping)152 TEST_F(DwarfTest, x86_64_RegisterMapping) {
153 constexpr bool is64bit = true;
154 DebugFrameOpCodeWriter<> opcodes;
155 DW_CHECK(".debug_frame contents:");
156 for (int i = 0; i < 16; i++) {
157 opcodes.RelOffset(Reg::X86_64Core(i), 0);
158 }
159 DW_CHECK("FDE");
160 DW_CHECK_NEXT("DW_CFA_nop:"); // TODO: Why is a nop here.
161 DW_CHECK_NEXT("DW_CFA_offset: reg0 0");
162 DW_CHECK_NEXT("DW_CFA_offset: reg2 0");
163 DW_CHECK_NEXT("DW_CFA_offset: reg1 0");
164 DW_CHECK_NEXT("DW_CFA_offset: reg3 0");
165 DW_CHECK_NEXT("DW_CFA_offset: reg7 0");
166 DW_CHECK_NEXT("DW_CFA_offset: reg6 0");
167 DW_CHECK_NEXT("DW_CFA_offset: reg4 0");
168 DW_CHECK_NEXT("DW_CFA_offset: reg5 0");
169 DW_CHECK_NEXT("DW_CFA_offset: reg8 0");
170 DW_CHECK_NEXT("DW_CFA_offset: reg9 0");
171 DW_CHECK_NEXT("DW_CFA_offset: reg10 0");
172 DW_CHECK_NEXT("DW_CFA_offset: reg11 0");
173 DW_CHECK_NEXT("DW_CFA_offset: reg12 0");
174 DW_CHECK_NEXT("DW_CFA_offset: reg13 0");
175 DW_CHECK_NEXT("DW_CFA_offset: reg14 0");
176 DW_CHECK_NEXT("DW_CFA_offset: reg15 0");
177
178 DebugFrameOpCodeWriter<> initial_opcodes;
179 WriteCIE(is64bit, Reg(16), initial_opcodes, &debug_frame_data_);
180 WriteFDE(is64bit,
181 /* cie_pointer= */ 0,
182 0x0100000000000000,
183 0x0200000000000000,
184 ArrayRef<const uint8_t>(*opcodes.data()),
185 &debug_frame_data_);
186
187 CheckObjdumpOutput(is64bit, "-debug-frame");
188 }
189
TEST_F(DwarfTest,DebugLine)190 TEST_F(DwarfTest, DebugLine) {
191 const bool is64bit = false;
192 const int code_factor_bits = 1;
193 DebugLineOpCodeWriter<> opcodes(is64bit, code_factor_bits);
194 DW_CHECK(".debug_line contents:");
195
196 std::vector<std::string> include_directories;
197 include_directories.push_back("/path/to/source");
198 DW_CHECK("include_directories[ 1] = \"/path/to/source\"");
199
200 std::vector<FileEntry> files {
201 { "file0.c", 0, 1000, 2000 },
202 { "file1.c", 1, 1000, 2000 },
203 { "file2.c", 1, 1000, 2000 },
204 };
205 DW_CHECK_NEXT("file_names[ 1]:");
206 DW_CHECK_NEXT(" name: \"file0.c\"");
207 DW_CHECK_NEXT(" dir_index: 0");
208 DW_CHECK_NEXT(" mod_time: 0x000003e8");
209 DW_CHECK_NEXT(" length: 0x000007d0");
210 DW_CHECK_NEXT("file_names[ 2]:");
211 DW_CHECK_NEXT(" name: \"file1.c\"");
212 DW_CHECK_NEXT(" dir_index: 1");
213 DW_CHECK_NEXT(" mod_time: 0x000003e8");
214 DW_CHECK_NEXT(" length: 0x000007d0");
215 DW_CHECK_NEXT("file_names[ 3]:");
216 DW_CHECK_NEXT(" name: \"file2.c\"");
217 DW_CHECK_NEXT(" dir_index: 1");
218 DW_CHECK_NEXT(" mod_time: 0x000003e8");
219 DW_CHECK_NEXT(" length: 0x000007d0");
220 DW_CHECK_NEXT("file_names[ 4]:");
221 DW_CHECK_NEXT(" name: \"file.c\"");
222 DW_CHECK_NEXT(" dir_index: 0");
223 DW_CHECK_NEXT(" mod_time: 0x000003e8");
224 DW_CHECK_NEXT(" length: 0x000007d0");
225
226 opcodes.SetAddress(0x01000000);
227 opcodes.SetIsStmt(true);
228 opcodes.AddRow();
229 opcodes.AdvancePC(0x01000100);
230 opcodes.SetFile(2);
231 opcodes.AdvanceLine(3);
232 opcodes.SetColumn(4);
233 opcodes.SetIsStmt(false);
234 opcodes.SetBasicBlock();
235 opcodes.SetPrologueEnd();
236 opcodes.SetEpilogueBegin();
237 opcodes.SetISA(5);
238 opcodes.EndSequence();
239 opcodes.DefineFile("file.c", 0, 1000, 2000);
240 DW_CHECK_NEXT("Address Line Column File ISA Discriminator Flags");
241 DW_CHECK_NEXT("------------------ ------ ------ ------ --- ------------- -------------");
242 DW_CHECK_NEXT("0x0000000001000000 1 0 1 0 0 is_stmt");
243 DW_CHECK_NEXT("0x0000000001000100 3 4 2 5 0 basic_block prologue_end epilogue_begin end_sequence");
244
245 WriteDebugLineTable(include_directories, files, opcodes, &debug_line_data_);
246
247 CheckObjdumpOutput(is64bit, "-debug-line");
248 }
249
250 // DWARF has special one byte codes which advance PC and line at the same time.
TEST_F(DwarfTest,DebugLineSpecialOpcodes)251 TEST_F(DwarfTest, DebugLineSpecialOpcodes) {
252 const bool is64bit = false;
253 const int code_factor_bits = 1;
254 uint32_t pc = 0x01000000;
255 int line = 1;
256 DebugLineOpCodeWriter<> opcodes(is64bit, code_factor_bits);
257 opcodes.SetAddress(pc);
258 size_t num_rows = 0;
259 DW_CHECK(".debug_line contents:");
260 DW_CHECK("file_names[ 1]:");
261 DW_CHECK(" name: \"file.c\"");
262 DW_CHECK("Address Line Column File ISA Discriminator Flags");
263 DW_CHECK("------------------ ------ ------ ------ --- ------------- -------------");
264 for (int addr_delta = 0; addr_delta < 80; addr_delta += 2) {
265 for (int line_delta = 16; line_delta >= -16; --line_delta) {
266 pc += addr_delta;
267 line += line_delta;
268 opcodes.AddRow(pc, line);
269 num_rows++;
270 ASSERT_EQ(opcodes.CurrentAddress(), pc);
271 ASSERT_EQ(opcodes.CurrentLine(), line);
272 char expected[1024];
273 sprintf(expected, "0x%016x %6i 0 1 0 0", pc, line);
274 DW_CHECK_NEXT(expected);
275 }
276 }
277 opcodes.EndSequence();
278 EXPECT_LT(opcodes.data()->size(), num_rows * 3);
279
280 std::vector<std::string> directories;
281 std::vector<FileEntry> files = { { "file.c", 0, 1000, 2000 } };
282 WriteDebugLineTable(directories, files, opcodes, &debug_line_data_);
283
284 CheckObjdumpOutput(is64bit, "-debug-line");
285 }
286
TEST_F(DwarfTest,DebugInfo)287 TEST_F(DwarfTest, DebugInfo) {
288 constexpr bool is64bit = false;
289
290 DebugAbbrevWriter<> debug_abbrev(&debug_abbrev_data_);
291 DW_CHECK(".debug_abbrev contents:");
292 DW_CHECK_NEXT("Abbrev table for offset: 0x00000000");
293 DW_CHECK_NEXT("[1] DW_TAG_compile_unit DW_CHILDREN_yes");
294 DW_CHECK_NEXT(" DW_AT_producer DW_FORM_strp");
295 DW_CHECK_NEXT(" DW_AT_low_pc DW_FORM_addr");
296 DW_CHECK_NEXT(" DW_AT_high_pc DW_FORM_addr");
297 DW_CHECK_NEXT("[2] DW_TAG_subprogram DW_CHILDREN_no");
298 DW_CHECK_NEXT(" DW_AT_name DW_FORM_strp");
299 DW_CHECK_NEXT(" DW_AT_low_pc DW_FORM_addr");
300 DW_CHECK_NEXT(" DW_AT_high_pc DW_FORM_addr");
301 DW_CHECK_NEXT("[3] DW_TAG_compile_unit DW_CHILDREN_no");
302
303 DebugInfoEntryWriter<> info(is64bit, &debug_abbrev);
304 DW_CHECK(".debug_info contents:");
305 info.StartTag(dwarf::DW_TAG_compile_unit);
306 DW_CHECK_NEXT("Compile Unit: length = 0x00000030 version = 0x0004 abbr_offset = 0x0000 addr_size = 0x04");
307 DW_CHECK_NEXT("DW_TAG_compile_unit");
308 info.WriteStrp(dwarf::DW_AT_producer, "Compiler name", &debug_str_data_);
309 DW_CHECK_NEXT(" DW_AT_producer (\"Compiler name\")");
310 info.WriteAddr(dwarf::DW_AT_low_pc, 0x01000000);
311 DW_CHECK_NEXT(" DW_AT_low_pc (0x0000000001000000)");
312 info.WriteAddr(dwarf::DW_AT_high_pc, 0x02000000);
313 DW_CHECK_NEXT(" DW_AT_high_pc (0x0000000002000000)");
314 info.StartTag(dwarf::DW_TAG_subprogram);
315 DW_CHECK_NEXT(" DW_TAG_subprogram");
316 info.WriteStrp(dwarf::DW_AT_name, "Foo", &debug_str_data_);
317 DW_CHECK_NEXT(" DW_AT_name (\"Foo\")");
318 info.WriteAddr(dwarf::DW_AT_low_pc, 0x01010000);
319 DW_CHECK_NEXT(" DW_AT_low_pc (0x0000000001010000)");
320 info.WriteAddr(dwarf::DW_AT_high_pc, 0x01020000);
321 DW_CHECK_NEXT(" DW_AT_high_pc (0x0000000001020000)");
322 info.EndTag(); // DW_TAG_subprogram
323 info.StartTag(dwarf::DW_TAG_subprogram);
324 DW_CHECK_NEXT(" DW_TAG_subprogram");
325 info.WriteStrp(dwarf::DW_AT_name, "Bar", &debug_str_data_);
326 DW_CHECK_NEXT(" DW_AT_name (\"Bar\")");
327 info.WriteAddr(dwarf::DW_AT_low_pc, 0x01020000);
328 DW_CHECK_NEXT(" DW_AT_low_pc (0x0000000001020000)");
329 info.WriteAddr(dwarf::DW_AT_high_pc, 0x01030000);
330 DW_CHECK_NEXT(" DW_AT_high_pc (0x0000000001030000)");
331 info.EndTag(); // DW_TAG_subprogram
332 info.EndTag(); // DW_TAG_compile_unit
333 DW_CHECK_NEXT(" NULL");
334 // Test that previous list was properly terminated and empty children.
335 info.StartTag(dwarf::DW_TAG_compile_unit);
336 info.EndTag(); // DW_TAG_compile_unit
337
338 dwarf::WriteDebugInfoCU(/* debug_abbrev_offset= */ 0, info, &debug_info_data_);
339
340 CheckObjdumpOutput(is64bit, "-debug-info -debug-abbrev");
341 }
342
343 #endif // ART_TARGET_ANDROID
344
345 } // namespace dwarf
346 } // namespace art
347