1 //===-- BreakpointResolverName.cpp ------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // C Includes
11 // C++ Includes
12 // Other libraries and framework includes
13 // Project includes
14 #include "lldb/Breakpoint/BreakpointResolverName.h"
15 
16 #include "lldb/Breakpoint/BreakpointLocation.h"
17 #include "lldb/Core/Log.h"
18 #include "lldb/Core/Module.h"
19 #include "lldb/Core/StreamString.h"
20 #include "lldb/Symbol/Block.h"
21 #include "lldb/Symbol/Function.h"
22 #include "lldb/Symbol/Symbol.h"
23 #include "lldb/Symbol/SymbolContext.h"
24 #include "lldb/Target/LanguageRuntime.h"
25 #include "Plugins/Language/ObjC/ObjCLanguage.h"
26 
27 using namespace lldb;
28 using namespace lldb_private;
29 
30 BreakpointResolverName::BreakpointResolverName (Breakpoint *bkpt,
31                                                 const char *name_cstr,
32                                                 uint32_t name_type_mask,
33                                                 LanguageType language,
34                                                 Breakpoint::MatchType type,
35                                                 bool skip_prologue) :
36     BreakpointResolver (bkpt, BreakpointResolver::NameResolver),
37     m_class_name (),
38     m_regex (),
39     m_match_type (type),
40     m_language (language),
41     m_skip_prologue (skip_prologue)
42 {
43     if (m_match_type == Breakpoint::Regexp)
44     {
45         if (!m_regex.Compile (name_cstr))
46         {
47             Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
48 
49             if (log)
50                 log->Warning ("function name regexp: \"%s\" did not compile.", name_cstr);
51         }
52     }
53     else
54     {
55         AddNameLookup (ConstString(name_cstr), name_type_mask);
56     }
57 }
58 
59 BreakpointResolverName::BreakpointResolverName (Breakpoint *bkpt,
60                                                 const char *names[],
61                                                 size_t num_names,
62                                                 uint32_t name_type_mask,
63                                                 LanguageType language,
64                                                 bool skip_prologue) :
65     BreakpointResolver (bkpt, BreakpointResolver::NameResolver),
66     m_match_type (Breakpoint::Exact),
67     m_language (language),
68     m_skip_prologue (skip_prologue)
69 {
70     for (size_t i = 0; i < num_names; i++)
71     {
72         AddNameLookup (ConstString (names[i]), name_type_mask);
73     }
74 }
75 
76 BreakpointResolverName::BreakpointResolverName (Breakpoint *bkpt,
77                                                 std::vector<std::string> names,
78                                                 uint32_t name_type_mask,
79                                                 LanguageType language,
80                                                 bool skip_prologue) :
81     BreakpointResolver (bkpt, BreakpointResolver::NameResolver),
82     m_match_type (Breakpoint::Exact),
83     m_language (language),
84     m_skip_prologue (skip_prologue)
85 {
86     for (const std::string& name : names)
87     {
88         AddNameLookup (ConstString (name.c_str(), name.size()), name_type_mask);
89     }
90 }
91 
92 BreakpointResolverName::BreakpointResolverName (Breakpoint *bkpt,
93                                                 RegularExpression &func_regex,
94                                                 lldb::LanguageType language,
95                                                 bool skip_prologue) :
96     BreakpointResolver (bkpt, BreakpointResolver::NameResolver),
97     m_class_name (nullptr),
98     m_regex (func_regex),
99     m_match_type (Breakpoint::Regexp),
100     m_language (language),
101     m_skip_prologue (skip_prologue)
102 {
103 }
104 
105 BreakpointResolverName::BreakpointResolverName(Breakpoint *bkpt,
106                                                const char *class_name,
107                                                const char *method,
108                                                Breakpoint::MatchType type,
109                                                bool skip_prologue ) :
110     BreakpointResolver (bkpt, BreakpointResolver::NameResolver),
111     m_class_name (class_name),
112     m_regex (),
113     m_match_type (type),
114     m_language (eLanguageTypeUnknown),
115     m_skip_prologue (skip_prologue)
116 {
117     LookupInfo lookup;
118     lookup.name.SetCString(method);
119     lookup.lookup_name = lookup.name;
120     lookup.name_type_mask = eFunctionNameTypeMethod;
121     lookup.match_name_after_lookup = false;
122     m_lookups.push_back (lookup);
123 }
124 
125 BreakpointResolverName::~BreakpointResolverName() = default;
126 
127 BreakpointResolverName::BreakpointResolverName(const BreakpointResolverName &rhs) :
128     BreakpointResolver(rhs.m_breakpoint, BreakpointResolver::NameResolver),
129     m_lookups(rhs.m_lookups),
130     m_class_name(rhs.m_class_name),
131     m_regex(rhs.m_regex),
132     m_match_type (rhs.m_match_type),
133     m_language (rhs.m_language),
134     m_skip_prologue (rhs.m_skip_prologue)
135 {
136 }
137 
138 void
139 BreakpointResolverName::AddNameLookup (const ConstString &name, uint32_t name_type_mask)
140 {
141     ObjCLanguage::MethodName objc_method(name.GetCString(), false);
142     if (objc_method.IsValid(false))
143     {
144         std::vector<ConstString> objc_names;
145         objc_method.GetFullNames(objc_names, true);
146         for (ConstString objc_name : objc_names)
147         {
148             LookupInfo lookup;
149             lookup.name = name;
150             lookup.lookup_name = objc_name;
151             lookup.name_type_mask = eFunctionNameTypeFull;
152             lookup.match_name_after_lookup = false;
153             m_lookups.push_back (lookup);
154         }
155     }
156     else
157     {
158         LookupInfo lookup;
159         lookup.name = name;
160         Module::PrepareForFunctionNameLookup(lookup.name, name_type_mask, m_language, lookup.lookup_name, lookup.name_type_mask, lookup.match_name_after_lookup);
161         m_lookups.push_back (lookup);
162     }
163 }
164 
165 void
166 BreakpointResolverName::LookupInfo::Prune (SymbolContextList &sc_list, size_t start_idx) const
167 {
168     if (match_name_after_lookup && name)
169     {
170         SymbolContext sc;
171         size_t i = start_idx;
172         while (i < sc_list.GetSize())
173         {
174             if (!sc_list.GetContextAtIndex(i, sc))
175                 break;
176             ConstString full_name (sc.GetFunctionName());
177             if (full_name && ::strstr(full_name.GetCString(), name.GetCString()) == nullptr)
178             {
179                 sc_list.RemoveContextAtIndex(i);
180             }
181             else
182             {
183                 ++i;
184             }
185         }
186     }
187 }
188 
189 // FIXME: Right now we look at the module level, and call the module's "FindFunctions".
190 // Greg says he will add function tables, maybe at the CompileUnit level to accelerate function
191 // lookup.  At that point, we should switch the depth to CompileUnit, and look in these tables.
192 
193 Searcher::CallbackReturn
194 BreakpointResolverName::SearchCallback(SearchFilter &filter,
195                                        SymbolContext &context,
196                                        Address *addr,
197                                        bool containing)
198 {
199     SymbolContextList func_list;
200     //SymbolContextList sym_list;
201 
202     uint32_t i;
203     bool new_location;
204     Address break_addr;
205     assert (m_breakpoint != nullptr);
206 
207     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
208 
209     if (m_class_name)
210     {
211         if (log)
212             log->Warning ("Class/method function specification not supported yet.\n");
213         return Searcher::eCallbackReturnStop;
214     }
215     bool filter_by_cu = (filter.GetFilterRequiredItems() & eSymbolContextCompUnit) != 0;
216     bool filter_by_language = (m_language != eLanguageTypeUnknown);
217     const bool include_symbols = !filter_by_cu;
218     const bool include_inlines = true;
219     const bool append = true;
220 
221     switch (m_match_type)
222     {
223         case Breakpoint::Exact:
224             if (context.module_sp)
225             {
226                 for (const LookupInfo &lookup : m_lookups)
227                 {
228                     const size_t start_func_idx = func_list.GetSize();
229                     context.module_sp->FindFunctions(lookup.lookup_name,
230                                                      nullptr,
231                                                      lookup.name_type_mask,
232                                                      include_symbols,
233                                                      include_inlines,
234                                                      append,
235                                                      func_list);
236                     const size_t end_func_idx = func_list.GetSize();
237 
238                     if (start_func_idx < end_func_idx)
239                         lookup.Prune (func_list, start_func_idx);
240                 }
241             }
242             break;
243         case Breakpoint::Regexp:
244             if (context.module_sp)
245             {
246                 context.module_sp->FindFunctions (m_regex,
247                                                   !filter_by_cu, // include symbols only if we aren't filtering by CU
248                                                   include_inlines,
249                                                   append,
250                                                   func_list);
251             }
252             break;
253         case Breakpoint::Glob:
254             if (log)
255                 log->Warning ("glob is not supported yet.");
256             break;
257     }
258 
259     // If the filter specifies a Compilation Unit, remove the ones that don't pass at this point.
260     if (filter_by_cu || filter_by_language)
261     {
262         Target &target = m_breakpoint->GetTarget();
263 
264         uint32_t num_functions = func_list.GetSize();
265 
266         for (size_t idx = 0; idx < num_functions; idx++)
267         {
268             bool remove_it = false;
269             SymbolContext sc;
270             func_list.GetContextAtIndex(idx, sc);
271             if (filter_by_cu)
272             {
273                 if (!sc.comp_unit || !filter.CompUnitPasses(*sc.comp_unit))
274                     remove_it = true;
275             }
276 
277             if (filter_by_language)
278             {
279                 const char *name = sc.GetFunctionName(Mangled::ePreferMangled).AsCString();
280                 if (name)
281                 {
282                     LanguageType sym_language = LanguageRuntime::GetLanguageForSymbolByName(target, name);
283                     if (m_language == eLanguageTypeC)
284                     {
285                         // We don't currently have a way to say "This symbol name is C" so for now, C means
286                         // not ObjC and not C++, etc...
287                         if (sym_language == eLanguageTypeC_plus_plus
288                             || sym_language == eLanguageTypeObjC
289                             || sym_language == eLanguageTypeSwift)
290                         {
291                             remove_it = true;
292                         }
293                     }
294                     else if (sym_language != m_language)
295                     {
296                         remove_it = true;
297                     }
298                 }
299             }
300 
301             if  (remove_it)
302             {
303                 func_list.RemoveContextAtIndex(idx);
304                 num_functions--;
305                 idx--;
306             }
307         }
308     }
309 
310     // Remove any duplicates between the function list and the symbol list
311     SymbolContext sc;
312     if (func_list.GetSize())
313     {
314         for (i = 0; i < func_list.GetSize(); i++)
315         {
316             if (func_list.GetContextAtIndex(i, sc))
317             {
318                 bool is_reexported = false;
319 
320                 if (sc.block && sc.block->GetInlinedFunctionInfo())
321                 {
322                     if (!sc.block->GetStartAddress(break_addr))
323                         break_addr.Clear();
324                 }
325                 else if (sc.function)
326                 {
327                     break_addr = sc.function->GetAddressRange().GetBaseAddress();
328                     if (m_skip_prologue && break_addr.IsValid())
329                     {
330                         const uint32_t prologue_byte_size = sc.function->GetPrologueByteSize();
331                         if (prologue_byte_size)
332                             break_addr.SetOffset(break_addr.GetOffset() + prologue_byte_size);
333                     }
334                 }
335                 else if (sc.symbol)
336                 {
337                     if (sc.symbol->GetType() == eSymbolTypeReExported)
338                     {
339                         const Symbol *actual_symbol = sc.symbol->ResolveReExportedSymbol(m_breakpoint->GetTarget());
340                         if (actual_symbol)
341                         {
342                             is_reexported = true;
343                             break_addr = actual_symbol->GetAddress();
344                         }
345                     }
346                     else
347                     {
348                         break_addr = sc.symbol->GetAddress();
349                     }
350 
351                     if (m_skip_prologue && break_addr.IsValid())
352                     {
353                         const uint32_t prologue_byte_size = sc.symbol->GetPrologueByteSize();
354                         if (prologue_byte_size)
355                             break_addr.SetOffset(break_addr.GetOffset() + prologue_byte_size);
356                     }
357                 }
358 
359                 if (break_addr.IsValid())
360                 {
361                     if (filter.AddressPasses(break_addr))
362                     {
363                         BreakpointLocationSP bp_loc_sp (m_breakpoint->AddLocation(break_addr, &new_location));
364                         bp_loc_sp->SetIsReExported(is_reexported);
365                         if (bp_loc_sp && new_location && !m_breakpoint->IsInternal())
366                         {
367                             if (log)
368                             {
369                                 StreamString s;
370                                 bp_loc_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
371                                 log->Printf ("Added location: %s\n", s.GetData());
372                             }
373                         }
374                     }
375                 }
376             }
377         }
378     }
379 
380     return Searcher::eCallbackReturnContinue;
381 }
382 
383 Searcher::Depth
384 BreakpointResolverName::GetDepth()
385 {
386     return Searcher::eDepthModule;
387 }
388 
389 void
390 BreakpointResolverName::GetDescription (Stream *s)
391 {
392     if (m_match_type == Breakpoint::Regexp)
393         s->Printf("regex = '%s'", m_regex.GetText());
394     else
395     {
396         size_t num_names = m_lookups.size();
397         if (num_names == 1)
398             s->Printf("name = '%s'", m_lookups[0].name.GetCString());
399         else
400         {
401             s->Printf("names = {");
402             for (size_t i = 0; i < num_names - 1; i++)
403             {
404                 s->Printf ("'%s', ", m_lookups[i].name.GetCString());
405             }
406             s->Printf ("'%s'}", m_lookups[num_names - 1].name.GetCString());
407         }
408     }
409     if (m_language != eLanguageTypeUnknown)
410     {
411         s->Printf (", language = %s", Language::GetNameForLanguageType(m_language));
412     }
413 }
414 
415 void
416 BreakpointResolverName::Dump (Stream *s) const
417 {
418 }
419 
420 lldb::BreakpointResolverSP
421 BreakpointResolverName::CopyForBreakpoint (Breakpoint &breakpoint)
422 {
423     lldb::BreakpointResolverSP ret_sp(new BreakpointResolverName(*this));
424     ret_sp->SetBreakpoint(&breakpoint);
425     return ret_sp;
426 }
427