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