1*fe013be4SDimitry Andric //===-- SymbolFileJSON.cpp ----------------------------------------------===//
2*fe013be4SDimitry Andric //
3*fe013be4SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*fe013be4SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*fe013be4SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*fe013be4SDimitry Andric //
7*fe013be4SDimitry Andric //===----------------------------------------------------------------------===//
8*fe013be4SDimitry Andric 
9*fe013be4SDimitry Andric #include "SymbolFileJSON.h"
10*fe013be4SDimitry Andric 
11*fe013be4SDimitry Andric #include "Plugins/ObjectFile/JSON/ObjectFileJSON.h"
12*fe013be4SDimitry Andric #include "lldb/Core/Module.h"
13*fe013be4SDimitry Andric #include "lldb/Core/PluginManager.h"
14*fe013be4SDimitry Andric #include "lldb/Symbol/CompileUnit.h"
15*fe013be4SDimitry Andric #include "lldb/Symbol/Function.h"
16*fe013be4SDimitry Andric #include "lldb/Symbol/ObjectFile.h"
17*fe013be4SDimitry Andric #include "lldb/Symbol/Symbol.h"
18*fe013be4SDimitry Andric #include "lldb/Symbol/SymbolContext.h"
19*fe013be4SDimitry Andric #include "lldb/Symbol/Symtab.h"
20*fe013be4SDimitry Andric #include "lldb/Symbol/TypeList.h"
21*fe013be4SDimitry Andric #include "lldb/Utility/LLDBLog.h"
22*fe013be4SDimitry Andric #include "lldb/Utility/Log.h"
23*fe013be4SDimitry Andric #include "lldb/Utility/RegularExpression.h"
24*fe013be4SDimitry Andric #include "lldb/Utility/Timer.h"
25*fe013be4SDimitry Andric #include "llvm/Support/MemoryBuffer.h"
26*fe013be4SDimitry Andric 
27*fe013be4SDimitry Andric #include <memory>
28*fe013be4SDimitry Andric #include <optional>
29*fe013be4SDimitry Andric 
30*fe013be4SDimitry Andric using namespace llvm;
31*fe013be4SDimitry Andric using namespace lldb;
32*fe013be4SDimitry Andric using namespace lldb_private;
33*fe013be4SDimitry Andric 
34*fe013be4SDimitry Andric LLDB_PLUGIN_DEFINE(SymbolFileJSON)
35*fe013be4SDimitry Andric 
36*fe013be4SDimitry Andric char SymbolFileJSON::ID;
37*fe013be4SDimitry Andric 
SymbolFileJSON(lldb::ObjectFileSP objfile_sp)38*fe013be4SDimitry Andric SymbolFileJSON::SymbolFileJSON(lldb::ObjectFileSP objfile_sp)
39*fe013be4SDimitry Andric     : SymbolFileCommon(std::move(objfile_sp)) {}
40*fe013be4SDimitry Andric 
Initialize()41*fe013be4SDimitry Andric void SymbolFileJSON::Initialize() {
42*fe013be4SDimitry Andric   PluginManager::RegisterPlugin(GetPluginNameStatic(),
43*fe013be4SDimitry Andric                                 GetPluginDescriptionStatic(), CreateInstance);
44*fe013be4SDimitry Andric }
45*fe013be4SDimitry Andric 
Terminate()46*fe013be4SDimitry Andric void SymbolFileJSON::Terminate() {
47*fe013be4SDimitry Andric   PluginManager::UnregisterPlugin(CreateInstance);
48*fe013be4SDimitry Andric }
49*fe013be4SDimitry Andric 
GetPluginDescriptionStatic()50*fe013be4SDimitry Andric llvm::StringRef SymbolFileJSON::GetPluginDescriptionStatic() {
51*fe013be4SDimitry Andric   return "Reads debug symbols from a JSON symbol table.";
52*fe013be4SDimitry Andric }
53*fe013be4SDimitry Andric 
CreateInstance(ObjectFileSP objfile_sp)54*fe013be4SDimitry Andric SymbolFile *SymbolFileJSON::CreateInstance(ObjectFileSP objfile_sp) {
55*fe013be4SDimitry Andric   return new SymbolFileJSON(std::move(objfile_sp));
56*fe013be4SDimitry Andric }
57*fe013be4SDimitry Andric 
CalculateAbilities()58*fe013be4SDimitry Andric uint32_t SymbolFileJSON::CalculateAbilities() {
59*fe013be4SDimitry Andric   if (!m_objfile_sp || !llvm::isa<ObjectFileJSON>(*m_objfile_sp))
60*fe013be4SDimitry Andric     return 0;
61*fe013be4SDimitry Andric 
62*fe013be4SDimitry Andric   return GlobalVariables | Functions;
63*fe013be4SDimitry Andric }
64*fe013be4SDimitry Andric 
ResolveSymbolContext(const Address & so_addr,SymbolContextItem resolve_scope,SymbolContext & sc)65*fe013be4SDimitry Andric uint32_t SymbolFileJSON::ResolveSymbolContext(const Address &so_addr,
66*fe013be4SDimitry Andric                                               SymbolContextItem resolve_scope,
67*fe013be4SDimitry Andric                                               SymbolContext &sc) {
68*fe013be4SDimitry Andric   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
69*fe013be4SDimitry Andric   if (m_objfile_sp->GetSymtab() == nullptr)
70*fe013be4SDimitry Andric     return 0;
71*fe013be4SDimitry Andric 
72*fe013be4SDimitry Andric   uint32_t resolved_flags = 0;
73*fe013be4SDimitry Andric   if (resolve_scope & eSymbolContextSymbol) {
74*fe013be4SDimitry Andric     sc.symbol = m_objfile_sp->GetSymtab()->FindSymbolContainingFileAddress(
75*fe013be4SDimitry Andric         so_addr.GetFileAddress());
76*fe013be4SDimitry Andric     if (sc.symbol)
77*fe013be4SDimitry Andric       resolved_flags |= eSymbolContextSymbol;
78*fe013be4SDimitry Andric   }
79*fe013be4SDimitry Andric   return resolved_flags;
80*fe013be4SDimitry Andric }
81*fe013be4SDimitry Andric 
ParseCompileUnitAtIndex(uint32_t idx)82*fe013be4SDimitry Andric CompUnitSP SymbolFileJSON::ParseCompileUnitAtIndex(uint32_t idx) { return {}; }
83*fe013be4SDimitry Andric 
GetTypes(SymbolContextScope * sc_scope,TypeClass type_mask,lldb_private::TypeList & type_list)84*fe013be4SDimitry Andric void SymbolFileJSON::GetTypes(SymbolContextScope *sc_scope, TypeClass type_mask,
85*fe013be4SDimitry Andric                               lldb_private::TypeList &type_list) {}
86*fe013be4SDimitry Andric 
AddSymbols(Symtab & symtab)87*fe013be4SDimitry Andric void SymbolFileJSON::AddSymbols(Symtab &symtab) {
88*fe013be4SDimitry Andric   if (!m_objfile_sp)
89*fe013be4SDimitry Andric     return;
90*fe013be4SDimitry Andric 
91*fe013be4SDimitry Andric   Symtab *json_symtab = m_objfile_sp->GetSymtab();
92*fe013be4SDimitry Andric   if (!json_symtab)
93*fe013be4SDimitry Andric     return;
94*fe013be4SDimitry Andric 
95*fe013be4SDimitry Andric   if (&symtab == json_symtab)
96*fe013be4SDimitry Andric     return;
97*fe013be4SDimitry Andric 
98*fe013be4SDimitry Andric   // Merge the two symbol tables.
99*fe013be4SDimitry Andric   const size_t num_new_symbols = json_symtab->GetNumSymbols();
100*fe013be4SDimitry Andric   for (size_t i = 0; i < num_new_symbols; ++i) {
101*fe013be4SDimitry Andric     Symbol *s = json_symtab->SymbolAtIndex(i);
102*fe013be4SDimitry Andric     symtab.AddSymbol(*s);
103*fe013be4SDimitry Andric   }
104*fe013be4SDimitry Andric   symtab.Finalize();
105*fe013be4SDimitry Andric }
106