blob: 462f082880920f96828366d309307cd66b6c2a1d [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SBInstruction.cpp ---------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/API/SBInstruction.h"
11
Greg Clayton1d273162010-10-06 03:09:58 +000012#include "lldb/API/SBAddress.h"
Caroline Tice7c9dd3c2011-04-05 23:22:54 +000013#include "lldb/API/SBFrame.h"
Greg Clayton1d273162010-10-06 03:09:58 +000014#include "lldb/API/SBInstruction.h"
15#include "lldb/API/SBStream.h"
Caroline Tice7c9dd3c2011-04-05 23:22:54 +000016#include "lldb/API/SBTarget.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/Core/Disassembler.h"
Caroline Tice7c9dd3c2011-04-05 23:22:54 +000018#include "lldb/Core/EmulateInstruction.h"
Jason Molendaaff1b352014-10-10 23:07:36 +000019#include "lldb/Core/Module.h"
Greg Clayton1d273162010-10-06 03:09:58 +000020#include "lldb/Core/StreamFile.h"
Pavel Labath7263f1b2017-10-31 10:56:03 +000021#include "lldb/Host/HostInfo.h"
Caroline Tice7c9dd3c2011-04-05 23:22:54 +000022#include "lldb/Target/ExecutionContext.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000023#include "lldb/Target/StackFrame.h"
Caroline Tice7c9dd3c2011-04-05 23:22:54 +000024#include "lldb/Target/Target.h"
Pavel Labath5f19b902017-11-13 16:16:33 +000025#include "lldb/Utility/ArchSpec.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000026#include "lldb/Utility/DataBufferHeap.h"
27#include "lldb/Utility/DataExtractor.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028
Greg Clayton4a9d83a2016-06-07 22:56:40 +000029//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +000030// We recently fixed a leak in one of the Instruction subclasses where the
31// instruction will only hold a weak reference to the disassembler to avoid a
32// cycle that was keeping both objects alive (leak) and we need the
33// InstructionImpl class to make sure our public API behaves as users would
34// expect. Calls in our public API allow clients to do things like:
Greg Clayton4a9d83a2016-06-07 22:56:40 +000035//
36// 1 lldb::SBInstruction inst;
37// 2 inst = target.ReadInstructions(pc, 1).GetInstructionAtIndex(0)
38// 3 if (inst.DoesBranch())
39// 4 ...
40//
41// There was a temporary lldb::DisassemblerSP object created in the
Adrian Prantl05097242018-04-30 16:49:04 +000042// SBInstructionList that was returned by lldb.target.ReadInstructions() that
43// will go away after line 2 but the "inst" object should be able to still
44// answer questions about itself. So we make sure that any SBInstruction
45// objects that are given out have a strong reference to the disassembler and
46// the instruction so that the object can live and successfully respond to all
47// queries.
Greg Clayton4a9d83a2016-06-07 22:56:40 +000048//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000049class InstructionImpl {
Greg Clayton4a9d83a2016-06-07 22:56:40 +000050public:
Kate Stoneb9c1b512016-09-06 20:57:50 +000051 InstructionImpl(const lldb::DisassemblerSP &disasm_sp,
52 const lldb::InstructionSP &inst_sp)
53 : m_disasm_sp(disasm_sp), m_inst_sp(inst_sp) {}
Greg Clayton4a9d83a2016-06-07 22:56:40 +000054
Kate Stoneb9c1b512016-09-06 20:57:50 +000055 lldb::InstructionSP GetSP() const { return m_inst_sp; }
Greg Clayton4a9d83a2016-06-07 22:56:40 +000056
Kate Stoneb9c1b512016-09-06 20:57:50 +000057 bool IsValid() const { return (bool)m_inst_sp; }
Greg Clayton4a9d83a2016-06-07 22:56:40 +000058
59protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +000060 lldb::DisassemblerSP m_disasm_sp; // Can be empty/invalid
61 lldb::InstructionSP m_inst_sp;
Greg Clayton4a9d83a2016-06-07 22:56:40 +000062};
63
Chris Lattner30fdc8d2010-06-08 16:52:24 +000064using namespace lldb;
65using namespace lldb_private;
66
Kate Stoneb9c1b512016-09-06 20:57:50 +000067SBInstruction::SBInstruction() : m_opaque_sp() {}
68
69SBInstruction::SBInstruction(const lldb::DisassemblerSP &disasm_sp,
70 const lldb::InstructionSP &inst_sp)
71 : m_opaque_sp(new InstructionImpl(disasm_sp, inst_sp)) {}
72
73SBInstruction::SBInstruction(const SBInstruction &rhs)
74 : m_opaque_sp(rhs.m_opaque_sp) {}
75
76const SBInstruction &SBInstruction::operator=(const SBInstruction &rhs) {
77 if (this != &rhs)
78 m_opaque_sp = rhs.m_opaque_sp;
79 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000080}
81
Kate Stoneb9c1b512016-09-06 20:57:50 +000082SBInstruction::~SBInstruction() {}
83
84bool SBInstruction::IsValid() { return m_opaque_sp && m_opaque_sp->IsValid(); }
85
86SBAddress SBInstruction::GetAddress() {
87 SBAddress sb_addr;
88 lldb::InstructionSP inst_sp(GetOpaque());
89 if (inst_sp && inst_sp->GetAddress().IsValid())
90 sb_addr.SetAddress(&inst_sp->GetAddress());
91 return sb_addr;
Greg Clayton1d273162010-10-06 03:09:58 +000092}
93
Kate Stoneb9c1b512016-09-06 20:57:50 +000094const char *SBInstruction::GetMnemonic(SBTarget target) {
95 lldb::InstructionSP inst_sp(GetOpaque());
96 if (inst_sp) {
97 ExecutionContext exe_ctx;
98 TargetSP target_sp(target.GetSP());
99 std::unique_lock<std::recursive_mutex> lock;
100 if (target_sp) {
101 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
Greg Claytonefabb122010-11-05 23:17:00 +0000102
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103 target_sp->CalculateExecutionContext(exe_ctx);
104 exe_ctx.SetProcessSP(target_sp->GetProcessSP());
Greg Clayton8f7180b2011-09-26 07:11:27 +0000105 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106 return inst_sp->GetMnemonic(&exe_ctx);
107 }
108 return NULL;
Greg Clayton8f7180b2011-09-26 07:11:27 +0000109}
110
Kate Stoneb9c1b512016-09-06 20:57:50 +0000111const char *SBInstruction::GetOperands(SBTarget target) {
112 lldb::InstructionSP inst_sp(GetOpaque());
113 if (inst_sp) {
114 ExecutionContext exe_ctx;
115 TargetSP target_sp(target.GetSP());
116 std::unique_lock<std::recursive_mutex> lock;
117 if (target_sp) {
118 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000119
Kate Stoneb9c1b512016-09-06 20:57:50 +0000120 target_sp->CalculateExecutionContext(exe_ctx);
121 exe_ctx.SetProcessSP(target_sp->GetProcessSP());
Greg Clayton8f7180b2011-09-26 07:11:27 +0000122 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123 return inst_sp->GetOperands(&exe_ctx);
124 }
125 return NULL;
Greg Clayton8f7180b2011-09-26 07:11:27 +0000126}
127
Kate Stoneb9c1b512016-09-06 20:57:50 +0000128const char *SBInstruction::GetComment(SBTarget target) {
129 lldb::InstructionSP inst_sp(GetOpaque());
130 if (inst_sp) {
131 ExecutionContext exe_ctx;
132 TargetSP target_sp(target.GetSP());
133 std::unique_lock<std::recursive_mutex> lock;
134 if (target_sp) {
135 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000136
Kate Stoneb9c1b512016-09-06 20:57:50 +0000137 target_sp->CalculateExecutionContext(exe_ctx);
138 exe_ctx.SetProcessSP(target_sp->GetProcessSP());
Greg Clayton8f7180b2011-09-26 07:11:27 +0000139 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140 return inst_sp->GetComment(&exe_ctx);
141 }
142 return NULL;
Greg Clayton8f7180b2011-09-26 07:11:27 +0000143}
144
Kate Stoneb9c1b512016-09-06 20:57:50 +0000145size_t SBInstruction::GetByteSize() {
146 lldb::InstructionSP inst_sp(GetOpaque());
147 if (inst_sp)
148 return inst_sp->GetOpcode().GetByteSize();
149 return 0;
Greg Clayton1d273162010-10-06 03:09:58 +0000150}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000151
Kate Stoneb9c1b512016-09-06 20:57:50 +0000152SBData SBInstruction::GetData(SBTarget target) {
153 lldb::SBData sb_data;
154 lldb::InstructionSP inst_sp(GetOpaque());
155 if (inst_sp) {
156 DataExtractorSP data_extractor_sp(new DataExtractor());
157 if (inst_sp->GetData(*data_extractor_sp)) {
158 sb_data.SetOpaque(data_extractor_sp);
Greg Clayton8f7180b2011-09-26 07:11:27 +0000159 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160 }
161 return sb_data;
Greg Clayton8f7180b2011-09-26 07:11:27 +0000162}
163
Kate Stoneb9c1b512016-09-06 20:57:50 +0000164bool SBInstruction::DoesBranch() {
165 lldb::InstructionSP inst_sp(GetOpaque());
166 if (inst_sp)
167 return inst_sp->DoesBranch();
168 return false;
Greg Clayton1d273162010-10-06 03:09:58 +0000169}
170
Kate Stoneb9c1b512016-09-06 20:57:50 +0000171bool SBInstruction::HasDelaySlot() {
172 lldb::InstructionSP inst_sp(GetOpaque());
173 if (inst_sp)
174 return inst_sp->HasDelaySlot();
175 return false;
Bhushan D. Attardedf5f0b42016-01-27 10:16:30 +0000176}
177
Nitesh Jaindd125942017-05-04 11:34:42 +0000178bool SBInstruction::CanSetBreakpoint () {
179 lldb::InstructionSP inst_sp(GetOpaque());
180 if (inst_sp)
181 return inst_sp->CanSetBreakpoint();
182 return false;
183}
184
Kate Stoneb9c1b512016-09-06 20:57:50 +0000185lldb::InstructionSP SBInstruction::GetOpaque() {
186 if (m_opaque_sp)
187 return m_opaque_sp->GetSP();
188 else
189 return lldb::InstructionSP();
Greg Clayton4a9d83a2016-06-07 22:56:40 +0000190}
191
Kate Stoneb9c1b512016-09-06 20:57:50 +0000192void SBInstruction::SetOpaque(const lldb::DisassemblerSP &disasm_sp,
193 const lldb::InstructionSP &inst_sp) {
194 m_opaque_sp.reset(new InstructionImpl(disasm_sp, inst_sp));
Greg Clayton1d273162010-10-06 03:09:58 +0000195}
196
Kate Stoneb9c1b512016-09-06 20:57:50 +0000197bool SBInstruction::GetDescription(lldb::SBStream &s) {
198 lldb::InstructionSP inst_sp(GetOpaque());
199 if (inst_sp) {
200 SymbolContext sc;
201 const Address &addr = inst_sp->GetAddress();
202 ModuleSP module_sp(addr.GetModule());
203 if (module_sp)
204 module_sp->ResolveSymbolContextForAddress(addr, eSymbolContextEverything,
205 sc);
206 // Use the "ref()" instead of the "get()" accessor in case the SBStream
207 // didn't have a stream already created, one will get created...
208 FormatEntity::Entry format;
209 FormatEntity::Parse("${addr}: ", format);
210 inst_sp->Dump(&s.ref(), 0, true, false, NULL, &sc, NULL, &format, 0);
211 return true;
212 }
213 return false;
214}
215
216void SBInstruction::Print(FILE *out) {
217 if (out == NULL)
218 return;
219
220 lldb::InstructionSP inst_sp(GetOpaque());
221 if (inst_sp) {
222 SymbolContext sc;
223 const Address &addr = inst_sp->GetAddress();
224 ModuleSP module_sp(addr.GetModule());
225 if (module_sp)
226 module_sp->ResolveSymbolContextForAddress(addr, eSymbolContextEverything,
227 sc);
228 StreamFile out_stream(out, false);
229 FormatEntity::Entry format;
230 FormatEntity::Parse("${addr}: ", format);
231 inst_sp->Dump(&out_stream, 0, true, false, NULL, &sc, NULL, &format, 0);
232 }
233}
234
235bool SBInstruction::EmulateWithFrame(lldb::SBFrame &frame,
236 uint32_t evaluate_options) {
237 lldb::InstructionSP inst_sp(GetOpaque());
238 if (inst_sp) {
239 lldb::StackFrameSP frame_sp(frame.GetFrameSP());
240
241 if (frame_sp) {
242 lldb_private::ExecutionContext exe_ctx;
243 frame_sp->CalculateExecutionContext(exe_ctx);
244 lldb_private::Target *target = exe_ctx.GetTargetPtr();
245 lldb_private::ArchSpec arch = target->GetArchitecture();
246
247 return inst_sp->Emulate(
248 arch, evaluate_options, (void *)frame_sp.get(),
249 &lldb_private::EmulateInstruction::ReadMemoryFrame,
250 &lldb_private::EmulateInstruction::WriteMemoryFrame,
251 &lldb_private::EmulateInstruction::ReadRegisterFrame,
252 &lldb_private::EmulateInstruction::WriteRegisterFrame);
Greg Clayton1d273162010-10-06 03:09:58 +0000253 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000254 }
255 return false;
Greg Clayton1d273162010-10-06 03:09:58 +0000256}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000257
Kate Stoneb9c1b512016-09-06 20:57:50 +0000258bool SBInstruction::DumpEmulation(const char *triple) {
259 lldb::InstructionSP inst_sp(GetOpaque());
260 if (inst_sp && triple) {
Pavel Labath7263f1b2017-10-31 10:56:03 +0000261 return inst_sp->DumpEmulation(HostInfo::GetAugmentedArchSpec(triple));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000262 }
263 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000264}
Caroline Tice7c9dd3c2011-04-05 23:22:54 +0000265
Kate Stoneb9c1b512016-09-06 20:57:50 +0000266bool SBInstruction::TestEmulation(lldb::SBStream &output_stream,
267 const char *test_file) {
268 if (!m_opaque_sp)
269 SetOpaque(lldb::DisassemblerSP(),
270 lldb::InstructionSP(new PseudoInstruction()));
Greg Claytonb9556ac2012-01-30 07:41:31 +0000271
Kate Stoneb9c1b512016-09-06 20:57:50 +0000272 lldb::InstructionSP inst_sp(GetOpaque());
273 if (inst_sp)
274 return inst_sp->TestEmulation(output_stream.get(), test_file);
275 return false;
Caroline Tice7c9dd3c2011-04-05 23:22:54 +0000276}