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 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 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. 105 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 134 void CompileUnit::AddFunction(FunctionSP &funcSP) { 135 m_functions_by_uid[funcSP->GetID()] = funcSP; 136 } 137 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 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 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 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 175 void CompileUnit::SetSupportFiles(const FileSpecList &support_files) { 176 m_support_files = support_files; 177 } 178 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 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 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 210 uint32_t CompileUnit::FindLineEntry(uint32_t start_idx, uint32_t line, 211 const FileSpec *file_spec_ptr, bool exact, 212 LineEntry *line_entry_ptr) { 213 uint32_t file_idx = 0; 214 215 if (file_spec_ptr) { 216 file_idx = GetSupportFiles().FindFileIndex(1, *file_spec_ptr, true); 217 if (file_idx == UINT32_MAX) 218 return UINT32_MAX; 219 } else { 220 // All the line table entries actually point to the version of the Compile 221 // Unit that is in the support files (the one at 0 was artificially added.) 222 // So prefer the one further on in the support files if it exists... 223 const FileSpecList &support_files = GetSupportFiles(); 224 const bool full = true; 225 file_idx = support_files.FindFileIndex( 226 1, support_files.GetFileSpecAtIndex(0), full); 227 if (file_idx == UINT32_MAX) 228 file_idx = 0; 229 } 230 LineTable *line_table = GetLineTable(); 231 if (line_table) 232 return line_table->FindLineEntryIndexByFileIndex(start_idx, file_idx, line, 233 exact, line_entry_ptr); 234 return UINT32_MAX; 235 } 236 237 void CompileUnit::ResolveSymbolContext(const FileSpec &file_spec, 238 uint32_t line, bool check_inlines, 239 bool exact, 240 SymbolContextItem resolve_scope, 241 SymbolContextList &sc_list) { 242 // First find all of the file indexes that match our "file_spec". If 243 // "file_spec" has an empty directory, then only compare the basenames when 244 // finding file indexes 245 std::vector<uint32_t> file_indexes; 246 bool file_spec_matches_cu_file_spec = 247 FileSpec::Match(file_spec, this->GetPrimaryFile()); 248 249 // If we are not looking for inlined functions and our file spec doesn't 250 // match then we are done... 251 if (!file_spec_matches_cu_file_spec && !check_inlines) 252 return; 253 254 uint32_t file_idx = 255 GetSupportFiles().FindFileIndex(1, file_spec, true); 256 while (file_idx != UINT32_MAX) { 257 file_indexes.push_back(file_idx); 258 file_idx = GetSupportFiles().FindFileIndex(file_idx + 1, file_spec, true); 259 } 260 261 const size_t num_file_indexes = file_indexes.size(); 262 if (num_file_indexes == 0) 263 return; 264 265 SymbolContext sc(GetModule()); 266 sc.comp_unit = this; 267 268 if (line == 0) { 269 if (file_spec_matches_cu_file_spec && !check_inlines) { 270 // only append the context if we aren't looking for inline call sites by 271 // file and line and if the file spec matches that of the compile unit 272 sc_list.Append(sc); 273 } 274 return; 275 } 276 277 LineTable *line_table = sc.comp_unit->GetLineTable(); 278 279 if (line_table == nullptr) 280 return; 281 282 uint32_t line_idx; 283 LineEntry line_entry; 284 285 if (num_file_indexes == 1) { 286 // We only have a single support file that matches, so use the line 287 // table function that searches for a line entries that match a single 288 // support file index 289 line_idx = line_table->FindLineEntryIndexByFileIndex( 290 0, file_indexes.front(), line, exact, &line_entry); 291 } else { 292 // We found multiple support files that match "file_spec" so use the 293 // line table function that searches for a line entries that match a 294 // multiple support file indexes. 295 line_idx = line_table->FindLineEntryIndexByFileIndex(0, file_indexes, line, 296 exact, &line_entry); 297 } 298 299 // If "exact == true", then "found_line" will be the same as "line". If 300 // "exact == false", the "found_line" will be the closest line entry 301 // with a line number greater than "line" and we will use this for our 302 // subsequent line exact matches below. 303 uint32_t found_line = line_entry.line; 304 305 while (line_idx != UINT32_MAX) { 306 // If they only asked for the line entry, then we're done, we can 307 // just copy that over. But if they wanted more than just the line 308 // number, fill it in. 309 if (resolve_scope == eSymbolContextLineEntry) { 310 sc.line_entry = line_entry; 311 } else { 312 line_entry.range.GetBaseAddress().CalculateSymbolContext(&sc, 313 resolve_scope); 314 } 315 316 sc_list.Append(sc); 317 if (num_file_indexes == 1) 318 line_idx = line_table->FindLineEntryIndexByFileIndex( 319 line_idx + 1, file_indexes.front(), found_line, true, &line_entry); 320 else 321 line_idx = line_table->FindLineEntryIndexByFileIndex( 322 line_idx + 1, file_indexes, found_line, true, &line_entry); 323 } 324 } 325 326 bool CompileUnit::GetIsOptimized() { 327 if (m_is_optimized == eLazyBoolCalculate) { 328 m_is_optimized = eLazyBoolNo; 329 if (SymbolFile *symfile = GetModule()->GetSymbolFile()) { 330 if (symfile->ParseIsOptimized(*this)) 331 m_is_optimized = eLazyBoolYes; 332 } 333 } 334 return m_is_optimized; 335 } 336 337 void CompileUnit::SetVariableList(VariableListSP &variables) { 338 m_variables = variables; 339 } 340 341 const std::vector<SourceModule> &CompileUnit::GetImportedModules() { 342 if (m_imported_modules.empty() && 343 m_flags.IsClear(flagsParsedImportedModules)) { 344 m_flags.Set(flagsParsedImportedModules); 345 if (SymbolFile *symfile = GetModule()->GetSymbolFile()) { 346 SymbolContext sc; 347 CalculateSymbolContext(&sc); 348 symfile->ParseImportedModules(sc, m_imported_modules); 349 } 350 } 351 return m_imported_modules; 352 } 353 354 bool CompileUnit::ForEachExternalModule( 355 llvm::DenseSet<SymbolFile *> &visited_symbol_files, 356 llvm::function_ref<bool(Module &)> lambda) { 357 if (SymbolFile *symfile = GetModule()->GetSymbolFile()) 358 return symfile->ForEachExternalModule(*this, visited_symbol_files, lambda); 359 return false; 360 } 361 362 const FileSpecList &CompileUnit::GetSupportFiles() { 363 if (m_support_files.GetSize() == 0) { 364 if (m_flags.IsClear(flagsParsedSupportFiles)) { 365 m_flags.Set(flagsParsedSupportFiles); 366 if (SymbolFile *symfile = GetModule()->GetSymbolFile()) 367 symfile->ParseSupportFiles(*this, m_support_files); 368 } 369 } 370 return m_support_files; 371 } 372 373 void *CompileUnit::GetUserData() const { return m_user_data; } 374