xref: /src/contrib/llvm-project/lldb/source/Breakpoint/BreakpointResolverAddress.cpp (revision 1db9f3b21e39176dd5b67cf8ac378633b172463e)
1cfca06d7SDimitry Andric //===-- BreakpointResolverAddress.cpp -------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste 
9f034231aSEd Maste #include "lldb/Breakpoint/BreakpointResolverAddress.h"
10f034231aSEd Maste #include "lldb/Breakpoint/BreakpointLocation.h"
11e81d9d49SDimitry Andric #include "lldb/Core/Module.h"
1214f1b3e8SDimitry Andric #include "lldb/Core/Section.h"
135e95aa85SEd Maste #include "lldb/Target/Process.h"
14f034231aSEd Maste #include "lldb/Target/Target.h"
15145449b1SDimitry Andric #include "lldb/Utility/LLDBLog.h"
1674a628f7SDimitry Andric #include "lldb/Utility/StreamString.h"
17f034231aSEd Maste 
18f034231aSEd Maste using namespace lldb;
19f034231aSEd Maste using namespace lldb_private;
20f034231aSEd Maste 
21f034231aSEd Maste // BreakpointResolverAddress:
BreakpointResolverAddress(const BreakpointSP & bkpt,const Address & addr,const FileSpec & module_spec)2214f1b3e8SDimitry Andric BreakpointResolverAddress::BreakpointResolverAddress(
23cfca06d7SDimitry Andric     const BreakpointSP &bkpt, const Address &addr, const FileSpec &module_spec)
2414f1b3e8SDimitry Andric     : BreakpointResolver(bkpt, BreakpointResolver::AddressResolver),
2514f1b3e8SDimitry Andric       m_addr(addr), m_resolved_addr(LLDB_INVALID_ADDRESS),
2614f1b3e8SDimitry Andric       m_module_filespec(module_spec) {}
2714f1b3e8SDimitry Andric 
BreakpointResolverAddress(const BreakpointSP & bkpt,const Address & addr)28cfca06d7SDimitry Andric BreakpointResolverAddress::BreakpointResolverAddress(const BreakpointSP &bkpt,
2914f1b3e8SDimitry Andric                                                      const Address &addr)
3014f1b3e8SDimitry Andric     : BreakpointResolver(bkpt, BreakpointResolver::AddressResolver),
31145449b1SDimitry Andric       m_addr(addr), m_resolved_addr(LLDB_INVALID_ADDRESS) {}
32e81d9d49SDimitry Andric 
CreateFromStructuredData(const StructuredData::Dictionary & options_dict,Status & error)33b1c73532SDimitry Andric BreakpointResolverSP BreakpointResolverAddress::CreateFromStructuredData(
34312c0ed1SDimitry Andric     const StructuredData::Dictionary &options_dict, Status &error) {
35b76161e4SDimitry Andric   llvm::StringRef module_name;
367fa27ce4SDimitry Andric   lldb::offset_t addr_offset;
3714f1b3e8SDimitry Andric   FileSpec module_filespec;
3814f1b3e8SDimitry Andric   bool success;
3914f1b3e8SDimitry Andric 
4014f1b3e8SDimitry Andric   success = options_dict.GetValueForKeyAsInteger(
4114f1b3e8SDimitry Andric       GetKey(OptionNames::AddressOffset), addr_offset);
4214f1b3e8SDimitry Andric   if (!success) {
4314f1b3e8SDimitry Andric     error.SetErrorString("BRFL::CFSD: Couldn't find address offset entry.");
4414f1b3e8SDimitry Andric     return nullptr;
4514f1b3e8SDimitry Andric   }
4614f1b3e8SDimitry Andric   Address address(addr_offset);
4714f1b3e8SDimitry Andric 
4814f1b3e8SDimitry Andric   success = options_dict.HasKey(GetKey(OptionNames::ModuleName));
4914f1b3e8SDimitry Andric   if (success) {
5014f1b3e8SDimitry Andric     success = options_dict.GetValueForKeyAsString(
5114f1b3e8SDimitry Andric         GetKey(OptionNames::ModuleName), module_name);
5214f1b3e8SDimitry Andric     if (!success) {
5314f1b3e8SDimitry Andric       error.SetErrorString("BRA::CFSD: Couldn't read module name entry.");
5414f1b3e8SDimitry Andric       return nullptr;
5514f1b3e8SDimitry Andric     }
5694994d37SDimitry Andric     module_filespec.SetFile(module_name, FileSpec::Style::native);
5714f1b3e8SDimitry Andric   }
58312c0ed1SDimitry Andric   return std::make_shared<BreakpointResolverAddress>(nullptr, address,
59b1c73532SDimitry Andric                                                      module_filespec);
60f034231aSEd Maste }
61f034231aSEd Maste 
6214f1b3e8SDimitry Andric StructuredData::ObjectSP
SerializeToStructuredData()6314f1b3e8SDimitry Andric BreakpointResolverAddress::SerializeToStructuredData() {
6414f1b3e8SDimitry Andric   StructuredData::DictionarySP options_dict_sp(
6514f1b3e8SDimitry Andric       new StructuredData::Dictionary());
6614f1b3e8SDimitry Andric   SectionSP section_sp = m_addr.GetSection();
6714f1b3e8SDimitry Andric   if (section_sp) {
68aca2e42cSDimitry Andric     if (ModuleSP module_sp = section_sp->GetModule()) {
69aca2e42cSDimitry Andric       const FileSpec &module_fspec = module_sp->GetFileSpec();
7014f1b3e8SDimitry Andric       options_dict_sp->AddStringItem(GetKey(OptionNames::ModuleName),
71aca2e42cSDimitry Andric                                      module_fspec.GetPath().c_str());
72aca2e42cSDimitry Andric     }
7314f1b3e8SDimitry Andric     options_dict_sp->AddIntegerItem(GetKey(OptionNames::AddressOffset),
7414f1b3e8SDimitry Andric                                     m_addr.GetOffset());
7514f1b3e8SDimitry Andric   } else {
7614f1b3e8SDimitry Andric     options_dict_sp->AddIntegerItem(GetKey(OptionNames::AddressOffset),
7714f1b3e8SDimitry Andric                                     m_addr.GetOffset());
7814f1b3e8SDimitry Andric     if (m_module_filespec) {
7914f1b3e8SDimitry Andric       options_dict_sp->AddStringItem(GetKey(OptionNames::ModuleName),
8014f1b3e8SDimitry Andric                                      m_module_filespec.GetPath());
8114f1b3e8SDimitry Andric     }
82f034231aSEd Maste   }
83f034231aSEd Maste 
8414f1b3e8SDimitry Andric   return WrapOptionsDict(options_dict_sp);
8514f1b3e8SDimitry Andric }
8614f1b3e8SDimitry Andric 
ResolveBreakpoint(SearchFilter & filter)8714f1b3e8SDimitry Andric void BreakpointResolverAddress::ResolveBreakpoint(SearchFilter &filter) {
88f73363f1SDimitry Andric   // If the address is not section relative, then we should not try to re-
89f73363f1SDimitry Andric   // resolve it, it is just some random address and we wouldn't know what to do
90f73363f1SDimitry Andric   // on reload.  But if it is section relative, we need to re-resolve it since
91f73363f1SDimitry Andric   // the section it's in may have shifted on re-run.
92e81d9d49SDimitry Andric   bool re_resolve = false;
93e81d9d49SDimitry Andric   if (m_addr.GetSection() || m_module_filespec)
94e81d9d49SDimitry Andric     re_resolve = true;
95cfca06d7SDimitry Andric   else if (GetBreakpoint()->GetNumLocations() == 0)
96e81d9d49SDimitry Andric     re_resolve = true;
97e81d9d49SDimitry Andric 
98e81d9d49SDimitry Andric   if (re_resolve)
99f034231aSEd Maste     BreakpointResolver::ResolveBreakpoint(filter);
100f034231aSEd Maste }
101f034231aSEd Maste 
ResolveBreakpointInModules(SearchFilter & filter,ModuleList & modules)10214f1b3e8SDimitry Andric void BreakpointResolverAddress::ResolveBreakpointInModules(
10314f1b3e8SDimitry Andric     SearchFilter &filter, ModuleList &modules) {
104e81d9d49SDimitry Andric   // See comment in ResolveBreakpoint.
105e81d9d49SDimitry Andric   bool re_resolve = false;
106e81d9d49SDimitry Andric   if (m_addr.GetSection())
107e81d9d49SDimitry Andric     re_resolve = true;
108cfca06d7SDimitry Andric   else if (GetBreakpoint()->GetNumLocations() == 0)
109e81d9d49SDimitry Andric     re_resolve = true;
110e81d9d49SDimitry Andric 
111e81d9d49SDimitry Andric   if (re_resolve)
112f034231aSEd Maste     BreakpointResolver::ResolveBreakpointInModules(filter, modules);
113f034231aSEd Maste }
114f034231aSEd Maste 
SearchCallback(SearchFilter & filter,SymbolContext & context,Address * addr)115ead24645SDimitry Andric Searcher::CallbackReturn BreakpointResolverAddress::SearchCallback(
116ead24645SDimitry Andric     SearchFilter &filter, SymbolContext &context, Address *addr) {
117cfca06d7SDimitry Andric   BreakpointSP breakpoint_sp = GetBreakpoint();
118cfca06d7SDimitry Andric   Breakpoint &breakpoint = *breakpoint_sp;
119f034231aSEd Maste 
12014f1b3e8SDimitry Andric   if (filter.AddressPasses(m_addr)) {
121cfca06d7SDimitry Andric     if (breakpoint.GetNumLocations() == 0) {
12214f1b3e8SDimitry Andric       // If the address is just an offset, and we're given a module, see if we
123f73363f1SDimitry Andric       // can find the appropriate module loaded in the binary, and fix up
124f73363f1SDimitry Andric       // m_addr to use that.
12514f1b3e8SDimitry Andric       if (!m_addr.IsSectionOffset() && m_module_filespec) {
126cfca06d7SDimitry Andric         Target &target = breakpoint.GetTarget();
127e81d9d49SDimitry Andric         ModuleSpec module_spec(m_module_filespec);
128e81d9d49SDimitry Andric         ModuleSP module_sp = target.GetImages().FindFirstModule(module_spec);
12914f1b3e8SDimitry Andric         if (module_sp) {
130e81d9d49SDimitry Andric           Address tmp_address;
131e81d9d49SDimitry Andric           if (module_sp->ResolveFileAddress(m_addr.GetOffset(), tmp_address))
132e81d9d49SDimitry Andric             m_addr = tmp_address;
133e81d9d49SDimitry Andric         }
134e81d9d49SDimitry Andric       }
135e81d9d49SDimitry Andric 
136cfca06d7SDimitry Andric       m_resolved_addr = m_addr.GetLoadAddress(&breakpoint.GetTarget());
137f3fbd1c0SDimitry Andric       BreakpointLocationSP bp_loc_sp(AddLocation(m_addr));
138cfca06d7SDimitry Andric       if (bp_loc_sp && !breakpoint.IsInternal()) {
139f034231aSEd Maste         StreamString s;
140f034231aSEd Maste         bp_loc_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
141145449b1SDimitry Andric         Log *log = GetLog(LLDBLog::Breakpoints);
142ead24645SDimitry Andric         LLDB_LOGF(log, "Added location: %s\n", s.GetData());
143f034231aSEd Maste       }
14414f1b3e8SDimitry Andric     } else {
145cfca06d7SDimitry Andric       BreakpointLocationSP loc_sp = breakpoint.GetLocationAtIndex(0);
14614f1b3e8SDimitry Andric       lldb::addr_t cur_load_location =
147cfca06d7SDimitry Andric           m_addr.GetLoadAddress(&breakpoint.GetTarget());
14814f1b3e8SDimitry Andric       if (cur_load_location != m_resolved_addr) {
149e81d9d49SDimitry Andric         m_resolved_addr = cur_load_location;
150e81d9d49SDimitry Andric         loc_sp->ClearBreakpointSite();
151e81d9d49SDimitry Andric         loc_sp->ResolveBreakpointSite();
152e81d9d49SDimitry Andric       }
153e81d9d49SDimitry Andric     }
154e81d9d49SDimitry Andric   }
155f034231aSEd Maste   return Searcher::eCallbackReturnStop;
156f034231aSEd Maste }
157f034231aSEd Maste 
GetDepth()15894994d37SDimitry Andric lldb::SearchDepth BreakpointResolverAddress::GetDepth() {
15994994d37SDimitry Andric   return lldb::eSearchDepthTarget;
160f034231aSEd Maste }
161f034231aSEd Maste 
GetDescription(Stream * s)16214f1b3e8SDimitry Andric void BreakpointResolverAddress::GetDescription(Stream *s) {
163f034231aSEd Maste   s->PutCString("address = ");
164cfca06d7SDimitry Andric   m_addr.Dump(s, GetBreakpoint()->GetTarget().GetProcessSP().get(),
16514f1b3e8SDimitry Andric               Address::DumpStyleModuleWithFileAddress,
16614f1b3e8SDimitry Andric               Address::DumpStyleLoadAddress);
167f034231aSEd Maste }
168f034231aSEd Maste 
Dump(Stream * s) const16914f1b3e8SDimitry Andric void BreakpointResolverAddress::Dump(Stream *s) const {}
170205afe67SEd Maste 
171205afe67SEd Maste lldb::BreakpointResolverSP
CopyForBreakpoint(BreakpointSP & breakpoint)172cfca06d7SDimitry Andric BreakpointResolverAddress::CopyForBreakpoint(BreakpointSP &breakpoint) {
17314f1b3e8SDimitry Andric   lldb::BreakpointResolverSP ret_sp(
174cfca06d7SDimitry Andric       new BreakpointResolverAddress(breakpoint, m_addr));
175205afe67SEd Maste   return ret_sp;
176205afe67SEd Maste }
177