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 } 91 92 void BreakpointResolverAddress::ResolveBreakpoint(SearchFilter &filter) { 93 // If the address is not section relative, then we should not try to re- 94 // resolve it, it is just some random address and we wouldn't know what to do 95 // on reload. But if it is section relative, we need to re-resolve it since 96 // the section it's in may have shifted on re-run. 97 bool re_resolve = false; 98 if (m_addr.GetSection() || m_module_filespec) 99 re_resolve = true; 100 else if (GetBreakpoint()->GetNumLocations() == 0) 101 re_resolve = true; 102 103 if (re_resolve) 104 BreakpointResolver::ResolveBreakpoint(filter); 105 } 106 107 void BreakpointResolverAddress::ResolveBreakpointInModules( 108 SearchFilter &filter, ModuleList &modules) { 109 // See comment in ResolveBreakpoint. 110 bool re_resolve = false; 111 if (m_addr.GetSection()) 112 re_resolve = true; 113 else if (GetBreakpoint()->GetNumLocations() == 0) 114 re_resolve = true; 115 116 if (re_resolve) 117 BreakpointResolver::ResolveBreakpointInModules(filter, modules); 118 } 119 120 Searcher::CallbackReturn BreakpointResolverAddress::SearchCallback( 121 SearchFilter &filter, SymbolContext &context, Address *addr) { 122 BreakpointSP breakpoint_sp = GetBreakpoint(); 123 Breakpoint &breakpoint = *breakpoint_sp; 124 125 if (filter.AddressPasses(m_addr)) { 126 if (breakpoint.GetNumLocations() == 0) { 127 // If the address is just an offset, and we're given a module, see if we 128 // can find the appropriate module loaded in the binary, and fix up 129 // m_addr to use that. 130 if (!m_addr.IsSectionOffset() && m_module_filespec) { 131 Target &target = breakpoint.GetTarget(); 132 ModuleSpec module_spec(m_module_filespec); 133 ModuleSP module_sp = target.GetImages().FindFirstModule(module_spec); 134 if (module_sp) { 135 Address tmp_address; 136 if (module_sp->ResolveFileAddress(m_addr.GetOffset(), tmp_address)) 137 m_addr = tmp_address; 138 } 139 } 140 141 m_resolved_addr = m_addr.GetLoadAddress(&breakpoint.GetTarget()); 142 BreakpointLocationSP bp_loc_sp(AddLocation(m_addr)); 143 if (bp_loc_sp && !breakpoint.IsInternal()) { 144 StreamString s; 145 bp_loc_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose); 146 Log *log( 147 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS)); 148 LLDB_LOGF(log, "Added location: %s\n", s.GetData()); 149 } 150 } else { 151 BreakpointLocationSP loc_sp = breakpoint.GetLocationAtIndex(0); 152 lldb::addr_t cur_load_location = 153 m_addr.GetLoadAddress(&breakpoint.GetTarget()); 154 if (cur_load_location != m_resolved_addr) { 155 m_resolved_addr = cur_load_location; 156 loc_sp->ClearBreakpointSite(); 157 loc_sp->ResolveBreakpointSite(); 158 } 159 } 160 } 161 return Searcher::eCallbackReturnStop; 162 } 163 164 lldb::SearchDepth BreakpointResolverAddress::GetDepth() { 165 return lldb::eSearchDepthTarget; 166 } 167 168 void BreakpointResolverAddress::GetDescription(Stream *s) { 169 s->PutCString("address = "); 170 m_addr.Dump(s, GetBreakpoint()->GetTarget().GetProcessSP().get(), 171 Address::DumpStyleModuleWithFileAddress, 172 Address::DumpStyleLoadAddress); 173 } 174 175 void BreakpointResolverAddress::Dump(Stream *s) const {} 176 177 lldb::BreakpointResolverSP 178 BreakpointResolverAddress::CopyForBreakpoint(BreakpointSP &breakpoint) { 179 lldb::BreakpointResolverSP ret_sp( 180 new BreakpointResolverAddress(breakpoint, m_addr)); 181 return ret_sp; 182 } 183