Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- 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 Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 12 | #include "lldb/API/SBAddress.h" |
Caroline Tice | af59180 | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 13 | #include "lldb/API/SBFrame.h" |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 14 | #include "lldb/API/SBInstruction.h" |
| 15 | #include "lldb/API/SBStream.h" |
Caroline Tice | af59180 | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 16 | #include "lldb/API/SBTarget.h" |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 17 | |
Caroline Tice | af59180 | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 18 | #include "lldb/Core/ArchSpec.h" |
Greg Clayton | 23b8abb | 2011-09-26 07:11:27 +0000 | [diff] [blame] | 19 | #include "lldb/Core/DataBufferHeap.h" |
Greg Clayton | 49ce896 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 20 | #include "lldb/Core/DataExtractor.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 21 | #include "lldb/Core/Disassembler.h" |
Caroline Tice | af59180 | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 22 | #include "lldb/Core/EmulateInstruction.h" |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 23 | #include "lldb/Core/StreamFile.h" |
Caroline Tice | af59180 | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 24 | #include "lldb/Target/ExecutionContext.h" |
| 25 | #include "lldb/Target/StackFrame.h" |
| 26 | #include "lldb/Target/Target.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace lldb; |
| 29 | using namespace lldb_private; |
| 30 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 31 | SBInstruction::SBInstruction () |
| 32 | { |
| 33 | } |
| 34 | |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 35 | SBInstruction::SBInstruction (const lldb::InstructionSP& inst_sp) : |
| 36 | m_opaque_sp (inst_sp) |
| 37 | { |
| 38 | } |
| 39 | |
Greg Clayton | 538eb82 | 2010-11-05 23:17:00 +0000 | [diff] [blame] | 40 | SBInstruction::SBInstruction(const SBInstruction &rhs) : |
| 41 | m_opaque_sp (rhs.m_opaque_sp) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | const SBInstruction & |
| 46 | SBInstruction::operator = (const SBInstruction &rhs) |
| 47 | { |
| 48 | if (this != &rhs) |
| 49 | m_opaque_sp = rhs.m_opaque_sp; |
| 50 | return *this; |
| 51 | } |
| 52 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 53 | SBInstruction::~SBInstruction () |
| 54 | { |
| 55 | } |
| 56 | |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 57 | bool |
| 58 | SBInstruction::IsValid() |
| 59 | { |
| 60 | return (m_opaque_sp.get() != NULL); |
| 61 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 62 | |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 63 | SBAddress |
| 64 | SBInstruction::GetAddress() |
| 65 | { |
| 66 | SBAddress sb_addr; |
| 67 | if (m_opaque_sp && m_opaque_sp->GetAddress().IsValid()) |
| 68 | sb_addr.SetAddress(&m_opaque_sp->GetAddress()); |
| 69 | return sb_addr; |
| 70 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 71 | |
Greg Clayton | 23b8abb | 2011-09-26 07:11:27 +0000 | [diff] [blame] | 72 | const char * |
Greg Clayton | d9b4425 | 2011-09-27 00:58:45 +0000 | [diff] [blame] | 73 | SBInstruction::GetMnemonic(SBTarget target) |
Greg Clayton | 23b8abb | 2011-09-26 07:11:27 +0000 | [diff] [blame] | 74 | { |
| 75 | if (m_opaque_sp) |
| 76 | { |
| 77 | Mutex::Locker api_locker; |
| 78 | ExecutionContext exe_ctx; |
Greg Clayton | 334d33a | 2012-01-30 07:41:31 +0000 | [diff] [blame] | 79 | TargetSP target_sp (target.GetSP()); |
| 80 | if (target_sp) |
Greg Clayton | 23b8abb | 2011-09-26 07:11:27 +0000 | [diff] [blame] | 81 | { |
Jim Ingham | 1b584eb | 2012-05-04 23:02:50 +0000 | [diff] [blame] | 82 | api_locker.Lock (target_sp->GetAPIMutex()); |
Greg Clayton | 334d33a | 2012-01-30 07:41:31 +0000 | [diff] [blame] | 83 | target_sp->CalculateExecutionContext (exe_ctx); |
| 84 | exe_ctx.SetProcessSP(target_sp->GetProcessSP()); |
Greg Clayton | 23b8abb | 2011-09-26 07:11:27 +0000 | [diff] [blame] | 85 | } |
Greg Clayton | 0fef968 | 2012-05-10 02:52:23 +0000 | [diff] [blame] | 86 | return m_opaque_sp->GetMnemonic(&exe_ctx); |
Greg Clayton | 23b8abb | 2011-09-26 07:11:27 +0000 | [diff] [blame] | 87 | } |
| 88 | return NULL; |
| 89 | } |
| 90 | |
| 91 | const char * |
Greg Clayton | d9b4425 | 2011-09-27 00:58:45 +0000 | [diff] [blame] | 92 | SBInstruction::GetOperands(SBTarget target) |
Greg Clayton | 23b8abb | 2011-09-26 07:11:27 +0000 | [diff] [blame] | 93 | { |
| 94 | if (m_opaque_sp) |
| 95 | { |
| 96 | Mutex::Locker api_locker; |
| 97 | ExecutionContext exe_ctx; |
Greg Clayton | 334d33a | 2012-01-30 07:41:31 +0000 | [diff] [blame] | 98 | TargetSP target_sp (target.GetSP()); |
| 99 | if (target_sp) |
Greg Clayton | 23b8abb | 2011-09-26 07:11:27 +0000 | [diff] [blame] | 100 | { |
Jim Ingham | 1b584eb | 2012-05-04 23:02:50 +0000 | [diff] [blame] | 101 | api_locker.Lock (target_sp->GetAPIMutex()); |
Greg Clayton | 334d33a | 2012-01-30 07:41:31 +0000 | [diff] [blame] | 102 | target_sp->CalculateExecutionContext (exe_ctx); |
| 103 | exe_ctx.SetProcessSP(target_sp->GetProcessSP()); |
Greg Clayton | 23b8abb | 2011-09-26 07:11:27 +0000 | [diff] [blame] | 104 | } |
Greg Clayton | 0fef968 | 2012-05-10 02:52:23 +0000 | [diff] [blame] | 105 | return m_opaque_sp->GetOperands(&exe_ctx); |
Greg Clayton | 23b8abb | 2011-09-26 07:11:27 +0000 | [diff] [blame] | 106 | } |
| 107 | return NULL; |
| 108 | } |
| 109 | |
| 110 | const char * |
| 111 | SBInstruction::GetComment(SBTarget target) |
| 112 | { |
| 113 | if (m_opaque_sp) |
| 114 | { |
| 115 | Mutex::Locker api_locker; |
| 116 | ExecutionContext exe_ctx; |
Greg Clayton | 334d33a | 2012-01-30 07:41:31 +0000 | [diff] [blame] | 117 | TargetSP target_sp (target.GetSP()); |
| 118 | if (target_sp) |
Greg Clayton | 23b8abb | 2011-09-26 07:11:27 +0000 | [diff] [blame] | 119 | { |
Jim Ingham | 1b584eb | 2012-05-04 23:02:50 +0000 | [diff] [blame] | 120 | api_locker.Lock (target_sp->GetAPIMutex()); |
Greg Clayton | 334d33a | 2012-01-30 07:41:31 +0000 | [diff] [blame] | 121 | target_sp->CalculateExecutionContext (exe_ctx); |
| 122 | exe_ctx.SetProcessSP(target_sp->GetProcessSP()); |
Greg Clayton | 23b8abb | 2011-09-26 07:11:27 +0000 | [diff] [blame] | 123 | } |
Greg Clayton | 0fef968 | 2012-05-10 02:52:23 +0000 | [diff] [blame] | 124 | return m_opaque_sp->GetComment(&exe_ctx); |
Greg Clayton | 23b8abb | 2011-09-26 07:11:27 +0000 | [diff] [blame] | 125 | } |
| 126 | return NULL; |
| 127 | } |
| 128 | |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 129 | size_t |
| 130 | SBInstruction::GetByteSize () |
| 131 | { |
| 132 | if (m_opaque_sp) |
Greg Clayton | 149731c | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 133 | return m_opaque_sp->GetOpcode().GetByteSize(); |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 134 | return 0; |
| 135 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 136 | |
Greg Clayton | 23b8abb | 2011-09-26 07:11:27 +0000 | [diff] [blame] | 137 | SBData |
| 138 | SBInstruction::GetData (SBTarget target) |
| 139 | { |
| 140 | lldb::SBData sb_data; |
| 141 | if (m_opaque_sp) |
| 142 | { |
Greg Clayton | 5056169 | 2012-04-11 21:13:31 +0000 | [diff] [blame] | 143 | DataExtractorSP data_extractor_sp (new DataExtractor()); |
Greg Clayton | 0fef968 | 2012-05-10 02:52:23 +0000 | [diff] [blame] | 144 | if (m_opaque_sp->GetData (*data_extractor_sp)) |
Greg Clayton | 23b8abb | 2011-09-26 07:11:27 +0000 | [diff] [blame] | 145 | { |
Greg Clayton | 23b8abb | 2011-09-26 07:11:27 +0000 | [diff] [blame] | 146 | sb_data.SetOpaque (data_extractor_sp); |
| 147 | } |
| 148 | } |
| 149 | return sb_data; |
| 150 | } |
| 151 | |
| 152 | |
| 153 | |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 154 | bool |
| 155 | SBInstruction::DoesBranch () |
| 156 | { |
| 157 | if (m_opaque_sp) |
| 158 | return m_opaque_sp->DoesBranch (); |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | void |
| 163 | SBInstruction::SetOpaque (const lldb::InstructionSP &inst_sp) |
| 164 | { |
| 165 | m_opaque_sp = inst_sp; |
| 166 | } |
| 167 | |
| 168 | bool |
| 169 | SBInstruction::GetDescription (lldb::SBStream &s) |
| 170 | { |
| 171 | if (m_opaque_sp) |
| 172 | { |
| 173 | // Use the "ref()" instead of the "get()" accessor in case the SBStream |
| 174 | // didn't have a stream already created, one will get created... |
Greg Clayton | 0fef968 | 2012-05-10 02:52:23 +0000 | [diff] [blame] | 175 | m_opaque_sp->Dump (&s.ref(), 0, true, false, NULL); |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 176 | return true; |
| 177 | } |
| 178 | return false; |
| 179 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 180 | |
| 181 | void |
| 182 | SBInstruction::Print (FILE *out) |
| 183 | { |
| 184 | if (out == NULL) |
| 185 | return; |
| 186 | |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 187 | if (m_opaque_sp) |
| 188 | { |
Greg Clayton | 5892856 | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 189 | StreamFile out_stream (out, false); |
Greg Clayton | 0fef968 | 2012-05-10 02:52:23 +0000 | [diff] [blame] | 190 | m_opaque_sp->Dump (&out_stream, 0, true, false, NULL); |
Greg Clayton | 5c4c746 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 191 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 192 | } |
Caroline Tice | af59180 | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 193 | |
| 194 | bool |
Greg Clayton | 888a733 | 2011-04-26 04:39:08 +0000 | [diff] [blame] | 195 | SBInstruction::EmulateWithFrame (lldb::SBFrame &frame, uint32_t evaluate_options) |
Caroline Tice | af59180 | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 196 | { |
Greg Clayton | 334d33a | 2012-01-30 07:41:31 +0000 | [diff] [blame] | 197 | if (m_opaque_sp) |
Caroline Tice | af59180 | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 198 | { |
Greg Clayton | 334d33a | 2012-01-30 07:41:31 +0000 | [diff] [blame] | 199 | lldb::StackFrameSP frame_sp (frame.GetFrameSP()); |
| 200 | |
| 201 | if (frame_sp) |
| 202 | { |
| 203 | lldb_private::ExecutionContext exe_ctx; |
| 204 | frame_sp->CalculateExecutionContext (exe_ctx); |
| 205 | lldb_private::Target *target = exe_ctx.GetTargetPtr(); |
| 206 | lldb_private::ArchSpec arch = target->GetArchitecture(); |
| 207 | |
| 208 | return m_opaque_sp->Emulate (arch, |
| 209 | evaluate_options, |
| 210 | (void *) frame_sp.get(), |
| 211 | &lldb_private::EmulateInstruction::ReadMemoryFrame, |
| 212 | &lldb_private::EmulateInstruction::WriteMemoryFrame, |
| 213 | &lldb_private::EmulateInstruction::ReadRegisterFrame, |
| 214 | &lldb_private::EmulateInstruction::WriteRegisterFrame); |
| 215 | } |
Caroline Tice | af59180 | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 216 | } |
| 217 | return false; |
| 218 | } |
| 219 | |
| 220 | bool |
| 221 | SBInstruction::DumpEmulation (const char *triple) |
| 222 | { |
| 223 | if (m_opaque_sp && triple) |
| 224 | { |
Greg Clayton | f15996e | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 225 | lldb_private::ArchSpec arch (triple, NULL); |
Caroline Tice | af59180 | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 226 | |
Caroline Tice | 0fe5a53 | 2011-04-08 23:33:06 +0000 | [diff] [blame] | 227 | return m_opaque_sp->DumpEmulation (arch); |
| 228 | |
Caroline Tice | af59180 | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 229 | } |
| 230 | return false; |
| 231 | } |
| 232 | |
Caroline Tice | 6b8d3b5 | 2011-04-19 23:30:03 +0000 | [diff] [blame] | 233 | bool |
| 234 | SBInstruction::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 Chen | 09008d0 | 2011-04-21 20:27:45 +0000 | [diff] [blame] | 239 | return m_opaque_sp->TestEmulation (output_stream.get(), test_file); |
| 240 | } |
Greg Clayton | 7fb1430 | 2012-04-13 00:07:34 +0000 | [diff] [blame] | 241 | |
| 242 | lldb::AddressClass |
| 243 | SBInstruction::GetAddressClass () |
| 244 | { |
| 245 | if (m_opaque_sp.get()) |
| 246 | return m_opaque_sp->GetAddressClass(); |
| 247 | return eAddressClassInvalid; |
| 248 | } |