Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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" |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 12 | #include "lldb/API/SBStream.h" |
| 13 | #include "lldb/Core/Disassembler.h" |
Jason Molenda | aff1b35 | 2014-10-10 23:07:36 +0000 | [diff] [blame] | 14 | #include "lldb/Core/Module.h" |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 15 | #include "lldb/Core/Stream.h" |
Jason Molenda | aff1b35 | 2014-10-10 23:07:36 +0000 | [diff] [blame] | 16 | #include "lldb/Symbol/SymbolContext.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 17 | |
| 18 | using namespace lldb; |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 19 | using namespace lldb_private; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 20 | |
| 21 | |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 22 | SBInstructionList::SBInstructionList () : |
| 23 | m_opaque_sp() |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 24 | { |
| 25 | } |
| 26 | |
Greg Clayton | efabb12 | 2010-11-05 23:17:00 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 41 | SBInstructionList::~SBInstructionList () |
| 42 | { |
| 43 | } |
| 44 | |
Johnny Chen | dd68ab8 | 2011-06-20 22:30:48 +0000 | [diff] [blame] | 45 | bool |
| 46 | SBInstructionList::IsValid () const |
| 47 | { |
| 48 | return m_opaque_sp.get() != NULL; |
| 49 | } |
| 50 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 51 | size_t |
| 52 | SBInstructionList::GetSize () |
| 53 | { |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 54 | if (m_opaque_sp) |
| 55 | return m_opaque_sp->GetInstructionList().GetSize(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | SBInstruction |
| 60 | SBInstructionList::GetInstructionAtIndex (uint32_t idx) |
| 61 | { |
| 62 | SBInstruction inst; |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 63 | if (m_opaque_sp && idx < m_opaque_sp->GetInstructionList().GetSize()) |
| 64 | inst.SetOpaque (m_opaque_sp->GetInstructionList().GetInstructionAtIndex (idx)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 65 | return inst; |
| 66 | } |
| 67 | |
| 68 | void |
| 69 | SBInstructionList::Clear () |
| 70 | { |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 71 | m_opaque_sp.reset(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void |
| 75 | SBInstructionList::AppendInstruction (SBInstruction insn) |
| 76 | { |
| 77 | } |
| 78 | |
| 79 | void |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 80 | SBInstructionList::SetDisassembler (const lldb::DisassemblerSP &opaque_sp) |
| 81 | { |
| 82 | m_opaque_sp = opaque_sp; |
| 83 | } |
| 84 | |
| 85 | void |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 86 | SBInstructionList::Print (FILE *out) |
| 87 | { |
| 88 | if (out == NULL) |
| 89 | return; |
| 90 | } |
| 91 | |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 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(); |
Greg Clayton | 357132e | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 104 | const uint32_t max_opcode_byte_size = m_opaque_sp->GetInstructionList().GetMaxOpcocdeByteSize(); |
Jason Molenda | aff1b35 | 2014-10-10 23:07:36 +0000 | [diff] [blame] | 105 | const char *disassemble_format = "${addr-file-or-load}: "; |
| 106 | SymbolContext sc; |
| 107 | SymbolContext prev_sc; |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 108 | 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 Molenda | aff1b35 | 2014-10-10 23:07:36 +0000 | [diff] [blame] | 113 | |
| 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 Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 123 | sref.EOL(); |
| 124 | } |
| 125 | return true; |
| 126 | } |
| 127 | } |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | |
Caroline Tice | 7c9dd3c | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 132 | bool |
| 133 | SBInstructionList::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 | |