LLDB  mainline
CompileUnit.cpp
Go to the documentation of this file.
1 //===-- CompileUnit.cpp -----------------------------------------*- C++ -*-===//
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 
10 #include "lldb/Core/Module.h"
11 #include "lldb/Symbol/LineTable.h"
14 #include "lldb/Target/Language.h"
15 
16 using namespace lldb;
17 using namespace lldb_private;
18 
19 CompileUnit::CompileUnit(const lldb::ModuleSP &module_sp, void *user_data,
20  const char *pathname, const lldb::user_id_t cu_sym_id,
21  lldb::LanguageType language,
22  lldb_private::LazyBool is_optimized)
23  : ModuleChild(module_sp), FileSpec(pathname), UserID(cu_sym_id),
24  m_user_data(user_data), m_language(language), m_flags(0),
25  m_support_files(), m_line_table_up(), m_variables(),
26  m_is_optimized(is_optimized) {
27  if (language != eLanguageTypeUnknown)
28  m_flags.Set(flagsParsedLanguage);
29  assert(module_sp);
30 }
31 
32 CompileUnit::CompileUnit(const lldb::ModuleSP &module_sp, void *user_data,
33  const FileSpec &fspec, const lldb::user_id_t cu_sym_id,
34  lldb::LanguageType language,
35  lldb_private::LazyBool is_optimized)
36  : ModuleChild(module_sp), FileSpec(fspec), UserID(cu_sym_id),
37  m_user_data(user_data), m_language(language), m_flags(0),
39  m_is_optimized(is_optimized) {
40  if (language != eLanguageTypeUnknown)
41  m_flags.Set(flagsParsedLanguage);
42  assert(module_sp);
43 }
44 
46 
48  sc->comp_unit = this;
49  GetModule()->CalculateSymbolContext(sc);
50 }
51 
53 
55 
57  GetModule()->DumpSymbolContext(s);
58  s->Printf(", CompileUnit{0x%8.8" PRIx64 "}", GetID());
59 }
60 
62  lldb::DescriptionLevel level) const {
63  const char *language = Language::GetNameForLanguageType(m_language);
64  *s << "id = " << (const UserID &)*this << ", file = \""
65  << (const FileSpec &)*this << "\", language = \"" << language << '"';
66 }
67 
69  llvm::function_ref<bool(const FunctionSP &)> lambda) const {
70  std::vector<lldb::FunctionSP> sorted_functions;
71  sorted_functions.reserve(m_functions_by_uid.size());
72  for (auto &p : m_functions_by_uid)
73  sorted_functions.push_back(p.second);
74  llvm::sort(sorted_functions.begin(), sorted_functions.end(),
75  [](const lldb::FunctionSP &a, const lldb::FunctionSP &b) {
76  return a->GetID() < b->GetID();
77  });
78 
79  for (auto &f : sorted_functions)
80  if (lambda(f))
81  return;
82 }
83 
84 // Dump the current contents of this object. No functions that cause on demand
85 // parsing of functions, globals, statics are called, so this is a good
86 // function to call to get an idea of the current contents of the CompileUnit
87 // object.
88 void CompileUnit::Dump(Stream *s, bool show_context) const {
89  const char *language = Language::GetNameForLanguageType(m_language);
90 
91  s->Printf("%p: ", static_cast<const void *>(this));
92  s->Indent();
93  *s << "CompileUnit" << static_cast<const UserID &>(*this) << ", language = \""
94  << language << "\", file = '" << static_cast<const FileSpec &>(*this)
95  << "'\n";
96 
97  // m_types.Dump(s);
98 
99  if (m_variables.get()) {
100  s->IndentMore();
101  m_variables->Dump(s, show_context);
102  s->IndentLess();
103  }
104 
105  if (!m_functions_by_uid.empty()) {
106  s->IndentMore();
107  ForeachFunction([&s, show_context](const FunctionSP &f) {
108  f->Dump(s, show_context);
109  return false;
110  });
111 
112  s->IndentLess();
113  s->EOL();
114  }
115 }
116 
117 // Add a function to this compile unit
118 void CompileUnit::AddFunction(FunctionSP &funcSP) {
119  m_functions_by_uid[funcSP->GetID()] = funcSP;
120 }
121 
122 // Find functions using the Mangled::Tokens token list. This function currently
123 // implements an interactive approach designed to find all instances of certain
124 // functions. It isn't designed to the quickest way to lookup functions as it
125 // will need to iterate through all functions and see if they match, though it
126 // does provide a powerful and context sensitive way to search for all
127 // functions with a certain name, all functions in a namespace, or all
128 // functions of a template type. See Mangled::Tokens::Parse() comments for more
129 // information.
130 //
131 // The function prototype will need to change to return a list of results. It
132 // was originally used to help debug the Mangled class and the
133 // Mangled::Tokens::MatchesQuery() function and it currently will print out a
134 // list of matching results for the functions that are currently in this
135 // compile unit.
136 //
137 // A FindFunctions method should be called prior to this that takes
138 // a regular function name (const char * or ConstString as a parameter) before
139 // resorting to this slower but more complete function. The other FindFunctions
140 // method should be able to take advantage of any accelerator tables available
141 // in the debug information (which is parsed by the SymbolFile parser plug-ins
142 // and registered with each Module).
143 // void
144 // CompileUnit::FindFunctions(const Mangled::Tokens& tokens)
145 //{
146 // if (!m_functions.empty())
147 // {
148 // Stream s(stdout);
149 // std::vector<FunctionSP>::const_iterator pos;
150 // std::vector<FunctionSP>::const_iterator end = m_functions.end();
151 // for (pos = m_functions.begin(); pos != end; ++pos)
152 // {
153 // const ConstString& demangled = (*pos)->Mangled().Demangled();
154 // if (demangled)
155 // {
156 // const Mangled::Tokens& func_tokens =
157 // (*pos)->Mangled().GetTokens();
158 // if (func_tokens.MatchesQuery (tokens))
159 // s << "demangled MATCH found: " << demangled << "\n";
160 // }
161 // }
162 // }
163 //}
164 
166  auto it = m_functions_by_uid.find(func_uid);
167  if (it == m_functions_by_uid.end())
168  return FunctionSP();
169  return it->second;
170 }
171 
174  if (m_flags.IsClear(flagsParsedLanguage)) {
175  m_flags.Set(flagsParsedLanguage);
176  SymbolVendor *symbol_vendor = GetModule()->GetSymbolVendor();
177  if (symbol_vendor) {
178  m_language = symbol_vendor->ParseLanguage(*this);
179  }
180  }
181  }
182  return m_language;
183 }
184 
186  if (m_line_table_up == nullptr) {
187  if (m_flags.IsClear(flagsParsedLineTable)) {
188  m_flags.Set(flagsParsedLineTable);
189  SymbolVendor *symbol_vendor = GetModule()->GetSymbolVendor();
190  if (symbol_vendor)
191  symbol_vendor->ParseLineTable(*this);
192  }
193  }
194  return m_line_table_up.get();
195 }
196 
198  if (line_table == nullptr)
199  m_flags.Clear(flagsParsedLineTable);
200  else
201  m_flags.Set(flagsParsedLineTable);
202  m_line_table_up.reset(line_table);
203 }
204 
206  if (m_debug_macros_sp.get() == nullptr) {
207  if (m_flags.IsClear(flagsParsedDebugMacros)) {
208  m_flags.Set(flagsParsedDebugMacros);
209  SymbolVendor *symbol_vendor = GetModule()->GetSymbolVendor();
210  if (symbol_vendor) {
211  symbol_vendor->ParseDebugMacros(*this);
212  }
213  }
214  }
215 
216  return m_debug_macros_sp.get();
217 }
218 
219 void CompileUnit::SetDebugMacros(const DebugMacrosSP &debug_macros_sp) {
220  if (debug_macros_sp.get() == nullptr)
221  m_flags.Clear(flagsParsedDebugMacros);
222  else
223  m_flags.Set(flagsParsedDebugMacros);
224  m_debug_macros_sp = debug_macros_sp;
225 }
226 
227 VariableListSP CompileUnit::GetVariableList(bool can_create) {
228  if (m_variables.get() == nullptr && can_create) {
229  SymbolContext sc;
231  assert(sc.module_sp);
232  sc.module_sp->GetSymbolVendor()->ParseVariablesForContext(sc);
233  }
234 
235  return m_variables;
236 }
237 
239  const FileSpec *file_spec_ptr, bool exact,
240  LineEntry *line_entry_ptr) {
241  uint32_t file_idx = 0;
242 
243  if (file_spec_ptr) {
244  file_idx = GetSupportFiles().FindFileIndex(1, *file_spec_ptr, true);
245  if (file_idx == UINT32_MAX)
246  return UINT32_MAX;
247  } else {
248  // All the line table entries actually point to the version of the Compile
249  // Unit that is in the support files (the one at 0 was artificially added.)
250  // So prefer the one further on in the support files if it exists...
251  FileSpecList &support_files = GetSupportFiles();
252  const bool full = true;
253  file_idx = support_files.FindFileIndex(
254  1, support_files.GetFileSpecAtIndex(0), full);
255  if (file_idx == UINT32_MAX)
256  file_idx = 0;
257  }
258  LineTable *line_table = GetLineTable();
259  if (line_table)
260  return line_table->FindLineEntryIndexByFileIndex(start_idx, file_idx, line,
261  exact, line_entry_ptr);
262  return UINT32_MAX;
263 }
264 
266  uint32_t line, bool check_inlines,
267  bool exact,
268  SymbolContextItem resolve_scope,
269  SymbolContextList &sc_list) {
270  // First find all of the file indexes that match our "file_spec". If
271  // "file_spec" has an empty directory, then only compare the basenames when
272  // finding file indexes
273  std::vector<uint32_t> file_indexes;
274  const bool full_match = (bool)file_spec.GetDirectory();
275  bool file_spec_matches_cu_file_spec =
276  FileSpec::Equal(file_spec, *this, full_match);
277 
278  // If we are not looking for inlined functions and our file spec doesn't
279  // match then we are done...
280  if (!file_spec_matches_cu_file_spec && !check_inlines)
281  return 0;
282 
283  uint32_t file_idx =
284  GetSupportFiles().FindFileIndex(1, file_spec, true);
285  while (file_idx != UINT32_MAX) {
286  file_indexes.push_back(file_idx);
287  file_idx = GetSupportFiles().FindFileIndex(file_idx + 1, file_spec, true);
288  }
289 
290  const size_t num_file_indexes = file_indexes.size();
291  if (num_file_indexes == 0)
292  return 0;
293 
294  const uint32_t prev_size = sc_list.GetSize();
295 
296  SymbolContext sc(GetModule());
297  sc.comp_unit = this;
298 
299  if (line != 0) {
300  LineTable *line_table = sc.comp_unit->GetLineTable();
301 
302  if (line_table != nullptr) {
303  uint32_t found_line;
304  uint32_t line_idx;
305 
306  if (num_file_indexes == 1) {
307  // We only have a single support file that matches, so use the line
308  // table function that searches for a line entries that match a single
309  // support file index
310  LineEntry line_entry;
311  line_idx = line_table->FindLineEntryIndexByFileIndex(
312  0, file_indexes.front(), line, exact, &line_entry);
313 
314  // If "exact == true", then "found_line" will be the same as "line". If
315  // "exact == false", the "found_line" will be the closest line entry
316  // with a line number greater than "line" and we will use this for our
317  // subsequent line exact matches below.
318  found_line = line_entry.line;
319 
320  while (line_idx != UINT32_MAX) {
321  // If they only asked for the line entry, then we're done, we can
322  // just copy that over. But if they wanted more than just the line
323  // number, fill it in.
324  if (resolve_scope == eSymbolContextLineEntry) {
325  sc.line_entry = line_entry;
326  } else {
328  &sc, resolve_scope);
329  }
330 
331  sc_list.Append(sc);
332  line_idx = line_table->FindLineEntryIndexByFileIndex(
333  line_idx + 1, file_indexes.front(), found_line, true,
334  &line_entry);
335  }
336  } else {
337  // We found multiple support files that match "file_spec" so use the
338  // line table function that searches for a line entries that match a
339  // multiple support file indexes.
340  LineEntry line_entry;
341  line_idx = line_table->FindLineEntryIndexByFileIndex(
342  0, file_indexes, line, exact, &line_entry);
343 
344  // If "exact == true", then "found_line" will be the same as "line". If
345  // "exact == false", the "found_line" will be the closest line entry
346  // with a line number greater than "line" and we will use this for our
347  // subsequent line exact matches below.
348  found_line = line_entry.line;
349 
350  while (line_idx != UINT32_MAX) {
351  if (resolve_scope == eSymbolContextLineEntry) {
352  sc.line_entry = line_entry;
353  } else {
355  &sc, resolve_scope);
356  }
357 
358  sc_list.Append(sc);
359  line_idx = line_table->FindLineEntryIndexByFileIndex(
360  line_idx + 1, file_indexes, found_line, true, &line_entry);
361  }
362  }
363  }
364  } else if (file_spec_matches_cu_file_spec && !check_inlines) {
365  // only append the context if we aren't looking for inline call sites by
366  // file and line and if the file spec matches that of the compile unit
367  sc_list.Append(sc);
368  }
369  return sc_list.GetSize() - prev_size;
370 }
371 
375  if (SymbolVendor *symbol_vendor = GetModule()->GetSymbolVendor()) {
376  if (symbol_vendor->ParseIsOptimized(*this))
378  }
379  }
380  return m_is_optimized;
381 }
382 
383 void CompileUnit::SetVariableList(VariableListSP &variables) {
384  m_variables = variables;
385 }
386 
387 const std::vector<SourceModule> &CompileUnit::GetImportedModules() {
388  if (m_imported_modules.empty() &&
389  m_flags.IsClear(flagsParsedImportedModules)) {
390  m_flags.Set(flagsParsedImportedModules);
391  if (SymbolVendor *symbol_vendor = GetModule()->GetSymbolVendor()) {
392  SymbolContext sc;
394  symbol_vendor->ParseImportedModules(sc, m_imported_modules);
395  }
396  }
397  return m_imported_modules;
398 }
399 
401  if (m_support_files.GetSize() == 0) {
402  if (m_flags.IsClear(flagsParsedSupportFiles)) {
403  m_flags.Set(flagsParsedSupportFiles);
404  SymbolVendor *symbol_vendor = GetModule()->GetSymbolVendor();
405  if (symbol_vendor) {
406  symbol_vendor->ParseSupportFiles(*this, m_support_files);
407  }
408  }
409  }
410  return m_support_files;
411 }
412 
413 void *CompileUnit::GetUserData() const { return m_user_data; }
lldb::LanguageType m_language
The programming language enumeration value.
Definition: CompileUnit.h:379
uint32_t FindLineEntry(uint32_t start_idx, uint32_t line, const FileSpec *file_spec_ptr, bool exact, LineEntry *line_entry)
Find the line entry by line and optional inlined file spec.
void CalculateSymbolContext(SymbolContext *sc) override
Reconstruct the object&#39;s symbol context into sc.
Definition: CompileUnit.cpp:47
A line table entry class.
Definition: LineEntry.h:20
CompileUnit(const lldb::ModuleSP &module_sp, void *user_data, const char *pathname, lldb::user_id_t uid, lldb::LanguageType language, lldb_private::LazyBool is_optimized)
Construct with a module, path, UID and language.
Definition: CompileUnit.cpp:19
void GetDescription(Stream *s, lldb::DescriptionLevel level) const
Definition: CompileUnit.cpp:61
std::unique_ptr< LineTable > m_line_table_up
Line table that will get parsed on demand.
Definition: CompileUnit.h:391
void DumpSymbolContext(Stream *s) override
Dump the object&#39;s symbol context to the stream s.
Definition: CompileUnit.cpp:56
DebugMacros * GetDebugMacros()
Defines a list of symbol context objects.
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
uint32_t CalculateSymbolContext(SymbolContext *sc, lldb::SymbolContextItem resolve_scope=lldb::eSymbolContextEverything) const
Reconstruct a symbol context from an address.
Definition: Address.cpp:782
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
std::vector< SourceModule > m_imported_modules
All modules, including the current module, imported by this compile unit.
Definition: CompileUnit.h:386
Defines a symbol context baton that can be handed other debug core functions.
Definition: SymbolContext.h:33
lldb::VariableListSP m_variables
Global and static variable list that will get parsed on demand.
Definition: CompileUnit.h:395
lldb::user_id_t GetID() const
Get accessor for the user ID.
Definition: UserID.h:49
A mix in class that contains a generic user ID.
Definition: UserID.h:33
A file utility class.
Definition: FileSpec.h:55
lldb::LanguageType GetLanguage()
CompileUnit * comp_unit
The CompileUnit for a given query.
~CompileUnit() override
Destructor.
Definition: CompileUnit.cpp:45
virtual bool ParseLineTable(CompileUnit &comp_unit)
lldb::ModuleSP CalculateSymbolContextModule() override
Definition: CompileUnit.cpp:52
lldb::FunctionSP FindFunctionByUID(lldb::user_id_t uid)
Finds a function by user ID.
const std::vector< SourceModule > & GetImportedModules()
Get the compile unit&#39;s imported module list.
void SetDebugMacros(const DebugMacrosSP &debug_macros)
lldb::ModuleSP GetModule() const
Get const accessor for the module pointer.
Definition: ModuleChild.cpp:27
A class that describes a compilation unit.
Definition: CompileUnit.h:35
virtual bool ParseSupportFiles(CompileUnit &comp_unit, FileSpecList &support_files)
void SetLineTable(LineTable *line_table)
Set the line table for the compile unit.
uint32_t GetSize() const
Get accessor for a symbol context list size.
Flags m_flags
Compile unit flags that help with partial parsing.
Definition: CompileUnit.h:381
DebugMacrosSP m_debug_macros_sp
Debug macros that will get parsed on demand.
Definition: CompileUnit.h:393
ValueType Clear(ValueType mask=~(ValueType) 0)
Clear one or more flags.
Definition: Flags.h:72
#define UINT32_MAX
Definition: lldb-defines.h:31
uint64_t user_id_t
Definition: lldb-types.h:84
LanguageType
Programming language type.
virtual lldb::LanguageType ParseLanguage(CompileUnit &comp_unit)
size_t EOL()
Output and End of Line character to the stream.
Definition: Stream.cpp:127
void * m_user_data
User data for the SymbolFile parser to store information into.
Definition: CompileUnit.h:377
A line table class.
Definition: LineTable.h:37
virtual bool ParseDebugMacros(CompileUnit &comp_unit)
bool GetIsOptimized()
Get whether compiler optimizations were enabled for this compile unit.
uint32_t line
The source line number, or zero if there is no line number information.
Definition: LineEntry.h:154
FileSpecList & GetSupportFiles()
Get the compile unit&#39;s support file list.
void IndentLess(int amount=2)
Decrement the current indentation level.
Definition: Stream.cpp:221
uint32_t FindLineEntryIndexByFileIndex(uint32_t start_idx, uint32_t file_idx, uint32_t line, bool exact, LineEntry *line_entry_ptr)
Find a line entry index that has a matching file index and source line number.
Definition: LineTable.cpp:319
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:106
void Append(const SymbolContext &sc)
Append a new symbol context to the list.
std::shared_ptr< DebugMacros > DebugMacrosSP
Definition: DebugMacros.h:21
lldb::VariableListSP GetVariableList(bool can_create)
Get the variable list for a compile unit.
lldb_private::LazyBool m_is_optimized
eLazyBoolYes if this compile unit was compiled with optimization.
Definition: CompileUnit.h:398
ValueType Set(ValueType mask)
Set one or more flags by logical OR&#39;ing mask with the current flags.
Definition: Flags.h:84
LineTable * GetLineTable()
Get the line table for the compile unit.
llvm::DenseMap< lldb::user_id_t, lldb::FunctionSP > m_functions_by_uid
Maps UIDs to functions.
Definition: CompileUnit.h:383
void Dump(Stream *s, bool show_context) const
Dump the compile unit contents to the stream s.
Definition: CompileUnit.cpp:88
void * GetUserData() const
Get the SymbolFile plug-in user data.
ConstString & GetDirectory()
Directory string get accessor.
Definition: FileSpec.cpp:363
Unknown or invalid language value.
void SetVariableList(lldb::VariableListSP &variable_list_sp)
Set accessor for the variable list.
static bool Equal(const FileSpec &a, const FileSpec &b, bool full)
Definition: FileSpec.cpp:319
lldb::ModuleSP module_sp
The Module for a given query.
LineEntry line_entry
The LineEntry for a given query.
Definition: SBAddress.h:15
size_t Indent(const char *s=nullptr)
Indent the current line in the stream.
Definition: Stream.cpp:131
uint32_t ResolveSymbolContext(const FileSpec &file_spec, uint32_t line, bool check_inlines, bool exact, lldb::SymbolContextItem resolve_scope, SymbolContextList &sc_list)
Resolve symbol contexts by file and line.
Address & GetBaseAddress()
Get accessor for the base address of the range.
Definition: AddressRange.h:220
A mix in class that contains a pointer back to the module that owns the object which inherits from it...
Definition: ModuleChild.h:19
void ForeachFunction(llvm::function_ref< bool(const lldb::FunctionSP &)> lambda) const
Apply a lambda to each function in this compile unit.
Definition: CompileUnit.cpp:68
void IndentMore(int amount=2)
Increment the current indentation level.
Definition: Stream.cpp:218
void AddFunction(lldb::FunctionSP &function_sp)
Add a function to this compile unit.
bool IsClear(ValueType bit) const
Test a single flag bit to see if it is clear (zero).
Definition: Flags.h:122
CompileUnit * CalculateSymbolContextCompileUnit() override
Definition: CompileUnit.cpp:54
FileSpecList m_support_files
Files associated with this compile unit&#39;s line table and declarations.
Definition: CompileUnit.h:389
static const char * GetNameForLanguageType(lldb::LanguageType language)
Definition: Language.cpp:219
AddressRange range
The section offset address range for this line entry.
Definition: LineEntry.h:150