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