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 13 #include "TraceCursorIntelPT.h" 14 15 #include <memory> 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 size_t DecodedThread::GetInstructionsCount() const { 39 return m_instruction_ips.size(); 40 } 41 42 lldb::addr_t DecodedThread::GetInstructionLoadAddress(size_t insn_index) const { 43 return m_instruction_ips[insn_index]; 44 } 45 46 TraceInstructionControlFlowType 47 DecodedThread::GetInstructionControlFlowType(size_t insn_index) const { 48 if (IsInstructionAnError(insn_index)) 49 return (TraceInstructionControlFlowType)0; 50 51 TraceInstructionControlFlowType mask = 52 eTraceInstructionControlFlowTypeInstruction; 53 54 lldb::addr_t load_address = m_instruction_ips[insn_index]; 55 uint8_t insn_byte_size = m_instruction_sizes[insn_index]; 56 pt_insn_class iclass = m_instruction_classes[insn_index]; 57 58 switch (iclass) { 59 case ptic_cond_jump: 60 case ptic_jump: 61 case ptic_far_jump: 62 mask |= eTraceInstructionControlFlowTypeBranch; 63 if (insn_index + 1 < m_instruction_ips.size() && 64 load_address + insn_byte_size != m_instruction_ips[insn_index + 1]) 65 mask |= eTraceInstructionControlFlowTypeTakenBranch; 66 break; 67 case ptic_return: 68 case ptic_far_return: 69 mask |= eTraceInstructionControlFlowTypeReturn; 70 break; 71 case ptic_call: 72 case ptic_far_call: 73 mask |= eTraceInstructionControlFlowTypeCall; 74 break; 75 default: 76 break; 77 } 78 79 return mask; 80 } 81 82 ThreadSP DecodedThread::GetThread() { return m_thread_sp; } 83 84 void DecodedThread::RecordTscForLastInstruction(uint64_t tsc) { 85 if (!m_last_tsc || *m_last_tsc != tsc) { 86 // In case the first instructions are errors or did not have a TSC, we'll 87 // get a first valid TSC not in position 0. We can safely force these error 88 // instructions to use the first valid TSC, so that all the trace has TSCs. 89 size_t start_index = 90 m_instruction_timestamps.empty() ? 0 : m_instruction_ips.size() - 1; 91 m_instruction_timestamps.emplace(start_index, tsc); 92 m_last_tsc = tsc; 93 } 94 } 95 96 void DecodedThread::AppendInstruction(const pt_insn &insn) { 97 m_instruction_ips.emplace_back(insn.ip); 98 m_instruction_sizes.emplace_back(insn.size); 99 m_instruction_classes.emplace_back(insn.iclass); 100 } 101 102 void DecodedThread::AppendInstruction(const pt_insn &insn, uint64_t tsc) { 103 AppendInstruction(insn); 104 RecordTscForLastInstruction(tsc); 105 } 106 107 void DecodedThread::AppendError(llvm::Error &&error) { 108 m_errors.try_emplace(m_instruction_ips.size(), toString(std::move(error))); 109 m_instruction_ips.emplace_back(LLDB_INVALID_ADDRESS); 110 m_instruction_sizes.emplace_back(0); 111 m_instruction_classes.emplace_back(pt_insn_class::ptic_error); 112 } 113 114 void DecodedThread::AppendError(llvm::Error &&error, uint64_t tsc) { 115 AppendError(std::move(error)); 116 RecordTscForLastInstruction(tsc); 117 } 118 119 void DecodedThread::LibiptErrors::RecordError(int libipt_error_code) { 120 libipt_errors[pt_errstr(pt_errcode(libipt_error_code))]++; 121 total_count++; 122 } 123 124 void DecodedThread::RecordTscError(int libipt_error_code) { 125 m_tsc_errors.RecordError(libipt_error_code); 126 } 127 128 const DecodedThread::LibiptErrors &DecodedThread::GetTscErrors() const { 129 return m_tsc_errors; 130 } 131 132 Optional<DecodedThread::TscRange> DecodedThread::CalculateTscRange( 133 size_t insn_index, 134 const Optional<DecodedThread::TscRange> &hint_range) const { 135 // We first try to check the given hint range in case we are traversing the 136 // trace in short jumps. If that fails, then we do the more expensive 137 // arbitrary lookup. 138 if (hint_range) { 139 Optional<TscRange> candidate_range; 140 if (insn_index < hint_range->GetStartInstructionIndex()) 141 candidate_range = hint_range->Prev(); 142 else if (insn_index > hint_range->GetEndInstructionIndex()) 143 candidate_range = hint_range->Next(); 144 else 145 candidate_range = hint_range; 146 147 if (candidate_range && candidate_range->InRange(insn_index)) 148 return candidate_range; 149 } 150 // Now we do a more expensive lookup 151 auto it = m_instruction_timestamps.upper_bound(insn_index); 152 if (it == m_instruction_timestamps.begin()) 153 return None; 154 155 return TscRange(--it, *this); 156 } 157 158 bool DecodedThread::IsInstructionAnError(size_t insn_idx) const { 159 return m_instruction_ips[insn_idx] == LLDB_INVALID_ADDRESS; 160 } 161 162 const char *DecodedThread::GetErrorByInstructionIndex(size_t insn_idx) { 163 auto it = m_errors.find(insn_idx); 164 if (it == m_errors.end()) 165 return nullptr; 166 167 return it->second.c_str(); 168 } 169 170 DecodedThread::DecodedThread(ThreadSP thread_sp) : m_thread_sp(thread_sp) {} 171 172 DecodedThread::DecodedThread(ThreadSP thread_sp, Error &&error) 173 : m_thread_sp(thread_sp) { 174 AppendError(std::move(error)); 175 } 176 177 lldb::TraceCursorUP DecodedThread::GetCursor() { 178 // We insert a fake error signaling an empty trace if needed becasue the 179 // TraceCursor requires non-empty traces. 180 if (m_instruction_ips.empty()) 181 AppendError(createStringError(inconvertibleErrorCode(), "empty trace")); 182 return std::make_unique<TraceCursorIntelPT>(m_thread_sp, shared_from_this()); 183 } 184 185 size_t DecodedThread::CalculateApproximateMemoryUsage() const { 186 return sizeof(pt_insn::ip) * m_instruction_ips.size() + 187 sizeof(pt_insn::size) * m_instruction_sizes.size() + 188 sizeof(pt_insn::iclass) * m_instruction_classes.size() + 189 (sizeof(size_t) + sizeof(uint64_t)) * m_instruction_timestamps.size() + 190 m_errors.getMemorySize(); 191 } 192 193 DecodedThread::TscRange::TscRange(std::map<size_t, uint64_t>::const_iterator it, 194 const DecodedThread &decoded_thread) 195 : m_it(it), m_decoded_thread(&decoded_thread) { 196 auto next_it = m_it; 197 ++next_it; 198 m_end_index = (next_it == m_decoded_thread->m_instruction_timestamps.end()) 199 ? m_decoded_thread->GetInstructionsCount() - 1 200 : next_it->first - 1; 201 } 202 203 size_t DecodedThread::TscRange::GetTsc() const { return m_it->second; } 204 205 size_t DecodedThread::TscRange::GetStartInstructionIndex() const { 206 return m_it->first; 207 } 208 209 size_t DecodedThread::TscRange::GetEndInstructionIndex() const { 210 return m_end_index; 211 } 212 213 bool DecodedThread::TscRange::InRange(size_t insn_index) const { 214 return GetStartInstructionIndex() <= insn_index && 215 insn_index <= GetEndInstructionIndex(); 216 } 217 218 Optional<DecodedThread::TscRange> DecodedThread::TscRange::Next() const { 219 auto next_it = m_it; 220 ++next_it; 221 if (next_it == m_decoded_thread->m_instruction_timestamps.end()) 222 return None; 223 return TscRange(next_it, *m_decoded_thread); 224 } 225 226 Optional<DecodedThread::TscRange> DecodedThread::TscRange::Prev() const { 227 if (m_it == m_decoded_thread->m_instruction_timestamps.begin()) 228 return None; 229 auto prev_it = m_it; 230 --prev_it; 231 return TscRange(prev_it, *m_decoded_thread); 232 } 233