LLDB  mainline
BreakpointResolverAddress.cpp
Go to the documentation of this file.
1 //===-- BreakpointResolverAddress.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 
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  Breakpoint *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)
34 }
35 
37 
39  Breakpoint *bkpt, const StructuredData::Dictionary &options_dict,
40  Status &error) {
41  llvm::StringRef module_name;
42  lldb::addr_t addr_offset;
43  FileSpec module_filespec;
44  bool success;
45 
46  success = options_dict.GetValueForKeyAsInteger(
47  GetKey(OptionNames::AddressOffset), addr_offset);
48  if (!success) {
49  error.SetErrorString("BRFL::CFSD: Couldn't find address offset entry.");
50  return nullptr;
51  }
52  Address address(addr_offset);
53 
54  success = options_dict.HasKey(GetKey(OptionNames::ModuleName));
55  if (success) {
56  success = options_dict.GetValueForKeyAsString(
57  GetKey(OptionNames::ModuleName), module_name);
58  if (!success) {
59  error.SetErrorString("BRA::CFSD: Couldn't read module name entry.");
60  return nullptr;
61  }
62  module_filespec.SetFile(module_name, FileSpec::Style::native);
63  }
64  return new BreakpointResolverAddress(bkpt, address, module_filespec);
65 }
66 
69  StructuredData::DictionarySP options_dict_sp(
71  SectionSP section_sp = m_addr.GetSection();
72  if (section_sp) {
73  ModuleSP module_sp = section_sp->GetModule();
74  ConstString module_name;
75  if (module_sp)
76  module_name.SetCString(module_name.GetCString());
77 
78  options_dict_sp->AddStringItem(GetKey(OptionNames::ModuleName),
79  module_name.GetCString());
80  options_dict_sp->AddIntegerItem(GetKey(OptionNames::AddressOffset),
81  m_addr.GetOffset());
82  } else {
83  options_dict_sp->AddIntegerItem(GetKey(OptionNames::AddressOffset),
84  m_addr.GetOffset());
85  if (m_module_filespec) {
86  options_dict_sp->AddStringItem(GetKey(OptionNames::ModuleName),
88  }
89  }
90 
91  return WrapOptionsDict(options_dict_sp);
92  return StructuredData::ObjectSP();
93 }
94 
96  // If the address is not section relative, then we should not try to re-
97  // resolve it, it is just some random address and we wouldn't know what to do
98  // on reload. But if it is section relative, we need to re-resolve it since
99  // the section it's in may have shifted on re-run.
100  bool re_resolve = false;
102  re_resolve = true;
103  else if (m_breakpoint->GetNumLocations() == 0)
104  re_resolve = true;
105 
106  if (re_resolve)
108 }
109 
111  SearchFilter &filter, ModuleList &modules) {
112  // See comment in ResolveBreakpoint.
113  bool re_resolve = false;
114  if (m_addr.GetSection())
115  re_resolve = true;
116  else if (m_breakpoint->GetNumLocations() == 0)
117  re_resolve = true;
118 
119  if (re_resolve)
121 }
122 
125  SymbolContext &context, Address *addr,
126  bool containing) {
127  assert(m_breakpoint != NULL);
128 
129  if (filter.AddressPasses(m_addr)) {
130  if (m_breakpoint->GetNumLocations() == 0) {
131  // If the address is just an offset, and we're given a module, see if we
132  // can find the appropriate module loaded in the binary, and fix up
133  // m_addr to use that.
135  Target &target = m_breakpoint->GetTarget();
136  ModuleSpec module_spec(m_module_filespec);
137  ModuleSP module_sp = target.GetImages().FindFirstModule(module_spec);
138  if (module_sp) {
139  Address tmp_address;
140  if (module_sp->ResolveFileAddress(m_addr.GetOffset(), tmp_address))
141  m_addr = tmp_address;
142  }
143  }
144 
146  BreakpointLocationSP bp_loc_sp(AddLocation(m_addr));
147  if (bp_loc_sp && !m_breakpoint->IsInternal()) {
148  StreamString s;
149  bp_loc_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
150  Log *log(
152  if (log)
153  log->Printf("Added location: %s\n", s.GetData());
154  }
155  } else {
156  BreakpointLocationSP loc_sp = m_breakpoint->GetLocationAtIndex(0);
157  lldb::addr_t cur_load_location =
159  if (cur_load_location != m_resolved_addr) {
160  m_resolved_addr = cur_load_location;
161  loc_sp->ClearBreakpointSite();
162  loc_sp->ResolveBreakpointSite();
163  }
164  }
165  }
167 }
168 
171 }
172 
174  s->PutCString("address = ");
178 }
179 
181 
182 lldb::BreakpointResolverSP
184  lldb::BreakpointResolverSP ret_sp(
185  new BreakpointResolverAddress(&breakpoint, m_addr));
186  return ret_sp;
187 }
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:61
bool IsSectionOffset() const
Check if an address is section offset.
Definition: Address.h:330
void ResolveBreakpointInModules(SearchFilter &filter, ModuleList &modules) override
In response to this method the resolver scans the modules in the module list modules, and adds any new locations it finds.
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
lldb::BreakpointLocationSP AddLocation(Address loc_addr, bool *new_location=NULL)
Defines a symbol context baton that can be handed other debug core functions.
Definition: SymbolContext.h:33
lldb::addr_t GetLoadAddress(Target *target) const
Get the load address.
Definition: Address.cpp:292
General Outline: A breakpoint has four main parts, a filter, a resolver, the list of breakpoint locat...
Definition: Breakpoint.h:78
virtual void ResolveBreakpointInModules(SearchFilter &filter, ModuleList &modules)
In response to this method the resolver scans the modules in the module list modules, and adds any new locations it finds.
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:374
void ResolveBreakpoint(SearchFilter &filter) override
In response to this method the resolver scans all the modules in the breakpoint's target...
A file utility class.
Definition: FileSpec.h:55
Target & GetTarget()
Accessor for the breakpoint Target.
Definition: Breakpoint.h:467
static BreakpointResolver * CreateFromStructuredData(Breakpoint *bkpt, const StructuredData::Dictionary &options_dict, Status &error)
const char * GetData() const
Definition: StreamString.h:43
std::shared_ptr< Dictionary > DictionarySP
virtual void ResolveBreakpoint(SearchFilter &filter)
In response to this method the resolver scans all the modules in the breakpoint&#39;s target...
lldb::addr_t GetOffset() const
Get the section relative offset value.
Definition: Address.h:317
#define LLDB_INVALID_ADDRESS
Invalid value definitions.
Definition: lldb-defines.h:85
const lldb::ProcessSP & GetProcessSP() const
Definition: Target.cpp:206
bool IsInternal() const
Tell whether this breakpoint is an "internal" breakpoint.
Definition: Breakpoint.cpp:251
Searcher::CallbackReturn SearchCallback(SearchFilter &filter, SymbolContext &context, Address *addr, bool containing) override
Log * GetLogIfAllCategoriesSet(uint32_t mask)
Definition: Logging.cpp:57
A collection class for Module objects.
Definition: ModuleList.h:91
General Outline: The BreakpointResolver is a Searcher.
#define LIBLLDB_LOG_BREAKPOINTS
Definition: Logging.h:19
lldb::ModuleSP FindFirstModule(const ModuleSpec &module_spec) const
Definition: ModuleList.cpp:599
void GetDescription(Stream *s) override
Prints a canonical description for the breakpoint to the stream s.
size_t GetNumLocations() const
Return the number of breakpoint locations.
Definition: Breakpoint.cpp:853
General Outline: Provides the callback and search depth for the SearchFilter search.
Definition: SearchFilter.h:101
General Outline: The AddressResolver is a Searcher.
void SetErrorString(llvm::StringRef err_str)
Set the current error string to err_str.
Definition: Status.cpp:241
StructuredData::ObjectSP SerializeToStructuredData() override
A section + offset based address class.
Definition: Address.h:80
bool HasKey(llvm::StringRef key) const
const ModuleList & GetImages() const
Get accessor for the images for this process.
Definition: Target.h:899
bool GetValueForKeyAsInteger(llvm::StringRef key, IntType &result) const
uint64_t addr_t
Definition: lldb-types.h:83
Display as the load address (if resolved).
Definition: Address.h:104
A uniqued constant string class.
Definition: ConstString.h:38
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:247
Definition: SBAddress.h:15
std::shared_ptr< Object > ObjectSP
lldb::BreakpointLocationSP GetLocationAtIndex(size_t index)
Get breakpoint locations by index.
Definition: Breakpoint.cpp:271
Display as the file address with the module name prepended (if any).
Definition: Address.h:99
void Dump(Stream *s) const override
Standard "Dump" method. At present it does nothing.
void SetFile(llvm::StringRef path, Style style)
Change the file specified with a new path.
Definition: FileSpec.cpp:198
lldb::SectionSP GetSection() const
Get const accessor for the section.
Definition: Address.h:410
virtual bool AddressPasses(Address &addr)
Call this method with a Address to see if address passes the filter.
StructuredData::DictionarySP WrapOptionsDict(StructuredData::DictionarySP options_dict_sp)
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:376
void Printf(const char *format,...) __attribute__((format(printf
Definition: Log.cpp:113
BreakpointResolverAddress(Breakpoint *bkpt, const Address &addr)
static const char * GetKey(OptionNames enum_value)
lldb::BreakpointResolverSP CopyForBreakpoint(Breakpoint &breakpoint) override
bool GetValueForKeyAsString(llvm::StringRef key, llvm::StringRef &result) const
An error handling class.
Definition: Status.h:44
void SetCString(const char *cstr)
Set the C string value.