Path: blob/main/contrib/llvm-project/lldb/source/Breakpoint/BreakpointResolver.cpp
39587 views
//===-- BreakpointResolver.cpp --------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "lldb/Breakpoint/BreakpointResolver.h"910#include "lldb/Breakpoint/Breakpoint.h"11#include "lldb/Breakpoint/BreakpointLocation.h"12// Have to include the other breakpoint resolver types here so the static13// create from StructuredData can call them.14#include "lldb/Breakpoint/BreakpointResolverAddress.h"15#include "lldb/Breakpoint/BreakpointResolverFileLine.h"16#include "lldb/Breakpoint/BreakpointResolverFileRegex.h"17#include "lldb/Breakpoint/BreakpointResolverName.h"18#include "lldb/Breakpoint/BreakpointResolverScripted.h"19#include "lldb/Core/Address.h"20#include "lldb/Core/ModuleList.h"21#include "lldb/Core/SearchFilter.h"22#include "lldb/Symbol/CompileUnit.h"23#include "lldb/Symbol/Function.h"24#include "lldb/Symbol/SymbolContext.h"25#include "lldb/Target/Language.h"26#include "lldb/Target/Target.h"27#include "lldb/Utility/LLDBLog.h"28#include "lldb/Utility/Log.h"29#include "lldb/Utility/Stream.h"30#include "lldb/Utility/StreamString.h"31#include <optional>3233using namespace lldb_private;34using namespace lldb;3536// BreakpointResolver:37const char *BreakpointResolver::g_ty_to_name[] = {"FileAndLine", "Address",38"SymbolName", "SourceRegex",39"Python", "Exception",40"Unknown"};4142const char *BreakpointResolver::g_option_names[static_cast<uint32_t>(43BreakpointResolver::OptionNames::LastOptionName)] = {44"AddressOffset", "Exact", "FileName", "Inlines", "Language",45"LineNumber", "Column", "ModuleName", "NameMask", "Offset",46"PythonClass", "Regex", "ScriptArgs", "SectionName", "SearchDepth",47"SkipPrologue", "SymbolNames"};4849const char *BreakpointResolver::ResolverTyToName(enum ResolverTy type) {50if (type > LastKnownResolverType)51return g_ty_to_name[UnknownResolver];5253return g_ty_to_name[type];54}5556BreakpointResolver::ResolverTy57BreakpointResolver::NameToResolverTy(llvm::StringRef name) {58for (size_t i = 0; i < LastKnownResolverType; i++) {59if (name == g_ty_to_name[i])60return (ResolverTy)i;61}62return UnknownResolver;63}6465BreakpointResolver::BreakpointResolver(const BreakpointSP &bkpt,66const unsigned char resolverTy,67lldb::addr_t offset)68: m_breakpoint(bkpt), m_offset(offset), SubclassID(resolverTy) {}6970BreakpointResolver::~BreakpointResolver() = default;7172BreakpointResolverSP BreakpointResolver::CreateFromStructuredData(73const StructuredData::Dictionary &resolver_dict, Status &error) {74BreakpointResolverSP result_sp;75if (!resolver_dict.IsValid()) {76error.SetErrorString("Can't deserialize from an invalid data object.");77return result_sp;78}7980llvm::StringRef subclass_name;8182bool success = resolver_dict.GetValueForKeyAsString(83GetSerializationSubclassKey(), subclass_name);8485if (!success) {86error.SetErrorString("Resolver data missing subclass resolver key");87return result_sp;88}8990ResolverTy resolver_type = NameToResolverTy(subclass_name);91if (resolver_type == UnknownResolver) {92error.SetErrorStringWithFormatv("Unknown resolver type: {0}.",93subclass_name);94return result_sp;95}9697StructuredData::Dictionary *subclass_options = nullptr;98success = resolver_dict.GetValueForKeyAsDictionary(99GetSerializationSubclassOptionsKey(), subclass_options);100if (!success || !subclass_options || !subclass_options->IsValid()) {101error.SetErrorString("Resolver data missing subclass options key.");102return result_sp;103}104105lldb::offset_t offset;106success = subclass_options->GetValueForKeyAsInteger(107GetKey(OptionNames::Offset), offset);108if (!success) {109error.SetErrorString("Resolver data missing offset options key.");110return result_sp;111}112113switch (resolver_type) {114case FileLineResolver:115result_sp = BreakpointResolverFileLine::CreateFromStructuredData(116*subclass_options, error);117break;118case AddressResolver:119result_sp = BreakpointResolverAddress::CreateFromStructuredData(120*subclass_options, error);121break;122case NameResolver:123result_sp = BreakpointResolverName::CreateFromStructuredData(124*subclass_options, error);125break;126case FileRegexResolver:127result_sp = BreakpointResolverFileRegex::CreateFromStructuredData(128*subclass_options, error);129break;130case PythonResolver:131result_sp = BreakpointResolverScripted::CreateFromStructuredData(132*subclass_options, error);133break;134case ExceptionResolver:135error.SetErrorString("Exception resolvers are hard.");136break;137default:138llvm_unreachable("Should never get an unresolvable resolver type.");139}140141if (error.Fail() || !result_sp)142return {};143144// Add on the global offset option:145result_sp->SetOffset(offset);146return result_sp;147}148149StructuredData::DictionarySP BreakpointResolver::WrapOptionsDict(150StructuredData::DictionarySP options_dict_sp) {151if (!options_dict_sp || !options_dict_sp->IsValid())152return StructuredData::DictionarySP();153154StructuredData::DictionarySP type_dict_sp(new StructuredData::Dictionary());155type_dict_sp->AddStringItem(GetSerializationSubclassKey(), GetResolverName());156type_dict_sp->AddItem(GetSerializationSubclassOptionsKey(), options_dict_sp);157158// Add the m_offset to the dictionary:159options_dict_sp->AddIntegerItem(GetKey(OptionNames::Offset), m_offset);160161return type_dict_sp;162}163164void BreakpointResolver::SetBreakpoint(const BreakpointSP &bkpt) {165assert(bkpt);166m_breakpoint = bkpt;167NotifyBreakpointSet();168}169170void BreakpointResolver::ResolveBreakpointInModules(SearchFilter &filter,171ModuleList &modules) {172filter.SearchInModuleList(*this, modules);173}174175void BreakpointResolver::ResolveBreakpoint(SearchFilter &filter) {176filter.Search(*this);177}178179namespace {180struct SourceLoc {181uint32_t line = UINT32_MAX;182uint16_t column;183SourceLoc(uint32_t l, std::optional<uint16_t> c)184: line(l), column(c ? *c : LLDB_INVALID_COLUMN_NUMBER) {}185SourceLoc(const SymbolContext &sc)186: line(sc.line_entry.line),187column(sc.line_entry.column ? sc.line_entry.column188: LLDB_INVALID_COLUMN_NUMBER) {}189};190191bool operator<(const SourceLoc lhs, const SourceLoc rhs) {192if (lhs.line < rhs.line)193return true;194if (lhs.line > rhs.line)195return false;196// uint32_t a_col = lhs.column ? lhs.column : LLDB_INVALID_COLUMN_NUMBER;197// uint32_t b_col = rhs.column ? rhs.column : LLDB_INVALID_COLUMN_NUMBER;198return lhs.column < rhs.column;199}200} // namespace201202void BreakpointResolver::SetSCMatchesByLine(203SearchFilter &filter, SymbolContextList &sc_list, bool skip_prologue,204llvm::StringRef log_ident, uint32_t line, std::optional<uint16_t> column) {205llvm::SmallVector<SymbolContext, 16> all_scs;206207for (const auto &sc : sc_list) {208if (Language::GetGlobalLanguageProperties()209.GetEnableFilterForLineBreakpoints())210if (Language *lang = Language::FindPlugin(sc.GetLanguage());211lang && lang->IgnoreForLineBreakpoints(sc))212continue;213all_scs.push_back(sc);214}215216while (all_scs.size()) {217uint32_t closest_line = UINT32_MAX;218219// Move all the elements with a matching file spec to the end.220auto &match = all_scs[0];221auto worklist_begin = std::partition(222all_scs.begin(), all_scs.end(), [&](const SymbolContext &sc) {223if (sc.line_entry.GetFile() == match.line_entry.GetFile() ||224sc.line_entry.original_file_sp->Equal(225*match.line_entry.original_file_sp,226SupportFile::eEqualFileSpecAndChecksumIfSet)) {227// When a match is found, keep track of the smallest line number.228closest_line = std::min(closest_line, sc.line_entry.line);229return false;230}231return true;232});233234// (worklist_begin, worklist_end) now contains all entries for one filespec.235auto worklist_end = all_scs.end();236237if (column) {238// If a column was requested, do a more precise match and only239// return the first location that comes before or at the240// requested location.241SourceLoc requested(line, *column);242// First, filter out all entries left of the requested column.243worklist_end = std::remove_if(244worklist_begin, worklist_end,245[&](const SymbolContext &sc) { return requested < SourceLoc(sc); });246// Sort the remaining entries by (line, column).247llvm::sort(worklist_begin, worklist_end,248[](const SymbolContext &a, const SymbolContext &b) {249return SourceLoc(a) < SourceLoc(b);250});251252// Filter out all locations with a source location after the closest match.253if (worklist_begin != worklist_end)254worklist_end = std::remove_if(255worklist_begin, worklist_end, [&](const SymbolContext &sc) {256return SourceLoc(*worklist_begin) < SourceLoc(sc);257});258} else {259// Remove all entries with a larger line number.260// ResolveSymbolContext will always return a number that is >=261// the line number you pass in. So the smaller line number is262// always better.263worklist_end = std::remove_if(worklist_begin, worklist_end,264[&](const SymbolContext &sc) {265return closest_line != sc.line_entry.line;266});267}268269// Sort by file address.270llvm::sort(worklist_begin, worklist_end,271[](const SymbolContext &a, const SymbolContext &b) {272return a.line_entry.range.GetBaseAddress().GetFileAddress() <273b.line_entry.range.GetBaseAddress().GetFileAddress();274});275276// Go through and see if there are line table entries that are277// contiguous, and if so keep only the first of the contiguous range.278// We do this by picking the first location in each lexical block.279llvm::SmallDenseSet<Block *, 8> blocks_with_breakpoints;280for (auto first = worklist_begin; first != worklist_end; ++first) {281assert(!blocks_with_breakpoints.count(first->block));282blocks_with_breakpoints.insert(first->block);283worklist_end =284std::remove_if(std::next(first), worklist_end,285[&](const SymbolContext &sc) {286return blocks_with_breakpoints.count(sc.block);287});288}289290// Make breakpoints out of the closest line number match.291for (auto &sc : llvm::make_range(worklist_begin, worklist_end))292AddLocation(filter, sc, skip_prologue, log_ident);293294// Remove all contexts processed by this iteration.295all_scs.erase(worklist_begin, all_scs.end());296}297}298299void BreakpointResolver::AddLocation(SearchFilter &filter,300const SymbolContext &sc,301bool skip_prologue,302llvm::StringRef log_ident) {303Log *log = GetLog(LLDBLog::Breakpoints);304Address line_start = sc.line_entry.range.GetBaseAddress();305if (!line_start.IsValid()) {306LLDB_LOGF(log,307"error: Unable to set breakpoint %s at file address "308"0x%" PRIx64 "\n",309log_ident.str().c_str(), line_start.GetFileAddress());310return;311}312313if (!filter.AddressPasses(line_start)) {314LLDB_LOGF(log,315"Breakpoint %s at file address 0x%" PRIx64316" didn't pass the filter.\n",317log_ident.str().c_str(), line_start.GetFileAddress());318}319320// If the line number is before the prologue end, move it there...321bool skipped_prologue = false;322if (skip_prologue && sc.function) {323Address prologue_addr(sc.function->GetAddressRange().GetBaseAddress());324if (prologue_addr.IsValid() && (line_start == prologue_addr)) {325const uint32_t prologue_byte_size = sc.function->GetPrologueByteSize();326if (prologue_byte_size) {327prologue_addr.Slide(prologue_byte_size);328329if (filter.AddressPasses(prologue_addr)) {330skipped_prologue = true;331line_start = prologue_addr;332}333}334}335}336337BreakpointLocationSP bp_loc_sp(AddLocation(line_start));338if (log && bp_loc_sp && !GetBreakpoint()->IsInternal()) {339StreamString s;340bp_loc_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);341LLDB_LOGF(log, "Added location (skipped prologue: %s): %s \n",342skipped_prologue ? "yes" : "no", s.GetData());343}344}345346BreakpointLocationSP BreakpointResolver::AddLocation(Address loc_addr,347bool *new_location) {348loc_addr.Slide(m_offset);349return GetBreakpoint()->AddLocation(loc_addr, new_location);350}351352void BreakpointResolver::SetOffset(lldb::addr_t offset) {353// There may already be an offset, so we are actually adjusting location354// addresses by the difference.355// lldb::addr_t slide = offset - m_offset;356// FIXME: We should go fix up all the already set locations for the new357// slide.358359m_offset = offset;360}361362363