1 //===-- BreakpointResolverFileLine.cpp --------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "lldb/Breakpoint/BreakpointResolverFileLine.h" 11 12 // C Includes 13 // C++ Includes 14 // Other libraries and framework includes 15 // Project includes 16 #include "lldb/Breakpoint/BreakpointLocation.h" 17 #include "lldb/Core/Module.h" 18 #include "lldb/Symbol/CompileUnit.h" 19 #include "lldb/Symbol/Function.h" 20 #include "lldb/Utility/Log.h" 21 #include "lldb/Utility/StreamString.h" 22 23 using namespace lldb; 24 using namespace lldb_private; 25 26 //---------------------------------------------------------------------- 27 // BreakpointResolverFileLine: 28 //---------------------------------------------------------------------- 29 BreakpointResolverFileLine::BreakpointResolverFileLine( 30 Breakpoint *bkpt, const FileSpec &file_spec, uint32_t line_no, 31 lldb::addr_t offset, bool check_inlines, bool skip_prologue, 32 bool exact_match) 33 : BreakpointResolver(bkpt, BreakpointResolver::FileLineResolver, offset), 34 m_file_spec(file_spec), m_line_number(line_no), m_inlines(check_inlines), 35 m_skip_prologue(skip_prologue), m_exact_match(exact_match) {} 36 37 BreakpointResolverFileLine::~BreakpointResolverFileLine() {} 38 39 BreakpointResolver *BreakpointResolverFileLine::CreateFromStructuredData( 40 Breakpoint *bkpt, const StructuredData::Dictionary &options_dict, 41 Status &error) { 42 llvm::StringRef filename; 43 uint32_t line_no; 44 bool check_inlines; 45 bool skip_prologue; 46 bool exact_match; 47 bool success; 48 49 lldb::addr_t offset = 0; 50 51 success = options_dict.GetValueForKeyAsString(GetKey(OptionNames::FileName), 52 filename); 53 if (!success) { 54 error.SetErrorString("BRFL::CFSD: Couldn't find filename entry."); 55 return nullptr; 56 } 57 58 success = options_dict.GetValueForKeyAsInteger( 59 GetKey(OptionNames::LineNumber), line_no); 60 if (!success) { 61 error.SetErrorString("BRFL::CFSD: Couldn't find line number entry."); 62 return nullptr; 63 } 64 65 success = options_dict.GetValueForKeyAsBoolean(GetKey(OptionNames::Inlines), 66 check_inlines); 67 if (!success) { 68 error.SetErrorString("BRFL::CFSD: Couldn't find check inlines entry."); 69 return nullptr; 70 } 71 72 success = options_dict.GetValueForKeyAsBoolean( 73 GetKey(OptionNames::SkipPrologue), skip_prologue); 74 if (!success) { 75 error.SetErrorString("BRFL::CFSD: Couldn't find skip prologue entry."); 76 return nullptr; 77 } 78 79 success = options_dict.GetValueForKeyAsBoolean( 80 GetKey(OptionNames::ExactMatch), exact_match); 81 if (!success) { 82 error.SetErrorString("BRFL::CFSD: Couldn't find exact match entry."); 83 return nullptr; 84 } 85 86 FileSpec file_spec(filename, false); 87 88 return new BreakpointResolverFileLine(bkpt, file_spec, line_no, offset, 89 check_inlines, skip_prologue, 90 exact_match); 91 } 92 93 StructuredData::ObjectSP 94 BreakpointResolverFileLine::SerializeToStructuredData() { 95 StructuredData::DictionarySP options_dict_sp( 96 new StructuredData::Dictionary()); 97 98 options_dict_sp->AddStringItem(GetKey(OptionNames::FileName), 99 m_file_spec.GetPath()); 100 options_dict_sp->AddIntegerItem(GetKey(OptionNames::LineNumber), 101 m_line_number); 102 options_dict_sp->AddBooleanItem(GetKey(OptionNames::Inlines), m_inlines); 103 options_dict_sp->AddBooleanItem(GetKey(OptionNames::SkipPrologue), 104 m_skip_prologue); 105 options_dict_sp->AddBooleanItem(GetKey(OptionNames::ExactMatch), 106 m_exact_match); 107 108 return WrapOptionsDict(options_dict_sp); 109 } 110 111 // Filter the symbol context list to remove contexts where the line number was 112 // moved into a new function. We do this conservatively, so if e.g. we cannot 113 // resolve the function in the context (which can happen in case of line-table- 114 // only debug info), we leave the context as is. The trickiest part here is 115 // handling inlined functions -- in this case we need to make sure we look at 116 // the declaration line of the inlined function, NOT the function it was 117 // inlined into. 118 void BreakpointResolverFileLine::FilterContexts(SymbolContextList &sc_list, 119 bool is_relative) { 120 if (m_exact_match) 121 return; // Nothing to do. Contexts are precise. 122 123 llvm::StringRef relative_path; 124 if (is_relative) 125 relative_path = m_file_spec.GetDirectory().GetStringRef(); 126 127 Log * log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS); 128 for(uint32_t i = 0; i < sc_list.GetSize(); ++i) { 129 SymbolContext sc; 130 sc_list.GetContextAtIndex(i, sc); 131 if (is_relative) { 132 // If the path was relative, make sure any matches match as long as the 133 // relative parts of the path match the path from support files 134 auto sc_dir = sc.line_entry.file.GetDirectory().GetStringRef(); 135 if (!sc_dir.endswith(relative_path)) { 136 // We had a relative path specified and the relative directory doesn't 137 // match so remove this one 138 LLDB_LOG(log, "removing not matching relative path {0} since it " 139 "doesn't end with {1}", sc_dir, relative_path); 140 sc_list.RemoveContextAtIndex(i); 141 --i; 142 continue; 143 } 144 } 145 146 if (!sc.block) 147 continue; 148 149 FileSpec file; 150 uint32_t line; 151 const Block *inline_block = sc.block->GetContainingInlinedBlock(); 152 if (inline_block) { 153 const Declaration &inline_declaration = inline_block->GetInlinedFunctionInfo()->GetDeclaration(); 154 if (!inline_declaration.IsValid()) 155 continue; 156 file = inline_declaration.GetFile(); 157 line = inline_declaration.GetLine(); 158 } else if (sc.function) 159 sc.function->GetStartLineSourceInfo(file, line); 160 else 161 continue; 162 163 if (file != sc.line_entry.file) { 164 LLDB_LOG(log, "unexpected symbol context file {0}", sc.line_entry.file); 165 continue; 166 } 167 168 // Compare the requested line number with the line of the function 169 // declaration. In case of a function declared as: 170 // 171 // int 172 // foo() 173 // { 174 // ... 175 // 176 // the compiler will set the declaration line to the "foo" line, which is 177 // the reason why we have -1 here. This can fail in case of two inline 178 // functions defined back-to-back: 179 // 180 // inline int foo1() { ... } 181 // inline int foo2() { ... } 182 // 183 // but that's the best we can do for now. 184 const int decl_line_is_too_late_fudge = 1; 185 if (m_line_number < line - decl_line_is_too_late_fudge) { 186 LLDB_LOG(log, "removing symbol context at {0}:{1}", file, line); 187 sc_list.RemoveContextAtIndex(i); 188 --i; 189 } 190 } 191 } 192 193 Searcher::CallbackReturn 194 BreakpointResolverFileLine::SearchCallback(SearchFilter &filter, 195 SymbolContext &context, 196 Address *addr, bool containing) { 197 SymbolContextList sc_list; 198 199 assert(m_breakpoint != NULL); 200 201 // There is a tricky bit here. You can have two compilation units that 202 // #include the same file, and in one of them the function at m_line_number 203 // is used (and so code and a line entry for it is generated) but in the 204 // other it isn't. If we considered the CU's independently, then in the 205 // second inclusion, we'd move the breakpoint to the next function that 206 // actually generated code in the header file. That would end up being 207 // confusing. So instead, we do the CU iterations by hand here, then scan 208 // through the complete list of matches, and figure out the closest line 209 // number match, and only set breakpoints on that match. 210 211 // Note also that if file_spec only had a file name and not a directory, 212 // there may be many different file spec's in the resultant list. The 213 // closest line match for one will not be right for some totally different 214 // file. So we go through the match list and pull out the sets that have the 215 // same file spec in their line_entry and treat each set separately. 216 217 FileSpec search_file_spec = m_file_spec; 218 const bool is_relative = m_file_spec.IsRelative(); 219 if (is_relative) 220 search_file_spec.GetDirectory().Clear(); 221 222 const size_t num_comp_units = context.module_sp->GetNumCompileUnits(); 223 for (size_t i = 0; i < num_comp_units; i++) { 224 CompUnitSP cu_sp(context.module_sp->GetCompileUnitAtIndex(i)); 225 if (cu_sp) { 226 if (filter.CompUnitPasses(*cu_sp)) 227 cu_sp->ResolveSymbolContext(search_file_spec, m_line_number, m_inlines, 228 m_exact_match, eSymbolContextEverything, 229 sc_list); 230 } 231 } 232 233 FilterContexts(sc_list, is_relative); 234 235 StreamString s; 236 s.Printf("for %s:%d ", m_file_spec.GetFilename().AsCString("<Unknown>"), 237 m_line_number); 238 239 SetSCMatchesByLine(filter, sc_list, m_skip_prologue, s.GetString()); 240 241 return Searcher::eCallbackReturnContinue; 242 } 243 244 Searcher::Depth BreakpointResolverFileLine::GetDepth() { 245 return Searcher::eDepthModule; 246 } 247 248 void BreakpointResolverFileLine::GetDescription(Stream *s) { 249 s->Printf("file = '%s', line = %u, exact_match = %d", 250 m_file_spec.GetPath().c_str(), m_line_number, m_exact_match); 251 } 252 253 void BreakpointResolverFileLine::Dump(Stream *s) const {} 254 255 lldb::BreakpointResolverSP 256 BreakpointResolverFileLine::CopyForBreakpoint(Breakpoint &breakpoint) { 257 lldb::BreakpointResolverSP ret_sp(new BreakpointResolverFileLine( 258 &breakpoint, m_file_spec, m_line_number, m_offset, m_inlines, 259 m_skip_prologue, m_exact_match)); 260 261 return ret_sp; 262 } 263