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, 411a81b273SJim Ingham Error &error) { 42e14dc268SJim Ingham std::string 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 111*bf37a037SPavel Labath // Filter the symbol context list to remove contexts where the line number was 112*bf37a037SPavel Labath // moved into a new function. We do this conservatively, so if e.g. we cannot 113*bf37a037SPavel Labath // resolve the function in the context (which can happen in case of 114*bf37a037SPavel Labath // line-table-only debug info), we leave the context as is. The trickiest part 115*bf37a037SPavel Labath // here is handling inlined functions -- in this case we need to make sure we 116*bf37a037SPavel Labath // look at the declaration line of the inlined function, NOT the function it was 117*bf37a037SPavel Labath // inlined into. 118*bf37a037SPavel Labath void BreakpointResolverFileLine::FilterContexts(SymbolContextList &sc_list) { 119*bf37a037SPavel Labath if (m_exact_match) 120*bf37a037SPavel Labath return; // Nothing to do. Contexts are precise. 121*bf37a037SPavel Labath 122*bf37a037SPavel Labath Log * log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS); 123*bf37a037SPavel Labath for(uint32_t i = 0; i < sc_list.GetSize(); ++i) { 124*bf37a037SPavel Labath SymbolContext sc; 125*bf37a037SPavel Labath sc_list.GetContextAtIndex(i, sc); 126*bf37a037SPavel Labath if (! sc.block) 127*bf37a037SPavel Labath continue; 128*bf37a037SPavel Labath 129*bf37a037SPavel Labath FileSpec file; 130*bf37a037SPavel Labath uint32_t line; 131*bf37a037SPavel Labath const Block *inline_block = sc.block->GetContainingInlinedBlock(); 132*bf37a037SPavel Labath if (inline_block) { 133*bf37a037SPavel Labath const Declaration &inline_declaration = inline_block->GetInlinedFunctionInfo()->GetDeclaration(); 134*bf37a037SPavel Labath if (!inline_declaration.IsValid()) 135*bf37a037SPavel Labath continue; 136*bf37a037SPavel Labath file = inline_declaration.GetFile(); 137*bf37a037SPavel Labath line = inline_declaration.GetLine(); 138*bf37a037SPavel Labath } else if (sc.function) 139*bf37a037SPavel Labath sc.function->GetStartLineSourceInfo(file, line); 140*bf37a037SPavel Labath else 141*bf37a037SPavel Labath continue; 142*bf37a037SPavel Labath 143*bf37a037SPavel Labath if (file != sc.line_entry.file) { 144*bf37a037SPavel Labath LLDB_LOG(log, "unexpected symbol context file {0}", sc.line_entry.file); 145*bf37a037SPavel Labath continue; 146*bf37a037SPavel Labath } 147*bf37a037SPavel Labath 148*bf37a037SPavel Labath // Compare the requested line number with the line of the function 149*bf37a037SPavel Labath // declaration. In case of a function declared as: 150*bf37a037SPavel Labath // 151*bf37a037SPavel Labath // int 152*bf37a037SPavel Labath // foo() 153*bf37a037SPavel Labath // { 154*bf37a037SPavel Labath // ... 155*bf37a037SPavel Labath // 156*bf37a037SPavel Labath // the compiler will set the declaration line to the "foo" line, which is 157*bf37a037SPavel Labath // the reason why we have -1 here. This can fail in case of two inline 158*bf37a037SPavel Labath // functions defined back-to-back: 159*bf37a037SPavel Labath // 160*bf37a037SPavel Labath // inline int foo1() { ... } 161*bf37a037SPavel Labath // inline int foo2() { ... } 162*bf37a037SPavel Labath // 163*bf37a037SPavel Labath // but that's the best we can do for now. 164*bf37a037SPavel Labath const int decl_line_is_too_late_fudge = 1; 165*bf37a037SPavel Labath if (m_line_number < line - decl_line_is_too_late_fudge) { 166*bf37a037SPavel Labath LLDB_LOG(log, "removing symbol context at {0}:{1}", file, line); 167*bf37a037SPavel Labath sc_list.RemoveContextAtIndex(i); 168*bf37a037SPavel Labath --i; 169*bf37a037SPavel Labath } 170*bf37a037SPavel Labath } 171*bf37a037SPavel Labath } 172*bf37a037SPavel Labath 17330fdc8d8SChris Lattner Searcher::CallbackReturn 174b9c1b51eSKate Stone BreakpointResolverFileLine::SearchCallback(SearchFilter &filter, 17530fdc8d8SChris Lattner SymbolContext &context, 176b9c1b51eSKate Stone Address *addr, bool containing) { 17730fdc8d8SChris Lattner SymbolContextList sc_list; 17830fdc8d8SChris Lattner 17930fdc8d8SChris Lattner assert(m_breakpoint != NULL); 18030fdc8d8SChris Lattner 181b9c1b51eSKate Stone // There is a tricky bit here. You can have two compilation units that 182*bf37a037SPavel Labath // #include the same file, and in one of them the function at m_line_number is 183*bf37a037SPavel Labath // used (and so code and a line entry for it is generated) but in the other it 184*bf37a037SPavel Labath // isn't. If we considered the CU's independently, then in the second 185*bf37a037SPavel Labath // inclusion, we'd move the breakpoint to the next function that actually 186*bf37a037SPavel Labath // generated code in the header file. That would end up being confusing. So 187*bf37a037SPavel Labath // instead, we do the CU iterations by hand here, then scan through the 188*bf37a037SPavel Labath // complete list of matches, and figure out the closest line number match, and 189*bf37a037SPavel Labath // only set breakpoints on that match. 190bc2f9182SJim Ingham 191b9c1b51eSKate Stone // Note also that if file_spec only had a file name and not a directory, there 192*bf37a037SPavel Labath // may be many different file spec's in the resultant list. The closest line 193*bf37a037SPavel Labath // match for one will not be right for some totally different file. So we go 194*bf37a037SPavel Labath // through the match list and pull out the sets that have the same file spec 195*bf37a037SPavel Labath // in their line_entry and treat each set separately. 196bc2f9182SJim Ingham 197c7bece56SGreg Clayton const size_t num_comp_units = context.module_sp->GetNumCompileUnits(); 198b9c1b51eSKate Stone for (size_t i = 0; i < num_comp_units; i++) { 199bc2f9182SJim Ingham CompUnitSP cu_sp(context.module_sp->GetCompileUnitAtIndex(i)); 200b9c1b51eSKate Stone if (cu_sp) { 2012dafd8edSGreg Clayton if (filter.CompUnitPasses(*cu_sp)) 202b9c1b51eSKate Stone cu_sp->ResolveSymbolContext(m_file_spec, m_line_number, m_inlines, 203b9c1b51eSKate Stone m_exact_match, eSymbolContextEverything, 204b9c1b51eSKate Stone sc_list); 205bc2f9182SJim Ingham } 2062dafd8edSGreg Clayton } 207*bf37a037SPavel Labath 208*bf37a037SPavel Labath FilterContexts(sc_list); 209*bf37a037SPavel Labath 21030fdc8d8SChris Lattner StreamString s; 211b9c1b51eSKate Stone s.Printf("for %s:%d ", m_file_spec.GetFilename().AsCString("<Unknown>"), 212969795f1SJim Ingham m_line_number); 213f642373cSJim Ingham 21495eae423SZachary Turner SetSCMatchesByLine(filter, sc_list, m_skip_prologue, s.GetString()); 215bc2f9182SJim Ingham 21630fdc8d8SChris Lattner return Searcher::eCallbackReturnContinue; 21730fdc8d8SChris Lattner } 21830fdc8d8SChris Lattner 219b9c1b51eSKate Stone Searcher::Depth BreakpointResolverFileLine::GetDepth() { 220bc2f9182SJim Ingham return Searcher::eDepthModule; 22130fdc8d8SChris Lattner } 22230fdc8d8SChris Lattner 223b9c1b51eSKate Stone void BreakpointResolverFileLine::GetDescription(Stream *s) { 224b9c1b51eSKate Stone s->Printf("file = '%s', line = %u, exact_match = %d", 225b9c1b51eSKate Stone m_file_spec.GetPath().c_str(), m_line_number, m_exact_match); 22630fdc8d8SChris Lattner } 22730fdc8d8SChris Lattner 228b9c1b51eSKate Stone void BreakpointResolverFileLine::Dump(Stream *s) const {} 22930fdc8d8SChris Lattner 23033df7cd3SJim Ingham lldb::BreakpointResolverSP 231b9c1b51eSKate Stone BreakpointResolverFileLine::CopyForBreakpoint(Breakpoint &breakpoint) { 232b9c1b51eSKate Stone lldb::BreakpointResolverSP ret_sp(new BreakpointResolverFileLine( 233b9c1b51eSKate Stone &breakpoint, m_file_spec, m_line_number, m_offset, m_inlines, 234b9c1b51eSKate Stone m_skip_prologue, m_exact_match)); 23533df7cd3SJim Ingham 23633df7cd3SJim Ingham return ret_sp; 23733df7cd3SJim Ingham } 238