1 //===-- SBInstructionList.cpp -----------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "lldb/API/SBInstructionList.h" 10 #include "SBReproducerPrivate.h" 11 #include "lldb/API/SBAddress.h" 12 #include "lldb/API/SBInstruction.h" 13 #include "lldb/API/SBStream.h" 14 #include "lldb/Core/Disassembler.h" 15 #include "lldb/Core/Module.h" 16 #include "lldb/Symbol/SymbolContext.h" 17 #include "lldb/Utility/Stream.h" 18 19 using namespace lldb; 20 using namespace lldb_private; 21 22 SBInstructionList::SBInstructionList() : m_opaque_sp() { 23 LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBInstructionList); 24 } 25 26 SBInstructionList::SBInstructionList(const SBInstructionList &rhs) 27 : m_opaque_sp(rhs.m_opaque_sp) { 28 LLDB_RECORD_CONSTRUCTOR(SBInstructionList, (const lldb::SBInstructionList &), 29 rhs); 30 } 31 32 const SBInstructionList &SBInstructionList:: 33 operator=(const SBInstructionList &rhs) { 34 LLDB_RECORD_METHOD( 35 const lldb::SBInstructionList &, 36 SBInstructionList, operator=,(const lldb::SBInstructionList &), rhs); 37 38 if (this != &rhs) 39 m_opaque_sp = rhs.m_opaque_sp; 40 return LLDB_RECORD_RESULT(*this); 41 } 42 43 SBInstructionList::~SBInstructionList() {} 44 45 bool SBInstructionList::IsValid() const { 46 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBInstructionList, IsValid); 47 return this->operator bool(); 48 } 49 SBInstructionList::operator bool() const { 50 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBInstructionList, operator bool); 51 52 return m_opaque_sp.get() != nullptr; 53 } 54 55 size_t SBInstructionList::GetSize() { 56 LLDB_RECORD_METHOD_NO_ARGS(size_t, SBInstructionList, GetSize); 57 58 if (m_opaque_sp) 59 return m_opaque_sp->GetInstructionList().GetSize(); 60 return 0; 61 } 62 63 SBInstruction SBInstructionList::GetInstructionAtIndex(uint32_t idx) { 64 LLDB_RECORD_METHOD(lldb::SBInstruction, SBInstructionList, 65 GetInstructionAtIndex, (uint32_t), idx); 66 67 SBInstruction inst; 68 if (m_opaque_sp && idx < m_opaque_sp->GetInstructionList().GetSize()) 69 inst.SetOpaque( 70 m_opaque_sp, 71 m_opaque_sp->GetInstructionList().GetInstructionAtIndex(idx)); 72 return LLDB_RECORD_RESULT(inst); 73 } 74 75 size_t SBInstructionList::GetInstructionsCount(const SBAddress &start, 76 const SBAddress &end, 77 bool canSetBreakpoint) { 78 LLDB_RECORD_METHOD(size_t, SBInstructionList, GetInstructionsCount, 79 (const lldb::SBAddress &, const lldb::SBAddress &, bool), 80 start, end, canSetBreakpoint); 81 82 size_t num_instructions = GetSize(); 83 size_t i = 0; 84 SBAddress addr; 85 size_t lower_index = 0; 86 size_t upper_index = 0; 87 size_t instructions_to_skip = 0; 88 for (i = 0; i < num_instructions; ++i) { 89 addr = GetInstructionAtIndex(i).GetAddress(); 90 if (start == addr) 91 lower_index = i; 92 if (end == addr) 93 upper_index = i; 94 } 95 if (canSetBreakpoint) 96 for (i = lower_index; i <= upper_index; ++i) { 97 SBInstruction insn = GetInstructionAtIndex(i); 98 if (!insn.CanSetBreakpoint()) 99 ++instructions_to_skip; 100 } 101 return upper_index - lower_index - instructions_to_skip; 102 } 103 104 void SBInstructionList::Clear() { 105 LLDB_RECORD_METHOD_NO_ARGS(void, SBInstructionList, Clear); 106 107 m_opaque_sp.reset(); 108 } 109 110 void SBInstructionList::AppendInstruction(SBInstruction insn) { 111 LLDB_RECORD_METHOD(void, SBInstructionList, AppendInstruction, 112 (lldb::SBInstruction), insn); 113 } 114 115 void SBInstructionList::SetDisassembler(const lldb::DisassemblerSP &opaque_sp) { 116 m_opaque_sp = opaque_sp; 117 } 118 119 void SBInstructionList::Print(FILE *out) { 120 LLDB_RECORD_METHOD(void, SBInstructionList, Print, (FILE *), out); 121 122 if (out == nullptr) 123 return; 124 } 125 126 bool SBInstructionList::GetDescription(lldb::SBStream &description) { 127 LLDB_RECORD_METHOD(bool, SBInstructionList, GetDescription, 128 (lldb::SBStream &), description); 129 130 if (m_opaque_sp) { 131 size_t num_instructions = GetSize(); 132 if (num_instructions) { 133 // Call the ref() to make sure a stream is created if one deesn't exist 134 // already inside description... 135 Stream &sref = description.ref(); 136 const uint32_t max_opcode_byte_size = 137 m_opaque_sp->GetInstructionList().GetMaxOpcocdeByteSize(); 138 FormatEntity::Entry format; 139 FormatEntity::Parse("${addr}: ", format); 140 SymbolContext sc; 141 SymbolContext prev_sc; 142 for (size_t i = 0; i < num_instructions; ++i) { 143 Instruction *inst = 144 m_opaque_sp->GetInstructionList().GetInstructionAtIndex(i).get(); 145 if (inst == nullptr) 146 break; 147 148 const Address &addr = inst->GetAddress(); 149 prev_sc = sc; 150 ModuleSP module_sp(addr.GetModule()); 151 if (module_sp) { 152 module_sp->ResolveSymbolContextForAddress( 153 addr, eSymbolContextEverything, sc); 154 } 155 156 inst->Dump(&sref, max_opcode_byte_size, true, false, nullptr, &sc, 157 &prev_sc, &format, 0); 158 sref.EOL(); 159 } 160 return true; 161 } 162 } 163 return false; 164 } 165 166 bool SBInstructionList::DumpEmulationForAllInstructions(const char *triple) { 167 LLDB_RECORD_METHOD(bool, SBInstructionList, DumpEmulationForAllInstructions, 168 (const char *), triple); 169 170 if (m_opaque_sp) { 171 size_t len = GetSize(); 172 for (size_t i = 0; i < len; ++i) { 173 if (!GetInstructionAtIndex((uint32_t)i).DumpEmulation(triple)) 174 return false; 175 } 176 } 177 return true; 178 } 179 180 namespace lldb_private { 181 namespace repro { 182 183 template <> 184 void RegisterMethods<SBInstructionList>(Registry &R) { 185 LLDB_REGISTER_CONSTRUCTOR(SBInstructionList, ()); 186 LLDB_REGISTER_CONSTRUCTOR(SBInstructionList, 187 (const lldb::SBInstructionList &)); 188 LLDB_REGISTER_METHOD( 189 const lldb::SBInstructionList &, 190 SBInstructionList, operator=,(const lldb::SBInstructionList &)); 191 LLDB_REGISTER_METHOD_CONST(bool, SBInstructionList, IsValid, ()); 192 LLDB_REGISTER_METHOD_CONST(bool, SBInstructionList, operator bool, ()); 193 LLDB_REGISTER_METHOD(size_t, SBInstructionList, GetSize, ()); 194 LLDB_REGISTER_METHOD(lldb::SBInstruction, SBInstructionList, 195 GetInstructionAtIndex, (uint32_t)); 196 LLDB_REGISTER_METHOD( 197 size_t, SBInstructionList, GetInstructionsCount, 198 (const lldb::SBAddress &, const lldb::SBAddress &, bool)); 199 LLDB_REGISTER_METHOD(void, SBInstructionList, Clear, ()); 200 LLDB_REGISTER_METHOD(void, SBInstructionList, AppendInstruction, 201 (lldb::SBInstruction)); 202 LLDB_REGISTER_METHOD(void, SBInstructionList, Print, (FILE *)); 203 LLDB_REGISTER_METHOD(bool, SBInstructionList, GetDescription, 204 (lldb::SBStream &)); 205 LLDB_REGISTER_METHOD(bool, SBInstructionList, 206 DumpEmulationForAllInstructions, (const char *)); 207 } 208 209 } 210 } 211