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" |
Jason Molenda | aff1b35 | 2014-10-10 23:07:36 +0000 | [diff] [blame] | 15 | #include "lldb/Symbol/SymbolContext.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 16 | #include "lldb/Utility/Stream.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 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 21 | SBInstructionList::SBInstructionList() : m_opaque_sp() {} |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 22 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 23 | SBInstructionList::SBInstructionList(const SBInstructionList &rhs) |
| 24 | : m_opaque_sp(rhs.m_opaque_sp) {} |
| 25 | |
| 26 | const SBInstructionList &SBInstructionList:: |
| 27 | operator=(const SBInstructionList &rhs) { |
| 28 | if (this != &rhs) |
| 29 | m_opaque_sp = rhs.m_opaque_sp; |
| 30 | return *this; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 33 | SBInstructionList::~SBInstructionList() {} |
| 34 | |
| 35 | bool SBInstructionList::IsValid() const { return m_opaque_sp.get() != NULL; } |
| 36 | |
| 37 | size_t SBInstructionList::GetSize() { |
| 38 | if (m_opaque_sp) |
| 39 | return m_opaque_sp->GetInstructionList().GetSize(); |
| 40 | return 0; |
Greg Clayton | efabb12 | 2010-11-05 23:17:00 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 43 | SBInstruction 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 Clayton | efabb12 | 2010-11-05 23:17:00 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 52 | void SBInstructionList::Clear() { m_opaque_sp.reset(); } |
Greg Clayton | efabb12 | 2010-11-05 23:17:00 +0000 | [diff] [blame] | 53 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 54 | void SBInstructionList::AppendInstruction(SBInstruction insn) {} |
| 55 | |
| 56 | void SBInstructionList::SetDisassembler(const lldb::DisassemblerSP &opaque_sp) { |
| 57 | m_opaque_sp = opaque_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 60 | void SBInstructionList::Print(FILE *out) { |
| 61 | if (out == NULL) |
| 62 | return; |
Johnny Chen | dd68ab8 | 2011-06-20 22:30:48 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 65 | bool 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 Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 83 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 84 | 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 Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 90 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 91 | |
| 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 Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 97 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 98 | } |
| 99 | return false; |
Greg Clayton | 1d27316 | 2010-10-06 03:09:58 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 102 | bool 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 Tice | 7c9dd3c | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 108 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 109 | } |
| 110 | return true; |
Caroline Tice | 7c9dd3c | 2011-04-05 23:22:54 +0000 | [diff] [blame] | 111 | } |