blob: eed032f88d0aa73717db67b07cfc8ab66fc38463 [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
24#include "debug/dwarf/debug_abbrev_writer.h"
25#include "debug/dwarf/debug_info_entry_writer.h"
26#include "debug/elf_compilation_unit.h"
27#include "debug/elf_debug_loc_writer.h"
28#include "debug/method_debug_info.h"
29#include "dex_file-inl.h"
30#include "dex_file.h"
31#include "elf_builder.h"
32#include "linear_alloc.h"
33#include "mirror/array.h"
34#include "mirror/class-inl.h"
35#include "mirror/class.h"
36
37namespace art {
38namespace debug {
39
40typedef std::vector<DexFile::LocalInfo> LocalInfos;
41
42static void LocalInfoCallback(void* ctx, const DexFile::LocalInfo& entry) {
43 static_cast<LocalInfos*>(ctx)->push_back(entry);
44}
45
46static std::vector<const char*> GetParamNames(const MethodDebugInfo* mi) {
47 std::vector<const char*> names;
48 if (mi->code_item != nullptr) {
49 const uint8_t* stream = mi->dex_file->GetDebugInfoStream(mi->code_item);
50 if (stream != nullptr) {
51 DecodeUnsignedLeb128(&stream); // line.
52 uint32_t parameters_size = DecodeUnsignedLeb128(&stream);
53 for (uint32_t i = 0; i < parameters_size; ++i) {
54 uint32_t id = DecodeUnsignedLeb128P1(&stream);
55 names.push_back(mi->dex_file->StringDataByIdx(id));
56 }
57 }
58 }
59 return names;
60}
61
62// Helper class to write .debug_info and its supporting sections.
63template<typename ElfTypes>
64class ElfDebugInfoWriter {
65 using Elf_Addr = typename ElfTypes::Addr;
66
67 public:
68 explicit ElfDebugInfoWriter(ElfBuilder<ElfTypes>* builder)
69 : builder_(builder),
70 debug_abbrev_(&debug_abbrev_buffer_) {
71 }
72
73 void Start() {
74 builder_->GetDebugInfo()->Start();
75 }
76
77 void End(bool write_oat_patches) {
78 builder_->GetDebugInfo()->End();
79 if (write_oat_patches) {
80 builder_->WritePatches(".debug_info.oat_patches",
81 ArrayRef<const uintptr_t>(debug_info_patches_));
82 }
83 builder_->WriteSection(".debug_abbrev", &debug_abbrev_buffer_);
84 if (!debug_loc_.empty()) {
85 builder_->WriteSection(".debug_loc", &debug_loc_);
86 }
87 if (!debug_ranges_.empty()) {
88 builder_->WriteSection(".debug_ranges", &debug_ranges_);
89 }
90 }
91
92 private:
93 ElfBuilder<ElfTypes>* builder_;
94 std::vector<uintptr_t> debug_info_patches_;
95 std::vector<uint8_t> debug_abbrev_buffer_;
96 dwarf::DebugAbbrevWriter<> debug_abbrev_;
97 std::vector<uint8_t> debug_loc_;
98 std::vector<uint8_t> debug_ranges_;
99
100 std::unordered_set<const char*> defined_dex_classes_; // For CHECKs only.
101
102 template<typename ElfTypes2>
103 friend class ElfCompilationUnitWriter;
104};
105
106// Helper class to write one compilation unit.
107// It holds helper methods and temporary state.
108template<typename ElfTypes>
109class ElfCompilationUnitWriter {
110 using Elf_Addr = typename ElfTypes::Addr;
111
112 public:
113 explicit ElfCompilationUnitWriter(ElfDebugInfoWriter<ElfTypes>* owner)
114 : owner_(owner),
115 info_(Is64BitInstructionSet(owner_->builder_->GetIsa()), &owner->debug_abbrev_) {
116 }
117
118 void Write(const ElfCompilationUnit& compilation_unit) {
119 CHECK(!compilation_unit.methods.empty());
120 const Elf_Addr text_address = owner_->builder_->GetText()->Exists()
121 ? owner_->builder_->GetText()->GetAddress()
122 : 0;
123 const uintptr_t cu_size = compilation_unit.high_pc - compilation_unit.low_pc;
124 using namespace dwarf; // NOLINT. For easy access to DWARF constants.
125
126 info_.StartTag(DW_TAG_compile_unit);
127 info_.WriteString(DW_AT_producer, "Android dex2oat");
128 info_.WriteData1(DW_AT_language, DW_LANG_Java);
129 info_.WriteString(DW_AT_comp_dir, "$JAVA_SRC_ROOT");
130 info_.WriteAddr(DW_AT_low_pc, text_address + compilation_unit.low_pc);
131 info_.WriteUdata(DW_AT_high_pc, dchecked_integral_cast<uint32_t>(cu_size));
132 info_.WriteSecOffset(DW_AT_stmt_list, compilation_unit.debug_line_offset);
133
134 const char* last_dex_class_desc = nullptr;
135 for (auto mi : compilation_unit.methods) {
136 const DexFile* dex = mi->dex_file;
137 const DexFile::CodeItem* dex_code = mi->code_item;
138 const DexFile::MethodId& dex_method = dex->GetMethodId(mi->dex_method_index);
139 const DexFile::ProtoId& dex_proto = dex->GetMethodPrototype(dex_method);
140 const DexFile::TypeList* dex_params = dex->GetProtoParameters(dex_proto);
141 const char* dex_class_desc = dex->GetMethodDeclaringClassDescriptor(dex_method);
142 const bool is_static = (mi->access_flags & kAccStatic) != 0;
143
144 // Enclose the method in correct class definition.
145 if (last_dex_class_desc != dex_class_desc) {
146 if (last_dex_class_desc != nullptr) {
147 EndClassTag();
148 }
149 // Write reference tag for the class we are about to declare.
150 size_t reference_tag_offset = info_.StartTag(DW_TAG_reference_type);
151 type_cache_.emplace(std::string(dex_class_desc), reference_tag_offset);
152 size_t type_attrib_offset = info_.size();
153 info_.WriteRef4(DW_AT_type, 0);
154 info_.EndTag();
155 // Declare the class that owns this method.
156 size_t class_offset = StartClassTag(dex_class_desc);
157 info_.UpdateUint32(type_attrib_offset, class_offset);
158 info_.WriteFlagPresent(DW_AT_declaration);
159 // Check that each class is defined only once.
160 bool unique = owner_->defined_dex_classes_.insert(dex_class_desc).second;
161 CHECK(unique) << "Redefinition of " << dex_class_desc;
162 last_dex_class_desc = dex_class_desc;
163 }
164
165 int start_depth = info_.Depth();
166 info_.StartTag(DW_TAG_subprogram);
167 WriteName(dex->GetMethodName(dex_method));
168 info_.WriteAddr(DW_AT_low_pc, text_address + mi->low_pc);
169 info_.WriteUdata(DW_AT_high_pc, dchecked_integral_cast<uint32_t>(mi->high_pc-mi->low_pc));
170 std::vector<uint8_t> expr_buffer;
171 Expression expr(&expr_buffer);
172 expr.WriteOpCallFrameCfa();
173 info_.WriteExprLoc(DW_AT_frame_base, expr);
174 WriteLazyType(dex->GetReturnTypeDescriptor(dex_proto));
175
176 // Write parameters. DecodeDebugLocalInfo returns them as well, but it does not
177 // guarantee order or uniqueness so it is safer to iterate over them manually.
178 // DecodeDebugLocalInfo might not also be available if there is no debug info.
179 std::vector<const char*> param_names = GetParamNames(mi);
180 uint32_t arg_reg = 0;
181 if (!is_static) {
182 info_.StartTag(DW_TAG_formal_parameter);
183 WriteName("this");
184 info_.WriteFlagPresent(DW_AT_artificial);
185 WriteLazyType(dex_class_desc);
186 if (dex_code != nullptr) {
187 // Write the stack location of the parameter.
188 const uint32_t vreg = dex_code->registers_size_ - dex_code->ins_size_ + arg_reg;
189 const bool is64bitValue = false;
190 WriteRegLocation(mi, vreg, is64bitValue, compilation_unit.low_pc);
191 }
192 arg_reg++;
193 info_.EndTag();
194 }
195 if (dex_params != nullptr) {
196 for (uint32_t i = 0; i < dex_params->Size(); ++i) {
197 info_.StartTag(DW_TAG_formal_parameter);
198 // Parameter names may not be always available.
199 if (i < param_names.size()) {
200 WriteName(param_names[i]);
201 }
202 // Write the type.
203 const char* type_desc = dex->StringByTypeIdx(dex_params->GetTypeItem(i).type_idx_);
204 WriteLazyType(type_desc);
205 const bool is64bitValue = type_desc[0] == 'D' || type_desc[0] == 'J';
206 if (dex_code != nullptr) {
207 // Write the stack location of the parameter.
208 const uint32_t vreg = dex_code->registers_size_ - dex_code->ins_size_ + arg_reg;
209 WriteRegLocation(mi, vreg, is64bitValue, compilation_unit.low_pc);
210 }
211 arg_reg += is64bitValue ? 2 : 1;
212 info_.EndTag();
213 }
214 if (dex_code != nullptr) {
215 DCHECK_EQ(arg_reg, dex_code->ins_size_);
216 }
217 }
218
219 // Write local variables.
220 LocalInfos local_infos;
221 if (dex->DecodeDebugLocalInfo(dex_code,
222 is_static,
223 mi->dex_method_index,
224 LocalInfoCallback,
225 &local_infos)) {
226 for (const DexFile::LocalInfo& var : local_infos) {
227 if (var.reg_ < dex_code->registers_size_ - dex_code->ins_size_) {
228 info_.StartTag(DW_TAG_variable);
229 WriteName(var.name_);
230 WriteLazyType(var.descriptor_);
231 bool is64bitValue = var.descriptor_[0] == 'D' || var.descriptor_[0] == 'J';
232 WriteRegLocation(mi, var.reg_, is64bitValue, compilation_unit.low_pc,
233 var.start_address_, var.end_address_);
234 info_.EndTag();
235 }
236 }
237 }
238
239 info_.EndTag();
240 CHECK_EQ(info_.Depth(), start_depth); // Balanced start/end.
241 }
242 if (last_dex_class_desc != nullptr) {
243 EndClassTag();
244 }
245 FinishLazyTypes();
246 CloseNamespacesAboveDepth(0);
247 info_.EndTag(); // DW_TAG_compile_unit
248 CHECK_EQ(info_.Depth(), 0);
249 std::vector<uint8_t> buffer;
250 buffer.reserve(info_.data()->size() + KB);
251 const size_t offset = owner_->builder_->GetDebugInfo()->GetSize();
252 // All compilation units share single table which is at the start of .debug_abbrev.
253 const size_t debug_abbrev_offset = 0;
254 WriteDebugInfoCU(debug_abbrev_offset, info_, offset, &buffer, &owner_->debug_info_patches_);
255 owner_->builder_->GetDebugInfo()->WriteFully(buffer.data(), buffer.size());
256 }
257
258 void Write(const ArrayRef<mirror::Class*>& types) SHARED_REQUIRES(Locks::mutator_lock_) {
259 using namespace dwarf; // NOLINT. For easy access to DWARF constants.
260
261 info_.StartTag(DW_TAG_compile_unit);
262 info_.WriteString(DW_AT_producer, "Android dex2oat");
263 info_.WriteData1(DW_AT_language, DW_LANG_Java);
264
265 // Base class references to be patched at the end.
266 std::map<size_t, mirror::Class*> base_class_references;
267
268 // Already written declarations or definitions.
269 std::map<mirror::Class*, size_t> class_declarations;
270
271 std::vector<uint8_t> expr_buffer;
272 for (mirror::Class* type : types) {
273 if (type->IsPrimitive()) {
274 // For primitive types the definition and the declaration is the same.
275 if (type->GetPrimitiveType() != Primitive::kPrimVoid) {
276 WriteTypeDeclaration(type->GetDescriptor(nullptr));
277 }
278 } else if (type->IsArrayClass()) {
279 mirror::Class* element_type = type->GetComponentType();
280 uint32_t component_size = type->GetComponentSize();
281 uint32_t data_offset = mirror::Array::DataOffset(component_size).Uint32Value();
282 uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value();
283
284 CloseNamespacesAboveDepth(0); // Declare in root namespace.
285 info_.StartTag(DW_TAG_array_type);
286 std::string descriptor_string;
287 WriteLazyType(element_type->GetDescriptor(&descriptor_string));
288 WriteLinkageName(type);
289 info_.WriteUdata(DW_AT_data_member_location, data_offset);
290 info_.StartTag(DW_TAG_subrange_type);
291 Expression count_expr(&expr_buffer);
292 count_expr.WriteOpPushObjectAddress();
293 count_expr.WriteOpPlusUconst(length_offset);
294 count_expr.WriteOpDerefSize(4); // Array length is always 32-bit wide.
295 info_.WriteExprLoc(DW_AT_count, count_expr);
296 info_.EndTag(); // DW_TAG_subrange_type.
297 info_.EndTag(); // DW_TAG_array_type.
298 } else if (type->IsInterface()) {
299 // Skip. Variables cannot have an interface as a dynamic type.
300 // We do not expose the interface information to the debugger in any way.
301 } else {
302 std::string descriptor_string;
303 const char* desc = type->GetDescriptor(&descriptor_string);
304 size_t class_offset = StartClassTag(desc);
305 class_declarations.emplace(type, class_offset);
306
307 if (!type->IsVariableSize()) {
308 info_.WriteUdata(DW_AT_byte_size, type->GetObjectSize());
309 }
310
311 WriteLinkageName(type);
312
313 if (type->IsObjectClass()) {
314 // Generate artificial member which is used to get the dynamic type of variable.
315 // The run-time value of this field will correspond to linkage name of some type.
316 // We need to do it only once in j.l.Object since all other types inherit it.
317 info_.StartTag(DW_TAG_member);
318 WriteName(".dynamic_type");
319 WriteLazyType(sizeof(uintptr_t) == 8 ? "J" : "I");
320 info_.WriteFlagPresent(DW_AT_artificial);
321 // Create DWARF expression to get the value of the methods_ field.
322 Expression expr(&expr_buffer);
323 // The address of the object has been implicitly pushed on the stack.
324 // Dereference the klass_ field of Object (32-bit; possibly poisoned).
325 DCHECK_EQ(type->ClassOffset().Uint32Value(), 0u);
326 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Class>), 4u);
327 expr.WriteOpDerefSize(4);
328 if (kPoisonHeapReferences) {
329 expr.WriteOpNeg();
330 // DWARF stack is pointer sized. Ensure that the high bits are clear.
331 expr.WriteOpConstu(0xFFFFFFFF);
332 expr.WriteOpAnd();
333 }
334 // Add offset to the methods_ field.
335 expr.WriteOpPlusUconst(mirror::Class::MethodsOffset().Uint32Value());
336 // Top of stack holds the location of the field now.
337 info_.WriteExprLoc(DW_AT_data_member_location, expr);
338 info_.EndTag(); // DW_TAG_member.
339 }
340
341 // Base class.
342 mirror::Class* base_class = type->GetSuperClass();
343 if (base_class != nullptr) {
344 info_.StartTag(DW_TAG_inheritance);
345 base_class_references.emplace(info_.size(), base_class);
346 info_.WriteRef4(DW_AT_type, 0);
347 info_.WriteUdata(DW_AT_data_member_location, 0);
348 info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_public);
349 info_.EndTag(); // DW_TAG_inheritance.
350 }
351
352 // Member variables.
353 for (uint32_t i = 0, count = type->NumInstanceFields(); i < count; ++i) {
354 ArtField* field = type->GetInstanceField(i);
355 info_.StartTag(DW_TAG_member);
356 WriteName(field->GetName());
357 WriteLazyType(field->GetTypeDescriptor());
358 info_.WriteUdata(DW_AT_data_member_location, field->GetOffset().Uint32Value());
359 uint32_t access_flags = field->GetAccessFlags();
360 if (access_flags & kAccPublic) {
361 info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_public);
362 } else if (access_flags & kAccProtected) {
363 info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_protected);
364 } else if (access_flags & kAccPrivate) {
365 info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_private);
366 }
367 info_.EndTag(); // DW_TAG_member.
368 }
369
370 if (type->IsStringClass()) {
371 // Emit debug info about an artifical class member for java.lang.String which represents
372 // the first element of the data stored in a string instance. Consumers of the debug
373 // info will be able to read the content of java.lang.String based on the count (real
374 // field) and based on the location of this data member.
375 info_.StartTag(DW_TAG_member);
376 WriteName("value");
377 // We don't support fields with C like array types so we just say its type is java char.
378 WriteLazyType("C"); // char.
379 info_.WriteUdata(DW_AT_data_member_location,
380 mirror::String::ValueOffset().Uint32Value());
381 info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_private);
382 info_.EndTag(); // DW_TAG_member.
383 }
384
385 EndClassTag();
386 }
387 }
388
389 // Write base class declarations.
390 for (const auto& base_class_reference : base_class_references) {
391 size_t reference_offset = base_class_reference.first;
392 mirror::Class* base_class = base_class_reference.second;
393 const auto& it = class_declarations.find(base_class);
394 if (it != class_declarations.end()) {
395 info_.UpdateUint32(reference_offset, it->second);
396 } else {
397 // Declare base class. We can not use the standard WriteLazyType
398 // since we want to avoid the DW_TAG_reference_tag wrapping.
399 std::string tmp_storage;
400 const char* base_class_desc = base_class->GetDescriptor(&tmp_storage);
401 size_t base_class_declaration_offset = StartClassTag(base_class_desc);
402 info_.WriteFlagPresent(DW_AT_declaration);
403 WriteLinkageName(base_class);
404 EndClassTag();
405 class_declarations.emplace(base_class, base_class_declaration_offset);
406 info_.UpdateUint32(reference_offset, base_class_declaration_offset);
407 }
408 }
409
410 FinishLazyTypes();
411 CloseNamespacesAboveDepth(0);
412 info_.EndTag(); // DW_TAG_compile_unit.
413 CHECK_EQ(info_.Depth(), 0);
414 std::vector<uint8_t> buffer;
415 buffer.reserve(info_.data()->size() + KB);
416 const size_t offset = owner_->builder_->GetDebugInfo()->GetSize();
417 // All compilation units share single table which is at the start of .debug_abbrev.
418 const size_t debug_abbrev_offset = 0;
419 WriteDebugInfoCU(debug_abbrev_offset, info_, offset, &buffer, &owner_->debug_info_patches_);
420 owner_->builder_->GetDebugInfo()->WriteFully(buffer.data(), buffer.size());
421 }
422
423 // Write table into .debug_loc which describes location of dex register.
424 // The dex register might be valid only at some points and it might
425 // move between machine registers and stack.
426 void WriteRegLocation(const MethodDebugInfo* method_info,
427 uint16_t vreg,
428 bool is64bitValue,
429 uint32_t compilation_unit_low_pc,
430 uint32_t dex_pc_low = 0,
431 uint32_t dex_pc_high = 0xFFFFFFFF) {
432 WriteDebugLocEntry(method_info,
433 vreg,
434 is64bitValue,
435 compilation_unit_low_pc,
436 dex_pc_low,
437 dex_pc_high,
438 owner_->builder_->GetIsa(),
439 &info_,
440 &owner_->debug_loc_,
441 &owner_->debug_ranges_);
442 }
443
444 // Linkage name uniquely identifies type.
445 // It is used to determine the dynamic type of objects.
446 // We use the methods_ field of class since it is unique and it is not moved by the GC.
447 void WriteLinkageName(mirror::Class* type) SHARED_REQUIRES(Locks::mutator_lock_) {
448 auto* methods_ptr = type->GetMethodsPtr();
449 if (methods_ptr == nullptr) {
450 // Some types might have no methods. Allocate empty array instead.
451 LinearAlloc* allocator = Runtime::Current()->GetLinearAlloc();
452 void* storage = allocator->Alloc(Thread::Current(), sizeof(LengthPrefixedArray<ArtMethod>));
453 methods_ptr = new (storage) LengthPrefixedArray<ArtMethod>(0);
454 type->SetMethodsPtr(methods_ptr, 0, 0);
455 DCHECK(type->GetMethodsPtr() != nullptr);
456 }
457 char name[32];
458 snprintf(name, sizeof(name), "0x%" PRIXPTR, reinterpret_cast<uintptr_t>(methods_ptr));
459 info_.WriteString(dwarf::DW_AT_linkage_name, name);
460 }
461
462 // Some types are difficult to define as we go since they need
463 // to be enclosed in the right set of namespaces. Therefore we
464 // just define all types lazily at the end of compilation unit.
465 void WriteLazyType(const char* type_descriptor) {
466 if (type_descriptor != nullptr && type_descriptor[0] != 'V') {
467 lazy_types_.emplace(std::string(type_descriptor), info_.size());
468 info_.WriteRef4(dwarf::DW_AT_type, 0);
469 }
470 }
471
472 void FinishLazyTypes() {
473 for (const auto& lazy_type : lazy_types_) {
474 info_.UpdateUint32(lazy_type.second, WriteTypeDeclaration(lazy_type.first));
475 }
476 lazy_types_.clear();
477 }
478
479 private:
480 void WriteName(const char* name) {
481 if (name != nullptr) {
482 info_.WriteString(dwarf::DW_AT_name, name);
483 }
484 }
485
486 // Convert dex type descriptor to DWARF.
487 // Returns offset in the compilation unit.
488 size_t WriteTypeDeclaration(const std::string& desc) {
489 using namespace dwarf; // NOLINT. For easy access to DWARF constants.
490
491 DCHECK(!desc.empty());
492 const auto& it = type_cache_.find(desc);
493 if (it != type_cache_.end()) {
494 return it->second;
495 }
496
497 size_t offset;
498 if (desc[0] == 'L') {
499 // Class type. For example: Lpackage/name;
500 size_t class_offset = StartClassTag(desc.c_str());
501 info_.WriteFlagPresent(DW_AT_declaration);
502 EndClassTag();
503 // Reference to the class type.
504 offset = info_.StartTag(DW_TAG_reference_type);
505 info_.WriteRef(DW_AT_type, class_offset);
506 info_.EndTag();
507 } else if (desc[0] == '[') {
508 // Array type.
509 size_t element_type = WriteTypeDeclaration(desc.substr(1));
510 CloseNamespacesAboveDepth(0); // Declare in root namespace.
511 size_t array_type = info_.StartTag(DW_TAG_array_type);
512 info_.WriteFlagPresent(DW_AT_declaration);
513 info_.WriteRef(DW_AT_type, element_type);
514 info_.EndTag();
515 offset = info_.StartTag(DW_TAG_reference_type);
516 info_.WriteRef4(DW_AT_type, array_type);
517 info_.EndTag();
518 } else {
519 // Primitive types.
520 DCHECK_EQ(desc.size(), 1u);
521
522 const char* name;
523 uint32_t encoding;
524 uint32_t byte_size;
525 switch (desc[0]) {
526 case 'B':
527 name = "byte";
528 encoding = DW_ATE_signed;
529 byte_size = 1;
530 break;
531 case 'C':
532 name = "char";
533 encoding = DW_ATE_UTF;
534 byte_size = 2;
535 break;
536 case 'D':
537 name = "double";
538 encoding = DW_ATE_float;
539 byte_size = 8;
540 break;
541 case 'F':
542 name = "float";
543 encoding = DW_ATE_float;
544 byte_size = 4;
545 break;
546 case 'I':
547 name = "int";
548 encoding = DW_ATE_signed;
549 byte_size = 4;
550 break;
551 case 'J':
552 name = "long";
553 encoding = DW_ATE_signed;
554 byte_size = 8;
555 break;
556 case 'S':
557 name = "short";
558 encoding = DW_ATE_signed;
559 byte_size = 2;
560 break;
561 case 'Z':
562 name = "boolean";
563 encoding = DW_ATE_boolean;
564 byte_size = 1;
565 break;
566 case 'V':
567 LOG(FATAL) << "Void type should not be encoded";
568 UNREACHABLE();
569 default:
570 LOG(FATAL) << "Unknown dex type descriptor: \"" << desc << "\"";
571 UNREACHABLE();
572 }
573 CloseNamespacesAboveDepth(0); // Declare in root namespace.
574 offset = info_.StartTag(DW_TAG_base_type);
575 WriteName(name);
576 info_.WriteData1(DW_AT_encoding, encoding);
577 info_.WriteData1(DW_AT_byte_size, byte_size);
578 info_.EndTag();
579 }
580
581 type_cache_.emplace(desc, offset);
582 return offset;
583 }
584
585 // Start DW_TAG_class_type tag nested in DW_TAG_namespace tags.
586 // Returns offset of the class tag in the compilation unit.
587 size_t StartClassTag(const char* desc) {
588 std::string name = SetNamespaceForClass(desc);
589 size_t offset = info_.StartTag(dwarf::DW_TAG_class_type);
590 WriteName(name.c_str());
591 return offset;
592 }
593
594 void EndClassTag() {
595 info_.EndTag();
596 }
597
598 // Set the current namespace nesting to one required by the given class.
599 // Returns the class name with namespaces, 'L', and ';' stripped.
600 std::string SetNamespaceForClass(const char* desc) {
601 DCHECK(desc != nullptr && desc[0] == 'L');
602 desc++; // Skip the initial 'L'.
603 size_t depth = 0;
604 for (const char* end; (end = strchr(desc, '/')) != nullptr; desc = end + 1, ++depth) {
605 // Check whether the name at this depth is already what we need.
606 if (depth < current_namespace_.size()) {
607 const std::string& name = current_namespace_[depth];
608 if (name.compare(0, name.size(), desc, end - desc) == 0) {
609 continue;
610 }
611 }
612 // Otherwise we need to open a new namespace tag at this depth.
613 CloseNamespacesAboveDepth(depth);
614 info_.StartTag(dwarf::DW_TAG_namespace);
615 std::string name(desc, end - desc);
616 WriteName(name.c_str());
617 current_namespace_.push_back(std::move(name));
618 }
619 CloseNamespacesAboveDepth(depth);
620 return std::string(desc, strchr(desc, ';') - desc);
621 }
622
623 // Close namespace tags to reach the given nesting depth.
624 void CloseNamespacesAboveDepth(size_t depth) {
625 DCHECK_LE(depth, current_namespace_.size());
626 while (current_namespace_.size() > depth) {
627 info_.EndTag();
628 current_namespace_.pop_back();
629 }
630 }
631
632 // For access to the ELF sections.
633 ElfDebugInfoWriter<ElfTypes>* owner_;
634 // Temporary buffer to create and store the entries.
635 dwarf::DebugInfoEntryWriter<> info_;
636 // Cache of already translated type descriptors.
637 std::map<std::string, size_t> type_cache_; // type_desc -> definition_offset.
638 // 32-bit references which need to be resolved to a type later.
639 // Given type may be used multiple times. Therefore we need a multimap.
640 std::multimap<std::string, size_t> lazy_types_; // type_desc -> patch_offset.
641 // The current set of open namespace tags which are active and not closed yet.
642 std::vector<std::string> current_namespace_;
643};
644
645} // namespace debug
646} // namespace art
647
648#endif // ART_COMPILER_DEBUG_ELF_DEBUG_INFO_WRITER_H_
649