LLDB  mainline
BreakpointResolverFileLine.cpp
Go to the documentation of this file.
1 //===-- BreakpointResolverFileLine.cpp ------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
10 
12 #include "lldb/Core/Module.h"
14 #include "lldb/Symbol/Function.h"
15 #include "lldb/Utility/Log.h"
17 
18 using namespace lldb;
19 using namespace lldb_private;
20 
21 // BreakpointResolverFileLine:
22 BreakpointResolverFileLine::BreakpointResolverFileLine(
23  const BreakpointSP &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)
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) {}
30 
32  const BreakpointSP &bkpt, const StructuredData::Dictionary &options_dict,
33  Status &error) {
34  llvm::StringRef filename;
35  uint32_t line_no;
36  uint32_t column;
37  bool check_inlines;
38  bool skip_prologue;
39  bool exact_match;
40  bool success;
41 
42  lldb::addr_t offset = 0;
43 
44  success = options_dict.GetValueForKeyAsString(GetKey(OptionNames::FileName),
45  filename);
46  if (!success) {
47  error.SetErrorString("BRFL::CFSD: Couldn't find filename entry.");
48  return nullptr;
49  }
50 
51  success = options_dict.GetValueForKeyAsInteger(
53  if (!success) {
54  error.SetErrorString("BRFL::CFSD: Couldn't find line number entry.");
55  return nullptr;
56  }
57 
58  success =
59  options_dict.GetValueForKeyAsInteger(GetKey(OptionNames::Column), column);
60  if (!success) {
61  // Backwards compatibility.
62  column = 0;
63  }
64 
65  success = options_dict.GetValueForKeyAsBoolean(GetKey(OptionNames::Inlines),
66  check_inlines);
67  if (!success) {
68  error.SetErrorString("BRFL::CFSD: Couldn't find check inlines entry.");
69  return nullptr;
70  }
71 
72  success = options_dict.GetValueForKeyAsBoolean(
73  GetKey(OptionNames::SkipPrologue), skip_prologue);
74  if (!success) {
75  error.SetErrorString("BRFL::CFSD: Couldn't find skip prologue entry.");
76  return nullptr;
77  }
78 
79  success = options_dict.GetValueForKeyAsBoolean(
80  GetKey(OptionNames::ExactMatch), exact_match);
81  if (!success) {
82  error.SetErrorString("BRFL::CFSD: Couldn't find exact match entry.");
83  return nullptr;
84  }
85 
86  FileSpec file_spec(filename);
87 
88  return new BreakpointResolverFileLine(bkpt, file_spec, line_no, column,
89  offset, check_inlines, skip_prologue,
90  exact_match);
91 }
92 
95  StructuredData::DictionarySP options_dict_sp(
97 
98  options_dict_sp->AddStringItem(GetKey(OptionNames::FileName),
99  m_file_spec.GetPath());
100  options_dict_sp->AddIntegerItem(GetKey(OptionNames::LineNumber),
101  m_line_number);
102  options_dict_sp->AddIntegerItem(GetKey(OptionNames::Column),
103  m_column);
104  options_dict_sp->AddBooleanItem(GetKey(OptionNames::Inlines), m_inlines);
105  options_dict_sp->AddBooleanItem(GetKey(OptionNames::SkipPrologue),
106  m_skip_prologue);
107  options_dict_sp->AddBooleanItem(GetKey(OptionNames::ExactMatch),
108  m_exact_match);
109 
110  return WrapOptionsDict(options_dict_sp);
111 }
112 
113 // Filter the symbol context list to remove contexts where the line number was
114 // moved into a new function. We do this conservatively, so if e.g. we cannot
115 // resolve the function in the context (which can happen in case of line-table-
116 // only debug info), we leave the context as is. The trickiest part here is
117 // handling inlined functions -- in this case we need to make sure we look at
118 // the declaration line of the inlined function, NOT the function it was
119 // inlined into.
121  bool is_relative) {
122  if (m_exact_match)
123  return; // Nothing to do. Contexts are precise.
124 
125  llvm::StringRef relative_path;
126  if (is_relative)
127  relative_path = m_file_spec.GetDirectory().GetStringRef();
128 
130  for(uint32_t i = 0; i < sc_list.GetSize(); ++i) {
131  SymbolContext sc;
132  sc_list.GetContextAtIndex(i, sc);
133  if (is_relative) {
134  // If the path was relative, make sure any matches match as long as the
135  // relative parts of the path match the path from support files
136  auto sc_dir = sc.line_entry.file.GetDirectory().GetStringRef();
137  if (!sc_dir.endswith(relative_path)) {
138  // We had a relative path specified and the relative directory doesn't
139  // match so remove this one
140  LLDB_LOG(log, "removing not matching relative path {0} since it "
141  "doesn't end with {1}", sc_dir, relative_path);
142  sc_list.RemoveContextAtIndex(i);
143  --i;
144  continue;
145  }
146  }
147 
148  if (!sc.block)
149  continue;
150 
151  FileSpec file;
152  uint32_t line;
153  const Block *inline_block = sc.block->GetContainingInlinedBlock();
154  if (inline_block) {
155  const Declaration &inline_declaration = inline_block->GetInlinedFunctionInfo()->GetDeclaration();
156  if (!inline_declaration.IsValid())
157  continue;
158  file = inline_declaration.GetFile();
159  line = inline_declaration.GetLine();
160  } else if (sc.function)
161  sc.function->GetStartLineSourceInfo(file, line);
162  else
163  continue;
164 
165  if (file != sc.line_entry.file) {
166  LLDB_LOG(log, "unexpected symbol context file {0}", sc.line_entry.file);
167  continue;
168  }
169 
170  // Compare the requested line number with the line of the function
171  // declaration. In case of a function declared as:
172  //
173  // int
174  // foo()
175  // {
176  // ...
177  //
178  // the compiler will set the declaration line to the "foo" line, which is
179  // the reason why we have -1 here. This can fail in case of two inline
180  // functions defined back-to-back:
181  //
182  // inline int foo1() { ... }
183  // inline int foo2() { ... }
184  //
185  // but that's the best we can do for now.
186  // One complication, if the line number returned from GetStartLineSourceInfo
187  // is 0, then we can't do this calculation. That can happen if
188  // GetStartLineSourceInfo gets an error, or if the first line number in
189  // the function really is 0 - which happens for some languages.
190  const int decl_line_is_too_late_fudge = 1;
191  if (line && m_line_number < line - decl_line_is_too_late_fudge) {
192  LLDB_LOG(log, "removing symbol context at {0}:{1}", file, line);
193  sc_list.RemoveContextAtIndex(i);
194  --i;
195  }
196  }
197 }
198 
200  SearchFilter &filter, SymbolContext &context, Address *addr) {
201  SymbolContextList sc_list;
202 
203  // There is a tricky bit here. You can have two compilation units that
204  // #include the same file, and in one of them the function at m_line_number
205  // is used (and so code and a line entry for it is generated) but in the
206  // other it isn't. If we considered the CU's independently, then in the
207  // second inclusion, we'd move the breakpoint to the next function that
208  // actually generated code in the header file. That would end up being
209  // confusing. So instead, we do the CU iterations by hand here, then scan
210  // through the complete list of matches, and figure out the closest line
211  // number match, and only set breakpoints on that match.
212 
213  // Note also that if file_spec only had a file name and not a directory,
214  // there may be many different file spec's in the resultant list. The
215  // closest line match for one will not be right for some totally different
216  // file. So we go through the match list and pull out the sets that have the
217  // same file spec in their line_entry and treat each set separately.
218 
219  FileSpec search_file_spec = m_file_spec;
220  const bool is_relative = m_file_spec.IsRelative();
221  if (is_relative)
222  search_file_spec.GetDirectory().Clear();
223 
224  const size_t num_comp_units = context.module_sp->GetNumCompileUnits();
225  for (size_t i = 0; i < num_comp_units; i++) {
226  CompUnitSP cu_sp(context.module_sp->GetCompileUnitAtIndex(i));
227  if (cu_sp) {
228  if (filter.CompUnitPasses(*cu_sp))
229  cu_sp->ResolveSymbolContext(search_file_spec, m_line_number, m_inlines,
230  m_exact_match, eSymbolContextEverything,
231  sc_list);
232  }
233  }
234 
235  FilterContexts(sc_list, is_relative);
236 
237  StreamString s;
238  s.Printf("for %s:%d ", m_file_spec.GetFilename().AsCString("<Unknown>"),
239  m_line_number);
240 
241  SetSCMatchesByLine(filter, sc_list, m_skip_prologue, s.GetString(),
242  m_line_number, m_column);
243 
245 }
246 
249 }
250 
252  s->Printf("file = '%s', line = %u, ", m_file_spec.GetPath().c_str(),
253  m_line_number);
254  if (m_column)
255  s->Printf("column = %u, ", m_column);
256  s->Printf("exact_match = %d", m_exact_match);
257 }
258 
260 
261 lldb::BreakpointResolverSP
263  lldb::BreakpointResolverSP ret_sp(new BreakpointResolverFileLine(
264  breakpoint, m_file_spec, m_line_number, m_column, GetOffset(), m_inlines,
265  m_skip_prologue, m_exact_match));
266 
267  return ret_sp;
268 }
Defines a list of symbol context objects.
A class that represents a running process on the host machine.
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
virtual bool CompUnitPasses(FileSpec &fileSpec)
Call this method with a FileSpec to see if file spec passes the filter as the name of a compilation u...
void Dump(Stream *s) const override
Standard "Dump" method. At present it does nothing.
Defines a symbol context baton that can be handed other debug core functions.
Definition: SymbolContext.h:33
A file utility class.
Definition: FileSpec.h:56
StructuredData::ObjectSP SerializeToStructuredData() override
A class that describes a single lexical block.
Definition: Block.h:41
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition: Log.h:242
uint32_t GetSize() const
Get accessor for a symbol context list size.
Function * function
The Function for a given query.
std::shared_ptr< Dictionary > DictionarySP
static BreakpointResolver * CreateFromStructuredData(const lldb::BreakpointSP &bkpt, const StructuredData::Dictionary &data_dict, Status &error)
FileSpec & GetFile()
Get accessor for file specification.
Definition: Declaration.h:138
static llvm::raw_ostream & error(Stream &strm)
void SetSCMatchesByLine(SearchFilter &filter, SymbolContextList &sc_list, bool skip_prologue, llvm::StringRef log_ident, uint32_t line=0, uint32_t column=0)
Takes a symbol context list of matches which supposedly represent the same file and line number in a ...
Log * GetLogIfAllCategoriesSet(uint32_t mask)
Definition: Logging.cpp:58
General Outline: The BreakpointResolver is a Searcher.
#define LIBLLDB_LOG_BREAKPOINTS
Definition: Logging.h:19
lldb::BreakpointResolverSP CopyForBreakpoint(lldb::BreakpointSP &breakpoint) override
void GetStartLineSourceInfo(FileSpec &source_file, uint32_t &line_no)
Find the file and line number of the source location of the start of the function.
Definition: Function.cpp:243
Block * GetContainingInlinedBlock()
Get the inlined block that contains this block.
Definition: Block.cpp:197
Searcher::CallbackReturn SearchCallback(SearchFilter &filter, SymbolContext &context, Address *addr) override
General Outline: Provides the callback and search depth for the SearchFilter search.
Definition: SearchFilter.h:83
llvm::StringRef GetString() const
void SetErrorString(llvm::StringRef err_str)
Set the current error string to err_str.
Definition: Status.cpp:242
FileSpec file
The source file, possibly mapped by the target.source-map setting.
Definition: LineEntry.h:140
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
Definition: ConstString.h:232
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
uint32_t GetLine() const
Get accessor for the declaration line number.
Definition: Declaration.h:151
A section + offset based address class.
Definition: Address.h:59
const InlineFunctionInfo * GetInlinedFunctionInfo() const
Get const accessor for any inlined function information.
Definition: Block.h:276
bool GetContextAtIndex(size_t idx, SymbolContext &sc) const
Get accessor for a symbol context at index idx.
bool GetValueForKeyAsInteger(llvm::StringRef key, IntType &result) const
ConstString & GetDirectory()
Directory string get accessor.
Definition: FileSpec.cpp:335
uint64_t addr_t
Definition: lldb-types.h:83
This class finds address for source file and line.
lldb::ModuleSP module_sp
The Module for a given query.
LineEntry line_entry
The LineEntry for a given query.
Definition: SBAddress.h:15
std::shared_ptr< Object > ObjectSP
bool GetValueForKeyAsBoolean(llvm::StringRef key, bool &result) const
void GetDescription(Stream *s) override
Prints a canonical description for the breakpoint to the stream s.
bool IsRelative() const
Returns true if the filespec represents a relative path.
Definition: FileSpec.cpp:475
Declaration & GetDeclaration()
Get accessor for the declaration information.
Definition: Function.cpp:51
StructuredData::DictionarySP WrapOptionsDict(StructuredData::DictionarySP options_dict_sp)
A class that describes the declaration location of a lldb object.
Definition: Declaration.h:24
Block * block
The Block for a given query.
"lldb/Breakpoint/BreakpointResolverFileLine.h" This class sets breakpoints by file and line...
static const char * GetKey(OptionNames enum_value)
void FilterContexts(SymbolContextList &sc_list, bool is_relative)
bool GetValueForKeyAsString(llvm::StringRef key, llvm::StringRef &result) const
void Clear()
Clear this object&#39;s state.
Definition: ConstString.h:262
An error handling class.
Definition: Status.h:44