Refactor jit debugger interface and its ELF creation.

Make it possible to store more then one method per entry,
and ref-count the number of live methods per entry.

Test: m test-art-host-gtest
Change-Id: I45d69185e85e47fbee88a8d1f549ede9875a3c0a
diff --git a/compiler/debug/elf_debug_writer.cc b/compiler/debug/elf_debug_writer.cc
index bb2a214..df5bb37 100644
--- a/compiler/debug/elf_debug_writer.cc
+++ b/compiler/debug/elf_debug_writer.cc
@@ -137,10 +137,17 @@
     InstructionSet isa,
     const InstructionSetFeatures* features,
     bool mini_debug_info,
-    const MethodDebugInfo& mi) {
-  CHECK_EQ(mi.is_code_address_text_relative, false);
+    ArrayRef<const MethodDebugInfo> method_infos) {
+  CHECK_GT(method_infos.size(), 0u);
+  uint64_t min_address = std::numeric_limits<uint64_t>::max();
+  uint64_t max_address = 0;
+  for (const MethodDebugInfo& mi : method_infos) {
+    CHECK_EQ(mi.is_code_address_text_relative, false);
+    min_address = std::min(min_address, mi.code_address);
+    max_address = std::max(max_address, mi.code_address + mi.code_size);
+  }
   DebugInfo debug_info{};
-  debug_info.compiled_methods = ArrayRef<const debug::MethodDebugInfo>(&mi, 1);
+  debug_info.compiled_methods = method_infos;
   std::vector<uint8_t> buffer;
   buffer.reserve(KB);
   linker::VectorOutputStream out("Debug ELF file", &buffer);
@@ -151,14 +158,14 @@
   if (mini_debug_info) {
     std::vector<uint8_t> mdi = MakeMiniDebugInfo(isa,
                                                  features,
-                                                 mi.code_address,
-                                                 mi.code_size,
+                                                 min_address,
+                                                 max_address - min_address,
                                                  /* dex_section_address */ 0,
                                                  /* dex_section_size */ 0,
                                                  debug_info);
     builder->WriteSection(".gnu_debugdata", &mdi);
   } else {
-    builder->GetText()->AllocateVirtualMemory(mi.code_address, mi.code_size);
+    builder->GetText()->AllocateVirtualMemory(min_address, max_address - min_address);
     WriteDebugInfo(builder.get(),
                    debug_info,
                    dwarf::DW_DEBUG_FRAME_FORMAT,
@@ -173,11 +180,11 @@
     InstructionSet isa,
     const InstructionSetFeatures* features,
     bool mini_debug_info,
-    const MethodDebugInfo& method_info) {
+    ArrayRef<const MethodDebugInfo> method_infos) {
   if (Is64BitInstructionSet(isa)) {
-    return MakeElfFileForJITInternal<ElfTypes64>(isa, features, mini_debug_info, method_info);
+    return MakeElfFileForJITInternal<ElfTypes64>(isa, features, mini_debug_info, method_infos);
   } else {
-    return MakeElfFileForJITInternal<ElfTypes32>(isa, features, mini_debug_info, method_info);
+    return MakeElfFileForJITInternal<ElfTypes32>(isa, features, mini_debug_info, method_infos);
   }
 }
 
diff --git a/compiler/debug/elf_debug_writer.h b/compiler/debug/elf_debug_writer.h
index 8ad0c42..e442e00 100644
--- a/compiler/debug/elf_debug_writer.h
+++ b/compiler/debug/elf_debug_writer.h
@@ -54,7 +54,7 @@
     InstructionSet isa,
     const InstructionSetFeatures* features,
     bool mini_debug_info,
-    const MethodDebugInfo& method_info);
+    ArrayRef<const MethodDebugInfo> method_infos);
 
 std::vector<uint8_t> WriteDebugElfFileForClasses(
     InstructionSet isa,
diff --git a/compiler/debug/elf_symtab_writer.h b/compiler/debug/elf_symtab_writer.h
index a853714..9c9e8b3 100644
--- a/compiler/debug/elf_symtab_writer.h
+++ b/compiler/debug/elf_symtab_writer.h
@@ -72,8 +72,8 @@
       continue;  // Add symbol only for the first instance.
     }
     size_t name_offset;
