blob: 0a7bb73fa6f8647d2657f0b24073746db1e7a06b [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 Clayton516f0842012-04-11 00:24:49 +000081 api_locker.Lock (target_sp->GetAPIMutex().GetMutex());
Greg Clayton334d33a2012-01-30 07:41:31 +000082 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 Clayton516f0842012-04-11 00:24:49 +0000100 api_locker.Lock (target_sp->GetAPIMutex().GetMutex());
Greg Clayton334d33a2012-01-30 07:41:31 +0000101 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 Clayton516f0842012-04-11 00:24:49 +0000119 api_locker.Lock (target_sp->GetAPIMutex().GetMutex());
Greg Clayton334d33a2012-01-30 07:41:31 +0000120 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 {
Greg Clayton50561692012-04-11 21:13:31 +0000142 DataExtractorSP data_extractor_sp (new DataExtractor());
143 if (m_opaque_sp->GetOpcode().GetData (*data_extractor_sp))
Greg Clayton23b8abb2011-09-26 07:11:27 +0000144 {
Greg Clayton23b8abb2011-09-26 07:11:27 +0000145 sb_data.SetOpaque (data_extractor_sp);
146 }
147 }
148 return sb_data;
149}
150
151
152
Greg Clayton5c4c7462010-10-06 03:09:58 +0000153bool
154SBInstruction::DoesBranch ()
155{
156 if (m_opaque_sp)
157 return m_opaque_sp->DoesBranch ();
158 return false;
159}
160
161void
162SBInstruction::SetOpaque (const lldb::InstructionSP &inst_sp)
163{
164 m_opaque_sp = inst_sp;
165}
166
167bool
168SBInstruction::GetDescription (lldb::SBStream &s)
169{
170 if (m_opaque_sp)
171 {
172 // Use the "ref()" instead of the "get()" accessor in case the SBStream
173 // didn't have a stream already created, one will get created...
Greg Clayton889fbd02011-03-26 19:14:58 +0000174 m_opaque_sp->Dump (&s.ref(), 0, true, false, NULL, false);
Greg Clayton5c4c7462010-10-06 03:09:58 +0000175 return true;
176 }
177 return false;
178}
Chris Lattner24943d22010-06-08 16:52:24 +0000179
180void
181SBInstruction::Print (FILE *out)
182{
183 if (out == NULL)
184 return;
185
Greg Clayton5c4c7462010-10-06 03:09:58 +0000186 if (m_opaque_sp)
187 {
Greg Clayton58928562011-02-09 01:08:52 +0000188 StreamFile out_stream (out, false);
Greg Clayton889fbd02011-03-26 19:14:58 +0000189 m_opaque_sp->Dump (&out_stream, 0, true, false, NULL, false);
Greg Clayton5c4c7462010-10-06 03:09:58 +0000190 }
Chris Lattner24943d22010-06-08 16:52:24 +0000191}
Caroline Ticeaf591802011-04-05 23:22:54 +0000192
193bool
Greg Clayton888a7332011-04-26 04:39:08 +0000194SBInstruction::EmulateWithFrame (lldb::SBFrame &frame, uint32_t evaluate_options)
Caroline Ticeaf591802011-04-05 23:22:54 +0000195{
Greg Clayton334d33a2012-01-30 07:41:31 +0000196 if (m_opaque_sp)
Caroline Ticeaf591802011-04-05 23:22:54 +0000197 {
Greg Clayton334d33a2012-01-30 07:41:31 +0000198 lldb::StackFrameSP frame_sp (frame.GetFrameSP());
199
200 if (frame_sp)
201 {
202 lldb_private::ExecutionContext exe_ctx;
203 frame_sp->CalculateExecutionContext (exe_ctx);
204 lldb_private::Target *target = exe_ctx.GetTargetPtr();
205 lldb_private::ArchSpec arch = target->GetArchitecture();
206
207 return m_opaque_sp->Emulate (arch,
208 evaluate_options,
209 (void *) frame_sp.get(),
210 &lldb_private::EmulateInstruction::ReadMemoryFrame,
211 &lldb_private::EmulateInstruction::WriteMemoryFrame,
212 &lldb_private::EmulateInstruction::ReadRegisterFrame,
213 &lldb_private::EmulateInstruction::WriteRegisterFrame);
214 }
Caroline Ticeaf591802011-04-05 23:22:54 +0000215 }
216 return false;
217}
218
219bool
220SBInstruction::DumpEmulation (const char *triple)
221{
222 if (m_opaque_sp && triple)
223 {
Greg Claytonf15996e2011-04-07 22:46:35 +0000224 lldb_private::ArchSpec arch (triple, NULL);
Caroline Ticeaf591802011-04-05 23:22:54 +0000225
Caroline Tice0fe5a532011-04-08 23:33:06 +0000226 return m_opaque_sp->DumpEmulation (arch);
227
Caroline Ticeaf591802011-04-05 23:22:54 +0000228 }
229 return false;
230}
231
Caroline Tice6b8d3b52011-04-19 23:30:03 +0000232bool
233SBInstruction::TestEmulation (lldb::SBStream &output_stream, const char *test_file)
234{
235 if (!m_opaque_sp.get())
236 m_opaque_sp.reset (new PseudoInstruction());
237
Johnny Chen09008d02011-04-21 20:27:45 +0000238 return m_opaque_sp->TestEmulation (output_stream.get(), test_file);
239}