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