1 //===-- BreakpointResolverName.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/BreakpointResolverName.h" 10 11 #include "lldb/Breakpoint/BreakpointLocation.h" 12 #include "lldb/Core/Architecture.h" 13 #include "lldb/Core/Module.h" 14 #include "lldb/Symbol/Block.h" 15 #include "lldb/Symbol/Function.h" 16 #include "lldb/Symbol/Symbol.h" 17 #include "lldb/Symbol/SymbolContext.h" 18 #include "lldb/Target/Language.h" 19 #include "lldb/Target/Target.h" 20 #include "lldb/Utility/LLDBLog.h" 21 #include "lldb/Utility/Log.h" 22 #include "lldb/Utility/StreamString.h" 23 24 using namespace lldb; 25 using namespace lldb_private; 26 27 BreakpointResolverName::BreakpointResolverName(const BreakpointSP &bkpt, 28 const char *name_cstr, FunctionNameType name_type_mask, 29 LanguageType language, Breakpoint::MatchType type, lldb::addr_t offset, 30 bool skip_prologue) 31 : BreakpointResolver(bkpt, BreakpointResolver::NameResolver, offset), 32 m_class_name(), m_regex(), m_match_type(type), m_language(language), 33 m_skip_prologue(skip_prologue) { 34 if (m_match_type == Breakpoint::Regexp) { 35 m_regex = RegularExpression(name_cstr); 36 if (!m_regex.IsValid()) { 37 Log *log = GetLog(LLDBLog::Breakpoints); 38 39 if (log) 40 log->Warning("function name regexp: \"%s\" did not compile.", 41 name_cstr); 42 } 43 } else { 44 AddNameLookup(ConstString(name_cstr), name_type_mask); 45 } 46 } 47 48 BreakpointResolverName::BreakpointResolverName( 49 const BreakpointSP &bkpt, const char *names[], size_t num_names, 50 FunctionNameType name_type_mask, LanguageType language, lldb::addr_t offset, 51 bool skip_prologue) 52 : BreakpointResolver(bkpt, BreakpointResolver::NameResolver, offset), 53 m_match_type(Breakpoint::Exact), m_language(language), 54 m_skip_prologue(skip_prologue) { 55 for (size_t i = 0; i < num_names; i++) { 56 AddNameLookup(ConstString(names[i]), name_type_mask); 57 } 58 } 59 60 BreakpointResolverName::BreakpointResolverName(const BreakpointSP &bkpt, 61 std::vector<std::string> names, 62 FunctionNameType name_type_mask, 63 LanguageType language, 64 lldb::addr_t offset, 65 bool skip_prologue) 66 : BreakpointResolver(bkpt, BreakpointResolver::NameResolver, offset), 67 m_match_type(Breakpoint::Exact), m_language(language), 68 m_skip_prologue(skip_prologue) { 69 for (const std::string &name : names) { 70 AddNameLookup(ConstString(name.c_str(), name.size()), name_type_mask); 71 } 72 } 73 74 BreakpointResolverName::BreakpointResolverName(const BreakpointSP &bkpt, 75 RegularExpression func_regex, 76 lldb::LanguageType language, 77 lldb::addr_t offset, 78 bool skip_prologue) 79 : BreakpointResolver(bkpt, BreakpointResolver::NameResolver, offset), 80 m_class_name(nullptr), m_regex(std::move(func_regex)), 81 m_match_type(Breakpoint::Regexp), m_language(language), 82 m_skip_prologue(skip_prologue) {} 83 84 BreakpointResolverName::BreakpointResolverName( 85 const BreakpointResolverName &rhs) 86 : BreakpointResolver(rhs.GetBreakpoint(), BreakpointResolver::NameResolver, 87 rhs.GetOffset()), 88 m_lookups(rhs.m_lookups), m_class_name(rhs.m_class_name), 89 m_regex(rhs.m_regex), m_match_type(rhs.m_match_type), 90 m_language(rhs.m_language), m_skip_prologue(rhs.m_skip_prologue) {} 91 92 BreakpointResolver *BreakpointResolverName::CreateFromStructuredData( 93 const BreakpointSP &bkpt, const StructuredData::Dictionary &options_dict, 94 Status &error) { 95 LanguageType language = eLanguageTypeUnknown; 96 llvm::StringRef language_name; 97 bool success = options_dict.GetValueForKeyAsString( 98 GetKey(OptionNames::LanguageName), language_name); 99 if (success) { 100 language = Language::GetLanguageTypeFromString(language_name); 101 if (language == eLanguageTypeUnknown) { 102 error.SetErrorStringWithFormatv("BRN::CFSD: Unknown language: {0}.", 103 language_name); 104 return nullptr; 105 } 106 } 107 108 lldb::addr_t offset = 0; 109 success = 110 options_dict.GetValueForKeyAsInteger(GetKey(OptionNames::Offset), offset); 111 if (!success) { 112 error.SetErrorString("BRN::CFSD: Missing offset entry."); 113 return nullptr; 114 } 115 116 bool skip_prologue; 117 success = options_dict.GetValueForKeyAsBoolean( 118 GetKey(OptionNames::SkipPrologue), skip_prologue); 119 if (!success) { 120 error.SetErrorString("BRN::CFSD: Missing Skip prologue entry."); 121 return nullptr; 122 } 123 124 llvm::StringRef regex_text; 125 success = options_dict.GetValueForKeyAsString( 126 GetKey(OptionNames::RegexString), regex_text); 127 if (success) { 128 return new BreakpointResolverName(bkpt, RegularExpression(regex_text), 129 language, offset, skip_prologue); 130 } else { 131 StructuredData::Array *names_array; 132 success = options_dict.GetValueForKeyAsArray( 133 GetKey(OptionNames::SymbolNameArray), names_array); 134 if (!success) { 135 error.SetErrorString("BRN::CFSD: Missing symbol names entry."); 136 return nullptr; 137 } 138 StructuredData::Array *names_mask_array; 139 success = options_dict.GetValueForKeyAsArray( 140 GetKey(OptionNames::NameMaskArray), names_mask_array); 141 if (!success) { 142 error.SetErrorString("BRN::CFSD: Missing symbol names mask entry."); 143 return nullptr; 144 } 145 146 size_t num_elem = names_array->GetSize(); 147 if (num_elem != names_mask_array->GetSize()) { 148 error.SetErrorString( 149 "BRN::CFSD: names and names mask arrays have different sizes."); 150 return nullptr; 151 } 152 153 if (num_elem == 0) { 154 error.SetErrorString( 155 "BRN::CFSD: no name entry in a breakpoint by name breakpoint."); 156 return nullptr; 157 } 158 std::vector<std::string> names; 159 std::vector<FunctionNameType> name_masks; 160 for (size_t i = 0; i < num_elem; i++) { 161 llvm::StringRef name; 162 163 success = names_array->GetItemAtIndexAsString(i, name); 164 if (!success) { 165 error.SetErrorString("BRN::CFSD: name entry is not a string."); 166 return nullptr; 167 } 168 std::underlying_type<FunctionNameType>::type fnt; 169 success = names_mask_array->GetItemAtIndexAsInteger(i, fnt); 170 if (!success) { 171 error.SetErrorString("BRN::CFSD: name mask entry is not an integer."); 172 return nullptr; 173 } 174 names.push_back(std::string(name)); 175 name_masks.push_back(static_cast<FunctionNameType>(fnt)); 176 } 177 178 BreakpointResolverName *resolver = new BreakpointResolverName( 179 bkpt, names[0].c_str(), name_masks[0], language, 180 Breakpoint::MatchType::Exact, offset, skip_prologue); 181 for (size_t i = 1; i < num_elem; i++) { 182 resolver->AddNameLookup(ConstString(names[i]), name_masks[i]); 183 } 184 return resolver; 185 } 186 } 187 188 StructuredData::ObjectSP BreakpointResolverName::SerializeToStructuredData() { 189 StructuredData::DictionarySP options_dict_sp( 190 new StructuredData::Dictionary()); 191 192 if (m_regex.IsValid()) { 193 options_dict_sp->AddStringItem(GetKey(OptionNames::RegexString), 194 m_regex.GetText()); 195 } else { 196 StructuredData::ArraySP names_sp(new StructuredData::Array()); 197 StructuredData::ArraySP name_masks_sp(new StructuredData::Array()); 198 for (auto lookup : m_lookups) { 199 names_sp->AddItem(StructuredData::StringSP( 200 new StructuredData::String(lookup.GetName().GetStringRef()))); 201 name_masks_sp->AddItem(StructuredData::IntegerSP( 202 new StructuredData::Integer(lookup.GetNameTypeMask()))); 203 } 204 options_dict_sp->AddItem(GetKey(OptionNames::SymbolNameArray), names_sp); 205 options_dict_sp->AddItem(GetKey(OptionNames::NameMaskArray), name_masks_sp); 206 } 207 if (m_language != eLanguageTypeUnknown) 208 options_dict_sp->AddStringItem( 209 GetKey(OptionNames::LanguageName), 210 Language::GetNameForLanguageType(m_language)); 211 options_dict_sp->AddBooleanItem(GetKey(OptionNames::SkipPrologue), 212 m_skip_prologue); 213 214 return WrapOptionsDict(options_dict_sp); 215 } 216 217 void BreakpointResolverName::AddNameLookup(ConstString name, 218 FunctionNameType name_type_mask) { 219 220 Module::LookupInfo lookup(name, name_type_mask, m_language); 221 m_lookups.emplace_back(lookup); 222 223 auto add_variant_funcs = [&](Language *lang) { 224 for (Language::MethodNameVariant variant : 225 lang->GetMethodNameVariants(name)) { 226 // FIXME: Should we be adding variants that aren't of type Full? 227 if (variant.GetType() & lldb::eFunctionNameTypeFull) { 228 Module::LookupInfo variant_lookup(name, variant.GetType(), 229 lang->GetLanguageType()); 230 variant_lookup.SetLookupName(variant.GetName()); 231 m_lookups.emplace_back(variant_lookup); 232 } 233 } 234 return true; 235 }; 236 237 if (Language *lang = Language::FindPlugin(m_language)) { 238 add_variant_funcs(lang); 239 } else { 240 // Most likely m_language is eLanguageTypeUnknown. We check each language for 241 // possible variants or more qualified names and create lookups for those as 242 // well. 243 Language::ForEach(add_variant_funcs); 244 } 245 } 246 247 // FIXME: Right now we look at the module level, and call the module's 248 // "FindFunctions". 249 // Greg says he will add function tables, maybe at the CompileUnit level to 250 // accelerate function lookup. At that point, we should switch the depth to 251 // CompileUnit, and look in these tables. 252 253 Searcher::CallbackReturn 254 BreakpointResolverName::SearchCallback(SearchFilter &filter, 255 SymbolContext &context, Address *addr) { 256 Log *log = GetLog(LLDBLog::Breakpoints); 257 258 if (m_class_name) { 259 if (log) 260 log->Warning("Class/method function specification not supported yet.\n"); 261 return Searcher::eCallbackReturnStop; 262 } 263 264 SymbolContextList func_list; 265 bool filter_by_cu = 266 (filter.GetFilterRequiredItems() & eSymbolContextCompUnit) != 0; 267 bool filter_by_language = (m_language != eLanguageTypeUnknown); 268 269 ModuleFunctionSearchOptions function_options; 270 function_options.include_symbols = !filter_by_cu; 271 function_options.include_inlines = true; 272 273 switch (m_match_type) { 274 case Breakpoint::Exact: 275 if (context.module_sp) { 276 for (const auto &lookup : m_lookups) { 277 const size_t start_func_idx = func_list.GetSize(); 278 context.module_sp->FindFunctions( 279 lookup.GetLookupName(), CompilerDeclContext(), 280 lookup.GetNameTypeMask(), function_options, func_list); 281 282 const size_t end_func_idx = func_list.GetSize(); 283 284 if (start_func_idx < end_func_idx) 285 lookup.Prune(func_list, start_func_idx); 286 } 287 } 288 break; 289 case Breakpoint::Regexp: 290 if (context.module_sp) { 291 context.module_sp->FindFunctions(m_regex, function_options, func_list); 292 } 293 break; 294 case Breakpoint::Glob: 295 if (log) 296 log->Warning("glob is not supported yet."); 297 break; 298 } 299 300 // If the filter specifies a Compilation Unit, remove the ones that don't 301 // pass at this point. 302 if (filter_by_cu || filter_by_language) { 303 uint32_t num_functions = func_list.GetSize(); 304 305 for (size_t idx = 0; idx < num_functions; idx++) { 306 bool remove_it = false; 307 SymbolContext sc; 308 func_list.GetContextAtIndex(idx, sc); 309 if (filter_by_cu) { 310 if (!sc.comp_unit || !filter.CompUnitPasses(*sc.comp_unit)) 311 remove_it = true; 312 } 313 314 if (filter_by_language) { 315 LanguageType sym_language = sc.GetLanguage(); 316 if ((Language::GetPrimaryLanguage(sym_language) != 317 Language::GetPrimaryLanguage(m_language)) && 318 (sym_language != eLanguageTypeUnknown)) { 319 remove_it = true; 320 } 321 } 322 323 if (remove_it) { 324 func_list.RemoveContextAtIndex(idx); 325 num_functions--; 326 idx--; 327 } 328 } 329 } 330 331 BreakpointSP breakpoint_sp = GetBreakpoint(); 332 Breakpoint &breakpoint = *breakpoint_sp; 333 Address break_addr; 334 335 // Remove any duplicates between the function list and the symbol list 336 SymbolContext sc; 337 if (!func_list.GetSize()) 338 return Searcher::eCallbackReturnContinue; 339 340 for (uint32_t i = 0; i < func_list.GetSize(); i++) { 341 if (!func_list.GetContextAtIndex(i, sc)) 342 continue; 343 344 bool is_reexported = false; 345 346 if (sc.block && sc.block->GetInlinedFunctionInfo()) { 347 if (!sc.block->GetStartAddress(break_addr)) 348 break_addr.Clear(); 349 } else if (sc.function) { 350 break_addr = sc.function->GetAddressRange().GetBaseAddress(); 351 if (m_skip_prologue && break_addr.IsValid()) { 352 const uint32_t prologue_byte_size = sc.function->GetPrologueByteSize(); 353 if (prologue_byte_size) 354 break_addr.SetOffset(break_addr.GetOffset() + prologue_byte_size); 355 } 356 } else if (sc.symbol) { 357 if (sc.symbol->GetType() == eSymbolTypeReExported) { 358 const Symbol *actual_symbol = 359 sc.symbol->ResolveReExportedSymbol(breakpoint.GetTarget()); 360 if (actual_symbol) { 361 is_reexported = true; 362 break_addr = actual_symbol->GetAddress(); 363 } 364 } else { 365 break_addr = sc.symbol->GetAddress(); 366 } 367 368 if (m_skip_prologue && break_addr.IsValid()) { 369 const uint32_t prologue_byte_size = sc.symbol->GetPrologueByteSize(); 370 if (prologue_byte_size) 371 break_addr.SetOffset(break_addr.GetOffset() + prologue_byte_size); 372 else { 373 const Architecture *arch = 374 breakpoint.GetTarget().GetArchitecturePlugin(); 375 if (arch) 376 arch->AdjustBreakpointAddress(*sc.symbol, break_addr); 377 } 378 } 379 } 380 381 if (!break_addr.IsValid()) 382 continue; 383 384 if (!filter.AddressPasses(break_addr)) 385 continue; 386 387 bool new_location; 388 BreakpointLocationSP bp_loc_sp(AddLocation(break_addr, &new_location)); 389 bp_loc_sp->SetIsReExported(is_reexported); 390 if (bp_loc_sp && new_location && !breakpoint.IsInternal()) { 391 if (log) { 392 StreamString s; 393 bp_loc_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose); 394 LLDB_LOGF(log, "Added location: %s\n", s.GetData()); 395 } 396 } 397 } 398 399 return Searcher::eCallbackReturnContinue; 400 } 401 402 lldb::SearchDepth BreakpointResolverName::GetDepth() { 403 return lldb::eSearchDepthModule; 404 } 405 406 void BreakpointResolverName::GetDescription(Stream *s) { 407 if (m_match_type == Breakpoint::Regexp) 408 s->Printf("regex = '%s'", m_regex.GetText().str().c_str()); 409 else { 410 size_t num_names = m_lookups.size(); 411 if (num_names == 1) 412 s->Printf("name = '%s'", m_lookups[0].GetName().GetCString()); 413 else { 414 s->Printf("names = {"); 415 for (size_t i = 0; i < num_names; i++) { 416 s->Printf("%s'%s'", (i == 0 ? "" : ", "), 417 m_lookups[i].GetName().GetCString()); 418 } 419 s->Printf("}"); 420 } 421 } 422 if (m_language != eLanguageTypeUnknown) { 423 s->Printf(", language = %s", Language::GetNameForLanguageType(m_language)); 424 } 425 } 426 427 void BreakpointResolverName::Dump(Stream *s) const {} 428 429 lldb::BreakpointResolverSP 430 BreakpointResolverName::CopyForBreakpoint(BreakpointSP &breakpoint) { 431 lldb::BreakpointResolverSP ret_sp(new BreakpointResolverName(*this)); 432 ret_sp->SetBreakpoint(breakpoint); 433 return ret_sp; 434 } 435