blob: a63f241f531243b3ee1e224f8b5561599d51cad3 [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
David Srbeckyc5bfa972016-02-05 15:49:10 +000044static std::vector<const char*> GetParamNames(const MethodDebugInfo* mi) {
45 std::vector<const char*> names;
Mathieu Chartier3e2e1232018-09-11 12:35:30 -070046 DCHECK(mi->dex_file != nullptr);
Mathieu Chartier8892c6b2018-01-09 15:10:17 -080047 CodeItemDebugInfoAccessor accessor(*mi->dex_file, mi->code_item, mi->dex_method_index);
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -080048 if (accessor.HasCodeItem()) {
Mathieu Chartier3e2e1232018-09-11 12:35:30 -070049 accessor.VisitParameterNames([&](const dex::StringIndex& id) {
50 names.push_back(mi->dex_file->StringDataByIdx(id));
51 });
David Srbeckyc5bfa972016-02-05 15:49:10 +000052 }
53 return names;
54}
55
56// Helper class to write .debug_info and its supporting sections.
57template<typename ElfTypes>
58class ElfDebugInfoWriter {
59 using Elf_Addr = typename ElfTypes::Addr;
60
61 public:
Vladimir Marko74527972016-11-29 15:57:32 +000062 explicit ElfDebugInfoWriter(linker::ElfBuilder<ElfTypes>* builder)
David Srbeckyc5bfa972016-02-05 15:49:10 +000063 : builder_(builder),
64 debug_abbrev_(&debug_abbrev_buffer_) {
65 }
66
67 void Start() {
68 builder_->GetDebugInfo()->Start();
69 }
70
71 void End(bool write_oat_patches) {
72 builder_->GetDebugInfo()->End();
73 if (write_oat_patches) {
74 builder_->WritePatches(".debug_info.oat_patches",
75 ArrayRef<const uintptr_t>(debug_info_patches_));
76 }
77 builder_->WriteSection(".debug_abbrev", &debug_abbrev_buffer_);
78 if (!debug_loc_.empty()) {
79 builder_->WriteSection(".debug_loc", &debug_loc_);
80 }
81 if (!debug_ranges_.empty()) {
82 builder_->WriteSection(".debug_ranges", &debug_ranges_);
83 }
84 }
85
86 private:
Vladimir Marko74527972016-11-29 15:57:32 +000087 linker::ElfBuilder<ElfTypes>* builder_;
David Srbeckyc5bfa972016-02-05 15:49:10 +000088 std::vector<uintptr_t> debug_info_patches_;
89 std::vector<uint8_t> debug_abbrev_buffer_;
90 dwarf::DebugAbbrevWriter<> debug_abbrev_;
91 std::vector<uint8_t> debug_loc_;
92 std::vector<uint8_t> debug_ranges_;
93
94 std::unordered_set<const char*> defined_dex_classes_; // For CHECKs only.
95
96 template<typename ElfTypes2>
97 friend class ElfCompilationUnitWriter;
98};
99
100// Helper class to write one compilation unit.
101// It holds helper methods and temporary state.
102template<typename ElfTypes>
103class ElfCompilationUnitWriter {
104 using Elf_Addr = typename ElfTypes::Addr;
105
106 public:
107 explicit ElfCompilationUnitWriter(ElfDebugInfoWriter<ElfTypes>* owner)
108 : owner_(owner),
109 info_(Is64BitInstructionSet(owner_->builder_->GetIsa()), &owner->debug_abbrev_) {
110 }
111
112 void Write(const ElfCompilationUnit& compilation_unit) {
113 CHECK(!compilation_unit.methods.empty());
David Srbecky197160d2016-03-07 17:33:57 +0000114 const Elf_Addr base_address = compilation_unit.is_code_address_text_relative
David Srbeckyc5bfa972016-02-05 15:49:10 +0000115 ? owner_->builder_->GetText()->GetAddress()
116 : 0;
David Srbecky56da23c2017-09-08 19:59:15 +0100117 const bool is64bit = Is64BitInstructionSet(owner_->builder_->GetIsa());
David Srbeckyc5bfa972016-02-05 15:49:10 +0000118 using namespace dwarf; // NOLINT. For easy access to DWARF constants.
119
120 info_.StartTag(DW_TAG_compile_unit);
121 info_.WriteString(DW_AT_producer, "Android dex2oat");
122 info_.WriteData1(DW_AT_language, DW_LANG_Java);
123 info_.WriteString(DW_AT_comp_dir, "$JAVA_SRC_ROOT");
David Srbecky56da23c2017-09-08 19:59:15 +0100124 // The low_pc acts as base address for several other addresses/ranges.
David Srbecky197160d2016-03-07 17:33:57 +0000125 info_.WriteAddr(DW_AT_low_pc, base_address + compilation_unit.code_address);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000126 info_.WriteSecOffset(DW_AT_stmt_list, compilation_unit.debug_line_offset);
127
David Srbecky56da23c2017-09-08 19:59:15 +0100128 // Write .debug_ranges entries covering code ranges of the whole compilation unit.
129 dwarf::Writer<> debug_ranges(&owner_->debug_ranges_);
130 info_.WriteSecOffset(DW_AT_ranges, owner_->debug_ranges_.size());
131 for (auto mi : compilation_unit.methods) {
132 uint64_t low_pc = mi->code_address - compilation_unit.code_address;
133 uint64_t high_pc = low_pc + mi->code_size;
134 if (is64bit) {
135 debug_ranges.PushUint64(low_pc);
136 debug_ranges.PushUint64(high_pc);
137 } else {
138 debug_ranges.PushUint32(low_pc);
139 debug_ranges.PushUint32(high_pc);
140 }
141 }
142 if (is64bit) {
143 debug_ranges.PushUint64(0); // End of list.
144 debug_ranges.PushUint64(0);
145 } else {
146 debug_ranges.PushUint32(0); // End of list.
147 debug_ranges.PushUint32(0);
148 }
149
David Srbeckyc5bfa972016-02-05 15:49:10 +0000150 const char* last_dex_class_desc = nullptr;
151 for (auto mi : compilation_unit.methods) {
David Srbecky09c2a6b2016-03-11 17:11:44 +0000152 DCHECK(mi->dex_file != nullptr);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000153 const DexFile* dex = mi->dex_file;
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800154 CodeItemDebugInfoAccessor accessor(*dex, mi->code_item, mi->dex_method_index);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800155 const dex::MethodId& dex_method = dex->GetMethodId(mi->dex_method_index);
156 const dex::ProtoId& dex_proto = dex->GetMethodPrototype(dex_method);
157 const dex::TypeList* dex_params = dex->GetProtoParameters(dex_proto);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000158 const char* dex_class_desc = dex->GetMethodDeclaringClassDescriptor(dex_method);
159 const bool is_static = (mi->access_flags & kAccStatic) != 0;
160
161 // Enclose the method in correct class definition.
162 if (last_dex_class_desc != dex_class_desc) {
163 if (last_dex_class_desc != nullptr) {
164 EndClassTag();
165 }
166 // Write reference tag for the class we are about to declare.
167 size_t reference_tag_offset = info_.StartTag(DW_TAG_reference_type);
168 type_cache_.emplace(std::string(dex_class_desc), reference_tag_offset);
169 size_t type_attrib_offset = info_.size();
170 info_.WriteRef4(DW_AT_type, 0);
171 info_.EndTag();
172 // Declare the class that owns this method.
173 size_t class_offset = StartClassTag(dex_class_desc);
174 info_.UpdateUint32(type_attrib_offset, class_offset);
175 info_.WriteFlagPresent(DW_AT_declaration);
176 // Check that each class is defined only once.
177 bool unique = owner_->defined_dex_classes_.insert(dex_class_desc).second;
178 CHECK(unique) << "Redefinition of " << dex_class_desc;
179 last_dex_class_desc = dex_class_desc;
180 }
181
182 int start_depth = info_.Depth();
183 info_.StartTag(DW_TAG_subprogram);
184 WriteName(dex->GetMethodName(dex_method));
David Srbecky197160d2016-03-07 17:33:57 +0000185 info_.WriteAddr(DW_AT_low_pc, base_address + mi->code_address);
186 info_.WriteUdata(DW_AT_high_pc, mi->code_size);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000187 std::vector<uint8_t> expr_buffer;
188 Expression expr(&expr_buffer);
189 expr.WriteOpCallFrameCfa();
190 info_.WriteExprLoc(DW_AT_frame_base, expr);
191 WriteLazyType(dex->GetReturnTypeDescriptor(dex_proto));
192
David Srbecky2ed15b62016-03-04 11:34:46 +0000193 // Decode dex register locations for all stack maps.
194 // It might be expensive, so do it just once and reuse the result.
David Srbecky9e4bbfb2018-05-31 08:52:16 +0100195 std::unique_ptr<const CodeInfo> code_info;
David Srbecky2ed15b62016-03-04 11:34:46 +0000196 std::vector<DexRegisterMap> dex_reg_maps;
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -0800197 if (accessor.HasCodeItem() && mi->code_info != nullptr) {
David Srbecky9e4bbfb2018-05-31 08:52:16 +0100198 code_info.reset(new CodeInfo(mi->code_info));
David Srbecky93bd3612018-07-02 19:30:18 +0100199 for (StackMap stack_map : code_info->GetStackMaps()) {
David Srbeckyfd89b072018-06-03 12:00:22 +0100200 dex_reg_maps.push_back(code_info->GetDexRegisterMapOf(stack_map));
David Srbecky2ed15b62016-03-04 11:34:46 +0000201 }
202 }
203
David Srbeckyc5bfa972016-02-05 15:49:10 +0000204 // Write parameters. DecodeDebugLocalInfo returns them as well, but it does not
205 // guarantee order or uniqueness so it is safer to iterate over them manually.
206 // DecodeDebugLocalInfo might not also be available if there is no debug info.
207 std::vector<const char*> param_names = GetParamNames(mi);
208 uint32_t arg_reg = 0;
209 if (!is_static) {
210 info_.StartTag(DW_TAG_formal_parameter);
211 WriteName("this");
212 info_.WriteFlagPresent(DW_AT_artificial);
213 WriteLazyType(dex_class_desc);
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -0800214 if (accessor.HasCodeItem()) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000215 // Write the stack location of the parameter.
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -0800216 const uint32_t vreg = accessor.RegistersSize() - accessor.InsSize() + arg_reg;
David Srbeckyc5bfa972016-02-05 15:49:10 +0000217 const bool is64bitValue = false;
David Srbecky197160d2016-03-07 17:33:57 +0000218 WriteRegLocation(mi, dex_reg_maps, vreg, is64bitValue, compilation_unit.code_address);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000219 }
220 arg_reg++;
221 info_.EndTag();
222 }
223 if (dex_params != nullptr) {
224 for (uint32_t i = 0; i < dex_params->Size(); ++i) {
225 info_.StartTag(DW_TAG_formal_parameter);
226 // Parameter names may not be always available.
227 if (i < param_names.size()) {
228 WriteName(param_names[i]);
229 }
230 // Write the type.
231 const char* type_desc = dex->StringByTypeIdx(dex_params->GetTypeItem(i).type_idx_);
232 WriteLazyType(type_desc);
233 const bool is64bitValue = type_desc[0] == 'D' || type_desc[0] == 'J';
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -0800234 if (accessor.HasCodeItem()) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000235 // Write the stack location of the parameter.
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -0800236 const uint32_t vreg = accessor.RegistersSize() - accessor.InsSize() + arg_reg;
David Srbecky197160d2016-03-07 17:33:57 +0000237 WriteRegLocation(mi, dex_reg_maps, vreg, is64bitValue, compilation_unit.code_address);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000238 }
239 arg_reg += is64bitValue ? 2 : 1;
240 info_.EndTag();
241 }
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -0800242 if (accessor.HasCodeItem()) {
243 DCHECK_EQ(arg_reg, accessor.InsSize());
David Srbeckyc5bfa972016-02-05 15:49:10 +0000244 }
245 }
246
247 // Write local variables.
Mathieu Chartiere5afbf32018-09-12 17:51:54 -0700248 std::vector<DexFile::LocalInfo> local_infos;
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800249 if (accessor.DecodeDebugLocalInfo(is_static,
250 mi->dex_method_index,
Mathieu Chartiere5afbf32018-09-12 17:51:54 -0700251 [&](const DexFile::LocalInfo& entry) {
252 local_infos.push_back(entry);
253 })) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000254 for (const DexFile::LocalInfo& var : local_infos) {
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -0800255 if (var.reg_ < accessor.RegistersSize() - accessor.InsSize()) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000256 info_.StartTag(DW_TAG_variable);
257 WriteName(var.name_);
258 WriteLazyType(var.descriptor_);
259 bool is64bitValue = var.descriptor_[0] == 'D' || var.descriptor_[0] == 'J';
David Srbecky2ed15b62016-03-04 11:34:46 +0000260 WriteRegLocation(mi,
261 dex_reg_maps,
262 var.reg_,
263 is64bitValue,
David Srbecky197160d2016-03-07 17:33:57 +0000264 compilation_unit.code_address,
David Srbecky2ed15b62016-03-04 11:34:46 +0000265 var.start_address_,
266 var.end_address_);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000267 info_.EndTag();
268 }
269 }
270 }
271
272 info_.EndTag();
273 CHECK_EQ(info_.Depth(), start_depth); // Balanced start/end.
274 }
275 if (last_dex_class_desc != nullptr) {
276 EndClassTag();
277 }
278 FinishLazyTypes();
279 CloseNamespacesAboveDepth(0);
280 info_.EndTag(); // DW_TAG_compile_unit
281 CHECK_EQ(info_.Depth(), 0);
282 std::vector<uint8_t> buffer;
283 buffer.reserve(info_.data()->size() + KB);
David Srbeckye155f4b2017-12-06 15:18:38 +0000284 const size_t offset = owner_->builder_->GetDebugInfo()->GetPosition();
David Srbeckyc5bfa972016-02-05 15:49:10 +0000285 // All compilation units share single table which is at the start of .debug_abbrev.
286 const size_t debug_abbrev_offset = 0;
287 WriteDebugInfoCU(debug_abbrev_offset, info_, offset, &buffer, &owner_->debug_info_patches_);
288 owner_->builder_->GetDebugInfo()->WriteFully(buffer.data(), buffer.size());
289 }
290
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700291 void Write(const ArrayRef<mirror::Class*>& types) REQUIRES_SHARED(Locks::mutator_lock_) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000292 using namespace dwarf; // NOLINT. For easy access to DWARF constants.
293
294 info_.StartTag(DW_TAG_compile_unit);
295 info_.WriteString(DW_AT_producer, "Android dex2oat");
296 info_.WriteData1(DW_AT_language, DW_LANG_Java);
297
298 // Base class references to be patched at the end.
299 std::map<size_t, mirror::Class*> base_class_references;
300
301 // Already written declarations or definitions.
302 std::map<mirror::Class*, size_t> class_declarations;
303
304 std::vector<uint8_t> expr_buffer;
305 for (mirror::Class* type : types) {
306 if (type->IsPrimitive()) {
307 // For primitive types the definition and the declaration is the same.
308 if (type->GetPrimitiveType() != Primitive::kPrimVoid) {
309 WriteTypeDeclaration(type->GetDescriptor(nullptr));
310 }
311 } else if (type->IsArrayClass()) {
312 mirror::Class* element_type = type->GetComponentType();
313 uint32_t component_size = type->GetComponentSize();
314 uint32_t data_offset = mirror::Array::DataOffset(component_size).Uint32Value();
315 uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value();
316
317 CloseNamespacesAboveDepth(0); // Declare in root namespace.
318 info_.StartTag(DW_TAG_array_type);
319 std::string descriptor_string;
320 WriteLazyType(element_type->GetDescriptor(&descriptor_string));
321 WriteLinkageName(type);
322 info_.WriteUdata(DW_AT_data_member_location, data_offset);
323 info_.StartTag(DW_TAG_subrange_type);
324 Expression count_expr(&expr_buffer);
325 count_expr.WriteOpPushObjectAddress();
326 count_expr.WriteOpPlusUconst(length_offset);
327 count_expr.WriteOpDerefSize(4); // Array length is always 32-bit wide.
328 info_.WriteExprLoc(DW_AT_count, count_expr);
329 info_.EndTag(); // DW_TAG_subrange_type.
330 info_.EndTag(); // DW_TAG_array_type.
331 } else if (type->IsInterface()) {
332 // Skip. Variables cannot have an interface as a dynamic type.
333 // We do not expose the interface information to the debugger in any way.
334 } else {
335 std::string descriptor_string;
336 const char* desc = type->GetDescriptor(&descriptor_string);
337 size_t class_offset = StartClassTag(desc);
338 class_declarations.emplace(type, class_offset);
339
340 if (!type->IsVariableSize()) {
341 info_.WriteUdata(DW_AT_byte_size, type->GetObjectSize());
342 }
343
344 WriteLinkageName(type);
345
346 if (type->IsObjectClass()) {
347 // Generate artificial member which is used to get the dynamic type of variable.
348 // The run-time value of this field will correspond to linkage name of some type.
349 // We need to do it only once in j.l.Object since all other types inherit it.
350 info_.StartTag(DW_TAG_member);
351 WriteName(".dynamic_type");
352 WriteLazyType(sizeof(uintptr_t) == 8 ? "J" : "I");
353 info_.WriteFlagPresent(DW_AT_artificial);
354 // Create DWARF expression to get the value of the methods_ field.
355 Expression expr(&expr_buffer);
356 // The address of the object has been implicitly pushed on the stack.
357 // Dereference the klass_ field of Object (32-bit; possibly poisoned).
358 DCHECK_EQ(type->ClassOffset().Uint32Value(), 0u);
359 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Class>), 4u);
360 expr.WriteOpDerefSize(4);
361 if (kPoisonHeapReferences) {
362 expr.WriteOpNeg();
363 // DWARF stack is pointer sized. Ensure that the high bits are clear.
364 expr.WriteOpConstu(0xFFFFFFFF);
365 expr.WriteOpAnd();
366 }
367 // Add offset to the methods_ field.
368 expr.WriteOpPlusUconst(mirror::Class::MethodsOffset().Uint32Value());
369 // Top of stack holds the location of the field now.
370 info_.WriteExprLoc(DW_AT_data_member_location, expr);
371 info_.EndTag(); // DW_TAG_member.
372 }
373
374 // Base class.
Andreas Gampe98104992018-10-16 12:49:47 -0700375 ObjPtr<mirror::Class> base_class = type->GetSuperClass();
David Srbeckyc5bfa972016-02-05 15:49:10 +0000376 if (base_class != nullptr) {
377 info_.StartTag(DW_TAG_inheritance);
Andreas Gampe98104992018-10-16 12:49:47 -0700378 base_class_references.emplace(info_.size(), base_class.Ptr());
David Srbeckyc5bfa972016-02-05 15:49:10 +0000379 info_.WriteRef4(DW_AT_type, 0);
380 info_.WriteUdata(DW_AT_data_member_location, 0);
381 info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_public);
382 info_.EndTag(); // DW_TAG_inheritance.
383 }
384
385 // Member variables.
386 for (uint32_t i = 0, count = type->NumInstanceFields(); i < count; ++i) {
387 ArtField* field = type->GetInstanceField(i);
388 info_.StartTag(DW_TAG_member);
389 WriteName(field->GetName());
390 WriteLazyType(field->GetTypeDescriptor());
391 info_.WriteUdata(DW_AT_data_member_location, field->GetOffset().Uint32Value());
392 uint32_t access_flags = field->GetAccessFlags();
393 if (access_flags & kAccPublic) {
394 info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_public);
395 } else if (access_flags & kAccProtected) {
396 info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_protected);
397 } else if (access_flags & kAccPrivate) {
398 info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_private);
399 }
400 info_.EndTag(); // DW_TAG_member.
401 }
402
403 if (type->IsStringClass()) {
404 // Emit debug info about an artifical class member for java.lang.String which represents
405 // the first element of the data stored in a string instance. Consumers of the debug
406 // info will be able to read the content of java.lang.String based on the count (real
407 // field) and based on the location of this data member.
408 info_.StartTag(DW_TAG_member);
409 WriteName("value");
410 // We don't support fields with C like array types so we just say its type is java char.
411 WriteLazyType("C"); // char.
412 info_.WriteUdata(DW_AT_data_member_location,
413 mirror::String::ValueOffset().Uint32Value());
414 info_.WriteSdata(DW_AT_accessibility, DW_ACCESS_private);
415 info_.EndTag(); // DW_TAG_member.
416 }
417
418 EndClassTag();
419 }
420 }
421
422 // Write base class declarations.
423 for (const auto& base_class_reference : base_class_references) {
424 size_t reference_offset = base_class_reference.first;
425 mirror::Class* base_class = base_class_reference.second;
Vladimir Marko3bada4b2017-05-19 12:32:47 +0100426 const auto it = class_declarations.find(base_class);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000427 if (it != class_declarations.end()) {
428 info_.UpdateUint32(reference_offset, it->second);
429 } else {
430 // Declare base class. We can not use the standard WriteLazyType
431 // since we want to avoid the DW_TAG_reference_tag wrapping.
432 std::string tmp_storage;
433 const char* base_class_desc = base_class->GetDescriptor(&tmp_storage);
434 size_t base_class_declaration_offset = StartClassTag(base_class_desc);
435 info_.WriteFlagPresent(DW_AT_declaration);
436 WriteLinkageName(base_class);
437 EndClassTag();
438 class_declarations.emplace(base_class, base_class_declaration_offset);
439 info_.UpdateUint32(reference_offset, base_class_declaration_offset);
440 }
441 }
442
443 FinishLazyTypes();
444 CloseNamespacesAboveDepth(0);
445 info_.EndTag(); // DW_TAG_compile_unit.
446 CHECK_EQ(info_.Depth(), 0);
447 std::vector<uint8_t> buffer;
448 buffer.reserve(info_.data()->size() + KB);
David Srbeckye155f4b2017-12-06 15:18:38 +0000449 const size_t offset = owner_->builder_->GetDebugInfo()->GetPosition();
David Srbeckyc5bfa972016-02-05 15:49:10 +0000450 // All compilation units share single table which is at the start of .debug_abbrev.
451 const size_t debug_abbrev_offset = 0;
452 WriteDebugInfoCU(debug_abbrev_offset, info_, offset, &buffer, &owner_->debug_info_patches_);
453 owner_->builder_->GetDebugInfo()->WriteFully(buffer.data(), buffer.size());
454 }
455
456 // Write table into .debug_loc which describes location of dex register.
457 // The dex register might be valid only at some points and it might
458 // move between machine registers and stack.
459 void WriteRegLocation(const MethodDebugInfo* method_info,
David Srbecky2ed15b62016-03-04 11:34:46 +0000460 const std::vector<DexRegisterMap>& dex_register_maps,
David Srbeckyc5bfa972016-02-05 15:49:10 +0000461 uint16_t vreg,
462 bool is64bitValue,
David Srbecky197160d2016-03-07 17:33:57 +0000463 uint64_t compilation_unit_code_address,
David Srbeckyc5bfa972016-02-05 15:49:10 +0000464 uint32_t dex_pc_low = 0,
465 uint32_t dex_pc_high = 0xFFFFFFFF) {
466 WriteDebugLocEntry(method_info,
David Srbecky2ed15b62016-03-04 11:34:46 +0000467 dex_register_maps,
David Srbeckyc5bfa972016-02-05 15:49:10 +0000468 vreg,
469 is64bitValue,
David Srbecky197160d2016-03-07 17:33:57 +0000470 compilation_unit_code_address,
David Srbeckyc5bfa972016-02-05 15:49:10 +0000471 dex_pc_low,
472 dex_pc_high,
473 owner_->builder_->GetIsa(),
474 &info_,
475 &owner_->debug_loc_,
476 &owner_->debug_ranges_);
477 }
478
479 // Linkage name uniquely identifies type.
480 // It is used to determine the dynamic type of objects.
481 // 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 -0700482 void WriteLinkageName(mirror::Class* type) REQUIRES_SHARED(Locks::mutator_lock_) {
David Srbeckyc5bfa972016-02-05 15:49:10 +0000483 auto* methods_ptr = type->GetMethodsPtr();
484 if (methods_ptr == nullptr) {
485 // Some types might have no methods. Allocate empty array instead.
486 LinearAlloc* allocator = Runtime::Current()->GetLinearAlloc();
487 void* storage = allocator->Alloc(Thread::Current(), sizeof(LengthPrefixedArray<ArtMethod>));
488 methods_ptr = new (storage) LengthPrefixedArray<ArtMethod>(0);
489 type->SetMethodsPtr(methods_ptr, 0, 0);
490 DCHECK(type->GetMethodsPtr() != nullptr);
491 }
492 char name[32];
493 snprintf(name, sizeof(name), "0x%" PRIXPTR, reinterpret_cast<uintptr_t>(methods_ptr));
494 info_.WriteString(dwarf::DW_AT_linkage_name, name);
495 }
496
497 // Some types are difficult to define as we go since they need
498 // to be enclosed in the right set of namespaces. Therefore we
499 // just define all types lazily at the end of compilation unit.
500 void WriteLazyType(const char* type_descriptor) {
501 if (type_descriptor != nullptr && type_descriptor[0] != 'V') {
502 lazy_types_.emplace(std::string(type_descriptor), info_.size());
503 info_.WriteRef4(dwarf::DW_AT_type, 0);
504 }
505 }
506
507 void FinishLazyTypes() {
508 for (const auto& lazy_type : lazy_types_) {
509 info_.UpdateUint32(lazy_type.second, WriteTypeDeclaration(lazy_type.first));
510 }
511 lazy_types_.clear();
512 }
513
514 private:
515 void WriteName(const char* name) {
516 if (name != nullptr) {
517 info_.WriteString(dwarf::DW_AT_name, name);
518 }
519 }
520
521 // Convert dex type descriptor to DWARF.
522 // Returns offset in the compilation unit.
523 size_t WriteTypeDeclaration(const std::string& desc) {
524 using namespace dwarf; // NOLINT. For easy access to DWARF constants.
525
526 DCHECK(!desc.empty());
Vladimir Marko3bada4b2017-05-19 12:32:47 +0100527 const auto it = type_cache_.find(desc);
David Srbeckyc5bfa972016-02-05 15:49:10 +0000528 if (it != type_cache_.end()) {
529 return it->second;
530 }
531
532 size_t offset;
533 if (desc[0] == 'L') {
534 // Class type. For example: Lpackage/name;
535 size_t class_offset = StartClassTag(desc.c_str());
536 info_.WriteFlagPresent(DW_AT_declaration);
537 EndClassTag();
538 // Reference to the class type.
539 offset = info_.StartTag(DW_TAG_reference_type);
540 info_.WriteRef(DW_AT_type, class_offset);
541 info_.EndTag();
542 } else if (desc[0] == '[') {
543 // Array type.
544 size_t element_type = WriteTypeDeclaration(desc.substr(1));
545 CloseNamespacesAboveDepth(0); // Declare in root namespace.
546 size_t array_type = info_.StartTag(DW_TAG_array_type);
547 info_.WriteFlagPresent(DW_AT_declaration);
548 info_.WriteRef(DW_AT_type, element_type);
549 info_.EndTag();
550 offset = info_.StartTag(DW_TAG_reference_type);
551 info_.WriteRef4(DW_AT_type, array_type);
552 info_.EndTag();
553 } else {
554 // Primitive types.
555 DCHECK_EQ(desc.size(), 1u);
556
557 const char* name;
558 uint32_t encoding;
559 uint32_t byte_size;
560 switch (desc[0]) {
561 case 'B':
562 name = "byte";
563 encoding = DW_ATE_signed;
564 byte_size = 1;
565 break;
566 case 'C':
567 name = "char";
568 encoding = DW_ATE_UTF;
569 byte_size = 2;
570 break;
571 case 'D':
572 name = "double";
573 encoding = DW_ATE_float;
574 byte_size = 8;
575 break;
576 case 'F':
577 name = "float";
578 encoding = DW_ATE_float;
579 byte_size = 4;
580 break;
581 case 'I':
582 name = "int";
583 encoding = DW_ATE_signed;
584 byte_size = 4;
585 break;
586 case 'J':
587 name = "long";
588 encoding = DW_ATE_signed;
589 byte_size = 8;
590 break;
591 case 'S':
592 name = "short";
593 encoding = DW_ATE_signed;
594 byte_size = 2;
595 break;
596 case 'Z':
597 name = "boolean";
598 encoding = DW_ATE_boolean;
599 byte_size = 1;
600 break;
601 case 'V':
602 LOG(FATAL) << "Void type should not be encoded";
603 UNREACHABLE();
604 default:
605 LOG(FATAL) << "Unknown dex type descriptor: \"" << desc << "\"";
606 UNREACHABLE();
607 }
608 CloseNamespacesAboveDepth(0); // Declare in root namespace.
609 offset = info_.StartTag(DW_TAG_base_type);
610 WriteName(name);
611 info_.WriteData1(DW_AT_encoding, encoding);
612 info_.WriteData1(DW_AT_byte_size, byte_size);
613 info_.EndTag();
614 }
615
616 type_cache_.emplace(desc, offset);
617 return offset;
618 }
619
620 // Start DW_TAG_class_type tag nested in DW_TAG_namespace tags.
621 // Returns offset of the class tag in the compilation unit.
622 size_t StartClassTag(const char* desc) {
623 std::string name = SetNamespaceForClass(desc);
624 size_t offset = info_.StartTag(dwarf::DW_TAG_class_type);
625 WriteName(name.c_str());
626 return offset;
627 }
628
629 void EndClassTag() {
630 info_.EndTag();
631 }
632
633 // Set the current namespace nesting to one required by the given class.
634 // Returns the class name with namespaces, 'L', and ';' stripped.
635 std::string SetNamespaceForClass(const char* desc) {
636 DCHECK(desc != nullptr && desc[0] == 'L');
637 desc++; // Skip the initial 'L'.
638 size_t depth = 0;
639 for (const char* end; (end = strchr(desc, '/')) != nullptr; desc = end + 1, ++depth) {
640 // Check whether the name at this depth is already what we need.
641 if (depth < current_namespace_.size()) {
642 const std::string& name = current_namespace_[depth];
643 if (name.compare(0, name.size(), desc, end - desc) == 0) {
644 continue;
645 }
646 }
647 // Otherwise we need to open a new namespace tag at this depth.
648 CloseNamespacesAboveDepth(depth);
649 info_.StartTag(dwarf::DW_TAG_namespace);
650 std::string name(desc, end - desc);
651 WriteName(name.c_str());
652 current_namespace_.push_back(std::move(name));
653 }
654 CloseNamespacesAboveDepth(depth);
655 return std::string(desc, strchr(desc, ';') - desc);
656 }
657
658 // Close namespace tags to reach the given nesting depth.
659 void CloseNamespacesAboveDepth(size_t depth) {
660 DCHECK_LE(depth, current_namespace_.size());
661 while (current_namespace_.size() > depth) {
662 info_.EndTag();
663 current_namespace_.pop_back();
664 }
665 }
666
667 // For access to the ELF sections.
668 ElfDebugInfoWriter<ElfTypes>* owner_;
669 // Temporary buffer to create and store the entries.
670 dwarf::DebugInfoEntryWriter<> info_;
671 // Cache of already translated type descriptors.
672 std::map<std::string, size_t> type_cache_; // type_desc -> definition_offset.
673 // 32-bit references which need to be resolved to a type later.
674 // Given type may be used multiple times. Therefore we need a multimap.
675 std::multimap<std::string, size_t> lazy_types_; // type_desc -> patch_offset.
676 // The current set of open namespace tags which are active and not closed yet.
677 std::vector<std::string> current_namespace_;
678};
679
680} // namespace debug
681} // namespace art
682
683#endif // ART_COMPILER_DEBUG_ELF_DEBUG_INFO_WRITER_H_
684