130fdc8d8SChris Lattner //===-- BreakpointResolverFileLine.cpp --------------------------*- C++ -*-===// 230fdc8d8SChris Lattner // 330fdc8d8SChris Lattner // The LLVM Compiler Infrastructure 430fdc8d8SChris Lattner // 530fdc8d8SChris Lattner // This file is distributed under the University of Illinois Open Source 630fdc8d8SChris Lattner // License. See LICENSE.TXT for details. 730fdc8d8SChris Lattner // 830fdc8d8SChris Lattner //===----------------------------------------------------------------------===// 930fdc8d8SChris Lattner 1030fdc8d8SChris Lattner #include "lldb/Breakpoint/BreakpointResolverFileLine.h" 1130fdc8d8SChris Lattner 1230fdc8d8SChris Lattner // C Includes 1330fdc8d8SChris Lattner // C++ Includes 1430fdc8d8SChris Lattner // Other libraries and framework includes 1530fdc8d8SChris Lattner // Project includes 1630fdc8d8SChris Lattner #include "lldb/Breakpoint/BreakpointLocation.h" 171f746071SGreg Clayton #include "lldb/Core/Module.h" 181f746071SGreg Clayton #include "lldb/Symbol/CompileUnit.h" 191f746071SGreg Clayton #include "lldb/Symbol/Function.h" 206f9e6901SZachary Turner #include "lldb/Utility/Log.h" 21bf9a7730SZachary Turner #include "lldb/Utility/StreamString.h" 2230fdc8d8SChris Lattner 2330fdc8d8SChris Lattner using namespace lldb; 2430fdc8d8SChris Lattner using namespace lldb_private; 2530fdc8d8SChris Lattner 2630fdc8d8SChris Lattner //---------------------------------------------------------------------- 2730fdc8d8SChris Lattner // BreakpointResolverFileLine: 2830fdc8d8SChris Lattner //---------------------------------------------------------------------- 29b9c1b51eSKate Stone BreakpointResolverFileLine::BreakpointResolverFileLine( 30b9c1b51eSKate Stone Breakpoint *bkpt, const FileSpec &file_spec, uint32_t line_no, 31431b1584SAdrian Prantl uint32_t column, lldb::addr_t offset, bool check_inlines, 32431b1584SAdrian Prantl bool skip_prologue, bool exact_match) 33b9c1b51eSKate Stone : BreakpointResolver(bkpt, BreakpointResolver::FileLineResolver, offset), 34431b1584SAdrian Prantl m_file_spec(file_spec), m_line_number(line_no), m_column(column), 35431b1584SAdrian Prantl m_inlines(check_inlines), m_skip_prologue(skip_prologue), 36431b1584SAdrian Prantl m_exact_match(exact_match) {} 3730fdc8d8SChris Lattner 38b9c1b51eSKate Stone BreakpointResolverFileLine::~BreakpointResolverFileLine() {} 3930fdc8d8SChris Lattner 40e14dc268SJim Ingham BreakpointResolver *BreakpointResolverFileLine::CreateFromStructuredData( 411a81b273SJim Ingham Breakpoint *bkpt, const StructuredData::Dictionary &options_dict, 4297206d57SZachary Turner Status &error) { 432833321fSZachary Turner llvm::StringRef filename; 44e14dc268SJim Ingham uint32_t line_no; 45431b1584SAdrian Prantl uint32_t column; 46e14dc268SJim Ingham bool check_inlines; 47e14dc268SJim Ingham bool skip_prologue; 48e14dc268SJim Ingham bool exact_match; 49e14dc268SJim Ingham bool success; 50e14dc268SJim Ingham 51e14dc268SJim Ingham lldb::addr_t offset = 0; 52e14dc268SJim Ingham 53e14dc268SJim Ingham success = options_dict.GetValueForKeyAsString(GetKey(OptionNames::FileName), 54e14dc268SJim Ingham filename); 55e14dc268SJim Ingham if (!success) { 56e14dc268SJim Ingham error.SetErrorString("BRFL::CFSD: Couldn't find filename entry."); 57e14dc268SJim Ingham return nullptr; 58e14dc268SJim Ingham } 59e14dc268SJim Ingham 60e14dc268SJim Ingham success = options_dict.GetValueForKeyAsInteger( 61e14dc268SJim Ingham GetKey(OptionNames::LineNumber), line_no); 62e14dc268SJim Ingham if (!success) { 63e14dc268SJim Ingham error.SetErrorString("BRFL::CFSD: Couldn't find line number entry."); 64e14dc268SJim Ingham return nullptr; 65e14dc268SJim Ingham } 66e14dc268SJim Ingham 67431b1584SAdrian Prantl success = 68431b1584SAdrian Prantl options_dict.GetValueForKeyAsInteger(GetKey(OptionNames::Column), column); 69431b1584SAdrian Prantl if (!success) { 70431b1584SAdrian Prantl // Backwards compatibility. 71431b1584SAdrian Prantl column = 0; 72431b1584SAdrian Prantl } 73431b1584SAdrian Prantl 74e14dc268SJim Ingham success = options_dict.GetValueForKeyAsBoolean(GetKey(OptionNames::Inlines), 75e14dc268SJim Ingham check_inlines); 76e14dc268SJim Ingham if (!success) { 77e14dc268SJim Ingham error.SetErrorString("BRFL::CFSD: Couldn't find check inlines entry."); 78e14dc268SJim Ingham return nullptr; 79e14dc268SJim Ingham } 80e14dc268SJim Ingham 81e14dc268SJim Ingham success = options_dict.GetValueForKeyAsBoolean( 82e14dc268SJim Ingham GetKey(OptionNames::SkipPrologue), skip_prologue); 83e14dc268SJim Ingham if (!success) { 84e14dc268SJim Ingham error.SetErrorString("BRFL::CFSD: Couldn't find skip prologue entry."); 85e14dc268SJim Ingham return nullptr; 86e14dc268SJim Ingham } 87e14dc268SJim Ingham 88e14dc268SJim Ingham success = options_dict.GetValueForKeyAsBoolean( 89e14dc268SJim Ingham GetKey(OptionNames::ExactMatch), exact_match); 90e14dc268SJim Ingham if (!success) { 91e14dc268SJim Ingham error.SetErrorString("BRFL::CFSD: Couldn't find exact match entry."); 92e14dc268SJim Ingham return nullptr; 93e14dc268SJim Ingham } 94e14dc268SJim Ingham 95*8f3be7a3SJonas Devlieghere FileSpec file_spec(filename); 96e14dc268SJim Ingham 97431b1584SAdrian Prantl return new BreakpointResolverFileLine(bkpt, file_spec, line_no, column, 98431b1584SAdrian Prantl offset, check_inlines, skip_prologue, 99e14dc268SJim Ingham exact_match); 100e14dc268SJim Ingham } 101e14dc268SJim Ingham 102e14dc268SJim Ingham StructuredData::ObjectSP 103e14dc268SJim Ingham BreakpointResolverFileLine::SerializeToStructuredData() { 104e14dc268SJim Ingham StructuredData::DictionarySP options_dict_sp( 105e14dc268SJim Ingham new StructuredData::Dictionary()); 106e14dc268SJim Ingham 107e14dc268SJim Ingham options_dict_sp->AddStringItem(GetKey(OptionNames::FileName), 108e14dc268SJim Ingham m_file_spec.GetPath()); 109e14dc268SJim Ingham options_dict_sp->AddIntegerItem(GetKey(OptionNames::LineNumber), 110e14dc268SJim Ingham m_line_number); 111431b1584SAdrian Prantl options_dict_sp->AddIntegerItem(GetKey(OptionNames::Column), 112431b1584SAdrian Prantl m_column); 113e14dc268SJim Ingham options_dict_sp->AddBooleanItem(GetKey(OptionNames::Inlines), m_inlines); 114e14dc268SJim Ingham options_dict_sp->AddBooleanItem(GetKey(OptionNames::SkipPrologue), 115e14dc268SJim Ingham m_skip_prologue); 116e14dc268SJim Ingham options_dict_sp->AddBooleanItem(GetKey(OptionNames::ExactMatch), 117e14dc268SJim Ingham m_exact_match); 118e14dc268SJim Ingham 119e14dc268SJim Ingham return WrapOptionsDict(options_dict_sp); 120e14dc268SJim Ingham } 121e14dc268SJim Ingham 122bf37a037SPavel Labath // Filter the symbol context list to remove contexts where the line number was 123bf37a037SPavel Labath // moved into a new function. We do this conservatively, so if e.g. we cannot 12405097246SAdrian Prantl // resolve the function in the context (which can happen in case of line-table- 12505097246SAdrian Prantl // only debug info), we leave the context as is. The trickiest part here is 12605097246SAdrian Prantl // handling inlined functions -- in this case we need to make sure we look at 12705097246SAdrian Prantl // the declaration line of the inlined function, NOT the function it was 128bf37a037SPavel Labath // inlined into. 12952098cf5SGreg Clayton void BreakpointResolverFileLine::FilterContexts(SymbolContextList &sc_list, 13052098cf5SGreg Clayton bool is_relative) { 131bf37a037SPavel Labath if (m_exact_match) 132bf37a037SPavel Labath return; // Nothing to do. Contexts are precise. 133bf37a037SPavel Labath 13452098cf5SGreg Clayton llvm::StringRef relative_path; 13552098cf5SGreg Clayton if (is_relative) 136776cd7adSGreg Clayton relative_path = m_file_spec.GetDirectory().GetStringRef(); 13752098cf5SGreg Clayton 138bf37a037SPavel Labath Log * log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS); 139bf37a037SPavel Labath for(uint32_t i = 0; i < sc_list.GetSize(); ++i) { 140bf37a037SPavel Labath SymbolContext sc; 141bf37a037SPavel Labath sc_list.GetContextAtIndex(i, sc); 14252098cf5SGreg Clayton if (is_relative) { 14352098cf5SGreg Clayton // If the path was relative, make sure any matches match as long as the 14452098cf5SGreg Clayton // relative parts of the path match the path from support files 14552098cf5SGreg Clayton auto sc_dir = sc.line_entry.file.GetDirectory().GetStringRef(); 14652098cf5SGreg Clayton if (!sc_dir.endswith(relative_path)) { 14705097246SAdrian Prantl // We had a relative path specified and the relative directory doesn't 14805097246SAdrian Prantl // match so remove this one 14952098cf5SGreg Clayton LLDB_LOG(log, "removing not matching relative path {0} since it " 15052098cf5SGreg Clayton "doesn't end with {1}", sc_dir, relative_path); 15152098cf5SGreg Clayton sc_list.RemoveContextAtIndex(i); 15252098cf5SGreg Clayton --i; 15352098cf5SGreg Clayton continue; 15452098cf5SGreg Clayton } 15552098cf5SGreg Clayton } 15652098cf5SGreg Clayton 157bf37a037SPavel Labath if (!sc.block) 158bf37a037SPavel Labath continue; 159bf37a037SPavel Labath 160bf37a037SPavel Labath FileSpec file; 161bf37a037SPavel Labath uint32_t line; 162bf37a037SPavel Labath const Block *inline_block = sc.block->GetContainingInlinedBlock(); 163bf37a037SPavel Labath if (inline_block) { 164bf37a037SPavel Labath const Declaration &inline_declaration = inline_block->GetInlinedFunctionInfo()->GetDeclaration(); 165bf37a037SPavel Labath if (!inline_declaration.IsValid()) 166bf37a037SPavel Labath continue; 167bf37a037SPavel Labath file = inline_declaration.GetFile(); 168bf37a037SPavel Labath line = inline_declaration.GetLine(); 169bf37a037SPavel Labath } else if (sc.function) 170bf37a037SPavel Labath sc.function->GetStartLineSourceInfo(file, line); 171bf37a037SPavel Labath else 172bf37a037SPavel Labath continue; 173bf37a037SPavel Labath 174bf37a037SPavel Labath if (file != sc.line_entry.file) { 175bf37a037SPavel Labath LLDB_LOG(log, "unexpected symbol context file {0}", sc.line_entry.file); 176bf37a037SPavel Labath continue; 177bf37a037SPavel Labath } 178bf37a037SPavel Labath 179bf37a037SPavel Labath // Compare the requested line number with the line of the function 180bf37a037SPavel Labath // declaration. In case of a function declared as: 181bf37a037SPavel Labath // 182bf37a037SPavel Labath // int 183bf37a037SPavel Labath // foo() 184bf37a037SPavel Labath // { 185bf37a037SPavel Labath // ... 186bf37a037SPavel Labath // 187bf37a037SPavel Labath // the compiler will set the declaration line to the "foo" line, which is 188bf37a037SPavel Labath // the reason why we have -1 here. This can fail in case of two inline 189bf37a037SPavel Labath // functions defined back-to-back: 190bf37a037SPavel Labath // 191bf37a037SPavel Labath // inline int foo1() { ... } 192bf37a037SPavel Labath // inline int foo2() { ... } 193bf37a037SPavel Labath // 194bf37a037SPavel Labath // but that's the best we can do for now. 1957aa4ed9bSJim Ingham // One complication, if the line number returned from GetStartLineSourceInfo 1967aa4ed9bSJim Ingham // is 0, then we can't do this calculation. That can happen if 1977aa4ed9bSJim Ingham // GetStartLineSourceInfo gets an error, or if the first line number in 1987aa4ed9bSJim Ingham // the function really is 0 - which happens for some languages. 199bf37a037SPavel Labath const int decl_line_is_too_late_fudge = 1; 2007aa4ed9bSJim Ingham if (line && m_line_number < line - decl_line_is_too_late_fudge) { 201bf37a037SPavel Labath LLDB_LOG(log, "removing symbol context at {0}:{1}", file, line); 202bf37a037SPavel Labath sc_list.RemoveContextAtIndex(i); 203bf37a037SPavel Labath --i; 204bf37a037SPavel Labath } 205bf37a037SPavel Labath } 206bf37a037SPavel Labath } 207bf37a037SPavel Labath 20830fdc8d8SChris Lattner Searcher::CallbackReturn 209b9c1b51eSKate Stone BreakpointResolverFileLine::SearchCallback(SearchFilter &filter, 21030fdc8d8SChris Lattner SymbolContext &context, 211b9c1b51eSKate Stone Address *addr, bool containing) { 21230fdc8d8SChris Lattner SymbolContextList sc_list; 21330fdc8d8SChris Lattner 21430fdc8d8SChris Lattner assert(m_breakpoint != NULL); 21530fdc8d8SChris Lattner 216b9c1b51eSKate Stone // There is a tricky bit here. You can have two compilation units that 21705097246SAdrian Prantl // #include the same file, and in one of them the function at m_line_number 21805097246SAdrian Prantl // is used (and so code and a line entry for it is generated) but in the 21905097246SAdrian Prantl // other it isn't. If we considered the CU's independently, then in the 22005097246SAdrian Prantl // second inclusion, we'd move the breakpoint to the next function that 22105097246SAdrian Prantl // actually generated code in the header file. That would end up being 22205097246SAdrian Prantl // confusing. So instead, we do the CU iterations by hand here, then scan 22305097246SAdrian Prantl // through the complete list of matches, and figure out the closest line 22405097246SAdrian Prantl // number match, and only set breakpoints on that match. 225bc2f9182SJim Ingham 22605097246SAdrian Prantl // Note also that if file_spec only had a file name and not a directory, 22705097246SAdrian Prantl // there may be many different file spec's in the resultant list. The 22805097246SAdrian Prantl // closest line match for one will not be right for some totally different 22905097246SAdrian Prantl // file. So we go through the match list and pull out the sets that have the 23005097246SAdrian Prantl // same file spec in their line_entry and treat each set separately. 231bc2f9182SJim Ingham 23252098cf5SGreg Clayton FileSpec search_file_spec = m_file_spec; 23352098cf5SGreg Clayton const bool is_relative = m_file_spec.IsRelative(); 23452098cf5SGreg Clayton if (is_relative) 23552098cf5SGreg Clayton search_file_spec.GetDirectory().Clear(); 23652098cf5SGreg Clayton 237c7bece56SGreg Clayton const size_t num_comp_units = context.module_sp->GetNumCompileUnits(); 238b9c1b51eSKate Stone for (size_t i = 0; i < num_comp_units; i++) { 239bc2f9182SJim Ingham CompUnitSP cu_sp(context.module_sp->GetCompileUnitAtIndex(i)); 240b9c1b51eSKate Stone if (cu_sp) { 2412dafd8edSGreg Clayton if (filter.CompUnitPasses(*cu_sp)) 24252098cf5SGreg Clayton cu_sp->ResolveSymbolContext(search_file_spec, m_line_number, m_inlines, 243b9c1b51eSKate Stone m_exact_match, eSymbolContextEverything, 244b9c1b51eSKate Stone sc_list); 245bc2f9182SJim Ingham } 2462dafd8edSGreg Clayton } 247bf37a037SPavel Labath 24852098cf5SGreg Clayton FilterContexts(sc_list, is_relative); 249bf37a037SPavel Labath 25030fdc8d8SChris Lattner StreamString s; 251b9c1b51eSKate Stone s.Printf("for %s:%d ", m_file_spec.GetFilename().AsCString("<Unknown>"), 252969795f1SJim Ingham m_line_number); 253f642373cSJim Ingham 254431b1584SAdrian Prantl SetSCMatchesByLine(filter, sc_list, m_skip_prologue, s.GetString(), 255431b1584SAdrian Prantl m_line_number, m_column); 256bc2f9182SJim Ingham 25730fdc8d8SChris Lattner return Searcher::eCallbackReturnContinue; 25830fdc8d8SChris Lattner } 25930fdc8d8SChris Lattner 2604911d36aSJim Ingham lldb::SearchDepth BreakpointResolverFileLine::GetDepth() { 2614911d36aSJim Ingham return lldb::eSearchDepthModule; 26230fdc8d8SChris Lattner } 26330fdc8d8SChris Lattner 264b9c1b51eSKate Stone void BreakpointResolverFileLine::GetDescription(Stream *s) { 265431b1584SAdrian Prantl s->Printf("file = '%s', line = %u, ", m_file_spec.GetPath().c_str(), 266431b1584SAdrian Prantl m_line_number); 267431b1584SAdrian Prantl if (m_column) 268431b1584SAdrian Prantl s->Printf("column = %u, ", m_column); 269431b1584SAdrian Prantl s->Printf("exact_match = %d", m_exact_match); 27030fdc8d8SChris Lattner } 27130fdc8d8SChris Lattner 272b9c1b51eSKate Stone void BreakpointResolverFileLine::Dump(Stream *s) const {} 27330fdc8d8SChris Lattner 27433df7cd3SJim Ingham lldb::BreakpointResolverSP 275b9c1b51eSKate Stone BreakpointResolverFileLine::CopyForBreakpoint(Breakpoint &breakpoint) { 276b9c1b51eSKate Stone lldb::BreakpointResolverSP ret_sp(new BreakpointResolverFileLine( 277431b1584SAdrian Prantl &breakpoint, m_file_spec, m_line_number, m_column, m_offset, m_inlines, 278b9c1b51eSKate Stone m_skip_prologue, m_exact_match)); 27933df7cd3SJim Ingham 28033df7cd3SJim Ingham return ret_sp; 28133df7cd3SJim Ingham } 282