blob: 26f8d1aa00573f0281813fb9d44675ca35765452 [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;
78 if (target.IsValid())
79 {
80 api_locker.Reset (target->GetAPIMutex().GetMutex());
81 target->CalculateExecutionContext (exe_ctx);
82 exe_ctx.SetProcessSP(target->GetProcessSP());
83 }
Greg Claytond9b44252011-09-27 00:58:45 +000084 return m_opaque_sp->GetMnemonic(exe_ctx.GetBestExecutionContextScope());
Greg Clayton23b8abb2011-09-26 07:11:27 +000085 }
86 return NULL;
87}
88
89const char *
Greg Claytond9b44252011-09-27 00:58:45 +000090SBInstruction::GetOperands(SBTarget target)
Greg Clayton23b8abb2011-09-26 07:11:27 +000091{
92 if (m_opaque_sp)
93 {
94 Mutex::Locker api_locker;
95 ExecutionContext exe_ctx;
96 if (target.IsValid())
97 {
98 api_locker.Reset (target->GetAPIMutex().GetMutex());
99 target->CalculateExecutionContext (exe_ctx);
100 exe_ctx.SetProcessSP(target->GetProcessSP());
101 }
Greg Claytond9b44252011-09-27 00:58:45 +0000102 return m_opaque_sp->GetOperands(exe_ctx.GetBestExecutionContextScope());
Greg Clayton23b8abb2011-09-26 07:11:27 +0000103 }
104 return NULL;
105}
106
107const char *
108SBInstruction::GetComment(SBTarget target)
109{
110 if (m_opaque_sp)
111 {
112 Mutex::Locker api_locker;
113 ExecutionContext exe_ctx;
114 if (target.IsValid())
115 {
116 api_locker.Reset (target->GetAPIMutex().GetMutex());
117 target->CalculateExecutionContext (exe_ctx);
118 exe_ctx.SetProcessSP(target->GetProcessSP());
119 }
120 return m_opaque_sp->GetComment(exe_ctx.GetBestExecutionContextScope());
121 }
122 return NULL;
123}
124
Greg Clayton5c4c7462010-10-06 03:09:58 +0000125size_t
126SBInstruction::GetByteSize ()
127{
128 if (m_opaque_sp)
Greg Clayton149731c2011-03-25 18:03:16 +0000129 return m_opaque_sp->GetOpcode().GetByteSize();
Greg Clayton5c4c7462010-10-06 03:09:58 +0000130 return 0;
131}
Chris Lattner24943d22010-06-08 16:52:24 +0000132
Greg Clayton23b8abb2011-09-26 07:11:27 +0000133SBData
134SBInstruction::GetData (SBTarget target)
135{
136 lldb::SBData sb_data;
137 if (m_opaque_sp)
138 {
139 const Opcode &opcode = m_opaque_sp->GetOpcode();
140 const void *opcode_data = opcode.GetOpcodeBytes();
141 const uint32_t opcode_data_size = opcode.GetByteSize();
142 if (opcode_data && opcode_data_size > 0)
143 {
144 ByteOrder data_byte_order = opcode.GetDataByteOrder();
145 if (data_byte_order == eByteOrderInvalid)
146 data_byte_order = target->GetArchitecture().GetByteOrder();
147 DataBufferSP data_buffer_sp (new DataBufferHeap (opcode_data, opcode_data_size));
148 DataExtractorSP data_extractor_sp (new DataExtractor (data_buffer_sp,
149 data_byte_order,
150 target.IsValid() ? target->GetArchitecture().GetAddressByteSize() : sizeof(void*)));
151 sb_data.SetOpaque (data_extractor_sp);
152 }
153 }
154 return sb_data;
155}
156
157
158
Greg Clayton5c4c7462010-10-06 03:09:58 +0000159bool
160SBInstruction::DoesBranch ()
161{
162 if (m_opaque_sp)
163 return m_opaque_sp->DoesBranch ();
164 return false;
165}
166
167void
168SBInstruction::SetOpaque (const lldb::InstructionSP &inst_sp)
169{
170 m_opaque_sp = inst_sp;
171}
172
173bool
174SBInstruction::GetDescription (lldb::SBStream &s)
175{
176 if (m_opaque_sp)
177 {
178 // Use the "ref()" instead of the "get()" accessor in case the SBStream
179 // didn't have a stream already created, one will get created...
Greg Clayton889fbd02011-03-26 19:14:58 +0000180 m_opaque_sp->Dump (&s.ref(), 0, true, false, NULL, false);
Greg Clayton5c4c7462010-10-06 03:09:58 +0000181 return true;
182 }
183 return false;
184}
Chris Lattner24943d22010-06-08 16:52:24 +0000185
186void
187SBInstruction::Print (FILE *out)
188{
189 if (out == NULL)
190 return;
191
Greg Clayton5c4c7462010-10-06 03:09:58 +0000192 if (m_opaque_sp)
193 {
Greg Clayton58928562011-02-09 01:08:52 +0000194 StreamFile out_stream (out, false);
Greg Clayton889fbd02011-03-26 19:14:58 +0000195 m_opaque_sp->Dump (&out_stream, 0, true, false, NULL, false);
Greg Clayton5c4c7462010-10-06 03:09:58 +0000196 }
Chris Lattner24943d22010-06-08 16:52:24 +0000197}
Caroline Ticeaf591802011-04-05 23:22:54 +0000198
199bool
Greg Clayton888a7332011-04-26 04:39:08 +0000200SBInstruction::EmulateWithFrame (lldb::SBFrame &frame, uint32_t evaluate_options)
Caroline Ticeaf591802011-04-05 23:22:54 +0000201{
202 if (m_opaque_sp && frame.get())
203 {
204 lldb_private::ExecutionContext exe_ctx;
205 frame->CalculateExecutionContext (exe_ctx);
Greg Clayton567e7f32011-09-22 04:58:26 +0000206 lldb_private::Target *target = exe_ctx.GetTargetPtr();
Caroline Ticeaf591802011-04-05 23:22:54 +0000207 lldb_private::ArchSpec arch = target->GetArchitecture();
208
209 return m_opaque_sp->Emulate (arch,
Greg Clayton888a7332011-04-26 04:39:08 +0000210 evaluate_options,
Caroline Ticeaf591802011-04-05 23:22:54 +0000211 (void *) frame.get(),
212 &lldb_private::EmulateInstruction::ReadMemoryFrame,
213 &lldb_private::EmulateInstruction::WriteMemoryFrame,
214 &lldb_private::EmulateInstruction::ReadRegisterFrame,
215 &lldb_private::EmulateInstruction::WriteRegisterFrame);
216 }
217 return false;
218}
219
220bool
221SBInstruction::DumpEmulation (const char *triple)
222{
223 if (m_opaque_sp && triple)
224 {
Greg Claytonf15996e2011-04-07 22:46:35 +0000225 lldb_private::ArchSpec arch (triple, NULL);
Caroline Ticeaf591802011-04-05 23:22:54 +0000226
Caroline Tice0fe5a532011-04-08 23:33:06 +0000227 return m_opaque_sp->DumpEmulation (arch);
228
Caroline Ticeaf591802011-04-05 23:22:54 +0000229 }
230 return false;
231}
232
Caroline Tice6b8d3b52011-04-19 23:30:03 +0000233bool
234SBInstruction::TestEmulation (lldb::SBStream &output_stream, const char *test_file)
235{
236 if (!m_opaque_sp.get())
237 m_opaque_sp.reset (new PseudoInstruction());
238
Johnny Chen09008d02011-04-21 20:27:45 +0000239 return m_opaque_sp->TestEmulation (output_stream.get(), test_file);
240}