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