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