1 //===-- BreakpointResolverAddress.cpp -------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Breakpoint/BreakpointResolverAddress.h"
12 #include "lldb/Breakpoint/BreakpointLocation.h"
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"
18 #include "lldb/Utility/StreamString.h"
21 using namespace lldb_private;
23 // BreakpointResolverAddress:
24 BreakpointResolverAddress::BreakpointResolverAddress(
25 const BreakpointSP &bkpt, const Address &addr, const FileSpec &module_spec)
26 : BreakpointResolver(bkpt, BreakpointResolver::AddressResolver),
27 m_addr(addr), m_resolved_addr(LLDB_INVALID_ADDRESS),
28 m_module_filespec(module_spec) {}
30 BreakpointResolverAddress::BreakpointResolverAddress(const BreakpointSP &bkpt,
32 : BreakpointResolver(bkpt, BreakpointResolver::AddressResolver),
33 m_addr(addr), m_resolved_addr(LLDB_INVALID_ADDRESS), m_module_filespec() {
36 BreakpointResolver *BreakpointResolverAddress::CreateFromStructuredData(
37 const BreakpointSP &bkpt, const StructuredData::Dictionary &options_dict,
39 llvm::StringRef module_name;
40 lldb::addr_t addr_offset;
41 FileSpec module_filespec;
44 success = options_dict.GetValueForKeyAsInteger(
45 GetKey(OptionNames::AddressOffset), addr_offset);
47 error.SetErrorString("BRFL::CFSD: Couldn't find address offset entry.");
50 Address address(addr_offset);
52 success = options_dict.HasKey(GetKey(OptionNames::ModuleName));
54 success = options_dict.GetValueForKeyAsString(
55 GetKey(OptionNames::ModuleName), module_name);
57 error.SetErrorString("BRA::CFSD: Couldn't read module name entry.");
60 module_filespec.SetFile(module_name, FileSpec::Style::native);
62 return new BreakpointResolverAddress(bkpt, address, module_filespec);
65 StructuredData::ObjectSP
66 BreakpointResolverAddress::SerializeToStructuredData() {
67 StructuredData::DictionarySP options_dict_sp(
68 new StructuredData::Dictionary());
69 SectionSP section_sp = m_addr.GetSection();
71 ModuleSP module_sp = section_sp->GetModule();
72 ConstString module_name;
74 module_name.SetCString(module_name.GetCString());
76 options_dict_sp->AddStringItem(GetKey(OptionNames::ModuleName),
77 module_name.GetCString());
78 options_dict_sp->AddIntegerItem(GetKey(OptionNames::AddressOffset),
81 options_dict_sp->AddIntegerItem(GetKey(OptionNames::AddressOffset),
83 if (m_module_filespec) {
84 options_dict_sp->AddStringItem(GetKey(OptionNames::ModuleName),
85 m_module_filespec.GetPath());
89 return WrapOptionsDict(options_dict_sp);
90 return StructuredData::ObjectSP();
93 void BreakpointResolverAddress::ResolveBreakpoint(SearchFilter &filter) {
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)
101 else if (GetBreakpoint()->GetNumLocations() == 0)
105 BreakpointResolver::ResolveBreakpoint(filter);
108 void BreakpointResolverAddress::ResolveBreakpointInModules(
109 SearchFilter &filter, ModuleList &modules) {
110 // See comment in ResolveBreakpoint.
111 bool re_resolve = false;
112 if (m_addr.GetSection())
114 else if (GetBreakpoint()->GetNumLocations() == 0)
118 BreakpointResolver::ResolveBreakpointInModules(filter, modules);
121 Searcher::CallbackReturn BreakpointResolverAddress::SearchCallback(
122 SearchFilter &filter, SymbolContext &context, Address *addr) {
123 BreakpointSP breakpoint_sp = GetBreakpoint();
124 Breakpoint &breakpoint = *breakpoint_sp;
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);
137 if (module_sp->ResolveFileAddress(m_addr.GetOffset(), tmp_address))
138 m_addr = tmp_address;
142 m_resolved_addr = m_addr.GetLoadAddress(&breakpoint.GetTarget());
143 BreakpointLocationSP bp_loc_sp(AddLocation(m_addr));
144 if (bp_loc_sp && !breakpoint.IsInternal()) {
146 bp_loc_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
148 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
149 LLDB_LOGF(log, "Added location: %s\n", s.GetData());
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();
162 return Searcher::eCallbackReturnStop;
165 lldb::SearchDepth BreakpointResolverAddress::GetDepth() {
166 return lldb::eSearchDepthTarget;
169 void BreakpointResolverAddress::GetDescription(Stream *s) {
170 s->PutCString("address = ");
171 m_addr.Dump(s, GetBreakpoint()->GetTarget().GetProcessSP().get(),
172 Address::DumpStyleModuleWithFileAddress,
173 Address::DumpStyleLoadAddress);
176 void BreakpointResolverAddress::Dump(Stream *s) const {}
178 lldb::BreakpointResolverSP
179 BreakpointResolverAddress::CopyForBreakpoint(BreakpointSP &breakpoint) {
180 lldb::BreakpointResolverSP ret_sp(
181 new BreakpointResolverAddress(breakpoint, m_addr));