]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/lldb/source/Breakpoint/BreakpointResolverAddress.cpp
MFV r368746:
[FreeBSD/FreeBSD.git] / contrib / llvm-project / lldb / source / Breakpoint / BreakpointResolverAddress.cpp
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
11
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"
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)
26     : BreakpointResolver(bkpt, BreakpointResolver::AddressResolver),
27       m_addr(addr), m_resolved_addr(LLDB_INVALID_ADDRESS),
28       m_module_filespec(module_spec) {}
29
30 BreakpointResolverAddress::BreakpointResolverAddress(const BreakpointSP &bkpt,
31                                                      const Address &addr)
32     : BreakpointResolver(bkpt, BreakpointResolver::AddressResolver),
33       m_addr(addr), m_resolved_addr(LLDB_INVALID_ADDRESS), m_module_filespec() {
34 }
35
36 BreakpointResolver *BreakpointResolverAddress::CreateFromStructuredData(
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
65 StructuredData::ObjectSP
66 BreakpointResolverAddress::SerializeToStructuredData() {
67   StructuredData::DictionarySP options_dict_sp(
68       new StructuredData::Dictionary());
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
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)
100     re_resolve = true;
101   else if (GetBreakpoint()->GetNumLocations() == 0)
102     re_resolve = true;
103
104   if (re_resolve)
105     BreakpointResolver::ResolveBreakpoint(filter);
106 }
107
108 void BreakpointResolverAddress::ResolveBreakpointInModules(
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)
118     BreakpointResolver::ResolveBreakpointInModules(filter, modules);
119 }
120
121 Searcher::CallbackReturn BreakpointResolverAddress::SearchCallback(
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(
148             lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
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   }
162   return Searcher::eCallbackReturnStop;
163 }
164
165 lldb::SearchDepth BreakpointResolverAddress::GetDepth() {
166   return lldb::eSearchDepthTarget;
167 }
168
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);
174 }
175
176 void BreakpointResolverAddress::Dump(Stream *s) const {}
177
178 lldb::BreakpointResolverSP
179 BreakpointResolverAddress::CopyForBreakpoint(BreakpointSP &breakpoint) {
180   lldb::BreakpointResolverSP ret_sp(
181       new BreakpointResolverAddress(breakpoint, m_addr));
182   return ret_sp;
183 }