180814287SRaphael Isemann //===-- BreakpointResolverFileLine.cpp ------------------------------------===// 230fdc8d8SChris Lattner // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 630fdc8d8SChris Lattner // 730fdc8d8SChris Lattner //===----------------------------------------------------------------------===// 830fdc8d8SChris Lattner 930fdc8d8SChris Lattner #include "lldb/Breakpoint/BreakpointResolverFileLine.h" 1030fdc8d8SChris Lattner 1130fdc8d8SChris Lattner #include "lldb/Breakpoint/BreakpointLocation.h" 121f746071SGreg Clayton #include "lldb/Core/Module.h" 131f746071SGreg Clayton #include "lldb/Symbol/CompileUnit.h" 141f746071SGreg Clayton #include "lldb/Symbol/Function.h" 156f9e6901SZachary Turner #include "lldb/Utility/Log.h" 16bf9a7730SZachary Turner #include "lldb/Utility/StreamString.h" 1730fdc8d8SChris Lattner 1830fdc8d8SChris Lattner using namespace lldb; 1930fdc8d8SChris Lattner using namespace lldb_private; 2030fdc8d8SChris Lattner 2130fdc8d8SChris Lattner // BreakpointResolverFileLine: 22b9c1b51eSKate Stone BreakpointResolverFileLine::BreakpointResolverFileLine( 23*6c17cc53STatyana Krasnukha const BreakpointSP &bkpt, const FileSpec &file_spec, uint32_t line_no, 24431b1584SAdrian Prantl uint32_t column, lldb::addr_t offset, bool check_inlines, 25431b1584SAdrian Prantl bool skip_prologue, bool exact_match) 26b9c1b51eSKate Stone : BreakpointResolver(bkpt, BreakpointResolver::FileLineResolver, offset), 27431b1584SAdrian Prantl m_file_spec(file_spec), m_line_number(line_no), m_column(column), 28431b1584SAdrian Prantl m_inlines(check_inlines), m_skip_prologue(skip_prologue), 29431b1584SAdrian Prantl m_exact_match(exact_match) {} 3030fdc8d8SChris Lattner 31e14dc268SJim Ingham BreakpointResolver *BreakpointResolverFileLine::CreateFromStructuredData( 32*6c17cc53STatyana Krasnukha const BreakpointSP &bkpt, const StructuredData::Dictionary &options_dict, 3397206d57SZachary Turner Status &error) { 342833321fSZachary Turner llvm::StringRef filename; 35e14dc268SJim Ingham uint32_t line_no; 36431b1584SAdrian Prantl uint32_t column; 37e14dc268SJim Ingham bool check_inlines; 38e14dc268SJim Ingham bool skip_prologue; 39e14dc268SJim Ingham bool exact_match; 40e14dc268SJim Ingham bool success; 41e14dc268SJim Ingham 42e14dc268SJim Ingham lldb::addr_t offset = 0; 43e14dc268SJim Ingham 44e14dc268SJim Ingham success = options_dict.GetValueForKeyAsString(GetKey(OptionNames::FileName), 45e14dc268SJim Ingham filename); 46e14dc268SJim Ingham if (!success) { 47e14dc268SJim Ingham error.SetErrorString("BRFL::CFSD: Couldn't find filename entry."); 48e14dc268SJim Ingham return nullptr; 49e14dc268SJim Ingham } 50e14dc268SJim Ingham 51e14dc268SJim Ingham success = options_dict.GetValueForKeyAsInteger( 52e14dc268SJim Ingham GetKey(OptionNames::LineNumber), line_no); 53e14dc268SJim Ingham if (!success) { 54e14dc268SJim Ingham error.SetErrorString("BRFL::CFSD: Couldn't find line number entry."); 55e14dc268SJim Ingham return nullptr; 56e14dc268SJim Ingham } 57e14dc268SJim Ingham 58431b1584SAdrian Prantl success = 59431b1584SAdrian Prantl options_dict.GetValueForKeyAsInteger(GetKey(OptionNames::Column), column); 60431b1584SAdrian Prantl if (!success) { 61431b1584SAdrian Prantl // Backwards compatibility. 62431b1584SAdrian Prantl column = 0; 63431b1584SAdrian Prantl } 64431b1584SAdrian Prantl 65e14dc268SJim Ingham success = options_dict.GetValueForKeyAsBoolean(GetKey(OptionNames::Inlines), 66e14dc268SJim Ingham check_inlines); 67e14dc268SJim Ingham if (!success) { 68e14dc268SJim Ingham error.SetErrorString("BRFL::CFSD: Couldn't find check inlines entry."); 69e14dc268SJim Ingham return nullptr; 70e14dc268SJim Ingham } 71e14dc268SJim Ingham 72e14dc268SJim Ingham success = options_dict.GetValueForKeyAsBoolean( 73e14dc268SJim Ingham GetKey(OptionNames::SkipPrologue), skip_prologue); 74e14dc268SJim Ingham if (!success) { 75e14dc268SJim Ingham error.SetErrorString("BRFL::CFSD: Couldn't find skip prologue entry."); 76e14dc268SJim Ingham return nullptr; 77e14dc268SJim Ingham } 78e14dc268SJim Ingham 79e14dc268SJim Ingham success = options_dict.GetValueForKeyAsBoolean( 80e14dc268SJim Ingham GetKey(OptionNames::ExactMatch), exact_match); 81e14dc268SJim Ingham if (!success) { 82e14dc268SJim Ingham error.SetErrorString("BRFL::CFSD: Couldn't find exact match entry."); 83e14dc268SJim Ingham return nullptr; 84e14dc268SJim Ingham } 85e14dc268SJim Ingham 868f3be7a3SJonas Devlieghere FileSpec file_spec(filename); 87e14dc268SJim Ingham 88431b1584SAdrian Prantl return new BreakpointResolverFileLine(bkpt, file_spec, line_no, column, 89431b1584SAdrian Prantl offset, check_inlines, skip_prologue, 90e14dc268SJim Ingham exact_match); 91e14dc268SJim Ingham } 92e14dc268SJim Ingham 93e14dc268SJim Ingham StructuredData::ObjectSP 94e14dc268SJim Ingham BreakpointResolverFileLine::SerializeToStructuredData() { 95e14dc268SJim Ingham StructuredData::DictionarySP options_dict_sp( 96e14dc268SJim Ingham new StructuredData::Dictionary()); 97e14dc268SJim Ingham 98e14dc268SJim Ingham options_dict_sp->AddStringItem(GetKey(OptionNames::FileName), 99e14dc268SJim Ingham m_file_spec.GetPath()); 100e14dc268SJim Ingham options_dict_sp->AddIntegerItem(GetKey(OptionNames::LineNumber), 101e14dc268SJim Ingham m_line_number); 102431b1584SAdrian Prantl options_dict_sp->AddIntegerItem(GetKey(OptionNames::Column), 103431b1584SAdrian Prantl m_column); 104e14dc268SJim Ingham options_dict_sp->AddBooleanItem(GetKey(OptionNames::Inlines), m_inlines); 105e14dc268SJim Ingham options_dict_sp->AddBooleanItem(GetKey(OptionNames::SkipPrologue), 106e14dc268SJim Ingham m_skip_prologue); 107e14dc268SJim Ingham options_dict_sp->AddBooleanItem(GetKey(OptionNames::ExactMatch), 108e14dc268SJim Ingham m_exact_match); 109e14dc268SJim Ingham 110e14dc268SJim Ingham return WrapOptionsDict(options_dict_sp); 111e14dc268SJim Ingham } 112e14dc268SJim Ingham 113bf37a037SPavel Labath // Filter the symbol context list to remove contexts where the line number was 114bf37a037SPavel Labath // moved into a new function. We do this conservatively, so if e.g. we cannot 11505097246SAdrian Prantl // resolve the function in the context (which can happen in case of line-table- 11605097246SAdrian Prantl // only debug info), we leave the context as is. The trickiest part here is 11705097246SAdrian Prantl // handling inlined functions -- in this case we need to make sure we look at 11805097246SAdrian Prantl // the declaration line of the inlined function, NOT the function it was 119bf37a037SPavel Labath // inlined into. 12052098cf5SGreg Clayton void BreakpointResolverFileLine::FilterContexts(SymbolContextList &sc_list, 12152098cf5SGreg Clayton bool is_relative) { 122bf37a037SPavel Labath if (m_exact_match) 123bf37a037SPavel Labath return; // Nothing to do. Contexts are precise. 124bf37a037SPavel Labath 12552098cf5SGreg Clayton llvm::StringRef relative_path; 12652098cf5SGreg Clayton if (is_relative) 127776cd7adSGreg Clayton relative_path = m_file_spec.GetDirectory().GetStringRef(); 12852098cf5SGreg Clayton 129bf37a037SPavel Labath Log * log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS); 130bf37a037SPavel Labath for(uint32_t i = 0; i < sc_list.GetSize(); ++i) { 131bf37a037SPavel Labath SymbolContext sc; 132bf37a037SPavel Labath sc_list.GetContextAtIndex(i, sc); 13352098cf5SGreg Clayton if (is_relative) { 13452098cf5SGreg Clayton // If the path was relative, make sure any matches match as long as the 13552098cf5SGreg Clayton // relative parts of the path match the path from support files 13652098cf5SGreg Clayton auto sc_dir = sc.line_entry.file.GetDirectory().GetStringRef(); 13752098cf5SGreg Clayton if (!sc_dir.endswith(relative_path)) { 13805097246SAdrian Prantl // We had a relative path specified and the relative directory doesn't 13905097246SAdrian Prantl // match so remove this one 14052098cf5SGreg Clayton LLDB_LOG(log, "removing not matching relative path {0} since it " 14152098cf5SGreg Clayton "doesn't end with {1}", sc_dir, relative_path); 14252098cf5SGreg Clayton sc_list.RemoveContextAtIndex(i); 14352098cf5SGreg Clayton --i; 14452098cf5SGreg Clayton continue; 14552098cf5SGreg Clayton } 14652098cf5SGreg Clayton } 14752098cf5SGreg Clayton 148bf37a037SPavel Labath if (!sc.block) 149bf37a037SPavel Labath continue; 150bf37a037SPavel Labath 151bf37a037SPavel Labath FileSpec file; 152bf37a037SPavel Labath uint32_t line; 153bf37a037SPavel Labath const Block *inline_block = sc.block->GetContainingInlinedBlock(); 154bf37a037SPavel Labath if (inline_block) { 155bf37a037SPavel Labath const Declaration &inline_declaration = inline_block->GetInlinedFunctionInfo()->GetDeclaration(); 156bf37a037SPavel Labath if (!inline_declaration.IsValid()) 157bf37a037SPavel Labath continue; 158bf37a037SPavel Labath file = inline_declaration.GetFile(); 159bf37a037SPavel Labath line = inline_declaration.GetLine(); 160bf37a037SPavel Labath } else if (sc.function) 161bf37a037SPavel Labath sc.function->GetStartLineSourceInfo(file, line); 162bf37a037SPavel Labath else 163bf37a037SPavel Labath continue; 164bf37a037SPavel Labath 165bf37a037SPavel Labath if (file != sc.line_entry.file) { 166bf37a037SPavel Labath LLDB_LOG(log, "unexpected symbol context file {0}", sc.line_entry.file); 167bf37a037SPavel Labath continue; 168bf37a037SPavel Labath } 169bf37a037SPavel Labath 170bf37a037SPavel Labath // Compare the requested line number with the line of the function 171bf37a037SPavel Labath // declaration. In case of a function declared as: 172bf37a037SPavel Labath // 173bf37a037SPavel Labath // int 174bf37a037SPavel Labath // foo() 175bf37a037SPavel Labath // { 176bf37a037SPavel Labath // ... 177bf37a037SPavel Labath // 178bf37a037SPavel Labath // the compiler will set the declaration line to the "foo" line, which is 179bf37a037SPavel Labath // the reason why we have -1 here. This can fail in case of two inline 180bf37a037SPavel Labath // functions defined back-to-back: 181bf37a037SPavel Labath // 182bf37a037SPavel Labath // inline int foo1() { ... } 183bf37a037SPavel Labath // inline int foo2() { ... } 184bf37a037SPavel Labath // 185bf37a037SPavel Labath // but that's the best we can do for now. 1867aa4ed9bSJim Ingham // One complication, if the line number returned from GetStartLineSourceInfo 1877aa4ed9bSJim Ingham // is 0, then we can't do this calculation. That can happen if 1887aa4ed9bSJim Ingham // GetStartLineSourceInfo gets an error, or if the first line number in 1897aa4ed9bSJim Ingham // the function really is 0 - which happens for some languages. 190bf37a037SPavel Labath const int decl_line_is_too_late_fudge = 1; 1917aa4ed9bSJim Ingham if (line && m_line_number < line - decl_line_is_too_late_fudge) { 192bf37a037SPavel Labath LLDB_LOG(log, "removing symbol context at {0}:{1}", file, line); 193bf37a037SPavel Labath sc_list.RemoveContextAtIndex(i); 194bf37a037SPavel Labath --i; 195bf37a037SPavel Labath } 196bf37a037SPavel Labath } 197bf37a037SPavel Labath } 198bf37a037SPavel Labath 19995e264fcSRaphael Isemann Searcher::CallbackReturn BreakpointResolverFileLine::SearchCallback( 20095e264fcSRaphael Isemann SearchFilter &filter, SymbolContext &context, Address *addr) { 20130fdc8d8SChris Lattner SymbolContextList sc_list; 20230fdc8d8SChris Lattner 203b9c1b51eSKate Stone // There is a tricky bit here. You can have two compilation units that 20405097246SAdrian Prantl // #include the same file, and in one of them the function at m_line_number 20505097246SAdrian Prantl // is used (and so code and a line entry for it is generated) but in the 20605097246SAdrian Prantl // other it isn't. If we considered the CU's independently, then in the 20705097246SAdrian Prantl // second inclusion, we'd move the breakpoint to the next function that 20805097246SAdrian Prantl // actually generated code in the header file. That would end up being 20905097246SAdrian Prantl // confusing. So instead, we do the CU iterations by hand here, then scan 21005097246SAdrian Prantl // through the complete list of matches, and figure out the closest line 21105097246SAdrian Prantl // number match, and only set breakpoints on that match. 212bc2f9182SJim Ingham 21305097246SAdrian Prantl // Note also that if file_spec only had a file name and not a directory, 21405097246SAdrian Prantl // there may be many different file spec's in the resultant list. The 21505097246SAdrian Prantl // closest line match for one will not be right for some totally different 21605097246SAdrian Prantl // file. So we go through the match list and pull out the sets that have the 21705097246SAdrian Prantl // same file spec in their line_entry and treat each set separately. 218bc2f9182SJim Ingham 21952098cf5SGreg Clayton FileSpec search_file_spec = m_file_spec; 22052098cf5SGreg Clayton const bool is_relative = m_file_spec.IsRelative(); 22152098cf5SGreg Clayton if (is_relative) 22252098cf5SGreg Clayton search_file_spec.GetDirectory().Clear(); 22352098cf5SGreg Clayton 224c7bece56SGreg Clayton const size_t num_comp_units = context.module_sp->GetNumCompileUnits(); 225b9c1b51eSKate Stone for (size_t i = 0; i < num_comp_units; i++) { 226bc2f9182SJim Ingham CompUnitSP cu_sp(context.module_sp->GetCompileUnitAtIndex(i)); 227b9c1b51eSKate Stone if (cu_sp) { 2282dafd8edSGreg Clayton if (filter.CompUnitPasses(*cu_sp)) 22952098cf5SGreg Clayton cu_sp->ResolveSymbolContext(search_file_spec, m_line_number, m_inlines, 230b9c1b51eSKate Stone m_exact_match, eSymbolContextEverything, 231b9c1b51eSKate Stone sc_list); 232bc2f9182SJim Ingham } 2332dafd8edSGreg Clayton } 234bf37a037SPavel Labath 23552098cf5SGreg Clayton FilterContexts(sc_list, is_relative); 236bf37a037SPavel Labath 23730fdc8d8SChris Lattner StreamString s; 238b9c1b51eSKate Stone s.Printf("for %s:%d ", m_file_spec.GetFilename().AsCString("<Unknown>"), 239969795f1SJim Ingham m_line_number); 240f642373cSJim Ingham 241431b1584SAdrian Prantl SetSCMatchesByLine(filter, sc_list, m_skip_prologue, s.GetString(), 242431b1584SAdrian Prantl m_line_number, m_column); 243bc2f9182SJim Ingham 24430fdc8d8SChris Lattner return Searcher::eCallbackReturnContinue; 24530fdc8d8SChris Lattner } 24630fdc8d8SChris Lattner 2474911d36aSJim Ingham lldb::SearchDepth BreakpointResolverFileLine::GetDepth() { 2484911d36aSJim Ingham return lldb::eSearchDepthModule; 24930fdc8d8SChris Lattner } 25030fdc8d8SChris Lattner 251b9c1b51eSKate Stone void BreakpointResolverFileLine::GetDescription(Stream *s) { 252431b1584SAdrian Prantl s->Printf("file = '%s', line = %u, ", m_file_spec.GetPath().c_str(), 253431b1584SAdrian Prantl m_line_number); 254431b1584SAdrian Prantl if (m_column) 255431b1584SAdrian Prantl s->Printf("column = %u, ", m_column); 256431b1584SAdrian Prantl s->Printf("exact_match = %d", m_exact_match); 25730fdc8d8SChris Lattner } 25830fdc8d8SChris Lattner 259b9c1b51eSKate Stone void BreakpointResolverFileLine::Dump(Stream *s) const {} 26030fdc8d8SChris Lattner 26133df7cd3SJim Ingham lldb::BreakpointResolverSP 262*6c17cc53STatyana Krasnukha BreakpointResolverFileLine::CopyForBreakpoint(BreakpointSP &breakpoint) { 263b9c1b51eSKate Stone lldb::BreakpointResolverSP ret_sp(new BreakpointResolverFileLine( 264*6c17cc53STatyana Krasnukha breakpoint, m_file_spec, m_line_number, m_column, GetOffset(), m_inlines, 265b9c1b51eSKate Stone m_skip_prologue, m_exact_match)); 26633df7cd3SJim Ingham 26733df7cd3SJim Ingham return ret_sp; 26833df7cd3SJim Ingham } 269