1ac7ddfbfSEd Maste //===-- BreakpointResolverAddress.cpp ---------------------------*- C++ -*-===//
2ac7ddfbfSEd Maste //
3ac7ddfbfSEd Maste // The LLVM Compiler Infrastructure
4ac7ddfbfSEd Maste //
5ac7ddfbfSEd Maste // This file is distributed under the University of Illinois Open Source
6ac7ddfbfSEd Maste // License. See LICENSE.TXT for details.
7ac7ddfbfSEd Maste //
8ac7ddfbfSEd Maste //===----------------------------------------------------------------------===//
9ac7ddfbfSEd Maste
10ac7ddfbfSEd Maste #include "lldb/Breakpoint/BreakpointResolverAddress.h"
11ac7ddfbfSEd Maste
12ac7ddfbfSEd Maste
13ac7ddfbfSEd Maste #include "lldb/Breakpoint/BreakpointLocation.h"
149f2f44ceSEd Maste #include "lldb/Core/Module.h"
15435933ddSDimitry Andric #include "lldb/Core/Section.h"
161c3bbb01SEd Maste #include "lldb/Target/Process.h"
17ac7ddfbfSEd Maste #include "lldb/Target/Target.h"
18f678e45dSDimitry Andric #include "lldb/Utility/Log.h"
19f678e45dSDimitry Andric #include "lldb/Utility/StreamString.h"
20ac7ddfbfSEd Maste
21ac7ddfbfSEd Maste using namespace lldb;
22ac7ddfbfSEd Maste using namespace lldb_private;
23ac7ddfbfSEd Maste
24ac7ddfbfSEd Maste //----------------------------------------------------------------------
25ac7ddfbfSEd Maste // BreakpointResolverAddress:
26ac7ddfbfSEd Maste //----------------------------------------------------------------------
BreakpointResolverAddress(Breakpoint * bkpt,const Address & addr,const FileSpec & module_spec)27435933ddSDimitry Andric BreakpointResolverAddress::BreakpointResolverAddress(
28435933ddSDimitry Andric Breakpoint *bkpt, const Address &addr, const FileSpec &module_spec)
29435933ddSDimitry Andric : BreakpointResolver(bkpt, BreakpointResolver::AddressResolver),
30435933ddSDimitry Andric m_addr(addr), m_resolved_addr(LLDB_INVALID_ADDRESS),
31435933ddSDimitry Andric m_module_filespec(module_spec) {}
32435933ddSDimitry Andric
BreakpointResolverAddress(Breakpoint * bkpt,const Address & addr)33435933ddSDimitry Andric BreakpointResolverAddress::BreakpointResolverAddress(Breakpoint *bkpt,
34435933ddSDimitry Andric const Address &addr)
35435933ddSDimitry Andric : BreakpointResolver(bkpt, BreakpointResolver::AddressResolver),
36435933ddSDimitry Andric m_addr(addr), m_resolved_addr(LLDB_INVALID_ADDRESS), m_module_filespec() {
379f2f44ceSEd Maste }
389f2f44ceSEd Maste
~BreakpointResolverAddress()39435933ddSDimitry Andric BreakpointResolverAddress::~BreakpointResolverAddress() {}
40435933ddSDimitry Andric
CreateFromStructuredData(Breakpoint * bkpt,const StructuredData::Dictionary & options_dict,Status & error)41435933ddSDimitry Andric BreakpointResolver *BreakpointResolverAddress::CreateFromStructuredData(
42435933ddSDimitry Andric Breakpoint *bkpt, const StructuredData::Dictionary &options_dict,
435517e702SDimitry Andric Status &error) {
445517e702SDimitry Andric llvm::StringRef module_name;
45435933ddSDimitry Andric lldb::addr_t addr_offset;
46435933ddSDimitry Andric FileSpec module_filespec;
47435933ddSDimitry Andric bool success;
48435933ddSDimitry Andric
49435933ddSDimitry Andric success = options_dict.GetValueForKeyAsInteger(
50435933ddSDimitry Andric GetKey(OptionNames::AddressOffset), addr_offset);
51435933ddSDimitry Andric if (!success) {
52435933ddSDimitry Andric error.SetErrorString("BRFL::CFSD: Couldn't find address offset entry.");
53435933ddSDimitry Andric return nullptr;
54435933ddSDimitry Andric }
55435933ddSDimitry Andric Address address(addr_offset);
56435933ddSDimitry Andric
57435933ddSDimitry Andric success = options_dict.HasKey(GetKey(OptionNames::ModuleName));
58435933ddSDimitry Andric if (success) {
59435933ddSDimitry Andric success = options_dict.GetValueForKeyAsString(
60435933ddSDimitry Andric GetKey(OptionNames::ModuleName), module_name);
61435933ddSDimitry Andric if (!success) {
62435933ddSDimitry Andric error.SetErrorString("BRA::CFSD: Couldn't read module name entry.");
63435933ddSDimitry Andric return nullptr;
64435933ddSDimitry Andric }
65*b5893f02SDimitry Andric module_filespec.SetFile(module_name, FileSpec::Style::native);
66435933ddSDimitry Andric }
67435933ddSDimitry Andric return new BreakpointResolverAddress(bkpt, address, module_filespec);
68ac7ddfbfSEd Maste }
69ac7ddfbfSEd Maste
70435933ddSDimitry Andric StructuredData::ObjectSP
SerializeToStructuredData()71435933ddSDimitry Andric BreakpointResolverAddress::SerializeToStructuredData() {
72435933ddSDimitry Andric StructuredData::DictionarySP options_dict_sp(
73435933ddSDimitry Andric new StructuredData::Dictionary());
74435933ddSDimitry Andric SectionSP section_sp = m_addr.GetSection();
75435933ddSDimitry Andric if (section_sp) {
76435933ddSDimitry Andric ModuleSP module_sp = section_sp->GetModule();
77435933ddSDimitry Andric ConstString module_name;
78435933ddSDimitry Andric if (module_sp)
79435933ddSDimitry Andric module_name.SetCString(module_name.GetCString());
80ac7ddfbfSEd Maste
81435933ddSDimitry Andric options_dict_sp->AddStringItem(GetKey(OptionNames::ModuleName),
82435933ddSDimitry Andric module_name.GetCString());
83435933ddSDimitry Andric options_dict_sp->AddIntegerItem(GetKey(OptionNames::AddressOffset),
84435933ddSDimitry Andric m_addr.GetOffset());
85435933ddSDimitry Andric } else {
86435933ddSDimitry Andric options_dict_sp->AddIntegerItem(GetKey(OptionNames::AddressOffset),
87435933ddSDimitry Andric m_addr.GetOffset());
88435933ddSDimitry Andric if (m_module_filespec) {
89435933ddSDimitry Andric options_dict_sp->AddStringItem(GetKey(OptionNames::ModuleName),
90435933ddSDimitry Andric m_module_filespec.GetPath());
91435933ddSDimitry Andric }
92ac7ddfbfSEd Maste }
93ac7ddfbfSEd Maste
94435933ddSDimitry Andric return WrapOptionsDict(options_dict_sp);
95435933ddSDimitry Andric return StructuredData::ObjectSP();
96435933ddSDimitry Andric }
97435933ddSDimitry Andric
ResolveBreakpoint(SearchFilter & filter)98435933ddSDimitry Andric void BreakpointResolverAddress::ResolveBreakpoint(SearchFilter &filter) {
994ba319b5SDimitry Andric // If the address is not section relative, then we should not try to re-
1004ba319b5SDimitry Andric // resolve it, it is just some random address and we wouldn't know what to do
1014ba319b5SDimitry Andric // on reload. But if it is section relative, we need to re-resolve it since
1024ba319b5SDimitry Andric // the section it's in may have shifted on re-run.
1039f2f44ceSEd Maste bool re_resolve = false;
1049f2f44ceSEd Maste if (m_addr.GetSection() || m_module_filespec)
1059f2f44ceSEd Maste re_resolve = true;
1069f2f44ceSEd Maste else if (m_breakpoint->GetNumLocations() == 0)
1079f2f44ceSEd Maste re_resolve = true;
1089f2f44ceSEd Maste
1099f2f44ceSEd Maste if (re_resolve)
110ac7ddfbfSEd Maste BreakpointResolver::ResolveBreakpoint(filter);
111ac7ddfbfSEd Maste }
112ac7ddfbfSEd Maste
ResolveBreakpointInModules(SearchFilter & filter,ModuleList & modules)113435933ddSDimitry Andric void BreakpointResolverAddress::ResolveBreakpointInModules(
114435933ddSDimitry Andric SearchFilter &filter, ModuleList &modules) {
1159f2f44ceSEd Maste // See comment in ResolveBreakpoint.
1169f2f44ceSEd Maste bool re_resolve = false;
1179f2f44ceSEd Maste if (m_addr.GetSection())
1189f2f44ceSEd Maste re_resolve = true;
1199f2f44ceSEd Maste else if (m_breakpoint->GetNumLocations() == 0)
1209f2f44ceSEd Maste re_resolve = true;
1219f2f44ceSEd Maste
1229f2f44ceSEd Maste if (re_resolve)
123ac7ddfbfSEd Maste BreakpointResolver::ResolveBreakpointInModules(filter, modules);
124ac7ddfbfSEd Maste }
125ac7ddfbfSEd Maste
126ac7ddfbfSEd Maste Searcher::CallbackReturn
SearchCallback(SearchFilter & filter,SymbolContext & context,Address * addr,bool containing)127435933ddSDimitry Andric BreakpointResolverAddress::SearchCallback(SearchFilter &filter,
128435933ddSDimitry Andric SymbolContext &context, Address *addr,
129435933ddSDimitry Andric bool containing) {
130ac7ddfbfSEd Maste assert(m_breakpoint != NULL);
131ac7ddfbfSEd Maste
132435933ddSDimitry Andric if (filter.AddressPasses(m_addr)) {
133435933ddSDimitry Andric if (m_breakpoint->GetNumLocations() == 0) {
134435933ddSDimitry Andric // If the address is just an offset, and we're given a module, see if we
1354ba319b5SDimitry Andric // can find the appropriate module loaded in the binary, and fix up
1364ba319b5SDimitry Andric // m_addr to use that.
137435933ddSDimitry Andric if (!m_addr.IsSectionOffset() && m_module_filespec) {
1389f2f44ceSEd Maste Target &target = m_breakpoint->GetTarget();
1399f2f44ceSEd Maste ModuleSpec module_spec(m_module_filespec);
1409f2f44ceSEd Maste ModuleSP module_sp = target.GetImages().FindFirstModule(module_spec);
141435933ddSDimitry Andric if (module_sp) {
1429f2f44ceSEd Maste Address tmp_address;
1439f2f44ceSEd Maste if (module_sp->ResolveFileAddress(m_addr.GetOffset(), tmp_address))
1449f2f44ceSEd Maste m_addr = tmp_address;
1459f2f44ceSEd Maste }
1469f2f44ceSEd Maste }
1479f2f44ceSEd Maste
1489f2f44ceSEd Maste m_resolved_addr = m_addr.GetLoadAddress(&m_breakpoint->GetTarget());
1494bb0738eSEd Maste BreakpointLocationSP bp_loc_sp(AddLocation(m_addr));
150435933ddSDimitry Andric if (bp_loc_sp && !m_breakpoint->IsInternal()) {
151ac7ddfbfSEd Maste StreamString s;
152ac7ddfbfSEd Maste bp_loc_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
153435933ddSDimitry Andric Log *log(
154435933ddSDimitry Andric lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
155ac7ddfbfSEd Maste if (log)
156ac7ddfbfSEd Maste log->Printf("Added location: %s\n", s.GetData());
157ac7ddfbfSEd Maste }
158435933ddSDimitry Andric } else {
1599f2f44ceSEd Maste BreakpointLocationSP loc_sp = m_breakpoint->GetLocationAtIndex(0);
160435933ddSDimitry Andric lldb::addr_t cur_load_location =
161435933ddSDimitry Andric m_addr.GetLoadAddress(&m_breakpoint->GetTarget());
162435933ddSDimitry Andric if (cur_load_location != m_resolved_addr) {
1639f2f44ceSEd Maste m_resolved_addr = cur_load_location;
1649f2f44ceSEd Maste loc_sp->ClearBreakpointSite();
1659f2f44ceSEd Maste loc_sp->ResolveBreakpointSite();
1669f2f44ceSEd Maste }
1679f2f44ceSEd Maste }
1689f2f44ceSEd Maste }
169ac7ddfbfSEd Maste return Searcher::eCallbackReturnStop;
170ac7ddfbfSEd Maste }
171ac7ddfbfSEd Maste
GetDepth()172*b5893f02SDimitry Andric lldb::SearchDepth BreakpointResolverAddress::GetDepth() {
173*b5893f02SDimitry Andric return lldb::eSearchDepthTarget;
174ac7ddfbfSEd Maste }
175ac7ddfbfSEd Maste
GetDescription(Stream * s)176435933ddSDimitry Andric void BreakpointResolverAddress::GetDescription(Stream *s) {
177ac7ddfbfSEd Maste s->PutCString("address = ");
178435933ddSDimitry Andric m_addr.Dump(s, m_breakpoint->GetTarget().GetProcessSP().get(),
179435933ddSDimitry Andric Address::DumpStyleModuleWithFileAddress,
180435933ddSDimitry Andric Address::DumpStyleLoadAddress);
181ac7ddfbfSEd Maste }
182ac7ddfbfSEd Maste
Dump(Stream * s) const183435933ddSDimitry Andric void BreakpointResolverAddress::Dump(Stream *s) const {}
1847aa51b79SEd Maste
1857aa51b79SEd Maste lldb::BreakpointResolverSP
CopyForBreakpoint(Breakpoint & breakpoint)186435933ddSDimitry Andric BreakpointResolverAddress::CopyForBreakpoint(Breakpoint &breakpoint) {
187435933ddSDimitry Andric lldb::BreakpointResolverSP ret_sp(
188435933ddSDimitry Andric new BreakpointResolverAddress(&breakpoint, m_addr));
1897aa51b79SEd Maste return ret_sp;
1907aa51b79SEd Maste }
191