blob: ec18e96b4b6dfb099068e678c91d40cc1626c10a [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
19#include "dwarf/debug_frame_opcode_writer.h"
David Srbeckyb5362472015-04-08 19:37:39 +010020#include "dwarf/debug_info_entry_writer.h"
David Srbecky15c19752015-03-31 14:53:55 +000021#include "dwarf/debug_line_opcode_writer.h"
David Srbeckyb5362472015-04-08 19:37:39 +010022#include "dwarf/headers.h"
David Srbecky15c19752015-03-31 14:53:55 +000023#include "gtest/gtest.h"
24
25namespace art {
26namespace dwarf {
27
28// Run the tests only on host since we need objdump.
29#ifndef HAVE_ANDROID_OS
30
31TEST_F(DwarfTest, DebugFrame) {
32 const bool is64bit = false;
33
34 // Pick offset value which would catch Uleb vs Sleb errors.
35 const int offset = 40000;
36 ASSERT_EQ(UnsignedLeb128Size(offset / 4), 2u);
37 ASSERT_EQ(SignedLeb128Size(offset / 4), 3u);
38 DW_CHECK("Data alignment factor: -4");
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("FDE");
45 int pc = 0;
46 for (int i : {0, 1, 0x3F, 0x40, 0xFF, 0x100, 0xFFFF, 0x10000}) {
47 pc += i;
48 opcodes.AdvancePC(pc);
49 }
50 DW_CHECK_NEXT("DW_CFA_advance_loc: 1 to 01000001");
51 DW_CHECK_NEXT("DW_CFA_advance_loc: 63 to 01000040");
52 DW_CHECK_NEXT("DW_CFA_advance_loc1: 64 to 01000080");
53 DW_CHECK_NEXT("DW_CFA_advance_loc1: 255 to 0100017f");
54 DW_CHECK_NEXT("DW_CFA_advance_loc2: 256 to 0100027f");
55 DW_CHECK_NEXT("DW_CFA_advance_loc2: 65535 to 0101027e");
56 DW_CHECK_NEXT("DW_CFA_advance_loc4: 65536 to 0102027e");
57 opcodes.DefCFA(reg, offset);
58 DW_CHECK_NEXT("DW_CFA_def_cfa: r6 (esi) ofs 40000");
59 opcodes.DefCFA(reg, -offset);
60 DW_CHECK_NEXT("DW_CFA_def_cfa_sf: r6 (esi) ofs -40000");
61 opcodes.DefCFARegister(reg);
62 DW_CHECK_NEXT("DW_CFA_def_cfa_register: r6 (esi)");
63 opcodes.DefCFAOffset(offset);
64 DW_CHECK_NEXT("DW_CFA_def_cfa_offset: 40000");
65 opcodes.DefCFAOffset(-offset);
66 DW_CHECK_NEXT("DW_CFA_def_cfa_offset_sf: -40000");
67 uint8_t expr[] = { 0 };
68 opcodes.DefCFAExpression(expr, arraysize(expr));
69 DW_CHECK_NEXT("DW_CFA_def_cfa_expression");
70 opcodes.Undefined(reg);
71 DW_CHECK_NEXT("DW_CFA_undefined: r6 (esi)");
72 opcodes.SameValue(reg);
73 DW_CHECK_NEXT("DW_CFA_same_value: r6 (esi)");
74 opcodes.Offset(Reg(0x3F), -offset);
75 // Bad register likely means that it does not exist on x86,
76 // but we want to test high register numbers anyway.
77 DW_CHECK_NEXT("DW_CFA_offset: bad register: r63 at cfa-40000");
78 opcodes.Offset(Reg(0x40), -offset);
79 DW_CHECK_NEXT("DW_CFA_offset_extended: bad register: r64 at cfa-40000");
80 opcodes.Offset(Reg(0x40), offset);
81 DW_CHECK_NEXT("DW_CFA_offset_extended_sf: bad register: r64 at cfa+40000");
82 opcodes.ValOffset(reg, -offset);
83 DW_CHECK_NEXT("DW_CFA_val_offset: r6 (esi) at cfa-40000");
84 opcodes.ValOffset(reg, offset);
85 DW_CHECK_NEXT("DW_CFA_val_offset_sf: r6 (esi) at cfa+40000");
86 opcodes.Register(reg, Reg(1));
87 DW_CHECK_NEXT("DW_CFA_register: r6 (esi) in r1 (ecx)");
88 opcodes.Expression(reg, expr, arraysize(expr));
89 DW_CHECK_NEXT("DW_CFA_expression: r6 (esi)");
90 opcodes.ValExpression(reg, expr, arraysize(expr));
91 DW_CHECK_NEXT("DW_CFA_val_expression: r6 (esi)");
92 opcodes.Restore(Reg(0x3F));
93 DW_CHECK_NEXT("DW_CFA_restore: bad register: r63");
94 opcodes.Restore(Reg(0x40));
95 DW_CHECK_NEXT("DW_CFA_restore_extended: bad register: r64");
96 opcodes.Restore(reg);
97 DW_CHECK_NEXT("DW_CFA_restore: r6 (esi)");
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: r4 (esp) ofs 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: r0 (eax) at cfa-108");
112 opcodes.RelOffset(Reg(1), 4); // push R1
113 DW_CHECK_NEXT("DW_CFA_offset: r1 (ecx) at cfa-104");
114 opcodes.RelOffsetForMany(Reg(2), 8, 1 | (1 << 3), 4); // push R2 and R5
115 DW_CHECK_NEXT("DW_CFA_offset: r2 (edx) at cfa-100");
116 DW_CHECK_NEXT("DW_CFA_offset: r5 (ebp) at cfa-96");
117 opcodes.RestoreMany(Reg(2), 1 | (1 << 3)); // pop R2 and R5
118 DW_CHECK_NEXT("DW_CFA_restore: r2 (edx)");
119 DW_CHECK_NEXT("DW_CFA_restore: r5 (ebp)");
120
David Srbecky15c19752015-03-31 14:53:55 +0000121 DebugFrameOpCodeWriter<> initial_opcodes;
David Srbeckyb5362472015-04-08 19:37:39 +0100122 WriteEhFrameCIE(is64bit, Reg(is64bit ? 16 : 8), initial_opcodes, &eh_frame_data_);
123 WriteEhFrameFDE(is64bit, 0, 0x01000000, 0x01000000, opcodes.data(), &eh_frame_data_);
David Srbecky15c19752015-03-31 14:53:55 +0000124 CheckObjdumpOutput(is64bit, "-W");
125}
126
David Srbecky1109fb32015-04-07 20:21:06 +0100127// TODO: objdump seems to have trouble with 64bit CIE length.
128TEST_F(DwarfTest, DISABLED_DebugFrame64) {
David Srbeckyb5362472015-04-08 19:37:39 +0100129 constexpr bool is64bit = true;
130 DebugFrameOpCodeWriter<> initial_opcodes;
131 WriteEhFrameCIE(is64bit, Reg(16), initial_opcodes, &eh_frame_data_);
132 DebugFrameOpCodeWriter<> opcodes;
133 WriteEhFrameFDE(is64bit, 0, 0x0100000000000000, 0x0200000000000000,
134 opcodes.data(), &eh_frame_data_);
David Srbecky15c19752015-03-31 14:53:55 +0000135 DW_CHECK("FDE cie=00000000 pc=100000000000000..300000000000000");
136 CheckObjdumpOutput(is64bit, "-W");
137}
138
139TEST_F(DwarfTest, DebugLine) {
140 const bool is64bit = false;
141 const int code_factor_bits = 1;
142 DebugLineOpCodeWriter<> opcodes(is64bit, code_factor_bits);
143
144 std::vector<std::string> include_directories;
145 include_directories.push_back("/path/to/source");
146 DW_CHECK("/path/to/source");
147
David Srbeckyb5362472015-04-08 19:37:39 +0100148 std::vector<FileEntry> files {
David Srbecky15c19752015-03-31 14:53:55 +0000149 { "file0.c", 0, 1000, 2000 },
150 { "file1.c", 1, 1000, 2000 },
151 { "file2.c", 1, 1000, 2000 },
152 };
153 DW_CHECK("1\t0\t1000\t2000\tfile0.c");
154 DW_CHECK_NEXT("2\t1\t1000\t2000\tfile1.c");
155 DW_CHECK_NEXT("3\t1\t1000\t2000\tfile2.c");
156
157 DW_CHECK("Line Number Statements");
158 opcodes.SetAddress(0x01000000);
159 DW_CHECK_NEXT("Extended opcode 2: set Address to 0x1000000");
160 opcodes.AddRow();
161 DW_CHECK_NEXT("Copy");
162 opcodes.AdvancePC(0x01000100);
163 DW_CHECK_NEXT("Advance PC by 256 to 0x1000100");
164 opcodes.SetFile(2);
165 DW_CHECK_NEXT("Set File Name to entry 2 in the File Name Table");
166 opcodes.AdvanceLine(3);
167 DW_CHECK_NEXT("Advance Line by 2 to 3");
168 opcodes.SetColumn(4);
169 DW_CHECK_NEXT("Set column to 4");
170 opcodes.NegateStmt();
171 DW_CHECK_NEXT("Set is_stmt to 0");
172 opcodes.SetBasicBlock();
173 DW_CHECK_NEXT("Set basic block");
174 opcodes.SetPrologueEnd();
175 DW_CHECK_NEXT("Set prologue_end to true");
176 opcodes.SetEpilogueBegin();
177 DW_CHECK_NEXT("Set epilogue_begin to true");
178 opcodes.SetISA(5);
179 DW_CHECK_NEXT("Set ISA to 5");
180 opcodes.EndSequence();
181 DW_CHECK_NEXT("Extended opcode 1: End of Sequence");
182 opcodes.DefineFile("file.c", 0, 1000, 2000);
183 DW_CHECK_NEXT("Extended opcode 3: define new File Table entry");
184 DW_CHECK_NEXT("Entry\tDir\tTime\tSize\tName");
185 DW_CHECK_NEXT("1\t0\t1000\t2000\tfile.c");
186
David Srbeckyb5362472015-04-08 19:37:39 +0100187 WriteDebugLineTable(include_directories, files, opcodes, &debug_line_data_);
David Srbecky15c19752015-03-31 14:53:55 +0000188 CheckObjdumpOutput(is64bit, "-W");
189}
190
191// DWARF has special one byte codes which advance PC and line at the same time.
192TEST_F(DwarfTest, DebugLineSpecialOpcodes) {
193 const bool is64bit = false;
194 const int code_factor_bits = 1;
195 uint32_t pc = 0x01000000;
196 int line = 1;
197 DebugLineOpCodeWriter<> opcodes(is64bit, code_factor_bits);
198 opcodes.SetAddress(pc);
199 size_t num_rows = 0;
200 DW_CHECK("Line Number Statements:");
201 DW_CHECK("Special opcode");
202 DW_CHECK("Advance PC by constant");
203 DW_CHECK("Decoded dump of debug contents of section .debug_line:");
204 DW_CHECK("Line number Starting address");
205 for (int addr_delta = 0; addr_delta < 80; addr_delta += 2) {
206 for (int line_delta = 16; line_delta >= -16; --line_delta) {
207 pc += addr_delta;
208 line += line_delta;
209 opcodes.AddRow(pc, line);
210 num_rows++;
211 ASSERT_EQ(opcodes.CurrentAddress(), pc);
212 ASSERT_EQ(opcodes.CurrentLine(), line);
213 char expected[1024];
214 sprintf(expected, "%i 0x%x", line, pc);
215 DW_CHECK_NEXT(expected);
216 }
217 }
218 EXPECT_LT(opcodes.data()->size(), num_rows * 3);
219
220 std::vector<std::string> directories;
David Srbeckyb5362472015-04-08 19:37:39 +0100221 std::vector<FileEntry> files { { "file.c", 0, 1000, 2000 } }; // NOLINT
222 WriteDebugLineTable(directories, files, opcodes, &debug_line_data_);
David Srbecky15c19752015-03-31 14:53:55 +0000223 CheckObjdumpOutput(is64bit, "-W -WL");
224}
225
David Srbeckyb5362472015-04-08 19:37:39 +0100226TEST_F(DwarfTest, DebugInfo) {
227 constexpr bool is64bit = false;
228 DebugInfoEntryWriter<> info(is64bit, &debug_abbrev_data_);
229 DW_CHECK("Contents of the .debug_info section:");
230 info.StartTag(dwarf::DW_TAG_compile_unit, dwarf::DW_CHILDREN_yes);
231 DW_CHECK("Abbrev Number: 1 (DW_TAG_compile_unit)");
232 info.WriteStrp(dwarf::DW_AT_producer, "Compiler name", &debug_str_data_);
233 DW_CHECK_NEXT("DW_AT_producer : (indirect string, offset: 0x0): Compiler name");
234 info.WriteAddr(dwarf::DW_AT_low_pc, 0x01000000);
235 DW_CHECK_NEXT("DW_AT_low_pc : 0x1000000");
236 info.WriteAddr(dwarf::DW_AT_high_pc, 0x02000000);
237 DW_CHECK_NEXT("DW_AT_high_pc : 0x2000000");
238 info.StartTag(dwarf::DW_TAG_subprogram, dwarf::DW_CHILDREN_no);
239 DW_CHECK("Abbrev Number: 2 (DW_TAG_subprogram)");
240 info.WriteStrp(dwarf::DW_AT_name, "Foo", &debug_str_data_);
241 DW_CHECK_NEXT("DW_AT_name : (indirect string, offset: 0xe): Foo");
242 info.WriteAddr(dwarf::DW_AT_low_pc, 0x01010000);
243 DW_CHECK_NEXT("DW_AT_low_pc : 0x1010000");
244 info.WriteAddr(dwarf::DW_AT_high_pc, 0x01020000);
245 DW_CHECK_NEXT("DW_AT_high_pc : 0x1020000");
246 info.EndTag(); // DW_TAG_subprogram
247 info.StartTag(dwarf::DW_TAG_subprogram, dwarf::DW_CHILDREN_no);
248 DW_CHECK("Abbrev Number: 2 (DW_TAG_subprogram)");
249 info.WriteStrp(dwarf::DW_AT_name, "Bar", &debug_str_data_);
250 DW_CHECK_NEXT("DW_AT_name : (indirect string, offset: 0x12): Bar");
251 info.WriteAddr(dwarf::DW_AT_low_pc, 0x01020000);
252 DW_CHECK_NEXT("DW_AT_low_pc : 0x1020000");
253 info.WriteAddr(dwarf::DW_AT_high_pc, 0x01030000);
254 DW_CHECK_NEXT("DW_AT_high_pc : 0x1030000");
255 info.EndTag(); // DW_TAG_subprogram
256 info.EndTag(); // DW_TAG_compile_unit
257 // Test that previous list was properly terminated and empty children.
258 info.StartTag(dwarf::DW_TAG_compile_unit, dwarf::DW_CHILDREN_yes);
259 info.EndTag(); // DW_TAG_compile_unit
260
261 // The abbrev table is just side product, but check it as well.
262 DW_CHECK("Abbrev Number: 3 (DW_TAG_compile_unit)");
263 DW_CHECK("Contents of the .debug_abbrev section:");
264 DW_CHECK("1 DW_TAG_compile_unit [has children]");
265 DW_CHECK_NEXT("DW_AT_producer DW_FORM_strp");
266 DW_CHECK_NEXT("DW_AT_low_pc DW_FORM_addr");
267 DW_CHECK_NEXT("DW_AT_high_pc DW_FORM_addr");
268 DW_CHECK("2 DW_TAG_subprogram [no children]");
269 DW_CHECK_NEXT("DW_AT_name DW_FORM_strp");
270 DW_CHECK_NEXT("DW_AT_low_pc DW_FORM_addr");
271 DW_CHECK_NEXT("DW_AT_high_pc DW_FORM_addr");
272 DW_CHECK("3 DW_TAG_compile_unit [has children]");
273
274 dwarf::WriteDebugInfoCU(0 /* debug_abbrev_offset */, info, &debug_info_data_);
275 CheckObjdumpOutput(is64bit, "-W");
276}
277
David Srbecky15c19752015-03-31 14:53:55 +0000278#endif // HAVE_ANDROID_OS
279
280} // namespace dwarf
281} // namespace art