130fdc8d8SChris Lattner //===-- BreakpointResolverFileLine.cpp --------------------------*- C++ -*-===//
230fdc8d8SChris Lattner //
330fdc8d8SChris Lattner //                     The LLVM Compiler Infrastructure
430fdc8d8SChris Lattner //
530fdc8d8SChris Lattner // This file is distributed under the University of Illinois Open Source
630fdc8d8SChris Lattner // License. See LICENSE.TXT for details.
730fdc8d8SChris Lattner //
830fdc8d8SChris Lattner //===----------------------------------------------------------------------===//
930fdc8d8SChris Lattner 
1030fdc8d8SChris Lattner #include "lldb/Breakpoint/BreakpointResolverFileLine.h"
1130fdc8d8SChris Lattner 
1230fdc8d8SChris Lattner // C Includes
1330fdc8d8SChris Lattner // C++ Includes
1430fdc8d8SChris Lattner // Other libraries and framework includes
1530fdc8d8SChris Lattner // Project includes
1630fdc8d8SChris Lattner #include "lldb/Breakpoint/BreakpointLocation.h"
171f746071SGreg Clayton #include "lldb/Core/Module.h"
181f746071SGreg Clayton #include "lldb/Symbol/CompileUnit.h"
191f746071SGreg Clayton #include "lldb/Symbol/Function.h"
206f9e6901SZachary Turner #include "lldb/Utility/Log.h"
21bf9a7730SZachary Turner #include "lldb/Utility/StreamString.h"
2230fdc8d8SChris Lattner 
2330fdc8d8SChris Lattner using namespace lldb;
2430fdc8d8SChris Lattner using namespace lldb_private;
2530fdc8d8SChris Lattner 
2630fdc8d8SChris Lattner //----------------------------------------------------------------------
2730fdc8d8SChris Lattner // BreakpointResolverFileLine:
2830fdc8d8SChris Lattner //----------------------------------------------------------------------
29b9c1b51eSKate Stone BreakpointResolverFileLine::BreakpointResolverFileLine(
30b9c1b51eSKate Stone     Breakpoint *bkpt, const FileSpec &file_spec, uint32_t line_no,
31b9c1b51eSKate Stone     lldb::addr_t offset, bool check_inlines, bool skip_prologue,
32b9c1b51eSKate Stone     bool exact_match)
33b9c1b51eSKate Stone     : BreakpointResolver(bkpt, BreakpointResolver::FileLineResolver, offset),
34b9c1b51eSKate Stone       m_file_spec(file_spec), m_line_number(line_no), m_inlines(check_inlines),
35b9c1b51eSKate Stone       m_skip_prologue(skip_prologue), m_exact_match(exact_match) {}
3630fdc8d8SChris Lattner 
37b9c1b51eSKate Stone BreakpointResolverFileLine::~BreakpointResolverFileLine() {}
3830fdc8d8SChris Lattner 
39e14dc268SJim Ingham BreakpointResolver *BreakpointResolverFileLine::CreateFromStructuredData(
401a81b273SJim Ingham     Breakpoint *bkpt, const StructuredData::Dictionary &options_dict,
4197206d57SZachary Turner     Status &error) {
422833321fSZachary Turner   llvm::StringRef filename;
43e14dc268SJim Ingham   uint32_t line_no;
44e14dc268SJim Ingham   bool check_inlines;
45e14dc268SJim Ingham   bool skip_prologue;
46e14dc268SJim Ingham   bool exact_match;
47e14dc268SJim Ingham   bool success;
48e14dc268SJim Ingham 
49e14dc268SJim Ingham   lldb::addr_t offset = 0;
50e14dc268SJim Ingham 
51e14dc268SJim Ingham   success = options_dict.GetValueForKeyAsString(GetKey(OptionNames::FileName),
52e14dc268SJim Ingham                                                 filename);
53e14dc268SJim Ingham   if (!success) {
54e14dc268SJim Ingham     error.SetErrorString("BRFL::CFSD: Couldn't find filename entry.");
55e14dc268SJim Ingham     return nullptr;
56e14dc268SJim Ingham   }
57e14dc268SJim Ingham 
58e14dc268SJim Ingham   success = options_dict.GetValueForKeyAsInteger(
59e14dc268SJim Ingham       GetKey(OptionNames::LineNumber), line_no);
60e14dc268SJim Ingham   if (!success) {
61e14dc268SJim Ingham     error.SetErrorString("BRFL::CFSD: Couldn't find line number entry.");
62e14dc268SJim Ingham     return nullptr;
63e14dc268SJim Ingham   }
64e14dc268SJim Ingham 
65e14dc268SJim Ingham   success = options_dict.GetValueForKeyAsBoolean(GetKey(OptionNames::Inlines),
66e14dc268SJim Ingham                                                  check_inlines);
67e14dc268SJim Ingham   if (!success) {
68e14dc268SJim Ingham     error.SetErrorString("BRFL::CFSD: Couldn't find check inlines entry.");
69e14dc268SJim Ingham     return nullptr;
70e14dc268SJim Ingham   }
71e14dc268SJim Ingham 
72e14dc268SJim Ingham   success = options_dict.GetValueForKeyAsBoolean(
73e14dc268SJim Ingham       GetKey(OptionNames::SkipPrologue), skip_prologue);
74e14dc268SJim Ingham   if (!success) {
75e14dc268SJim Ingham     error.SetErrorString("BRFL::CFSD: Couldn't find skip prologue entry.");
76e14dc268SJim Ingham     return nullptr;
77e14dc268SJim Ingham   }
78e14dc268SJim Ingham 
79e14dc268SJim Ingham   success = options_dict.GetValueForKeyAsBoolean(
80e14dc268SJim Ingham       GetKey(OptionNames::ExactMatch), exact_match);
81e14dc268SJim Ingham   if (!success) {
82e14dc268SJim Ingham     error.SetErrorString("BRFL::CFSD: Couldn't find exact match entry.");
83e14dc268SJim Ingham     return nullptr;
84e14dc268SJim Ingham   }
85e14dc268SJim Ingham 
86771ef6d4SMalcolm Parsons   FileSpec file_spec(filename, false);
87e14dc268SJim Ingham 
88e14dc268SJim Ingham   return new BreakpointResolverFileLine(bkpt, file_spec, line_no, offset,
89e14dc268SJim Ingham                                         check_inlines, skip_prologue,
90e14dc268SJim Ingham                                         exact_match);
91e14dc268SJim Ingham }
92e14dc268SJim Ingham 
93e14dc268SJim Ingham StructuredData::ObjectSP
94e14dc268SJim Ingham BreakpointResolverFileLine::SerializeToStructuredData() {
95e14dc268SJim Ingham   StructuredData::DictionarySP options_dict_sp(
96e14dc268SJim Ingham       new StructuredData::Dictionary());
97e14dc268SJim Ingham 
98e14dc268SJim Ingham   options_dict_sp->AddStringItem(GetKey(OptionNames::FileName),
99e14dc268SJim Ingham                                  m_file_spec.GetPath());
100e14dc268SJim Ingham   options_dict_sp->AddIntegerItem(GetKey(OptionNames::LineNumber),
101e14dc268SJim Ingham                                   m_line_number);
102e14dc268SJim Ingham   options_dict_sp->AddBooleanItem(GetKey(OptionNames::Inlines), m_inlines);
103e14dc268SJim Ingham   options_dict_sp->AddBooleanItem(GetKey(OptionNames::SkipPrologue),
104e14dc268SJim Ingham                                   m_skip_prologue);
105e14dc268SJim Ingham   options_dict_sp->AddBooleanItem(GetKey(OptionNames::ExactMatch),
106e14dc268SJim Ingham                                   m_exact_match);
107e14dc268SJim Ingham 
108e14dc268SJim Ingham   return WrapOptionsDict(options_dict_sp);
109e14dc268SJim Ingham }
110e14dc268SJim Ingham 
111bf37a037SPavel Labath // Filter the symbol context list to remove contexts where the line number was
112bf37a037SPavel Labath // moved into a new function. We do this conservatively, so if e.g. we cannot
113bf37a037SPavel Labath // resolve the function in the context (which can happen in case of
114bf37a037SPavel Labath // line-table-only debug info), we leave the context as is. The trickiest part
115bf37a037SPavel Labath // here is handling inlined functions -- in this case we need to make sure we
116bf37a037SPavel Labath // look at the declaration line of the inlined function, NOT the function it was
117bf37a037SPavel Labath // inlined into.
11852098cf5SGreg Clayton void BreakpointResolverFileLine::FilterContexts(SymbolContextList &sc_list,
11952098cf5SGreg Clayton                                                 bool is_relative) {
120bf37a037SPavel Labath   if (m_exact_match)
121bf37a037SPavel Labath     return; // Nothing to do. Contexts are precise.
122bf37a037SPavel Labath 
12352098cf5SGreg Clayton   llvm::StringRef relative_path;
12452098cf5SGreg Clayton   if (is_relative)
125*776cd7adSGreg Clayton     relative_path = m_file_spec.GetDirectory().GetStringRef();
12652098cf5SGreg Clayton 
127bf37a037SPavel Labath   Log * log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS);
128bf37a037SPavel Labath   for(uint32_t i = 0; i < sc_list.GetSize(); ++i) {
129bf37a037SPavel Labath     SymbolContext sc;
130bf37a037SPavel Labath     sc_list.GetContextAtIndex(i, sc);
13152098cf5SGreg Clayton     if (is_relative) {
13252098cf5SGreg Clayton       // If the path was relative, make sure any matches match as long as the
13352098cf5SGreg Clayton       // relative parts of the path match the path from support files
13452098cf5SGreg Clayton       auto sc_dir = sc.line_entry.file.GetDirectory().GetStringRef();
13552098cf5SGreg Clayton       if (!sc_dir.endswith(relative_path)) {
13652098cf5SGreg Clayton         // We had a relative path specified and the relative directory
13752098cf5SGreg Clayton         // doesn't match so remove this one
13852098cf5SGreg Clayton         LLDB_LOG(log, "removing not matching relative path {0} since it "
13952098cf5SGreg Clayton                 "doesn't end with {1}", sc_dir, relative_path);
14052098cf5SGreg Clayton         sc_list.RemoveContextAtIndex(i);
14152098cf5SGreg Clayton         --i;
14252098cf5SGreg Clayton         continue;
14352098cf5SGreg Clayton       }
14452098cf5SGreg Clayton     }
14552098cf5SGreg Clayton 
146bf37a037SPavel Labath     if (!sc.block)
147bf37a037SPavel Labath       continue;
148bf37a037SPavel Labath 
149bf37a037SPavel Labath     FileSpec file;
150bf37a037SPavel Labath     uint32_t line;
151bf37a037SPavel Labath     const Block *inline_block = sc.block->GetContainingInlinedBlock();
152bf37a037SPavel Labath     if (inline_block) {
153bf37a037SPavel Labath       const Declaration &inline_declaration = inline_block->GetInlinedFunctionInfo()->GetDeclaration();
154bf37a037SPavel Labath       if (!inline_declaration.IsValid())
155bf37a037SPavel Labath         continue;
156bf37a037SPavel Labath       file = inline_declaration.GetFile();
157bf37a037SPavel Labath       line = inline_declaration.GetLine();
158bf37a037SPavel Labath     } else if (sc.function)
159bf37a037SPavel Labath       sc.function->GetStartLineSourceInfo(file, line);
160bf37a037SPavel Labath     else
161bf37a037SPavel Labath       continue;
162bf37a037SPavel Labath 
163bf37a037SPavel Labath     if (file != sc.line_entry.file) {
164bf37a037SPavel Labath       LLDB_LOG(log, "unexpected symbol context file {0}", sc.line_entry.file);
165bf37a037SPavel Labath       continue;
166bf37a037SPavel Labath     }
167bf37a037SPavel Labath 
168bf37a037SPavel Labath     // Compare the requested line number with the line of the function
169bf37a037SPavel Labath     // declaration. In case of a function declared as:
170bf37a037SPavel Labath     //
171bf37a037SPavel Labath     // int
172bf37a037SPavel Labath     // foo()
173bf37a037SPavel Labath     // {
174bf37a037SPavel Labath     //   ...
175bf37a037SPavel Labath     //
176bf37a037SPavel Labath     // the compiler will set the declaration line to the "foo" line, which is
177bf37a037SPavel Labath     // the reason why we have -1 here. This can fail in case of two inline
178bf37a037SPavel Labath     // functions defined back-to-back:
179bf37a037SPavel Labath     //
180bf37a037SPavel Labath     // inline int foo1() { ... }
181bf37a037SPavel Labath     // inline int foo2() { ... }
182bf37a037SPavel Labath     //
183bf37a037SPavel Labath     // but that's the best we can do for now.
184bf37a037SPavel Labath     const int decl_line_is_too_late_fudge = 1;
185bf37a037SPavel Labath     if (m_line_number < line - decl_line_is_too_late_fudge) {
186bf37a037SPavel Labath       LLDB_LOG(log, "removing symbol context at {0}:{1}", file, line);
187bf37a037SPavel Labath       sc_list.RemoveContextAtIndex(i);
188bf37a037SPavel Labath       --i;
189bf37a037SPavel Labath     }
190bf37a037SPavel Labath   }
191bf37a037SPavel Labath }
192bf37a037SPavel Labath 
19330fdc8d8SChris Lattner Searcher::CallbackReturn
194b9c1b51eSKate Stone BreakpointResolverFileLine::SearchCallback(SearchFilter &filter,
19530fdc8d8SChris Lattner                                            SymbolContext &context,
196b9c1b51eSKate Stone                                            Address *addr, bool containing) {
19730fdc8d8SChris Lattner   SymbolContextList sc_list;
19830fdc8d8SChris Lattner 
19930fdc8d8SChris Lattner   assert(m_breakpoint != NULL);
20030fdc8d8SChris Lattner 
201b9c1b51eSKate Stone   // There is a tricky bit here.  You can have two compilation units that
202bf37a037SPavel Labath   // #include the same file, and in one of them the function at m_line_number is
203bf37a037SPavel Labath   // used (and so code and a line entry for it is generated) but in the other it
204bf37a037SPavel Labath   // isn't.  If we considered the CU's independently, then in the second
205bf37a037SPavel Labath   // inclusion, we'd move the breakpoint to the next function that actually
206bf37a037SPavel Labath   // generated code in the header file.  That would end up being confusing.  So
207bf37a037SPavel Labath   // instead, we do the CU iterations by hand here, then scan through the
208bf37a037SPavel Labath   // complete list of matches, and figure out the closest line number match, and
209bf37a037SPavel Labath   // only set breakpoints on that match.
210bc2f9182SJim Ingham 
211b9c1b51eSKate Stone   // Note also that if file_spec only had a file name and not a directory, there
212bf37a037SPavel Labath   // may be many different file spec's in the resultant list.  The closest line
213bf37a037SPavel Labath   // match for one will not be right for some totally different file.  So we go
214bf37a037SPavel Labath   // through the match list and pull out the sets that have the same file spec
215bf37a037SPavel Labath   // in their line_entry and treat each set separately.
216bc2f9182SJim Ingham 
21752098cf5SGreg Clayton   FileSpec search_file_spec = m_file_spec;
21852098cf5SGreg Clayton   const bool is_relative = m_file_spec.IsRelative();
21952098cf5SGreg Clayton   if (is_relative)
22052098cf5SGreg Clayton     search_file_spec.GetDirectory().Clear();
22152098cf5SGreg Clayton 
222c7bece56SGreg Clayton   const size_t num_comp_units = context.module_sp->GetNumCompileUnits();
223b9c1b51eSKate Stone   for (size_t i = 0; i < num_comp_units; i++) {
224bc2f9182SJim Ingham     CompUnitSP cu_sp(context.module_sp->GetCompileUnitAtIndex(i));
225b9c1b51eSKate Stone     if (cu_sp) {
2262dafd8edSGreg Clayton       if (filter.CompUnitPasses(*cu_sp))
22752098cf5SGreg Clayton         cu_sp->ResolveSymbolContext(search_file_spec, m_line_number, m_inlines,
228b9c1b51eSKate Stone                                     m_exact_match, eSymbolContextEverything,
229b9c1b51eSKate Stone                                     sc_list);
230bc2f9182SJim Ingham     }
2312dafd8edSGreg Clayton   }
232bf37a037SPavel Labath 
23352098cf5SGreg Clayton   FilterContexts(sc_list, is_relative);
234bf37a037SPavel Labath 
23530fdc8d8SChris Lattner   StreamString s;
236b9c1b51eSKate Stone   s.Printf("for %s:%d ", m_file_spec.GetFilename().AsCString("<Unknown>"),
237969795f1SJim Ingham            m_line_number);
238f642373cSJim Ingham 
23995eae423SZachary Turner   SetSCMatchesByLine(filter, sc_list, m_skip_prologue, s.GetString());
240bc2f9182SJim Ingham 
24130fdc8d8SChris Lattner   return Searcher::eCallbackReturnContinue;
24230fdc8d8SChris Lattner }
24330fdc8d8SChris Lattner 
244b9c1b51eSKate Stone Searcher::Depth BreakpointResolverFileLine::GetDepth() {
245bc2f9182SJim Ingham   return Searcher::eDepthModule;
24630fdc8d8SChris Lattner }
24730fdc8d8SChris Lattner 
248b9c1b51eSKate Stone void BreakpointResolverFileLine::GetDescription(Stream *s) {
249b9c1b51eSKate Stone   s->Printf("file = '%s', line = %u, exact_match = %d",
250b9c1b51eSKate Stone             m_file_spec.GetPath().c_str(), m_line_number, m_exact_match);
25130fdc8d8SChris Lattner }
25230fdc8d8SChris Lattner 
253b9c1b51eSKate Stone void BreakpointResolverFileLine::Dump(Stream *s) const {}
25430fdc8d8SChris Lattner 
25533df7cd3SJim Ingham lldb::BreakpointResolverSP
256b9c1b51eSKate Stone BreakpointResolverFileLine::CopyForBreakpoint(Breakpoint &breakpoint) {
257b9c1b51eSKate Stone   lldb::BreakpointResolverSP ret_sp(new BreakpointResolverFileLine(
258b9c1b51eSKate Stone       &breakpoint, m_file_spec, m_line_number, m_offset, m_inlines,
259b9c1b51eSKate Stone       m_skip_prologue, m_exact_match));
26033df7cd3SJim Ingham 
26133df7cd3SJim Ingham   return ret_sp;
26233df7cd3SJim Ingham }
263