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