130fdc8d8SChris Lattner //===-- BreakpointResolverFileLine.cpp --------------------------*- C++ -*-===// 230fdc8d8SChris Lattner // 3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 5*2946cd70SChandler 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 //---------------------------------------------------------------------- 2230fdc8d8SChris Lattner // BreakpointResolverFileLine: 2330fdc8d8SChris Lattner //---------------------------------------------------------------------- 24b9c1b51eSKate Stone BreakpointResolverFileLine::BreakpointResolverFileLine( 25b9c1b51eSKate Stone Breakpoint *bkpt, const FileSpec &file_spec, uint32_t line_no, 26431b1584SAdrian Prantl uint32_t column, lldb::addr_t offset, bool check_inlines, 27431b1584SAdrian Prantl bool skip_prologue, bool exact_match) 28b9c1b51eSKate Stone : BreakpointResolver(bkpt, BreakpointResolver::FileLineResolver, offset), 29431b1584SAdrian Prantl m_file_spec(file_spec), m_line_number(line_no), m_column(column), 30431b1584SAdrian Prantl m_inlines(check_inlines), m_skip_prologue(skip_prologue), 31431b1584SAdrian Prantl m_exact_match(exact_match) {} 3230fdc8d8SChris Lattner 33b9c1b51eSKate Stone BreakpointResolverFileLine::~BreakpointResolverFileLine() {} 3430fdc8d8SChris Lattner 35e14dc268SJim Ingham BreakpointResolver *BreakpointResolverFileLine::CreateFromStructuredData( 361a81b273SJim Ingham Breakpoint *bkpt, const StructuredData::Dictionary &options_dict, 3797206d57SZachary Turner Status &error) { 382833321fSZachary Turner llvm::StringRef filename; 39e14dc268SJim Ingham uint32_t line_no; 40431b1584SAdrian Prantl uint32_t column; 41e14dc268SJim Ingham bool check_inlines; 42e14dc268SJim Ingham bool skip_prologue; 43e14dc268SJim Ingham bool exact_match; 44e14dc268SJim Ingham bool success; 45e14dc268SJim Ingham 46e14dc268SJim Ingham lldb::addr_t offset = 0; 47e14dc268SJim Ingham 48e14dc268SJim Ingham success = options_dict.GetValueForKeyAsString(GetKey(OptionNames::FileName), 49e14dc268SJim Ingham filename); 50e14dc268SJim Ingham if (!success) { 51e14dc268SJim Ingham error.SetErrorString("BRFL::CFSD: Couldn't find filename entry."); 52e14dc268SJim Ingham return nullptr; 53e14dc268SJim Ingham } 54e14dc268SJim Ingham 55e14dc268SJim Ingham success = options_dict.GetValueForKeyAsInteger( 56e14dc268SJim Ingham GetKey(OptionNames::LineNumber), line_no); 57e14dc268SJim Ingham if (!success) { 58e14dc268SJim Ingham error.SetErrorString("BRFL::CFSD: Couldn't find line number entry."); 59e14dc268SJim Ingham return nullptr; 60e14dc268SJim Ingham } 61e14dc268SJim Ingham 62431b1584SAdrian Prantl success = 63431b1584SAdrian Prantl options_dict.GetValueForKeyAsInteger(GetKey(OptionNames::Column), column); 64431b1584SAdrian Prantl if (!success) { 65431b1584SAdrian Prantl // Backwards compatibility. 66431b1584SAdrian Prantl column = 0; 67431b1584SAdrian Prantl } 68431b1584SAdrian Prantl 69e14dc268SJim Ingham success = options_dict.GetValueForKeyAsBoolean(GetKey(OptionNames::Inlines), 70e14dc268SJim Ingham check_inlines); 71e14dc268SJim Ingham if (!success) { 72e14dc268SJim Ingham error.SetErrorString("BRFL::CFSD: Couldn't find check inlines entry."); 73e14dc268SJim Ingham return nullptr; 74e14dc268SJim Ingham } 75e14dc268SJim Ingham 76e14dc268SJim Ingham success = options_dict.GetValueForKeyAsBoolean( 77e14dc268SJim Ingham GetKey(OptionNames::SkipPrologue), skip_prologue); 78e14dc268SJim Ingham if (!success) { 79e14dc268SJim Ingham error.SetErrorString("BRFL::CFSD: Couldn't find skip prologue entry."); 80e14dc268SJim Ingham return nullptr; 81e14dc268SJim Ingham } 82e14dc268SJim Ingham 83e14dc268SJim Ingham success = options_dict.GetValueForKeyAsBoolean( 84e14dc268SJim Ingham GetKey(OptionNames::ExactMatch), exact_match); 85e14dc268SJim Ingham if (!success) { 86e14dc268SJim Ingham error.SetErrorString("BRFL::CFSD: Couldn't find exact match entry."); 87e14dc268SJim Ingham return nullptr; 88e14dc268SJim Ingham } 89e14dc268SJim Ingham 908f3be7a3SJonas Devlieghere FileSpec file_spec(filename); 91e14dc268SJim Ingham 92431b1584SAdrian Prantl return new BreakpointResolverFileLine(bkpt, file_spec, line_no, column, 93431b1584SAdrian Prantl offset, check_inlines, skip_prologue, 94e14dc268SJim Ingham exact_match); 95e14dc268SJim Ingham } 96e14dc268SJim Ingham 97e14dc268SJim Ingham StructuredData::ObjectSP 98e14dc268SJim Ingham BreakpointResolverFileLine::SerializeToStructuredData() { 99e14dc268SJim Ingham StructuredData::DictionarySP options_dict_sp( 100e14dc268SJim Ingham new StructuredData::Dictionary()); 101e14dc268SJim Ingham 102e14dc268SJim Ingham options_dict_sp->AddStringItem(GetKey(OptionNames::FileName), 103e14dc268SJim Ingham m_file_spec.GetPath()); 104e14dc268SJim Ingham options_dict_sp->AddIntegerItem(GetKey(OptionNames::LineNumber), 105e14dc268SJim Ingham m_line_number); 106431b1584SAdrian Prantl options_dict_sp->AddIntegerItem(GetKey(OptionNames::Column), 107431b1584SAdrian Prantl m_column); 108e14dc268SJim Ingham options_dict_sp->AddBooleanItem(GetKey(OptionNames::Inlines), m_inlines); 109e14dc268SJim Ingham options_dict_sp->AddBooleanItem(GetKey(OptionNames::SkipPrologue), 110e14dc268SJim Ingham m_skip_prologue); 111e14dc268SJim Ingham options_dict_sp->AddBooleanItem(GetKey(OptionNames::ExactMatch), 112e14dc268SJim Ingham m_exact_match); 113e14dc268SJim Ingham 114e14dc268SJim Ingham return WrapOptionsDict(options_dict_sp); 115e14dc268SJim Ingham } 116e14dc268SJim Ingham 117bf37a037SPavel Labath // Filter the symbol context list to remove contexts where the line number was 118bf37a037SPavel Labath // moved into a new function. We do this conservatively, so if e.g. we cannot 11905097246SAdrian Prantl // resolve the function in the context (which can happen in case of line-table- 12005097246SAdrian Prantl // only debug info), we leave the context as is. The trickiest part here is 12105097246SAdrian Prantl // handling inlined functions -- in this case we need to make sure we look at 12205097246SAdrian Prantl // the declaration line of the inlined function, NOT the function it was 123bf37a037SPavel Labath // inlined into. 12452098cf5SGreg Clayton void BreakpointResolverFileLine::FilterContexts(SymbolContextList &sc_list, 12552098cf5SGreg Clayton bool is_relative) { 126bf37a037SPavel Labath if (m_exact_match) 127bf37a037SPavel Labath return; // Nothing to do. Contexts are precise. 128bf37a037SPavel Labath 12952098cf5SGreg Clayton llvm::StringRef relative_path; 13052098cf5SGreg Clayton if (is_relative) 131776cd7adSGreg Clayton relative_path = m_file_spec.GetDirectory().GetStringRef(); 13252098cf5SGreg Clayton 133bf37a037SPavel Labath Log * log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS); 134bf37a037SPavel Labath for(uint32_t i = 0; i < sc_list.GetSize(); ++i) { 135bf37a037SPavel Labath SymbolContext sc; 136bf37a037SPavel Labath sc_list.GetContextAtIndex(i, sc); 13752098cf5SGreg Clayton if (is_relative) { 13852098cf5SGreg Clayton // If the path was relative, make sure any matches match as long as the 13952098cf5SGreg Clayton // relative parts of the path match the path from support files 14052098cf5SGreg Clayton auto sc_dir = sc.line_entry.file.GetDirectory().GetStringRef(); 14152098cf5SGreg Clayton if (!sc_dir.endswith(relative_path)) { 14205097246SAdrian Prantl // We had a relative path specified and the relative directory doesn't 14305097246SAdrian Prantl // match so remove this one 14452098cf5SGreg Clayton LLDB_LOG(log, "removing not matching relative path {0} since it " 14552098cf5SGreg Clayton "doesn't end with {1}", sc_dir, relative_path); 14652098cf5SGreg Clayton sc_list.RemoveContextAtIndex(i); 14752098cf5SGreg Clayton --i; 14852098cf5SGreg Clayton continue; 14952098cf5SGreg Clayton } 15052098cf5SGreg Clayton } 15152098cf5SGreg Clayton 152bf37a037SPavel Labath if (!sc.block) 153bf37a037SPavel Labath continue; 154bf37a037SPavel Labath 155bf37a037SPavel Labath FileSpec file; 156bf37a037SPavel Labath uint32_t line; 157bf37a037SPavel Labath const Block *inline_block = sc.block->GetContainingInlinedBlock(); 158bf37a037SPavel Labath if (inline_block) { 159bf37a037SPavel Labath const Declaration &inline_declaration = inline_block->GetInlinedFunctionInfo()->GetDeclaration(); 160bf37a037SPavel Labath if (!inline_declaration.IsValid()) 161bf37a037SPavel Labath continue; 162bf37a037SPavel Labath file = inline_declaration.GetFile(); 163bf37a037SPavel Labath line = inline_declaration.GetLine(); 164bf37a037SPavel Labath } else if (sc.function) 165bf37a037SPavel Labath sc.function->GetStartLineSourceInfo(file, line); 166bf37a037SPavel Labath else 167bf37a037SPavel Labath continue; 168bf37a037SPavel Labath 169bf37a037SPavel Labath if (file != sc.line_entry.file) { 170bf37a037SPavel Labath LLDB_LOG(log, "unexpected symbol context file {0}", sc.line_entry.file); 171bf37a037SPavel Labath continue; 172bf37a037SPavel Labath } 173bf37a037SPavel Labath 174bf37a037SPavel Labath // Compare the requested line number with the line of the function 175bf37a037SPavel Labath // declaration. In case of a function declared as: 176bf37a037SPavel Labath // 177bf37a037SPavel Labath // int 178bf37a037SPavel Labath // foo() 179bf37a037SPavel Labath // { 180bf37a037SPavel Labath // ... 181bf37a037SPavel Labath // 182bf37a037SPavel Labath // the compiler will set the declaration line to the "foo" line, which is 183bf37a037SPavel Labath // the reason why we have -1 here. This can fail in case of two inline 184bf37a037SPavel Labath // functions defined back-to-back: 185bf37a037SPavel Labath // 186bf37a037SPavel Labath // inline int foo1() { ... } 187bf37a037SPavel Labath // inline int foo2() { ... } 188bf37a037SPavel Labath // 189bf37a037SPavel Labath // but that's the best we can do for now. 1907aa4ed9bSJim Ingham // One complication, if the line number returned from GetStartLineSourceInfo 1917aa4ed9bSJim Ingham // is 0, then we can't do this calculation. That can happen if 1927aa4ed9bSJim Ingham // GetStartLineSourceInfo gets an error, or if the first line number in 1937aa4ed9bSJim Ingham // the function really is 0 - which happens for some languages. 194bf37a037SPavel Labath const int decl_line_is_too_late_fudge = 1; 1957aa4ed9bSJim Ingham if (line && m_line_number < line - decl_line_is_too_late_fudge) { 196bf37a037SPavel Labath LLDB_LOG(log, "removing symbol context at {0}:{1}", file, line); 197bf37a037SPavel Labath sc_list.RemoveContextAtIndex(i); 198bf37a037SPavel Labath --i; 199bf37a037SPavel Labath } 200bf37a037SPavel Labath } 201bf37a037SPavel Labath } 202bf37a037SPavel Labath 20330fdc8d8SChris Lattner Searcher::CallbackReturn 204b9c1b51eSKate Stone BreakpointResolverFileLine::SearchCallback(SearchFilter &filter, 20530fdc8d8SChris Lattner SymbolContext &context, 206b9c1b51eSKate Stone Address *addr, bool containing) { 20730fdc8d8SChris Lattner SymbolContextList sc_list; 20830fdc8d8SChris Lattner 20930fdc8d8SChris Lattner assert(m_breakpoint != NULL); 21030fdc8d8SChris Lattner 211b9c1b51eSKate Stone // There is a tricky bit here. You can have two compilation units that 21205097246SAdrian Prantl // #include the same file, and in one of them the function at m_line_number 21305097246SAdrian Prantl // is used (and so code and a line entry for it is generated) but in the 21405097246SAdrian Prantl // other it isn't. If we considered the CU's independently, then in the 21505097246SAdrian Prantl // second inclusion, we'd move the breakpoint to the next function that 21605097246SAdrian Prantl // actually generated code in the header file. That would end up being 21705097246SAdrian Prantl // confusing. So instead, we do the CU iterations by hand here, then scan 21805097246SAdrian Prantl // through the complete list of matches, and figure out the closest line 21905097246SAdrian Prantl // number match, and only set breakpoints on that match. 220bc2f9182SJim Ingham 22105097246SAdrian Prantl // Note also that if file_spec only had a file name and not a directory, 22205097246SAdrian Prantl // there may be many different file spec's in the resultant list. The 22305097246SAdrian Prantl // closest line match for one will not be right for some totally different 22405097246SAdrian Prantl // file. So we go through the match list and pull out the sets that have the 22505097246SAdrian Prantl // same file spec in their line_entry and treat each set separately. 226bc2f9182SJim Ingham 22752098cf5SGreg Clayton FileSpec search_file_spec = m_file_spec; 22852098cf5SGreg Clayton const bool is_relative = m_file_spec.IsRelative(); 22952098cf5SGreg Clayton if (is_relative) 23052098cf5SGreg Clayton search_file_spec.GetDirectory().Clear(); 23152098cf5SGreg Clayton 232c7bece56SGreg Clayton const size_t num_comp_units = context.module_sp->GetNumCompileUnits(); 233b9c1b51eSKate Stone for (size_t i = 0; i < num_comp_units; i++) { 234bc2f9182SJim Ingham CompUnitSP cu_sp(context.module_sp->GetCompileUnitAtIndex(i)); 235b9c1b51eSKate Stone if (cu_sp) { 2362dafd8edSGreg Clayton if (filter.CompUnitPasses(*cu_sp)) 23752098cf5SGreg Clayton cu_sp->ResolveSymbolContext(search_file_spec, m_line_number, m_inlines, 238b9c1b51eSKate Stone m_exact_match, eSymbolContextEverything, 239b9c1b51eSKate Stone sc_list); 240bc2f9182SJim Ingham } 2412dafd8edSGreg Clayton } 242bf37a037SPavel Labath 24352098cf5SGreg Clayton FilterContexts(sc_list, is_relative); 244bf37a037SPavel Labath 24530fdc8d8SChris Lattner StreamString s; 246b9c1b51eSKate Stone s.Printf("for %s:%d ", m_file_spec.GetFilename().AsCString("<Unknown>"), 247969795f1SJim Ingham m_line_number); 248f642373cSJim Ingham 249431b1584SAdrian Prantl SetSCMatchesByLine(filter, sc_list, m_skip_prologue, s.GetString(), 250431b1584SAdrian Prantl m_line_number, m_column); 251bc2f9182SJim Ingham 25230fdc8d8SChris Lattner return Searcher::eCallbackReturnContinue; 25330fdc8d8SChris Lattner } 25430fdc8d8SChris Lattner 2554911d36aSJim Ingham lldb::SearchDepth BreakpointResolverFileLine::GetDepth() { 2564911d36aSJim Ingham return lldb::eSearchDepthModule; 25730fdc8d8SChris Lattner } 25830fdc8d8SChris Lattner 259b9c1b51eSKate Stone void BreakpointResolverFileLine::GetDescription(Stream *s) { 260431b1584SAdrian Prantl s->Printf("file = '%s', line = %u, ", m_file_spec.GetPath().c_str(), 261431b1584SAdrian Prantl m_line_number); 262431b1584SAdrian Prantl if (m_column) 263431b1584SAdrian Prantl s->Printf("column = %u, ", m_column); 264431b1584SAdrian Prantl s->Printf("exact_match = %d", m_exact_match); 26530fdc8d8SChris Lattner } 26630fdc8d8SChris Lattner 267b9c1b51eSKate Stone void BreakpointResolverFileLine::Dump(Stream *s) const {} 26830fdc8d8SChris Lattner 26933df7cd3SJim Ingham lldb::BreakpointResolverSP 270b9c1b51eSKate Stone BreakpointResolverFileLine::CopyForBreakpoint(Breakpoint &breakpoint) { 271b9c1b51eSKate Stone lldb::BreakpointResolverSP ret_sp(new BreakpointResolverFileLine( 272431b1584SAdrian Prantl &breakpoint, m_file_spec, m_line_number, m_column, m_offset, m_inlines, 273b9c1b51eSKate Stone m_skip_prologue, m_exact_match)); 27433df7cd3SJim Ingham 27533df7cd3SJim Ingham return ret_sp; 27633df7cd3SJim Ingham } 277