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),
85  m_module_filespec.GetPath());
86  }
87  }
88 
89  return WrapOptionsDict(options_dict_sp);
90  return StructuredData::ObjectSP();
91 }
92 
94  // If the address is not section relative, then we should not try to re-
95  // resolve it, it is just some random address and we wouldn't know what to do
96  // on reload. But if it is section relative, we need to re-resolve it since
97  // the section it's in may have shifted on re-run.
98  bool re_resolve = false;
99  if (m_addr.GetSection() || m_module_filespec)
100  re_resolve = true;
101  else if (GetBreakpoint()->GetNumLocations() == 0)
102  re_resolve = true;
103 
104  if (re_resolve)
106 }
107 
109  SearchFilter &filter, ModuleList &modules) {
110  // See comment in ResolveBreakpoint.
111  bool re_resolve = false;
112  if (m_addr.GetSection())
113  re_resolve = true;
114  else if (GetBreakpoint()->GetNumLocations() == 0)
115  re_resolve = true;
116 
117  if (re_resolve)
119 }
120 
122  SearchFilter &filter, SymbolContext &context, Address *addr) {
123  BreakpointSP breakpoint_sp = GetBreakpoint();
124  Breakpoint &breakpoint = *breakpoint_sp;
125 
126  if (filter.AddressPasses(m_addr)) {
127  if (breakpoint.GetNumLocations() == 0) {
128  // If the address is just an offset, and we're given a module, see if we
129  // can find the appropriate module loaded in the binary, and fix up
130  // m_addr to use that.
131  if (!m_addr.IsSectionOffset() && m_module_filespec) {
132  Target &target = breakpoint.GetTarget();
133  ModuleSpec module_spec(m_module_filespec);
134  ModuleSP module_sp = target.GetImages().FindFirstModule(module_spec);
135  if (module_sp) {
136  Address tmp_address;
137  if (module_sp->ResolveFileAddress(m_addr.GetOffset(), tmp_address))
138  m_addr = tmp_address;
139  }
140  }
141 
142  m_resolved_addr = m_addr.GetLoadAddress(&breakpoint.GetTarget());
143  BreakpointLocationSP bp_loc_sp(AddLocation(m_addr));
144  if (bp_loc_sp && !breakpoint.IsInternal()) {
145  StreamString s;
146  bp_loc_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
147  Log *log(
149  LLDB_LOGF(log, "Added location: %s\n", s.GetData());
150  }
151  } else {
152  BreakpointLocationSP loc_sp = breakpoint.GetLocationAtIndex(0);
153  lldb::addr_t cur_load_location =
154  m_addr.GetLoadAddress(&breakpoint.GetTarget());
155  if (cur_load_location != m_resolved_addr) {
156  m_resolved_addr = cur_load_location;
157  loc_sp->ClearBreakpointSite();
158  loc_sp->ResolveBreakpointSite();
159  }
160  }
161  }
163 }
164 
167 }
168 
170  s->PutCString("address = ");
171  m_addr.Dump(s, GetBreakpoint()->GetTarget().GetProcessSP().get(),
174 }
175 
177 
178 lldb::BreakpointResolverSP
180  lldb::BreakpointResolverSP ret_sp(
181  new BreakpointResolverAddress(breakpoint, m_addr));
182  return ret_sp;
183 }
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:63
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.
A class that represents a running process on the host machine.
BreakpointResolverAddress(const lldb::BreakpointSP &bkpt, const Address &addr)
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
"lldb/Breakpoint/BreakpointResolverAddress.h" This class sets breakpoints on a given Address...
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:310
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.
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:56
Target & GetTarget()
Accessor for the breakpoint Target.
Definition: Breakpoint.h:452
lldb::BreakpointLocationSP AddLocation(Address loc_addr, bool *new_location=nullptr)
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...
#define LLDB_INVALID_ADDRESS
Invalid value definitions.
Definition: lldb-defines.h:85
bool IsInternal() const
Tell whether this breakpoint is an "internal" breakpoint.
Definition: Breakpoint.cpp:263
static llvm::raw_ostream & error(Stream &strm)
Log * GetLogIfAllCategoriesSet(uint32_t mask)
Definition: Logging.cpp:58
A collection class for Module objects.
Definition: ModuleList.h:71
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:572
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:865
static BreakpointResolver * CreateFromStructuredData(const lldb::BreakpointSP &bkpt, const StructuredData::Dictionary &options_dict, Status &error)
General Outline: Provides the callback and search depth for the SearchFilter search.
Definition: SearchFilter.h:83
General Outline: The AddressResolver is a Searcher.
void SetErrorString(llvm::StringRef err_str)
Set the current error string to err_str.
Definition: Status.cpp:242
StructuredData::ObjectSP SerializeToStructuredData() override
Searcher::CallbackReturn SearchCallback(SearchFilter &filter, SymbolContext &context, Address *addr) override
A section + offset based address class.
Definition: Address.h:59
bool HasKey(llvm::StringRef key) const
lldb::BreakpointResolverSP CopyForBreakpoint(lldb::BreakpointSP &breakpoint) override
const ModuleList & GetImages() const
Get accessor for the images for this process.
Definition: Target.h:900
#define LLDB_LOGF(log,...)
Definition: Log.h:249
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:96
A uniqued constant string class.
Definition: ConstString.h:40
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:246
Definition: SBAddress.h:15
std::shared_ptr< Object > ObjectSP
lldb::BreakpointSP GetBreakpoint() const
This gets the breakpoint for this resolver.
lldb::BreakpointLocationSP GetLocationAtIndex(size_t index)
Get breakpoint locations by index.
Definition: Breakpoint.cpp:283
Display as the file address with the module name prepended (if any).
Definition: Address.h:90
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:174
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)
static const char * GetKey(OptionNames enum_value)
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.