130fdc8d8SChris Lattner //===-- BreakpointResolverFileLine.cpp --------------------------*- C++ -*-===// 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( 23b9c1b51eSKate Stone Breakpoint *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 31b9c1b51eSKate Stone BreakpointResolverFileLine::~BreakpointResolverFileLine() {} 3230fdc8d8SChris Lattner 33e14dc268SJim Ingham BreakpointResolver *BreakpointResolverFileLine::CreateFromStructuredData( 341a81b273SJim Ingham Breakpoint *bkpt, const StructuredData::Dictionary &options_dict, 3597206d57SZachary Turner Status &error) { 362833321fSZachary Turner llvm::StringRef filename; 37e14dc268SJim Ingham uint32_t line_no; 38431b1584SAdrian Prantl uint32_t column; 39e14dc268SJim Ingham bool check_inlines; 40e14dc268SJim Ingham bool skip_prologue; 41e14dc268SJim Ingham bool exact_match; 42e14dc268SJim Ingham bool success; 43e14dc268SJim Ingham 44e14dc268SJim Ingham lldb::addr_t offset = 0; 45e14dc268SJim Ingham 46e14dc268SJim Ingham success = options_dict.GetValueForKeyAsString(GetKey(OptionNames::FileName), 47e14dc268SJim Ingham filename); 48e14dc268SJim Ingham if (!success) { 49e14dc268SJim Ingham error.SetErrorString("BRFL::CFSD: Couldn't find filename entry."); 50e14dc268SJim Ingham return nullptr; 51e14dc268SJim Ingham } 52e14dc268SJim Ingham 53e14dc268SJim Ingham success = options_dict.GetValueForKeyAsInteger( 54e14dc268SJim Ingham GetKey(OptionNames::LineNumber), line_no); 55e14dc268SJim Ingham if (!success) { 56e14dc268SJim Ingham error.SetErrorString("BRFL::CFSD: Couldn't find line number entry."); 57e14dc268SJim Ingham return nullptr; 58e14dc268SJim Ingham } 59e14dc268SJim Ingham 60431b1584SAdrian Prantl success = 61431b1584SAdrian Prantl options_dict.GetValueForKeyAsInteger(GetKey(OptionNames::Column), column); 62431b1584SAdrian Prantl if (!success) { 63431b1584SAdrian Prantl // Backwards compatibility. 64431b1584SAdrian Prantl column = 0; 65431b1584SAdrian Prantl } 66431b1584SAdrian Prantl 67e14dc268SJim Ingham success = options_dict.GetValueForKeyAsBoolean(GetKey(OptionNames::Inlines), 68e14dc268SJim Ingham check_inlines); 69e14dc268SJim Ingham if (!success) { 70e14dc268SJim Ingham error.SetErrorString("BRFL::CFSD: Couldn't find check inlines entry."); 71e14dc268SJim Ingham return nullptr; 72e14dc268SJim Ingham } 73e14dc268SJim Ingham 74e14dc268SJim Ingham success = options_dict.GetValueForKeyAsBoolean( 75e14dc268SJim Ingham GetKey(OptionNames::SkipPrologue), skip_prologue); 76e14dc268SJim Ingham if (!success) { 77e14dc268SJim Ingham error.SetErrorString("BRFL::CFSD: Couldn't find skip prologue entry."); 78e14dc268SJim Ingham return nullptr; 79e14dc268SJim Ingham } 80e14dc268SJim Ingham 81e14dc268SJim Ingham success = options_dict.GetValueForKeyAsBoolean( 82e14dc268SJim Ingham GetKey(OptionNames::ExactMatch), exact_match); 83e14dc268SJim Ingham if (!success) { 84e14dc268SJim Ingham error.SetErrorString("BRFL::CFSD: Couldn't find exact match entry."); 85e14dc268SJim Ingham return nullptr; 86e14dc268SJim Ingham } 87e14dc268SJim Ingham 888f3be7a3SJonas Devlieghere FileSpec file_spec(filename); 89e14dc268SJim Ingham 90431b1584SAdrian Prantl return new BreakpointResolverFileLine(bkpt, file_spec, line_no, column, 91431b1584SAdrian Prantl offset, check_inlines, skip_prologue, 92e14dc268SJim Ingham exact_match); 93e14dc268SJim Ingham } 94e14dc268SJim Ingham 95e14dc268SJim Ingham StructuredData::ObjectSP 96e14dc268SJim Ingham BreakpointResolverFileLine::SerializeToStructuredData() { 97e14dc268SJim Ingham StructuredData::DictionarySP options_dict_sp( 98e14dc268SJim Ingham new StructuredData::Dictionary()); 99e14dc268SJim Ingham 100e14dc268SJim Ingham options_dict_sp->AddStringItem(GetKey(OptionNames::FileName), 101e14dc268SJim Ingham m_file_spec.GetPath()); 102e14dc268SJim Ingham options_dict_sp->AddIntegerItem(GetKey(OptionNames::LineNumber), 103e14dc268SJim Ingham m_line_number); 104431b1584SAdrian Prantl options_dict_sp->AddIntegerItem(GetKey(OptionNames::Column), 105431b1584SAdrian Prantl m_column); 106e14dc268SJim Ingham options_dict_sp->AddBooleanItem(GetKey(OptionNames::Inlines), m_inlines); 107e14dc268SJim Ingham options_dict_sp->AddBooleanItem(GetKey(OptionNames::SkipPrologue), 108e14dc268SJim Ingham m_skip_prologue); 109e14dc268SJim Ingham options_dict_sp->AddBooleanItem(GetKey(OptionNames::ExactMatch), 110e14dc268SJim Ingham m_exact_match); 111e14dc268SJim Ingham 112e14dc268SJim Ingham return WrapOptionsDict(options_dict_sp); 113e14dc268SJim Ingham } 114e14dc268SJim Ingham 115bf37a037SPavel Labath // Filter the symbol context list to remove contexts where the line number was 116bf37a037SPavel Labath // moved into a new function. We do this conservatively, so if e.g. we cannot 11705097246SAdrian Prantl // resolve the function in the context (which can happen in case of line-table- 11805097246SAdrian Prantl // only debug info), we leave the context as is. The trickiest part here is 11905097246SAdrian Prantl // handling inlined functions -- in this case we need to make sure we look at 12005097246SAdrian Prantl // the declaration line of the inlined function, NOT the function it was 121bf37a037SPavel Labath // inlined into. 12252098cf5SGreg Clayton void BreakpointResolverFileLine::FilterContexts(SymbolContextList &sc_list, 12352098cf5SGreg Clayton bool is_relative) { 124bf37a037SPavel Labath if (m_exact_match) 125bf37a037SPavel Labath return; // Nothing to do. Contexts are precise. 126bf37a037SPavel Labath 12752098cf5SGreg Clayton llvm::StringRef relative_path; 12852098cf5SGreg Clayton if (is_relative) 129776cd7adSGreg Clayton relative_path = m_file_spec.GetDirectory().GetStringRef(); 13052098cf5SGreg Clayton 131bf37a037SPavel Labath Log * log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS); 132bf37a037SPavel Labath for(uint32_t i = 0; i < sc_list.GetSize(); ++i) { 133bf37a037SPavel Labath SymbolContext sc; 134bf37a037SPavel Labath sc_list.GetContextAtIndex(i, sc); 13552098cf5SGreg Clayton if (is_relative) { 13652098cf5SGreg Clayton // If the path was relative, make sure any matches match as long as the 13752098cf5SGreg Clayton // relative parts of the path match the path from support files 13852098cf5SGreg Clayton auto sc_dir = sc.line_entry.file.GetDirectory().GetStringRef(); 13952098cf5SGreg Clayton if (!sc_dir.endswith(relative_path)) { 14005097246SAdrian Prantl // We had a relative path specified and the relative directory doesn't 14105097246SAdrian Prantl // match so remove this one 14252098cf5SGreg Clayton LLDB_LOG(log, "removing not matching relative path {0} since it " 14352098cf5SGreg Clayton "doesn't end with {1}", sc_dir, relative_path); 14452098cf5SGreg Clayton sc_list.RemoveContextAtIndex(i); 14552098cf5SGreg Clayton --i; 14652098cf5SGreg Clayton continue; 14752098cf5SGreg Clayton } 14852098cf5SGreg Clayton } 14952098cf5SGreg Clayton 150bf37a037SPavel Labath if (!sc.block) 151bf37a037SPavel Labath continue; 152bf37a037SPavel Labath 153bf37a037SPavel Labath FileSpec file; 154bf37a037SPavel Labath uint32_t line; 155bf37a037SPavel Labath const Block *inline_block = sc.block->GetContainingInlinedBlock(); 156bf37a037SPavel Labath if (inline_block) { 157bf37a037SPavel Labath const Declaration &inline_declaration = inline_block->GetInlinedFunctionInfo()->GetDeclaration(); 158bf37a037SPavel Labath if (!inline_declaration.IsValid()) 159bf37a037SPavel Labath continue; 160bf37a037SPavel Labath file = inline_declaration.GetFile(); 161bf37a037SPavel Labath line = inline_declaration.GetLine(); 162bf37a037SPavel Labath } else if (sc.function) 163bf37a037SPavel Labath sc.function->GetStartLineSourceInfo(file, line); 164bf37a037SPavel Labath else 165bf37a037SPavel Labath continue; 166bf37a037SPavel Labath 167bf37a037SPavel Labath if (file != sc.line_entry.file) { 168bf37a037SPavel Labath LLDB_LOG(log, "unexpected symbol context file {0}", sc.line_entry.file); 169bf37a037SPavel Labath continue; 170bf37a037SPavel Labath } 171bf37a037SPavel Labath 172bf37a037SPavel Labath // Compare the requested line number with the line of the function 173bf37a037SPavel Labath // declaration. In case of a function declared as: 174bf37a037SPavel Labath // 175bf37a037SPavel Labath // int 176bf37a037SPavel Labath // foo() 177bf37a037SPavel Labath // { 178bf37a037SPavel Labath // ... 179bf37a037SPavel Labath // 180bf37a037SPavel Labath // the compiler will set the declaration line to the "foo" line, which is 181bf37a037SPavel Labath // the reason why we have -1 here. This can fail in case of two inline 182bf37a037SPavel Labath // functions defined back-to-back: 183bf37a037SPavel Labath // 184bf37a037SPavel Labath // inline int foo1() { ... } 185bf37a037SPavel Labath // inline int foo2() { ... } 186bf37a037SPavel Labath // 187bf37a037SPavel Labath // but that's the best we can do for now. 1887aa4ed9bSJim Ingham // One complication, if the line number returned from GetStartLineSourceInfo 1897aa4ed9bSJim Ingham // is 0, then we can't do this calculation. That can happen if 1907aa4ed9bSJim Ingham // GetStartLineSourceInfo gets an error, or if the first line number in 1917aa4ed9bSJim Ingham // the function really is 0 - which happens for some languages. 192bf37a037SPavel Labath const int decl_line_is_too_late_fudge = 1; 1937aa4ed9bSJim Ingham if (line && m_line_number < line - decl_line_is_too_late_fudge) { 194bf37a037SPavel Labath LLDB_LOG(log, "removing symbol context at {0}:{1}", file, line); 195bf37a037SPavel Labath sc_list.RemoveContextAtIndex(i); 196bf37a037SPavel Labath --i; 197bf37a037SPavel Labath } 198bf37a037SPavel Labath } 199bf37a037SPavel Labath } 200bf37a037SPavel Labath 201*95e264fcSRaphael Isemann Searcher::CallbackReturn BreakpointResolverFileLine::SearchCallback( 202*95e264fcSRaphael Isemann SearchFilter &filter, SymbolContext &context, Address *addr) { 20330fdc8d8SChris Lattner SymbolContextList sc_list; 20430fdc8d8SChris Lattner 205248a1305SKonrad Kleine assert(m_breakpoint != nullptr); 20630fdc8d8SChris Lattner 207b9c1b51eSKate Stone // There is a tricky bit here. You can have two compilation units that 20805097246SAdrian Prantl // #include the same file, and in one of them the function at m_line_number 20905097246SAdrian Prantl // is used (and so code and a line entry for it is generated) but in the 21005097246SAdrian Prantl // other it isn't. If we considered the CU's independently, then in the 21105097246SAdrian Prantl // second inclusion, we'd move the breakpoint to the next function that 21205097246SAdrian Prantl // actually generated code in the header file. That would end up being 21305097246SAdrian Prantl // confusing. So instead, we do the CU iterations by hand here, then scan 21405097246SAdrian Prantl // through the complete list of matches, and figure out the closest line 21505097246SAdrian Prantl // number match, and only set breakpoints on that match. 216bc2f9182SJim Ingham 21705097246SAdrian Prantl // Note also that if file_spec only had a file name and not a directory, 21805097246SAdrian Prantl // there may be many different file spec's in the resultant list. The 21905097246SAdrian Prantl // closest line match for one will not be right for some totally different 22005097246SAdrian Prantl // file. So we go through the match list and pull out the sets that have the 22105097246SAdrian Prantl // same file spec in their line_entry and treat each set separately. 222bc2f9182SJim Ingham 22352098cf5SGreg Clayton FileSpec search_file_spec = m_file_spec; 22452098cf5SGreg Clayton const bool is_relative = m_file_spec.IsRelative(); 22552098cf5SGreg Clayton if (is_relative) 22652098cf5SGreg Clayton search_file_spec.GetDirectory().Clear(); 22752098cf5SGreg Clayton 228c7bece56SGreg Clayton const size_t num_comp_units = context.module_sp->GetNumCompileUnits(); 229b9c1b51eSKate Stone for (size_t i = 0; i < num_comp_units; i++) { 230bc2f9182SJim Ingham CompUnitSP cu_sp(context.module_sp->GetCompileUnitAtIndex(i)); 231b9c1b51eSKate Stone if (cu_sp) { 2322dafd8edSGreg Clayton if (filter.CompUnitPasses(*cu_sp)) 23352098cf5SGreg Clayton cu_sp->ResolveSymbolContext(search_file_spec, m_line_number, m_inlines, 234b9c1b51eSKate Stone m_exact_match, eSymbolContextEverything, 235b9c1b51eSKate Stone sc_list); 236bc2f9182SJim Ingham } 2372dafd8edSGreg Clayton } 238bf37a037SPavel Labath 23952098cf5SGreg Clayton FilterContexts(sc_list, is_relative); 240bf37a037SPavel Labath 24130fdc8d8SChris Lattner StreamString s; 242b9c1b51eSKate Stone s.Printf("for %s:%d ", m_file_spec.GetFilename().AsCString("<Unknown>"), 243969795f1SJim Ingham m_line_number); 244f642373cSJim Ingham 245431b1584SAdrian Prantl SetSCMatchesByLine(filter, sc_list, m_skip_prologue, s.GetString(), 246431b1584SAdrian Prantl m_line_number, m_column); 247bc2f9182SJim Ingham 24830fdc8d8SChris Lattner return Searcher::eCallbackReturnContinue; 24930fdc8d8SChris Lattner } 25030fdc8d8SChris Lattner 2514911d36aSJim Ingham lldb::SearchDepth BreakpointResolverFileLine::GetDepth() { 2524911d36aSJim Ingham return lldb::eSearchDepthModule; 25330fdc8d8SChris Lattner } 25430fdc8d8SChris Lattner 255b9c1b51eSKate Stone void BreakpointResolverFileLine::GetDescription(Stream *s) { 256431b1584SAdrian Prantl s->Printf("file = '%s', line = %u, ", m_file_spec.GetPath().c_str(), 257431b1584SAdrian Prantl m_line_number); 258431b1584SAdrian Prantl if (m_column) 259431b1584SAdrian Prantl s->Printf("column = %u, ", m_column); 260431b1584SAdrian Prantl s->Printf("exact_match = %d", m_exact_match); 26130fdc8d8SChris Lattner } 26230fdc8d8SChris Lattner 263b9c1b51eSKate Stone void BreakpointResolverFileLine::Dump(Stream *s) const {} 26430fdc8d8SChris Lattner 26533df7cd3SJim Ingham lldb::BreakpointResolverSP 266b9c1b51eSKate Stone BreakpointResolverFileLine::CopyForBreakpoint(Breakpoint &breakpoint) { 267b9c1b51eSKate Stone lldb::BreakpointResolverSP ret_sp(new BreakpointResolverFileLine( 268431b1584SAdrian Prantl &breakpoint, m_file_spec, m_line_number, m_column, m_offset, m_inlines, 269b9c1b51eSKate Stone m_skip_prologue, m_exact_match)); 27033df7cd3SJim Ingham 27133df7cd3SJim Ingham return ret_sp; 27233df7cd3SJim Ingham } 273