180814287SRaphael Isemann //===-- BreakpointResolverFileLine.cpp ------------------------------------===//
230fdc8d8SChris Lattner //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
630fdc8d8SChris Lattner //
730fdc8d8SChris Lattner //===----------------------------------------------------------------------===//
830fdc8d8SChris Lattner 
930fdc8d8SChris Lattner #include "lldb/Breakpoint/BreakpointResolverFileLine.h"
1030fdc8d8SChris Lattner 
1130fdc8d8SChris Lattner #include "lldb/Breakpoint/BreakpointLocation.h"
121f746071SGreg Clayton #include "lldb/Core/Module.h"
131f746071SGreg Clayton #include "lldb/Symbol/CompileUnit.h"
141f746071SGreg Clayton #include "lldb/Symbol/Function.h"
156f9e6901SZachary Turner #include "lldb/Utility/Log.h"
16bf9a7730SZachary Turner #include "lldb/Utility/StreamString.h"
1730fdc8d8SChris Lattner 
1830fdc8d8SChris Lattner using namespace lldb;
1930fdc8d8SChris Lattner using namespace lldb_private;
2030fdc8d8SChris Lattner 
2130fdc8d8SChris Lattner // BreakpointResolverFileLine:
22b9c1b51eSKate Stone BreakpointResolverFileLine::BreakpointResolverFileLine(
23*3e2ed744SMed Ismail Bennani     const BreakpointSP &bkpt, lldb::addr_t offset, bool skip_prologue,
24*3e2ed744SMed Ismail Bennani     const SourceLocationSpec &location_spec)
25b9c1b51eSKate Stone     : BreakpointResolver(bkpt, BreakpointResolver::FileLineResolver, offset),
26*3e2ed744SMed Ismail Bennani       m_location_spec(location_spec), m_skip_prologue(skip_prologue) {}
2730fdc8d8SChris Lattner 
28e14dc268SJim Ingham BreakpointResolver *BreakpointResolverFileLine::CreateFromStructuredData(
296c17cc53STatyana Krasnukha     const BreakpointSP &bkpt, const StructuredData::Dictionary &options_dict,
3097206d57SZachary Turner     Status &error) {
312833321fSZachary Turner   llvm::StringRef filename;
32*3e2ed744SMed Ismail Bennani   uint32_t line;
33*3e2ed744SMed Ismail Bennani   uint16_t column;
34e14dc268SJim Ingham   bool check_inlines;
35e14dc268SJim Ingham   bool skip_prologue;
36e14dc268SJim Ingham   bool exact_match;
37e14dc268SJim Ingham   bool success;
38e14dc268SJim Ingham 
39e14dc268SJim Ingham   lldb::addr_t offset = 0;
40e14dc268SJim Ingham 
41e14dc268SJim Ingham   success = options_dict.GetValueForKeyAsString(GetKey(OptionNames::FileName),
42e14dc268SJim Ingham                                                 filename);
43e14dc268SJim Ingham   if (!success) {
44e14dc268SJim Ingham     error.SetErrorString("BRFL::CFSD: Couldn't find filename entry.");
45e14dc268SJim Ingham     return nullptr;
46e14dc268SJim Ingham   }
47e14dc268SJim Ingham 
48e14dc268SJim Ingham   success = options_dict.GetValueForKeyAsInteger(
49*3e2ed744SMed Ismail Bennani       GetKey(OptionNames::LineNumber), line);
50e14dc268SJim Ingham   if (!success) {
51e14dc268SJim Ingham     error.SetErrorString("BRFL::CFSD: Couldn't find line number entry.");
52e14dc268SJim Ingham     return nullptr;
53e14dc268SJim Ingham   }
54e14dc268SJim Ingham 
55431b1584SAdrian Prantl   success =
56431b1584SAdrian Prantl       options_dict.GetValueForKeyAsInteger(GetKey(OptionNames::Column), column);
57431b1584SAdrian Prantl   if (!success) {
58431b1584SAdrian Prantl     // Backwards compatibility.
59431b1584SAdrian Prantl     column = 0;
60431b1584SAdrian Prantl   }
61431b1584SAdrian Prantl 
62e14dc268SJim Ingham   success = options_dict.GetValueForKeyAsBoolean(GetKey(OptionNames::Inlines),
63e14dc268SJim Ingham                                                  check_inlines);
64e14dc268SJim Ingham   if (!success) {
65e14dc268SJim Ingham     error.SetErrorString("BRFL::CFSD: Couldn't find check inlines entry.");
66e14dc268SJim Ingham     return nullptr;
67e14dc268SJim Ingham   }
68e14dc268SJim Ingham 
69e14dc268SJim Ingham   success = options_dict.GetValueForKeyAsBoolean(
70e14dc268SJim Ingham       GetKey(OptionNames::SkipPrologue), skip_prologue);
71e14dc268SJim Ingham   if (!success) {
72e14dc268SJim Ingham     error.SetErrorString("BRFL::CFSD: Couldn't find skip prologue entry.");
73e14dc268SJim Ingham     return nullptr;
74e14dc268SJim Ingham   }
75e14dc268SJim Ingham 
76e14dc268SJim Ingham   success = options_dict.GetValueForKeyAsBoolean(
77e14dc268SJim Ingham       GetKey(OptionNames::ExactMatch), exact_match);
78e14dc268SJim Ingham   if (!success) {
79e14dc268SJim Ingham     error.SetErrorString("BRFL::CFSD: Couldn't find exact match entry.");
80e14dc268SJim Ingham     return nullptr;
81e14dc268SJim Ingham   }
82e14dc268SJim Ingham 
83*3e2ed744SMed Ismail Bennani   SourceLocationSpec location_spec(FileSpec(filename), line, column,
84*3e2ed744SMed Ismail Bennani                                    check_inlines, exact_match);
85*3e2ed744SMed Ismail Bennani   if (!location_spec)
86*3e2ed744SMed Ismail Bennani     return nullptr;
87e14dc268SJim Ingham 
88*3e2ed744SMed Ismail Bennani   return new BreakpointResolverFileLine(bkpt, offset, skip_prologue,
89*3e2ed744SMed Ismail Bennani                                         location_spec);
90e14dc268SJim Ingham }
91e14dc268SJim Ingham 
92e14dc268SJim Ingham StructuredData::ObjectSP
93e14dc268SJim Ingham BreakpointResolverFileLine::SerializeToStructuredData() {
94e14dc268SJim Ingham   StructuredData::DictionarySP options_dict_sp(
95e14dc268SJim Ingham       new StructuredData::Dictionary());
96e14dc268SJim Ingham 
97e14dc268SJim Ingham   options_dict_sp->AddBooleanItem(GetKey(OptionNames::SkipPrologue),
98e14dc268SJim Ingham                                   m_skip_prologue);
99*3e2ed744SMed Ismail Bennani   options_dict_sp->AddStringItem(GetKey(OptionNames::FileName),
100*3e2ed744SMed Ismail Bennani                                  m_location_spec.GetFileSpec().GetPath());
101*3e2ed744SMed Ismail Bennani   options_dict_sp->AddIntegerItem(GetKey(OptionNames::LineNumber),
102*3e2ed744SMed Ismail Bennani                                   m_location_spec.GetLine().getValueOr(0));
103*3e2ed744SMed Ismail Bennani   options_dict_sp->AddIntegerItem(
104*3e2ed744SMed Ismail Bennani       GetKey(OptionNames::Column),
105*3e2ed744SMed Ismail Bennani       m_location_spec.GetColumn().getValueOr(LLDB_INVALID_COLUMN_NUMBER));
106*3e2ed744SMed Ismail Bennani   options_dict_sp->AddBooleanItem(GetKey(OptionNames::Inlines),
107*3e2ed744SMed Ismail Bennani                                   m_location_spec.GetCheckInlines());
108e14dc268SJim Ingham   options_dict_sp->AddBooleanItem(GetKey(OptionNames::ExactMatch),
109*3e2ed744SMed Ismail Bennani                                   m_location_spec.GetExactMatch());
110e14dc268SJim Ingham 
111e14dc268SJim Ingham   return WrapOptionsDict(options_dict_sp);
112e14dc268SJim Ingham }
113e14dc268SJim Ingham 
114bf37a037SPavel Labath // Filter the symbol context list to remove contexts where the line number was
115bf37a037SPavel Labath // moved into a new function. We do this conservatively, so if e.g. we cannot
11605097246SAdrian Prantl // resolve the function in the context (which can happen in case of line-table-
11705097246SAdrian Prantl // only debug info), we leave the context as is. The trickiest part here is
11805097246SAdrian Prantl // handling inlined functions -- in this case we need to make sure we look at
11905097246SAdrian Prantl // the declaration line of the inlined function, NOT the function it was
120bf37a037SPavel Labath // inlined into.
12152098cf5SGreg Clayton void BreakpointResolverFileLine::FilterContexts(SymbolContextList &sc_list,
12252098cf5SGreg Clayton                                                 bool is_relative) {
123*3e2ed744SMed Ismail Bennani   if (m_location_spec.GetExactMatch())
124bf37a037SPavel Labath     return; // Nothing to do. Contexts are precise.
125bf37a037SPavel Labath 
12652098cf5SGreg Clayton   llvm::StringRef relative_path;
12752098cf5SGreg Clayton   if (is_relative)
128*3e2ed744SMed Ismail Bennani     relative_path = m_location_spec.GetFileSpec().GetDirectory().GetStringRef();
12952098cf5SGreg Clayton 
130bf37a037SPavel Labath   Log * log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS);
131bf37a037SPavel Labath   for(uint32_t i = 0; i < sc_list.GetSize(); ++i) {
132bf37a037SPavel Labath     SymbolContext sc;
133bf37a037SPavel Labath     sc_list.GetContextAtIndex(i, sc);
13452098cf5SGreg Clayton     if (is_relative) {
13552098cf5SGreg Clayton       // If the path was relative, make sure any matches match as long as the
13652098cf5SGreg Clayton       // relative parts of the path match the path from support files
13752098cf5SGreg Clayton       auto sc_dir = sc.line_entry.file.GetDirectory().GetStringRef();
13852098cf5SGreg Clayton       if (!sc_dir.endswith(relative_path)) {
13905097246SAdrian Prantl         // We had a relative path specified and the relative directory doesn't
14005097246SAdrian Prantl         // match so remove this one
14152098cf5SGreg Clayton         LLDB_LOG(log, "removing not matching relative path {0} since it "
14252098cf5SGreg Clayton                 "doesn't end with {1}", sc_dir, relative_path);
14352098cf5SGreg Clayton         sc_list.RemoveContextAtIndex(i);
14452098cf5SGreg Clayton         --i;
14552098cf5SGreg Clayton         continue;
14652098cf5SGreg Clayton       }
14752098cf5SGreg Clayton     }
14852098cf5SGreg Clayton 
149bf37a037SPavel Labath     if (!sc.block)
150bf37a037SPavel Labath       continue;
151bf37a037SPavel Labath 
152bf37a037SPavel Labath     FileSpec file;
153bf37a037SPavel Labath     uint32_t line;
154bf37a037SPavel Labath     const Block *inline_block = sc.block->GetContainingInlinedBlock();
155bf37a037SPavel Labath     if (inline_block) {
156bf37a037SPavel Labath       const Declaration &inline_declaration = inline_block->GetInlinedFunctionInfo()->GetDeclaration();
157bf37a037SPavel Labath       if (!inline_declaration.IsValid())
158bf37a037SPavel Labath         continue;
159bf37a037SPavel Labath       file = inline_declaration.GetFile();
160bf37a037SPavel Labath       line = inline_declaration.GetLine();
161bf37a037SPavel Labath     } else if (sc.function)
162bf37a037SPavel Labath       sc.function->GetStartLineSourceInfo(file, line);
163bf37a037SPavel Labath     else
164bf37a037SPavel Labath       continue;
165bf37a037SPavel Labath 
166bf37a037SPavel Labath     if (file != sc.line_entry.file) {
167bf37a037SPavel Labath       LLDB_LOG(log, "unexpected symbol context file {0}", sc.line_entry.file);
168bf37a037SPavel Labath       continue;
169bf37a037SPavel Labath     }
170bf37a037SPavel Labath 
171bf37a037SPavel Labath     // Compare the requested line number with the line of the function
172bf37a037SPavel Labath     // declaration. In case of a function declared as:
173bf37a037SPavel Labath     //
174bf37a037SPavel Labath     // int
175bf37a037SPavel Labath     // foo()
176bf37a037SPavel Labath     // {
177bf37a037SPavel Labath     //   ...
178bf37a037SPavel Labath     //
179bf37a037SPavel Labath     // the compiler will set the declaration line to the "foo" line, which is
180bf37a037SPavel Labath     // the reason why we have -1 here. This can fail in case of two inline
181bf37a037SPavel Labath     // functions defined back-to-back:
182bf37a037SPavel Labath     //
183bf37a037SPavel Labath     // inline int foo1() { ... }
184bf37a037SPavel Labath     // inline int foo2() { ... }
185bf37a037SPavel Labath     //
186bf37a037SPavel Labath     // but that's the best we can do for now.
1877aa4ed9bSJim Ingham     // One complication, if the line number returned from GetStartLineSourceInfo
1887aa4ed9bSJim Ingham     // is 0, then we can't do this calculation.  That can happen if
1897aa4ed9bSJim Ingham     // GetStartLineSourceInfo gets an error, or if the first line number in
1907aa4ed9bSJim Ingham     // the function really is 0 - which happens for some languages.
191bff38912SJim Ingham 
192bff38912SJim Ingham     // But only do this calculation if the line number we found in the SC
193bff38912SJim Ingham     // was different from the one requested in the source file.  If we actually
194bff38912SJim Ingham     // found an exact match it must be valid.
195bff38912SJim Ingham 
196*3e2ed744SMed Ismail Bennani     if (m_location_spec.GetLine() == sc.line_entry.line)
197bff38912SJim Ingham       continue;
198bff38912SJim Ingham 
199bf37a037SPavel Labath     const int decl_line_is_too_late_fudge = 1;
200*3e2ed744SMed Ismail Bennani     if (line &&
201*3e2ed744SMed Ismail Bennani         m_location_spec.GetLine() < line - decl_line_is_too_late_fudge) {
202bf37a037SPavel Labath       LLDB_LOG(log, "removing symbol context at {0}:{1}", file, line);
203bf37a037SPavel Labath       sc_list.RemoveContextAtIndex(i);
204bf37a037SPavel Labath       --i;
205bf37a037SPavel Labath     }
206bf37a037SPavel Labath   }
207bf37a037SPavel Labath }
208bf37a037SPavel Labath 
20995e264fcSRaphael Isemann Searcher::CallbackReturn BreakpointResolverFileLine::SearchCallback(
21095e264fcSRaphael Isemann     SearchFilter &filter, SymbolContext &context, Address *addr) {
21130fdc8d8SChris Lattner   SymbolContextList sc_list;
21230fdc8d8SChris Lattner 
213b9c1b51eSKate Stone   // There is a tricky bit here.  You can have two compilation units that
21405097246SAdrian Prantl   // #include the same file, and in one of them the function at m_line_number
21505097246SAdrian Prantl   // is used (and so code and a line entry for it is generated) but in the
21605097246SAdrian Prantl   // other it isn't.  If we considered the CU's independently, then in the
21705097246SAdrian Prantl   // second inclusion, we'd move the breakpoint to the next function that
21805097246SAdrian Prantl   // actually generated code in the header file.  That would end up being
21905097246SAdrian Prantl   // confusing.  So instead, we do the CU iterations by hand here, then scan
22005097246SAdrian Prantl   // through the complete list of matches, and figure out the closest line
22105097246SAdrian Prantl   // number match, and only set breakpoints on that match.
222bc2f9182SJim Ingham 
22305097246SAdrian Prantl   // Note also that if file_spec only had a file name and not a directory,
22405097246SAdrian Prantl   // there may be many different file spec's in the resultant list.  The
22505097246SAdrian Prantl   // closest line match for one will not be right for some totally different
22605097246SAdrian Prantl   // file.  So we go through the match list and pull out the sets that have the
22705097246SAdrian Prantl   // same file spec in their line_entry and treat each set separately.
228bc2f9182SJim Ingham 
229*3e2ed744SMed Ismail Bennani   const uint32_t line = m_location_spec.GetLine().getValueOr(0);
230*3e2ed744SMed Ismail Bennani   const llvm::Optional<uint16_t> column = m_location_spec.GetColumn();
231*3e2ed744SMed Ismail Bennani 
232*3e2ed744SMed Ismail Bennani   FileSpec search_file_spec = m_location_spec.GetFileSpec();
233*3e2ed744SMed Ismail Bennani   const bool is_relative = search_file_spec.IsRelative();
23452098cf5SGreg Clayton   if (is_relative)
23552098cf5SGreg Clayton     search_file_spec.GetDirectory().Clear();
23652098cf5SGreg Clayton 
237c7bece56SGreg Clayton   const size_t num_comp_units = context.module_sp->GetNumCompileUnits();
238b9c1b51eSKate Stone   for (size_t i = 0; i < num_comp_units; i++) {
239bc2f9182SJim Ingham     CompUnitSP cu_sp(context.module_sp->GetCompileUnitAtIndex(i));
240b9c1b51eSKate Stone     if (cu_sp) {
2412dafd8edSGreg Clayton       if (filter.CompUnitPasses(*cu_sp))
242*3e2ed744SMed Ismail Bennani         cu_sp->ResolveSymbolContext(m_location_spec, eSymbolContextEverything,
243b9c1b51eSKate Stone                                     sc_list);
244bc2f9182SJim Ingham     }
2452dafd8edSGreg Clayton   }
246bf37a037SPavel Labath 
24752098cf5SGreg Clayton   FilterContexts(sc_list, is_relative);
248bf37a037SPavel Labath 
24930fdc8d8SChris Lattner   StreamString s;
250*3e2ed744SMed Ismail Bennani   s.Printf("for %s:%d ",
251*3e2ed744SMed Ismail Bennani            m_location_spec.GetFileSpec().GetFilename().AsCString("<Unknown>"),
252*3e2ed744SMed Ismail Bennani            line);
253f642373cSJim Ingham 
254*3e2ed744SMed Ismail Bennani   SetSCMatchesByLine(filter, sc_list, m_skip_prologue, s.GetString(), line,
255*3e2ed744SMed Ismail Bennani                      column);
256bc2f9182SJim Ingham 
25730fdc8d8SChris Lattner   return Searcher::eCallbackReturnContinue;
25830fdc8d8SChris Lattner }
25930fdc8d8SChris Lattner 
2604911d36aSJim Ingham lldb::SearchDepth BreakpointResolverFileLine::GetDepth() {
2614911d36aSJim Ingham   return lldb::eSearchDepthModule;
26230fdc8d8SChris Lattner }
26330fdc8d8SChris Lattner 
264b9c1b51eSKate Stone void BreakpointResolverFileLine::GetDescription(Stream *s) {
265*3e2ed744SMed Ismail Bennani   s->Printf("file = '%s', line = %u, ",
266*3e2ed744SMed Ismail Bennani             m_location_spec.GetFileSpec().GetPath().c_str(),
267*3e2ed744SMed Ismail Bennani             m_location_spec.GetLine().getValueOr(0));
268*3e2ed744SMed Ismail Bennani   auto column = m_location_spec.GetColumn();
269*3e2ed744SMed Ismail Bennani   if (column)
270*3e2ed744SMed Ismail Bennani     s->Printf("column = %u, ", *column);
271*3e2ed744SMed Ismail Bennani   s->Printf("exact_match = %d", m_location_spec.GetExactMatch());
27230fdc8d8SChris Lattner }
27330fdc8d8SChris Lattner 
274b9c1b51eSKate Stone void BreakpointResolverFileLine::Dump(Stream *s) const {}
27530fdc8d8SChris Lattner 
27633df7cd3SJim Ingham lldb::BreakpointResolverSP
2776c17cc53STatyana Krasnukha BreakpointResolverFileLine::CopyForBreakpoint(BreakpointSP &breakpoint) {
278b9c1b51eSKate Stone   lldb::BreakpointResolverSP ret_sp(new BreakpointResolverFileLine(
279*3e2ed744SMed Ismail Bennani       breakpoint, GetOffset(), m_skip_prologue, m_location_spec));
28033df7cd3SJim Ingham 
28133df7cd3SJim Ingham   return ret_sp;
28233df7cd3SJim Ingham }
283