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