1 //===-- BreakpointResolverFileLine.cpp --------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Breakpoint/BreakpointResolverFileLine.h"
14 // Other libraries and framework 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"
24 using namespace lldb_private;
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,
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) {}
37 BreakpointResolverFileLine::~BreakpointResolverFileLine() {}
39 BreakpointResolver *BreakpointResolverFileLine::CreateFromStructuredData(
40 Breakpoint *bkpt, const StructuredData::Dictionary &options_dict,
42 llvm::StringRef filename;
49 lldb::addr_t offset = 0;
51 success = options_dict.GetValueForKeyAsString(GetKey(OptionNames::FileName),
54 error.SetErrorString("BRFL::CFSD: Couldn't find filename entry.");
58 success = options_dict.GetValueForKeyAsInteger(
59 GetKey(OptionNames::LineNumber), line_no);
61 error.SetErrorString("BRFL::CFSD: Couldn't find line number entry.");
65 success = options_dict.GetValueForKeyAsBoolean(GetKey(OptionNames::Inlines),
68 error.SetErrorString("BRFL::CFSD: Couldn't find check inlines entry.");
72 success = options_dict.GetValueForKeyAsBoolean(
73 GetKey(OptionNames::SkipPrologue), skip_prologue);
75 error.SetErrorString("BRFL::CFSD: Couldn't find skip prologue entry.");
79 success = options_dict.GetValueForKeyAsBoolean(
80 GetKey(OptionNames::ExactMatch), exact_match);
82 error.SetErrorString("BRFL::CFSD: Couldn't find exact match entry.");
86 FileSpec file_spec(filename, false);
88 return new BreakpointResolverFileLine(bkpt, file_spec, line_no, offset,
89 check_inlines, skip_prologue,
93 StructuredData::ObjectSP
94 BreakpointResolverFileLine::SerializeToStructuredData() {
95 StructuredData::DictionarySP options_dict_sp(
96 new StructuredData::Dictionary());
98 options_dict_sp->AddStringItem(GetKey(OptionNames::FileName),
99 m_file_spec.GetPath());
100 options_dict_sp->AddIntegerItem(GetKey(OptionNames::LineNumber),
102 options_dict_sp->AddBooleanItem(GetKey(OptionNames::Inlines), m_inlines);
103 options_dict_sp->AddBooleanItem(GetKey(OptionNames::SkipPrologue),
105 options_dict_sp->AddBooleanItem(GetKey(OptionNames::ExactMatch),
108 return WrapOptionsDict(options_dict_sp);
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
118 void BreakpointResolverFileLine::FilterContexts(SymbolContextList &sc_list,
121 return; // Nothing to do. Contexts are precise.
123 llvm::StringRef relative_path;
125 relative_path = m_file_spec.GetDirectory().GetStringRef();
127 Log * log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS);
128 for(uint32_t i = 0; i < sc_list.GetSize(); ++i) {
130 sc_list.GetContextAtIndex(i, sc);
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);
151 const Block *inline_block = sc.block->GetContainingInlinedBlock();
153 const Declaration &inline_declaration = inline_block->GetInlinedFunctionInfo()->GetDeclaration();
154 if (!inline_declaration.IsValid())
156 file = inline_declaration.GetFile();
157 line = inline_declaration.GetLine();
158 } else if (sc.function)
159 sc.function->GetStartLineSourceInfo(file, line);
163 if (file != sc.line_entry.file) {
164 LLDB_LOG(log, "unexpected symbol context file {0}", sc.line_entry.file);
168 // Compare the requested line number with the line of the function
169 // declaration. In case of a function declared as:
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:
180 // inline int foo1() { ... }
181 // inline int foo2() { ... }
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);
193 Searcher::CallbackReturn
194 BreakpointResolverFileLine::SearchCallback(SearchFilter &filter,
195 SymbolContext &context,
196 Address *addr, bool containing) {
197 SymbolContextList sc_list;
199 assert(m_breakpoint != NULL);
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.
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.
217 FileSpec search_file_spec = m_file_spec;
218 const bool is_relative = m_file_spec.IsRelative();
220 search_file_spec.GetDirectory().Clear();
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));
226 if (filter.CompUnitPasses(*cu_sp))
227 cu_sp->ResolveSymbolContext(search_file_spec, m_line_number, m_inlines,
228 m_exact_match, eSymbolContextEverything,
233 FilterContexts(sc_list, is_relative);
236 s.Printf("for %s:%d ", m_file_spec.GetFilename().AsCString("<Unknown>"),
239 SetSCMatchesByLine(filter, sc_list, m_skip_prologue, s.GetString());
241 return Searcher::eCallbackReturnContinue;
244 Searcher::Depth BreakpointResolverFileLine::GetDepth() {
245 return Searcher::eDepthModule;
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);
253 void BreakpointResolverFileLine::Dump(Stream *s) const {}
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));