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