-    if (!info.trampoline_name.empty()) {
-      name_offset = strtab->Write(info.trampoline_name);
+    if (!info.custom_name.empty()) {
+      name_offset = strtab->Write(info.custom_name);
     } else {
       DCHECK(info.dex_file != nullptr);
       std::string name = info.dex_file->PrettyMethod(info.dex_method_index, !mini_debug_info);
diff --git a/compiler/debug/method_debug_info.h b/compiler/debug/method_debug_info.h
index 43c8de2..d0b03ec 100644
--- a/compiler/debug/method_debug_info.h
+++ b/compiler/debug/method_debug_info.h
@@ -27,7 +27,7 @@
 namespace debug {
 
 struct MethodDebugInfo {
-  std::string trampoline_name;
+  std::string custom_name;
   const DexFile* dex_file;  // Native methods (trampolines) do not reference dex file.
   size_t class_def_index;
   uint32_t dex_method_index;
diff --git a/compiler/jit/jit_compiler.cc b/compiler/jit/jit_compiler.cc
index 88e3e5b..2c62095 100644
--- a/compiler/jit/jit_compiler.cc
+++ b/compiler/jit/jit_compiler.cc
@@ -76,6 +76,7 @@
     const ArrayRef<mirror::Class*> types_array(types, count);
     std::vector<uint8_t> elf_file = debug::WriteDebugElfFileForClasses(
         kRuntimeISA, jit_compiler->GetCompilerDriver()->GetInstructionSetFeatures(), types_array);
+    MutexLock mu(Thread::Current(), g_jit_debug_mutex);
     CreateJITCodeEntry(std::move(elf_file));
   }
 }
diff --git a/compiler/linker/arm/relative_patcher_arm_base.cc b/compiler/linker/arm/relative_patcher_arm_base.cc
index cedbe5d..6e0286a 100644
--- a/compiler/linker/arm/relative_patcher_arm_base.cc
+++ b/compiler/linker/arm/relative_patcher_arm_base.cc
@@ -250,12 +250,12 @@
     for (size_t i = start, num = data.NumberOfThunks(); i != num; ++i) {
       debug::MethodDebugInfo info = {};
       if (i == 0u) {
-        info.trampoline_name = base_name;
+        info.custom_name = base_name;
       } else {
         // Add a disambiguating tag for subsequent identical thunks. Since the `thunks_`
         // keeps records also for thunks in previous oat files, names based on the thunk
         // index shall be unique across the whole multi-oat output.
-        info.trampoline_name = base_name + "_" + std::to_string(i);
+        info.custom_name = base_name + "_" + std::to_string(i);
       }
       info.isa = instruction_set_;
       info.is_code_address_text_relative = true;
diff --git a/compiler/optimizing/optimizing_compiler.cc b/compiler/optimizing/optimizing_compiler.cc
index a3b1f0c..c35c490 100644
--- a/compiler/optimizing/optimizing_compiler.cc
+++ b/compiler/optimizing/optimizing_compiler.cc
@@ -382,6 +382,8 @@
                             PassObserver* pass_observer,
                             VariableSizedHandleScope* handles) const;
 
+  void GenerateJitDebugInfo(debug::MethodDebugInfo method_debug_info);
+
   std::unique_ptr<OptimizingCompilerStats> compilation_stats_;
 
   std::unique_ptr<std::ostream> visualizer_output_;
@@ -1230,7 +1232,7 @@
       const auto* method_header = reinterpret_cast<const OatQuickMethodHeader*>(code);
       const uintptr_t code_address = reinterpret_cast<uintptr_t>(method_header->GetCode());
       debug::MethodDebugInfo info = {};
-      DCHECK(info.trampoline_name.empty());
+      DCHECK(info.custom_name.empty());
       info.dex_file = dex_file;
       info.class_def_index = class_def_idx;
       info.dex_method_index = method_idx;
@@ -1246,14 +1248,7 @@
       info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
       info.code_info = nullptr;
       info.cfi = jni_compiled_method.GetCfi();
-      // If both flags are passed, generate full debug info.
-      const bool mini_debug_info = !compiler_options.GetGenerateDebugInfo();
-      std::vector<uint8_t> elf_file = debug::MakeElfFileForJIT(
-          GetCompilerDriver()->GetInstructionSet(),
-          GetCompilerDriver()->GetInstructionSetFeatures(),
-          mini_debug_info,
-          info);
-      CreateJITCodeEntryForAddress(code_address, std::move(elf_file));
+      GenerateJitDebugInfo(info);
     }
 
     Runtime::Current()->GetJit()->AddMemoryUsage(method, allocator.BytesUsed());
@@ -1361,7 +1356,7 @@
     const auto* method_header = reinterpret_cast<const OatQuickMethodHeader*>(code);
     const uintptr_t code_address = reinterpret_cast<uintptr_t>(method_header->GetCode());
     debug::MethodDebugInfo info = {};
-    DCHECK(info.trampoline_name.empty());
+    DCHECK(info.custom_name.empty());
     info.dex_file = dex_file;
     info.class_def_index = class_def_idx;
     info.dex_method_index = method_idx;
@@ -1377,14 +1372,7 @@
     info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
     info.code_info = stack_map_size == 0 ? nullptr : stack_map_data;
     info.cfi = ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data());
-    // If both flags are passed, generate full debug info.
-    const bool mini_debug_info = !compiler_options.GetGenerateDebugInfo();
-    std::vector<uint8_t> elf_file = debug::MakeElfFileForJIT(
-        GetCompilerDriver()->GetInstructionSet(),
-        GetCompilerDriver()->GetInstructionSetFeatures(),
-        mini_debug_info,
-        info);
-    CreateJITCodeEntryForAddress(code_address, std::move(elf_file));
+    GenerateJitDebugInfo(info);
   }
 
   Runtime::Current()->GetJit()->AddMemoryUsage(method, allocator.BytesUsed());
@@ -1408,4 +1396,22 @@
   return true;
 }
 
+void OptimizingCompiler::GenerateJitDebugInfo(debug::MethodDebugInfo info) {
+  const CompilerOptions& compiler_options = GetCompilerDriver()->GetCompilerOptions();
+  DCHECK(compiler_options.GenerateAnyDebugInfo());
+
+  // If both flags are passed, generate full debug info.
+  const bool mini_debug_info = !compiler_options.GetGenerateDebugInfo();
+
+  // Create entry for the single method that we just compiled.
+  std::vector<uint8_t> elf_file = debug::MakeElfFileForJIT(
+      GetCompilerDriver()->GetInstructionSet(),
+      GetCompilerDriver()->GetInstructionSetFeatures(),
+      mini_debug_info,
+      ArrayRef<const debug::MethodDebugInfo>(&info, 1));
+  MutexLock mu(Thread::Current(), g_jit_debug_mutex);
+  JITCodeEntry* entry = CreateJITCodeEntry(elf_file);
+  IncrementJITCodeEntryRefcount(entry, info.code_address);
+}
+
 }  // namespace art