2010-06-09 00:52:24 +08:00
|
|
|
//===-- SBInstruction.cpp ---------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "lldb/API/SBInstruction.h"
|
|
|
|
|
2010-10-06 11:09:58 +08:00
|
|
|
#include "lldb/API/SBAddress.h"
|
2011-04-06 07:22:54 +08:00
|
|
|
#include "lldb/API/SBFrame.h"
|
2010-10-06 11:09:58 +08:00
|
|
|
#include "lldb/API/SBInstruction.h"
|
|
|
|
#include "lldb/API/SBStream.h"
|
2011-04-06 07:22:54 +08:00
|
|
|
#include "lldb/API/SBTarget.h"
|
2010-10-06 11:09:58 +08:00
|
|
|
|
2011-04-06 07:22:54 +08:00
|
|
|
#include "lldb/Core/ArchSpec.h"
|
2010-06-09 00:52:24 +08:00
|
|
|
#include "lldb/Core/Disassembler.h"
|
2011-04-06 07:22:54 +08:00
|
|
|
#include "lldb/Core/EmulateInstruction.h"
|
2014-10-11 07:07:36 +08:00
|
|
|
#include "lldb/Core/Module.h"
|
2010-10-06 11:09:58 +08:00
|
|
|
#include "lldb/Core/StreamFile.h"
|
2011-04-06 07:22:54 +08:00
|
|
|
#include "lldb/Target/ExecutionContext.h"
|
2013-11-04 17:33:30 +08:00
|
|
|
#include "lldb/Target/StackFrame.h"
|
2011-04-06 07:22:54 +08:00
|
|
|
#include "lldb/Target/Target.h"
|
2017-03-04 09:30:05 +08:00
|
|
|
#include "lldb/Utility/DataBufferHeap.h"
|
|
|
|
#include "lldb/Utility/DataExtractor.h"
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2016-06-08 06:56:40 +08:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// We recently fixed a leak in one of the Instruction subclasses where
|
|
|
|
// the instruction will only hold a weak reference to the disassembler
|
|
|
|
// to avoid a cycle that was keeping both objects alive (leak) and we
|
|
|
|
// need the InstructionImpl class to make sure our public API behaves
|
|
|
|
// as users would expect. Calls in our public API allow clients to do
|
|
|
|
// things like:
|
|
|
|
//
|
|
|
|
// 1 lldb::SBInstruction inst;
|
|
|
|
// 2 inst = target.ReadInstructions(pc, 1).GetInstructionAtIndex(0)
|
|
|
|
// 3 if (inst.DoesBranch())
|
|
|
|
// 4 ...
|
|
|
|
//
|
|
|
|
// There was a temporary lldb::DisassemblerSP object created in the
|
|
|
|
// SBInstructionList that was returned by lldb.target.ReadInstructions()
|
|
|
|
// that will go away after line 2 but the "inst" object should be able
|
|
|
|
// to still answer questions about itself. So we make sure that any
|
|
|
|
// SBInstruction objects that are given out have a strong reference to
|
|
|
|
// the disassembler and the instruction so that the object can live and
|
|
|
|
// successfully respond to all queries.
|
|
|
|
//----------------------------------------------------------------------
|
2016-09-07 04:57:50 +08:00
|
|
|
class InstructionImpl {
|
2016-06-08 06:56:40 +08:00
|
|
|
public:
|
2016-09-07 04:57:50 +08:00
|
|
|
InstructionImpl(const lldb::DisassemblerSP &disasm_sp,
|
|
|
|
const lldb::InstructionSP &inst_sp)
|
|
|
|
: m_disasm_sp(disasm_sp), m_inst_sp(inst_sp) {}
|
2016-06-08 06:56:40 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
lldb::InstructionSP GetSP() const { return m_inst_sp; }
|
2016-06-08 06:56:40 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
bool IsValid() const { return (bool)m_inst_sp; }
|
2016-06-08 06:56:40 +08:00
|
|
|
|
|
|
|
protected:
|
2016-09-07 04:57:50 +08:00
|
|
|
lldb::DisassemblerSP m_disasm_sp; // Can be empty/invalid
|
|
|
|
lldb::InstructionSP m_inst_sp;
|
2016-06-08 06:56:40 +08:00
|
|
|
};
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
using namespace lldb;
|
|
|
|
using namespace lldb_private;
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
SBInstruction::SBInstruction() : m_opaque_sp() {}
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
SBInstruction::SBInstruction(const lldb::DisassemblerSP &disasm_sp,
|
|
|
|
const lldb::InstructionSP &inst_sp)
|
|
|
|
: m_opaque_sp(new InstructionImpl(disasm_sp, inst_sp)) {}
|
2010-10-06 11:09:58 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
SBInstruction::SBInstruction(const SBInstruction &rhs)
|
|
|
|
: m_opaque_sp(rhs.m_opaque_sp) {}
|
2010-11-06 07:17:00 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
const SBInstruction &SBInstruction::operator=(const SBInstruction &rhs) {
|
|
|
|
if (this != &rhs)
|
|
|
|
m_opaque_sp = rhs.m_opaque_sp;
|
|
|
|
return *this;
|
2010-11-06 07:17:00 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
SBInstruction::~SBInstruction() {}
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
bool SBInstruction::IsValid() { return m_opaque_sp && m_opaque_sp->IsValid(); }
|
2010-10-06 11:09:58 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
SBAddress SBInstruction::GetAddress() {
|
|
|
|
SBAddress sb_addr;
|
|
|
|
lldb::InstructionSP inst_sp(GetOpaque());
|
|
|
|
if (inst_sp && inst_sp->GetAddress().IsValid())
|
|
|
|
sb_addr.SetAddress(&inst_sp->GetAddress());
|
|
|
|
return sb_addr;
|
2010-10-06 11:09:58 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
const char *SBInstruction::GetMnemonic(SBTarget target) {
|
|
|
|
lldb::InstructionSP inst_sp(GetOpaque());
|
|
|
|
if (inst_sp) {
|
|
|
|
ExecutionContext exe_ctx;
|
|
|
|
TargetSP target_sp(target.GetSP());
|
|
|
|
std::unique_lock<std::recursive_mutex> lock;
|
|
|
|
if (target_sp) {
|
|
|
|
lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
|
|
|
|
|
|
|
|
target_sp->CalculateExecutionContext(exe_ctx);
|
|
|
|
exe_ctx.SetProcessSP(target_sp->GetProcessSP());
|
2011-09-26 15:11:27 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
return inst_sp->GetMnemonic(&exe_ctx);
|
|
|
|
}
|
|
|
|
return NULL;
|
2011-09-26 15:11:27 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
const char *SBInstruction::GetOperands(SBTarget target) {
|
|
|
|
lldb::InstructionSP inst_sp(GetOpaque());
|
|
|
|
if (inst_sp) {
|
|
|
|
ExecutionContext exe_ctx;
|
|
|
|
TargetSP target_sp(target.GetSP());
|
|
|
|
std::unique_lock<std::recursive_mutex> lock;
|
|
|
|
if (target_sp) {
|
|
|
|
lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
|
|
|
|
|
|
|
|
target_sp->CalculateExecutionContext(exe_ctx);
|
|
|
|
exe_ctx.SetProcessSP(target_sp->GetProcessSP());
|
2011-09-26 15:11:27 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
return inst_sp->GetOperands(&exe_ctx);
|
|
|
|
}
|
|
|
|
return NULL;
|
2011-09-26 15:11:27 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
const char *SBInstruction::GetComment(SBTarget target) {
|
|
|
|
lldb::InstructionSP inst_sp(GetOpaque());
|
|
|
|
if (inst_sp) {
|
|
|
|
ExecutionContext exe_ctx;
|
|
|
|
TargetSP target_sp(target.GetSP());
|
|
|
|
std::unique_lock<std::recursive_mutex> lock;
|
|
|
|
if (target_sp) {
|
|
|
|
lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
|
|
|
|
|
|
|
|
target_sp->CalculateExecutionContext(exe_ctx);
|
|
|
|
exe_ctx.SetProcessSP(target_sp->GetProcessSP());
|
2011-09-26 15:11:27 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
return inst_sp->GetComment(&exe_ctx);
|
|
|
|
}
|
|
|
|
return NULL;
|
2011-09-26 15:11:27 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
size_t SBInstruction::GetByteSize() {
|
|
|
|
lldb::InstructionSP inst_sp(GetOpaque());
|
|
|
|
if (inst_sp)
|
|
|
|
return inst_sp->GetOpcode().GetByteSize();
|
|
|
|
return 0;
|
2010-10-06 11:09:58 +08:00
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
SBData SBInstruction::GetData(SBTarget target) {
|
|
|
|
lldb::SBData sb_data;
|
|
|
|
lldb::InstructionSP inst_sp(GetOpaque());
|
|
|
|
if (inst_sp) {
|
|
|
|
DataExtractorSP data_extractor_sp(new DataExtractor());
|
|
|
|
if (inst_sp->GetData(*data_extractor_sp)) {
|
|
|
|
sb_data.SetOpaque(data_extractor_sp);
|
2011-09-26 15:11:27 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
return sb_data;
|
2011-09-26 15:11:27 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
bool SBInstruction::DoesBranch() {
|
|
|
|
lldb::InstructionSP inst_sp(GetOpaque());
|
|
|
|
if (inst_sp)
|
|
|
|
return inst_sp->DoesBranch();
|
|
|
|
return false;
|
2010-10-06 11:09:58 +08:00
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
bool SBInstruction::HasDelaySlot() {
|
|
|
|
lldb::InstructionSP inst_sp(GetOpaque());
|
|
|
|
if (inst_sp)
|
|
|
|
return inst_sp->HasDelaySlot();
|
|
|
|
return false;
|
2016-01-27 18:16:30 +08:00
|
|
|
}
|
|
|
|
|
2017-05-04 19:34:42 +08:00
|
|
|
bool SBInstruction::CanSetBreakpoint () {
|
|
|
|
lldb::InstructionSP inst_sp(GetOpaque());
|
|
|
|
if (inst_sp)
|
|
|
|
return inst_sp->CanSetBreakpoint();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
lldb::InstructionSP SBInstruction::GetOpaque() {
|
|
|
|
if (m_opaque_sp)
|
|
|
|
return m_opaque_sp->GetSP();
|
|
|
|
else
|
|
|
|
return lldb::InstructionSP();
|
2016-06-08 06:56:40 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
void SBInstruction::SetOpaque(const lldb::DisassemblerSP &disasm_sp,
|
|
|
|
const lldb::InstructionSP &inst_sp) {
|
|
|
|
m_opaque_sp.reset(new InstructionImpl(disasm_sp, inst_sp));
|
2010-10-06 11:09:58 +08:00
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
bool SBInstruction::GetDescription(lldb::SBStream &s) {
|
|
|
|
lldb::InstructionSP inst_sp(GetOpaque());
|
|
|
|
if (inst_sp) {
|
|
|
|
SymbolContext sc;
|
|
|
|
const Address &addr = inst_sp->GetAddress();
|
|
|
|
ModuleSP module_sp(addr.GetModule());
|
|
|
|
if (module_sp)
|
|
|
|
module_sp->ResolveSymbolContextForAddress(addr, eSymbolContextEverything,
|
|
|
|
sc);
|
|
|
|
// Use the "ref()" instead of the "get()" accessor in case the SBStream
|
|
|
|
// didn't have a stream already created, one will get created...
|
|
|
|
FormatEntity::Entry format;
|
|
|
|
FormatEntity::Parse("${addr}: ", format);
|
|
|
|
inst_sp->Dump(&s.ref(), 0, true, false, NULL, &sc, NULL, &format, 0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2010-10-06 11:09:58 +08:00
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
void SBInstruction::Print(FILE *out) {
|
|
|
|
if (out == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
lldb::InstructionSP inst_sp(GetOpaque());
|
|
|
|
if (inst_sp) {
|
|
|
|
SymbolContext sc;
|
|
|
|
const Address &addr = inst_sp->GetAddress();
|
|
|
|
ModuleSP module_sp(addr.GetModule());
|
|
|
|
if (module_sp)
|
|
|
|
module_sp->ResolveSymbolContextForAddress(addr, eSymbolContextEverything,
|
|
|
|
sc);
|
|
|
|
StreamFile out_stream(out, false);
|
|
|
|
FormatEntity::Entry format;
|
|
|
|
FormatEntity::Parse("${addr}: ", format);
|
|
|
|
inst_sp->Dump(&out_stream, 0, true, false, NULL, &sc, NULL, &format, 0);
|
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
2011-04-06 07:22:54 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
bool SBInstruction::EmulateWithFrame(lldb::SBFrame &frame,
|
|
|
|
uint32_t evaluate_options) {
|
|
|
|
lldb::InstructionSP inst_sp(GetOpaque());
|
|
|
|
if (inst_sp) {
|
|
|
|
lldb::StackFrameSP frame_sp(frame.GetFrameSP());
|
|
|
|
|
|
|
|
if (frame_sp) {
|
|
|
|
lldb_private::ExecutionContext exe_ctx;
|
|
|
|
frame_sp->CalculateExecutionContext(exe_ctx);
|
|
|
|
lldb_private::Target *target = exe_ctx.GetTargetPtr();
|
|
|
|
lldb_private::ArchSpec arch = target->GetArchitecture();
|
|
|
|
|
|
|
|
return inst_sp->Emulate(
|
|
|
|
arch, evaluate_options, (void *)frame_sp.get(),
|
|
|
|
&lldb_private::EmulateInstruction::ReadMemoryFrame,
|
|
|
|
&lldb_private::EmulateInstruction::WriteMemoryFrame,
|
|
|
|
&lldb_private::EmulateInstruction::ReadRegisterFrame,
|
|
|
|
&lldb_private::EmulateInstruction::WriteRegisterFrame);
|
2011-04-06 07:22:54 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
return false;
|
2011-04-06 07:22:54 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
bool SBInstruction::DumpEmulation(const char *triple) {
|
|
|
|
lldb::InstructionSP inst_sp(GetOpaque());
|
|
|
|
if (inst_sp && triple) {
|
|
|
|
lldb_private::ArchSpec arch(triple, NULL);
|
|
|
|
return inst_sp->DumpEmulation(arch);
|
|
|
|
}
|
|
|
|
return false;
|
2011-04-06 07:22:54 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
bool SBInstruction::TestEmulation(lldb::SBStream &output_stream,
|
|
|
|
const char *test_file) {
|
|
|
|
if (!m_opaque_sp)
|
|
|
|
SetOpaque(lldb::DisassemblerSP(),
|
|
|
|
lldb::InstructionSP(new PseudoInstruction()));
|
2016-06-08 06:56:40 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
lldb::InstructionSP inst_sp(GetOpaque());
|
|
|
|
if (inst_sp)
|
|
|
|
return inst_sp->TestEmulation(output_stream.get(), test_file);
|
|
|
|
return false;
|
2011-04-22 04:27:45 +08:00
|
|
|
}
|
2012-04-13 08:07:34 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
lldb::AddressClass SBInstruction::GetAddressClass() {
|
|
|
|
lldb::InstructionSP inst_sp(GetOpaque());
|
|
|
|
if (inst_sp)
|
|
|
|
return inst_sp->GetAddressClass();
|
|
|
|
return eAddressClassInvalid;
|
2012-04-13 08:07:34 +08:00
|
|
|
}
|