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"
15 #include "lldb/Utility/LLDBLog.h"
17 
18 using namespace lldb;
19 using namespace lldb_private;
20 
21 // BreakpointResolverAddress:
22 BreakpointResolverAddress::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 BreakpointSP &bkpt, const StructuredData::Dictionary &options_dict,
35  Status &error) {
36  llvm::StringRef module_name;
37  lldb::addr_t addr_offset;
38  FileSpec module_filespec;
39  bool success;
40 
41  success = options_dict.GetValueForKeyAsInteger(
42  GetKey(OptionNames::AddressOffset), addr_offset);
43  if (!success) {
44  error.SetErrorString("BRFL::CFSD: Couldn't find address offset entry.");
45  return nullptr;
46  }
47  Address address(addr_offset);
48 
49  success = options_dict.HasKey(GetKey(OptionNames::ModuleName));
50  if (success) {
51  success = options_dict.GetValueForKeyAsString(
52  GetKey(OptionNames::ModuleName), module_name);
53  if (!success) {
54  error.SetErrorString("BRA::CFSD: Couldn't read module name entry.");
55  return nullptr;
56  }
57  module_filespec.SetFile(module_name, FileSpec::Style::native);
58  }
59  return new BreakpointResolverAddress(bkpt, address, module_filespec);
60 }
61 
64  StructuredData::DictionarySP options_dict_sp(
66  SectionSP section_sp = m_addr.GetSection();
67  if (section_sp) {
68  ModuleSP module_sp = section_sp->GetModule();
69  ConstString module_name;
70  if (module_sp)
71  module_name.SetCString(module_name.GetCString());
72 
73  options_dict_sp->AddStringItem(GetKey(OptionNames::ModuleName),
74  module_name.GetCString());
75  options_dict_sp->AddIntegerItem(GetKey(OptionNames::AddressOffset),
76  m_addr.GetOffset());
77  } else {
78  options_dict_sp->AddIntegerItem(GetKey(OptionNames::AddressOffset),
79  m_addr.GetOffset());
80  if (m_module_filespec) {
81  options_dict_sp->AddStringItem(GetKey(OptionNames::ModuleName),
83  }
84  }
85 
86  return WrapOptionsDict(options_dict_sp);
87 }
88 
90  // If the address is not section relative, then we should not try to re-
91  // resolve it, it is just some random address and we wouldn't know what to do
92  // on reload. But if it is section relative, we need to re-resolve it since
93  // the section it's in may have shifted on re-run.
94  bool re_resolve = false;
96  re_resolve = true;
97  else if (GetBreakpoint()->GetNumLocations() == 0)
98  re_resolve = true;
99 
100  if (re_resolve)
102 }
103 
105  SearchFilter &filter, ModuleList &modules) {
106  // See comment in ResolveBreakpoint.
107  bool re_resolve = false;
108  if (m_addr.GetSection())
109  re_resolve = true;
110  else if (GetBreakpoint()->GetNumLocations() == 0)
111  re_resolve = true;
112 
113  if (re_resolve)
115 }
116 
118  SearchFilter &filter, SymbolContext &context, Address *addr) {
119  BreakpointSP breakpoint_sp = GetBreakpoint();
120  Breakpoint &breakpoint = *breakpoint_sp;
121 
122  if (filter.AddressPasses(m_addr)) {
123  if (breakpoint.GetNumLocations() == 0) {
124  // If the address is just an offset, and we're given a module, see if we
125  // can find the appropriate module loaded in the binary, and fix up
126  // m_addr to use that.
128  Target &target = breakpoint.GetTarget();
129  ModuleSpec module_spec(m_module_filespec);
130  ModuleSP module_sp = target.GetImages().FindFirstModule(module_spec);
131  if (module_sp) {
132  Address tmp_address;
133  if (module_sp->ResolveFileAddress(m_addr.GetOffset(), tmp_address))
134  m_addr = tmp_address;
135  }
136  }
137 
139  BreakpointLocationSP bp_loc_sp(AddLocation(m_addr));
140  if (bp_loc_sp && !breakpoint.IsInternal()) {
141  StreamString s;
142  bp_loc_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
144  LLDB_LOGF(log, "Added location: %s\n", s.GetData());
145  }
146  } else {
147  BreakpointLocationSP loc_sp = breakpoint.GetLocationAtIndex(0);
148  lldb::addr_t cur_load_location =
149  m_addr.GetLoadAddress(&breakpoint.GetTarget());
150  if (cur_load_location != m_resolved_addr) {
151  m_resolved_addr = cur_load_location;
152  loc_sp->ClearBreakpointSite();
153  loc_sp->ResolveBreakpointSite();
154  }
155  }
156  }
158 }
159 
162 }
163 
165  s->PutCString("address = ");
166  m_addr.Dump(s, GetBreakpoint()->GetTarget().GetProcessSP().get(),
169 }
170 
172 
173 lldb::BreakpointResolverSP
175  lldb::BreakpointResolverSP ret_sp(
176  new BreakpointResolverAddress(breakpoint, m_addr));
177  return ret_sp;
178 }
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:174
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:104
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:270
Module.h
BreakpointLocation.h
lldb_private::ConstString::SetCString
void SetCString(const char *cstr)
Set the C string value.
Definition: ConstString.cpp:301
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:464
Section.h
lldb_private::Target::GetImages
const ModuleList & GetImages() const
Get accessor for the images for this process.
Definition: Target.h:938
lldb_private::BreakpointResolverAddress::CreateFromStructuredData
static BreakpointResolver * CreateFromStructuredData(const lldb::BreakpointSP &bkpt, const StructuredData::Dictionary &options_dict, Status &error)
Definition: BreakpointResolverAddress.cpp:33
Process.h
lldb_private::BreakpointResolverAddress::SerializeToStructuredData
StructuredData::ObjectSP SerializeToStructuredData() override
Definition: BreakpointResolverAddress.cpp:63
lldb_private::StructuredData::Dictionary::GetValueForKeyAsInteger
bool GetValueForKeyAsInteger(llvm::StringRef key, IntType &result) const
Definition: StructuredData.h:413
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:171
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:170
lldb_private::Address::DumpStyleLoadAddress
@ DumpStyleLoadAddress
Display as the load address (if resolved).
Definition: Address.h:96
lldb_private::ModuleList
Definition: ModuleList.h:81
lldb_private::SearchFilter
General Outline: Provides the callback and search depth for the SearchFilter search.
Definition: SearchFilter.h:83
lldb_private::BreakpointResolver::OptionNames::AddressOffset
@ AddressOffset
lldb_private::Breakpoint::GetLocationAtIndex
lldb::BreakpointLocationSP GetLocationAtIndex(size_t index)
Get breakpoint locations by index.
Definition: Breakpoint.cpp:279
lldb_private::Breakpoint::IsInternal
bool IsInternal() const
Tell whether this breakpoint is an "internal" breakpoint.
Definition: Breakpoint.cpp:259
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:89
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:618
lldb_private::LLDBLog::Breakpoints
@ Breakpoints
lldb_private::StructuredData::DictionarySP
std::shared_ptr< Dictionary > DictionarySP
Definition: StructuredData.h:67
lldb_private::BreakpointResolverAddress::GetDepth
lldb::SearchDepth GetDepth() override
Definition: BreakpointResolverAddress.cpp:160
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:429
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:337
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:149
lldb_private::ModuleSpec
Definition: ModuleSpec.h:27
lldb_private::BreakpointResolver::OptionNames::ModuleName
@ ModuleName
lldb_private::Status
Definition: Status.h:44
lldb_private::Address::Dump
bool Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style, DumpStyle fallback_style=DumpStyleInvalid, uint32_t addr_byte_size=UINT32_MAX, bool all_ranges=false) const
Dump a description of this object to a Stream.
Definition: Address.cpp:406
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:164
lldb_private::Address::GetLoadAddress
lldb::addr_t GetLoadAddress(Target *target) const
Get the load address.
Definition: Address.cpp:311
lldb_private::StructuredData::Dictionary::HasKey
bool HasKey(llvm::StringRef key) const
Definition: StructuredData.h:498
LLDB_INVALID_ADDRESS
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:74
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:175
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:332
lldb_private::Breakpoint::GetTarget
Target & GetTarget()
Accessor for the breakpoint Target.
Definition: Breakpoint.h:458
lldb_private::Breakpoint::GetNumLocations
size_t GetNumLocations() const
Return the number of breakpoint locations.
Definition: Breakpoint.cpp:842
lldb_private::Address::GetOffset
lldb::addr_t GetOffset() const
Get the section relative offset value.
Definition: Address.h:319
lldb_private::Log
Definition: Log.h:48
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:172
lldb_private::StructuredData::Dictionary::GetValueForKeyAsString
bool GetValueForKeyAsString(llvm::StringRef key, llvm::StringRef &result) const
Definition: StructuredData.h:433
lldb_private::GetLog
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:235
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:347
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
LLDBLog.h
lldb_private::Breakpoint
General Outline: A breakpoint has four main parts, a filter, a resolver, the list of breakpoint locat...
Definition: Breakpoint.h:80
lldb_private::BreakpointResolverAddress::SearchCallback
Searcher::CallbackReturn SearchCallback(SearchFilter &filter, SymbolContext &context, Address *addr) override
Definition: BreakpointResolverAddress.cpp:117