blob: 04c37f50c2d72e1c5b758f779487fe64542717b4 [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"
Jason Molendaaff1b352014-10-10 23:07:36 +000015#include "lldb/Symbol/SymbolContext.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000016#include "lldb/Utility/Stream.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
Kate Stoneb9c1b512016-09-06 20:57:50 +000021SBInstructionList::SBInstructionList() : m_opaque_sp() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022
Kate Stoneb9c1b512016-09-06 20:57:50 +000023SBInstructionList::SBInstructionList(const SBInstructionList &rhs)
24 : m_opaque_sp(rhs.m_opaque_sp) {}
25
26const SBInstructionList &SBInstructionList::
27operator=(const SBInstructionList &rhs) {
28 if (this != &rhs)
29 m_opaque_sp = rhs.m_opaque_sp;
30 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031}
32
Kate Stoneb9c1b512016-09-06 20:57:50 +000033SBInstructionList::~SBInstructionList() {}
34
35bool SBInstructionList::IsValid() const { return m_opaque_sp.get() != NULL; }
36
37size_t SBInstructionList::GetSize() {
38 if (m_opaque_sp)
39 return m_opaque_sp->GetInstructionList().GetSize();
40 return 0;
Greg Claytonefabb122010-11-05 23:17:00 +000041}
42
Kate Stoneb9c1b512016-09-06 20:57:50 +000043SBInstruction SBInstructionList::GetInstructionAtIndex(uint32_t idx) {
44 SBInstruction inst;
45 if (m_opaque_sp && idx < m_opaque_sp->GetInstructionList().GetSize())
46 inst.SetOpaque(
47 m_opaque_sp,
48 m_opaque_sp->GetInstructionList().GetInstructionAtIndex(idx));
49 return inst;
Greg Claytonefabb122010-11-05 23:17:00 +000050}
51
Kate Stoneb9c1b512016-09-06 20:57:50 +000052void SBInstructionList::Clear() { m_opaque_sp.reset(); }
Greg Claytonefabb122010-11-05 23:17:00 +000053
Kate Stoneb9c1b512016-09-06 20:57:50 +000054void SBInstructionList::AppendInstruction(SBInstruction insn) {}
55
56void SBInstructionList::SetDisassembler(const lldb::DisassemblerSP &opaque_sp) {
57 m_opaque_sp = opaque_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058}
59
Kate Stoneb9c1b512016-09-06 20:57:50 +000060void SBInstructionList::Print(FILE *out) {
61 if (out == NULL)
62 return;
Johnny Chendd68ab82011-06-20 22:30:48 +000063}
64
Kate Stoneb9c1b512016-09-06 20:57:50 +000065bool SBInstructionList::GetDescription(lldb::SBStream &description) {
66 if (m_opaque_sp) {
67 size_t num_instructions = GetSize();
68 if (num_instructions) {
69 // Call the ref() to make sure a stream is created if one deesn't
70 // exist already inside description...
71 Stream &sref = description.ref();
72 const uint32_t max_opcode_byte_size =
73 m_opaque_sp->GetInstructionList().GetMaxOpcocdeByteSize();
74 FormatEntity::Entry format;
75 FormatEntity::Parse("${addr}: ", format);
76 SymbolContext sc;
77 SymbolContext prev_sc;
78 for (size_t i = 0; i < num_instructions; ++i) {
79 Instruction *inst =
80 m_opaque_sp->GetInstructionList().GetInstructionAtIndex(i).get();
81 if (inst == NULL)
82 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000083
Kate Stoneb9c1b512016-09-06 20:57:50 +000084 const Address &addr = inst->GetAddress();
85 prev_sc = sc;
86 ModuleSP module_sp(addr.GetModule());
87 if (module_sp) {
88 module_sp->ResolveSymbolContextForAddress(
89 addr, eSymbolContextEverything, sc);
Greg Clayton1d273162010-10-06 03:09:58 +000090 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000091
92 inst->Dump(&sref, max_opcode_byte_size, true, false, NULL, &sc,
93 &prev_sc, &format, 0);
94 sref.EOL();
95 }
96 return true;
Greg Clayton1d273162010-10-06 03:09:58 +000097 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000098 }
99 return false;
Greg Clayton1d273162010-10-06 03:09:58 +0000100}
101
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102bool SBInstructionList::DumpEmulationForAllInstructions(const char *triple) {
103 if (m_opaque_sp) {
104 size_t len = GetSize();
105 for (size_t i = 0; i < len; ++i) {
106 if (!GetInstructionAtIndex((uint32_t)i).DumpEmulation(triple))
107 return false;
Caroline Tice7c9dd3c2011-04-05 23:22:54 +0000108 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109 }
110 return true;
Caroline Tice7c9dd3c2011-04-05 23:22:54 +0000111}