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