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 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), 38 m_support_files(), m_line_table_up(), m_variables(), 39 m_is_optimized(is_optimized) { 40 if (language != eLanguageTypeUnknown) 41 m_flags.Set(flagsParsedLanguage); 42 assert(module_sp); 43 } 44 45 CompileUnit::~CompileUnit() {} 46 47 void CompileUnit::CalculateSymbolContext(SymbolContext *sc) { 48 sc->comp_unit = this; 49 GetModule()->CalculateSymbolContext(sc); 50 } 51 52 ModuleSP CompileUnit::CalculateSymbolContextModule() { return GetModule(); } 53 54 CompileUnit *CompileUnit::CalculateSymbolContextCompileUnit() { return this; } 55 56 void CompileUnit::DumpSymbolContext(Stream *s) { 57 GetModule()->DumpSymbolContext(s); 58 s->Printf(", CompileUnit{0x%8.8" PRIx64 "}", GetID()); 59 } 60 61 void CompileUnit::GetDescription(Stream *s, 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 68 void CompileUnit::ForeachFunction( 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 FunctionSP CompileUnit::FindFunctionByUID(lldb::user_id_t func_uid) { 123 auto it = m_functions_by_uid.find(func_uid); 124 if (it == m_functions_by_uid.end()) 125 return FunctionSP(); 126 return it->second; 127 } 128 129 lldb::LanguageType CompileUnit::GetLanguage() { 130 if (m_language == eLanguageTypeUnknown) { 131 if (m_flags.IsClear(flagsParsedLanguage)) { 132 m_flags.Set(flagsParsedLanguage); 133 if (SymbolFile *symfile = GetModule()->GetSymbolFile()) 134 m_language = symfile->ParseLanguage(*this); 135 } 136 } 137 return m_language; 138 } 139 140 LineTable *CompileUnit::GetLineTable() { 141 if (m_line_table_up == nullptr) { 142 if (m_flags.IsClear(flagsParsedLineTable)) { 143 m_flags.Set(flagsParsedLineTable); 144 if (SymbolFile *symfile = GetModule()->GetSymbolFile()) 145 symfile->ParseLineTable(*this); 146 } 147 } 148 return m_line_table_up.get(); 149 } 150 151 void CompileUnit::SetLineTable(LineTable *line_table) { 152 if (line_table == nullptr) 153 m_flags.Clear(flagsParsedLineTable); 154 else 155 m_flags.Set(flagsParsedLineTable); 156 m_line_table_up.reset(line_table); 157 } 158 159 void CompileUnit::SetSupportFiles(const FileSpecList &support_files) { 160 m_support_files = support_files; 161 } 162 163 DebugMacros *CompileUnit::GetDebugMacros() { 164 if (m_debug_macros_sp.get() == nullptr) { 165 if (m_flags.IsClear(flagsParsedDebugMacros)) { 166 m_flags.Set(flagsParsedDebugMacros); 167 if (SymbolFile *symfile = GetModule()->GetSymbolFile()) 168 symfile->ParseDebugMacros(*this); 169 } 170 } 171 172 return m_debug_macros_sp.get(); 173 } 174 175 void CompileUnit::SetDebugMacros(const DebugMacrosSP &debug_macros_sp) { 176 if (debug_macros_sp.get() == nullptr) 177 m_flags.Clear(flagsParsedDebugMacros); 178 else 179 m_flags.Set(flagsParsedDebugMacros); 180 m_debug_macros_sp = debug_macros_sp; 181 } 182 183 VariableListSP CompileUnit::GetVariableList(bool can_create) { 184 if (m_variables.get() == nullptr && can_create) { 185 SymbolContext sc; 186 CalculateSymbolContext(&sc); 187 assert(sc.module_sp); 188 sc.module_sp->GetSymbolFile()->ParseVariablesForContext(sc); 189 } 190 191 return m_variables; 192 } 193 194 uint32_t CompileUnit::FindLineEntry(uint32_t start_idx, uint32_t line, 195 const FileSpec *file_spec_ptr, bool exact, 196 LineEntry *line_entry_ptr) { 197 uint32_t file_idx = 0; 198 199 if (file_spec_ptr) { 200 file_idx = GetSupportFiles().FindFileIndex(1, *file_spec_ptr, true); 201 if (file_idx == UINT32_MAX) 202 return UINT32_MAX; 203 } else { 204 // All the line table entries actually point to the version of the Compile 205 // Unit that is in the support files (the one at 0 was artificially added.) 206 // So prefer the one further on in the support files if it exists... 207 const FileSpecList &support_files = GetSupportFiles(); 208 const bool full = true; 209 file_idx = support_files.FindFileIndex( 210 1, support_files.GetFileSpecAtIndex(0), full); 211 if (file_idx == UINT32_MAX) 212 file_idx = 0; 213 } 214 LineTable *line_table = GetLineTable(); 215 if (line_table) 216 return line_table->FindLineEntryIndexByFileIndex(start_idx, file_idx, line, 217 exact, line_entry_ptr); 218 return UINT32_MAX; 219 } 220 221 uint32_t CompileUnit::ResolveSymbolContext(const FileSpec &file_spec, 222 uint32_t line, bool check_inlines, 223 bool exact, 224 SymbolContextItem resolve_scope, 225 SymbolContextList &sc_list) { 226 // First find all of the file indexes that match our "file_spec". If 227 // "file_spec" has an empty directory, then only compare the basenames when 228 // finding file indexes 229 std::vector<uint32_t> file_indexes; 230 const bool full_match = (bool)file_spec.GetDirectory(); 231 bool file_spec_matches_cu_file_spec = 232 FileSpec::Equal(file_spec, *this, full_match); 233 234 // If we are not looking for inlined functions and our file spec doesn't 235 // match then we are done... 236 if (!file_spec_matches_cu_file_spec && !check_inlines) 237 return 0; 238 239 uint32_t file_idx = 240 GetSupportFiles().FindFileIndex(1, file_spec, true); 241 while (file_idx != UINT32_MAX) { 242 file_indexes.push_back(file_idx); 243 file_idx = GetSupportFiles().FindFileIndex(file_idx + 1, file_spec, true); 244 } 245 246 const size_t num_file_indexes = file_indexes.size(); 247 if (num_file_indexes == 0) 248 return 0; 249 250 const uint32_t prev_size = sc_list.GetSize(); 251 252 SymbolContext sc(GetModule()); 253 sc.comp_unit = this; 254 255 if (line != 0) { 256 LineTable *line_table = sc.comp_unit->GetLineTable(); 257 258 if (line_table != nullptr) { 259 uint32_t found_line; 260 uint32_t line_idx; 261 262 if (num_file_indexes == 1) { 263 // We only have a single support file that matches, so use the line 264 // table function that searches for a line entries that match a single 265 // support file index 266 LineEntry line_entry; 267 line_idx = line_table->FindLineEntryIndexByFileIndex( 268 0, file_indexes.front(), line, exact, &line_entry); 269 270 // If "exact == true", then "found_line" will be the same as "line". If 271 // "exact == false", the "found_line" will be the closest line entry 272 // with a line number greater than "line" and we will use this for our 273 // subsequent line exact matches below. 274 found_line = line_entry.line; 275 276 while (line_idx != UINT32_MAX) { 277 // If they only asked for the line entry, then we're done, we can 278 // just copy that over. But if they wanted more than just the line 279 // number, fill it in. 280 if (resolve_scope == eSymbolContextLineEntry) { 281 sc.line_entry = line_entry; 282 } else { 283 line_entry.range.GetBaseAddress().CalculateSymbolContext( 284 &sc, resolve_scope); 285 } 286 287 sc_list.Append(sc); 288 line_idx = line_table->FindLineEntryIndexByFileIndex( 289 line_idx + 1, file_indexes.front(), found_line, true, 290 &line_entry); 291 } 292 } else { 293 // We found multiple support files that match "file_spec" so use the 294 // line table function that searches for a line entries that match a 295 // multiple support file indexes. 296 LineEntry line_entry; 297 line_idx = line_table->FindLineEntryIndexByFileIndex( 298 0, file_indexes, line, exact, &line_entry); 299 300 // If "exact == true", then "found_line" will be the same as "line". If 301 // "exact == false", the "found_line" will be the closest line entry 302 // with a line number greater than "line" and we will use this for our 303 // subsequent line exact matches below. 304 found_line = line_entry.line; 305 306 while (line_idx != UINT32_MAX) { 307 if (resolve_scope == eSymbolContextLineEntry) { 308 sc.line_entry = line_entry; 309 } else { 310 line_entry.range.GetBaseAddress().CalculateSymbolContext( 311 &sc, resolve_scope); 312 } 313 314 sc_list.Append(sc); 315 line_idx = line_table->FindLineEntryIndexByFileIndex( 316 line_idx + 1, file_indexes, found_line, true, &line_entry); 317 } 318 } 319 } 320 } else if (file_spec_matches_cu_file_spec && !check_inlines) { 321 // only append the context if we aren't looking for inline call sites by 322 // file and line and if the file spec matches that of the compile unit 323 sc_list.Append(sc); 324 } 325 return sc_list.GetSize() - prev_size; 326 } 327 328 bool CompileUnit::GetIsOptimized() { 329 if (m_is_optimized == eLazyBoolCalculate) { 330 m_is_optimized = eLazyBoolNo; 331 if (SymbolFile *symfile = GetModule()->GetSymbolFile()) { 332 if (symfile->ParseIsOptimized(*this)) 333 m_is_optimized = eLazyBoolYes; 334 } 335 } 336 return m_is_optimized; 337 } 338 339 void CompileUnit::SetVariableList(VariableListSP &variables) { 340 m_variables = variables; 341 } 342 343 const std::vector<SourceModule> &CompileUnit::GetImportedModules() { 344 if (m_imported_modules.empty() && 345 m_flags.IsClear(flagsParsedImportedModules)) { 346 m_flags.Set(flagsParsedImportedModules); 347 if (SymbolFile *symfile = GetModule()->GetSymbolFile()) { 348 SymbolContext sc; 349 CalculateSymbolContext(&sc); 350 symfile->ParseImportedModules(sc, m_imported_modules); 351 } 352 } 353 return m_imported_modules; 354 } 355 356 void CompileUnit::ForEachExternalModule(llvm::function_ref<void(ModuleSP)> f) { 357 if (SymbolFile *symfile = GetModule()->GetSymbolFile()) 358 symfile->ForEachExternalModule(*this, f); 359 } 360 361 const FileSpecList &CompileUnit::GetSupportFiles() { 362 if (m_support_files.GetSize() == 0) { 363 if (m_flags.IsClear(flagsParsedSupportFiles)) { 364 m_flags.Set(flagsParsedSupportFiles); 365 if (SymbolFile *symfile = GetModule()->GetSymbolFile()) 366 symfile->ParseSupportFiles(*this, m_support_files); 367 } 368 } 369 return m_support_files; 370 } 371 372 void *CompileUnit::GetUserData() const { return m_user_data; } 373