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