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 
10 
11 
13 #include "lldb/Core/Module.h"
14 #include "lldb/Core/Section.h"
15 #include "lldb/Target/Process.h"
16 #include "lldb/Target/Target.h"
17 #include "lldb/Utility/Log.h"
19 
20 using namespace lldb;
21 using namespace lldb_private;
22 
23 // BreakpointResolverAddress:
24 BreakpointResolverAddress::BreakpointResolverAddress(
25  const BreakpointSP &bkpt, const Address &addr, const FileSpec &module_spec)
27  m_addr(addr), m_resolved_addr(LLDB_INVALID_ADDRESS),
28  m_module_filespec(module_spec) {}
29 
31  const Address &addr)
33  m_addr(addr), m_resolved_addr(LLDB_INVALID_ADDRESS), m_module_filespec() {
34 }
35 
37  const BreakpointSP &bkpt, const StructuredData::Dictionary &options_dict,
38  Status &error) {
39  llvm::StringRef module_name;
40  lldb::addr_t addr_offset;
41  FileSpec module_filespec;
42  bool success;
43 
44  success = options_dict.GetValueForKeyAsInteger(
45  GetKey(OptionNames::AddressOffset), addr_offset);
46  if (!success) {
47  error.SetErrorString("BRFL::CFSD: Couldn't find address offset entry.");
48  return nullptr;
49  }
50  Address address(addr_offset);
51 
52  success = options_dict.HasKey(GetKey(OptionNames::ModuleName));
53  if (success) {
54  success = options_dict.GetValueForKeyAsString(
55  GetKey(OptionNames::ModuleName), module_name);
56  if (!success) {
57  error.SetErrorString("BRA::CFSD: Couldn't read module name entry.");
58  return nullptr;
59  }
60  module_filespec.SetFile(module_name, FileSpec::Style::native);
61  }
62  return new BreakpointResolverAddress(bkpt, address, module_filespec);
63 }
64 
67  StructuredData::DictionarySP options_dict_sp(
69  SectionSP section_sp = m_addr.GetSection();
70  if (section_sp) {
71  ModuleSP module_sp = section_sp->GetModule();
72  ConstString module_name;
73  if (module_sp)
74  module_name.SetCString(module_name.GetCString());
75 
76  options_dict_sp->AddStringItem(GetKey(OptionNames::ModuleName),
77  module_name.GetCString());
78  options_dict_sp->AddIntegerItem(GetKey(OptionNames::AddressOffset),
79  m_addr.GetOffset());
80  } else {
81  options_dict_sp->AddIntegerItem(GetKey(OptionNames::AddressOffset),
82  m_addr.GetOffset());
83  if (m_module_filespec) {
84  options_dict_sp->AddStringItem(GetKey(OptionNames::ModuleName),
86  }
87  }
88 
89  return WrapOptionsDict(options_dict_sp);
90 }
91 
93  // If the address is not section relative, then we should not try to re-
94  // resolve it, it is just some random address and we wouldn't know what to do
95  // on reload. But if it is section relative, we need to re-resolve it since
96  // the section it's in may have shifted on re-run.
97  bool re_resolve = false;
99  re_resolve = true;
100  else if (GetBreakpoint()->GetNumLocations() == 0)
101  re_resolve = true;
102 
103  if (re_resolve)
105 }
106 
108  SearchFilter &filter, ModuleList &modules) {
109  // See comment in ResolveBreakpoint.
110  bool re_resolve = false;
111  if (m_addr.GetSection())
112  re_resolve = true;
113  else if (GetBreakpoint()->GetNumLocations() == 0)
114  re_resolve = true;
115 
116  if (re_resolve)
118 }
119 
121  SearchFilter &filter, SymbolContext &context, Address *addr) {
122  BreakpointSP breakpoint_sp = GetBreakpoint();
123  Breakpoint &breakpoint = *breakpoint_sp;
124 
125  if (filter.AddressPasses(m_addr)) {
126  if (breakpoint.GetNumLocations() == 0) {
127  // If the address is just an offset, and we're given a module, see if we
128  // can find the appropriate module loaded in the binary, and fix up
129  // m_addr to use that.
131  Target &target = breakpoint.GetTarget();
132  ModuleSpec module_spec(m_module_filespec);
133  ModuleSP module_sp = target.GetImages().FindFirstModule(module_spec);
134  if (module_sp) {
135  Address tmp_address;
136  if (module_sp->ResolveFileAddress(m_addr.GetOffset(), tmp_address))
137  m_addr = tmp_address;
138  }
139  }
140 
142  BreakpointLocationSP bp_loc_sp(AddLocation(m_addr));
143  if (bp_loc_sp && !breakpoint.IsInternal()) {
144  StreamString s;
145  bp_loc_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
146  Log *log(
148  LLDB_LOGF(log, "Added location: %s\n", s.GetData());
149  }
150  } else {
151  BreakpointLocationSP loc_sp = breakpoint.GetLocationAtIndex(0);
152  lldb::addr_t cur_load_location =
153  m_addr.GetLoadAddress(&breakpoint.GetTarget());
154  if (cur_load_location != m_resolved_addr) {
155  m_resolved_addr = cur_load_location;
156  loc_sp->ClearBreakpointSite();
157  loc_sp->ResolveBreakpointSite();
158  }
159  }
160  }
162 }
163 
166 }
167 
169  s->PutCString("address = ");
170  m_addr.Dump(s, GetBreakpoint()->GetTarget().GetProcessSP().get(),
173 }
174 
176 
177 lldb::BreakpointResolverSP
179  lldb::BreakpointResolverSP ret_sp(
180  new BreakpointResolverAddress(breakpoint, m_addr));
181  return ret_sp;
182 }
lldb_private::BreakpointResolverAddress::BreakpointResolverAddress
BreakpointResolverAddress(const lldb::BreakpointSP &bkpt, const Address &addr)
lldb_private::BreakpointResolverAddress::CopyForBreakpoint
lldb::BreakpointResolverSP CopyForBreakpoint(lldb::BreakpointSP &breakpoint) override
Definition: BreakpointResolverAddress.cpp:178
lldb_private::StructuredData::Dictionary
Definition: StructuredData.h:352
lldb_private::BreakpointResolverAddress::ResolveBreakpointInModules
void ResolveBreakpointInModules(SearchFilter &filter, ModuleList &modules) override
In response to this method the resolver scans the modules in the module list modules,...
Definition: BreakpointResolverAddress.cpp:107
lldb_private::BreakpointResolverAddress::m_addr
Address m_addr
Definition: BreakpointResolverAddress.h:65
lldb::SearchDepth
SearchDepth
Definition: lldb-enumerations.h:283
lldb_private::SearchFilter::AddressPasses
virtual bool AddressPasses(Address &addr)
Call this method with a Address to see if address passes the filter.
Definition: SearchFilter.cpp:141
lldb::eSearchDepthTarget
@ eSearchDepthTarget
Definition: lldb-enumerations.h:285
LLDB_LOGF
#define LLDB_LOGF(log,...)
Definition: Log.h:249
Module.h
BreakpointLocation.h
lldb_private::ConstString::SetCString
void SetCString(const char *cstr)
Set the C string value.
Definition: ConstString.cpp:302
lldb_private::Stream
Definition: Stream.h:28
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
lldb_private::SymbolContext
Definition: SymbolContext.h:33
lldb_private::Target
Definition: Target.h:451
Section.h
lldb_private::Target::GetImages
const ModuleList & GetImages() const
Get accessor for the images for this process.
Definition: Target.h:925
lldb_private::BreakpointResolverAddress::CreateFromStructuredData
static BreakpointResolver * CreateFromStructuredData(const lldb::BreakpointSP &bkpt, const StructuredData::Dictionary &options_dict, Status &error)
Definition: BreakpointResolverAddress.cpp:36
Process.h
lldb_private::BreakpointResolverAddress::SerializeToStructuredData
StructuredData::ObjectSP SerializeToStructuredData() override
Definition: BreakpointResolverAddress.cpp:66
lldb_private::StructuredData::Dictionary::GetValueForKeyAsInteger
bool GetValueForKeyAsInteger(llvm::StringRef key, IntType &result) const
Definition: StructuredData.h:403
Target.h
lldb_private::FileSpec
Definition: FileSpec.h:56
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::BreakpointResolverAddress::Dump
void Dump(Stream *s) const override
Standard "Dump" method. At present it does nothing.
Definition: BreakpointResolverAddress.cpp:175
lldb_private::BreakpointResolver::ResolveBreakpointInModules
virtual void ResolveBreakpointInModules(SearchFilter &filter, ModuleList &modules)
In response to this method the resolver scans the modules in the module list modules,...
Definition: BreakpointResolver.cpp:171
lldb_private::Address::DumpStyleLoadAddress
@ DumpStyleLoadAddress
Display as the load address (if resolved).
Definition: Address.h:96
lldb_private::ModuleList
Definition: ModuleList.h:72
lldb_private::SearchFilter
General Outline: Provides the callback and search depth for the SearchFilter search.
Definition: SearchFilter.h:83
Log.h
lldb_private::BreakpointResolver::OptionNames::AddressOffset
@ AddressOffset
lldb_private::Breakpoint::GetLocationAtIndex
lldb::BreakpointLocationSP GetLocationAtIndex(size_t index)
Get breakpoint locations by index.
Definition: Breakpoint.cpp:278
lldb_private::Breakpoint::IsInternal
bool IsInternal() const
Tell whether this breakpoint is an "internal" breakpoint.
Definition: Breakpoint.cpp:258
lldb_private::StructuredData::ObjectSP
std::shared_ptr< Object > ObjectSP
Definition: StructuredData.h:59
lldb_private::StreamString::GetData
const char * GetData() const
Definition: StreamString.h:43
lldb_private::BreakpointResolverAddress::ResolveBreakpoint
void ResolveBreakpoint(SearchFilter &filter) override
In response to this method the resolver scans all the modules in the breakpoint's target,...
Definition: BreakpointResolverAddress.cpp:92
StreamString.h
lldb_private::ConstString
Definition: ConstString.h:40
lldb_private::Searcher::CallbackReturn
CallbackReturn
Definition: SearchFilter.h:44
lldb_private::StreamString
Definition: StreamString.h:23
lldb_private::ModuleList::FindFirstModule
lldb::ModuleSP FindFirstModule(const ModuleSpec &module_spec) const
Definition: ModuleList.cpp:582
lldb_private::GetLogIfAllCategoriesSet
Log * GetLogIfAllCategoriesSet(uint32_t mask)
Definition: Logging.cpp:58
lldb_private::StructuredData::DictionarySP
std::shared_ptr< Dictionary > DictionarySP
Definition: StructuredData.h:67
lldb_private::BreakpointResolverAddress::GetDepth
lldb::SearchDepth GetDepth() override
Definition: BreakpointResolverAddress.cpp:164
lldb_private::AddressResolver
General Outline: The AddressResolver is a Searcher.
Definition: AddressResolver.h:33
lldb_private::Address::GetSection
lldb::SectionSP GetSection() const
Get const accessor for the section.
Definition: Address.h:420
lldb_private::Searcher::eCallbackReturnStop
@ eCallbackReturnStop
Definition: SearchFilter.h:45
lldb_private::Address::DumpStyleModuleWithFileAddress
@ DumpStyleModuleWithFileAddress
Display as the file address with the module name prepended (if any).
Definition: Address.h:90
lldb_private::BreakpointResolver::AddLocation
lldb::BreakpointLocationSP AddLocation(Address loc_addr, bool *new_location=nullptr)
Definition: BreakpointResolver.cpp:338
lldb_private::BreakpointResolverAddress::m_module_filespec
FileSpec m_module_filespec
Definition: BreakpointResolverAddress.h:69
lldb_private::BreakpointResolver
General Outline: The BreakpointResolver is a Searcher.
Definition: BreakpointResolver.h:35
lldb_private::BreakpointResolver::WrapOptionsDict
StructuredData::DictionarySP WrapOptionsDict(StructuredData::DictionarySP options_dict_sp)
Definition: BreakpointResolver.cpp:150
lldb_private::ModuleSpec
Definition: ModuleSpec.h:26
lldb_private::BreakpointResolver::OptionNames::ModuleName
@ ModuleName
lldb_private::Status
Definition: Status.h:44
LIBLLDB_LOG_BREAKPOINTS
#define LIBLLDB_LOG_BREAKPOINTS
Definition: Logging.h:19
lldb_private::Address
Definition: Address.h:59
lldb_private::BreakpointResolver::GetKey
static const char * GetKey(OptionNames enum_value)
Definition: BreakpointResolver.h:193
lldb_private::BreakpointResolverAddress::GetDescription
void GetDescription(Stream *s) override
Prints a canonical description for the breakpoint to the stream s.
Definition: BreakpointResolverAddress.cpp:168
lldb_private::Address::GetLoadAddress
lldb::addr_t GetLoadAddress(Target *target) const
Get the load address.
Definition: Address.cpp:310
lldb_private::StructuredData::Dictionary::HasKey
bool HasKey(llvm::StringRef key) const
Definition: StructuredData.h:488
lldb_private::Address::Dump
bool Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style, DumpStyle fallback_style=DumpStyleInvalid, uint32_t addr_byte_size=UINT32_MAX) const
Dump a description of this object to a Stream.
Definition: Address.cpp:405
LLDB_INVALID_ADDRESS
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:86
lldb_private::ConstString::GetCString
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:216
lldb_private::BreakpointResolver::ResolveBreakpoint
virtual void ResolveBreakpoint(SearchFilter &filter)
In response to this method the resolver scans all the modules in the breakpoint's target,...
Definition: BreakpointResolver.cpp:176
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb::eDescriptionLevelVerbose
@ eDescriptionLevelVerbose
Definition: lldb-enumerations.h:210
lldb_private::Address::IsSectionOffset
bool IsSectionOffset() const
Check if an address is section offset.
Definition: Address.h:323
lldb_private::Breakpoint::GetTarget
Target & GetTarget()
Accessor for the breakpoint Target.
Definition: Breakpoint.h:453
lldb_private::Breakpoint::GetNumLocations
size_t GetNumLocations() const
Return the number of breakpoint locations.
Definition: Breakpoint.cpp:837
lldb_private::Address::GetOffset
lldb::addr_t GetOffset() const
Get the section relative offset value.
Definition: Address.h:310
lldb_private::Log
Definition: Log.h:49
lldb_private::Stream::PutCString
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:63
lldb_private::FileSpec::SetFile
void SetFile(llvm::StringRef path, Style style)
Change the file specified with a new path.
Definition: FileSpec.cpp:174
lldb_private::StructuredData::Dictionary::GetValueForKeyAsString
bool GetValueForKeyAsString(llvm::StringRef key, llvm::StringRef &result) const
Definition: StructuredData.h:423
lldb_private::FileSpec::GetPath
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:348
lldb_private::BreakpointResolverAddress::m_resolved_addr
lldb::addr_t m_resolved_addr
Definition: BreakpointResolverAddress.h:67
BreakpointResolverAddress.h
lldb
Definition: SBAddress.h:15
lldb_private::BreakpointResolver::GetBreakpoint
lldb::BreakpointSP GetBreakpoint() const
This gets the breakpoint for this resolver.
Definition: BreakpointResolver.h:62
lldb_private::Breakpoint
General Outline: A breakpoint has four main parts, a filter, a resolver, the list of breakpoint locat...
Definition: Breakpoint.h:79
lldb_private::BreakpointResolverAddress::SearchCallback
Searcher::CallbackReturn SearchCallback(SearchFilter &filter, SymbolContext &context, Address *addr) override
Definition: BreakpointResolverAddress.cpp:120