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