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 // One complication, if the line number returned from GetStartLineSourceInfo 185 // is 0, then we can't do this calculation. That can happen if 186 // GetStartLineSourceInfo gets an error, or if the first line number in 187 // the function really is 0 - which happens for some languages. 188 const int decl_line_is_too_late_fudge = 1; 189 if (line && m_line_number < line - decl_line_is_too_late_fudge) { 190 LLDB_LOG(log, "removing symbol context at {0}:{1}", file, line); 191 sc_list.RemoveContextAtIndex(i); 192 --i; 193 } 194 } 195 } 196 197 Searcher::CallbackReturn 198 BreakpointResolverFileLine::SearchCallback(SearchFilter &filter, 199 SymbolContext &context, 200 Address *addr, bool containing) { 201 SymbolContextList sc_list; 202 203 assert(m_breakpoint != NULL); 204 205 // There is a tricky bit here. You can have two compilation units that 206 // #include the same file, and in one of them the function at m_line_number 207 // is used (and so code and a line entry for it is generated) but in the 208 // other it isn't. If we considered the CU's independently, then in the 209 // second inclusion, we'd move the breakpoint to the next function that 210 // actually generated code in the header file. That would end up being 211 // confusing. So instead, we do the CU iterations by hand here, then scan 212 // through the complete list of matches, and figure out the closest line 213 // number match, and only set breakpoints on that match. 214 215 // Note also that if file_spec only had a file name and not a directory, 216 // there may be many different file spec's in the resultant list. The 217 // closest line match for one will not be right for some totally different 218 // file. So we go through the match list and pull out the sets that have the 219 // same file spec in their line_entry and treat each set separately. 220 221 FileSpec search_file_spec = m_file_spec; 222 const bool is_relative = m_file_spec.IsRelative(); 223 if (is_relative) 224 search_file_spec.GetDirectory().Clear(); 225 226 const size_t num_comp_units = context.module_sp->GetNumCompileUnits(); 227 for (size_t i = 0; i < num_comp_units; i++) { 228 CompUnitSP cu_sp(context.module_sp->GetCompileUnitAtIndex(i)); 229 if (cu_sp) { 230 if (filter.CompUnitPasses(*cu_sp)) 231 cu_sp->ResolveSymbolContext(search_file_spec, m_line_number, m_inlines, 232 m_exact_match, eSymbolContextEverything, 233 sc_list); 234 } 235 } 236 237 FilterContexts(sc_list, is_relative); 238 239 StreamString s; 240 s.Printf("for %s:%d ", m_file_spec.GetFilename().AsCString("<Unknown>"), 241 m_line_number); 242 243 SetSCMatchesByLine(filter, sc_list, m_skip_prologue, s.GetString()); 244 245 return Searcher::eCallbackReturnContinue; 246 } 247 248 Searcher::Depth BreakpointResolverFileLine::GetDepth() { 249 return Searcher::eDepthModule; 250 } 251 252 void BreakpointResolverFileLine::GetDescription(Stream *s) { 253 s->Printf("file = '%s', line = %u, exact_match = %d", 254 m_file_spec.GetPath().c_str(), m_line_number, m_exact_match); 255 } 256 257 void BreakpointResolverFileLine::Dump(Stream *s) const {} 258 259 lldb::BreakpointResolverSP 260 BreakpointResolverFileLine::CopyForBreakpoint(Breakpoint &breakpoint) { 261 lldb::BreakpointResolverSP ret_sp(new BreakpointResolverFileLine( 262 &breakpoint, m_file_spec, m_line_number, m_offset, m_inlines, 263 m_skip_prologue, m_exact_match)); 264 265 return ret_sp; 266 } 267