LLDB  mainline
SBInstructionList.cpp
Go to the documentation of this file.
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 
10 #include "lldb/API/SBAddress.h"
11 #include "lldb/API/SBFile.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/Core/StreamFile.h"
19 #include "lldb/Utility/Stream.h"
20 
21 using namespace lldb;
22 using namespace lldb_private;
23 
25 
27  : m_opaque_sp(rhs.m_opaque_sp) {
28  LLDB_INSTRUMENT_VA(this, rhs);
29 }
30 
33  LLDB_INSTRUMENT_VA(this, rhs);
34 
35  if (this != &rhs)
37  return *this;
38 }
39 
41 
43  LLDB_INSTRUMENT_VA(this);
44  return this->operator bool();
45 }
46 SBInstructionList::operator bool() const {
47  LLDB_INSTRUMENT_VA(this);
48 
49  return m_opaque_sp.get() != nullptr;
50 }
51 
53  LLDB_INSTRUMENT_VA(this);
54 
55  if (m_opaque_sp)
56  return m_opaque_sp->GetInstructionList().GetSize();
57  return 0;
58 }
59 
61  LLDB_INSTRUMENT_VA(this, idx);
62 
63  SBInstruction inst;
64  if (m_opaque_sp && idx < m_opaque_sp->GetInstructionList().GetSize())
65  inst.SetOpaque(
67  m_opaque_sp->GetInstructionList().GetInstructionAtIndex(idx));
68  return inst;
69 }
70 
72  const SBAddress &end,
73  bool canSetBreakpoint) {
74  LLDB_INSTRUMENT_VA(this, start, end, canSetBreakpoint);
75 
76  size_t num_instructions = GetSize();
77  size_t i = 0;
78  SBAddress addr;
79  size_t lower_index = 0;
80  size_t upper_index = 0;
81  size_t instructions_to_skip = 0;
82  for (i = 0; i < num_instructions; ++i) {
84  if (start == addr)
85  lower_index = i;
86  if (end == addr)
87  upper_index = i;
88  }
89  if (canSetBreakpoint)
90  for (i = lower_index; i <= upper_index; ++i) {
92  if (!insn.CanSetBreakpoint())
93  ++instructions_to_skip;
94  }
95  return upper_index - lower_index - instructions_to_skip;
96 }
97 
99  LLDB_INSTRUMENT_VA(this);
100 
101  m_opaque_sp.reset();
102 }
103 
105  LLDB_INSTRUMENT_VA(this, insn);
106 }
107 
108 void SBInstructionList::SetDisassembler(const lldb::DisassemblerSP &opaque_sp) {
109  m_opaque_sp = opaque_sp;
110 }
111 
112 void SBInstructionList::Print(FILE *out) {
113  LLDB_INSTRUMENT_VA(this, out);
114  if (out == nullptr)
115  return;
116  StreamFile stream(out, false);
117  GetDescription(stream);
118 }
119 
121  LLDB_INSTRUMENT_VA(this, out);
122  if (!out.IsValid())
123  return;
124  StreamFile stream(out.m_opaque_sp);
125  GetDescription(stream);
126 }
127 
128 void SBInstructionList::Print(FileSP out_sp) {
129  LLDB_INSTRUMENT_VA(this, out_sp);
130  if (!out_sp || !out_sp->IsValid())
131  return;
132  StreamFile stream(out_sp);
133  GetDescription(stream);
134 }
135 
137  LLDB_INSTRUMENT_VA(this, stream);
138  return GetDescription(stream.ref());
139 }
140 
142 
143  if (m_opaque_sp) {
144  size_t num_instructions = GetSize();
145  if (num_instructions) {
146  // Call the ref() to make sure a stream is created if one deesn't exist
147  // already inside description...
148  const uint32_t max_opcode_byte_size =
149  m_opaque_sp->GetInstructionList().GetMaxOpcocdeByteSize();
150  FormatEntity::Entry format;
151  FormatEntity::Parse("${addr}: ", format);
152  SymbolContext sc;
153  SymbolContext prev_sc;
154  for (size_t i = 0; i < num_instructions; ++i) {
155  Instruction *inst =
156  m_opaque_sp->GetInstructionList().GetInstructionAtIndex(i).get();
157  if (inst == nullptr)
158  break;
159 
160  const Address &addr = inst->GetAddress();
161  prev_sc = sc;
162  ModuleSP module_sp(addr.GetModule());
163  if (module_sp) {
164  module_sp->ResolveSymbolContextForAddress(
165  addr, eSymbolContextEverything, sc);
166  }
167 
168  inst->Dump(&sref, max_opcode_byte_size, true, false,
169  /*show_control_flow_kind=*/false, nullptr, &sc, &prev_sc,
170  &format, 0);
171  sref.EOL();
172  }
173  return true;
174  }
175  }
176  return false;
177 }
178 
180  LLDB_INSTRUMENT_VA(this, triple);
181 
182  if (m_opaque_sp) {
183  size_t len = GetSize();
184  for (size_t i = 0; i < len; ++i) {
185  if (!GetInstructionAtIndex((uint32_t)i).DumpEmulation(triple))
186  return false;
187  }
188  }
189  return true;
190 }
Instrumentation.h
lldb::SBInstructionList::DumpEmulationForAllInstructions
bool DumpEmulationForAllInstructions(const char *triple)
Definition: SBInstructionList.cpp:179
lldb::SBFile::IsValid
bool IsValid() const
Definition: SBFile.cpp:101
lldb::SBStream::ref
lldb_private::Stream & ref()
Definition: SBStream.cpp:176
lldb::SBFile::m_opaque_sp
FileSP m_opaque_sp
Definition: SBFile.h:45
lldb::SBInstructionList::AppendInstruction
void AppendInstruction(lldb::SBInstruction inst)
Definition: SBInstructionList.cpp:104
lldb::SBInstructionList::GetSize
size_t GetSize()
Definition: SBInstructionList.cpp:52
lldb::SBInstructionList::operator=
const SBInstructionList & operator=(const SBInstructionList &rhs)
Definition: SBInstructionList.cpp:32
lldb::SBInstructionList::GetDescription
bool GetDescription(lldb::SBStream &description)
Definition: SBInstructionList.cpp:136
lldb::SBInstruction::GetAddress
SBAddress GetAddress()
Definition: SBInstruction.cpp:100
Module.h
Disassembler.h
lldb_private::Instruction
Definition: Disassembler.h:58
lldb_private::Stream
Definition: Stream.h:28
lldb::SBInstructionList::m_opaque_sp
lldb::DisassemblerSP m_opaque_sp
Definition: SBInstructionList.h:67
lldb::SBInstructionList::Clear
void Clear()
Definition: SBInstructionList.cpp:98
lldb_private::SymbolContext
Definition: SymbolContext.h:33
lldb::SBInstructionList
Definition: SBInstructionList.h:18
lldb::SBInstruction::SetOpaque
void SetOpaque(const lldb::DisassemblerSP &disasm_sp, const lldb::InstructionSP &inst_sp)
Definition: SBInstruction.cpp:243
SBStream.h
lldb::SBFile
Definition: SBFile.h:16
lldb::SBInstructionList::GetInstructionsCount
size_t GetInstructionsCount(const SBAddress &start, const SBAddress &end, bool canSetBreakpoint=false)
Definition: SBInstructionList.cpp:71
lldb::SBInstruction::CanSetBreakpoint
bool CanSetBreakpoint()
Definition: SBInstruction.cpp:227
lldb_private::Instruction::Dump
virtual void Dump(Stream *s, uint32_t max_opcode_byte_size, bool show_address, bool show_bytes, bool show_control_flow_kind, const ExecutionContext *exe_ctx, const SymbolContext *sym_ctx, const SymbolContext *prev_sym_ctx, const FormatEntity::Entry *disassembly_addr_format, size_t max_address_text_size)
Dump the text representation of this Instruction to a Stream.
Definition: Disassembler.cpp:605
lldb_private::Instruction::GetAddress
const Address & GetAddress() const
Definition: Disassembler.h:65
SBInstructionList.h
lldb::SBInstructionList::~SBInstructionList
~SBInstructionList()
lldb::SBInstructionList::Print
void Print(FILE *out)
Definition: SBInstructionList.cpp:112
SBAddress.h
lldb::SBInstructionList::SBInstructionList
SBInstructionList()
Definition: SBInstructionList.cpp:24
StreamFile.h
SBFile.h
uint32_t
lldb_private::Address
Definition: Address.h:59
SBInstruction.h
lldb_private::Stream::EOL
size_t EOL()
Output and End of Line character to the stream.
Definition: Stream.cpp:128
lldb::SBInstructionList::GetInstructionAtIndex
lldb::SBInstruction GetInstructionAtIndex(uint32_t idx)
Definition: SBInstructionList.cpp:60
lldb::SBInstructionList::SetDisassembler
void SetDisassembler(const lldb::DisassemblerSP &opaque_sp)
Definition: SBInstructionList.cpp:108
lldb_private::FormatEntity::Entry
Definition: FormatEntity.h:40
lldb::SBInstruction
Definition: SBInstruction.h:24
lldb::SBInstructionList::IsValid
bool IsValid() const
Definition: SBInstructionList.cpp:42
lldb::SBAddress
Definition: SBAddress.h:17
SymbolContext.h
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
LLDB_INSTRUMENT_VA
#define LLDB_INSTRUMENT_VA(...)
Definition: Instrumentation.h:98
lldb_private::StreamFile
Definition: StreamFile.h:22
lldb_private::Address::GetModule
lldb::ModuleSP GetModule() const
Get accessor for the module for this address.
Definition: Address.cpp:283
Stream.h
lldb::SBStream
Definition: SBStream.h:18
lldb
Definition: SBAddress.h:15