xref: /freebsd/contrib/llvm-project/lldb/source/Breakpoint/BreakpointResolver.cpp (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===-- BreakpointResolver.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 
9 #include "lldb/Breakpoint/BreakpointResolver.h"
10 
11 #include "lldb/Breakpoint/Breakpoint.h"
12 #include "lldb/Breakpoint/BreakpointLocation.h"
13 // Have to include the other breakpoint resolver types here so the static
14 // create from StructuredData can call them.
15 #include "lldb/Breakpoint/BreakpointResolverAddress.h"
16 #include "lldb/Breakpoint/BreakpointResolverFileLine.h"
17 #include "lldb/Breakpoint/BreakpointResolverFileRegex.h"
18 #include "lldb/Breakpoint/BreakpointResolverName.h"
19 #include "lldb/Breakpoint/BreakpointResolverScripted.h"
20 #include "lldb/Core/Address.h"
21 #include "lldb/Core/ModuleList.h"
22 #include "lldb/Core/SearchFilter.h"
23 #include "lldb/Symbol/CompileUnit.h"
24 #include "lldb/Symbol/Function.h"
25 #include "lldb/Symbol/SymbolContext.h"
26 #include "lldb/Target/Language.h"
27 #include "lldb/Target/Target.h"
28 #include "lldb/Utility/LLDBLog.h"
29 #include "lldb/Utility/Log.h"
30 #include "lldb/Utility/Stream.h"
31 #include "lldb/Utility/StreamString.h"
32 #include <optional>
33 
34 using namespace lldb_private;
35 using namespace lldb;
36 
37 // BreakpointResolver:
38 const char *BreakpointResolver::g_ty_to_name[] = {"FileAndLine", "Address",
39                                                   "SymbolName",  "SourceRegex",
40                                                   "Python",   "Exception",
41                                                   "Unknown"};
42 
43 const char *BreakpointResolver::g_option_names[static_cast<uint32_t>(
44     BreakpointResolver::OptionNames::LastOptionName)] = {
45     "AddressOffset", "Exact",     "FileName",     "Inlines",     "Language",
46     "LineNumber",    "Column",    "ModuleName",   "NameMask",    "Offset",
47     "PythonClass",   "Regex",     "ScriptArgs",   "SectionName", "SearchDepth",
48     "SkipPrologue",  "SymbolNames"};
49 
ResolverTyToName(enum ResolverTy type)50 const char *BreakpointResolver::ResolverTyToName(enum ResolverTy type) {
51   if (type > LastKnownResolverType)
52     return g_ty_to_name[UnknownResolver];
53 
54   return g_ty_to_name[type];
55 }
56 
57 BreakpointResolver::ResolverTy
NameToResolverTy(llvm::StringRef name)58 BreakpointResolver::NameToResolverTy(llvm::StringRef name) {
59   for (size_t i = 0; i < LastKnownResolverType; i++) {
60     if (name == g_ty_to_name[i])
61       return (ResolverTy)i;
62   }
63   return UnknownResolver;
64 }
65 
BreakpointResolver(const BreakpointSP & bkpt,const unsigned char resolverTy,lldb::addr_t offset)66 BreakpointResolver::BreakpointResolver(const BreakpointSP &bkpt,
67                                        const unsigned char resolverTy,
68                                        lldb::addr_t offset)
69     : m_breakpoint(bkpt), m_offset(offset), SubclassID(resolverTy) {}
70 
71 BreakpointResolver::~BreakpointResolver() = default;
72 
CreateFromStructuredData(const StructuredData::Dictionary & resolver_dict,Status & error)73 BreakpointResolverSP BreakpointResolver::CreateFromStructuredData(
74     const StructuredData::Dictionary &resolver_dict, Status &error) {
75   BreakpointResolverSP result_sp;
76   if (!resolver_dict.IsValid()) {
77     error = Status::FromErrorString(
78         "Can't deserialize from an invalid data object.");
79     return result_sp;
80   }
81 
82   llvm::StringRef subclass_name;
83 
84   bool success = resolver_dict.GetValueForKeyAsString(
85       GetSerializationSubclassKey(), subclass_name);
86 
87   if (!success) {
88     error =
89         Status::FromErrorString("Resolver data missing subclass resolver key");
90     return result_sp;
91   }
92 
93   ResolverTy resolver_type = NameToResolverTy(subclass_name);
94   if (resolver_type == UnknownResolver) {
95     error = Status::FromErrorStringWithFormatv("Unknown resolver type: {0}.",
96                                                subclass_name);
97     return result_sp;
98   }
99 
100   StructuredData::Dictionary *subclass_options = nullptr;
101   success = resolver_dict.GetValueForKeyAsDictionary(
102       GetSerializationSubclassOptionsKey(), subclass_options);
103   if (!success || !subclass_options || !subclass_options->IsValid()) {
104     error =
105         Status::FromErrorString("Resolver data missing subclass options key.");
106     return result_sp;
107   }
108 
109   lldb::offset_t offset;
110   success = subclass_options->GetValueForKeyAsInteger(
111       GetKey(OptionNames::Offset), offset);
112   if (!success) {
113     error =
114         Status::FromErrorString("Resolver data missing offset options key.");
115     return result_sp;
116   }
117 
118   switch (resolver_type) {
119   case FileLineResolver:
120     result_sp = BreakpointResolverFileLine::CreateFromStructuredData(
121         *subclass_options, error);
122     break;
123   case AddressResolver:
124     result_sp = BreakpointResolverAddress::CreateFromStructuredData(
125         *subclass_options, error);
126     break;
127   case NameResolver:
128     result_sp = BreakpointResolverName::CreateFromStructuredData(
129         *subclass_options, error);
130     break;
131   case FileRegexResolver:
132     result_sp = BreakpointResolverFileRegex::CreateFromStructuredData(
133         *subclass_options, error);
134     break;
135   case PythonResolver:
136     result_sp = BreakpointResolverScripted::CreateFromStructuredData(
137         *subclass_options, error);
138     break;
139   case ExceptionResolver:
140     error = Status::FromErrorString("Exception resolvers are hard.");
141     break;
142   default:
143     llvm_unreachable("Should never get an unresolvable resolver type.");
144   }
145 
146   if (error.Fail() || !result_sp)
147     return {};
148 
149   // Add on the global offset option:
150   result_sp->SetOffset(offset);
151   return result_sp;
152 }
153 
WrapOptionsDict(StructuredData::DictionarySP options_dict_sp)154 StructuredData::DictionarySP BreakpointResolver::WrapOptionsDict(
155     StructuredData::DictionarySP options_dict_sp) {
156   if (!options_dict_sp || !options_dict_sp->IsValid())
157     return StructuredData::DictionarySP();
158 
159   StructuredData::DictionarySP type_dict_sp(new StructuredData::Dictionary());
160   type_dict_sp->AddStringItem(GetSerializationSubclassKey(), GetResolverName());
161   type_dict_sp->AddItem(GetSerializationSubclassOptionsKey(), options_dict_sp);
162 
163   // Add the m_offset to the dictionary:
164   options_dict_sp->AddIntegerItem(GetKey(OptionNames::Offset), m_offset);
165 
166   return type_dict_sp;
167 }
168 
SetBreakpoint(const BreakpointSP & bkpt)169 void BreakpointResolver::SetBreakpoint(const BreakpointSP &bkpt) {
170   assert(bkpt);
171   m_breakpoint = bkpt;
172   NotifyBreakpointSet();
173 }
174 
ResolveBreakpointInModules(SearchFilter & filter,ModuleList & modules)175 void BreakpointResolver::ResolveBreakpointInModules(SearchFilter &filter,
176                                                     ModuleList &modules) {
177   filter.SearchInModuleList(*this, modules);
178 }
179 
ResolveBreakpoint(SearchFilter & filter)180 void BreakpointResolver::ResolveBreakpoint(SearchFilter &filter) {
181   filter.Search(*this);
182 }
183 
184 namespace {
185 struct SourceLoc {
186   uint32_t line = UINT32_MAX;
187   uint16_t column;
SourceLoc__anon3914934c0111::SourceLoc188   SourceLoc(uint32_t l, std::optional<uint16_t> c)
189       : line(l), column(c ? *c : LLDB_INVALID_COLUMN_NUMBER) {}
SourceLoc__anon3914934c0111::SourceLoc190   SourceLoc(const SymbolContext &sc)
191       : line(sc.line_entry.line),
192         column(sc.line_entry.column ? sc.line_entry.column
193                                     : LLDB_INVALID_COLUMN_NUMBER) {}
194 };
195 
operator <(const SourceLoc lhs,const SourceLoc rhs)196 bool operator<(const SourceLoc lhs, const SourceLoc rhs) {
197   if (lhs.line < rhs.line)
198     return true;
199   if (lhs.line > rhs.line)
200     return false;
201   //  uint32_t a_col = lhs.column ? lhs.column : LLDB_INVALID_COLUMN_NUMBER;
202   //  uint32_t b_col = rhs.column ? rhs.column : LLDB_INVALID_COLUMN_NUMBER;
203   return lhs.column < rhs.column;
204 }
205 } // namespace
206 
SetSCMatchesByLine(SearchFilter & filter,SymbolContextList & sc_list,bool skip_prologue,llvm::StringRef log_ident,uint32_t line,std::optional<uint16_t> column)207 void BreakpointResolver::SetSCMatchesByLine(
208     SearchFilter &filter, SymbolContextList &sc_list, bool skip_prologue,
209     llvm::StringRef log_ident, uint32_t line, std::optional<uint16_t> column) {
210   llvm::SmallVector<SymbolContext, 16> all_scs(sc_list.begin(), sc_list.end());
211 
212   // Let the language plugin filter `sc_list`. Because all symbol contexts in
213   // sc_list are assumed to belong to the same File, Line and CU, the code below
214   // assumes they have the same language.
215   if (!sc_list.IsEmpty() && Language::GetGlobalLanguageProperties()
216                                 .GetEnableFilterForLineBreakpoints())
217     if (Language *lang = Language::FindPlugin(sc_list[0].GetLanguage()))
218       lang->FilterForLineBreakpoints(all_scs);
219 
220   while (all_scs.size()) {
221     uint32_t closest_line = UINT32_MAX;
222 
223     // Move all the elements with a matching file spec to the end.
224     auto &match = all_scs[0];
225     auto worklist_begin = std::partition(
226         all_scs.begin(), all_scs.end(), [&](const SymbolContext &sc) {
227           if (sc.line_entry.GetFile() == match.line_entry.GetFile() ||
228               sc.line_entry.original_file_sp->Equal(
229                   *match.line_entry.original_file_sp,
230                   SupportFile::eEqualFileSpecAndChecksumIfSet)) {
231             // When a match is found, keep track of the smallest line number.
232             closest_line = std::min(closest_line, sc.line_entry.line);
233             return false;
234           }
235           return true;
236         });
237 
238     // (worklist_begin, worklist_end) now contains all entries for one filespec.
239     auto worklist_end = all_scs.end();
240 
241     if (column) {
242       // If a column was requested, do a more precise match and only
243       // return the first location that comes before or at the
244       // requested location.
245       SourceLoc requested(line, *column);
246       // First, filter out all entries left of the requested column.
247       worklist_end = std::remove_if(
248           worklist_begin, worklist_end,
249           [&](const SymbolContext &sc) { return requested < SourceLoc(sc); });
250       // Sort the remaining entries by (line, column).
251       llvm::sort(worklist_begin, worklist_end,
252                  [](const SymbolContext &a, const SymbolContext &b) {
253                    return SourceLoc(a) < SourceLoc(b);
254                  });
255 
256       // Filter out all locations with a source location after the closest match.
257       if (worklist_begin != worklist_end)
258         worklist_end = std::remove_if(
259             worklist_begin, worklist_end, [&](const SymbolContext &sc) {
260               return SourceLoc(*worklist_begin) < SourceLoc(sc);
261             });
262     } else {
263       // Remove all entries with a larger line number.
264       // ResolveSymbolContext will always return a number that is >=
265       // the line number you pass in. So the smaller line number is
266       // always better.
267       worklist_end = std::remove_if(worklist_begin, worklist_end,
268                                     [&](const SymbolContext &sc) {
269                                       return closest_line != sc.line_entry.line;
270                                     });
271     }
272 
273     // Sort by file address.
274     llvm::sort(worklist_begin, worklist_end,
275                [](const SymbolContext &a, const SymbolContext &b) {
276                  return a.line_entry.range.GetBaseAddress().GetFileAddress() <
277                         b.line_entry.range.GetBaseAddress().GetFileAddress();
278                });
279 
280     // Go through and see if there are line table entries that are
281     // contiguous, and if so keep only the first of the contiguous range.
282     // We do this by picking the first location in each lexical block.
283     llvm::SmallDenseSet<Block *, 8> blocks_with_breakpoints;
284     for (auto first = worklist_begin; first != worklist_end; ++first) {
285       assert(!blocks_with_breakpoints.count(first->block));
286       blocks_with_breakpoints.insert(first->block);
287       worklist_end =
288           std::remove_if(std::next(first), worklist_end,
289                          [&](const SymbolContext &sc) {
290                            return blocks_with_breakpoints.count(sc.block);
291                          });
292     }
293 
294     // Make breakpoints out of the closest line number match.
295     for (auto &sc : llvm::make_range(worklist_begin, worklist_end))
296       AddLocation(filter, sc, skip_prologue, log_ident);
297 
298     // Remove all contexts processed by this iteration.
299     all_scs.erase(worklist_begin, all_scs.end());
300   }
301 }
302 
AddLocation(SearchFilter & filter,const SymbolContext & sc,bool skip_prologue,llvm::StringRef log_ident)303 void BreakpointResolver::AddLocation(SearchFilter &filter,
304                                      const SymbolContext &sc,
305                                      bool skip_prologue,
306                                      llvm::StringRef log_ident) {
307   Log *log = GetLog(LLDBLog::Breakpoints);
308   Address line_start = sc.line_entry.range.GetBaseAddress();
309   if (!line_start.IsValid()) {
310     LLDB_LOGF(log,
311               "error: Unable to set breakpoint %s at file address "
312               "0x%" PRIx64 "\n",
313               log_ident.str().c_str(), line_start.GetFileAddress());
314     return;
315   }
316 
317   if (!filter.AddressPasses(line_start)) {
318     LLDB_LOGF(log,
319               "Breakpoint %s at file address 0x%" PRIx64
320               " didn't pass the filter.\n",
321               log_ident.str().c_str(), line_start.GetFileAddress());
322   }
323 
324   // If the line number is before the prologue end, move it there...
325   bool skipped_prologue = false;
326   if (skip_prologue && sc.function) {
327     Address prologue_addr = sc.function->GetAddress();
328     if (prologue_addr.IsValid() && (line_start == prologue_addr)) {
329       const uint32_t prologue_byte_size = sc.function->GetPrologueByteSize();
330       if (prologue_byte_size) {
331         prologue_addr.Slide(prologue_byte_size);
332 
333         if (filter.AddressPasses(prologue_addr)) {
334           skipped_prologue = true;
335           line_start = prologue_addr;
336         }
337       }
338     }
339   }
340 
341   BreakpointLocationSP bp_loc_sp(AddLocation(line_start));
342   // If the address that we resolved the location to returns a different
343   // LineEntry from the one in the incoming SC, we're probably dealing with an
344   // inlined call site, so set that as the preferred LineEntry:
345   LineEntry resolved_entry;
346   if (!skipped_prologue && bp_loc_sp &&
347       line_start.CalculateSymbolContextLineEntry(resolved_entry) &&
348       LineEntry::Compare(resolved_entry, sc.line_entry)) {
349     // FIXME: The function name will also be wrong here.  Do we need to record
350     // that as well, or can we figure that out again when we report this
351     // breakpoint location.
352     if (!bp_loc_sp->SetPreferredLineEntry(sc.line_entry)) {
353       LLDB_LOG(log, "Tried to add a preferred line entry that didn't have the "
354                     "same address as this location's address.");
355     }
356   }
357   if (log && bp_loc_sp && !GetBreakpoint()->IsInternal()) {
358     StreamString s;
359     bp_loc_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
360     LLDB_LOGF(log, "Added location (skipped prologue: %s): %s \n",
361               skipped_prologue ? "yes" : "no", s.GetData());
362   }
363 }
364 
AddLocation(Address loc_addr,bool * new_location)365 BreakpointLocationSP BreakpointResolver::AddLocation(Address loc_addr,
366                                                      bool *new_location) {
367   loc_addr.Slide(m_offset);
368   return GetBreakpoint()->AddLocation(loc_addr, new_location);
369 }
370 
SetOffset(lldb::addr_t offset)371 void BreakpointResolver::SetOffset(lldb::addr_t offset) {
372   // There may already be an offset, so we are actually adjusting location
373   // addresses by the difference.
374   // lldb::addr_t slide = offset - m_offset;
375   // FIXME: We should go fix up all the already set locations for the new
376   // slide.
377 
378   m_offset = offset;
379 }
380