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 
9 #include "lldb/Breakpoint/BreakpointResolverAddress.h"
10 #include "lldb/Breakpoint/BreakpointLocation.h"
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"
16 #include "lldb/Utility/StreamString.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)
24     : BreakpointResolver(bkpt, BreakpointResolver::AddressResolver),
25       m_addr(addr), m_resolved_addr(LLDB_INVALID_ADDRESS),
26       m_module_filespec(module_spec) {}
27 
28 BreakpointResolverAddress::BreakpointResolverAddress(const BreakpointSP &bkpt,
29                                                      const Address &addr)
30     : BreakpointResolver(bkpt, BreakpointResolver::AddressResolver),
31       m_addr(addr), m_resolved_addr(LLDB_INVALID_ADDRESS), m_module_filespec() {
32 }
33 
34 BreakpointResolver *BreakpointResolverAddress::CreateFromStructuredData(
35     const BreakpointSP &bkpt, const StructuredData::Dictionary &options_dict,
36     Status &error) {
37   llvm::StringRef module_name;
38   lldb::addr_t addr_offset;
39   FileSpec module_filespec;
40   bool success;
41 
42   success = options_dict.GetValueForKeyAsInteger(
43       GetKey(OptionNames::AddressOffset), addr_offset);
44   if (!success) {
45     error.SetErrorString("BRFL::CFSD: Couldn't find address offset entry.");
46     return nullptr;
47   }
48   Address address(addr_offset);
49 
50   success = options_dict.HasKey(GetKey(OptionNames::ModuleName));
51   if (success) {
52     success = options_dict.GetValueForKeyAsString(
53         GetKey(OptionNames::ModuleName), module_name);
54     if (!success) {
55       error.SetErrorString("BRA::CFSD: Couldn't read module name entry.");
56       return nullptr;
57     }
58     module_filespec.SetFile(module_name, FileSpec::Style::native);
59   }
60   return new BreakpointResolverAddress(bkpt, address, module_filespec);
61 }
62 
63 StructuredData::ObjectSP
64 BreakpointResolverAddress::SerializeToStructuredData() {
65   StructuredData::DictionarySP options_dict_sp(
66       new StructuredData::Dictionary());
67   SectionSP section_sp = m_addr.GetSection();
68   if (section_sp) {
69     ModuleSP module_sp = section_sp->GetModule();
70     ConstString module_name;
71     if (module_sp)
72       module_name.SetCString(module_name.GetCString());
73 
74     options_dict_sp->AddStringItem(GetKey(OptionNames::ModuleName),
75                                    module_name.GetCString());
76     options_dict_sp->AddIntegerItem(GetKey(OptionNames::AddressOffset),
77                                     m_addr.GetOffset());
78   } else {
79     options_dict_sp->AddIntegerItem(GetKey(OptionNames::AddressOffset),
80                                     m_addr.GetOffset());
81     if (m_module_filespec) {
82       options_dict_sp->AddStringItem(GetKey(OptionNames::ModuleName),
83                                      m_module_filespec.GetPath());
84     }
85   }
86 
87   return WrapOptionsDict(options_dict_sp);
88 }
89 
90 void BreakpointResolverAddress::ResolveBreakpoint(SearchFilter &filter) {
91   // If the address is not section relative, then we should not try to re-
92   // resolve it, it is just some random address and we wouldn't know what to do
93   // on reload.  But if it is section relative, we need to re-resolve it since
94   // the section it's in may have shifted on re-run.
95   bool re_resolve = false;
96   if (m_addr.GetSection() || m_module_filespec)
97     re_resolve = true;
98   else if (GetBreakpoint()->GetNumLocations() == 0)
99     re_resolve = true;
100 
101   if (re_resolve)
102     BreakpointResolver::ResolveBreakpoint(filter);
103 }
104 
105 void BreakpointResolverAddress::ResolveBreakpointInModules(
106     SearchFilter &filter, ModuleList &modules) {
107   // See comment in ResolveBreakpoint.
108   bool re_resolve = false;
109   if (m_addr.GetSection())
110     re_resolve = true;
111   else if (GetBreakpoint()->GetNumLocations() == 0)
112     re_resolve = true;
113 
114   if (re_resolve)
115     BreakpointResolver::ResolveBreakpointInModules(filter, modules);
116 }
117 
118 Searcher::CallbackReturn BreakpointResolverAddress::SearchCallback(
119     SearchFilter &filter, SymbolContext &context, Address *addr) {
120   BreakpointSP breakpoint_sp = GetBreakpoint();
121   Breakpoint &breakpoint = *breakpoint_sp;
122 
123   if (filter.AddressPasses(m_addr)) {
124     if (breakpoint.GetNumLocations() == 0) {
125       // If the address is just an offset, and we're given a module, see if we
126       // can find the appropriate module loaded in the binary, and fix up
127       // m_addr to use that.
128       if (!m_addr.IsSectionOffset() && m_module_filespec) {
129         Target &target = breakpoint.GetTarget();
130         ModuleSpec module_spec(m_module_filespec);
131         ModuleSP module_sp = target.GetImages().FindFirstModule(module_spec);
132         if (module_sp) {
133           Address tmp_address;
134           if (module_sp->ResolveFileAddress(m_addr.GetOffset(), tmp_address))
135             m_addr = tmp_address;
136         }
137       }
138 
139       m_resolved_addr = m_addr.GetLoadAddress(&breakpoint.GetTarget());
140       BreakpointLocationSP bp_loc_sp(AddLocation(m_addr));
141       if (bp_loc_sp && !breakpoint.IsInternal()) {
142         StreamString s;
143         bp_loc_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
144         Log *log = GetLog(LLDBLog::Breakpoints);
145         LLDB_LOGF(log, "Added location: %s\n", s.GetData());
146       }
147     } else {
148       BreakpointLocationSP loc_sp = breakpoint.GetLocationAtIndex(0);
149       lldb::addr_t cur_load_location =
150           m_addr.GetLoadAddress(&breakpoint.GetTarget());
151       if (cur_load_location != m_resolved_addr) {
152         m_resolved_addr = cur_load_location;
153         loc_sp->ClearBreakpointSite();
154         loc_sp->ResolveBreakpointSite();
155       }
156     }
157   }
158   return Searcher::eCallbackReturnStop;
159 }
160 
161 lldb::SearchDepth BreakpointResolverAddress::GetDepth() {
162   return lldb::eSearchDepthTarget;
163 }
164 
165 void BreakpointResolverAddress::GetDescription(Stream *s) {
166   s->PutCString("address = ");
167   m_addr.Dump(s, GetBreakpoint()->GetTarget().GetProcessSP().get(),
168               Address::DumpStyleModuleWithFileAddress,
169               Address::DumpStyleLoadAddress);
170 }
171 
172 void BreakpointResolverAddress::Dump(Stream *s) const {}
173 
174 lldb::BreakpointResolverSP
175 BreakpointResolverAddress::CopyForBreakpoint(BreakpointSP &breakpoint) {
176   lldb::BreakpointResolverSP ret_sp(
177       new BreakpointResolverAddress(breakpoint, m_addr));
178   return ret_sp;
179 }
180