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