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
114 // line-table-only debug info), we leave the context as is. The trickiest part
115 // here is handling inlined functions -- in this case we need to make sure we
116 // look at the declaration line of the inlined function, NOT the function it was
118 void BreakpointResolverFileLine::FilterContexts(SymbolContextList &sc_list) {
120 return; // Nothing to do. Contexts are precise.
122 Log * log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS);
123 for(uint32_t i = 0; i < sc_list.GetSize(); ++i) {
125 sc_list.GetContextAtIndex(i, sc);
131 const Block *inline_block = sc.block->GetContainingInlinedBlock();
133 const Declaration &inline_declaration = inline_block->GetInlinedFunctionInfo()->GetDeclaration();
134 if (!inline_declaration.IsValid())
136 file = inline_declaration.GetFile();
137 line = inline_declaration.GetLine();
138 } else if (sc.function)
139 sc.function->GetStartLineSourceInfo(file, line);
143 if (file != sc.line_entry.file) {
144 LLDB_LOG(log, "unexpected symbol context file {0}", sc.line_entry.file);
148 // Compare the requested line number with the line of the function
149 // declaration. In case of a function declared as:
156 // the compiler will set the declaration line to the "foo" line, which is
157 // the reason why we have -1 here. This can fail in case of two inline
158 // functions defined back-to-back:
160 // inline int foo1() { ... }
161 // inline int foo2() { ... }
163 // but that's the best we can do for now.
164 const int decl_line_is_too_late_fudge = 1;
165 if (m_line_number < line - decl_line_is_too_late_fudge) {
166 LLDB_LOG(log, "removing symbol context at {0}:{1}", file, line);
167 sc_list.RemoveContextAtIndex(i);
173 Searcher::CallbackReturn
174 BreakpointResolverFileLine::SearchCallback(SearchFilter &filter,
175 SymbolContext &context,
176 Address *addr, bool containing) {
177 SymbolContextList sc_list;
179 assert(m_breakpoint != NULL);
181 // There is a tricky bit here. You can have two compilation units that
182 // #include the same file, and in one of them the function at m_line_number is
183 // used (and so code and a line entry for it is generated) but in the other it
184 // isn't. If we considered the CU's independently, then in the second
185 // inclusion, we'd move the breakpoint to the next function that actually
186 // generated code in the header file. That would end up being confusing. So
187 // instead, we do the CU iterations by hand here, then scan through the
188 // complete list of matches, and figure out the closest line number match, and
189 // only set breakpoints on that match.
191 // Note also that if file_spec only had a file name and not a directory, there
192 // may be many different file spec's in the resultant list. The closest line
193 // match for one will not be right for some totally different file. So we go
194 // through the match list and pull out the sets that have the same file spec
195 // in their line_entry and treat each set separately.
197 const size_t num_comp_units = context.module_sp->GetNumCompileUnits();
198 for (size_t i = 0; i < num_comp_units; i++) {
199 CompUnitSP cu_sp(context.module_sp->GetCompileUnitAtIndex(i));
201 if (filter.CompUnitPasses(*cu_sp))
202 cu_sp->ResolveSymbolContext(m_file_spec, m_line_number, m_inlines,
203 m_exact_match, eSymbolContextEverything,
208 FilterContexts(sc_list);
211 s.Printf("for %s:%d ", m_file_spec.GetFilename().AsCString("<Unknown>"),
214 SetSCMatchesByLine(filter, sc_list, m_skip_prologue, s.GetString());
216 return Searcher::eCallbackReturnContinue;
219 Searcher::Depth BreakpointResolverFileLine::GetDepth() {
220 return Searcher::eDepthModule;
223 void BreakpointResolverFileLine::GetDescription(Stream *s) {
224 s->Printf("file = '%s', line = %u, exact_match = %d",
225 m_file_spec.GetPath().c_str(), m_line_number, m_exact_match);
228 void BreakpointResolverFileLine::Dump(Stream *s) const {}
230 lldb::BreakpointResolverSP
231 BreakpointResolverFileLine::CopyForBreakpoint(Breakpoint &breakpoint) {
232 lldb::BreakpointResolverSP ret_sp(new BreakpointResolverFileLine(
233 &breakpoint, m_file_spec, m_line_number, m_offset, m_inlines,
234 m_skip_prologue, m_exact_match));