LLDB mainline
BreakpointResolverAddress.cpp
Go to the documentation of this file.
1//===-- BreakpointResolverAddress.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
11#include "lldb/Core/Module.h"
12#include "lldb/Core/Section.h"
13#include "lldb/Target/Process.h"
14#include "lldb/Target/Target.h"
17
18using namespace lldb;
19using namespace lldb_private;
20
21// BreakpointResolverAddress:
23 const BreakpointSP &bkpt, const Address &addr, const FileSpec &module_spec)
25 m_addr(addr), m_resolved_addr(LLDB_INVALID_ADDRESS),
26 m_module_filespec(module_spec) {}
27
29 const Address &addr)
31 m_addr(addr), m_resolved_addr(LLDB_INVALID_ADDRESS) {}
32
34 const StructuredData::Dictionary &options_dict, Status &error) {
35 llvm::StringRef module_name;
36 lldb::offset_t addr_offset;
37 FileSpec module_filespec;
38 bool success;
39
40 success = options_dict.GetValueForKeyAsInteger(
42 if (!success) {
43 error.SetErrorString("BRFL::CFSD: Couldn't find address offset entry.");
44 return nullptr;
45 }
46 Address address(addr_offset);
47
48 success = options_dict.HasKey(GetKey(OptionNames::ModuleName));
49 if (success) {
50 success = options_dict.GetValueForKeyAsString(
51 GetKey(OptionNames::ModuleName), module_name);
52 if (!success) {
53 error.SetErrorString("BRA::CFSD: Couldn't read module name entry.");
54 return nullptr;
55 }
56 module_filespec.SetFile(module_name, FileSpec::Style::native);
57 }
58 return std::make_shared<BreakpointResolverAddress>(nullptr, address,
59 module_filespec);
60}
61
64 StructuredData::DictionarySP options_dict_sp(
66 SectionSP section_sp = m_addr.GetSection();
67 if (section_sp) {
68 if (ModuleSP module_sp = section_sp->GetModule()) {
69 const FileSpec &module_fspec = module_sp->GetFileSpec();
70 options_dict_sp->AddStringItem(GetKey(OptionNames::ModuleName),
71 module_fspec.GetPath().c_str());
72 }
73 options_dict_sp->AddIntegerItem(GetKey(OptionNames::AddressOffset),
75 } else {
76 options_dict_sp->AddIntegerItem(GetKey(OptionNames::AddressOffset),
79 options_dict_sp->AddStringItem(GetKey(OptionNames::ModuleName),
81 }
82 }
83
84 return WrapOptionsDict(options_dict_sp);
85}
86
88 // If the address is not section relative, then we should not try to re-
89 // resolve it, it is just some random address and we wouldn't know what to do
90 // on reload. But if it is section relative, we need to re-resolve it since
91 // the section it's in may have shifted on re-run.
92 bool re_resolve = false;
94 re_resolve = true;
95 else if (GetBreakpoint()->GetNumLocations() == 0)
96 re_resolve = true;
97
98 if (re_resolve)
100}
101
103 SearchFilter &filter, ModuleList &modules) {
104 // See comment in ResolveBreakpoint.
105 bool re_resolve = false;
106 if (m_addr.GetSection())
107 re_resolve = true;
108 else if (GetBreakpoint()->GetNumLocations() == 0)
109 re_resolve = true;
110
111 if (re_resolve)
113}
114
116 SearchFilter &filter, SymbolContext &context, Address *addr) {
117 BreakpointSP breakpoint_sp = GetBreakpoint();
118 Breakpoint &breakpoint = *breakpoint_sp;
119
120 if (filter.AddressPasses(m_addr)) {
121 if (breakpoint.GetNumLocations() == 0) {
122 // If the address is just an offset, and we're given a module, see if we
123 // can find the appropriate module loaded in the binary, and fix up
124 // m_addr to use that.
126 Target &target = breakpoint.GetTarget();
127 ModuleSpec module_spec(m_module_filespec);
128 ModuleSP module_sp = target.GetImages().FindFirstModule(module_spec);
129 if (module_sp) {
130 Address tmp_address;
131 if (module_sp->ResolveFileAddress(m_addr.GetOffset(), tmp_address))
132 m_addr = tmp_address;
133 }
134 }
135
138 if (bp_loc_sp && !breakpoint.IsInternal()) {
139 StreamString s;
140 bp_loc_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
142 LLDB_LOGF(log, "Added location: %s\n", s.GetData());
143 }
144 } else {
145 BreakpointLocationSP loc_sp = breakpoint.GetLocationAtIndex(0);
146 lldb::addr_t cur_load_location =
147 m_addr.GetLoadAddress(&breakpoint.GetTarget());
148 if (cur_load_location != m_resolved_addr) {
149 m_resolved_addr = cur_load_location;
150 loc_sp->ClearBreakpointSite();
151 loc_sp->ResolveBreakpointSite();
152 }
153 }
154 }
156}
157
160}
161
163 s->PutCString("address = ");
164 m_addr.Dump(s, GetBreakpoint()->GetTarget().GetProcessSP().get(),
167}
168
170
174 new BreakpointResolverAddress(breakpoint, m_addr));
175 return ret_sp;
176}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOGF(log,...)
Definition: Log.h:366
General Outline: The AddressResolver is a Searcher.
A section + offset based address class.
Definition: Address.h:62
lldb::addr_t GetLoadAddress(Target *target) const
Get the load address.
Definition: Address.cpp:313
lldb::SectionSP GetSection() const
Get const accessor for the section.
Definition: Address.h:439
@ DumpStyleModuleWithFileAddress
Display as the file address with the module name prepended (if any).
Definition: Address.h:93
@ DumpStyleLoadAddress
Display as the load address (if resolved).
Definition: Address.h:99
bool Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style, DumpStyle fallback_style=DumpStyleInvalid, uint32_t addr_byte_size=UINT32_MAX, bool all_ranges=false, std::optional< Stream::HighlightSettings > settings=std::nullopt) const
Dump a description of this object to a Stream.
Definition: Address.cpp:408
lldb::addr_t GetOffset() const
Get the section relative offset value.
Definition: Address.h:329
bool IsSectionOffset() const
Check if an address is section offset.
Definition: Address.h:342
"lldb/Breakpoint/BreakpointResolverAddress.h" This class sets breakpoints on a given Address.
StructuredData::ObjectSP SerializeToStructuredData() override
Searcher::CallbackReturn SearchCallback(SearchFilter &filter, SymbolContext &context, Address *addr) override
void GetDescription(Stream *s) override
Prints a canonical description for the breakpoint to the stream s.
static lldb::BreakpointResolverSP CreateFromStructuredData(const StructuredData::Dictionary &options_dict, Status &error)
lldb::BreakpointResolverSP CopyForBreakpoint(lldb::BreakpointSP &breakpoint) override
void ResolveBreakpointInModules(SearchFilter &filter, ModuleList &modules) override
In response to this method the resolver scans the modules in the module list modules,...
void ResolveBreakpoint(SearchFilter &filter) override
In response to this method the resolver scans all the modules in the breakpoint's target,...
BreakpointResolverAddress(const lldb::BreakpointSP &bkpt, const Address &addr)
void Dump(Stream *s) const override
Standard "Dump" method. At present it does nothing.
General Outline: The BreakpointResolver is a Searcher.
lldb::BreakpointLocationSP AddLocation(Address loc_addr, bool *new_location=nullptr)
static const char * GetKey(OptionNames enum_value)
StructuredData::DictionarySP WrapOptionsDict(StructuredData::DictionarySP options_dict_sp)
lldb::BreakpointSP GetBreakpoint() const
This gets the breakpoint for this resolver.
virtual void ResolveBreakpointInModules(SearchFilter &filter, ModuleList &modules)
In response to this method the resolver scans the modules in the module list modules,...
virtual void ResolveBreakpoint(SearchFilter &filter)
In response to this method the resolver scans all the modules in the breakpoint's target,...
General Outline: A breakpoint has four main parts, a filter, a resolver, the list of breakpoint locat...
Definition: Breakpoint.h:81
lldb::BreakpointLocationSP GetLocationAtIndex(size_t index)
Get breakpoint locations by index.
Definition: Breakpoint.cpp:269
Target & GetTarget()
Accessor for the breakpoint Target.
Definition: Breakpoint.h:463
size_t GetNumLocations() const
Return the number of breakpoint locations.
Definition: Breakpoint.cpp:830
bool IsInternal() const
Tell whether this breakpoint is an "internal" breakpoint.
Definition: Breakpoint.cpp:249
A file utility class.
Definition: FileSpec.h:56
void SetFile(llvm::StringRef path, Style style)
Change the file specified with a new path.
Definition: FileSpec.cpp:174
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:367
A collection class for Module objects.
Definition: ModuleList.h:103
lldb::ModuleSP FindFirstModule(const ModuleSpec &module_spec) const
Definition: ModuleList.cpp:626
General Outline: Provides the callback and search depth for the SearchFilter search.
Definition: SearchFilter.h:83
virtual bool AddressPasses(Address &addr)
Call this method with a Address to see if address passes the filter.
An error handling class.
Definition: Status.h:44
const char * GetData() const
Definition: StreamString.h:45
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:65
bool GetValueForKeyAsInteger(llvm::StringRef key, IntType &result) const
bool GetValueForKeyAsString(llvm::StringRef key, llvm::StringRef &result) const
bool HasKey(llvm::StringRef key) const
std::shared_ptr< Dictionary > DictionarySP
std::shared_ptr< Object > ObjectSP
Defines a symbol context baton that can be handed other debug core functions.
Definition: SymbolContext.h:34
const ModuleList & GetImages() const
Get accessor for the images for this process.
Definition: Target.h:986
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:82
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:331
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::BreakpointResolver > BreakpointResolverSP
Definition: lldb-forward.h:326
std::shared_ptr< lldb_private::BreakpointLocation > BreakpointLocationSP
Definition: lldb-forward.h:322
@ eDescriptionLevelVerbose
uint64_t offset_t
Definition: lldb-types.h:85
std::shared_ptr< lldb_private::Breakpoint > BreakpointSP
Definition: lldb-forward.h:319
@ eSearchDepthTarget
std::shared_ptr< lldb_private::Section > SectionSP
Definition: lldb-forward.h:414
uint64_t addr_t
Definition: lldb-types.h:80
std::shared_ptr< lldb_private::Module > ModuleSP
Definition: lldb-forward.h:371