blob: 87e679fbea98c9e6579adbd49914b5f168179fea [file] [log] [blame]
David Srbeckyc5bfa972016-02-05 15:49:10 +00001/*
2 * Copyright (C) 2016 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#ifndef ART_COMPILER_DEBUG_ELF_DEBUG_INFO_WRITER_H_
18#define ART_COMPILER_DEBUG_ELF_DEBUG_INFO_WRITER_H_
19
20#include <map>
21#include <unordered_set>
22#include <vector>
23
Andreas Gampea1d2f952017-04-20 22:53:58 -070024#include "art_field-inl.h"
David Srbeckyc5bfa972016-02-05 15:49:10 +000025#include "debug/dwarf/debug_abbrev_writer.h"
26#include "debug/dwarf/debug_info_entry_writer.h"
27#include "debug/elf_compilation_unit.h"
28#include "debug/elf_debug_loc_writer.h"
29#include "debug/method_debug_info.h"
David Sehr9e734c72018-01-04 17:56:19 -080030#include "dex/code_item_accessors-inl.h"
31#include "dex/dex_file-inl.h"
32#include "dex/dex_file.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070033#include "heap_poisoning.h"
David Srbeckyc5bfa972016-02-05 15:49:10 +000034#include "linear_alloc.h"
Vladimir Marko74527972016-11-29 15:57:32 +000035#include "linker/elf_builder.h"
David Srbeckyc5bfa972016-02-05 15:49:10 +000036#include "mirror/array.h"
37#include "mirror/class-inl.h"
38#include "mirror/class.h"
Nicolas Geoffray58cc1cb2017-11-20 13:27:29 +000039#include "oat_file.h"
David Srbeckyc5bfa972016-02-05 15:49:10 +000040
41namespace art {
42namespace debug {
43
44typedef std::vector<DexFile::LocalInfo> LocalInfos;
45
46static void LocalInfoCallback(void* ctx, const DexFile::LocalInfo& entry) {
47 static_cast<LocalInfos*>(ctx)->push_back(entry);
48}
49
50static std::vector<const char*> GetParamNames(const MethodDebugInfo* mi) {
51 std::vector<const char*> names;
Mathieu Chartier8892c6b2018-01-09 15:10:17 -080052 CodeItemDebugInfoAccessor accessor(*mi->dex_file, mi->code_item, mi->dex_method_index);
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -080053 if (accessor.HasCodeItem()) {
David Srbecky09c2a6b2016-03-11 17:11:44 +000054 DCHECK(mi->dex_file != nullptr);
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -080055 const uint8_t* stream = mi->dex_file->GetDebugInfoStream(accessor.DebugInfoOffset());
David Srbeckyc5bfa972016-02-05 15:49:10 +000056 if (stream != nullptr) {
57 DecodeUnsignedLeb128(&stream); // line.
58 uint32_t parameters_size = DecodeUnsignedLeb128(&stream);
59 for (uint32_t i = 0; i < parameters_size; ++i) {
60 uint32_t id = DecodeUnsignedLeb128P1(&stream);
Andreas Gampe8a0128a2016-11-28 07:38:35 -080061 names.push_back(mi->dex_file->StringDataByIdx(dex::StringIndex(id)));
David Srbeckyc5bfa972016-02-05 15:49:10 +000062 }
63 }
64 }
65 return names;
66}
67
68// Helper class to write .debug_info and its supporting sections.
69template<typename ElfTypes>
70class ElfDebugInfoWriter {
71 using Elf_Addr = typename ElfTypes::Addr;
72
73 public:
Vladimir Marko74527972016-11-29 15:57:32 +000074 explicit ElfDebugInfoWriter(linker::ElfBuilder<ElfTypes>* builder)
David Srbeckyc5bfa972016-02-05 15:49:10 +000075 : builder_(builder),
76 debug_abbrev_(&debug_abbrev_buffer_) {
77 }
78
79 void Start() {
80 builder_->GetDebugInfo()->Start();
81 }
82
83 void End(bool write_oat_patches) {
84 builder_->GetDebugInfo()->End();
85 if (write_oat_patches) {
86 builder_->WritePatches(".debug_info.oat_patches",
87 ArrayRef<const uintptr_t>(debug_info_patches_));
88 }
89 builder_->WriteSection(".debug_abbrev", &debug_abbrev_buffer_);
90 if (!debug_loc_.empty()) {
91 builder_->WriteSection(".debug_loc", &debug_loc_);
92 }
93 if (!debug_ranges_.empty()) {
94 builder_->WriteSection(".debug_ranges", &debug_ranges_);
95 }
96 }
97
98 private:
Vladimir Marko74527972016-11-29 15:57:32 +000099 linker::ElfBuilder<ElfTypes>* builder_;
David Srbeckyc5bfa972016-02-05 15:49:10 +0000100 std::vector<uintptr_t> debug_info_patches_;
101 std::vector<uint8_t> debug_abbrev_buffer_;
102 dwarf::DebugAbbrevWriter<> debug_abbrev_;
103 std::vector<uint8_t> debug_loc_;
104 std::vector<uint8_t> debug_ranges_;
105
106 std::unordered_set<const char*> defined_dex_classes_; // For CHECKs only.
107
108 template<typename ElfTypes2>
109 friend class ElfCompilationUnitWriter;
110};
111
112// Helper class to write one compilation unit.
113// It holds helper methods and temporary state.
114template<typename ElfTypes>
115class ElfCompilationUnitWriter {
116 using Elf_Addr = typename ElfTypes::Addr;
117
118 public:
119 explicit ElfCompilationUnitWriter(ElfDebugInfoWriter<ElfTypes>* owner)
120 : owner_(owner),
121 info_(Is64BitInstructionSet(owner_->builder_->GetIsa()), &owner->debug_abbrev_) {
122 }
123
124 void Write(const ElfCompilationUnit& compilation_unit) {
125 CHECK(!compilation_unit.methods.empty());
David Srbecky197160d2016-03-07 17:33:57 +0000126 const Elf_Addr base_address = compilation_unit.is_code_address_text_relative
David Srbeckyc5bfa972016-02-05 15:49:10 +0000127 ? owner_->builder_->GetText()->GetAddress()
128 : 0;
David Srbecky56da23c2017-09-08 19:59:15 +0100129 const bool is64bit = Is64BitInstructionSet(owner_->builder_->GetIsa());
David Srbeckyc5bfa972016-02-05 15:49:10 +0000130 using namespace dwarf; // NOLINT. For easy access to DWARF constants.
131
132 info_.StartTag(DW_TAG_compile_unit);
133 info_.WriteString(DW_AT_producer, "Android dex2oat");
134 info_.WriteData1(DW_AT_language, DW_LANG_Java);
135 info_.WriteString(DW_AT_comp_dir, "$JAVA_SRC_ROOT");
David Srbecky56da23c2017-09-08 19:59:15 +0100136 // The low_pc acts as base address for several other addresses/ranges.
David Srbecky197160d2016-03-07 17:33:57 +0000137 info_.WriteAddr(DW_AT_low_pc, base_address + compilation_unit.code_address);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000138 info_.WriteSecOffset(DW_AT_stmt_list, compilation_unit.debug_line_offset);
139
David Srbecky56da23c2017-09-08 19:59:15 +0100140 // Write .debug_ranges entries covering code ranges of the whole compilation unit.
141 dwarf::Writer<> debug_ranges(&owner_->debug_ranges_);
142 info_.WriteSecOffset(DW_AT_ranges, owner_->debug_ranges_.size());
143 for (auto mi : compilation_unit.methods) {
144 uint64_t low_pc = mi->code_address - compilation_unit.code_address;
145 uint64_t high_pc = low_pc + mi->code_size;
146 if (is64bit) {
147 debug_ranges.PushUint64(low_pc);
148 debug_ranges.PushUint64(high_pc);
149 } else {
150 debug_ranges.PushUint32(low_pc);
151 debug_ranges.PushUint32(high_pc);
152 }
153 }
154 if (is64bit) {
155 debug_ranges.PushUint64(0); // End of list.
156 debug_ranges.PushUint64(0);
157 } else {
158 debug_ranges.PushUint32(0); // End of list.
159 debug_ranges.PushUint32(0);
160 }
161
David Srbeckyc5bfa972016-02-05 15:49:10 +0000162 const char* last_dex_class_desc = nullptr;
163 for (auto mi : compilation_unit.methods) {
David Srbecky09c2a6b2016-03-11 17:11:44 +0000164 DCHECK(mi->dex_file != nullptr);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000165 const DexFile* dex = mi->dex_file;
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800166 CodeItemDebugInfoAccessor accessor(*dex, mi->code_item, mi->dex_method_index);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000167 const DexFile::MethodId& dex_method = dex->GetMethodId(mi->dex_method_index);
168 const DexFile::ProtoId& dex_proto = dex->GetMethodPrototype(dex_method);
169 const DexFile::TypeList* dex_params = dex->GetProtoParameters(dex_proto);
170 const char* dex_class_desc = dex->GetMethodDeclaringClassDescriptor(dex_method);
171 const bool is_static = (mi->access_flags & kAccStatic) != 0;
172
173 // Enclose the method in correct class definition.
174 if (last_dex_class_desc != dex_class_desc) {
175 if (last_dex_class_desc != nullptr) {
176 EndClassTag();
177 }
178 // Write reference tag for the class we are about to declare.
179 size_t reference_tag_offset = info_.StartTag(DW_TAG_reference_type);
180 type_cache_.emplace(std::string(dex_class_desc), reference_tag_offset);
181 size_t type_attrib_offset = info_.size();
182 info_.WriteRef4(DW_AT_type, 0);
183 info_.EndTag();
184 // Declare the class that owns this method.
185 size_t class_offset = StartClassTag(dex_class_desc);
186 info_.UpdateUint32(type_attrib_offset, class_offset);
187 info_.WriteFlagPresent(DW_AT_declaration);
188 // Check that each class is defined only once.
189 bool unique = owner_->defined_dex_classes_.insert(dex_class_desc).second;
190 CHECK(unique) << "Redefinition of " << dex_class_desc;
191 last_dex_class_desc = dex_class_desc;
192 }
193
194 int start_depth = info_.Depth();
195 info_.StartTag(DW_TAG_subprogram);
196 WriteName(dex->GetMethodName(dex_method));
David Srbecky197160d2016-03-07 17:33:57 +0000197 info_.WriteAddr(DW_AT_low_pc, base_address + mi->code_address);
198 info_.WriteUdata(DW_AT_high_pc, mi->code_size);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000199 std::vector<uint8_t> expr_buffer;
200 Expression expr(&expr_buffer);
201 expr.WriteOpCallFrameCfa();
202 info_.WriteExprLoc(DW_AT_frame_base, expr);
203 WriteLazyType(dex->GetReturnTypeDescriptor(dex_proto));
204
David Srbecky2ed15b62016-03-04 11:34:46 +0000205 // Decode dex register locations for all stack maps.
206 // It might be expensive, so do it just once and reuse the result.
207 std::vector<DexRegisterMap> dex_reg_maps;
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -0800208 if (accessor.HasCodeItem() && mi->code_info != nullptr) {
David Srbecky197160d2016-03-07 17:33:57 +0000209 const CodeInfo code_info(mi->code_info);
David Srbecky052f8ca2018-04-26 15:42:54 +0100210 for (size_t s = 0; s < code_info.GetNumberOfStackMaps(); ++s) {
211 const StackMap stack_map = code_info.GetStackMapAt(s);
David Srbecky2ed15b62016-03-04 11:34:46 +0000212 dex_reg_maps.push_back(code_info.GetDexRegisterMapOf(
David Srbecky052f8ca2018-04-26 15:42:54 +0100213 stack_map, accessor.RegistersSize()));
David Srbecky2ed15b62016-03-04 11:34:46 +0000214 }
215 }
216
David Srbeckyc5bfa972016-02-05 15:49:10 +0000217 // Write parameters. DecodeDebugLocalInfo returns them as well, but it does not
218 // guarantee order or uniqueness so it is safer to iterate over them manually.
219 // DecodeDebugLocalInfo might not also be available if there is no debug info.
220 std::vector<const char*> param_names = GetParamNames(mi);
221 uint32_t arg_reg = 0;
222 if (!is_static) {
223 info_.StartTag(DW_TAG_formal_parameter);
224 WriteName("this");
225 info_.WriteFlagPresent(DW_AT_artificial);
226 WriteLazyType(dex_class_desc);
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -0800227 if (accessor.HasCodeItem()) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000228 // Write the stack location of the parameter.
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -0800229 const uint32_t vreg = accessor.RegistersSize() - accessor.InsSize() + arg_reg;
David Srbeckyc5bfa972016-02-05 15:49:10 +0000230 const bool is64bitValue = false;
David Srbecky197160d2016-03-07 17:33:57 +0000231 WriteRegLocation(mi, dex_reg_maps, vreg, is64bitValue, compilation_unit.code_address);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000232 }
233 arg_reg++;
234 info_.EndTag();
235 }
236 if (dex_params != nullptr) {
237 for (uint32_t i = 0; i < dex_params->Size(); ++i) {
238 info_.StartTag(DW_TAG_formal_parameter);
239 // Parameter names may not be always available.
240 if (i < param_names.size()) {
241 WriteName(param_names[i]);
242 }
243 // Write the type.
244 const char* type_desc = dex->StringByTypeIdx(dex_params->GetTypeItem(i).type_idx_);
245 WriteLazyType(type_desc);
246 const bool is64bitValue = type_desc[0] == 'D' || type_desc[0] == 'J';
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -0800247 if (accessor.HasCodeItem()) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000248 // Write the stack location of the parameter.
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -0800249 const uint32_t vreg = accessor.RegistersSize() - accessor.InsSize() + arg_reg;
David Srbecky197160d2016-03-07 17:33:57 +0000250 WriteRegLocation(mi, dex_reg_maps, vreg, is64bitValue, compilation_unit.code_address);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000251 }
252 arg_reg += is64bitValue ? 2 : 1;
253 info_.EndTag();
254 }
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -0800255 if (accessor.HasCodeItem()) {
256 DCHECK_EQ(arg_reg, accessor.InsSize());
David Srbeckyc5bfa972016-02-05 15:49:10 +0000257 }
258 }
259
260 // Write local variables.
261 LocalInfos local_infos;
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800262 if (accessor.DecodeDebugLocalInfo(is_static,
263 mi->dex_method_index,
264 LocalInfoCallback,
265 &local_infos)) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000266 for (const DexFile::LocalInfo& var : local_infos) {
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -0800267 if (var.reg_ < accessor.RegistersSize() - accessor.InsSize()) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000268 info_.StartTag(DW_TAG_variable);
269 WriteName(var.name_);
270 WriteLazyType(var.descriptor_);
271 bool is64bitValue = var.descriptor_[0] == 'D' || var.descriptor_[0] == 'J';
David Srbecky2ed15b62016-03-04 11:34:46 +0000272 WriteRegLocation(mi,
273 dex_reg_maps,
274 var.reg_,
275 is64bitValue,
David Srbecky197160d2016-03-07 17:33:57 +0000276 compilation_unit.code_address,
David Srbecky2ed15b62016-03-04 11:34:46 +0000277 var.start_address_,
278 var.end_address_);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000279 info_.EndTag();
280 }
281 }
282 }
283
284 info_.EndTag();
285 CHECK_EQ(info_.Depth(), start_depth); // Balanced start/end.
286 }
287 if (last_dex_class_desc != nullptr) {
288 EndClassTag();
289 }
290 FinishLazyTypes();
291 CloseNamespacesAboveDepth(0);
292 info_.EndTag(); // DW_TAG_compile_unit
293 CHECK_EQ(info_.Depth(), 0);
294 std::vector<uint8_t> buffer;
295 buffer.reserve(info_.data()->size() + KB);
David Srbeckye155f4b2017-12-06 15:18:38 +0000296 const size_t offset = owner_->builder_->GetDebugInfo()->GetPosition();
David Srbeckyc5bfa972016-02-05 15:49:10 +0000297 // All compilation units share single table which is at the start of .debug_abbrev.
298 const size_t debug_abbrev_offset = 0;
299 WriteDebugInfoCU(debug_abbrev_offset, info_, offset, &buffer, &owner_->debug_info_patches_);
300 owner_->builder_->GetDebugInfo()->WriteFully(buffer.data(), buffer.size());
301 }
302
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700303 void Write(const ArrayRef<mirror::Class*>& types) REQUIRES_SHARED(Locks::mutator_lock_) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000304 using namespace dwarf; // NOLINT. For easy access to DWARF constants.
305
306 info_.StartTag(DW_TAG_compile_unit);
307 info_.WriteString(DW_AT_producer, "Android dex2oat");
308 info_.WriteData1(DW_AT_language, DW_LANG_Java);
309
310 // Base class references to be patched at the end.
311 std::map<size_t, mirror::Class*> base_class_references;
312
313 // Already written declarations or definitions.
314 std::map<mirror::Class*, size_t> class_declarations;
315
316 std::vector<uint8_t> expr_buffer;
317 for (mirror::Class* type : types) {
318 if (type->IsPrimitive()) {
319 // For primitive types the definition and the declaration is the same.
320 if (type->GetPrimitiveType() != Primitive::kPrimVoid) {
321 WriteTypeDeclaration(type->GetDescriptor(nullptr));
322 }
323 } else if (type->IsArrayClass()) {
324 mirror::Class* element_type = type->GetComponentType();
325 uint32_t component_size = type->GetComponentSize();
326 uint32_t data_offset = mirror::Array::DataOffset(component_size).Uint32Value();
327 uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value();
328
329 CloseNamespacesAboveDepth(0); // Declare in root namespace.
330 info_.StartTag(DW_TAG_array_type);
331 std::string descriptor_string;
332 WriteLazyType(element_type->GetDescriptor(&descriptor_string));
333 WriteLinkageName(type);
334 info_.WriteUdata(DW_AT_data_member_location, data_offset);
335 info_.StartTag(DW_TAG_subrange_type);
336 Expression count_expr(&expr_buffer);
337 count_expr.WriteOpPushObjectAddress();
338 count_expr.WriteOpPlusUconst(length_offset);
339 count_expr.WriteOpDerefSize(4); // Array length is always 32-bit wide.
340 info_.WriteExprLoc(DW_AT_count, count_expr);
341 info_.EndTag(); // DW_TAG_subrange_type.
342 info_.EndTag(); // DW_TAG_array_type.
343 } else if (type->IsInterface()) {
344 // Skip. Variables cannot have an interface as a dynamic type.
345 // We do not expose the interface information to the debugger in any way.
346 } else {
347 std::string descriptor_string;
348 const char* desc = type->GetDescriptor(&descriptor_string);
349 size_t class_offset = StartClassTag(desc);
350 class_declarations.emplace(type, class_offset);
351
352 if (!type->IsVariableSize()) {
353 info_.WriteUdata(DW_AT_byte_size, type->GetObjectSize());
354 }
355
356 WriteLinkageName(type);
357
358 if (type->IsObjectClass()) {
359 // Generate artificial member which is used to get the dynamic type of variable.
360 // The run-time value of this field will correspond to linkage name of some type.
361 // We need to do it only once in j.l.Object since all other types inherit it.
362 info_.StartTag(DW_TAG_member);
363 WriteName(".dynamic_type");
364 WriteLazyType(sizeof(uintptr_t) == 8 ? "J" : "I");
365 info_.WriteFlagPresent(DW_AT_artificial);
366 // Create DWARF expression to get the value of the methods_ field.
367 Expression expr(&expr_buffer);
368 // The address of the object has been implicitly pushed on the stack.
369 // Dereference the klass_ field of Object (32-bit; possibly poisoned).
370 DCHECK_EQ(type->ClassOffset().Uint32Value(), 0u);
371 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Class>), 4u);
372 expr.WriteOpDerefSize(4);
373 if (kPoisonHeapReferences) {
374 expr.WriteOpNeg();
375 // DWARF stack is pointer sized. Ensure that the high bits are clear.
376 expr.WriteOpConstu(0xFFFFFFFF);
377 expr.WriteOpAnd();
378 }
379 // Add offset to the methods_ field.
380 expr.WriteOpPlusUconst(mirror::Class::MethodsOffset().Uint32Value());
381 // Top of stack holds the location of the field now.
382 info_.WriteExprLoc(DW_AT_data_member_location, expr);
383 info_.EndTag(); // DW_TAG_member.
384 }
385
386 // Base class.
387 mirror::Class* base_class = type->GetSuperClass();
388 if (base_class != nullptr) {
389 info_.StartTag(DW_TAG_inheritance);
390 base_class_references.emplace(info_.size(), base_class);
391 info_.WriteRef4(DW_AT_type, 0);
392 info_.WriteUdata(DW_AT_data_member_location, 0);
393 info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_public);
394 info_.EndTag(); // DW_TAG_inheritance.
395 }
396
397 // Member variables.
398 for (uint32_t i = 0, count = type->NumInstanceFields(); i < count; ++i) {
399 ArtField* field = type->GetInstanceField(i);
400 info_.StartTag(DW_TAG_member);
401 WriteName(field->GetName());
402 WriteLazyType(field->GetTypeDescriptor());
403 info_.WriteUdata(DW_AT_data_member_location, field->GetOffset().Uint32Value());
404 uint32_t access_flags = field->GetAccessFlags();
405 if (access_flags & kAccPublic) {
406 info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_public);
407 } else if (access_flags & kAccProtected) {
408 info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_protected);
409 } else if (access_flags & kAccPrivate) {
410 info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_private);
411 }
412 info_.EndTag(); // DW_TAG_member.
413 }
414
415 if (type->IsStringClass()) {
416 // Emit debug info about an artifical class member for java.lang.String which represents
417 // the first element of the data stored in a string instance. Consumers of the debug
418 // info will be able to read the content of java.lang.String based on the count (real
419 // field) and based on the location of this data member.
420 info_.StartTag(DW_TAG_member);
421 WriteName("value");
422 // We don't support fields with C like array types so we just say its type is java char.
423 WriteLazyType("C"); // char.
424 info_.WriteUdata(DW_AT_data_member_location,
425 mirror::String::ValueOffset().Uint32Value());
426 info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_private);
427 info_.EndTag(); // DW_TAG_member.
428 }
429
430 EndClassTag();
431 }
432 }
433
434 // Write base class declarations.
435 for (const auto& base_class_reference : base_class_references) {
436 size_t reference_offset = base_class_reference.first;
437 mirror::Class* base_class = base_class_reference.second;
Vladimir Marko3bada4b2017-05-19 12:32:47 +0100438 const auto it = class_declarations.find(base_class);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000439 if (it != class_declarations.end()) {
440 info_.UpdateUint32(reference_offset, it->second);
441 } else {
442 // Declare base class. We can not use the standard WriteLazyType
443 // since we want to avoid the DW_TAG_reference_tag wrapping.
444 std::string tmp_storage;
445 const char* base_class_desc = base_class->GetDescriptor(&tmp_storage);
446 size_t base_class_declaration_offset = StartClassTag(base_class_desc);
447 info_.WriteFlagPresent(DW_AT_declaration);
448 WriteLinkageName(base_class);
449 EndClassTag();
450 class_declarations.emplace(base_class, base_class_declaration_offset);
451 info_.UpdateUint32(reference_offset, base_class_declaration_offset);
452 }
453 }
454
455 FinishLazyTypes();
456 CloseNamespacesAboveDepth(0);
457 info_.EndTag(); // DW_TAG_compile_unit.
458 CHECK_EQ(info_.Depth(), 0);
459 std::vector<uint8_t> buffer;
460 buffer.reserve(info_.data()->size() + KB);
David Srbeckye155f4b2017-12-06 15:18:38 +0000461 const size_t offset = owner_->builder_->GetDebugInfo()->GetPosition();
David Srbeckyc5bfa972016-02-05 15:49:10 +0000462 // All compilation units share single table which is at the start of .debug_abbrev.
463 const size_t debug_abbrev_offset = 0;
464 WriteDebugInfoCU(debug_abbrev_offset, info_, offset, &buffer, &owner_->debug_info_patches_);
465 owner_->builder_->GetDebugInfo()->WriteFully(buffer.data(), buffer.size());
466 }
467
468 // Write table into .debug_loc which describes location of dex register.
469 // The dex register might be valid only at some points and it might
470 // move between machine registers and stack.
471 void WriteRegLocation(const MethodDebugInfo* method_info,
David Srbecky2ed15b62016-03-04 11:34:46 +0000472 const std::vector<DexRegisterMap>& dex_register_maps,
David Srbeckyc5bfa972016-02-05 15:49:10 +0000473 uint16_t vreg,
474 bool is64bitValue,
David Srbecky197160d2016-03-07 17:33:57 +0000475 uint64_t compilation_unit_code_address,
David Srbeckyc5bfa972016-02-05 15:49:10 +0000476 uint32_t dex_pc_low = 0,
477 uint32_t dex_pc_high = 0xFFFFFFFF) {
478 WriteDebugLocEntry(method_info,
David Srbecky2ed15b62016-03-04 11:34:46 +0000479 dex_register_maps,
David Srbeckyc5bfa972016-02-05 15:49:10 +0000480 vreg,
481 is64bitValue,
David Srbecky197160d2016-03-07 17:33:57 +0000482 compilation_unit_code_address,
David Srbeckyc5bfa972016-02-05 15:49:10 +0000483 dex_pc_low,
484 dex_pc_high,
485 owner_->builder_->GetIsa(),
486 &info_,
487 &owner_->debug_loc_,
488 &owner_->debug_ranges_);
489 }
490
491 // Linkage name uniquely identifies type.
492 // It is used to determine the dynamic type of objects.
493 // We use the methods_ field of class since it is unique and it is not moved by the GC.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700494 void WriteLinkageName(mirror::Class* type) REQUIRES_SHARED(Locks::mutator_lock_) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000495 auto* methods_ptr = type->GetMethodsPtr();
496 if (methods_ptr == nullptr) {
497 // Some types might have no methods. Allocate empty array instead.
498 LinearAlloc* allocator = Runtime::Current()->GetLinearAlloc();
499 void* storage = allocator->Alloc(Thread::Current(), sizeof(LengthPrefixedArray<ArtMethod>));
500 methods_ptr = new (storage) LengthPrefixedArray<ArtMethod>(0);
501 type->SetMethodsPtr(methods_ptr, 0, 0);
502 DCHECK(type->GetMethodsPtr() != nullptr);
503 }
504 char name[32];
505 snprintf(name, sizeof(name), "0x%" PRIXPTR, reinterpret_cast<uintptr_t>(methods_ptr));
506 info_.WriteString(dwarf::DW_AT_linkage_name, name);
507 }
508
509 // Some types are difficult to define as we go since they need
510 // to be enclosed in the right set of namespaces. Therefore we
511 // just define all types lazily at the end of compilation unit.
512 void WriteLazyType(const char* type_descriptor) {
513 if (type_descriptor != nullptr && type_descriptor[0] != 'V') {
514 lazy_types_.emplace(std::string(type_descriptor), info_.size());
515 info_.WriteRef4(dwarf::DW_AT_type, 0);
516 }
517 }
518
519 void FinishLazyTypes() {
520 for (const auto& lazy_type : lazy_types_) {
521 info_.UpdateUint32(lazy_type.second, WriteTypeDeclaration(lazy_type.first));
522 }
523 lazy_types_.clear();
524 }
525
526 private:
527 void WriteName(const char* name) {
528 if (name != nullptr) {
529 info_.WriteString(dwarf::DW_AT_name, name);
530 }
531 }
532
533 // Convert dex type descriptor to DWARF.
534 // Returns offset in the compilation unit.
535 size_t WriteTypeDeclaration(const std::string& desc) {
536 using namespace dwarf; // NOLINT. For easy access to DWARF constants.
537
538 DCHECK(!desc.empty());
Vladimir Marko3bada4b2017-05-19 12:32:47 +0100539 const auto it = type_cache_.find(desc);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000540 if (it != type_cache_.end()) {
541 return it->second;
542 }
543
544 size_t offset;
545 if (desc[0] == 'L') {
546 // Class type. For example: Lpackage/name;
547 size_t class_offset = StartClassTag(desc.c_str());
548 info_.WriteFlagPresent(DW_AT_declaration);
549 EndClassTag();
550 // Reference to the class type.
551 offset = info_.StartTag(DW_TAG_reference_type);
552 info_.WriteRef(DW_AT_type, class_offset);
553 info_.EndTag();
554 } else if (desc[0] == '[') {
555 // Array type.
556 size_t element_type = WriteTypeDeclaration(desc.substr(1));
557 CloseNamespacesAboveDepth(0); // Declare in root namespace.
558 size_t array_type = info_.StartTag(DW_TAG_array_type);
559 info_.WriteFlagPresent(DW_AT_declaration);
560 info_.WriteRef(DW_AT_type, element_type);
561 info_.EndTag();
562 offset = info_.StartTag(DW_TAG_reference_type);
563 info_.WriteRef4(DW_AT_type, array_type);
564 info_.EndTag();
565 } else {
566 // Primitive types.
567 DCHECK_EQ(desc.size(), 1u);
568
569 const char* name;
570 uint32_t encoding;
571 uint32_t byte_size;
572 switch (desc[0]) {
573 case 'B':
574 name = "byte";
575 encoding = DW_ATE_signed;
576 byte_size = 1;
577 break;
578 case 'C':
579 name = "char";
580 encoding = DW_ATE_UTF;
581 byte_size = 2;
582 break;
583 case 'D':
584 name = "double";
585 encoding = DW_ATE_float;
586 byte_size = 8;
587 break;
588 case 'F':
589 name = "float";
590 encoding = DW_ATE_float;
591 byte_size = 4;
592 break;
593 case 'I':
594 name = "int";
595 encoding = DW_ATE_signed;
596 byte_size = 4;
597 break;
598 case 'J':
599 name = "long";
600 encoding = DW_ATE_signed;
601 byte_size = 8;
602 break;
603 case 'S':
604 name = "short";
605 encoding = DW_ATE_signed;
606 byte_size = 2;
607 break;
608 case 'Z':
609 name = "boolean";
610 encoding = DW_ATE_boolean;
611 byte_size = 1;
612 break;
613 case 'V':
614 LOG(FATAL) << "Void type should not be encoded";
615 UNREACHABLE();
616 default:
617 LOG(FATAL) << "Unknown dex type descriptor: \"" << desc << "\"";
618 UNREACHABLE();
619 }
620 CloseNamespacesAboveDepth(0); // Declare in root namespace.
621 offset = info_.StartTag(DW_TAG_base_type);
622 WriteName(name);
623 info_.WriteData1(DW_AT_encoding, encoding);
624 info_.WriteData1(DW_AT_byte_size, byte_size);
625 info_.EndTag();
626 }
627
628 type_cache_.emplace(desc, offset);
629 return offset;
630 }
631
632 // Start DW_TAG_class_type tag nested in DW_TAG_namespace tags.
633 // Returns offset of the class tag in the compilation unit.
634 size_t StartClassTag(const char* desc) {
635 std::string name = SetNamespaceForClass(desc);
636 size_t offset = info_.StartTag(dwarf::DW_TAG_class_type);
637 WriteName(name.c_str());
638 return offset;
639 }
640
641 void EndClassTag() {
642 info_.EndTag();
643 }
644
645 // Set the current namespace nesting to one required by the given class.
646 // Returns the class name with namespaces, 'L', and ';' stripped.
647 std::string SetNamespaceForClass(const char* desc) {
648 DCHECK(desc != nullptr && desc[0] == 'L');
649 desc++; // Skip the initial 'L'.
650 size_t depth = 0;
651 for (const char* end; (end = strchr(desc, '/')) != nullptr; desc = end + 1, ++depth) {
652 // Check whether the name at this depth is already what we need.
653 if (depth < current_namespace_.size()) {
654 const std::string& name = current_namespace_[depth];
655 if (name.compare(0, name.size(), desc, end - desc) == 0) {
656 continue;
657 }
658 }
659 // Otherwise we need to open a new namespace tag at this depth.
660 CloseNamespacesAboveDepth(depth);
661 info_.StartTag(dwarf::DW_TAG_namespace);
662 std::string name(desc, end - desc);
663 WriteName(name.c_str());
664 current_namespace_.push_back(std::move(name));
665 }
666 CloseNamespacesAboveDepth(depth);
667 return std::string(desc, strchr(desc, ';') - desc);
668 }
669
670 // Close namespace tags to reach the given nesting depth.
671 void CloseNamespacesAboveDepth(size_t depth) {
672 DCHECK_LE(depth, current_namespace_.size());
673 while (current_namespace_.size() > depth) {
674 info_.EndTag();
675 current_namespace_.pop_back();
676 }
677 }
678
679 // For access to the ELF sections.
680 ElfDebugInfoWriter<ElfTypes>* owner_;
681 // Temporary buffer to create and store the entries.
682 dwarf::DebugInfoEntryWriter<> info_;
683 // Cache of already translated type descriptors.
684 std::map<std::string, size_t> type_cache_; // type_desc -> definition_offset.
685 // 32-bit references which need to be resolved to a type later.
686 // Given type may be used multiple times. Therefore we need a multimap.
687 std::multimap<std::string, size_t> lazy_types_; // type_desc -> patch_offset.
688 // The current set of open namespace tags which are active and not closed yet.
689 std::vector<std::string> current_namespace_;
690};
691
692} // namespace debug
693} // namespace art
694
695#endif // ART_COMPILER_DEBUG_ELF_DEBUG_INFO_WRITER_H_
696