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