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