Chris Lattner | 30fdc8d | 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 | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 12 | #include "lldb/API/SBAddress.h" |
Caroline Tice | 7c9dd3c | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 13 | #include "lldb/API/SBFrame.h" |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 14 | #include "lldb/API/SBInstruction.h" |
| 15 | #include "lldb/API/SBStream.h" |
Caroline Tice | 7c9dd3c | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 16 | #include "lldb/API/SBTarget.h" |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 17 | |
Caroline Tice | 7c9dd3c | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 18 | #include "lldb/Core/ArchSpec.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 19 | #include "lldb/Core/Disassembler.h" |
Caroline Tice | 7c9dd3c | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 20 | #include "lldb/Core/EmulateInstruction.h" |
Jason Molenda | aff1b35 | 2014-10-10 23:07:36 +0000 | [diff] [blame] | 21 | #include "lldb/Core/Module.h" |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 22 | #include "lldb/Core/StreamFile.h" |
Pavel Labath | 7263f1b | 2017-10-31 10:56:03 +0000 | [diff] [blame^] | 23 | #include "lldb/Host/HostInfo.h" |
Caroline Tice | 7c9dd3c | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 24 | #include "lldb/Target/ExecutionContext.h" |
Jason Molenda | b57e4a1 | 2013-11-04 09:33:30 +0000 | [diff] [blame] | 25 | #include "lldb/Target/StackFrame.h" |
Caroline Tice | 7c9dd3c | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 26 | #include "lldb/Target/Target.h" |
Zachary Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 27 | #include "lldb/Utility/DataBufferHeap.h" |
| 28 | #include "lldb/Utility/DataExtractor.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 29 | |
Greg Clayton | 4a9d83a | 2016-06-07 22:56:40 +0000 | [diff] [blame] | 30 | //---------------------------------------------------------------------- |
| 31 | // We recently fixed a leak in one of the Instruction subclasses where |
| 32 | // the instruction will only hold a weak reference to the disassembler |
| 33 | // to avoid a cycle that was keeping both objects alive (leak) and we |
| 34 | // need the InstructionImpl class to make sure our public API behaves |
| 35 | // as users would expect. Calls in our public API allow clients to do |
| 36 | // things like: |
| 37 | // |
| 38 | // 1 lldb::SBInstruction inst; |
| 39 | // 2 inst = target.ReadInstructions(pc, 1).GetInstructionAtIndex(0) |
| 40 | // 3 if (inst.DoesBranch()) |
| 41 | // 4 ... |
| 42 | // |
| 43 | // There was a temporary lldb::DisassemblerSP object created in the |
| 44 | // SBInstructionList that was returned by lldb.target.ReadInstructions() |
| 45 | // that will go away after line 2 but the "inst" object should be able |
| 46 | // to still answer questions about itself. So we make sure that any |
| 47 | // SBInstruction objects that are given out have a strong reference to |
| 48 | // the disassembler and the instruction so that the object can live and |
| 49 | // successfully respond to all queries. |
| 50 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 51 | class InstructionImpl { |
Greg Clayton | 4a9d83a | 2016-06-07 22:56:40 +0000 | [diff] [blame] | 52 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 53 | InstructionImpl(const lldb::DisassemblerSP &disasm_sp, |
| 54 | const lldb::InstructionSP &inst_sp) |
| 55 | : m_disasm_sp(disasm_sp), m_inst_sp(inst_sp) {} |
Greg Clayton | 4a9d83a | 2016-06-07 22:56:40 +0000 | [diff] [blame] | 56 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 57 | lldb::InstructionSP GetSP() const { return m_inst_sp; } |
Greg Clayton | 4a9d83a | 2016-06-07 22:56:40 +0000 | [diff] [blame] | 58 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 59 | bool IsValid() const { return (bool)m_inst_sp; } |
Greg Clayton | 4a9d83a | 2016-06-07 22:56:40 +0000 | [diff] [blame] | 60 | |
| 61 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 62 | lldb::DisassemblerSP m_disasm_sp; // Can be empty/invalid |
| 63 | lldb::InstructionSP m_inst_sp; |
Greg Clayton | 4a9d83a | 2016-06-07 22:56:40 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 66 | using namespace lldb; |
| 67 | using namespace lldb_private; |
| 68 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 69 | SBInstruction::SBInstruction() : m_opaque_sp() {} |
| 70 | |
| 71 | SBInstruction::SBInstruction(const lldb::DisassemblerSP &disasm_sp, |
| 72 | const lldb::InstructionSP &inst_sp) |
| 73 | : m_opaque_sp(new InstructionImpl(disasm_sp, inst_sp)) {} |
| 74 | |
| 75 | SBInstruction::SBInstruction(const SBInstruction &rhs) |
| 76 | : m_opaque_sp(rhs.m_opaque_sp) {} |
| 77 | |
| 78 | const SBInstruction &SBInstruction::operator=(const SBInstruction &rhs) { |
| 79 | if (this != &rhs) |
| 80 | m_opaque_sp = rhs.m_opaque_sp; |
| 81 | return *this; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 84 | SBInstruction::~SBInstruction() {} |
| 85 | |
| 86 | bool SBInstruction::IsValid() { return m_opaque_sp && m_opaque_sp->IsValid(); } |
| 87 | |
| 88 | SBAddress SBInstruction::GetAddress() { |
| 89 | SBAddress sb_addr; |
| 90 | lldb::InstructionSP inst_sp(GetOpaque()); |
| 91 | if (inst_sp && inst_sp->GetAddress().IsValid()) |
| 92 | sb_addr.SetAddress(&inst_sp->GetAddress()); |
| 93 | return sb_addr; |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 96 | const char *SBInstruction::GetMnemonic(SBTarget target) { |
| 97 | lldb::InstructionSP inst_sp(GetOpaque()); |
| 98 | if (inst_sp) { |
| 99 | ExecutionContext exe_ctx; |
| 100 | TargetSP target_sp(target.GetSP()); |
| 101 | std::unique_lock<std::recursive_mutex> lock; |
| 102 | if (target_sp) { |
| 103 | lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex()); |
Greg Clayton | efabb12 | 2010-11-05 23:17:00 +0000 | [diff] [blame] | 104 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 105 | target_sp->CalculateExecutionContext(exe_ctx); |
| 106 | exe_ctx.SetProcessSP(target_sp->GetProcessSP()); |
Greg Clayton | 8f7180b | 2011-09-26 07:11:27 +0000 | [diff] [blame] | 107 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 108 | return inst_sp->GetMnemonic(&exe_ctx); |
| 109 | } |
| 110 | return NULL; |
Greg Clayton | 8f7180b | 2011-09-26 07:11:27 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 113 | const char *SBInstruction::GetOperands(SBTarget target) { |
| 114 | lldb::InstructionSP inst_sp(GetOpaque()); |
| 115 | if (inst_sp) { |
| 116 | ExecutionContext exe_ctx; |
| 117 | TargetSP target_sp(target.GetSP()); |
| 118 | std::unique_lock<std::recursive_mutex> lock; |
| 119 | if (target_sp) { |
| 120 | lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex()); |
Saleem Abdulrasool | bb19a13 | 2016-05-19 05:13:57 +0000 | [diff] [blame] | 121 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 122 | target_sp->CalculateExecutionContext(exe_ctx); |
| 123 | exe_ctx.SetProcessSP(target_sp->GetProcessSP()); |
Greg Clayton | 8f7180b | 2011-09-26 07:11:27 +0000 | [diff] [blame] | 124 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 125 | return inst_sp->GetOperands(&exe_ctx); |
| 126 | } |
| 127 | return NULL; |
Greg Clayton | 8f7180b | 2011-09-26 07:11:27 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 130 | const char *SBInstruction::GetComment(SBTarget target) { |
| 131 | lldb::InstructionSP inst_sp(GetOpaque()); |
| 132 | if (inst_sp) { |
| 133 | ExecutionContext exe_ctx; |
| 134 | TargetSP target_sp(target.GetSP()); |
| 135 | std::unique_lock<std::recursive_mutex> lock; |
| 136 | if (target_sp) { |
| 137 | lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex()); |
Saleem Abdulrasool | bb19a13 | 2016-05-19 05:13:57 +0000 | [diff] [blame] | 138 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 139 | target_sp->CalculateExecutionContext(exe_ctx); |
| 140 | exe_ctx.SetProcessSP(target_sp->GetProcessSP()); |
Greg Clayton | 8f7180b | 2011-09-26 07:11:27 +0000 | [diff] [blame] | 141 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 142 | return inst_sp->GetComment(&exe_ctx); |
| 143 | } |
| 144 | return NULL; |
Greg Clayton | 8f7180b | 2011-09-26 07:11:27 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 147 | size_t SBInstruction::GetByteSize() { |
| 148 | lldb::InstructionSP inst_sp(GetOpaque()); |
| 149 | if (inst_sp) |
| 150 | return inst_sp->GetOpcode().GetByteSize(); |
| 151 | return 0; |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 152 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 153 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 154 | SBData SBInstruction::GetData(SBTarget target) { |
| 155 | lldb::SBData sb_data; |
| 156 | lldb::InstructionSP inst_sp(GetOpaque()); |
| 157 | if (inst_sp) { |
| 158 | DataExtractorSP data_extractor_sp(new DataExtractor()); |
| 159 | if (inst_sp->GetData(*data_extractor_sp)) { |
| 160 | sb_data.SetOpaque(data_extractor_sp); |
Greg Clayton | 8f7180b | 2011-09-26 07:11:27 +0000 | [diff] [blame] | 161 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 162 | } |
| 163 | return sb_data; |
Greg Clayton | 8f7180b | 2011-09-26 07:11:27 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 166 | bool SBInstruction::DoesBranch() { |
| 167 | lldb::InstructionSP inst_sp(GetOpaque()); |
| 168 | if (inst_sp) |
| 169 | return inst_sp->DoesBranch(); |
| 170 | return false; |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 173 | bool SBInstruction::HasDelaySlot() { |
| 174 | lldb::InstructionSP inst_sp(GetOpaque()); |
| 175 | if (inst_sp) |
| 176 | return inst_sp->HasDelaySlot(); |
| 177 | return false; |
Bhushan D. Attarde | df5f0b4 | 2016-01-27 10:16:30 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Nitesh Jain | dd12594 | 2017-05-04 11:34:42 +0000 | [diff] [blame] | 180 | bool SBInstruction::CanSetBreakpoint () { |
| 181 | lldb::InstructionSP inst_sp(GetOpaque()); |
| 182 | if (inst_sp) |
| 183 | return inst_sp->CanSetBreakpoint(); |
| 184 | return false; |
| 185 | } |
| 186 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 187 | lldb::InstructionSP SBInstruction::GetOpaque() { |
| 188 | if (m_opaque_sp) |
| 189 | return m_opaque_sp->GetSP(); |
| 190 | else |
| 191 | return lldb::InstructionSP(); |
Greg Clayton | 4a9d83a | 2016-06-07 22:56:40 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 194 | void SBInstruction::SetOpaque(const lldb::DisassemblerSP &disasm_sp, |
| 195 | const lldb::InstructionSP &inst_sp) { |
| 196 | m_opaque_sp.reset(new InstructionImpl(disasm_sp, inst_sp)); |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 199 | bool SBInstruction::GetDescription(lldb::SBStream &s) { |
| 200 | lldb::InstructionSP inst_sp(GetOpaque()); |
| 201 | if (inst_sp) { |
| 202 | SymbolContext sc; |
| 203 | const Address &addr = inst_sp->GetAddress(); |
| 204 | ModuleSP module_sp(addr.GetModule()); |
| 205 | if (module_sp) |
| 206 | module_sp->ResolveSymbolContextForAddress(addr, eSymbolContextEverything, |
| 207 | sc); |
| 208 | // Use the "ref()" instead of the "get()" accessor in case the SBStream |
| 209 | // didn't have a stream already created, one will get created... |
| 210 | FormatEntity::Entry format; |
| 211 | FormatEntity::Parse("${addr}: ", format); |
| 212 | inst_sp->Dump(&s.ref(), 0, true, false, NULL, &sc, NULL, &format, 0); |
| 213 | return true; |
| 214 | } |
| 215 | return false; |
| 216 | } |
| 217 | |
| 218 | void SBInstruction::Print(FILE *out) { |
| 219 | if (out == NULL) |
| 220 | return; |
| 221 | |
| 222 | lldb::InstructionSP inst_sp(GetOpaque()); |
| 223 | if (inst_sp) { |
| 224 | SymbolContext sc; |
| 225 | const Address &addr = inst_sp->GetAddress(); |
| 226 | ModuleSP module_sp(addr.GetModule()); |
| 227 | if (module_sp) |
| 228 | module_sp->ResolveSymbolContextForAddress(addr, eSymbolContextEverything, |
| 229 | sc); |
| 230 | StreamFile out_stream(out, false); |
| 231 | FormatEntity::Entry format; |
| 232 | FormatEntity::Parse("${addr}: ", format); |
| 233 | inst_sp->Dump(&out_stream, 0, true, false, NULL, &sc, NULL, &format, 0); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | bool SBInstruction::EmulateWithFrame(lldb::SBFrame &frame, |
| 238 | uint32_t evaluate_options) { |
| 239 | lldb::InstructionSP inst_sp(GetOpaque()); |
| 240 | if (inst_sp) { |
| 241 | lldb::StackFrameSP frame_sp(frame.GetFrameSP()); |
| 242 | |
| 243 | if (frame_sp) { |
| 244 | lldb_private::ExecutionContext exe_ctx; |
| 245 | frame_sp->CalculateExecutionContext(exe_ctx); |
| 246 | lldb_private::Target *target = exe_ctx.GetTargetPtr(); |
| 247 | lldb_private::ArchSpec arch = target->GetArchitecture(); |
| 248 | |
| 249 | return inst_sp->Emulate( |
| 250 | arch, evaluate_options, (void *)frame_sp.get(), |
| 251 | &lldb_private::EmulateInstruction::ReadMemoryFrame, |
| 252 | &lldb_private::EmulateInstruction::WriteMemoryFrame, |
| 253 | &lldb_private::EmulateInstruction::ReadRegisterFrame, |
| 254 | &lldb_private::EmulateInstruction::WriteRegisterFrame); |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 255 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 256 | } |
| 257 | return false; |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 258 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 259 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 260 | bool SBInstruction::DumpEmulation(const char *triple) { |
| 261 | lldb::InstructionSP inst_sp(GetOpaque()); |
| 262 | if (inst_sp && triple) { |
Pavel Labath | 7263f1b | 2017-10-31 10:56:03 +0000 | [diff] [blame^] | 263 | return inst_sp->DumpEmulation(HostInfo::GetAugmentedArchSpec(triple)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 264 | } |
| 265 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 266 | } |
Caroline Tice | 7c9dd3c | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 267 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 268 | bool SBInstruction::TestEmulation(lldb::SBStream &output_stream, |
| 269 | const char *test_file) { |
| 270 | if (!m_opaque_sp) |
| 271 | SetOpaque(lldb::DisassemblerSP(), |
| 272 | lldb::InstructionSP(new PseudoInstruction())); |
Greg Clayton | b9556ac | 2012-01-30 07:41:31 +0000 | [diff] [blame] | 273 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 274 | lldb::InstructionSP inst_sp(GetOpaque()); |
| 275 | if (inst_sp) |
| 276 | return inst_sp->TestEmulation(output_stream.get(), test_file); |
| 277 | return false; |
Caroline Tice | 7c9dd3c | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 280 | lldb::AddressClass SBInstruction::GetAddressClass() { |
| 281 | lldb::InstructionSP inst_sp(GetOpaque()); |
| 282 | if (inst_sp) |
| 283 | return inst_sp->GetAddressClass(); |
| 284 | return eAddressClassInvalid; |
Greg Clayton | c8e0c24 | 2012-04-13 00:07:34 +0000 | [diff] [blame] | 285 | } |