llvm-project
425 строк · 14.9 Кб
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#include <optional>17
18using namespace lldb;19using namespace lldb_private;20
21CompileUnit::CompileUnit(const lldb::ModuleSP &module_sp, void *user_data,22const char *pathname, const lldb::user_id_t cu_sym_id,23lldb::LanguageType language,24lldb_private::LazyBool is_optimized)25: CompileUnit(module_sp, user_data,26std::make_shared<SupportFile>(FileSpec(pathname)), cu_sym_id,27language, is_optimized) {}28
29CompileUnit::CompileUnit(const lldb::ModuleSP &module_sp, void *user_data,30lldb::SupportFileSP support_file_sp,31const lldb::user_id_t cu_sym_id,32lldb::LanguageType language,33lldb_private::LazyBool is_optimized,34SupportFileList &&support_files)35: ModuleChild(module_sp), UserID(cu_sym_id), m_user_data(user_data),36m_language(language), m_flags(0),37m_primary_support_file_sp(support_file_sp),38m_support_files(std::move(support_files)), m_is_optimized(is_optimized) {39if (language != eLanguageTypeUnknown)40m_flags.Set(flagsParsedLanguage);41assert(module_sp);42}
43
44void CompileUnit::CalculateSymbolContext(SymbolContext *sc) {45sc->comp_unit = this;46GetModule()->CalculateSymbolContext(sc);47}
48
49ModuleSP CompileUnit::CalculateSymbolContextModule() { return GetModule(); }50
51CompileUnit *CompileUnit::CalculateSymbolContextCompileUnit() { return this; }52
53void CompileUnit::DumpSymbolContext(Stream *s) {54GetModule()->DumpSymbolContext(s);55s->Printf(", CompileUnit{0x%8.8" PRIx64 "}", GetID());56}
57
58void CompileUnit::GetDescription(Stream *s,59lldb::DescriptionLevel level) const {60const char *language = GetCachedLanguage();61*s << "id = " << (const UserID &)*this << ", file = \""62<< this->GetPrimaryFile() << "\", language = \"" << language << '"';63}
64
65void CompileUnit::ForeachFunction(66llvm::function_ref<bool(const FunctionSP &)> lambda) const {67std::vector<lldb::FunctionSP> sorted_functions;68sorted_functions.reserve(m_functions_by_uid.size());69for (auto &p : m_functions_by_uid)70sorted_functions.push_back(p.second);71llvm::sort(sorted_functions,72[](const lldb::FunctionSP &a, const lldb::FunctionSP &b) {73return a->GetID() < b->GetID();74});75
76for (auto &f : sorted_functions)77if (lambda(f))78return;79}
80
81lldb::FunctionSP CompileUnit::FindFunction(82llvm::function_ref<bool(const FunctionSP &)> matching_lambda) {83LLDB_SCOPED_TIMER();84
85lldb::ModuleSP module = CalculateSymbolContextModule();86
87if (!module)88return {};89
90SymbolFile *symbol_file = module->GetSymbolFile();91
92if (!symbol_file)93return {};94
95// m_functions_by_uid is filled in lazily but we need all the entries.96symbol_file->ParseFunctions(*this);97
98for (auto &p : m_functions_by_uid) {99if (matching_lambda(p.second))100return p.second;101}102return {};103}
104
105const char *CompileUnit::GetCachedLanguage() const {106if (m_flags.IsClear(flagsParsedLanguage))107return "<not loaded>";108return Language::GetNameForLanguageType(m_language);109}
110
111// Dump the current contents of this object. No functions that cause on demand
112// parsing of functions, globals, statics are called, so this is a good
113// function to call to get an idea of the current contents of the CompileUnit
114// object.
115void CompileUnit::Dump(Stream *s, bool show_context) const {116const char *language = GetCachedLanguage();117
118s->Printf("%p: ", static_cast<const void *>(this));119s->Indent();120*s << "CompileUnit" << static_cast<const UserID &>(*this) << ", language = \""121<< language << "\", file = '" << GetPrimaryFile() << "'\n";122
123// m_types.Dump(s);124
125if (m_variables.get()) {126s->IndentMore();127m_variables->Dump(s, show_context);128s->IndentLess();129}130
131if (!m_functions_by_uid.empty()) {132s->IndentMore();133ForeachFunction([&s, show_context](const FunctionSP &f) {134f->Dump(s, show_context);135return false;136});137
138s->IndentLess();139s->EOL();140}141}
142
143// Add a function to this compile unit
144void CompileUnit::AddFunction(FunctionSP &funcSP) {145m_functions_by_uid[funcSP->GetID()] = funcSP;146}
147
148FunctionSP CompileUnit::FindFunctionByUID(lldb::user_id_t func_uid) {149auto it = m_functions_by_uid.find(func_uid);150if (it == m_functions_by_uid.end())151return FunctionSP();152return it->second;153}
154
155lldb::LanguageType CompileUnit::GetLanguage() {156if (m_language == eLanguageTypeUnknown) {157if (m_flags.IsClear(flagsParsedLanguage)) {158m_flags.Set(flagsParsedLanguage);159if (SymbolFile *symfile = GetModule()->GetSymbolFile())160m_language = symfile->ParseLanguage(*this);161}162}163return m_language;164}
165
166LineTable *CompileUnit::GetLineTable() {167if (m_line_table_up == nullptr) {168if (m_flags.IsClear(flagsParsedLineTable)) {169m_flags.Set(flagsParsedLineTable);170if (SymbolFile *symfile = GetModule()->GetSymbolFile())171symfile->ParseLineTable(*this);172}173}174return m_line_table_up.get();175}
176
177void CompileUnit::SetLineTable(LineTable *line_table) {178if (line_table == nullptr)179m_flags.Clear(flagsParsedLineTable);180else181m_flags.Set(flagsParsedLineTable);182m_line_table_up.reset(line_table);183}
184
185DebugMacros *CompileUnit::GetDebugMacros() {186if (m_debug_macros_sp.get() == nullptr) {187if (m_flags.IsClear(flagsParsedDebugMacros)) {188m_flags.Set(flagsParsedDebugMacros);189if (SymbolFile *symfile = GetModule()->GetSymbolFile())190symfile->ParseDebugMacros(*this);191}192}193
194return m_debug_macros_sp.get();195}
196
197void CompileUnit::SetDebugMacros(const DebugMacrosSP &debug_macros_sp) {198if (debug_macros_sp.get() == nullptr)199m_flags.Clear(flagsParsedDebugMacros);200else201m_flags.Set(flagsParsedDebugMacros);202m_debug_macros_sp = debug_macros_sp;203}
204
205VariableListSP CompileUnit::GetVariableList(bool can_create) {206if (m_variables.get() == nullptr && can_create) {207SymbolContext sc;208CalculateSymbolContext(&sc);209assert(sc.module_sp);210sc.module_sp->GetSymbolFile()->ParseVariablesForContext(sc);211}212
213return m_variables;214}
215
216std::vector<uint32_t> FindFileIndexes(const SupportFileList &files,217const FileSpec &file) {218std::vector<uint32_t> result;219uint32_t idx = -1;220while ((idx = files.FindCompatibleIndex(idx + 1, file)) !=221UINT32_MAX)222result.push_back(idx);223return result;224}
225
226uint32_t CompileUnit::FindLineEntry(uint32_t start_idx, uint32_t line,227const FileSpec *file_spec_ptr, bool exact,228LineEntry *line_entry_ptr) {229if (!file_spec_ptr)230file_spec_ptr = &GetPrimaryFile();231std::vector<uint32_t> file_indexes = FindFileIndexes(GetSupportFiles(),232*file_spec_ptr);233if (file_indexes.empty())234return UINT32_MAX;235
236// TODO: Handle SourceLocationSpec column information237SourceLocationSpec location_spec(*file_spec_ptr, line,238/*column=*/std::nullopt,239/*check_inlines=*/false, exact);240
241LineTable *line_table = GetLineTable();242if (line_table)243return line_table->FindLineEntryIndexByFileIndex(244start_idx, file_indexes, location_spec, line_entry_ptr);245return UINT32_MAX;246}
247
248void CompileUnit::ResolveSymbolContext(249const SourceLocationSpec &src_location_spec,250SymbolContextItem resolve_scope, SymbolContextList &sc_list) {251const FileSpec file_spec = src_location_spec.GetFileSpec();252const uint32_t line = src_location_spec.GetLine().value_or(0);253const bool check_inlines = src_location_spec.GetCheckInlines();254
255// First find all of the file indexes that match our "file_spec". If256// "file_spec" has an empty directory, then only compare the basenames when257// finding file indexes258bool file_spec_matches_cu_file_spec =259FileSpec::Match(file_spec, this->GetPrimaryFile());260
261// If we are not looking for inlined functions and our file spec doesn't262// match then we are done...263if (!file_spec_matches_cu_file_spec && !check_inlines)264return;265
266SymbolContext sc(GetModule());267sc.comp_unit = this;268
269if (line == 0) {270if (file_spec_matches_cu_file_spec && !check_inlines) {271// only append the context if we aren't looking for inline call sites by272// file and line and if the file spec matches that of the compile unit273sc_list.Append(sc);274}275return;276}277
278std::vector<uint32_t> file_indexes = FindFileIndexes(GetSupportFiles(),279file_spec);280const size_t num_file_indexes = file_indexes.size();281if (num_file_indexes == 0)282return;283
284// Found a matching source file in this compile unit load its debug info.285GetModule()->GetSymbolFile()->SetLoadDebugInfoEnabled();286
287LineTable *line_table = sc.comp_unit->GetLineTable();288
289if (line_table == nullptr) {290if (file_spec_matches_cu_file_spec && !check_inlines) {291sc_list.Append(sc);292}293return;294}295
296uint32_t line_idx;297LineEntry line_entry;298
299if (num_file_indexes == 1) {300// We only have a single support file that matches, so use the line301// table function that searches for a line entries that match a single302// support file index303line_idx = line_table->FindLineEntryIndexByFileIndex(3040, file_indexes.front(), src_location_spec, &line_entry);305} else {306// We found multiple support files that match "file_spec" so use the307// line table function that searches for a line entries that match a308// multiple support file indexes.309line_idx = line_table->FindLineEntryIndexByFileIndex(3100, file_indexes, src_location_spec, &line_entry);311}312
313// If "exact == true", then "found_line" will be the same as "line". If314// "exact == false", the "found_line" will be the closest line entry315// with a line number greater than "line" and we will use this for our316// subsequent line exact matches below.317const bool inlines = false;318const bool exact = true;319const std::optional<uint16_t> column =320src_location_spec.GetColumn() ? std::optional<uint16_t>(line_entry.column)321: std::nullopt;322
323SourceLocationSpec found_entry(line_entry.GetFile(), line_entry.line, column,324inlines, exact);325
326while (line_idx != UINT32_MAX) {327// If they only asked for the line entry, then we're done, we can328// just copy that over. But if they wanted more than just the line329// number, fill it in.330SymbolContext resolved_sc;331sc.line_entry = line_entry;332if (resolve_scope == eSymbolContextLineEntry) {333sc_list.Append(sc);334} else {335line_entry.range.GetBaseAddress().CalculateSymbolContext(&resolved_sc,336resolve_scope);337// Sometimes debug info is bad and isn't able to resolve the line entry's338// address back to the same compile unit and/or line entry. If the compile339// unit changed, then revert back to just the compile unit and line entry.340// Prior to this fix, the above code might end up not being able to lookup341// the address, and then it would clear compile unit and the line entry in342// the symbol context and the breakpoint would fail to get set even though343// we have a valid line table entry in this compile unit. The address344// lookup can also end up finding another function in another compiler345// unit if the DWARF has overlappging address ranges. So if we end up with346// no compile unit or a different one after the above function call,347// revert back to the same results as if resolve_scope was set exactly to348// eSymbolContextLineEntry.349if (resolved_sc.comp_unit == this) {350sc_list.Append(resolved_sc);351} else {352if (resolved_sc.comp_unit == nullptr && resolved_sc.module_sp) {353// Only report an error if we don't map back to any compile unit. With354// link time optimizations, the debug info might have many compile355// units that have the same address range due to function outlining356// or other link time optimizations. If the compile unit is NULL, then357// address resolving is completely failing and more deserving of an358// error message the user can see.359resolved_sc.module_sp->ReportError(360"unable to resolve a line table file address {0:x16} back "361"to a compile unit, please file a bug and attach the address "362"and file.",363line_entry.range.GetBaseAddress().GetFileAddress());364}365sc_list.Append(sc);366}367}368
369if (num_file_indexes == 1)370line_idx = line_table->FindLineEntryIndexByFileIndex(371line_idx + 1, file_indexes.front(), found_entry, &line_entry);372else373line_idx = line_table->FindLineEntryIndexByFileIndex(374line_idx + 1, file_indexes, found_entry, &line_entry);375}376}
377
378bool CompileUnit::GetIsOptimized() {379if (m_is_optimized == eLazyBoolCalculate) {380m_is_optimized = eLazyBoolNo;381if (SymbolFile *symfile = GetModule()->GetSymbolFile()) {382if (symfile->ParseIsOptimized(*this))383m_is_optimized = eLazyBoolYes;384}385}386return m_is_optimized;387}
388
389void CompileUnit::SetVariableList(VariableListSP &variables) {390m_variables = variables;391}
392
393const std::vector<SourceModule> &CompileUnit::GetImportedModules() {394if (m_imported_modules.empty() &&395m_flags.IsClear(flagsParsedImportedModules)) {396m_flags.Set(flagsParsedImportedModules);397if (SymbolFile *symfile = GetModule()->GetSymbolFile()) {398SymbolContext sc;399CalculateSymbolContext(&sc);400symfile->ParseImportedModules(sc, m_imported_modules);401}402}403return m_imported_modules;404}
405
406bool CompileUnit::ForEachExternalModule(407llvm::DenseSet<SymbolFile *> &visited_symbol_files,408llvm::function_ref<bool(Module &)> lambda) {409if (SymbolFile *symfile = GetModule()->GetSymbolFile())410return symfile->ForEachExternalModule(*this, visited_symbol_files, lambda);411return false;412}
413
414const SupportFileList &CompileUnit::GetSupportFiles() {415if (m_support_files.GetSize() == 0) {416if (m_flags.IsClear(flagsParsedSupportFiles)) {417m_flags.Set(flagsParsedSupportFiles);418if (SymbolFile *symfile = GetModule()->GetSymbolFile())419symfile->ParseSupportFiles(*this, m_support_files);420}421}422return m_support_files;423}
424
425void *CompileUnit::GetUserData() const { return m_user_data; }426