Skip to content

Commit 0c7a7dc

Browse files
committed
add instructions_offset to breakppoint resolver
1 parent 4d48545 commit 0c7a7dc

File tree

11 files changed

+89
-34
lines changed

11 files changed

+89
-34
lines changed

lldb/include/lldb/API/SBTarget.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "lldb/API/SBValue.h"
2424
#include "lldb/API/SBWatchpoint.h"
2525
#include "lldb/API/SBWatchpointOptions.h"
26+
#include "lldb/lldb-types.h"
2627

2728
namespace lldb_private {
2829
namespace python {
@@ -738,7 +739,7 @@ class LLDB_API SBTarget {
738739
lldb::SBBreakpoint BreakpointCreateBySBAddress(SBAddress &address);
739740

740741
lldb::SBBreakpoint BreakpointCreateByFileAddress(const SBFileSpec &file_spec,
741-
addr_t file_addr);
742+
addr_t file_addr, addr_t offset = 0, addr_t instructions_offset = 0);
742743

743744
/// Create a breakpoint using a scripted resolver.
744745
///

lldb/include/lldb/Breakpoint/BreakpointResolver.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "lldb/Utility/FileSpec.h"
1717
#include "lldb/Utility/RegularExpression.h"
1818
#include "lldb/lldb-private.h"
19+
#include "lldb/lldb-types.h"
1920
#include <optional>
2021

2122
namespace lldb_private {
@@ -47,7 +48,8 @@ class BreakpointResolver : public Searcher {
4748
/// The concrete breakpoint resolver type for this breakpoint.
4849
BreakpointResolver(const lldb::BreakpointSP &bkpt,
4950
unsigned char resolverType,
50-
lldb::addr_t offset = 0);
51+
lldb::addr_t offset = 0,
52+
lldb::addr_t instructions_offset = 0);
5153

5254
/// The Destructor is virtual, all significant breakpoint resolvers derive
5355
/// from this class.
@@ -182,6 +184,7 @@ class BreakpointResolver : public Searcher {
182184
SearchDepth,
183185
SkipPrologue,
184186
SymbolNameArray,
187+
InstructionsOffset,
185188
LastOptionName
186189
};
187190
static const char
@@ -220,6 +223,7 @@ class BreakpointResolver : public Searcher {
220223
lldb::BreakpointWP m_breakpoint; // This is the breakpoint we add locations to.
221224
lldb::addr_t m_offset; // A random offset the user asked us to add to any
222225
// breakpoints we set.
226+
lldb::addr_t m_instructions_offset; // Number of instructions to add to the resolved breakpoint address.
223227

224228
// Subclass identifier (for llvm isa/dyn_cast)
225229
const unsigned char SubclassID;

lldb/include/lldb/Breakpoint/BreakpointResolverAddress.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ class BreakpointResolverAddress : public BreakpointResolver {
2828
const Address &addr,
2929
const FileSpec &module_spec);
3030

31+
BreakpointResolverAddress(const lldb::BreakpointSP &bkpt,
32+
const Address &addr,
33+
const FileSpec &module_spec,
34+
lldb::addr_t offset,
35+
lldb::addr_t instructions_offset);
36+
3137
~BreakpointResolverAddress() override = default;
3238

3339
static lldb::BreakpointResolverSP

lldb/include/lldb/Core/Disassembler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,8 @@ class InstructionList {
291291

292292
size_t GetSize() const;
293293

294+
size_t GetTotalByteSize() const;
295+
294296
uint32_t GetMaxOpcocdeByteSize() const;
295297

296298
lldb::InstructionSP GetInstructionAtIndex(size_t idx) const;

lldb/include/lldb/Target/Target.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "lldb/Breakpoint/BreakpointList.h"
1919
#include "lldb/Breakpoint/BreakpointName.h"
2020
#include "lldb/Breakpoint/WatchpointList.h"
21+
#include "lldb/Core/Address.h"
2122
#include "lldb/Core/Architecture.h"
2223
#include "lldb/Core/Disassembler.h"
2324
#include "lldb/Core/ModuleList.h"
@@ -727,7 +728,9 @@ class Target : public std::enable_shared_from_this<Target>,
727728
lldb::BreakpointSP CreateAddressInModuleBreakpoint(lldb::addr_t file_addr,
728729
bool internal,
729730
const FileSpec &file_spec,
730-
bool request_hardware);
731+
bool request_hardware,
732+
lldb::addr_t offset = 0,
733+
lldb::addr_t instructions_offset = 0);
731734

732735
// Use this to create Address breakpoints:
733736
lldb::BreakpointSP CreateBreakpoint(const Address &addr, bool internal,
@@ -1328,6 +1331,10 @@ class Target : public std::enable_shared_from_this<Target>,
13281331
const lldb_private::RegisterFlags &flags,
13291332
uint32_t byte_size);
13301333

1334+
lldb::DisassemblerSP ReadInstructions(const Address &start_addr,
1335+
uint32_t count,
1336+
const char *flavor_string = nullptr);
1337+
13311338
// Target Stop Hooks
13321339
class StopHook : public UserID {
13331340
public:

lldb/source/API/SBTarget.cpp

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -950,14 +950,14 @@ SBBreakpoint SBTarget::BreakpointCreateBySBAddress(SBAddress &sb_address) {
950950
return sb_bp;
951951
}
952952

953-
SBBreakpoint SBTarget::BreakpointCreateByFileAddress(const SBFileSpec &file_spec, addr_t file_addr) {
953+
SBBreakpoint SBTarget::BreakpointCreateByFileAddress(const SBFileSpec &file_spec, addr_t file_addr, addr_t offset, addr_t instructions_offset) {
954954
LLDB_INSTRUMENT_VA(this, file_spec, file_addr);
955955

956956
SBBreakpoint sb_bp;
957957
if (TargetSP target_sp = GetSP()) {
958958
std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
959959
const bool hardware = false;
960-
sb_bp = target_sp->CreateAddressInModuleBreakpoint(file_addr, false, *file_spec.get(), hardware);
960+
sb_bp = target_sp->CreateAddressInModuleBreakpoint(file_addr, false, *file_spec.get(), hardware, offset, instructions_offset);
961961
}
962962

963963
return sb_bp;
@@ -1969,29 +1969,8 @@ lldb::SBInstructionList SBTarget::ReadInstructions(lldb::SBAddress base_addr,
19691969

19701970
if (TargetSP target_sp = GetSP()) {
19711971
if (Address *addr_ptr = base_addr.get()) {
1972-
DataBufferHeap data(
1973-
target_sp->GetArchitecture().GetMaximumOpcodeByteSize() * count, 0);
1974-
bool force_live_memory = true;
1975-
lldb_private::Status error;
1976-
lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
1977-
const size_t bytes_read =
1978-
target_sp->ReadMemory(*addr_ptr, data.GetBytes(), data.GetByteSize(),
1979-
error, force_live_memory, &load_addr);
1980-
1981-
const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
1982-
if (!flavor_string || flavor_string[0] == '\0') {
1983-
// FIXME - we don't have the mechanism in place to do per-architecture
1984-
// settings. But since we know that for now we only support flavors on
1985-
// x86 & x86_64,
1986-
const llvm::Triple::ArchType arch =
1987-
target_sp->GetArchitecture().GetTriple().getArch();
1988-
if (arch == llvm::Triple::x86 || arch == llvm::Triple::x86_64)
1989-
flavor_string = target_sp->GetDisassemblyFlavor();
1990-
}
1991-
sb_instructions.SetDisassembler(Disassembler::DisassembleBytes(
1992-
target_sp->GetArchitecture(), nullptr, flavor_string,
1993-
target_sp->GetDisassemblyCPU(), target_sp->GetDisassemblyFeatures(),
1994-
*addr_ptr, data.GetBytes(), bytes_read, count, data_from_file));
1972+
sb_instructions.SetDisassembler(target_sp->ReadInstructions(
1973+
*addr_ptr, count, flavor_string));
19951974
}
19961975
}
19971976

lldb/source/Breakpoint/BreakpointResolver.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "lldb/Utility/Log.h"
3030
#include "lldb/Utility/Stream.h"
3131
#include "lldb/Utility/StreamString.h"
32+
#include "lldb/lldb-forward.h"
3233
#include <optional>
3334

3435
using namespace lldb_private;
@@ -45,7 +46,7 @@ const char *BreakpointResolver::g_option_names[static_cast<uint32_t>(
4546
"AddressOffset", "Exact", "FileName", "Inlines", "Language",
4647
"LineNumber", "Column", "ModuleName", "NameMask", "Offset",
4748
"PythonClass", "Regex", "ScriptArgs", "SectionName", "SearchDepth",
48-
"SkipPrologue", "SymbolNames"};
49+
"SkipPrologue", "SymbolNames", "InstructionsOffset"};
4950

5051
const char *BreakpointResolver::ResolverTyToName(enum ResolverTy type) {
5152
if (type > LastKnownResolverType)
@@ -65,8 +66,9 @@ BreakpointResolver::NameToResolverTy(llvm::StringRef name) {
6566

6667
BreakpointResolver::BreakpointResolver(const BreakpointSP &bkpt,
6768
const unsigned char resolverTy,
68-
lldb::addr_t offset)
69-
: m_breakpoint(bkpt), m_offset(offset), SubclassID(resolverTy) {}
69+
lldb::addr_t offset,
70+
lldb::addr_t instructions_offset)
71+
: m_breakpoint(bkpt), m_offset(offset), m_instructions_offset(instructions_offset), SubclassID(resolverTy) {}
7072

7173
BreakpointResolver::~BreakpointResolver() = default;
7274

@@ -364,6 +366,12 @@ void BreakpointResolver::AddLocation(SearchFilter &filter,
364366

365367
BreakpointLocationSP BreakpointResolver::AddLocation(Address loc_addr,
366368
bool *new_location) {
369+
if (m_instructions_offset != 0) {
370+
Target &target = GetBreakpoint()->GetTarget();
371+
const DisassemblerSP instructions = target.ReadInstructions(loc_addr, m_instructions_offset);
372+
loc_addr.Slide(instructions->GetInstructionList().GetTotalByteSize());
373+
}
374+
367375
loc_addr.Slide(m_offset);
368376
return GetBreakpoint()->AddLocation(loc_addr, new_location);
369377
}

lldb/source/Breakpoint/BreakpointResolverAddress.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ BreakpointResolverAddress::BreakpointResolverAddress(const BreakpointSP &bkpt,
3030
: BreakpointResolver(bkpt, BreakpointResolver::AddressResolver),
3131
m_addr(addr), m_resolved_addr(LLDB_INVALID_ADDRESS) {}
3232

33+
BreakpointResolverAddress::BreakpointResolverAddress(
34+
const BreakpointSP &bkpt, const Address &addr, const FileSpec &module_spec, lldb::addr_t offset, lldb::addr_t instructions_offset)
35+
: BreakpointResolver(bkpt, BreakpointResolver::AddressResolver, offset, instructions_offset),
36+
m_addr(addr), m_resolved_addr(LLDB_INVALID_ADDRESS),
37+
m_module_filespec(module_spec) {}
38+
3339
BreakpointResolverSP BreakpointResolverAddress::CreateFromStructuredData(
3440
const StructuredData::Dictionary &options_dict, Status &error) {
3541
llvm::StringRef module_name;

lldb/source/Core/Disassembler.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,16 @@ uint32_t InstructionList::GetMaxOpcocdeByteSize() const {
10161016
return max_inst_size;
10171017
}
10181018

1019+
size_t InstructionList::GetTotalByteSize() const {
1020+
size_t total_byte_size = 0;
1021+
collection::const_iterator pos, end;
1022+
for (pos = m_instructions.begin(), end = m_instructions.end(); pos != end;
1023+
++pos) {
1024+
total_byte_size += (*pos)->GetOpcode().GetByteSize();
1025+
}
1026+
return total_byte_size;
1027+
}
1028+
10191029
InstructionSP InstructionList::GetInstructionAtIndex(size_t idx) const {
10201030
InstructionSP inst_sp;
10211031
if (idx < m_instructions.size())

lldb/source/Target/Target.cpp

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
#include "lldb/Utility/State.h"
6666
#include "lldb/Utility/StreamString.h"
6767
#include "lldb/Utility/Timer.h"
68+
#include "lldb/lldb-forward.h"
6869

6970
#include "llvm/ADT/ScopeExit.h"
7071
#include "llvm/ADT/SetVector.h"
@@ -567,11 +568,13 @@ BreakpointSP Target::CreateBreakpoint(const Address &addr, bool internal,
567568
lldb::BreakpointSP
568569
Target::CreateAddressInModuleBreakpoint(lldb::addr_t file_addr, bool internal,
569570
const FileSpec &file_spec,
570-
bool request_hardware) {
571+
bool request_hardware,
572+
lldb::addr_t offset,
573+
lldb::addr_t instructions_offset) {
571574
SearchFilterSP filter_sp(
572575
new SearchFilterForUnconstrainedSearches(shared_from_this()));
573576
BreakpointResolverSP resolver_sp(new BreakpointResolverAddress(
574-
nullptr, file_addr, file_spec));
577+
nullptr, file_addr, file_spec, offset, instructions_offset));
575578
return CreateBreakpoint(filter_sp, resolver_sp, internal, request_hardware,
576579
false);
577580
}
@@ -2990,6 +2993,35 @@ lldb::addr_t Target::GetBreakableLoadAddress(lldb::addr_t addr) {
29902993
return arch_plugin ? arch_plugin->GetBreakableLoadAddress(addr, *this) : addr;
29912994
}
29922995

2996+
lldb::DisassemblerSP Target::ReadInstructions(const Address &start_addr, uint32_t count,
2997+
const char *flavor_string) {
2998+
if (!m_process_sp)
2999+
return lldb::DisassemblerSP();
3000+
3001+
DataBufferHeap data(GetArchitecture().GetMaximumOpcodeByteSize() * count, 0);
3002+
bool force_live_memory = true;
3003+
lldb_private::Status error;
3004+
lldb::addr_t load_addr = LLDB_INVALID_ADDRESS;
3005+
const size_t bytes_read = ReadMemory(start_addr, data.GetBytes(), data.GetByteSize(),
3006+
error, force_live_memory, &load_addr);
3007+
3008+
const bool data_from_file = load_addr == LLDB_INVALID_ADDRESS;
3009+
if (!flavor_string || flavor_string[0] == '\0') {
3010+
// FIXME - we don't have the mechanism in place to do per-architecture
3011+
// settings. But since we know that for now we only support flavors on
3012+
// x86 & x86_64,
3013+
const llvm::Triple::ArchType arch =
3014+
GetArchitecture().GetTriple().getArch();
3015+
if (arch == llvm::Triple::x86 || arch == llvm::Triple::x86_64)
3016+
flavor_string = GetDisassemblyFlavor();
3017+
}
3018+
3019+
return Disassembler::DisassembleBytes(
3020+
GetArchitecture(), nullptr, flavor_string,
3021+
GetDisassemblyCPU(), GetDisassemblyFeatures(),
3022+
start_addr, data.GetBytes(), bytes_read, count, data_from_file);
3023+
}
3024+
29933025
SourceManager &Target::GetSourceManager() {
29943026
if (!m_source_manager_up)
29953027
m_source_manager_up = std::make_unique<SourceManager>(shared_from_this());

0 commit comments

Comments
 (0)