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 "lldb/API/SBInstruction.h" 11 #include "lldb/API/SBAddress.h" 12 #include "lldb/API/SBStream.h" 13 #include "lldb/Core/Disassembler.h" 14 #include "lldb/Core/Module.h" 15 #include "lldb/Symbol/SymbolContext.h" 16 #include "lldb/Utility/Stream.h" 17 18 using namespace lldb; 19 using namespace lldb_private; 20 21 SBInstructionList::SBInstructionList() : m_opaque_sp() {} 22 23 SBInstructionList::SBInstructionList(const SBInstructionList &rhs) 24 : m_opaque_sp(rhs.m_opaque_sp) {} 25 26 const SBInstructionList &SBInstructionList:: 27 operator=(const SBInstructionList &rhs) { 28 if (this != &rhs) 29 m_opaque_sp = rhs.m_opaque_sp; 30 return *this; 31 } 32 33 SBInstructionList::~SBInstructionList() {} 34 35 bool SBInstructionList::IsValid() const { return m_opaque_sp.get() != NULL; } 36 37 size_t SBInstructionList::GetSize() { 38 if (m_opaque_sp) 39 return m_opaque_sp->GetInstructionList().GetSize(); 40 return 0; 41 } 42 43 SBInstruction SBInstructionList::GetInstructionAtIndex(uint32_t idx) { 44 SBInstruction inst; 45 if (m_opaque_sp && idx < m_opaque_sp->GetInstructionList().GetSize()) 46 inst.SetOpaque( 47 m_opaque_sp, 48 m_opaque_sp->GetInstructionList().GetInstructionAtIndex(idx)); 49 return inst; 50 } 51 52 size_t SBInstructionList::GetInstructionsCount(const SBAddress &start, 53 const SBAddress &end, 54 bool canSetBreakpoint) { 55 size_t num_instructions = GetSize(); 56 size_t i = 0; 57 SBAddress addr; 58 size_t lower_index = 0; 59 size_t upper_index = 0; 60 size_t instructions_to_skip = 0; 61 for (i = 0; i < num_instructions; ++i) { 62 addr = GetInstructionAtIndex(i).GetAddress(); 63 if (start == addr) 64 lower_index = i; 65 if (end == addr) 66 upper_index = i; 67 } 68 if (canSetBreakpoint) 69 for (i = lower_index; i <= upper_index; ++i) { 70 SBInstruction insn = GetInstructionAtIndex(i); 71 if (!insn.CanSetBreakpoint()) 72 ++instructions_to_skip; 73 } 74 return upper_index - lower_index - instructions_to_skip; 75 } 76 77 void SBInstructionList::Clear() { m_opaque_sp.reset(); } 78 79 void SBInstructionList::AppendInstruction(SBInstruction insn) {} 80 81 void SBInstructionList::SetDisassembler(const lldb::DisassemblerSP &opaque_sp) { 82 m_opaque_sp = opaque_sp; 83 } 84 85 void SBInstructionList::Print(FILE *out) { 86 if (out == NULL) 87 return; 88 } 89 90 bool SBInstructionList::GetDescription(lldb::SBStream &description) { 91 if (m_opaque_sp) { 92 size_t num_instructions = GetSize(); 93 if (num_instructions) { 94 // Call the ref() to make sure a stream is created if one deesn't exist 95 // already inside description... 96 Stream &sref = description.ref(); 97 const uint32_t max_opcode_byte_size = 98 m_opaque_sp->GetInstructionList().GetMaxOpcocdeByteSize(); 99 FormatEntity::Entry format; 100 FormatEntity::Parse("${addr}: ", format); 101 SymbolContext sc; 102 SymbolContext prev_sc; 103 for (size_t i = 0; i < num_instructions; ++i) { 104 Instruction *inst = 105 m_opaque_sp->GetInstructionList().GetInstructionAtIndex(i).get(); 106 if (inst == NULL) 107 break; 108 109 const Address &addr = inst->GetAddress(); 110 prev_sc = sc; 111 ModuleSP module_sp(addr.GetModule()); 112 if (module_sp) { 113 module_sp->ResolveSymbolContextForAddress( 114 addr, eSymbolContextEverything, sc); 115 } 116 117 inst->Dump(&sref, max_opcode_byte_size, true, false, NULL, &sc, 118 &prev_sc, &format, 0); 119 sref.EOL(); 120 } 121 return true; 122 } 123 } 124 return false; 125 } 126 127 bool SBInstructionList::DumpEmulationForAllInstructions(const char *triple) { 128 if (m_opaque_sp) { 129 size_t len = GetSize(); 130 for (size_t i = 0; i < len; ++i) { 131 if (!GetInstructionAtIndex((uint32_t)i).DumpEmulation(triple)) 132 return false; 133 } 134 } 135 return true; 136 } 137