1c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky/* 2c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky * Copyright (C) 2016 The Android Open Source Project 3c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky * 4c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky * Licensed under the Apache License, Version 2.0 (the "License"); 5c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky * you may not use this file except in compliance with the License. 6c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky * You may obtain a copy of the License at 7c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky * 8c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky * http://www.apache.org/licenses/LICENSE-2.0 9c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky * 10c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky * Unless required by applicable law or agreed to in writing, software 11c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky * distributed under the License is distributed on an "AS IS" BASIS, 12c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky * See the License for the specific language governing permissions and 14c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky * limitations under the License. 15c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky */ 16c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky 17c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky#ifndef ART_COMPILER_DEBUG_ELF_DEBUG_LINE_WRITER_H_ 18c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky#define ART_COMPILER_DEBUG_ELF_DEBUG_LINE_WRITER_H_ 19c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky 206a6b38fbed22688fac7e061450a8a9c64a685fafDavid Srbecky#include <unordered_set> 21c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky#include <vector> 22c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky 23c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky#include "compiled_method.h" 24c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky#include "debug/dwarf/debug_line_opcode_writer.h" 25c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky#include "debug/dwarf/headers.h" 26c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky#include "debug/elf_compilation_unit.h" 27c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky#include "dex_file-inl.h" 28c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky#include "elf_builder.h" 29c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky#include "stack_map.h" 30c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky 31c5bfa97c47d656b76f297af8abcd5f7502987399David Srbeckynamespace art { 32c5bfa97c47d656b76f297af8abcd5f7502987399David Srbeckynamespace debug { 33c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky 34c5bfa97c47d656b76f297af8abcd5f7502987399David Srbeckytypedef std::vector<DexFile::PositionInfo> PositionInfos; 35c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky 36c5bfa97c47d656b76f297af8abcd5f7502987399David Srbeckystatic bool PositionInfoCallback(void* ctx, const DexFile::PositionInfo& entry) { 37c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky static_cast<PositionInfos*>(ctx)->push_back(entry); 38c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky return false; 39c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky} 40c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky 41c5bfa97c47d656b76f297af8abcd5f7502987399David Srbeckytemplate<typename ElfTypes> 42c5bfa97c47d656b76f297af8abcd5f7502987399David Srbeckyclass ElfDebugLineWriter { 43c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky using Elf_Addr = typename ElfTypes::Addr; 44c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky 45c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky public: 46c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky explicit ElfDebugLineWriter(ElfBuilder<ElfTypes>* builder) : builder_(builder) { 47c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky } 48c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky 49c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky void Start() { 50c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky builder_->GetDebugLine()->Start(); 51c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky } 52c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky 53c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky // Write line table for given set of methods. 54c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky // Returns the number of bytes written. 55c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky size_t WriteCompilationUnit(ElfCompilationUnit& compilation_unit) { 56c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky const bool is64bit = Is64BitInstructionSet(builder_->GetIsa()); 57197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky const Elf_Addr base_address = compilation_unit.is_code_address_text_relative 58c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky ? builder_->GetText()->GetAddress() 59c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky : 0; 60c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky 61c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky compilation_unit.debug_line_offset = builder_->GetDebugLine()->GetSize(); 62c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky 63c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky std::vector<dwarf::FileEntry> files; 64c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky std::unordered_map<std::string, size_t> files_map; 65c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky std::vector<std::string> directories; 66c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky std::unordered_map<std::string, size_t> directories_map; 67c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky int code_factor_bits_ = 0; 68c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky int dwarf_isa = -1; 69c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky switch (builder_->GetIsa()) { 70c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky case kArm: // arm actually means thumb2. 71c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky case kThumb2: 72c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky code_factor_bits_ = 1; // 16-bit instuctions 73c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky dwarf_isa = 1; // DW_ISA_ARM_thumb. 74c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky break; 75c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky case kArm64: 76c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky case kMips: 77c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky case kMips64: 78c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky code_factor_bits_ = 2; // 32-bit instructions 79c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky break; 80c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky case kNone: 81c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky case kX86: 82c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky case kX86_64: 83c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky break; 84c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky } 856a6b38fbed22688fac7e061450a8a9c64a685fafDavid Srbecky std::unordered_set<uint64_t> seen_addresses(compilation_unit.methods.size()); 86c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky dwarf::DebugLineOpCodeWriter<> opcodes(is64bit, code_factor_bits_); 87c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky for (const MethodDebugInfo* mi : compilation_unit.methods) { 88c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky // Ignore function if we have already generated line table for the same address. 89c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky // It would confuse the debugger and the DWARF specification forbids it. 906a6b38fbed22688fac7e061450a8a9c64a685fafDavid Srbecky // We allow the line table for method to be replicated in different compilation unit. 916a6b38fbed22688fac7e061450a8a9c64a685fafDavid Srbecky // This ensures that each compilation unit contains line table for all its methods. 926a6b38fbed22688fac7e061450a8a9c64a685fafDavid Srbecky if (!seen_addresses.insert(mi->code_address).second) { 93c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky continue; 94c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky } 95c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky 9699b87ebbe287ddadf530b217b11a38226bca4367David Srbecky uint32_t prologue_end = std::numeric_limits<uint32_t>::max(); 97197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky std::vector<SrcMapElem> pc2dex_map; 98197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky if (mi->code_info != nullptr) { 99c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky // Use stack maps to create mapping table from pc to dex. 100197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky const CodeInfo code_info(mi->code_info); 10109ed09866da6d8c7448ef297c148bfa577a247c2David Srbecky const CodeInfoEncoding encoding = code_info.ExtractEncoding(); 10209ed09866da6d8c7448ef297c148bfa577a247c2David Srbecky pc2dex_map.reserve(code_info.GetNumberOfStackMaps(encoding)); 10309ed09866da6d8c7448ef297c148bfa577a247c2David Srbecky for (uint32_t s = 0; s < code_info.GetNumberOfStackMaps(encoding); s++) { 104c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky StackMap stack_map = code_info.GetStackMapAt(s, encoding); 105c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky DCHECK(stack_map.IsValid()); 10609ed09866da6d8c7448ef297c148bfa577a247c2David Srbecky const uint32_t pc = stack_map.GetNativePcOffset(encoding.stack_map_encoding); 10709ed09866da6d8c7448ef297c148bfa577a247c2David Srbecky const int32_t dex = stack_map.GetDexPc(encoding.stack_map_encoding); 108197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky pc2dex_map.push_back({pc, dex}); 10909ed09866da6d8c7448ef297c148bfa577a247c2David Srbecky if (stack_map.HasDexRegisterMap(encoding.stack_map_encoding)) { 11099b87ebbe287ddadf530b217b11a38226bca4367David Srbecky // Guess that the first map with local variables is the end of prologue. 11199b87ebbe287ddadf530b217b11a38226bca4367David Srbecky prologue_end = std::min(prologue_end, pc); 112c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky } 113c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky } 114197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky std::sort(pc2dex_map.begin(), pc2dex_map.end()); 115c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky } 116c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky 11799b87ebbe287ddadf530b217b11a38226bca4367David Srbecky if (pc2dex_map.empty()) { 118c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky continue; 119c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky } 120c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky 121252fa90d25eb3fa9c4189304d021533c9657fea7David Srbecky // Compensate for compiler's off-by-one-instruction error. 122252fa90d25eb3fa9c4189304d021533c9657fea7David Srbecky // 123252fa90d25eb3fa9c4189304d021533c9657fea7David Srbecky // The compiler generates stackmap with PC *after* the branch instruction 124252fa90d25eb3fa9c4189304d021533c9657fea7David Srbecky // (because this is the PC which is easier to obtain when unwinding). 125252fa90d25eb3fa9c4189304d021533c9657fea7David Srbecky // 126252fa90d25eb3fa9c4189304d021533c9657fea7David Srbecky // However, the debugger is more clever and it will ask us for line-number 127252fa90d25eb3fa9c4189304d021533c9657fea7David Srbecky // mapping at the location of the branch instruction (since the following 128252fa90d25eb3fa9c4189304d021533c9657fea7David Srbecky // instruction could belong to other line, this is the correct thing to do). 129252fa90d25eb3fa9c4189304d021533c9657fea7David Srbecky // 130252fa90d25eb3fa9c4189304d021533c9657fea7David Srbecky // So we really want to just decrement the PC by one instruction so that the 131252fa90d25eb3fa9c4189304d021533c9657fea7David Srbecky // branch instruction is covered as well. However, we do not know the size 132252fa90d25eb3fa9c4189304d021533c9657fea7David Srbecky // of the previous instruction, and we can not subtract just a fixed amount 133252fa90d25eb3fa9c4189304d021533c9657fea7David Srbecky // (the debugger would trust us that the PC is valid; it might try to set 134252fa90d25eb3fa9c4189304d021533c9657fea7David Srbecky // breakpoint there at some point, and setting breakpoint in mid-instruction 135252fa90d25eb3fa9c4189304d021533c9657fea7David Srbecky // would make the process crash in spectacular way). 136252fa90d25eb3fa9c4189304d021533c9657fea7David Srbecky // 137252fa90d25eb3fa9c4189304d021533c9657fea7David Srbecky // Therefore, we say that the PC which the compiler gave us for the stackmap 138252fa90d25eb3fa9c4189304d021533c9657fea7David Srbecky // is the end of its associated address range, and we use the PC from the 139252fa90d25eb3fa9c4189304d021533c9657fea7David Srbecky // previous stack map as the start of the range. This ensures that the PC is 140252fa90d25eb3fa9c4189304d021533c9657fea7David Srbecky // valid and that the branch instruction is covered. 141252fa90d25eb3fa9c4189304d021533c9657fea7David Srbecky // 142252fa90d25eb3fa9c4189304d021533c9657fea7David Srbecky // This ensures we have correct line number mapping at call sites (which is 143252fa90d25eb3fa9c4189304d021533c9657fea7David Srbecky // important for backtraces), but there is nothing we can do for non-call 144252fa90d25eb3fa9c4189304d021533c9657fea7David Srbecky // sites (so stepping through optimized code in debugger is not possible). 145252fa90d25eb3fa9c4189304d021533c9657fea7David Srbecky // 146252fa90d25eb3fa9c4189304d021533c9657fea7David Srbecky // We do not adjust the stackmaps if the code was compiled as debuggable. 147252fa90d25eb3fa9c4189304d021533c9657fea7David Srbecky // In that case, the stackmaps should accurately cover all instructions. 148252fa90d25eb3fa9c4189304d021533c9657fea7David Srbecky if (!mi->is_native_debuggable) { 149252fa90d25eb3fa9c4189304d021533c9657fea7David Srbecky for (size_t i = pc2dex_map.size() - 1; i > 0; --i) { 150252fa90d25eb3fa9c4189304d021533c9657fea7David Srbecky pc2dex_map[i].from_ = pc2dex_map[i - 1].from_; 151252fa90d25eb3fa9c4189304d021533c9657fea7David Srbecky } 152252fa90d25eb3fa9c4189304d021533c9657fea7David Srbecky pc2dex_map[0].from_ = 0; 153252fa90d25eb3fa9c4189304d021533c9657fea7David Srbecky } 154252fa90d25eb3fa9c4189304d021533c9657fea7David Srbecky 155197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky Elf_Addr method_address = base_address + mi->code_address; 156c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky 15799b87ebbe287ddadf530b217b11a38226bca4367David Srbecky PositionInfos dex2line_map; 15809c2a6be63337ee060e2d54bd01cf18be7301d29David Srbecky DCHECK(mi->dex_file != nullptr); 159c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky const DexFile* dex = mi->dex_file; 16099b87ebbe287ddadf530b217b11a38226bca4367David Srbecky if (!dex->DecodeDebugPositionInfo(mi->code_item, PositionInfoCallback, &dex2line_map)) { 161c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky continue; 162c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky } 163c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky 16499b87ebbe287ddadf530b217b11a38226bca4367David Srbecky if (dex2line_map.empty()) { 165c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky continue; 166c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky } 167c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky 168c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky opcodes.SetAddress(method_address); 169c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky if (dwarf_isa != -1) { 170c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky opcodes.SetISA(dwarf_isa); 171c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky } 172c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky 173c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky // Get and deduplicate directory and filename. 174c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky int file_index = 0; // 0 - primary source file of the compilation. 175c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky auto& dex_class_def = dex->GetClassDef(mi->class_def_index); 176c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky const char* source_file = dex->GetSourceFile(dex_class_def); 177c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky if (source_file != nullptr) { 178c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky std::string file_name(source_file); 179c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky size_t file_name_slash = file_name.find_last_of('/'); 180c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky std::string class_name(dex->GetClassDescriptor(dex_class_def)); 181c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky size_t class_name_slash = class_name.find_last_of('/'); 182c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky std::string full_path(file_name); 183c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky 184c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky // Guess directory from package name. 185c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky int directory_index = 0; // 0 - current directory of the compilation. 186c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky if (file_name_slash == std::string::npos && // Just filename. 187c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky class_name.front() == 'L' && // Type descriptor for a class. 188c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky class_name_slash != std::string::npos) { // Has package name. 189c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky std::string package_name = class_name.substr(1, class_name_slash - 1); 190c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky auto it = directories_map.find(package_name); 191c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky if (it == directories_map.end()) { 192c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky directory_index = 1 + directories.size(); 193c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky directories_map.emplace(package_name, directory_index); 194c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky directories.push_back(package_name); 195c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky } else { 196c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky directory_index = it->second; 197c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky } 198c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky full_path = package_name + "/" + file_name; 199c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky } 200c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky 201c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky // Add file entry. 202c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky auto it2 = files_map.find(full_path); 203c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky if (it2 == files_map.end()) { 204c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky file_index = 1 + files.size(); 205c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky files_map.emplace(full_path, file_index); 206c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky files.push_back(dwarf::FileEntry { 207c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky file_name, 208c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky directory_index, 209c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky 0, // Modification time - NA. 210c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky 0, // File size - NA. 211c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky }); 212c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky } else { 213c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky file_index = it2->second; 214c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky } 215c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky } 216c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky opcodes.SetFile(file_index); 217c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky 218c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky // Generate mapping opcodes from PC to Java lines. 219c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky if (file_index != 0) { 22091cc06c1814bd1d0fd6635bc3d7632a2bb7b0e7cDavid Srbecky // If the method was not compiled as native-debuggable, we still generate all available 22191cc06c1814bd1d0fd6635bc3d7632a2bb7b0e7cDavid Srbecky // lines, but we try to prevent the debugger from stepping and setting breakpoints since 22291cc06c1814bd1d0fd6635bc3d7632a2bb7b0e7cDavid Srbecky // the information is too inaccurate for that (breakpoints would be set after the calls). 22391cc06c1814bd1d0fd6635bc3d7632a2bb7b0e7cDavid Srbecky const bool default_is_stmt = mi->is_native_debuggable; 224c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky bool first = true; 22599b87ebbe287ddadf530b217b11a38226bca4367David Srbecky for (SrcMapElem pc2dex : pc2dex_map) { 226c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky uint32_t pc = pc2dex.from_; 227c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky int dex_pc = pc2dex.to_; 228c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky // Find mapping with address with is greater than our dex pc; then go back one step. 22999b87ebbe287ddadf530b217b11a38226bca4367David Srbecky auto dex2line = std::upper_bound( 23099b87ebbe287ddadf530b217b11a38226bca4367David Srbecky dex2line_map.begin(), 23199b87ebbe287ddadf530b217b11a38226bca4367David Srbecky dex2line_map.end(), 23299b87ebbe287ddadf530b217b11a38226bca4367David Srbecky dex_pc, 233c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky [](uint32_t address, const DexFile::PositionInfo& entry) { 234c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky return address < entry.address_; 235c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky }); 23699b87ebbe287ddadf530b217b11a38226bca4367David Srbecky // Look for first valid mapping after the prologue. 23799b87ebbe287ddadf530b217b11a38226bca4367David Srbecky if (dex2line != dex2line_map.begin() && pc >= prologue_end) { 23899b87ebbe287ddadf530b217b11a38226bca4367David Srbecky int line = (--dex2line)->line_; 239c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky if (first) { 240c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky first = false; 241c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky if (pc > 0) { 242c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky // Assume that any preceding code is prologue. 24399b87ebbe287ddadf530b217b11a38226bca4367David Srbecky int first_line = dex2line_map.front().line_; 244c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky // Prologue is not a sensible place for a breakpoint. 24591cc06c1814bd1d0fd6635bc3d7632a2bb7b0e7cDavid Srbecky opcodes.SetIsStmt(false); 246c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky opcodes.AddRow(method_address, first_line); 247c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky opcodes.SetPrologueEnd(); 248c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky } 24991cc06c1814bd1d0fd6635bc3d7632a2bb7b0e7cDavid Srbecky opcodes.SetIsStmt(default_is_stmt); 250c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky opcodes.AddRow(method_address + pc, line); 251c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky } else if (line != opcodes.CurrentLine()) { 25291cc06c1814bd1d0fd6635bc3d7632a2bb7b0e7cDavid Srbecky opcodes.SetIsStmt(default_is_stmt); 253c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky opcodes.AddRow(method_address + pc, line); 254c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky } 255c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky } 256c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky } 257c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky } else { 258c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky // line 0 - instruction cannot be attributed to any source line. 259c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky opcodes.AddRow(method_address, 0); 260c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky } 261c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky 262197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky opcodes.AdvancePC(method_address + mi->code_size); 263c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky opcodes.EndSequence(); 264c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky } 265c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky std::vector<uint8_t> buffer; 266c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky buffer.reserve(opcodes.data()->size() + KB); 267c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky size_t offset = builder_->GetDebugLine()->GetSize(); 268c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky WriteDebugLineTable(directories, files, opcodes, offset, &buffer, &debug_line_patches_); 269c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky builder_->GetDebugLine()->WriteFully(buffer.data(), buffer.size()); 270c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky return buffer.size(); 271c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky } 272c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky 273c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky void End(bool write_oat_patches) { 274c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky builder_->GetDebugLine()->End(); 275c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky if (write_oat_patches) { 276c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky builder_->WritePatches(".debug_line.oat_patches", 277c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky ArrayRef<const uintptr_t>(debug_line_patches_)); 278c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky } 279c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky } 280c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky 281c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky private: 282c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky ElfBuilder<ElfTypes>* builder_; 283c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky std::vector<uintptr_t> debug_line_patches_; 284c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky}; 285c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky 286c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky} // namespace debug 287c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky} // namespace art 288c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky 289c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky#endif // ART_COMPILER_DEBUG_ELF_DEBUG_LINE_WRITER_H_ 290c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky 291