1 //===-- DecodedThread.cpp -------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "DecodedThread.h" 10 11 #include <intel-pt.h> 12 #include <memory> 13 14 #include "TraceCursorIntelPT.h" 15 #include "lldb/Utility/StreamString.h" 16 17 using namespace lldb; 18 using namespace lldb_private; 19 using namespace lldb_private::trace_intel_pt; 20 using namespace llvm; 21 22 char IntelPTError::ID; 23 24 IntelPTError::IntelPTError(int libipt_error_code, lldb::addr_t address) 25 : m_libipt_error_code(libipt_error_code), m_address(address) { 26 assert(libipt_error_code < 0); 27 } 28 29 void IntelPTError::log(llvm::raw_ostream &OS) const { 30 const char *libipt_error_message = pt_errstr(pt_errcode(m_libipt_error_code)); 31 if (m_address != LLDB_INVALID_ADDRESS && m_address > 0) { 32 write_hex(OS, m_address, HexPrintStyle::PrefixLower, 18); 33 OS << " "; 34 } 35 OS << "error: " << libipt_error_message; 36 } 37 38 IntelPTInstruction::IntelPTInstruction(llvm::Error err) { 39 llvm::handleAllErrors(std::move(err), 40 [&](std::unique_ptr<llvm::ErrorInfoBase> info) { 41 m_error = std::move(info); 42 }); 43 m_pt_insn.ip = LLDB_INVALID_ADDRESS; 44 m_pt_insn.iclass = ptic_error; 45 } 46 47 bool IntelPTInstruction::IsError() const { return (bool)m_error; } 48 49 lldb::addr_t IntelPTInstruction::GetLoadAddress() const { return m_pt_insn.ip; } 50 51 Optional<uint64_t> IntelPTInstruction::GetTimestampCounter() const { 52 return m_timestamp; 53 } 54 55 Error IntelPTInstruction::ToError() const { 56 if (!IsError()) 57 return Error::success(); 58 59 if (m_error->isA<IntelPTError>()) 60 return make_error<IntelPTError>(static_cast<IntelPTError &>(*m_error)); 61 return make_error<StringError>(m_error->message(), 62 m_error->convertToErrorCode()); 63 } 64 65 TraceInstructionControlFlowType 66 IntelPTInstruction::GetControlFlowType(lldb::addr_t next_load_address) const { 67 if (IsError()) 68 return (TraceInstructionControlFlowType)0; 69 70 TraceInstructionControlFlowType mask = 71 eTraceInstructionControlFlowTypeInstruction; 72 73 switch (m_pt_insn.iclass) { 74 case ptic_cond_jump: 75 case ptic_jump: 76 case ptic_far_jump: 77 mask |= eTraceInstructionControlFlowTypeBranch; 78 if (m_pt_insn.ip + m_pt_insn.size != next_load_address) 79 mask |= eTraceInstructionControlFlowTypeTakenBranch; 80 break; 81 case ptic_return: 82 case ptic_far_return: 83 mask |= eTraceInstructionControlFlowTypeReturn; 84 break; 85 case ptic_call: 86 case ptic_far_call: 87 mask |= eTraceInstructionControlFlowTypeCall; 88 break; 89 default: 90 break; 91 } 92 93 return mask; 94 } 95 96 ArrayRef<IntelPTInstruction> DecodedThread::GetInstructions() const { 97 return makeArrayRef(m_instructions); 98 } 99 100 DecodedThread::DecodedThread(ThreadSP thread_sp, Error error) 101 : m_thread_sp(thread_sp) { 102 m_instructions.emplace_back(std::move(error)); 103 } 104 105 DecodedThread::DecodedThread(ThreadSP thread_sp, 106 std::vector<IntelPTInstruction> &&instructions) 107 : m_thread_sp(thread_sp), m_instructions(std::move(instructions)) { 108 if (m_instructions.empty()) 109 m_instructions.emplace_back( 110 createStringError(inconvertibleErrorCode(), "empty trace")); 111 } 112 113 lldb::TraceCursorUP DecodedThread::GetCursor() { 114 return std::make_unique<TraceCursorIntelPT>(m_thread_sp, shared_from_this()); 115 } 116