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