blob: f3553bcc99d1b81e571dbdffad8df2a25bb77674 [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"
20#include "dwarf/debug_frame_writer.h"
21#include "dwarf/debug_line_opcode_writer.h"
22#include "dwarf/debug_line_writer.h"
23#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
121 DebugFrameWriter<> eh_frame(&eh_frame_data_, is64bit);
122 DebugFrameOpCodeWriter<> initial_opcodes;
123 eh_frame.WriteCIE(Reg(is64bit ? 16 : 8), // Return address register.
124 initial_opcodes); // Initial opcodes.
125 eh_frame.WriteFDE(0x01000000, 0x01000000,
126 opcodes.data()->data(), opcodes.data()->size());
127 CheckObjdumpOutput(is64bit, "-W");
128}
129
130TEST_F(DwarfTest, DebugFrame64) {
131 const bool is64bit = true;
132 DebugFrameWriter<> eh_frame(&eh_frame_data_, is64bit);
133 DebugFrameOpCodeWriter<> no_opcodes;
134 eh_frame.WriteCIE(Reg(16), no_opcodes);
135 eh_frame.WriteFDE(0x0100000000000000, 0x0200000000000000,
136 no_opcodes.data()->data(), no_opcodes.data()->size());
137 DW_CHECK("FDE cie=00000000 pc=100000000000000..300000000000000");
138 CheckObjdumpOutput(is64bit, "-W");
139}
140
141TEST_F(DwarfTest, DebugLine) {
142 const bool is64bit = false;
143 const int code_factor_bits = 1;
144 DebugLineOpCodeWriter<> opcodes(is64bit, code_factor_bits);
145
146 std::vector<std::string> include_directories;
147 include_directories.push_back("/path/to/source");
148 DW_CHECK("/path/to/source");
149
150 std::vector<DebugLineWriter<>::FileEntry> files {
151 { "file0.c", 0, 1000, 2000 },
152 { "file1.c", 1, 1000, 2000 },
153 { "file2.c", 1, 1000, 2000 },
154 };
155 DW_CHECK("1\t0\t1000\t2000\tfile0.c");
156 DW_CHECK_NEXT("2\t1\t1000\t2000\tfile1.c");
157 DW_CHECK_NEXT("3\t1\t1000\t2000\tfile2.c");
158
159 DW_CHECK("Line Number Statements");
160 opcodes.SetAddress(0x01000000);
161 DW_CHECK_NEXT("Extended opcode 2: set Address to 0x1000000");
162 opcodes.AddRow();
163 DW_CHECK_NEXT("Copy");
164 opcodes.AdvancePC(0x01000100);
165 DW_CHECK_NEXT("Advance PC by 256 to 0x1000100");
166 opcodes.SetFile(2);
167 DW_CHECK_NEXT("Set File Name to entry 2 in the File Name Table");
168 opcodes.AdvanceLine(3);
169 DW_CHECK_NEXT("Advance Line by 2 to 3");
170 opcodes.SetColumn(4);
171 DW_CHECK_NEXT("Set column to 4");
172 opcodes.NegateStmt();
173 DW_CHECK_NEXT("Set is_stmt to 0");
174 opcodes.SetBasicBlock();
175 DW_CHECK_NEXT("Set basic block");
176 opcodes.SetPrologueEnd();
177 DW_CHECK_NEXT("Set prologue_end to true");
178 opcodes.SetEpilogueBegin();
179 DW_CHECK_NEXT("Set epilogue_begin to true");
180 opcodes.SetISA(5);
181 DW_CHECK_NEXT("Set ISA to 5");
182 opcodes.EndSequence();
183 DW_CHECK_NEXT("Extended opcode 1: End of Sequence");
184 opcodes.DefineFile("file.c", 0, 1000, 2000);
185 DW_CHECK_NEXT("Extended opcode 3: define new File Table entry");
186 DW_CHECK_NEXT("Entry\tDir\tTime\tSize\tName");
187 DW_CHECK_NEXT("1\t0\t1000\t2000\tfile.c");
188
189 DebugLineWriter<> debug_line(&debug_line_data_);
190 debug_line.WriteTable(include_directories, files, opcodes);
191 CheckObjdumpOutput(is64bit, "-W");
192}
193
194// DWARF has special one byte codes which advance PC and line at the same time.
195TEST_F(DwarfTest, DebugLineSpecialOpcodes) {
196 const bool is64bit = false;
197 const int code_factor_bits = 1;
198 uint32_t pc = 0x01000000;
199 int line = 1;
200 DebugLineOpCodeWriter<> opcodes(is64bit, code_factor_bits);
201 opcodes.SetAddress(pc);
202 size_t num_rows = 0;
203 DW_CHECK("Line Number Statements:");
204 DW_CHECK("Special opcode");
205 DW_CHECK("Advance PC by constant");
206 DW_CHECK("Decoded dump of debug contents of section .debug_line:");
207 DW_CHECK("Line number Starting address");
208 for (int addr_delta = 0; addr_delta < 80; addr_delta += 2) {
209 for (int line_delta = 16; line_delta >= -16; --line_delta) {
210 pc += addr_delta;
211 line += line_delta;
212 opcodes.AddRow(pc, line);
213 num_rows++;
214 ASSERT_EQ(opcodes.CurrentAddress(), pc);
215 ASSERT_EQ(opcodes.CurrentLine(), line);
216 char expected[1024];
217 sprintf(expected, "%i 0x%x", line, pc);
218 DW_CHECK_NEXT(expected);
219 }
220 }
221 EXPECT_LT(opcodes.data()->size(), num_rows * 3);
222
223 std::vector<std::string> directories;
224 std::vector<DebugLineWriter<>::FileEntry> files {
225 { "file.c", 0, 1000, 2000 },
226 };
227 DebugLineWriter<> debug_line(&debug_line_data_);
228 debug_line.WriteTable(directories, files, opcodes);
229 CheckObjdumpOutput(is64bit, "-W -WL");
230}
231
232#endif // HAVE_ANDROID_OS
233
234} // namespace dwarf
235} // namespace art