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/dwarf_constants.h" 20 #include "dwarf/debug_frame_opcode_writer.h" 21 #include "dwarf/debug_info_entry_writer.h" 22 #include "dwarf/debug_line_opcode_writer.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 HAVE_ANDROID_OS 31 32 constexpr CFIFormat kCFIFormat = DW_DEBUG_FRAME_FORMAT; 33 34 TEST_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 124 DebugFrameOpCodeWriter<> initial_opcodes; 125 WriteDebugFrameCIE(is64bit, DW_EH_PE_absptr, Reg(is64bit ? 16 : 8), 126 initial_opcodes, kCFIFormat, &debug_frame_data_); 127 std::vector<uintptr_t> debug_frame_patches; 128 std::vector<uintptr_t> expected_patches { 28 }; // NOLINT 129 WriteDebugFrameFDE(is64bit, 0, 0x01000000, 0x01000000, opcodes.data(), 130 kCFIFormat, &debug_frame_data_, &debug_frame_patches); 131 132 EXPECT_EQ(expected_patches, debug_frame_patches); 133 CheckObjdumpOutput(is64bit, "-W"); 134 } 135 136 TEST_F(DwarfTest, DebugFrame64) { 137 constexpr bool is64bit = true; 138 DebugFrameOpCodeWriter<> initial_opcodes; 139 WriteDebugFrameCIE(is64bit, DW_EH_PE_absptr, Reg(16), 140 initial_opcodes, kCFIFormat, &debug_frame_data_); 141 DebugFrameOpCodeWriter<> opcodes; 142 std::vector<uintptr_t> debug_frame_patches; 143 std::vector<uintptr_t> expected_patches { 32 }; // NOLINT 144 WriteDebugFrameFDE(is64bit, 0, 0x0100000000000000, 0x0200000000000000, 145 opcodes.data(), kCFIFormat, &debug_frame_data_, &debug_frame_patches); 146 DW_CHECK("FDE cie=00000000 pc=100000000000000..300000000000000"); 147 148 EXPECT_EQ(expected_patches, debug_frame_patches); 149 CheckObjdumpOutput(is64bit, "-W"); 150 } 151 152 // Test x86_64 register mapping. It is the only non-trivial architecture. 153 // ARM, X86, and Mips have: dwarf_reg = art_reg + constant. 154 TEST_F(DwarfTest, x86_64_RegisterMapping) { 155 constexpr bool is64bit = true; 156 DebugFrameOpCodeWriter<> opcodes; 157 for (int i = 0; i < 16; i++) { 158 opcodes.RelOffset(Reg::X86_64Core(i), 0); 159 } 160 DW_CHECK("FDE"); 161 DW_CHECK_NEXT("DW_CFA_offset: r0 (rax)"); 162 DW_CHECK_NEXT("DW_CFA_offset: r2 (rcx)"); 163 DW_CHECK_NEXT("DW_CFA_offset: r1 (rdx)"); 164 DW_CHECK_NEXT("DW_CFA_offset: r3 (rbx)"); 165 DW_CHECK_NEXT("DW_CFA_offset: r7 (rsp)"); 166 DW_CHECK_NEXT("DW_CFA_offset: r6 (rbp)"); 167 DW_CHECK_NEXT("DW_CFA_offset: r4 (rsi)"); 168 DW_CHECK_NEXT("DW_CFA_offset: r5 (rdi)"); 169 DW_CHECK_NEXT("DW_CFA_offset: r8 (r8)"); 170 DW_CHECK_NEXT("DW_CFA_offset: r9 (r9)"); 171 DW_CHECK_NEXT("DW_CFA_offset: r10 (r10)"); 172 DW_CHECK_NEXT("DW_CFA_offset: r11 (r11)"); 173 DW_CHECK_NEXT("DW_CFA_offset: r12 (r12)"); 174 DW_CHECK_NEXT("DW_CFA_offset: r13 (r13)"); 175 DW_CHECK_NEXT("DW_CFA_offset: r14 (r14)"); 176 DW_CHECK_NEXT("DW_CFA_offset: r15 (r15)"); 177 DebugFrameOpCodeWriter<> initial_opcodes; 178 WriteDebugFrameCIE(is64bit, DW_EH_PE_absptr, Reg(16), 179 initial_opcodes, kCFIFormat, &debug_frame_data_); 180 std::vector<uintptr_t> debug_frame_patches; 181 WriteDebugFrameFDE(is64bit, 0, 0x0100000000000000, 0x0200000000000000, 182 opcodes.data(), kCFIFormat, &debug_frame_data_, &debug_frame_patches); 183 184 CheckObjdumpOutput(is64bit, "-W"); 185 } 186 187 TEST_F(DwarfTest, DebugLine) { 188 const bool is64bit = false; 189 const int code_factor_bits = 1; 190 DebugLineOpCodeWriter<> opcodes(is64bit, code_factor_bits); 191 192 std::vector<std::string> include_directories; 193 include_directories.push_back("/path/to/source"); 194 DW_CHECK("/path/to/source"); 195 196 std::vector<FileEntry> files { 197 { "file0.c", 0, 1000, 2000 }, 198 { "file1.c", 1, 1000, 2000 }, 199 { "file2.c", 1, 1000, 2000 }, 200 }; 201 DW_CHECK("1\t0\t1000\t2000\tfile0.c"); 202 DW_CHECK_NEXT("2\t1\t1000\t2000\tfile1.c"); 203 DW_CHECK_NEXT("3\t1\t1000\t2000\tfile2.c"); 204 205 DW_CHECK("Line Number Statements"); 206 opcodes.SetAddress(0x01000000); 207 DW_CHECK_NEXT("Extended opcode 2: set Address to 0x1000000"); 208 opcodes.AddRow(); 209 DW_CHECK_NEXT("Copy"); 210 opcodes.AdvancePC(0x01000100); 211 DW_CHECK_NEXT("Advance PC by 256 to 0x1000100"); 212 opcodes.SetFile(2); 213 DW_CHECK_NEXT("Set File Name to entry 2 in the File Name Table"); 214 opcodes.AdvanceLine(3); 215 DW_CHECK_NEXT("Advance Line by 2 to 3"); 216 opcodes.SetColumn(4); 217 DW_CHECK_NEXT("Set column to 4"); 218 opcodes.NegateStmt(); 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 std::vector<uintptr_t> debug_line_patches; 236 std::vector<uintptr_t> expected_patches { 87 }; // NOLINT 237 WriteDebugLineTable(include_directories, files, opcodes, 238 &debug_line_data_, &debug_line_patches); 239 240 EXPECT_EQ(expected_patches, debug_line_patches); 241 CheckObjdumpOutput(is64bit, "-W"); 242 } 243 244 // DWARF has special one byte codes which advance PC and line at the same time. 245 TEST_F(DwarfTest, DebugLineSpecialOpcodes) { 246 const bool is64bit = false; 247 const int code_factor_bits = 1; 248 uint32_t pc = 0x01000000; 249 int line = 1; 250 DebugLineOpCodeWriter<> opcodes(is64bit, code_factor_bits); 251 opcodes.SetAddress(pc); 252 size_t num_rows = 0; 253 DW_CHECK("Line Number Statements:"); 254 DW_CHECK("Special opcode"); 255 DW_CHECK("Advance PC by constant"); 256 DW_CHECK("Decoded dump of debug contents of section .debug_line:"); 257 DW_CHECK("Line number Starting address"); 258 for (int addr_delta = 0; addr_delta < 80; addr_delta += 2) { 259 for (int line_delta = 16; line_delta >= -16; --line_delta) { 260 pc += addr_delta; 261 line += line_delta; 262 opcodes.AddRow(pc, line); 263 num_rows++; 264 ASSERT_EQ(opcodes.CurrentAddress(), pc); 265 ASSERT_EQ(opcodes.CurrentLine(), line); 266 char expected[1024]; 267 sprintf(expected, "%i 0x%x", line, pc); 268 DW_CHECK_NEXT(expected); 269 } 270 } 271 EXPECT_LT(opcodes.data()->size(), num_rows * 3); 272 273 std::vector<std::string> directories; 274 std::vector<FileEntry> files { { "file.c", 0, 1000, 2000 } }; // NOLINT 275 std::vector<uintptr_t> debug_line_patches; 276 WriteDebugLineTable(directories, files, opcodes, 277 &debug_line_data_, &debug_line_patches); 278 279 CheckObjdumpOutput(is64bit, "-W -WL"); 280 } 281 282 TEST_F(DwarfTest, DebugInfo) { 283 constexpr bool is64bit = false; 284 DebugInfoEntryWriter<> info(is64bit, &debug_abbrev_data_); 285 DW_CHECK("Contents of the .debug_info section:"); 286 info.StartTag(dwarf::DW_TAG_compile_unit, dwarf::DW_CHILDREN_yes); 287 DW_CHECK("Abbrev Number: 1 (DW_TAG_compile_unit)"); 288 info.WriteStrp(dwarf::DW_AT_producer, "Compiler name", &debug_str_data_); 289 DW_CHECK_NEXT("DW_AT_producer : (indirect string, offset: 0x0): Compiler name"); 290 info.WriteAddr(dwarf::DW_AT_low_pc, 0x01000000); 291 DW_CHECK_NEXT("DW_AT_low_pc : 0x1000000"); 292 info.WriteAddr(dwarf::DW_AT_high_pc, 0x02000000); 293 DW_CHECK_NEXT("DW_AT_high_pc : 0x2000000"); 294 info.StartTag(dwarf::DW_TAG_subprogram, dwarf::DW_CHILDREN_no); 295 DW_CHECK("Abbrev Number: 2 (DW_TAG_subprogram)"); 296 info.WriteStrp(dwarf::DW_AT_name, "Foo", &debug_str_data_); 297 DW_CHECK_NEXT("DW_AT_name : (indirect string, offset: 0xe): Foo"); 298 info.WriteAddr(dwarf::DW_AT_low_pc, 0x01010000); 299 DW_CHECK_NEXT("DW_AT_low_pc : 0x1010000"); 300 info.WriteAddr(dwarf::DW_AT_high_pc, 0x01020000); 301 DW_CHECK_NEXT("DW_AT_high_pc : 0x1020000"); 302 info.EndTag(); // DW_TAG_subprogram 303 info.StartTag(dwarf::DW_TAG_subprogram, dwarf::DW_CHILDREN_no); 304 DW_CHECK("Abbrev Number: 2 (DW_TAG_subprogram)"); 305 info.WriteStrp(dwarf::DW_AT_name, "Bar", &debug_str_data_); 306 DW_CHECK_NEXT("DW_AT_name : (indirect string, offset: 0x12): Bar"); 307 info.WriteAddr(dwarf::DW_AT_low_pc, 0x01020000); 308 DW_CHECK_NEXT("DW_AT_low_pc : 0x1020000"); 309 info.WriteAddr(dwarf::DW_AT_high_pc, 0x01030000); 310 DW_CHECK_NEXT("DW_AT_high_pc : 0x1030000"); 311 info.EndTag(); // DW_TAG_subprogram 312 info.EndTag(); // DW_TAG_compile_unit 313 // Test that previous list was properly terminated and empty children. 314 info.StartTag(dwarf::DW_TAG_compile_unit, dwarf::DW_CHILDREN_yes); 315 info.EndTag(); // DW_TAG_compile_unit 316 317 // The abbrev table is just side product, but check it as well. 318 DW_CHECK("Abbrev Number: 3 (DW_TAG_compile_unit)"); 319 DW_CHECK("Contents of the .debug_abbrev section:"); 320 DW_CHECK("1 DW_TAG_compile_unit [has children]"); 321 DW_CHECK_NEXT("DW_AT_producer DW_FORM_strp"); 322 DW_CHECK_NEXT("DW_AT_low_pc DW_FORM_addr"); 323 DW_CHECK_NEXT("DW_AT_high_pc DW_FORM_addr"); 324 DW_CHECK("2 DW_TAG_subprogram [no children]"); 325 DW_CHECK_NEXT("DW_AT_name DW_FORM_strp"); 326 DW_CHECK_NEXT("DW_AT_low_pc DW_FORM_addr"); 327 DW_CHECK_NEXT("DW_AT_high_pc DW_FORM_addr"); 328 DW_CHECK("3 DW_TAG_compile_unit [has children]"); 329 330 std::vector<uintptr_t> debug_info_patches; 331 std::vector<uintptr_t> expected_patches { 16, 20, 29, 33, 42, 46 }; // NOLINT 332 dwarf::WriteDebugInfoCU(0 /* debug_abbrev_offset */, info, 333 &debug_info_data_, &debug_info_patches); 334 335 EXPECT_EQ(expected_patches, debug_info_patches); 336 CheckObjdumpOutput(is64bit, "-W"); 337 } 338 339 #endif // HAVE_ANDROID_OS 340 341 } // namespace dwarf 342 } // namespace art 343