LLDB  mainline
SymbolVendorWasm.cpp
Go to the documentation of this file.
1 //===-- SymbolVendorWasm.cpp ----------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "SymbolVendorWasm.h"
10 
11 #include <cstring>
12 
14 #include "lldb/Core/Module.h"
15 #include "lldb/Core/ModuleSpec.h"
17 #include "lldb/Core/Section.h"
18 #include "lldb/Host/Host.h"
20 #include "lldb/Symbol/ObjectFile.h"
21 #include "lldb/Target/Target.h"
23 #include "lldb/Utility/Timer.h"
24 
25 using namespace lldb;
26 using namespace lldb_private;
27 using namespace lldb_private::wasm;
28 
30 
31 // SymbolVendorWasm constructor
32 SymbolVendorWasm::SymbolVendorWasm(const lldb::ModuleSP &module_sp)
33  : SymbolVendor(module_sp) {}
34 
35 void SymbolVendorWasm::Initialize() {
36  PluginManager::RegisterPlugin(GetPluginNameStatic(),
37  GetPluginDescriptionStatic(), CreateInstance);
38 }
39 
40 void SymbolVendorWasm::Terminate() {
41  PluginManager::UnregisterPlugin(CreateInstance);
42 }
43 
44 lldb_private::ConstString SymbolVendorWasm::GetPluginNameStatic() {
45  static ConstString g_name("WASM");
46  return g_name;
47 }
48 
49 const char *SymbolVendorWasm::GetPluginDescriptionStatic() {
50  return "Symbol vendor for WASM that looks for dwo files that match "
51  "executables.";
52 }
53 
54 // CreateInstance
55 //
56 // Platforms can register a callback to use when creating symbol vendors to
57 // allow for complex debug information file setups, and to also allow for
58 // finding separate debug information files.
60 SymbolVendorWasm::CreateInstance(const lldb::ModuleSP &module_sp,
61  lldb_private::Stream *feedback_strm) {
62  if (!module_sp)
63  return nullptr;
64 
65  ObjectFileWasm *obj_file =
66  llvm::dyn_cast_or_null<ObjectFileWasm>(module_sp->GetObjectFile());
67  if (!obj_file)
68  return nullptr;
69 
70  // If the main object file already contains debug info, then we are done.
71  if (obj_file->GetSectionList()->FindSectionByType(
73  return nullptr;
74 
75  LLDB_SCOPED_TIMERF("SymbolVendorWasm::CreateInstance (module = %s)",
76  module_sp->GetFileSpec().GetPath().c_str());
77 
78  ModuleSpec module_spec;
79  module_spec.GetFileSpec() = obj_file->GetFileSpec();
80  FileSystem::Instance().Resolve(module_spec.GetFileSpec());
81  module_spec.GetUUID() = obj_file->GetUUID();
82 
83  // A Wasm module may have a custom section named "external_debug_info" whose
84  // content is the absolute or relative path of the Wasm module that contains
85  // debug symbols for this module.
86  llvm::Optional<FileSpec> symbol_file_spec =
87  obj_file->GetExternalDebugInfoFileSpec();
88  if (!symbol_file_spec)
89  return nullptr;
90  module_spec.GetSymbolFileSpec() = *symbol_file_spec;
91 
92  FileSpecList search_paths = Target::GetDefaultDebugFileSearchPaths();
93  FileSpec sym_fspec =
94  Symbols::LocateExecutableSymbolFile(module_spec, search_paths);
95  if (!sym_fspec)
96  return nullptr;
97 
98  DataBufferSP sym_file_data_sp;
99  lldb::offset_t sym_file_data_offset = 0;
100  ObjectFileSP sym_objfile_sp = ObjectFile::FindPlugin(
101  module_sp, &sym_fspec, 0, FileSystem::Instance().GetByteSize(sym_fspec),
102  sym_file_data_sp, sym_file_data_offset);
103  if (!sym_objfile_sp)
104  return nullptr;
105 
106  // This objfile is for debugging purposes.
107  sym_objfile_sp->SetType(ObjectFile::eTypeDebugInfo);
108 
109  SymbolVendorWasm *symbol_vendor = new SymbolVendorWasm(module_sp);
110 
111  // Get the module unified section list and add our debug sections to
112  // that.
113  SectionList *module_section_list = module_sp->GetSectionList();
114  SectionList *objfile_section_list = sym_objfile_sp->GetSectionList();
115 
116  static const SectionType g_sections[] = {
127  for (SectionType section_type : g_sections) {
128  if (SectionSP section_sp =
129  objfile_section_list->FindSectionByType(section_type, true)) {
130  if (SectionSP module_section_sp =
131  module_section_list->FindSectionByType(section_type, true))
132  module_section_list->ReplaceSection(module_section_sp->GetID(),
133  section_sp);
134  else
135  module_section_list->AddSection(section_sp);
136  }
137  }
138 
139  symbol_vendor->AddSymbolFileRepresentation(sym_objfile_sp);
140  return symbol_vendor;
141 }
lldb_private::SectionList::ReplaceSection
bool ReplaceSection(lldb::user_id_t sect_id, const lldb::SectionSP &section_sp, uint32_t depth=UINT32_MAX)
Definition: Section.cpp:447
lldb::eSectionTypeDWARFDebugLoc
@ eSectionTypeDWARFDebugLoc
Definition: lldb-enumerations.h:670
LLDB_SCOPED_TIMERF
#define LLDB_SCOPED_TIMERF(...)
Definition: Timer.h:86
lldb_private::ObjectFile::GetFileSpec
virtual FileSpec & GetFileSpec()
Get accessor to the object file specification.
Definition: ObjectFile.h:290
lldb_private::SymbolVendor::AddSymbolFileRepresentation
void AddSymbolFileRepresentation(const lldb::ObjectFileSP &objfile_sp)
Definition: SymbolVendor.cpp:64
ModuleSpec.h
lldb_private::wasm::ObjectFileWasm::GetUUID
UUID GetUUID() override
Gets the UUID for this object file.
Definition: ObjectFileWasm.h:91
Host.h
ObjectFileWasm.h
lldb::eSectionTypeDWARFDebugAranges
@ eSectionTypeDWARFDebugAranges
Definition: lldb-enumerations.h:665
lldb_private::wasm
Definition: DynamicLoaderWasmDYLD.h:15
lldb::eSectionTypeDWARFDebugRanges
@ eSectionTypeDWARFDebugRanges
Definition: lldb-enumerations.h:675
lldb::eSectionTypeDWARFDebugMacro
@ eSectionTypeDWARFDebugMacro
Definition: lldb-enumerations.h:672
lldb::eSectionTypeDWARFDebugFrame
@ eSectionTypeDWARFDebugFrame
Definition: lldb-enumerations.h:667
Module.h
lldb::eSectionTypeDWARFDebugLine
@ eSectionTypeDWARFDebugLine
Definition: lldb-enumerations.h:669
lldb::eSectionTypeDWARFDebugAddr
@ eSectionTypeDWARFDebugAddr
Definition: lldb-enumerations.h:664
LocateSymbolFile.h
lldb_private::SectionList
Definition: Section.h:34
lldb::offset_t
uint64_t offset_t
Definition: lldb-types.h:87
lldb_private::Stream
Definition: Stream.h:28
lldb::eSectionTypeDWARFDebugStrOffsets
@ eSectionTypeDWARFDebugStrOffsets
Definition: lldb-enumerations.h:677
lldb_private::wasm::ObjectFileWasm
Generic Wasm object file reader.
Definition: ObjectFileWasm.h:22
Section.h
lldb_private::SymbolVendor
Definition: SymbolVendor.h:31
lldb_private::ModuleSpec::GetSymbolFileSpec
FileSpec & GetSymbolFileSpec()
Definition: ModuleSpec.h:82
Target.h
lldb_private::ModuleSpec::GetUUID
UUID & GetUUID()
Definition: ModuleSpec.h:104
lldb_private::SectionList::AddSection
size_t AddSection(const lldb::SectionSP &section_sp)
Definition: Section.cpp:407
lldb_private::FileSpec
Definition: FileSpec.h:56
lldb_private::ObjectFile::GetSectionList
virtual SectionList * GetSectionList(bool update_module_section_list=true)
Gets the section list for the currently selected architecture (and object for archives).
Definition: ObjectFile.cpp:583
lldb::eSectionTypeDWARFDebugAbbrev
@ eSectionTypeDWARFDebugAbbrev
Definition: lldb-enumerations.h:663
lldb::eSectionTypeDWARFDebugStr
@ eSectionTypeDWARFDebugStr
Definition: lldb-enumerations.h:676
StreamString.h
lldb_private::ConstString
Definition: ConstString.h:40
lldb::eSectionTypeDWARFDebugInfo
@ eSectionTypeDWARFDebugInfo
Definition: lldb-enumerations.h:668
Timer.h
lldb::eSectionTypeDWARFDebugTypes
@ eSectionTypeDWARFDebugTypes
DWARF .debug_types section.
Definition: lldb-enumerations.h:695
lldb::eSectionTypeDWARFDebugCuIndex
@ eSectionTypeDWARFDebugCuIndex
Definition: lldb-enumerations.h:666
ObjectFile.h
lldb_private::wasm::SymbolVendorWasm
Definition: SymbolVendorWasm.h:18
lldb_private::SectionList::FindSectionByType
lldb::SectionSP FindSectionByType(lldb::SectionType sect_type, bool check_children, size_t start_idx=0) const
Definition: Section.cpp:524
lldb_private::ModuleSpec::GetFileSpec
FileSpec & GetFileSpec()
Definition: ModuleSpec.h:58
lldb_private::ModuleSpec
Definition: ModuleSpec.h:26
lldb::SectionType
SectionType
Definition: lldb-enumerations.h:646
lldb::eSectionTypeDWARFDebugPubTypes
@ eSectionTypeDWARFDebugPubTypes
Definition: lldb-enumerations.h:674
lldb::eSectionTypeDWARFDebugPubNames
@ eSectionTypeDWARFDebugPubNames
Definition: lldb-enumerations.h:673
lldb::eSectionTypeDWARFDebugLineStr
@ eSectionTypeDWARFDebugLineStr
DWARF v5 .debug_line_str.
Definition: lldb-enumerations.h:698
PluginManager.h
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
SymbolVendorWasm.h
LLDB_PLUGIN_DEFINE
#define LLDB_PLUGIN_DEFINE(PluginName)
Definition: PluginManager.h:31
lldb::eSectionTypeDWARFDebugMacInfo
@ eSectionTypeDWARFDebugMacInfo
Definition: lldb-enumerations.h:671
lldb
Definition: SBAddress.h:15
lldb_private::wasm::ObjectFileWasm::GetExternalDebugInfoFileSpec
llvm::Optional< FileSpec > GetExternalDebugInfoFileSpec()
A Wasm module that has external DWARF debug information should contain a custom section named "extern...
Definition: ObjectFileWasm.cpp:414
lldb::eSectionTypeDWARFDebugLocLists
@ eSectionTypeDWARFDebugLocLists
DWARF v5 .debug_loclists.
Definition: lldb-enumerations.h:700
lldb::eSectionTypeDWARFDebugRngLists
@ eSectionTypeDWARFDebugRngLists
DWARF v5 .debug_rnglists.
Definition: lldb-enumerations.h:699