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, 31b9c1b51eSKate Stone lldb::addr_t offset, bool check_inlines, bool skip_prologue, 32b9c1b51eSKate Stone bool exact_match) 33b9c1b51eSKate Stone : BreakpointResolver(bkpt, BreakpointResolver::FileLineResolver, offset), 34b9c1b51eSKate Stone m_file_spec(file_spec), m_line_number(line_no), m_inlines(check_inlines), 35b9c1b51eSKate Stone m_skip_prologue(skip_prologue), m_exact_match(exact_match) {} 3630fdc8d8SChris Lattner 37b9c1b51eSKate Stone BreakpointResolverFileLine::~BreakpointResolverFileLine() {} 3830fdc8d8SChris Lattner 39e14dc268SJim Ingham BreakpointResolver *BreakpointResolverFileLine::CreateFromStructuredData( 401a81b273SJim Ingham Breakpoint *bkpt, const StructuredData::Dictionary &options_dict, 4197206d57SZachary Turner Status &error) { 422833321fSZachary Turner llvm::StringRef filename; 43e14dc268SJim Ingham uint32_t line_no; 44e14dc268SJim Ingham bool check_inlines; 45e14dc268SJim Ingham bool skip_prologue; 46e14dc268SJim Ingham bool exact_match; 47e14dc268SJim Ingham bool success; 48e14dc268SJim Ingham 49e14dc268SJim Ingham lldb::addr_t offset = 0; 50e14dc268SJim Ingham 51e14dc268SJim Ingham success = options_dict.GetValueForKeyAsString(GetKey(OptionNames::FileName), 52e14dc268SJim Ingham filename); 53e14dc268SJim Ingham if (!success) { 54e14dc268SJim Ingham error.SetErrorString("BRFL::CFSD: Couldn't find filename entry."); 55e14dc268SJim Ingham return nullptr; 56e14dc268SJim Ingham } 57e14dc268SJim Ingham 58e14dc268SJim Ingham success = options_dict.GetValueForKeyAsInteger( 59e14dc268SJim Ingham GetKey(OptionNames::LineNumber), line_no); 60e14dc268SJim Ingham if (!success) { 61e14dc268SJim Ingham error.SetErrorString("BRFL::CFSD: Couldn't find line number entry."); 62e14dc268SJim Ingham return nullptr; 63e14dc268SJim Ingham } 64e14dc268SJim Ingham 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 86771ef6d4SMalcolm Parsons FileSpec file_spec(filename, false); 87e14dc268SJim Ingham 88e14dc268SJim Ingham return new BreakpointResolverFileLine(bkpt, file_spec, line_no, offset, 89e14dc268SJim Ingham 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); 102e14dc268SJim Ingham options_dict_sp->AddBooleanItem(GetKey(OptionNames::Inlines), m_inlines); 103e14dc268SJim Ingham options_dict_sp->AddBooleanItem(GetKey(OptionNames::SkipPrologue), 104e14dc268SJim Ingham m_skip_prologue); 105e14dc268SJim Ingham options_dict_sp->AddBooleanItem(GetKey(OptionNames::ExactMatch), 106e14dc268SJim Ingham m_exact_match); 107e14dc268SJim Ingham 108e14dc268SJim Ingham return WrapOptionsDict(options_dict_sp); 109e14dc268SJim Ingham } 110e14dc268SJim Ingham 111bf37a037SPavel Labath // Filter the symbol context list to remove contexts where the line number was 112bf37a037SPavel Labath // moved into a new function. We do this conservatively, so if e.g. we cannot 11305097246SAdrian Prantl // resolve the function in the context (which can happen in case of line-table- 11405097246SAdrian Prantl // only debug info), we leave the context as is. The trickiest part here is 11505097246SAdrian Prantl // handling inlined functions -- in this case we need to make sure we look at 11605097246SAdrian Prantl // the declaration line of the inlined function, NOT the function it was 117bf37a037SPavel Labath // inlined into. 11852098cf5SGreg Clayton void BreakpointResolverFileLine::FilterContexts(SymbolContextList &sc_list, 11952098cf5SGreg Clayton bool is_relative) { 120bf37a037SPavel Labath if (m_exact_match) 121bf37a037SPavel Labath return; // Nothing to do. Contexts are precise. 122bf37a037SPavel Labath 12352098cf5SGreg Clayton llvm::StringRef relative_path; 12452098cf5SGreg Clayton if (is_relative) 125776cd7adSGreg Clayton relative_path = m_file_spec.GetDirectory().GetStringRef(); 12652098cf5SGreg Clayton 127bf37a037SPavel Labath Log * log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS); 128bf37a037SPavel Labath for(uint32_t i = 0; i < sc_list.GetSize(); ++i) { 129bf37a037SPavel Labath SymbolContext sc; 130bf37a037SPavel Labath sc_list.GetContextAtIndex(i, sc); 13152098cf5SGreg Clayton if (is_relative) { 13252098cf5SGreg Clayton // If the path was relative, make sure any matches match as long as the 13352098cf5SGreg Clayton // relative parts of the path match the path from support files 13452098cf5SGreg Clayton auto sc_dir = sc.line_entry.file.GetDirectory().GetStringRef(); 13552098cf5SGreg Clayton if (!sc_dir.endswith(relative_path)) { 13605097246SAdrian Prantl // We had a relative path specified and the relative directory doesn't 13705097246SAdrian Prantl // match so remove this one 13852098cf5SGreg Clayton LLDB_LOG(log, "removing not matching relative path {0} since it " 13952098cf5SGreg Clayton "doesn't end with {1}", sc_dir, relative_path); 14052098cf5SGreg Clayton sc_list.RemoveContextAtIndex(i); 14152098cf5SGreg Clayton --i; 14252098cf5SGreg Clayton continue; 14352098cf5SGreg Clayton } 14452098cf5SGreg Clayton } 14552098cf5SGreg Clayton 146bf37a037SPavel Labath if (!sc.block) 147bf37a037SPavel Labath continue; 148bf37a037SPavel Labath 149bf37a037SPavel Labath FileSpec file; 150bf37a037SPavel Labath uint32_t line; 151bf37a037SPavel Labath const Block *inline_block = sc.block->GetContainingInlinedBlock(); 152bf37a037SPavel Labath if (inline_block) { 153bf37a037SPavel Labath const Declaration &inline_declaration = inline_block->GetInlinedFunctionInfo()->GetDeclaration(); 154bf37a037SPavel Labath if (!inline_declaration.IsValid()) 155bf37a037SPavel Labath continue; 156bf37a037SPavel Labath file = inline_declaration.GetFile(); 157bf37a037SPavel Labath line = inline_declaration.GetLine(); 158bf37a037SPavel Labath } else if (sc.function) 159bf37a037SPavel Labath sc.function->GetStartLineSourceInfo(file, line); 160bf37a037SPavel Labath else 161bf37a037SPavel Labath continue; 162bf37a037SPavel Labath 163bf37a037SPavel Labath if (file != sc.line_entry.file) { 164bf37a037SPavel Labath LLDB_LOG(log, "unexpected symbol context file {0}", sc.line_entry.file); 165bf37a037SPavel Labath continue; 166bf37a037SPavel Labath } 167bf37a037SPavel Labath 168bf37a037SPavel Labath // Compare the requested line number with the line of the function 169bf37a037SPavel Labath // declaration. In case of a function declared as: 170bf37a037SPavel Labath // 171bf37a037SPavel Labath // int 172bf37a037SPavel Labath // foo() 173bf37a037SPavel Labath // { 174bf37a037SPavel Labath // ... 175bf37a037SPavel Labath // 176bf37a037SPavel Labath // the compiler will set the declaration line to the "foo" line, which is 177bf37a037SPavel Labath // the reason why we have -1 here. This can fail in case of two inline 178bf37a037SPavel Labath // functions defined back-to-back: 179bf37a037SPavel Labath // 180bf37a037SPavel Labath // inline int foo1() { ... } 181bf37a037SPavel Labath // inline int foo2() { ... } 182bf37a037SPavel Labath // 183bf37a037SPavel Labath // but that's the best we can do for now. 184*7aa4ed9bSJim Ingham // One complication, if the line number returned from GetStartLineSourceInfo 185*7aa4ed9bSJim Ingham // is 0, then we can't do this calculation. That can happen if 186*7aa4ed9bSJim Ingham // GetStartLineSourceInfo gets an error, or if the first line number in 187*7aa4ed9bSJim Ingham // the function really is 0 - which happens for some languages. 188bf37a037SPavel Labath const int decl_line_is_too_late_fudge = 1; 189*7aa4ed9bSJim Ingham if (line && m_line_number < line - decl_line_is_too_late_fudge) { 190bf37a037SPavel Labath LLDB_LOG(log, "removing symbol context at {0}:{1}", file, line); 191bf37a037SPavel Labath sc_list.RemoveContextAtIndex(i); 192bf37a037SPavel Labath --i; 193bf37a037SPavel Labath } 194bf37a037SPavel Labath } 195bf37a037SPavel Labath } 196bf37a037SPavel Labath 19730fdc8d8SChris Lattner Searcher::CallbackReturn 198b9c1b51eSKate Stone BreakpointResolverFileLine::SearchCallback(SearchFilter &filter, 19930fdc8d8SChris Lattner SymbolContext &context, 200b9c1b51eSKate Stone Address *addr, bool containing) { 20130fdc8d8SChris Lattner SymbolContextList sc_list; 20230fdc8d8SChris Lattner 20330fdc8d8SChris Lattner assert(m_breakpoint != NULL); 20430fdc8d8SChris Lattner 205b9c1b51eSKate Stone // There is a tricky bit here. You can have two compilation units that 20605097246SAdrian Prantl // #include the same file, and in one of them the function at m_line_number 20705097246SAdrian Prantl // is used (and so code and a line entry for it is generated) but in the 20805097246SAdrian Prantl // other it isn't. If we considered the CU's independently, then in the 20905097246SAdrian Prantl // second inclusion, we'd move the breakpoint to the next function that 21005097246SAdrian Prantl // actually generated code in the header file. That would end up being 21105097246SAdrian Prantl // confusing. So instead, we do the CU iterations by hand here, then scan 21205097246SAdrian Prantl // through the complete list of matches, and figure out the closest line 21305097246SAdrian Prantl // number match, and only set breakpoints on that match. 214bc2f9182SJim Ingham 21505097246SAdrian Prantl // Note also that if file_spec only had a file name and not a directory, 21605097246SAdrian Prantl // there may be many different file spec's in the resultant list. The 21705097246SAdrian Prantl // closest line match for one will not be right for some totally different 21805097246SAdrian Prantl // file. So we go through the match list and pull out the sets that have the 21905097246SAdrian Prantl // same file spec in their line_entry and treat each set separately. 220bc2f9182SJim Ingham 22152098cf5SGreg Clayton FileSpec search_file_spec = m_file_spec; 22252098cf5SGreg Clayton const bool is_relative = m_file_spec.IsRelative(); 22352098cf5SGreg Clayton if (is_relative) 22452098cf5SGreg Clayton search_file_spec.GetDirectory().Clear(); 22552098cf5SGreg Clayton 226c7bece56SGreg Clayton const size_t num_comp_units = context.module_sp->GetNumCompileUnits(); 227b9c1b51eSKate Stone for (size_t i = 0; i < num_comp_units; i++) { 228bc2f9182SJim Ingham CompUnitSP cu_sp(context.module_sp->GetCompileUnitAtIndex(i)); 229b9c1b51eSKate Stone if (cu_sp) { 2302dafd8edSGreg Clayton if (filter.CompUnitPasses(*cu_sp)) 23152098cf5SGreg Clayton cu_sp->ResolveSymbolContext(search_file_spec, m_line_number, m_inlines, 232b9c1b51eSKate Stone m_exact_match, eSymbolContextEverything, 233b9c1b51eSKate Stone sc_list); 234bc2f9182SJim Ingham } 2352dafd8edSGreg Clayton } 236bf37a037SPavel Labath 23752098cf5SGreg Clayton FilterContexts(sc_list, is_relative); 238bf37a037SPavel Labath 23930fdc8d8SChris Lattner StreamString s; 240b9c1b51eSKate Stone s.Printf("for %s:%d ", m_file_spec.GetFilename().AsCString("<Unknown>"), 241969795f1SJim Ingham m_line_number); 242f642373cSJim Ingham 24395eae423SZachary Turner SetSCMatchesByLine(filter, sc_list, m_skip_prologue, s.GetString()); 244bc2f9182SJim Ingham 24530fdc8d8SChris Lattner return Searcher::eCallbackReturnContinue; 24630fdc8d8SChris Lattner } 24730fdc8d8SChris Lattner 248b9c1b51eSKate Stone Searcher::Depth BreakpointResolverFileLine::GetDepth() { 249bc2f9182SJim Ingham return Searcher::eDepthModule; 25030fdc8d8SChris Lattner } 25130fdc8d8SChris Lattner 252b9c1b51eSKate Stone void BreakpointResolverFileLine::GetDescription(Stream *s) { 253b9c1b51eSKate Stone s->Printf("file = '%s', line = %u, exact_match = %d", 254b9c1b51eSKate Stone m_file_spec.GetPath().c_str(), m_line_number, m_exact_match); 25530fdc8d8SChris Lattner } 25630fdc8d8SChris Lattner 257b9c1b51eSKate Stone void BreakpointResolverFileLine::Dump(Stream *s) const {} 25830fdc8d8SChris Lattner 25933df7cd3SJim Ingham lldb::BreakpointResolverSP 260b9c1b51eSKate Stone BreakpointResolverFileLine::CopyForBreakpoint(Breakpoint &breakpoint) { 261b9c1b51eSKate Stone lldb::BreakpointResolverSP ret_sp(new BreakpointResolverFileLine( 262b9c1b51eSKate Stone &breakpoint, m_file_spec, m_line_number, m_offset, m_inlines, 263b9c1b51eSKate Stone m_skip_prologue, m_exact_match)); 26433df7cd3SJim Ingham 26533df7cd3SJim Ingham return ret_sp; 26633df7cd3SJim Ingham } 267