blob: f384b5b97b470f4f0765033d8328a28c3ab063b0 [file] [log] [blame]
Chris Lattner24943d22010-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 Clayton5c4c7462010-10-06 03:09:58 +000012#include "lldb/API/SBAddress.h"
Caroline Ticeaf591802011-04-05 23:22:54 +000013#include "lldb/API/SBFrame.h"
Greg Clayton5c4c7462010-10-06 03:09:58 +000014#include "lldb/API/SBInstruction.h"
15#include "lldb/API/SBStream.h"
Caroline Ticeaf591802011-04-05 23:22:54 +000016#include "lldb/API/SBTarget.h"
Greg Clayton5c4c7462010-10-06 03:09:58 +000017
Caroline Ticeaf591802011-04-05 23:22:54 +000018#include "lldb/Core/ArchSpec.h"
Greg Clayton23b8abb2011-09-26 07:11:27 +000019#include "lldb/Core/DataBufferHeap.h"
Chris Lattner24943d22010-06-08 16:52:24 +000020#include "lldb/Core/Disassembler.h"
Caroline Ticeaf591802011-04-05 23:22:54 +000021#include "lldb/Core/EmulateInstruction.h"
Greg Clayton5c4c7462010-10-06 03:09:58 +000022#include "lldb/Core/StreamFile.h"
Caroline Ticeaf591802011-04-05 23:22:54 +000023#include "lldb/Target/ExecutionContext.h"
24#include "lldb/Target/StackFrame.h"
25#include "lldb/Target/Target.h"
Chris Lattner24943d22010-06-08 16:52:24 +000026
27using namespace lldb;
28using namespace lldb_private;
29
Chris Lattner24943d22010-06-08 16:52:24 +000030SBInstruction::SBInstruction ()
31{
32}
33
Greg Clayton5c4c7462010-10-06 03:09:58 +000034SBInstruction::SBInstruction (const lldb::InstructionSP& inst_sp) :
35 m_opaque_sp (inst_sp)
36{
37}
38
Greg Clayton538eb822010-11-05 23:17:00 +000039SBInstruction::SBInstruction(const SBInstruction &rhs) :
40 m_opaque_sp (rhs.m_opaque_sp)
41{
42}
43
44const SBInstruction &
45SBInstruction::operator = (const SBInstruction &rhs)
46{
47 if (this != &rhs)
48 m_opaque_sp = rhs.m_opaque_sp;
49 return *this;
50}
51
Chris Lattner24943d22010-06-08 16:52:24 +000052SBInstruction::~SBInstruction ()
53{
54}
55
Greg Clayton5c4c7462010-10-06 03:09:58 +000056bool
57SBInstruction::IsValid()
58{
59 return (m_opaque_sp.get() != NULL);
60}
Chris Lattner24943d22010-06-08 16:52:24 +000061
Greg Clayton5c4c7462010-10-06 03:09:58 +000062SBAddress
63SBInstruction::GetAddress()
64{
65 SBAddress sb_addr;
66 if (m_opaque_sp && m_opaque_sp->GetAddress().IsValid())
67 sb_addr.SetAddress(&m_opaque_sp->GetAddress());
68 return sb_addr;
69}
Chris Lattner24943d22010-06-08 16:52:24 +000070
Greg Clayton23b8abb2011-09-26 07:11:27 +000071const char *
Greg Claytond9b44252011-09-27 00:58:45 +000072SBInstruction::GetMnemonic(SBTarget target)
Greg Clayton23b8abb2011-09-26 07:11:27 +000073{
74 if (m_opaque_sp)
75 {
76 Mutex::Locker api_locker;
77 ExecutionContext exe_ctx;
Greg Clayton334d33a2012-01-30 07:41:31 +000078 TargetSP target_sp (target.GetSP());
79 if (target_sp)
Greg Clayton23b8abb2011-09-26 07:11:27 +000080 {
Greg Clayton334d33a2012-01-30 07:41:31 +000081 api_locker.Reset (target_sp->GetAPIMutex().GetMutex());
82 target_sp->CalculateExecutionContext (exe_ctx);
83 exe_ctx.SetProcessSP(target_sp->GetProcessSP());
Greg Clayton23b8abb2011-09-26 07:11:27 +000084 }
Greg Claytond9b44252011-09-27 00:58:45 +000085 return m_opaque_sp->GetMnemonic(exe_ctx.GetBestExecutionContextScope());
Greg Clayton23b8abb2011-09-26 07:11:27 +000086 }
87 return NULL;
88}
89
90const char *
Greg Claytond9b44252011-09-27 00:58:45 +000091SBInstruction::GetOperands(SBTarget target)
Greg Clayton23b8abb2011-09-26 07:11:27 +000092{
93 if (m_opaque_sp)
94 {
95 Mutex::Locker api_locker;
96 ExecutionContext exe_ctx;
Greg Clayton334d33a2012-01-30 07:41:31 +000097 TargetSP target_sp (target.GetSP());
98 if (target_sp)
Greg Clayton23b8abb2011-09-26 07:11:27 +000099 {
Greg Clayton334d33a2012-01-30 07:41:31 +0000100 api_locker.Reset (target_sp->GetAPIMutex().GetMutex());
101 target_sp->CalculateExecutionContext (exe_ctx);
102 exe_ctx.SetProcessSP(target_sp->GetProcessSP());
Greg Clayton23b8abb2011-09-26 07:11:27 +0000103 }
Greg Claytond9b44252011-09-27 00:58:45 +0000104 return m_opaque_sp->GetOperands(exe_ctx.GetBestExecutionContextScope());
Greg Clayton23b8abb2011-09-26 07:11:27 +0000105 }
106 return NULL;
107}
108
109const char *
110SBInstruction::GetComment(SBTarget target)
111{
112 if (m_opaque_sp)
113 {
114 Mutex::Locker api_locker;
115 ExecutionContext exe_ctx;
Greg Clayton334d33a2012-01-30 07:41:31 +0000116 TargetSP target_sp (target.GetSP());
117 if (target_sp)
Greg Clayton23b8abb2011-09-26 07:11:27 +0000118 {
Greg Clayton334d33a2012-01-30 07:41:31 +0000119 api_locker.Reset (target_sp->GetAPIMutex().GetMutex());
120 target_sp->CalculateExecutionContext (exe_ctx);
121 exe_ctx.SetProcessSP(target_sp->GetProcessSP());
Greg Clayton23b8abb2011-09-26 07:11:27 +0000122 }
123 return m_opaque_sp->GetComment(exe_ctx.GetBestExecutionContextScope());
124 }
125 return NULL;
126}
127
Greg Clayton5c4c7462010-10-06 03:09:58 +0000128size_t
129SBInstruction::GetByteSize ()
130{
131 if (m_opaque_sp)
Greg Clayton149731c2011-03-25 18:03:16 +0000132 return m_opaque_sp->GetOpcode().GetByteSize();
Greg Clayton5c4c7462010-10-06 03:09:58 +0000133 return 0;
134}
Chris Lattner24943d22010-06-08 16:52:24 +0000135
Greg Clayton23b8abb2011-09-26 07:11:27 +0000136SBData
137SBInstruction::GetData (SBTarget target)
138{
139 lldb::SBData sb_data;
140 if (m_opaque_sp)
141 {
142 const Opcode &opcode = m_opaque_sp->GetOpcode();
143 const void *opcode_data = opcode.GetOpcodeBytes();
144 const uint32_t opcode_data_size = opcode.GetByteSize();
145 if (opcode_data && opcode_data_size > 0)
146 {
147 ByteOrder data_byte_order = opcode.GetDataByteOrder();
Greg Clayton334d33a2012-01-30 07:41:31 +0000148 TargetSP target_sp (target.GetSP());
149 if (data_byte_order == eByteOrderInvalid && target_sp)
150 data_byte_order = target_sp->GetArchitecture().GetByteOrder();
Greg Clayton23b8abb2011-09-26 07:11:27 +0000151 DataBufferSP data_buffer_sp (new DataBufferHeap (opcode_data, opcode_data_size));
152 DataExtractorSP data_extractor_sp (new DataExtractor (data_buffer_sp,
153 data_byte_order,
Greg Clayton334d33a2012-01-30 07:41:31 +0000154 target_sp ? target_sp->GetArchitecture().GetAddressByteSize() : sizeof(void*)));
Greg Clayton23b8abb2011-09-26 07:11:27 +0000155 sb_data.SetOpaque (data_extractor_sp);
156 }
157 }
158 return sb_data;
159}
160
161
162
Greg Clayton5c4c7462010-10-06 03:09:58 +0000163bool
164SBInstruction::DoesBranch ()
165{
166 if (m_opaque_sp)
167 return m_opaque_sp->DoesBranch ();
168 return false;
169}
170
171void
172SBInstruction::SetOpaque (const lldb::InstructionSP &inst_sp)
173{
174 m_opaque_sp = inst_sp;
175}
176
177bool
178SBInstruction::GetDescription (lldb::SBStream &s)
179{
180 if (m_opaque_sp)
181 {
182 // Use the "ref()" instead of the "get()" accessor in case the SBStream
183 // didn't have a stream already created, one will get created...
Greg Clayton889fbd02011-03-26 19:14:58 +0000184 m_opaque_sp->Dump (&s.ref(), 0, true, false, NULL, false);
Greg Clayton5c4c7462010-10-06 03:09:58 +0000185 return true;
186 }
187 return false;
188}
Chris Lattner24943d22010-06-08 16:52:24 +0000189
190void
191SBInstruction::Print (FILE *out)
192{
193 if (out == NULL)
194 return;
195
Greg Clayton5c4c7462010-10-06 03:09:58 +0000196 if (m_opaque_sp)
197 {
Greg Clayton58928562011-02-09 01:08:52 +0000198 StreamFile out_stream (out, false);
Greg Clayton889fbd02011-03-26 19:14:58 +0000199 m_opaque_sp->Dump (&out_stream, 0, true, false, NULL, false);
Greg Clayton5c4c7462010-10-06 03:09:58 +0000200 }
Chris Lattner24943d22010-06-08 16:52:24 +0000201}
Caroline Ticeaf591802011-04-05 23:22:54 +0000202
203bool
Greg Clayton888a7332011-04-26 04:39:08 +0000204SBInstruction::EmulateWithFrame (lldb::SBFrame &frame, uint32_t evaluate_options)
Caroline Ticeaf591802011-04-05 23:22:54 +0000205{
Greg Clayton334d33a2012-01-30 07:41:31 +0000206 if (m_opaque_sp)
Caroline Ticeaf591802011-04-05 23:22:54 +0000207 {
Greg Clayton334d33a2012-01-30 07:41:31 +0000208 lldb::StackFrameSP frame_sp (frame.GetFrameSP());
209
210 if (frame_sp)
211 {
212 lldb_private::ExecutionContext exe_ctx;
213 frame_sp->CalculateExecutionContext (exe_ctx);
214 lldb_private::Target *target = exe_ctx.GetTargetPtr();
215 lldb_private::ArchSpec arch = target->GetArchitecture();
216
217 return m_opaque_sp->Emulate (arch,
218 evaluate_options,
219 (void *) frame_sp.get(),
220 &lldb_private::EmulateInstruction::ReadMemoryFrame,
221 &lldb_private::EmulateInstruction::WriteMemoryFrame,
222 &lldb_private::EmulateInstruction::ReadRegisterFrame,
223 &lldb_private::EmulateInstruction::WriteRegisterFrame);
224 }
Caroline Ticeaf591802011-04-05 23:22:54 +0000225 }
226 return false;
227}
228
229bool
230SBInstruction::DumpEmulation (const char *triple)
231{
232 if (m_opaque_sp && triple)
233 {
Greg Claytonf15996e2011-04-07 22:46:35 +0000234 lldb_private::ArchSpec arch (triple, NULL);
Caroline Ticeaf591802011-04-05 23:22:54 +0000235
Caroline Tice0fe5a532011-04-08 23:33:06 +0000236 return m_opaque_sp->DumpEmulation (arch);
237
Caroline Ticeaf591802011-04-05 23:22:54 +0000238 }
239 return false;
240}
241
Caroline Tice6b8d3b52011-04-19 23:30:03 +0000242bool
243SBInstruction::TestEmulation (lldb::SBStream &output_stream, const char *test_file)
244{
245 if (!m_opaque_sp.get())
246 m_opaque_sp.reset (new PseudoInstruction());
247
Johnny Chen09008d02011-04-21 20:27:45 +0000248 return m_opaque_sp->TestEmulation (output_stream.get(), test_file);
249}