Path: blob/main/contrib/llvm-project/lldb/source/Symbol/CompileUnit.cpp
39587 views
//===-- CompileUnit.cpp ---------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "lldb/Symbol/CompileUnit.h"9#include "lldb/Core/Module.h"10#include "lldb/Symbol/LineTable.h"11#include "lldb/Symbol/SymbolFile.h"12#include "lldb/Symbol/VariableList.h"13#include "lldb/Target/Language.h"14#include "lldb/Utility/Timer.h"15#include <optional>1617using namespace lldb;18using namespace lldb_private;1920CompileUnit::CompileUnit(const lldb::ModuleSP &module_sp, void *user_data,21const char *pathname, const lldb::user_id_t cu_sym_id,22lldb::LanguageType language,23lldb_private::LazyBool is_optimized)24: CompileUnit(module_sp, user_data,25std::make_shared<SupportFile>(FileSpec(pathname)), cu_sym_id,26language, is_optimized) {}2728CompileUnit::CompileUnit(const lldb::ModuleSP &module_sp, void *user_data,29lldb::SupportFileSP support_file_sp,30const lldb::user_id_t cu_sym_id,31lldb::LanguageType language,32lldb_private::LazyBool is_optimized,33SupportFileList &&support_files)34: ModuleChild(module_sp), UserID(cu_sym_id), m_user_data(user_data),35m_language(language), m_flags(0),36m_primary_support_file_sp(support_file_sp),37m_support_files(std::move(support_files)), m_is_optimized(is_optimized) {38if (language != eLanguageTypeUnknown)39m_flags.Set(flagsParsedLanguage);40assert(module_sp);41}4243void CompileUnit::CalculateSymbolContext(SymbolContext *sc) {44sc->comp_unit = this;45GetModule()->CalculateSymbolContext(sc);46}4748ModuleSP CompileUnit::CalculateSymbolContextModule() { return GetModule(); }4950CompileUnit *CompileUnit::CalculateSymbolContextCompileUnit() { return this; }5152void CompileUnit::DumpSymbolContext(Stream *s) {53GetModule()->DumpSymbolContext(s);54s->Printf(", CompileUnit{0x%8.8" PRIx64 "}", GetID());55}5657void CompileUnit::GetDescription(Stream *s,58lldb::DescriptionLevel level) const {59const char *language = GetCachedLanguage();60*s << "id = " << (const UserID &)*this << ", file = \""61<< this->GetPrimaryFile() << "\", language = \"" << language << '"';62}6364void CompileUnit::ForeachFunction(65llvm::function_ref<bool(const FunctionSP &)> lambda) const {66std::vector<lldb::FunctionSP> sorted_functions;67sorted_functions.reserve(m_functions_by_uid.size());68for (auto &p : m_functions_by_uid)69sorted_functions.push_back(p.second);70llvm::sort(sorted_functions,71[](const lldb::FunctionSP &a, const lldb::FunctionSP &b) {72return a->GetID() < b->GetID();73});7475for (auto &f : sorted_functions)76if (lambda(f))77return;78}7980lldb::FunctionSP CompileUnit::FindFunction(81llvm::function_ref<bool(const FunctionSP &)> matching_lambda) {82LLDB_SCOPED_TIMER();8384lldb::ModuleSP module = CalculateSymbolContextModule();8586if (!module)87return {};8889SymbolFile *symbol_file = module->GetSymbolFile();9091if (!symbol_file)92return {};9394// m_functions_by_uid is filled in lazily but we need all the entries.95symbol_file->ParseFunctions(*this);9697for (auto &p : m_functions_by_uid) {98if (matching_lambda(p.second))99return p.second;100}101return {};102}103104const char *CompileUnit::GetCachedLanguage() const {105if (m_flags.IsClear(flagsParsedLanguage))106return "<not loaded>";107return Language::GetNameForLanguageType(m_language);108}109110// Dump the current contents of this object. No functions that cause on demand111// parsing of functions, globals, statics are called, so this is a good112// function to call to get an idea of the current contents of the CompileUnit113// object.114void CompileUnit::Dump(Stream *s, bool show_context) const {115const char *language = GetCachedLanguage();116117s->Printf("%p: ", static_cast<const void *>(this));118s->Indent();119*s << "CompileUnit" << static_cast<const UserID &>(*this) << ", language = \""120<< language << "\", file = '" << GetPrimaryFile() << "'\n";121122// m_types.Dump(s);123124if (m_variables.get()) {125s->IndentMore();126m_variables->Dump(s, show_context);127s->IndentLess();128}129130if (!m_functions_by_uid.empty()) {131s->IndentMore();132ForeachFunction([&s, show_context](const FunctionSP &f) {133f->Dump(s, show_context);134return false;135});136137s->IndentLess();138s->EOL();139}140}141142// Add a function to this compile unit143void CompileUnit::AddFunction(FunctionSP &funcSP) {144m_functions_by_uid[funcSP->GetID()] = funcSP;145}146147FunctionSP CompileUnit::FindFunctionByUID(lldb::user_id_t func_uid) {148auto it = m_functions_by_uid.find(func_uid);149if (it == m_functions_by_uid.end())150return FunctionSP();151return it->second;152}153154lldb::LanguageType CompileUnit::GetLanguage() {155if (m_language == eLanguageTypeUnknown) {156if (m_flags.IsClear(flagsParsedLanguage)) {157m_flags.Set(flagsParsedLanguage);158if (SymbolFile *symfile = GetModule()->GetSymbolFile())159m_language = symfile->ParseLanguage(*this);160}161}162return m_language;163}164165LineTable *CompileUnit::GetLineTable() {166if (m_line_table_up == nullptr) {167if (m_flags.IsClear(flagsParsedLineTable)) {168m_flags.Set(flagsParsedLineTable);169if (SymbolFile *symfile = GetModule()->GetSymbolFile())170symfile->ParseLineTable(*this);171}172}173return m_line_table_up.get();174}175176void CompileUnit::SetLineTable(LineTable *line_table) {177if (line_table == nullptr)178m_flags.Clear(flagsParsedLineTable);179else180m_flags.Set(flagsParsedLineTable);181m_line_table_up.reset(line_table);182}183184DebugMacros *CompileUnit::GetDebugMacros() {185if (m_debug_macros_sp.get() == nullptr) {186if (m_flags.IsClear(flagsParsedDebugMacros)) {187m_flags.Set(flagsParsedDebugMacros);188if (SymbolFile *symfile = GetModule()->GetSymbolFile())189symfile->ParseDebugMacros(*this);190}191}192193return m_debug_macros_sp.get();194}195196void CompileUnit::SetDebugMacros(const DebugMacrosSP &debug_macros_sp) {197if (debug_macros_sp.get() == nullptr)198m_flags.Clear(flagsParsedDebugMacros);199else200m_flags.Set(flagsParsedDebugMacros);201m_debug_macros_sp = debug_macros_sp;202}203204VariableListSP CompileUnit::GetVariableList(bool can_create) {205if (m_variables.get() == nullptr && can_create) {206SymbolContext sc;207CalculateSymbolContext(&sc);208assert(sc.module_sp);209sc.module_sp->GetSymbolFile()->ParseVariablesForContext(sc);210}211212return m_variables;213}214215std::vector<uint32_t> FindFileIndexes(const SupportFileList &files,216const FileSpec &file) {217std::vector<uint32_t> result;218uint32_t idx = -1;219while ((idx = files.FindCompatibleIndex(idx + 1, file)) !=220UINT32_MAX)221result.push_back(idx);222return result;223}224225uint32_t CompileUnit::FindLineEntry(uint32_t start_idx, uint32_t line,226const FileSpec *file_spec_ptr, bool exact,227LineEntry *line_entry_ptr) {228if (!file_spec_ptr)229file_spec_ptr = &GetPrimaryFile();230std::vector<uint32_t> file_indexes = FindFileIndexes(GetSupportFiles(),231*file_spec_ptr);232if (file_indexes.empty())233return UINT32_MAX;234235// TODO: Handle SourceLocationSpec column information236SourceLocationSpec location_spec(*file_spec_ptr, line,237/*column=*/std::nullopt,238/*check_inlines=*/false, exact);239240LineTable *line_table = GetLineTable();241if (line_table)242return line_table->FindLineEntryIndexByFileIndex(243start_idx, file_indexes, location_spec, line_entry_ptr);244return UINT32_MAX;245}246247void CompileUnit::ResolveSymbolContext(248const SourceLocationSpec &src_location_spec,249SymbolContextItem resolve_scope, SymbolContextList &sc_list) {250const FileSpec file_spec = src_location_spec.GetFileSpec();251const uint32_t line = src_location_spec.GetLine().value_or(0);252const bool check_inlines = src_location_spec.GetCheckInlines();253254// First find all of the file indexes that match our "file_spec". If255// "file_spec" has an empty directory, then only compare the basenames when256// finding file indexes257bool file_spec_matches_cu_file_spec =258FileSpec::Match(file_spec, this->GetPrimaryFile());259260// If we are not looking for inlined functions and our file spec doesn't261// match then we are done...262if (!file_spec_matches_cu_file_spec && !check_inlines)263return;264265SymbolContext sc(GetModule());266sc.comp_unit = this;267268if (line == 0) {269if (file_spec_matches_cu_file_spec && !check_inlines) {270// only append the context if we aren't looking for inline call sites by271// file and line and if the file spec matches that of the compile unit272sc_list.Append(sc);273}274return;275}276277std::vector<uint32_t> file_indexes = FindFileIndexes(GetSupportFiles(),278file_spec);279const size_t num_file_indexes = file_indexes.size();280if (num_file_indexes == 0)281return;282283// Found a matching source file in this compile unit load its debug info.284GetModule()->GetSymbolFile()->SetLoadDebugInfoEnabled();285286LineTable *line_table = sc.comp_unit->GetLineTable();287288if (line_table == nullptr) {289if (file_spec_matches_cu_file_spec && !check_inlines) {290sc_list.Append(sc);291}292return;293}294295uint32_t line_idx;296LineEntry line_entry;297298if (num_file_indexes == 1) {299// We only have a single support file that matches, so use the line300// table function that searches for a line entries that match a single301// support file index302line_idx = line_table->FindLineEntryIndexByFileIndex(3030, file_indexes.front(), src_location_spec, &line_entry);304} else {305// We found multiple support files that match "file_spec" so use the306// line table function that searches for a line entries that match a307// multiple support file indexes.308line_idx = line_table->FindLineEntryIndexByFileIndex(3090, file_indexes, src_location_spec, &line_entry);310}311312// If "exact == true", then "found_line" will be the same as "line". If313// "exact == false", the "found_line" will be the closest line entry314// with a line number greater than "line" and we will use this for our315// subsequent line exact matches below.316const bool inlines = false;317const bool exact = true;318const std::optional<uint16_t> column =319src_location_spec.GetColumn() ? std::optional<uint16_t>(line_entry.column)320: std::nullopt;321322SourceLocationSpec found_entry(line_entry.GetFile(), line_entry.line, column,323inlines, exact);324325while (line_idx != UINT32_MAX) {326// If they only asked for the line entry, then we're done, we can327// just copy that over. But if they wanted more than just the line328// number, fill it in.329SymbolContext resolved_sc;330sc.line_entry = line_entry;331if (resolve_scope == eSymbolContextLineEntry) {332sc_list.Append(sc);333} else {334line_entry.range.GetBaseAddress().CalculateSymbolContext(&resolved_sc,335resolve_scope);336// Sometimes debug info is bad and isn't able to resolve the line entry's337// address back to the same compile unit and/or line entry. If the compile338// unit changed, then revert back to just the compile unit and line entry.339// Prior to this fix, the above code might end up not being able to lookup340// the address, and then it would clear compile unit and the line entry in341// the symbol context and the breakpoint would fail to get set even though342// we have a valid line table entry in this compile unit. The address343// lookup can also end up finding another function in another compiler344// unit if the DWARF has overlappging address ranges. So if we end up with345// no compile unit or a different one after the above function call,346// revert back to the same results as if resolve_scope was set exactly to347// eSymbolContextLineEntry.348if (resolved_sc.comp_unit == this) {349sc_list.Append(resolved_sc);350} else {351if (resolved_sc.comp_unit == nullptr && resolved_sc.module_sp) {352// Only report an error if we don't map back to any compile unit. With353// link time optimizations, the debug info might have many compile354// units that have the same address range due to function outlining355// or other link time optimizations. If the compile unit is NULL, then356// address resolving is completely failing and more deserving of an357// error message the user can see.358resolved_sc.module_sp->ReportError(359"unable to resolve a line table file address {0:x16} back "360"to a compile unit, please file a bug and attach the address "361"and file.",362line_entry.range.GetBaseAddress().GetFileAddress());363}364sc_list.Append(sc);365}366}367368if (num_file_indexes == 1)369line_idx = line_table->FindLineEntryIndexByFileIndex(370line_idx + 1, file_indexes.front(), found_entry, &line_entry);371else372line_idx = line_table->FindLineEntryIndexByFileIndex(373line_idx + 1, file_indexes, found_entry, &line_entry);374}375}376377bool CompileUnit::GetIsOptimized() {378if (m_is_optimized == eLazyBoolCalculate) {379m_is_optimized = eLazyBoolNo;380if (SymbolFile *symfile = GetModule()->GetSymbolFile()) {381if (symfile->ParseIsOptimized(*this))382m_is_optimized = eLazyBoolYes;383}384}385return m_is_optimized;386}387388void CompileUnit::SetVariableList(VariableListSP &variables) {389m_variables = variables;390}391392const std::vector<SourceModule> &CompileUnit::GetImportedModules() {393if (m_imported_modules.empty() &&394m_flags.IsClear(flagsParsedImportedModules)) {395m_flags.Set(flagsParsedImportedModules);396if (SymbolFile *symfile = GetModule()->GetSymbolFile()) {397SymbolContext sc;398CalculateSymbolContext(&sc);399symfile->ParseImportedModules(sc, m_imported_modules);400}401}402return m_imported_modules;403}404405bool CompileUnit::ForEachExternalModule(406llvm::DenseSet<SymbolFile *> &visited_symbol_files,407llvm::function_ref<bool(Module &)> lambda) {408if (SymbolFile *symfile = GetModule()->GetSymbolFile())409return symfile->ForEachExternalModule(*this, visited_symbol_files, lambda);410return false;411}412413const SupportFileList &CompileUnit::GetSupportFiles() {414if (m_support_files.GetSize() == 0) {415if (m_flags.IsClear(flagsParsedSupportFiles)) {416m_flags.Set(flagsParsedSupportFiles);417if (SymbolFile *symfile = GetModule()->GetSymbolFile())418symfile->ParseSupportFiles(*this, m_support_files);419}420}421return m_support_files;422}423424void *CompileUnit::GetUserData() const { return m_user_data; }425426427