1 //===-- SBInstructionList.cpp -----------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
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"
20 using namespace lldb_private;
22 SBInstructionList::SBInstructionList() : m_opaque_sp() {
23 LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBInstructionList);
26 SBInstructionList::SBInstructionList(const SBInstructionList &rhs)
27 : m_opaque_sp(rhs.m_opaque_sp) {
28 LLDB_RECORD_CONSTRUCTOR(SBInstructionList, (const lldb::SBInstructionList &),
32 const SBInstructionList &SBInstructionList::
33 operator=(const SBInstructionList &rhs) {
35 const lldb::SBInstructionList &,
36 SBInstructionList, operator=,(const lldb::SBInstructionList &), rhs);
39 m_opaque_sp = rhs.m_opaque_sp;
40 return LLDB_RECORD_RESULT(*this);
43 SBInstructionList::~SBInstructionList() {}
45 bool SBInstructionList::IsValid() const {
46 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBInstructionList, IsValid);
47 return this->operator bool();
49 SBInstructionList::operator bool() const {
50 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBInstructionList, operator bool);
52 return m_opaque_sp.get() != nullptr;
55 size_t SBInstructionList::GetSize() {
56 LLDB_RECORD_METHOD_NO_ARGS(size_t, SBInstructionList, GetSize);
59 return m_opaque_sp->GetInstructionList().GetSize();
63 SBInstruction SBInstructionList::GetInstructionAtIndex(uint32_t idx) {
64 LLDB_RECORD_METHOD(lldb::SBInstruction, SBInstructionList,
65 GetInstructionAtIndex, (uint32_t), idx);
68 if (m_opaque_sp && idx < m_opaque_sp->GetInstructionList().GetSize())
71 m_opaque_sp->GetInstructionList().GetInstructionAtIndex(idx));
72 return LLDB_RECORD_RESULT(inst);
75 size_t SBInstructionList::GetInstructionsCount(const SBAddress &start,
77 bool canSetBreakpoint) {
78 LLDB_RECORD_METHOD(size_t, SBInstructionList, GetInstructionsCount,
79 (const lldb::SBAddress &, const lldb::SBAddress &, bool),
80 start, end, canSetBreakpoint);
82 size_t num_instructions = GetSize();
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();
96 for (i = lower_index; i <= upper_index; ++i) {
97 SBInstruction insn = GetInstructionAtIndex(i);
98 if (!insn.CanSetBreakpoint())
99 ++instructions_to_skip;
101 return upper_index - lower_index - instructions_to_skip;
104 void SBInstructionList::Clear() {
105 LLDB_RECORD_METHOD_NO_ARGS(void, SBInstructionList, Clear);
110 void SBInstructionList::AppendInstruction(SBInstruction insn) {
111 LLDB_RECORD_METHOD(void, SBInstructionList, AppendInstruction,
112 (lldb::SBInstruction), insn);
115 void SBInstructionList::SetDisassembler(const lldb::DisassemblerSP &opaque_sp) {
116 m_opaque_sp = opaque_sp;
119 void SBInstructionList::Print(FILE *out) {
120 LLDB_RECORD_METHOD(void, SBInstructionList, Print, (FILE *), out);
126 bool SBInstructionList::GetDescription(lldb::SBStream &description) {
127 LLDB_RECORD_METHOD(bool, SBInstructionList, GetDescription,
128 (lldb::SBStream &), description);
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);
141 SymbolContext prev_sc;
142 for (size_t i = 0; i < num_instructions; ++i) {
144 m_opaque_sp->GetInstructionList().GetInstructionAtIndex(i).get();
148 const Address &addr = inst->GetAddress();
150 ModuleSP module_sp(addr.GetModule());
152 module_sp->ResolveSymbolContextForAddress(
153 addr, eSymbolContextEverything, sc);
156 inst->Dump(&sref, max_opcode_byte_size, true, false, nullptr, &sc,
157 &prev_sc, &format, 0);
166 bool SBInstructionList::DumpEmulationForAllInstructions(const char *triple) {
167 LLDB_RECORD_METHOD(bool, SBInstructionList, DumpEmulationForAllInstructions,
168 (const char *), triple);
171 size_t len = GetSize();
172 for (size_t i = 0; i < len; ++i) {
173 if (!GetInstructionAtIndex((uint32_t)i).DumpEmulation(triple))
180 namespace lldb_private {
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,
205 LLDB_REGISTER_METHOD(bool, SBInstructionList,
206 DumpEmulationForAllInstructions, (const char *));