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" 15c34698a8SPavel Labath #include "lldb/Utility/LLDBLog.h" 166f9e6901SZachary Turner #include "lldb/Utility/Log.h" 17bf9a7730SZachary Turner #include "lldb/Utility/StreamString.h" 1830fdc8d8SChris Lattner 1930fdc8d8SChris Lattner using namespace lldb; 2030fdc8d8SChris Lattner using namespace lldb_private; 2130fdc8d8SChris Lattner 2230fdc8d8SChris Lattner // BreakpointResolverFileLine: 23b9c1b51eSKate Stone BreakpointResolverFileLine::BreakpointResolverFileLine( 243e2ed744SMed Ismail Bennani const BreakpointSP &bkpt, lldb::addr_t offset, bool skip_prologue, 253e2ed744SMed Ismail Bennani const SourceLocationSpec &location_spec) 26b9c1b51eSKate Stone : BreakpointResolver(bkpt, BreakpointResolver::FileLineResolver, offset), 273e2ed744SMed Ismail Bennani m_location_spec(location_spec), m_skip_prologue(skip_prologue) {} 2830fdc8d8SChris Lattner 29e14dc268SJim Ingham BreakpointResolver *BreakpointResolverFileLine::CreateFromStructuredData( 306c17cc53STatyana Krasnukha const BreakpointSP &bkpt, const StructuredData::Dictionary &options_dict, 3197206d57SZachary Turner Status &error) { 322833321fSZachary Turner llvm::StringRef filename; 333e2ed744SMed Ismail Bennani uint32_t line; 343e2ed744SMed Ismail Bennani uint16_t column; 35e14dc268SJim Ingham bool check_inlines; 36e14dc268SJim Ingham bool skip_prologue; 37e14dc268SJim Ingham bool exact_match; 38e14dc268SJim Ingham bool success; 39e14dc268SJim Ingham 40e14dc268SJim Ingham lldb::addr_t offset = 0; 41e14dc268SJim Ingham 42e14dc268SJim Ingham success = options_dict.GetValueForKeyAsString(GetKey(OptionNames::FileName), 43e14dc268SJim Ingham filename); 44e14dc268SJim Ingham if (!success) { 45e14dc268SJim Ingham error.SetErrorString("BRFL::CFSD: Couldn't find filename entry."); 46e14dc268SJim Ingham return nullptr; 47e14dc268SJim Ingham } 48e14dc268SJim Ingham 49e14dc268SJim Ingham success = options_dict.GetValueForKeyAsInteger( 503e2ed744SMed Ismail Bennani GetKey(OptionNames::LineNumber), line); 51e14dc268SJim Ingham if (!success) { 52e14dc268SJim Ingham error.SetErrorString("BRFL::CFSD: Couldn't find line number entry."); 53e14dc268SJim Ingham return nullptr; 54e14dc268SJim Ingham } 55e14dc268SJim Ingham 56431b1584SAdrian Prantl success = 57431b1584SAdrian Prantl options_dict.GetValueForKeyAsInteger(GetKey(OptionNames::Column), column); 58431b1584SAdrian Prantl if (!success) { 59431b1584SAdrian Prantl // Backwards compatibility. 60431b1584SAdrian Prantl column = 0; 61431b1584SAdrian Prantl } 62431b1584SAdrian Prantl 63e14dc268SJim Ingham success = options_dict.GetValueForKeyAsBoolean(GetKey(OptionNames::Inlines), 64e14dc268SJim Ingham check_inlines); 65e14dc268SJim Ingham if (!success) { 66e14dc268SJim Ingham error.SetErrorString("BRFL::CFSD: Couldn't find check inlines entry."); 67e14dc268SJim Ingham return nullptr; 68e14dc268SJim Ingham } 69e14dc268SJim Ingham 70e14dc268SJim Ingham success = options_dict.GetValueForKeyAsBoolean( 71e14dc268SJim Ingham GetKey(OptionNames::SkipPrologue), skip_prologue); 72e14dc268SJim Ingham if (!success) { 73e14dc268SJim Ingham error.SetErrorString("BRFL::CFSD: Couldn't find skip prologue entry."); 74e14dc268SJim Ingham return nullptr; 75e14dc268SJim Ingham } 76e14dc268SJim Ingham 77e14dc268SJim Ingham success = options_dict.GetValueForKeyAsBoolean( 78e14dc268SJim Ingham GetKey(OptionNames::ExactMatch), exact_match); 79e14dc268SJim Ingham if (!success) { 80e14dc268SJim Ingham error.SetErrorString("BRFL::CFSD: Couldn't find exact match entry."); 81e14dc268SJim Ingham return nullptr; 82e14dc268SJim Ingham } 83e14dc268SJim Ingham 843e2ed744SMed Ismail Bennani SourceLocationSpec location_spec(FileSpec(filename), line, column, 853e2ed744SMed Ismail Bennani check_inlines, exact_match); 863e2ed744SMed Ismail Bennani if (!location_spec) 873e2ed744SMed Ismail Bennani return nullptr; 88e14dc268SJim Ingham 893e2ed744SMed Ismail Bennani return new BreakpointResolverFileLine(bkpt, offset, skip_prologue, 903e2ed744SMed Ismail Bennani location_spec); 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->AddBooleanItem(GetKey(OptionNames::SkipPrologue), 99e14dc268SJim Ingham m_skip_prologue); 1003e2ed744SMed Ismail Bennani options_dict_sp->AddStringItem(GetKey(OptionNames::FileName), 1013e2ed744SMed Ismail Bennani m_location_spec.GetFileSpec().GetPath()); 1023e2ed744SMed Ismail Bennani options_dict_sp->AddIntegerItem(GetKey(OptionNames::LineNumber), 103*aa88161bSKazu Hirata m_location_spec.GetLine().value_or(0)); 1043e2ed744SMed Ismail Bennani options_dict_sp->AddIntegerItem( 1053e2ed744SMed Ismail Bennani GetKey(OptionNames::Column), 106*aa88161bSKazu Hirata m_location_spec.GetColumn().value_or(LLDB_INVALID_COLUMN_NUMBER)); 1073e2ed744SMed Ismail Bennani options_dict_sp->AddBooleanItem(GetKey(OptionNames::Inlines), 1083e2ed744SMed Ismail Bennani m_location_spec.GetCheckInlines()); 109e14dc268SJim Ingham options_dict_sp->AddBooleanItem(GetKey(OptionNames::ExactMatch), 1103e2ed744SMed Ismail Bennani m_location_spec.GetExactMatch()); 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) { 1243e2ed744SMed Ismail Bennani if (m_location_spec.GetExactMatch()) 125bf37a037SPavel Labath return; // Nothing to do. Contexts are precise. 126bf37a037SPavel Labath 12752098cf5SGreg Clayton llvm::StringRef relative_path; 12852098cf5SGreg Clayton if (is_relative) 1293e2ed744SMed Ismail Bennani relative_path = m_location_spec.GetFileSpec().GetDirectory().GetStringRef(); 13052098cf5SGreg Clayton 131a007a6d8SPavel Labath Log *log = GetLog(LLDBLog::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. 192bff38912SJim Ingham 193bff38912SJim Ingham // But only do this calculation if the line number we found in the SC 194bff38912SJim Ingham // was different from the one requested in the source file. If we actually 195bff38912SJim Ingham // found an exact match it must be valid. 196bff38912SJim Ingham 1973e2ed744SMed Ismail Bennani if (m_location_spec.GetLine() == sc.line_entry.line) 198bff38912SJim Ingham continue; 199bff38912SJim Ingham 200bf37a037SPavel Labath const int decl_line_is_too_late_fudge = 1; 2013e2ed744SMed Ismail Bennani if (line && 2023e2ed744SMed Ismail Bennani m_location_spec.GetLine() < line - decl_line_is_too_late_fudge) { 203bf37a037SPavel Labath LLDB_LOG(log, "removing symbol context at {0}:{1}", file, line); 204bf37a037SPavel Labath sc_list.RemoveContextAtIndex(i); 205bf37a037SPavel Labath --i; 206bf37a037SPavel Labath } 207bf37a037SPavel Labath } 208bf37a037SPavel Labath } 209bf37a037SPavel Labath 21095e264fcSRaphael Isemann Searcher::CallbackReturn BreakpointResolverFileLine::SearchCallback( 21195e264fcSRaphael Isemann SearchFilter &filter, SymbolContext &context, Address *addr) { 21230fdc8d8SChris Lattner SymbolContextList sc_list; 21330fdc8d8SChris Lattner 214b9c1b51eSKate Stone // There is a tricky bit here. You can have two compilation units that 21505097246SAdrian Prantl // #include the same file, and in one of them the function at m_line_number 21605097246SAdrian Prantl // is used (and so code and a line entry for it is generated) but in the 21705097246SAdrian Prantl // other it isn't. If we considered the CU's independently, then in the 21805097246SAdrian Prantl // second inclusion, we'd move the breakpoint to the next function that 21905097246SAdrian Prantl // actually generated code in the header file. That would end up being 22005097246SAdrian Prantl // confusing. So instead, we do the CU iterations by hand here, then scan 22105097246SAdrian Prantl // through the complete list of matches, and figure out the closest line 22205097246SAdrian Prantl // number match, and only set breakpoints on that match. 223bc2f9182SJim Ingham 22405097246SAdrian Prantl // Note also that if file_spec only had a file name and not a directory, 22505097246SAdrian Prantl // there may be many different file spec's in the resultant list. The 22605097246SAdrian Prantl // closest line match for one will not be right for some totally different 22705097246SAdrian Prantl // file. So we go through the match list and pull out the sets that have the 22805097246SAdrian Prantl // same file spec in their line_entry and treat each set separately. 229bc2f9182SJim Ingham 230*aa88161bSKazu Hirata const uint32_t line = m_location_spec.GetLine().value_or(0); 2313e2ed744SMed Ismail Bennani const llvm::Optional<uint16_t> column = m_location_spec.GetColumn(); 2323e2ed744SMed Ismail Bennani 2330a68443bSWalter Erquinigo // We'll create a new SourceLocationSpec that can take into account the 2340a68443bSWalter Erquinigo // relative path case, and we'll use it to resolve the symbol context 2350a68443bSWalter Erquinigo // of the CUs. 2363e2ed744SMed Ismail Bennani FileSpec search_file_spec = m_location_spec.GetFileSpec(); 2373e2ed744SMed Ismail Bennani const bool is_relative = search_file_spec.IsRelative(); 23852098cf5SGreg Clayton if (is_relative) 23952098cf5SGreg Clayton search_file_spec.GetDirectory().Clear(); 2400a68443bSWalter Erquinigo SourceLocationSpec search_location_spec( 241*aa88161bSKazu Hirata search_file_spec, m_location_spec.GetLine().value_or(0), 2420a68443bSWalter Erquinigo m_location_spec.GetColumn(), m_location_spec.GetCheckInlines(), 2430a68443bSWalter Erquinigo m_location_spec.GetExactMatch()); 24452098cf5SGreg Clayton 245c7bece56SGreg Clayton const size_t num_comp_units = context.module_sp->GetNumCompileUnits(); 246b9c1b51eSKate Stone for (size_t i = 0; i < num_comp_units; i++) { 247bc2f9182SJim Ingham CompUnitSP cu_sp(context.module_sp->GetCompileUnitAtIndex(i)); 248b9c1b51eSKate Stone if (cu_sp) { 2492dafd8edSGreg Clayton if (filter.CompUnitPasses(*cu_sp)) 2500a68443bSWalter Erquinigo cu_sp->ResolveSymbolContext(search_location_spec, 2510a68443bSWalter Erquinigo eSymbolContextEverything, sc_list); 252bc2f9182SJim Ingham } 2532dafd8edSGreg Clayton } 254bf37a037SPavel Labath 25552098cf5SGreg Clayton FilterContexts(sc_list, is_relative); 256bf37a037SPavel Labath 25730fdc8d8SChris Lattner StreamString s; 2583e2ed744SMed Ismail Bennani s.Printf("for %s:%d ", 2593e2ed744SMed Ismail Bennani m_location_spec.GetFileSpec().GetFilename().AsCString("<Unknown>"), 2603e2ed744SMed Ismail Bennani line); 261f642373cSJim Ingham 2623e2ed744SMed Ismail Bennani SetSCMatchesByLine(filter, sc_list, m_skip_prologue, s.GetString(), line, 2633e2ed744SMed Ismail Bennani column); 264bc2f9182SJim Ingham 26530fdc8d8SChris Lattner return Searcher::eCallbackReturnContinue; 26630fdc8d8SChris Lattner } 26730fdc8d8SChris Lattner 2684911d36aSJim Ingham lldb::SearchDepth BreakpointResolverFileLine::GetDepth() { 2694911d36aSJim Ingham return lldb::eSearchDepthModule; 27030fdc8d8SChris Lattner } 27130fdc8d8SChris Lattner 272b9c1b51eSKate Stone void BreakpointResolverFileLine::GetDescription(Stream *s) { 2733e2ed744SMed Ismail Bennani s->Printf("file = '%s', line = %u, ", 2743e2ed744SMed Ismail Bennani m_location_spec.GetFileSpec().GetPath().c_str(), 275*aa88161bSKazu Hirata m_location_spec.GetLine().value_or(0)); 2763e2ed744SMed Ismail Bennani auto column = m_location_spec.GetColumn(); 2773e2ed744SMed Ismail Bennani if (column) 2783e2ed744SMed Ismail Bennani s->Printf("column = %u, ", *column); 2793e2ed744SMed Ismail Bennani s->Printf("exact_match = %d", m_location_spec.GetExactMatch()); 28030fdc8d8SChris Lattner } 28130fdc8d8SChris Lattner 282b9c1b51eSKate Stone void BreakpointResolverFileLine::Dump(Stream *s) const {} 28330fdc8d8SChris Lattner 28433df7cd3SJim Ingham lldb::BreakpointResolverSP 2856c17cc53STatyana Krasnukha BreakpointResolverFileLine::CopyForBreakpoint(BreakpointSP &breakpoint) { 286b9c1b51eSKate Stone lldb::BreakpointResolverSP ret_sp(new BreakpointResolverFileLine( 2873e2ed744SMed Ismail Bennani breakpoint, GetOffset(), m_skip_prologue, m_location_spec)); 28833df7cd3SJim Ingham 28933df7cd3SJim Ingham return ret_sp; 29033df7cd3SJim Ingham } 291