15ffd83dbSDimitry Andric //===-- BreakpointResolverAddress.cpp -------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointResolverAddress.h"
100b57cec5SDimitry Andric #include "lldb/Breakpoint/BreakpointLocation.h"
110b57cec5SDimitry Andric #include "lldb/Core/Module.h"
120b57cec5SDimitry Andric #include "lldb/Core/Section.h"
130b57cec5SDimitry Andric #include "lldb/Target/Process.h"
140b57cec5SDimitry Andric #include "lldb/Target/Target.h"
1581ad6265SDimitry Andric #include "lldb/Utility/LLDBLog.h"
160b57cec5SDimitry Andric #include "lldb/Utility/StreamString.h"
170b57cec5SDimitry Andric
180b57cec5SDimitry Andric using namespace lldb;
190b57cec5SDimitry Andric using namespace lldb_private;
200b57cec5SDimitry Andric
210b57cec5SDimitry Andric // BreakpointResolverAddress:
BreakpointResolverAddress(const BreakpointSP & bkpt,const Address & addr,const FileSpec & module_spec)220b57cec5SDimitry Andric BreakpointResolverAddress::BreakpointResolverAddress(
235ffd83dbSDimitry Andric const BreakpointSP &bkpt, const Address &addr, const FileSpec &module_spec)
240b57cec5SDimitry Andric : BreakpointResolver(bkpt, BreakpointResolver::AddressResolver),
250b57cec5SDimitry Andric m_addr(addr), m_resolved_addr(LLDB_INVALID_ADDRESS),
260b57cec5SDimitry Andric m_module_filespec(module_spec) {}
270b57cec5SDimitry Andric
BreakpointResolverAddress(const BreakpointSP & bkpt,const Address & addr)285ffd83dbSDimitry Andric BreakpointResolverAddress::BreakpointResolverAddress(const BreakpointSP &bkpt,
290b57cec5SDimitry Andric const Address &addr)
300b57cec5SDimitry Andric : BreakpointResolver(bkpt, BreakpointResolver::AddressResolver),
3181ad6265SDimitry Andric m_addr(addr), m_resolved_addr(LLDB_INVALID_ADDRESS) {}
320b57cec5SDimitry Andric
CreateFromStructuredData(const StructuredData::Dictionary & options_dict,Status & error)33c9157d92SDimitry Andric BreakpointResolverSP BreakpointResolverAddress::CreateFromStructuredData(
34c9157d92SDimitry Andric const StructuredData::Dictionary &options_dict, Status &error) {
350b57cec5SDimitry Andric llvm::StringRef module_name;
36fe013be4SDimitry Andric lldb::offset_t addr_offset;
370b57cec5SDimitry Andric FileSpec module_filespec;
380b57cec5SDimitry Andric bool success;
390b57cec5SDimitry Andric
400b57cec5SDimitry Andric success = options_dict.GetValueForKeyAsInteger(
410b57cec5SDimitry Andric GetKey(OptionNames::AddressOffset), addr_offset);
420b57cec5SDimitry Andric if (!success) {
430b57cec5SDimitry Andric error.SetErrorString("BRFL::CFSD: Couldn't find address offset entry.");
440b57cec5SDimitry Andric return nullptr;
450b57cec5SDimitry Andric }
460b57cec5SDimitry Andric Address address(addr_offset);
470b57cec5SDimitry Andric
480b57cec5SDimitry Andric success = options_dict.HasKey(GetKey(OptionNames::ModuleName));
490b57cec5SDimitry Andric if (success) {
500b57cec5SDimitry Andric success = options_dict.GetValueForKeyAsString(
510b57cec5SDimitry Andric GetKey(OptionNames::ModuleName), module_name);
520b57cec5SDimitry Andric if (!success) {
530b57cec5SDimitry Andric error.SetErrorString("BRA::CFSD: Couldn't read module name entry.");
540b57cec5SDimitry Andric return nullptr;
550b57cec5SDimitry Andric }
560b57cec5SDimitry Andric module_filespec.SetFile(module_name, FileSpec::Style::native);
570b57cec5SDimitry Andric }
58c9157d92SDimitry Andric return std::make_shared<BreakpointResolverAddress>(nullptr, address,
59c9157d92SDimitry Andric module_filespec);
600b57cec5SDimitry Andric }
610b57cec5SDimitry Andric
620b57cec5SDimitry Andric StructuredData::ObjectSP
SerializeToStructuredData()630b57cec5SDimitry Andric BreakpointResolverAddress::SerializeToStructuredData() {
640b57cec5SDimitry Andric StructuredData::DictionarySP options_dict_sp(
650b57cec5SDimitry Andric new StructuredData::Dictionary());
660b57cec5SDimitry Andric SectionSP section_sp = m_addr.GetSection();
670b57cec5SDimitry Andric if (section_sp) {
68*cdc20ff6SDimitry Andric if (ModuleSP module_sp = section_sp->GetModule()) {
69*cdc20ff6SDimitry Andric const FileSpec &module_fspec = module_sp->GetFileSpec();
700b57cec5SDimitry Andric options_dict_sp->AddStringItem(GetKey(OptionNames::ModuleName),
71*cdc20ff6SDimitry Andric module_fspec.GetPath().c_str());
72*cdc20ff6SDimitry Andric }
730b57cec5SDimitry Andric options_dict_sp->AddIntegerItem(GetKey(OptionNames::AddressOffset),
740b57cec5SDimitry Andric m_addr.GetOffset());
750b57cec5SDimitry Andric } else {
760b57cec5SDimitry Andric options_dict_sp->AddIntegerItem(GetKey(OptionNames::AddressOffset),
770b57cec5SDimitry Andric m_addr.GetOffset());
780b57cec5SDimitry Andric if (m_module_filespec) {
790b57cec5SDimitry Andric options_dict_sp->AddStringItem(GetKey(OptionNames::ModuleName),
800b57cec5SDimitry Andric m_module_filespec.GetPath());
810b57cec5SDimitry Andric }
820b57cec5SDimitry Andric }
830b57cec5SDimitry Andric
840b57cec5SDimitry Andric return WrapOptionsDict(options_dict_sp);
850b57cec5SDimitry Andric }
860b57cec5SDimitry Andric
ResolveBreakpoint(SearchFilter & filter)870b57cec5SDimitry Andric void BreakpointResolverAddress::ResolveBreakpoint(SearchFilter &filter) {
880b57cec5SDimitry Andric // If the address is not section relative, then we should not try to re-
890b57cec5SDimitry Andric // resolve it, it is just some random address and we wouldn't know what to do
900b57cec5SDimitry Andric // on reload. But if it is section relative, we need to re-resolve it since
910b57cec5SDimitry Andric // the section it's in may have shifted on re-run.
920b57cec5SDimitry Andric bool re_resolve = false;
930b57cec5SDimitry Andric if (m_addr.GetSection() || m_module_filespec)
940b57cec5SDimitry Andric re_resolve = true;
955ffd83dbSDimitry Andric else if (GetBreakpoint()->GetNumLocations() == 0)
960b57cec5SDimitry Andric re_resolve = true;
970b57cec5SDimitry Andric
980b57cec5SDimitry Andric if (re_resolve)
990b57cec5SDimitry Andric BreakpointResolver::ResolveBreakpoint(filter);
1000b57cec5SDimitry Andric }
1010b57cec5SDimitry Andric
ResolveBreakpointInModules(SearchFilter & filter,ModuleList & modules)1020b57cec5SDimitry Andric void BreakpointResolverAddress::ResolveBreakpointInModules(
1030b57cec5SDimitry Andric SearchFilter &filter, ModuleList &modules) {
1040b57cec5SDimitry Andric // See comment in ResolveBreakpoint.
1050b57cec5SDimitry Andric bool re_resolve = false;
1060b57cec5SDimitry Andric if (m_addr.GetSection())
1070b57cec5SDimitry Andric re_resolve = true;
1085ffd83dbSDimitry Andric else if (GetBreakpoint()->GetNumLocations() == 0)
1090b57cec5SDimitry Andric re_resolve = true;
1100b57cec5SDimitry Andric
1110b57cec5SDimitry Andric if (re_resolve)
1120b57cec5SDimitry Andric BreakpointResolver::ResolveBreakpointInModules(filter, modules);
1130b57cec5SDimitry Andric }
1140b57cec5SDimitry Andric
SearchCallback(SearchFilter & filter,SymbolContext & context,Address * addr)1159dba64beSDimitry Andric Searcher::CallbackReturn BreakpointResolverAddress::SearchCallback(
1169dba64beSDimitry Andric SearchFilter &filter, SymbolContext &context, Address *addr) {
1175ffd83dbSDimitry Andric BreakpointSP breakpoint_sp = GetBreakpoint();
1185ffd83dbSDimitry Andric Breakpoint &breakpoint = *breakpoint_sp;
1190b57cec5SDimitry Andric
1200b57cec5SDimitry Andric if (filter.AddressPasses(m_addr)) {
1215ffd83dbSDimitry Andric if (breakpoint.GetNumLocations() == 0) {
1220b57cec5SDimitry Andric // If the address is just an offset, and we're given a module, see if we
1230b57cec5SDimitry Andric // can find the appropriate module loaded in the binary, and fix up
1240b57cec5SDimitry Andric // m_addr to use that.
1250b57cec5SDimitry Andric if (!m_addr.IsSectionOffset() && m_module_filespec) {
1265ffd83dbSDimitry Andric Target &target = breakpoint.GetTarget();
1270b57cec5SDimitry Andric ModuleSpec module_spec(m_module_filespec);
1280b57cec5SDimitry Andric ModuleSP module_sp = target.GetImages().FindFirstModule(module_spec);
1290b57cec5SDimitry Andric if (module_sp) {
1300b57cec5SDimitry Andric Address tmp_address;
1310b57cec5SDimitry Andric if (module_sp->ResolveFileAddress(m_addr.GetOffset(), tmp_address))
1320b57cec5SDimitry Andric m_addr = tmp_address;
1330b57cec5SDimitry Andric }
1340b57cec5SDimitry Andric }
1350b57cec5SDimitry Andric
1365ffd83dbSDimitry Andric m_resolved_addr = m_addr.GetLoadAddress(&breakpoint.GetTarget());
1370b57cec5SDimitry Andric BreakpointLocationSP bp_loc_sp(AddLocation(m_addr));
1385ffd83dbSDimitry Andric if (bp_loc_sp && !breakpoint.IsInternal()) {
1390b57cec5SDimitry Andric StreamString s;
1400b57cec5SDimitry Andric bp_loc_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
14181ad6265SDimitry Andric Log *log = GetLog(LLDBLog::Breakpoints);
1429dba64beSDimitry Andric LLDB_LOGF(log, "Added location: %s\n", s.GetData());
1430b57cec5SDimitry Andric }
1440b57cec5SDimitry Andric } else {
1455ffd83dbSDimitry Andric BreakpointLocationSP loc_sp = breakpoint.GetLocationAtIndex(0);
1460b57cec5SDimitry Andric lldb::addr_t cur_load_location =
1475ffd83dbSDimitry Andric m_addr.GetLoadAddress(&breakpoint.GetTarget());
1480b57cec5SDimitry Andric if (cur_load_location != m_resolved_addr) {
1490b57cec5SDimitry Andric m_resolved_addr = cur_load_location;
1500b57cec5SDimitry Andric loc_sp->ClearBreakpointSite();
1510b57cec5SDimitry Andric loc_sp->ResolveBreakpointSite();
1520b57cec5SDimitry Andric }
1530b57cec5SDimitry Andric }
1540b57cec5SDimitry Andric }
1550b57cec5SDimitry Andric return Searcher::eCallbackReturnStop;
1560b57cec5SDimitry Andric }
1570b57cec5SDimitry Andric
GetDepth()1580b57cec5SDimitry Andric lldb::SearchDepth BreakpointResolverAddress::GetDepth() {
1590b57cec5SDimitry Andric return lldb::eSearchDepthTarget;
1600b57cec5SDimitry Andric }
1610b57cec5SDimitry Andric
GetDescription(Stream * s)1620b57cec5SDimitry Andric void BreakpointResolverAddress::GetDescription(Stream *s) {
1630b57cec5SDimitry Andric s->PutCString("address = ");
1645ffd83dbSDimitry Andric m_addr.Dump(s, GetBreakpoint()->GetTarget().GetProcessSP().get(),
1650b57cec5SDimitry Andric Address::DumpStyleModuleWithFileAddress,
1660b57cec5SDimitry Andric Address::DumpStyleLoadAddress);
1670b57cec5SDimitry Andric }
1680b57cec5SDimitry Andric
Dump(Stream * s) const1690b57cec5SDimitry Andric void BreakpointResolverAddress::Dump(Stream *s) const {}
1700b57cec5SDimitry Andric
1710b57cec5SDimitry Andric lldb::BreakpointResolverSP
CopyForBreakpoint(BreakpointSP & breakpoint)1725ffd83dbSDimitry Andric BreakpointResolverAddress::CopyForBreakpoint(BreakpointSP &breakpoint) {
1730b57cec5SDimitry Andric lldb::BreakpointResolverSP ret_sp(
1745ffd83dbSDimitry Andric new BreakpointResolverAddress(breakpoint, m_addr));
1750b57cec5SDimitry Andric return ret_sp;
1760b57cec5SDimitry Andric }
177