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 *this;
41 }
42 
43 SBInstructionList::~SBInstructionList() {}
44 
45 bool SBInstructionList::IsValid() const {
46   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBInstructionList, IsValid);
47 
48   return m_opaque_sp.get() != NULL;
49 }
50 
51 size_t SBInstructionList::GetSize() {
52   LLDB_RECORD_METHOD_NO_ARGS(size_t, SBInstructionList, GetSize);
53 
54   if (m_opaque_sp)
55     return m_opaque_sp->GetInstructionList().GetSize();
56   return 0;
57 }
58 
59 SBInstruction SBInstructionList::GetInstructionAtIndex(uint32_t idx) {
60   LLDB_RECORD_METHOD(lldb::SBInstruction, SBInstructionList,
61                      GetInstructionAtIndex, (uint32_t), idx);
62 
63   SBInstruction inst;
64   if (m_opaque_sp && idx < m_opaque_sp->GetInstructionList().GetSize())
65     inst.SetOpaque(
66         m_opaque_sp,
67         m_opaque_sp->GetInstructionList().GetInstructionAtIndex(idx));
68   return LLDB_RECORD_RESULT(inst);
69 }
70 
71 size_t SBInstructionList::GetInstructionsCount(const SBAddress &start,
72                                                const SBAddress &end,
73                                                bool canSetBreakpoint) {
74   LLDB_RECORD_METHOD(size_t, SBInstructionList, GetInstructionsCount,
75                      (const lldb::SBAddress &, const lldb::SBAddress &, bool),
76                      start, end, canSetBreakpoint);
77 
78   size_t num_instructions = GetSize();
79   size_t i = 0;
80   SBAddress addr;
81   size_t lower_index = 0;
82   size_t upper_index = 0;
83   size_t instructions_to_skip = 0;
84   for (i = 0; i < num_instructions; ++i) {
85     addr = GetInstructionAtIndex(i).GetAddress();
86     if (start == addr)
87       lower_index = i;
88     if (end == addr)
89       upper_index = i;
90   }
91   if (canSetBreakpoint)
92     for (i = lower_index; i <= upper_index; ++i) {
93       SBInstruction insn = GetInstructionAtIndex(i);
94       if (!insn.CanSetBreakpoint())
95         ++instructions_to_skip;
96     }
97   return upper_index - lower_index - instructions_to_skip;
98 }
99 
100 void SBInstructionList::Clear() {
101   LLDB_RECORD_METHOD_NO_ARGS(void, SBInstructionList, Clear);
102 
103   m_opaque_sp.reset();
104 }
105 
106 void SBInstructionList::AppendInstruction(SBInstruction insn) {
107   LLDB_RECORD_METHOD(void, SBInstructionList, AppendInstruction,
108                      (lldb::SBInstruction), insn);
109 }
110 
111 void SBInstructionList::SetDisassembler(const lldb::DisassemblerSP &opaque_sp) {
112   m_opaque_sp = opaque_sp;
113 }
114 
115 void SBInstructionList::Print(FILE *out) {
116   LLDB_RECORD_METHOD(void, SBInstructionList, Print, (FILE *), out);
117 
118   if (out == NULL)
119     return;
120 }
121 
122 bool SBInstructionList::GetDescription(lldb::SBStream &description) {
123   LLDB_RECORD_METHOD(bool, SBInstructionList, GetDescription,
124                      (lldb::SBStream &), description);
125 
126   if (m_opaque_sp) {
127     size_t num_instructions = GetSize();
128     if (num_instructions) {
129       // Call the ref() to make sure a stream is created if one deesn't exist
130       // already inside description...
131       Stream &sref = description.ref();
132       const uint32_t max_opcode_byte_size =
133           m_opaque_sp->GetInstructionList().GetMaxOpcocdeByteSize();
134       FormatEntity::Entry format;
135       FormatEntity::Parse("${addr}: ", format);
136       SymbolContext sc;
137       SymbolContext prev_sc;
138       for (size_t i = 0; i < num_instructions; ++i) {
139         Instruction *inst =
140             m_opaque_sp->GetInstructionList().GetInstructionAtIndex(i).get();
141         if (inst == NULL)
142           break;
143 
144         const Address &addr = inst->GetAddress();
145         prev_sc = sc;
146         ModuleSP module_sp(addr.GetModule());
147         if (module_sp) {
148           module_sp->ResolveSymbolContextForAddress(
149               addr, eSymbolContextEverything, sc);
150         }
151 
152         inst->Dump(&sref, max_opcode_byte_size, true, false, NULL, &sc,
153                    &prev_sc, &format, 0);
154         sref.EOL();
155       }
156       return true;
157     }
158   }
159   return false;
160 }
161 
162 bool SBInstructionList::DumpEmulationForAllInstructions(const char *triple) {
163   LLDB_RECORD_METHOD(bool, SBInstructionList, DumpEmulationForAllInstructions,
164                      (const char *), triple);
165 
166   if (m_opaque_sp) {
167     size_t len = GetSize();
168     for (size_t i = 0; i < len; ++i) {
169       if (!GetInstructionAtIndex((uint32_t)i).DumpEmulation(triple))
170         return false;
171     }
172   }
173   return true;
174 }
175