1 //===-- BreakpointResolverFileLine.cpp --------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Breakpoint/BreakpointResolverFileLine.h"
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"
19 using namespace lldb_private;
21 // BreakpointResolverFileLine:
22 BreakpointResolverFileLine::BreakpointResolverFileLine(
23 Breakpoint *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) {}
31 BreakpointResolverFileLine::~BreakpointResolverFileLine() {}
33 BreakpointResolver *BreakpointResolverFileLine::CreateFromStructuredData(
34 Breakpoint *bkpt, const StructuredData::Dictionary &options_dict,
36 llvm::StringRef filename;
44 lldb::addr_t offset = 0;
46 success = options_dict.GetValueForKeyAsString(GetKey(OptionNames::FileName),
49 error.SetErrorString("BRFL::CFSD: Couldn't find filename entry.");
53 success = options_dict.GetValueForKeyAsInteger(
54 GetKey(OptionNames::LineNumber), line_no);
56 error.SetErrorString("BRFL::CFSD: Couldn't find line number entry.");
61 options_dict.GetValueForKeyAsInteger(GetKey(OptionNames::Column), column);
63 // Backwards compatibility.
67 success = options_dict.GetValueForKeyAsBoolean(GetKey(OptionNames::Inlines),
70 error.SetErrorString("BRFL::CFSD: Couldn't find check inlines entry.");
74 success = options_dict.GetValueForKeyAsBoolean(
75 GetKey(OptionNames::SkipPrologue), skip_prologue);
77 error.SetErrorString("BRFL::CFSD: Couldn't find skip prologue entry.");
81 success = options_dict.GetValueForKeyAsBoolean(
82 GetKey(OptionNames::ExactMatch), exact_match);
84 error.SetErrorString("BRFL::CFSD: Couldn't find exact match entry.");
88 FileSpec file_spec(filename);
90 return new BreakpointResolverFileLine(bkpt, file_spec, line_no, column,
91 offset, check_inlines, skip_prologue,
95 StructuredData::ObjectSP
96 BreakpointResolverFileLine::SerializeToStructuredData() {
97 StructuredData::DictionarySP options_dict_sp(
98 new StructuredData::Dictionary());
100 options_dict_sp->AddStringItem(GetKey(OptionNames::FileName),
101 m_file_spec.GetPath());
102 options_dict_sp->AddIntegerItem(GetKey(OptionNames::LineNumber),
104 options_dict_sp->AddIntegerItem(GetKey(OptionNames::Column),
106 options_dict_sp->AddBooleanItem(GetKey(OptionNames::Inlines), m_inlines);
107 options_dict_sp->AddBooleanItem(GetKey(OptionNames::SkipPrologue),
109 options_dict_sp->AddBooleanItem(GetKey(OptionNames::ExactMatch),
112 return WrapOptionsDict(options_dict_sp);
115 // Filter the symbol context list to remove contexts where the line number was
116 // moved into a new function. We do this conservatively, so if e.g. we cannot
117 // resolve the function in the context (which can happen in case of line-table-
118 // only debug info), we leave the context as is. The trickiest part here is
119 // handling inlined functions -- in this case we need to make sure we look at
120 // the declaration line of the inlined function, NOT the function it was
122 void BreakpointResolverFileLine::FilterContexts(SymbolContextList &sc_list,
125 return; // Nothing to do. Contexts are precise.
127 llvm::StringRef relative_path;
129 relative_path = m_file_spec.GetDirectory().GetStringRef();
131 Log * log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS);
132 for(uint32_t i = 0; i < sc_list.GetSize(); ++i) {
134 sc_list.GetContextAtIndex(i, sc);
136 // If the path was relative, make sure any matches match as long as the
137 // relative parts of the path match the path from support files
138 auto sc_dir = sc.line_entry.file.GetDirectory().GetStringRef();
139 if (!sc_dir.endswith(relative_path)) {
140 // We had a relative path specified and the relative directory doesn't
141 // match so remove this one
142 LLDB_LOG(log, "removing not matching relative path {0} since it "
143 "doesn't end with {1}", sc_dir, relative_path);
144 sc_list.RemoveContextAtIndex(i);
155 const Block *inline_block = sc.block->GetContainingInlinedBlock();
157 const Declaration &inline_declaration = inline_block->GetInlinedFunctionInfo()->GetDeclaration();
158 if (!inline_declaration.IsValid())
160 file = inline_declaration.GetFile();
161 line = inline_declaration.GetLine();
162 } else if (sc.function)
163 sc.function->GetStartLineSourceInfo(file, line);
167 if (file != sc.line_entry.file) {
168 LLDB_LOG(log, "unexpected symbol context file {0}", sc.line_entry.file);
172 // Compare the requested line number with the line of the function
173 // declaration. In case of a function declared as:
180 // the compiler will set the declaration line to the "foo" line, which is
181 // the reason why we have -1 here. This can fail in case of two inline
182 // functions defined back-to-back:
184 // inline int foo1() { ... }
185 // inline int foo2() { ... }
187 // but that's the best we can do for now.
188 // One complication, if the line number returned from GetStartLineSourceInfo
189 // is 0, then we can't do this calculation. That can happen if
190 // GetStartLineSourceInfo gets an error, or if the first line number in
191 // the function really is 0 - which happens for some languages.
192 const int decl_line_is_too_late_fudge = 1;
193 if (line && m_line_number < line - decl_line_is_too_late_fudge) {
194 LLDB_LOG(log, "removing symbol context at {0}:{1}", file, line);
195 sc_list.RemoveContextAtIndex(i);
201 Searcher::CallbackReturn
202 BreakpointResolverFileLine::SearchCallback(SearchFilter &filter,
203 SymbolContext &context,
204 Address *addr, bool containing) {
205 SymbolContextList sc_list;
207 assert(m_breakpoint != nullptr);
209 // There is a tricky bit here. You can have two compilation units that
210 // #include the same file, and in one of them the function at m_line_number
211 // is used (and so code and a line entry for it is generated) but in the
212 // other it isn't. If we considered the CU's independently, then in the
213 // second inclusion, we'd move the breakpoint to the next function that
214 // actually generated code in the header file. That would end up being
215 // confusing. So instead, we do the CU iterations by hand here, then scan
216 // through the complete list of matches, and figure out the closest line
217 // number match, and only set breakpoints on that match.
219 // Note also that if file_spec only had a file name and not a directory,
220 // there may be many different file spec's in the resultant list. The
221 // closest line match for one will not be right for some totally different
222 // file. So we go through the match list and pull out the sets that have the
223 // same file spec in their line_entry and treat each set separately.
225 FileSpec search_file_spec = m_file_spec;
226 const bool is_relative = m_file_spec.IsRelative();
228 search_file_spec.GetDirectory().Clear();
230 const size_t num_comp_units = context.module_sp->GetNumCompileUnits();
231 for (size_t i = 0; i < num_comp_units; i++) {
232 CompUnitSP cu_sp(context.module_sp->GetCompileUnitAtIndex(i));
234 if (filter.CompUnitPasses(*cu_sp))
235 cu_sp->ResolveSymbolContext(search_file_spec, m_line_number, m_inlines,
236 m_exact_match, eSymbolContextEverything,
241 FilterContexts(sc_list, is_relative);
244 s.Printf("for %s:%d ", m_file_spec.GetFilename().AsCString("<Unknown>"),
247 SetSCMatchesByLine(filter, sc_list, m_skip_prologue, s.GetString(),
248 m_line_number, m_column);
250 return Searcher::eCallbackReturnContinue;
253 lldb::SearchDepth BreakpointResolverFileLine::GetDepth() {
254 return lldb::eSearchDepthModule;
257 void BreakpointResolverFileLine::GetDescription(Stream *s) {
258 s->Printf("file = '%s', line = %u, ", m_file_spec.GetPath().c_str(),
261 s->Printf("column = %u, ", m_column);
262 s->Printf("exact_match = %d", m_exact_match);
265 void BreakpointResolverFileLine::Dump(Stream *s) const {}
267 lldb::BreakpointResolverSP
268 BreakpointResolverFileLine::CopyForBreakpoint(Breakpoint &breakpoint) {
269 lldb::BreakpointResolverSP ret_sp(new BreakpointResolverFileLine(
270 &breakpoint, m_file_spec, m_line_number, m_column, m_offset, m_inlines,
271 m_skip_prologue, m_exact_match));