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