File: dwarf_test.cc

package info (click to toggle)
android-platform-art 14.0.0%2Br15-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 96,796 kB
  • sloc: cpp: 522,217; java: 194,312; asm: 28,950; python: 14,910; xml: 5,087; sh: 4,528; ansic: 4,035; makefile: 110; perl: 77
file content (343 lines) | stat: -rw-r--r-- 13,040 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "dwarf_test.h"

#include "dwarf/debug_frame_opcode_writer.h"
#include "dwarf/debug_info_entry_writer.h"
#include "dwarf/debug_line_opcode_writer.h"
#include "dwarf/dwarf_constants.h"
#include "dwarf/headers.h"
#include "gtest/gtest.h"

namespace art HIDDEN {
namespace dwarf {

// Run the tests only on host since we need objdump.
#ifndef ART_TARGET_ANDROID

TEST_F(DwarfTest, DebugFrame) {
  const bool is64bit = false;

  // Pick offset value which would catch Uleb vs Sleb errors.
  const int offset = 40000;
  ASSERT_EQ(UnsignedLeb128Size(offset / 4), 2u);
  ASSERT_EQ(SignedLeb128Size(offset / 4), 3u);
  const Reg reg(6);

  // Test the opcodes in the order mentioned in the spec.
  // There are usually several encoding variations of each opcode.
  DebugFrameOpCodeWriter<> opcodes;
  DW_CHECK(".debug_frame contents:");
  DW_CHECK("FDE");
  DW_CHECK_NEXT("DWARF32");
  DW_CHECK_NEXT("DW_CFA_nop:");  // TODO: Why is a nop here.
  int pc = 0;
  for (int i : {0, 1, 0x3F, 0x40, 0xFF, 0x100, 0xFFFF, 0x10000}) {
    pc += i;
    opcodes.AdvancePC(pc);
  }
  DW_CHECK_NEXT("DW_CFA_advance_loc: 1");
  DW_CHECK_NEXT("DW_CFA_advance_loc: 63");
  DW_CHECK_NEXT("DW_CFA_advance_loc1: 64");
  DW_CHECK_NEXT("DW_CFA_advance_loc1: 255");
  DW_CHECK_NEXT("DW_CFA_advance_loc2: 256");
  DW_CHECK_NEXT("DW_CFA_advance_loc2: 65535");
  DW_CHECK_NEXT("DW_CFA_advance_loc4: 65536");
  opcodes.DefCFA(reg, offset);
  DW_CHECK_NEXT("DW_CFA_def_cfa: ESI +40000");
  opcodes.DefCFA(reg, -offset);
  DW_CHECK_NEXT("DW_CFA_def_cfa_sf: ESI -40000");
  opcodes.DefCFARegister(reg);
  DW_CHECK_NEXT("DW_CFA_def_cfa_register: ESI");
  opcodes.DefCFAOffset(offset);
  DW_CHECK_NEXT("DW_CFA_def_cfa_offset: +40000");
  opcodes.DefCFAOffset(-offset);
  DW_CHECK_NEXT("DW_CFA_def_cfa_offset_sf: -40000");
  uint8_t expr[] = { /*nop*/ 0x96 };
  opcodes.DefCFAExpression(expr, arraysize(expr));
  DW_CHECK_NEXT("DW_CFA_def_cfa_expression: DW_OP_nop");
  opcodes.Undefined(reg);
  DW_CHECK_NEXT("DW_CFA_undefined: ESI");
  opcodes.SameValue(reg);
  DW_CHECK_NEXT("DW_CFA_same_value: ESI");
  opcodes.Offset(Reg(0x3F), -offset);
  DW_CHECK_NEXT("DW_CFA_offset: reg63 -40000");
  opcodes.Offset(Reg(0x40), -offset);
  DW_CHECK_NEXT("DW_CFA_offset_extended: reg64 -40000");
  opcodes.Offset(Reg(0x40), offset);
  DW_CHECK_NEXT("DW_CFA_offset_extended_sf: reg64 40000");
  opcodes.ValOffset(reg, -offset);
  DW_CHECK_NEXT("DW_CFA_val_offset: ESI -40000");
  opcodes.ValOffset(reg, offset);
  DW_CHECK_NEXT("DW_CFA_val_offset_sf: ESI 40000");
  opcodes.Register(reg, Reg(1));
  DW_CHECK_NEXT("DW_CFA_register: ESI ECX");
  opcodes.Expression(reg, expr, arraysize(expr));
  DW_CHECK_NEXT("DW_CFA_expression: ESI DW_OP_nop");
  opcodes.ValExpression(reg, expr, arraysize(expr));
  DW_CHECK_NEXT("DW_CFA_val_expression: ESI DW_OP_nop");
  opcodes.Restore(Reg(0x3F));
  DW_CHECK_NEXT("DW_CFA_restore: reg63");
  opcodes.Restore(Reg(0x40));
  DW_CHECK_NEXT("DW_CFA_restore_extended: reg64");
  opcodes.Restore(reg);
  DW_CHECK_NEXT("DW_CFA_restore: ESI");
  opcodes.RememberState();
  DW_CHECK_NEXT("DW_CFA_remember_state:");
  opcodes.RestoreState();
  DW_CHECK_NEXT("DW_CFA_restore_state:");
  opcodes.Nop();
  DW_CHECK_NEXT("DW_CFA_nop:");

  // Also test helpers.
  opcodes.DefCFA(Reg(4), 100);  // ESP
  DW_CHECK_NEXT("DW_CFA_def_cfa: ESP +100");
  opcodes.AdjustCFAOffset(8);
  DW_CHECK_NEXT("DW_CFA_def_cfa_offset: +108");
  opcodes.RelOffset(Reg(0), 0);  // push R0
  DW_CHECK_NEXT("DW_CFA_offset: EAX -108");
  opcodes.RelOffset(Reg(1), 4);  // push R1
  DW_CHECK_NEXT("DW_CFA_offset: ECX -104");
  opcodes.RelOffsetForMany(Reg(2), 8, 1 | (1 << 3), 4);  // push R2 and R5
  DW_CHECK_NEXT("DW_CFA_offset: EDX -100");
  DW_CHECK_NEXT("DW_CFA_offset: EBP -96");
  opcodes.RestoreMany(Reg(2), 1 | (1 << 3));  // pop R2 and R5
  DW_CHECK_NEXT("DW_CFA_restore: EDX");
  DW_CHECK_NEXT("DW_CFA_restore: EBP");

  DebugFrameOpCodeWriter<> initial_opcodes;
  WriteCIE(is64bit, Reg(is64bit ? 16 : 8), initial_opcodes, &debug_frame_data_);
  WriteFDE(is64bit,
           /* cie_pointer= */ 0,
           0x01000000,
           0x01000000,
           ArrayRef<const uint8_t>(*opcodes.data()),
           &debug_frame_data_);

  CheckObjdumpOutput(is64bit, "-debug-frame");
}

TEST_F(DwarfTest, DISABLED_DebugFrame64) {
  constexpr bool is64bit = true;
  DebugFrameOpCodeWriter<> initial_opcodes;
  WriteCIE(is64bit, Reg(16), initial_opcodes, &debug_frame_data_);
  DebugFrameOpCodeWriter<> opcodes;
  DW_CHECK(".debug_frame contents:");
  WriteFDE(is64bit,
           /* cie_pointer= */ 0,
           0x0100000000000000,
           0x0200000000000000,
           ArrayRef<const uint8_t>(*opcodes.data()),
           &debug_frame_data_);
  DW_CHECK("FDE cie=00000000 pc=100000000000000..300000000000000");

  CheckObjdumpOutput(is64bit, "-debug-frame");
}

// Test x86_64 register mapping. It is the only non-trivial architecture.
// ARM and X86 have: dwarf_reg = art_reg + constant.
TEST_F(DwarfTest, x86_64_RegisterMapping) {
  constexpr bool is64bit = true;
  DebugFrameOpCodeWriter<> opcodes;
  DW_CHECK(".debug_frame contents:");
  for (int i = 0; i < 16; i++) {
    opcodes.RelOffset(Reg::X86_64Core(i), 0);
  }
  DW_CHECK("FDE");
  DW_CHECK_NEXT("DWARF32");
  DW_CHECK_NEXT("DW_CFA_nop:");  // TODO: Why is a nop here.
  DW_CHECK_NEXT("DW_CFA_offset: RAX 0");
  DW_CHECK_NEXT("DW_CFA_offset: RCX 0");
  DW_CHECK_NEXT("DW_CFA_offset: RDX 0");
  DW_CHECK_NEXT("DW_CFA_offset: RBX 0");
  DW_CHECK_NEXT("DW_CFA_offset: RSP 0");
  DW_CHECK_NEXT("DW_CFA_offset: RBP 0");
  DW_CHECK_NEXT("DW_CFA_offset: RSI 0");
  DW_CHECK_NEXT("DW_CFA_offset: RDI 0");
  DW_CHECK_NEXT("DW_CFA_offset: R8 0");
  DW_CHECK_NEXT("DW_CFA_offset: R9 0");
  DW_CHECK_NEXT("DW_CFA_offset: R10 0");
  DW_CHECK_NEXT("DW_CFA_offset: R11 0");
  DW_CHECK_NEXT("DW_CFA_offset: R12 0");
  DW_CHECK_NEXT("DW_CFA_offset: R13 0");
  DW_CHECK_NEXT("DW_CFA_offset: R14 0");
  DW_CHECK_NEXT("DW_CFA_offset: R15 0");

  DebugFrameOpCodeWriter<> initial_opcodes;
  WriteCIE(is64bit, Reg(16), initial_opcodes, &debug_frame_data_);
  WriteFDE(is64bit,
           /* cie_pointer= */ 0,
           0x0100000000000000,
           0x0200000000000000,
           ArrayRef<const uint8_t>(*opcodes.data()),
           &debug_frame_data_);

  CheckObjdumpOutput(is64bit, "-debug-frame");
}

TEST_F(DwarfTest, DebugLine) {
  const bool is64bit = false;
  const int code_factor_bits = 1;
  DebugLineOpCodeWriter<> opcodes(is64bit, code_factor_bits);
  DW_CHECK(".debug_line contents:");

  std::vector<std::string> include_directories;
  include_directories.push_back("/path/to/source");
  DW_CHECK("include_directories[  1] = \"/path/to/source\"");

  std::vector<FileEntry> files {
    { "file0.c", 0, 1000, 2000 },
    { "file1.c", 1, 1000, 2000 },
    { "file2.c", 1, 1000, 2000 },
  };
  DW_CHECK_NEXT("file_names[  1]:");
  DW_CHECK_NEXT("           name: \"file0.c\"");
  DW_CHECK_NEXT("      dir_index: 0");
  DW_CHECK_NEXT("       mod_time: 0x000003e8");
  DW_CHECK_NEXT("         length: 0x000007d0");
  DW_CHECK_NEXT("file_names[  2]:");
  DW_CHECK_NEXT("           name: \"file1.c\"");
  DW_CHECK_NEXT("      dir_index: 1");
  DW_CHECK_NEXT("       mod_time: 0x000003e8");
  DW_CHECK_NEXT("         length: 0x000007d0");
  DW_CHECK_NEXT("file_names[  3]:");
  DW_CHECK_NEXT("           name: \"file2.c\"");
  DW_CHECK_NEXT("      dir_index: 1");
  DW_CHECK_NEXT("       mod_time: 0x000003e8");
  DW_CHECK_NEXT("         length: 0x000007d0");

  opcodes.SetAddress(0x01000000);
  opcodes.SetIsStmt(true);
  opcodes.AddRow();
  opcodes.AdvancePC(0x01000100);
  opcodes.SetFile(2);
  opcodes.AdvanceLine(3);
  opcodes.SetColumn(4);
  opcodes.SetIsStmt(false);
  opcodes.SetBasicBlock();
  opcodes.SetPrologueEnd();
  opcodes.SetEpilogueBegin();
  opcodes.SetISA(5);
  opcodes.EndSequence();
  opcodes.DefineFile("file.c", 0, 1000, 2000);
  DW_CHECK_NEXT("Address            Line   Column File   ISA Discriminator Flags");
  DW_CHECK_NEXT("------------------ ------ ------ ------ --- ------------- -------------");
  DW_CHECK_NEXT("0x0000000001000000      1      0      1   0             0  is_stmt");
  DW_CHECK_NEXT("0x0000000001000100      3      4      2   5             0  basic_block prologue_end epilogue_begin end_sequence");

  WriteDebugLineTable(include_directories, files, opcodes, &debug_line_data_);

  CheckObjdumpOutput(is64bit, "-debug-line");
}

// DWARF has special one byte codes which advance PC and line at the same time.
TEST_F(DwarfTest, DebugLineSpecialOpcodes) {
  const bool is64bit = false;
  const int code_factor_bits = 1;
  uint32_t pc = 0x01000000;
  int line = 1;
  DebugLineOpCodeWriter<> opcodes(is64bit, code_factor_bits);
  opcodes.SetAddress(pc);
  size_t num_rows = 0;
  DW_CHECK(".debug_line contents:");
  DW_CHECK("file_names[  1]:");
  DW_CHECK("           name: \"file.c\"");
  DW_CHECK("Address            Line   Column File   ISA Discriminator Flags");
  DW_CHECK("------------------ ------ ------ ------ --- ------------- -------------");
  for (int addr_delta = 0; addr_delta < 80; addr_delta += 2) {
    for (int line_delta = 16; line_delta >= -16; --line_delta) {
      pc += addr_delta;
      line += line_delta;
      opcodes.AddRow(pc, line);
      num_rows++;
      ASSERT_EQ(opcodes.CurrentAddress(), pc);
      ASSERT_EQ(opcodes.CurrentLine(), line);
      char expected[1024];
      sprintf(expected, "0x%016x %6i      0      1   0             0", pc, line);
      DW_CHECK_NEXT(expected);
    }
  }
  opcodes.EndSequence();
  EXPECT_LT(opcodes.data()->size(), num_rows * 3);

  std::vector<std::string> directories;
  std::vector<FileEntry> files = { { "file.c", 0, 1000, 2000 } };
  WriteDebugLineTable(directories, files, opcodes, &debug_line_data_);

  CheckObjdumpOutput(is64bit, "-debug-line");
}

TEST_F(DwarfTest, DebugInfo) {
  constexpr bool is64bit = false;

  DebugAbbrevWriter<> debug_abbrev(&debug_abbrev_data_);
  DW_CHECK(".debug_abbrev contents:");
  DW_CHECK_NEXT("Abbrev table for offset: 0x00000000");
  DW_CHECK_NEXT("[1] DW_TAG_compile_unit DW_CHILDREN_yes");
  DW_CHECK_NEXT(" DW_AT_producer DW_FORM_strp");
  DW_CHECK_NEXT(" DW_AT_low_pc DW_FORM_addr");
  DW_CHECK_NEXT(" DW_AT_high_pc DW_FORM_addr");
  DW_CHECK_NEXT("[2] DW_TAG_subprogram DW_CHILDREN_no");
  DW_CHECK_NEXT(" DW_AT_name DW_FORM_strp");
  DW_CHECK_NEXT(" DW_AT_low_pc DW_FORM_addr");
  DW_CHECK_NEXT(" DW_AT_high_pc DW_FORM_addr");
  DW_CHECK_NEXT("[3] DW_TAG_compile_unit DW_CHILDREN_no");

  DebugInfoEntryWriter<> info(is64bit, &debug_abbrev);
  DW_CHECK(".debug_info contents:");
  info.StartTag(dwarf::DW_TAG_compile_unit);
  DW_CHECK_NEXT("Compile Unit: length = 0x00000030, format = DWARF32, version = 0x0004, abbr_offset = 0x0000, addr_size = 0x04");
  DW_CHECK_NEXT("DW_TAG_compile_unit");
  info.WriteStrp(dwarf::DW_AT_producer, "Compiler name", &debug_str_data_);
  DW_CHECK_NEXT("  DW_AT_producer (\"Compiler name\")");
  info.WriteAddr(dwarf::DW_AT_low_pc, 0x01000000);
  DW_CHECK_NEXT("  DW_AT_low_pc (0x01000000)");
  info.WriteAddr(dwarf::DW_AT_high_pc, 0x02000000);
  DW_CHECK_NEXT("  DW_AT_high_pc (0x02000000)");
  info.StartTag(dwarf::DW_TAG_subprogram);
  DW_CHECK_NEXT("  DW_TAG_subprogram");
  info.WriteStrp(dwarf::DW_AT_name, "Foo", &debug_str_data_);
  DW_CHECK_NEXT("    DW_AT_name (\"Foo\")");
  info.WriteAddr(dwarf::DW_AT_low_pc, 0x01010000);
  DW_CHECK_NEXT("    DW_AT_low_pc (0x01010000)");
  info.WriteAddr(dwarf::DW_AT_high_pc, 0x01020000);
  DW_CHECK_NEXT("    DW_AT_high_pc (0x01020000)");
  info.EndTag();  // DW_TAG_subprogram
  info.StartTag(dwarf::DW_TAG_subprogram);
  DW_CHECK_NEXT("  DW_TAG_subprogram");
  info.WriteStrp(dwarf::DW_AT_name, "Bar", &debug_str_data_);
  DW_CHECK_NEXT("    DW_AT_name (\"Bar\")");
  info.WriteAddr(dwarf::DW_AT_low_pc, 0x01020000);
  DW_CHECK_NEXT("    DW_AT_low_pc (0x01020000)");
  info.WriteAddr(dwarf::DW_AT_high_pc, 0x01030000);
  DW_CHECK_NEXT("    DW_AT_high_pc (0x01030000)");
  info.EndTag();  // DW_TAG_subprogram
  info.EndTag();  // DW_TAG_compile_unit
  DW_CHECK_NEXT("  NULL");
  // Test that previous list was properly terminated and empty children.
  info.StartTag(dwarf::DW_TAG_compile_unit);
  info.EndTag();  // DW_TAG_compile_unit

  dwarf::WriteDebugInfoCU(/* debug_abbrev_offset= */ 0, info, &debug_info_data_);

  CheckObjdumpOutput(is64bit, "-debug-info -debug-abbrev");
}

#endif  // ART_TARGET_ANDROID

}  // namespace dwarf
}  // namespace art