• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- CompileUnit.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 "lldb/Symbol/CompileUnit.h"
10 #include "lldb/Core/Module.h"
11 #include "lldb/Symbol/LineTable.h"
12 #include "lldb/Symbol/SymbolFile.h"
13 #include "lldb/Symbol/VariableList.h"
14 #include "lldb/Target/Language.h"
15 #include "lldb/Utility/Timer.h"
16 
17 using namespace lldb;
18 using namespace lldb_private;
19 
CompileUnit(const lldb::ModuleSP & module_sp,void * user_data,const char * pathname,const lldb::user_id_t cu_sym_id,lldb::LanguageType language,lldb_private::LazyBool is_optimized)20 CompileUnit::CompileUnit(const lldb::ModuleSP &module_sp, void *user_data,
21                          const char *pathname, const lldb::user_id_t cu_sym_id,
22                          lldb::LanguageType language,
23                          lldb_private::LazyBool is_optimized)
24     : CompileUnit(module_sp, user_data, FileSpec(pathname), cu_sym_id, language,
25                   is_optimized) {}
26 
CompileUnit(const lldb::ModuleSP & module_sp,void * user_data,const FileSpec & fspec,const lldb::user_id_t cu_sym_id,lldb::LanguageType language,lldb_private::LazyBool is_optimized)27 CompileUnit::CompileUnit(const lldb::ModuleSP &module_sp, void *user_data,
28                          const FileSpec &fspec, const lldb::user_id_t cu_sym_id,
29                          lldb::LanguageType language,
30                          lldb_private::LazyBool is_optimized)
31     : ModuleChild(module_sp), UserID(cu_sym_id), m_user_data(user_data),
32       m_language(language), m_flags(0), m_file_spec(fspec),
33       m_is_optimized(is_optimized) {
34   if (language != eLanguageTypeUnknown)
35     m_flags.Set(flagsParsedLanguage);
36   assert(module_sp);
37 }
38 
CalculateSymbolContext(SymbolContext * sc)39 void CompileUnit::CalculateSymbolContext(SymbolContext *sc) {
40   sc->comp_unit = this;
41   GetModule()->CalculateSymbolContext(sc);
42 }
43 
CalculateSymbolContextModule()44 ModuleSP CompileUnit::CalculateSymbolContextModule() { return GetModule(); }
45 
CalculateSymbolContextCompileUnit()46 CompileUnit *CompileUnit::CalculateSymbolContextCompileUnit() { return this; }
47 
DumpSymbolContext(Stream * s)48 void CompileUnit::DumpSymbolContext(Stream *s) {
49   GetModule()->DumpSymbolContext(s);
50   s->Printf(", CompileUnit{0x%8.8" PRIx64 "}", GetID());
51 }
52 
GetDescription(Stream * s,lldb::DescriptionLevel level) const53 void CompileUnit::GetDescription(Stream *s,
54                                  lldb::DescriptionLevel level) const {
55   const char *language = Language::GetNameForLanguageType(m_language);
56   *s << "id = " << (const UserID &)*this << ", file = \""
57      << this->GetPrimaryFile() << "\", language = \"" << language << '"';
58 }
59 
ForeachFunction(llvm::function_ref<bool (const FunctionSP &)> lambda) const60 void CompileUnit::ForeachFunction(
61     llvm::function_ref<bool(const FunctionSP &)> lambda) const {
62   std::vector<lldb::FunctionSP> sorted_functions;
63   sorted_functions.reserve(m_functions_by_uid.size());
64   for (auto &p : m_functions_by_uid)
65     sorted_functions.push_back(p.second);
66   llvm::sort(sorted_functions.begin(), sorted_functions.end(),
67              [](const lldb::FunctionSP &a, const lldb::FunctionSP &b) {
68                return a->GetID() < b->GetID();
69              });
70 
71   for (auto &f : sorted_functions)
72     if (lambda(f))
73       return;
74 }
75 
FindFunction(llvm::function_ref<bool (const FunctionSP &)> matching_lambda)76 lldb::FunctionSP CompileUnit::FindFunction(
77     llvm::function_ref<bool(const FunctionSP &)> matching_lambda) {
78   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
79   Timer scoped_timer(func_cat, "CompileUnit::FindFunction");
80 
81   lldb::ModuleSP module = CalculateSymbolContextModule();
82 
83   if (!module)
84     return {};
85 
86   SymbolFile *symbol_file = module->GetSymbolFile();
87 
88   if (!symbol_file)
89     return {};
90 
91   // m_functions_by_uid is filled in lazily but we need all the entries.
92   symbol_file->ParseFunctions(*this);
93 
94   for (auto &p : m_functions_by_uid) {
95     if (matching_lambda(p.second))
96       return p.second;
97   }
98   return {};
99 }
100 
101 // Dump the current contents of this object. No functions that cause on demand
102 // parsing of functions, globals, statics are called, so this is a good
103 // function to call to get an idea of the current contents of the CompileUnit
104 // object.
Dump(Stream * s,bool show_context) const105 void CompileUnit::Dump(Stream *s, bool show_context) const {
106   const char *language = Language::GetNameForLanguageType(m_language);
107 
108   s->Printf("%p: ", static_cast<const void *>(this));
109   s->Indent();
110   *s << "CompileUnit" << static_cast<const UserID &>(*this) << ", language = \""
111      << language << "\", file = '" << GetPrimaryFile() << "'\n";
112 
113   //  m_types.Dump(s);
114 
115   if (m_variables.get()) {
116     s->IndentMore();
117     m_variables->Dump(s, show_context);
118     s->IndentLess();
119   }
120 
121   if (!m_functions_by_uid.empty()) {
122     s->IndentMore();
123     ForeachFunction([&s, show_context](const FunctionSP &f) {
124       f->Dump(s, show_context);
125       return false;
126     });
127 
128     s->IndentLess();
129     s->EOL();
130   }
131 }
132 
133 // Add a function to this compile unit
AddFunction(FunctionSP & funcSP)134 void CompileUnit::AddFunction(FunctionSP &funcSP) {
135   m_functions_by_uid[funcSP->GetID()] = funcSP;
136 }
137 
FindFunctionByUID(lldb::user_id_t func_uid)138 FunctionSP CompileUnit::FindFunctionByUID(lldb::user_id_t func_uid) {
139   auto it = m_functions_by_uid.find(func_uid);
140   if (it == m_functions_by_uid.end())
141     return FunctionSP();
142   return it->second;
143 }
144 
GetLanguage()145 lldb::LanguageType CompileUnit::GetLanguage() {
146   if (m_language == eLanguageTypeUnknown) {
147     if (m_flags.IsClear(flagsParsedLanguage)) {
148       m_flags.Set(flagsParsedLanguage);
149       if (SymbolFile *symfile = GetModule()->GetSymbolFile())
150         m_language = symfile->ParseLanguage(*this);
151     }
152   }
153   return m_language;
154 }
155 
GetLineTable()156 LineTable *CompileUnit::GetLineTable() {
157   if (m_line_table_up == nullptr) {
158     if (m_flags.IsClear(flagsParsedLineTable)) {
159       m_flags.Set(flagsParsedLineTable);
160       if (SymbolFile *symfile = GetModule()->GetSymbolFile())
161         symfile->ParseLineTable(*this);
162     }
163   }
164   return m_line_table_up.get();
165 }
166 
SetLineTable(LineTable * line_table)167 void CompileUnit::SetLineTable(LineTable *line_table) {
168   if (line_table == nullptr)
169     m_flags.Clear(flagsParsedLineTable);
170   else
171     m_flags.Set(flagsParsedLineTable);
172   m_line_table_up.reset(line_table);
173 }
174 
SetSupportFiles(const FileSpecList & support_files)175 void CompileUnit::SetSupportFiles(const FileSpecList &support_files) {
176   m_support_files = support_files;
177 }
178 
GetDebugMacros()179 DebugMacros *CompileUnit::GetDebugMacros() {
180   if (m_debug_macros_sp.get() == nullptr) {
181     if (m_flags.IsClear(flagsParsedDebugMacros)) {
182       m_flags.Set(flagsParsedDebugMacros);
183       if (SymbolFile *symfile = GetModule()->GetSymbolFile())
184         symfile->ParseDebugMacros(*this);
185     }
186   }
187 
188   return m_debug_macros_sp.get();
189 }
190 
SetDebugMacros(const DebugMacrosSP & debug_macros_sp)191 void CompileUnit::SetDebugMacros(const DebugMacrosSP &debug_macros_sp) {
192   if (debug_macros_sp.get() == nullptr)
193     m_flags.Clear(flagsParsedDebugMacros);
194   else
195     m_flags.Set(flagsParsedDebugMacros);
196   m_debug_macros_sp = debug_macros_sp;
197 }
198 
GetVariableList(bool can_create)199 VariableListSP CompileUnit::GetVariableList(bool can_create) {
200   if (m_variables.get() == nullptr && can_create) {
201     SymbolContext sc;
202     CalculateSymbolContext(&sc);
203     assert(sc.module_sp);
204     sc.module_sp->GetSymbolFile()->ParseVariablesForContext(sc);
205   }
206 
207   return m_variables;
208 }
209 
FindFileIndexes(const FileSpecList & files,const FileSpec & file)210 std::vector<uint32_t> FindFileIndexes(const FileSpecList &files, const FileSpec &file) {
211   std::vector<uint32_t> result;
212   uint32_t idx = -1;
213   while ((idx = files.FindFileIndex(idx + 1, file, /*full=*/true)) !=
214          UINT32_MAX)
215     result.push_back(idx);
216   return result;
217 }
218 
FindLineEntry(uint32_t start_idx,uint32_t line,const FileSpec * file_spec_ptr,bool exact,LineEntry * line_entry_ptr)219 uint32_t CompileUnit::FindLineEntry(uint32_t start_idx, uint32_t line,
220                                     const FileSpec *file_spec_ptr, bool exact,
221                                     LineEntry *line_entry_ptr) {
222   if (!file_spec_ptr)
223     file_spec_ptr = &GetPrimaryFile();
224   std::vector<uint32_t> file_indexes = FindFileIndexes(GetSupportFiles(), *file_spec_ptr);
225   if (file_indexes.empty())
226     return UINT32_MAX;
227 
228   LineTable *line_table = GetLineTable();
229   if (line_table)
230     return line_table->FindLineEntryIndexByFileIndex(
231         start_idx, file_indexes, line, exact, line_entry_ptr);
232   return UINT32_MAX;
233 }
234 
ResolveSymbolContext(const FileSpec & file_spec,uint32_t line,bool check_inlines,bool exact,SymbolContextItem resolve_scope,SymbolContextList & sc_list)235 void CompileUnit::ResolveSymbolContext(const FileSpec &file_spec,
236                                        uint32_t line, bool check_inlines,
237                                        bool exact,
238                                        SymbolContextItem resolve_scope,
239                                        SymbolContextList &sc_list) {
240   // First find all of the file indexes that match our "file_spec". If
241   // "file_spec" has an empty directory, then only compare the basenames when
242   // finding file indexes
243   std::vector<uint32_t> file_indexes;
244   bool file_spec_matches_cu_file_spec =
245       FileSpec::Match(file_spec, this->GetPrimaryFile());
246 
247   // If we are not looking for inlined functions and our file spec doesn't
248   // match then we are done...
249   if (!file_spec_matches_cu_file_spec && !check_inlines)
250     return;
251 
252   uint32_t file_idx =
253       GetSupportFiles().FindFileIndex(0, file_spec, true);
254   while (file_idx != UINT32_MAX) {
255     file_indexes.push_back(file_idx);
256     file_idx = GetSupportFiles().FindFileIndex(file_idx + 1, file_spec, true);
257   }
258 
259   const size_t num_file_indexes = file_indexes.size();
260   if (num_file_indexes == 0)
261     return;
262 
263   SymbolContext sc(GetModule());
264   sc.comp_unit = this;
265 
266   if (line == 0) {
267     if (file_spec_matches_cu_file_spec && !check_inlines) {
268       // only append the context if we aren't looking for inline call sites by
269       // file and line and if the file spec matches that of the compile unit
270       sc_list.Append(sc);
271     }
272     return;
273   }
274 
275   LineTable *line_table = sc.comp_unit->GetLineTable();
276 
277   if (line_table == nullptr)
278     return;
279 
280   uint32_t line_idx;
281   LineEntry line_entry;
282 
283   if (num_file_indexes == 1) {
284     // We only have a single support file that matches, so use the line
285     // table function that searches for a line entries that match a single
286     // support file index
287     line_idx = line_table->FindLineEntryIndexByFileIndex(
288         0, file_indexes.front(), line, exact, &line_entry);
289   } else {
290     // We found multiple support files that match "file_spec" so use the
291     // line table function that searches for a line entries that match a
292     // multiple support file indexes.
293     line_idx = line_table->FindLineEntryIndexByFileIndex(0, file_indexes, line,
294                                                          exact, &line_entry);
295   }
296 
297   // If "exact == true", then "found_line" will be the same as "line". If
298   // "exact == false", the "found_line" will be the closest line entry
299   // with a line number greater than "line" and we will use this for our
300   // subsequent line exact matches below.
301   uint32_t found_line = line_entry.line;
302 
303   while (line_idx != UINT32_MAX) {
304     // If they only asked for the line entry, then we're done, we can
305     // just copy that over. But if they wanted more than just the line
306     // number, fill it in.
307     if (resolve_scope == eSymbolContextLineEntry) {
308       sc.line_entry = line_entry;
309     } else {
310       line_entry.range.GetBaseAddress().CalculateSymbolContext(&sc,
311                                                                resolve_scope);
312     }
313 
314     sc_list.Append(sc);
315     if (num_file_indexes == 1)
316       line_idx = line_table->FindLineEntryIndexByFileIndex(
317           line_idx + 1, file_indexes.front(), found_line, true, &line_entry);
318     else
319       line_idx = line_table->FindLineEntryIndexByFileIndex(
320           line_idx + 1, file_indexes, found_line, true, &line_entry);
321   }
322 }
323 
GetIsOptimized()324 bool CompileUnit::GetIsOptimized() {
325   if (m_is_optimized == eLazyBoolCalculate) {
326     m_is_optimized = eLazyBoolNo;
327     if (SymbolFile *symfile = GetModule()->GetSymbolFile()) {
328       if (symfile->ParseIsOptimized(*this))
329         m_is_optimized = eLazyBoolYes;
330     }
331   }
332   return m_is_optimized;
333 }
334 
SetVariableList(VariableListSP & variables)335 void CompileUnit::SetVariableList(VariableListSP &variables) {
336   m_variables = variables;
337 }
338 
GetImportedModules()339 const std::vector<SourceModule> &CompileUnit::GetImportedModules() {
340   if (m_imported_modules.empty() &&
341       m_flags.IsClear(flagsParsedImportedModules)) {
342     m_flags.Set(flagsParsedImportedModules);
343     if (SymbolFile *symfile = GetModule()->GetSymbolFile()) {
344       SymbolContext sc;
345       CalculateSymbolContext(&sc);
346       symfile->ParseImportedModules(sc, m_imported_modules);
347     }
348   }
349   return m_imported_modules;
350 }
351 
ForEachExternalModule(llvm::DenseSet<SymbolFile * > & visited_symbol_files,llvm::function_ref<bool (Module &)> lambda)352 bool CompileUnit::ForEachExternalModule(
353     llvm::DenseSet<SymbolFile *> &visited_symbol_files,
354     llvm::function_ref<bool(Module &)> lambda) {
355   if (SymbolFile *symfile = GetModule()->GetSymbolFile())
356     return symfile->ForEachExternalModule(*this, visited_symbol_files, lambda);
357   return false;
358 }
359 
GetSupportFiles()360 const FileSpecList &CompileUnit::GetSupportFiles() {
361   if (m_support_files.GetSize() == 0) {
362     if (m_flags.IsClear(flagsParsedSupportFiles)) {
363       m_flags.Set(flagsParsedSupportFiles);
364       if (SymbolFile *symfile = GetModule()->GetSymbolFile())
365         symfile->ParseSupportFiles(*this, m_support_files);
366     }
367   }
368   return m_support_files;
369 }
370 
GetUserData() const371 void *CompileUnit::GetUserData() const { return m_user_data; }
372