blob: 31585b3e6868a4704ed0910f6d15c72946f7db57 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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"
Greg Clayton1d273162010-10-06 03:09:58 +000012#include "lldb/API/SBStream.h"
13#include "lldb/Core/Disassembler.h"
Jason Molendaaff1b352014-10-10 23:07:36 +000014#include "lldb/Core/Module.h"
Greg Clayton1d273162010-10-06 03:09:58 +000015#include "lldb/Core/Stream.h"
Jason Molendaaff1b352014-10-10 23:07:36 +000016#include "lldb/Symbol/SymbolContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017
18using namespace lldb;
Greg Clayton1d273162010-10-06 03:09:58 +000019using namespace lldb_private;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020
21
Greg Clayton1d273162010-10-06 03:09:58 +000022SBInstructionList::SBInstructionList () :
23 m_opaque_sp()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024{
25}
26
Greg Claytonefabb122010-11-05 23:17:00 +000027SBInstructionList::SBInstructionList(const SBInstructionList &rhs) :
28 m_opaque_sp (rhs.m_opaque_sp)
29{
30}
31
32const SBInstructionList &
33SBInstructionList::operator = (const SBInstructionList &rhs)
34{
35 if (this != &rhs)
36 m_opaque_sp = rhs.m_opaque_sp;
37 return *this;
38}
39
40
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041SBInstructionList::~SBInstructionList ()
42{
43}
44
Johnny Chendd68ab82011-06-20 22:30:48 +000045bool
46SBInstructionList::IsValid () const
47{
48 return m_opaque_sp.get() != NULL;
49}
50
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051size_t
52SBInstructionList::GetSize ()
53{
Greg Clayton1d273162010-10-06 03:09:58 +000054 if (m_opaque_sp)
55 return m_opaque_sp->GetInstructionList().GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056 return 0;
57}
58
59SBInstruction
60SBInstructionList::GetInstructionAtIndex (uint32_t idx)
61{
62 SBInstruction inst;
Greg Clayton1d273162010-10-06 03:09:58 +000063 if (m_opaque_sp && idx < m_opaque_sp->GetInstructionList().GetSize())
64 inst.SetOpaque (m_opaque_sp->GetInstructionList().GetInstructionAtIndex (idx));
Chris Lattner30fdc8d2010-06-08 16:52:24 +000065 return inst;
66}
67
68void
69SBInstructionList::Clear ()
70{
Greg Clayton1d273162010-10-06 03:09:58 +000071 m_opaque_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072}
73
74void
75SBInstructionList::AppendInstruction (SBInstruction insn)
76{
77}
78
79void
Greg Clayton1d273162010-10-06 03:09:58 +000080SBInstructionList::SetDisassembler (const lldb::DisassemblerSP &opaque_sp)
81{
82 m_opaque_sp = opaque_sp;
83}
84
85void
Chris Lattner30fdc8d2010-06-08 16:52:24 +000086SBInstructionList::Print (FILE *out)
87{
88 if (out == NULL)
89 return;
90}
91
Greg Clayton1d273162010-10-06 03:09:58 +000092
93bool
94SBInstructionList::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();
Greg Clayton357132e2011-03-26 19:14:58 +0000104 const uint32_t max_opcode_byte_size = m_opaque_sp->GetInstructionList().GetMaxOpcocdeByteSize();
Jason Molendaaff1b352014-10-10 23:07:36 +0000105 const char *disassemble_format = "${addr-file-or-load}: ";
106 SymbolContext sc;
107 SymbolContext prev_sc;
Greg Clayton1d273162010-10-06 03:09:58 +0000108 for (size_t i=0; i<num_instructions; ++i)
109 {
110 Instruction *inst = m_opaque_sp->GetInstructionList().GetInstructionAtIndex (i).get();
111 if (inst == NULL)
112 break;
Jason Molendaaff1b352014-10-10 23:07:36 +0000113
114 const Address &addr = inst->GetAddress();
115 prev_sc = sc;
116 ModuleSP module_sp (addr.GetModule());
117 if (module_sp)
118 {
119 module_sp->ResolveSymbolContextForAddress(addr, eSymbolContextEverything, sc);
120 }
121
122 inst->Dump (&sref, max_opcode_byte_size, true, false, NULL, &sc, &prev_sc, disassemble_format);
Greg Clayton1d273162010-10-06 03:09:58 +0000123 sref.EOL();
124 }
125 return true;
126 }
127 }
128 return false;
129}
130
131
Caroline Tice7c9dd3c2011-04-05 23:22:54 +0000132bool
133SBInstructionList::DumpEmulationForAllInstructions (const char *triple)
134{
135 if (m_opaque_sp)
136 {
137 size_t len = GetSize();
138 for (size_t i = 0; i < len; ++i)
139 {
140 if (!GetInstructionAtIndex((uint32_t) i).DumpEmulation (triple))
141 return false;
142 }
143 }
144 return true;
145}
146