xref: /llvm-project/lldb/source/Plugins/Trace/intel-pt/LibiptDecoder.cpp (revision f190ce625ab0dc5a5e2b2515e6d26debb34843ab)
1 //===-- LibiptDecoder.cpp --======-----------------------------------------===//
2 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3 // See https://llvm.org/LICENSE.txt for license information.
4 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5 //
6 //===----------------------------------------------------------------------===//
7 
8 #include "LibiptDecoder.h"
9 #include "TraceIntelPT.h"
10 #include "lldb/Target/Process.h"
11 #include <optional>
12 
13 using namespace lldb;
14 using namespace lldb_private;
15 using namespace lldb_private::trace_intel_pt;
16 using namespace llvm;
17 
18 bool IsLibiptError(int status) { return status < 0; }
19 
20 bool IsEndOfStream(int status) {
21   assert(status >= 0 && "We can't check if we reached the end of the stream if "
22                         "we got a failed status");
23   return status & pts_eos;
24 }
25 
26 bool HasEvents(int status) {
27   assert(status >= 0 && "We can't check for events if we got a failed status");
28   return status & pts_event_pending;
29 }
30 
31 // RAII deleter for libipt's decoders
32 auto InsnDecoderDeleter = [](pt_insn_decoder *decoder) {
33   pt_insn_free_decoder(decoder);
34 };
35 
36 auto QueryDecoderDeleter = [](pt_query_decoder *decoder) {
37   pt_qry_free_decoder(decoder);
38 };
39 
40 using PtInsnDecoderUP =
41     std::unique_ptr<pt_insn_decoder, decltype(InsnDecoderDeleter)>;
42 
43 using PtQueryDecoderUP =
44     std::unique_ptr<pt_query_decoder, decltype(QueryDecoderDeleter)>;
45 
46 /// Create a basic configuration object limited to a given buffer that can be
47 /// used for many different decoders.
48 static Expected<pt_config> CreateBasicLibiptConfig(TraceIntelPT &trace_intel_pt,
49                                                    ArrayRef<uint8_t> buffer) {
50   Expected<pt_cpu> cpu_info = trace_intel_pt.GetCPUInfo();
51   if (!cpu_info)
52     return cpu_info.takeError();
53 
54   pt_config config;
55   pt_config_init(&config);
56   config.cpu = *cpu_info;
57 
58   int status = pt_cpu_errata(&config.errata, &config.cpu);
59   if (IsLibiptError(status))
60     return make_error<IntelPTError>(status);
61 
62   // The libipt library does not modify the trace buffer, hence the
63   // following casts are safe.
64   config.begin = const_cast<uint8_t *>(buffer.data());
65   config.end = const_cast<uint8_t *>(buffer.data() + buffer.size());
66   return config;
67 }
68 
69 /// Callback used by libipt for reading the process memory.
70 ///
71 /// More information can be found in
72 /// https://github.com/intel/libipt/blob/master/doc/man/pt_image_set_callback.3.md
73 static int ReadProcessMemory(uint8_t *buffer, size_t size,
74                              const pt_asid * /* unused */, uint64_t pc,
75                              void *context) {
76   Process *process = static_cast<Process *>(context);
77 
78   Status error;
79   int bytes_read = process->ReadMemory(pc, buffer, size, error);
80   if (error.Fail())
81     return -pte_nomap;
82   return bytes_read;
83 }
84 
85 /// Set up the memory image callback for the given decoder.
86 static Error SetupMemoryImage(pt_insn_decoder *decoder, Process &process) {
87   pt_image *image = pt_insn_get_image(decoder);
88 
89   int status = pt_image_set_callback(image, ReadProcessMemory, &process);
90   if (IsLibiptError(status))
91     return make_error<IntelPTError>(status);
92   return Error::success();
93 }
94 
95 /// Create an instruction decoder for the given buffer and the given process.
96 static Expected<PtInsnDecoderUP>
97 CreateInstructionDecoder(TraceIntelPT &trace_intel_pt, ArrayRef<uint8_t> buffer,
98                          Process &process) {
99   Expected<pt_config> config = CreateBasicLibiptConfig(trace_intel_pt, buffer);
100   if (!config)
101     return config.takeError();
102 
103   pt_insn_decoder *decoder_ptr = pt_insn_alloc_decoder(&*config);
104   if (!decoder_ptr)
105     return make_error<IntelPTError>(-pte_nomem);
106 
107   PtInsnDecoderUP decoder_up(decoder_ptr, InsnDecoderDeleter);
108 
109   if (Error err = SetupMemoryImage(decoder_ptr, process))
110     return std::move(err);
111 
112   return decoder_up;
113 }
114 
115 /// Create a query decoder for the given buffer. The query decoder is the
116 /// highest level decoder that operates directly on packets and doesn't perform
117 /// actual instruction decoding. That's why it can be useful for inspecting a
118 /// raw trace without pinning it to a particular process.
119 static Expected<PtQueryDecoderUP>
120 CreateQueryDecoder(TraceIntelPT &trace_intel_pt, ArrayRef<uint8_t> buffer) {
121   Expected<pt_config> config = CreateBasicLibiptConfig(trace_intel_pt, buffer);
122   if (!config)
123     return config.takeError();
124 
125   pt_query_decoder *decoder_ptr = pt_qry_alloc_decoder(&*config);
126   if (!decoder_ptr)
127     return make_error<IntelPTError>(-pte_nomem);
128 
129   return PtQueryDecoderUP(decoder_ptr, QueryDecoderDeleter);
130 }
131 
132 /// Class used to identify anomalies in traces, which should often indicate a
133 /// fatal error in the trace.
134 class PSBBlockAnomalyDetector {
135 public:
136   PSBBlockAnomalyDetector(pt_insn_decoder &decoder,
137                           TraceIntelPT &trace_intel_pt,
138                           DecodedThread &decoded_thread)
139       : m_decoder(decoder), m_decoded_thread(decoded_thread) {
140     m_infinite_decoding_loop_threshold =
141         trace_intel_pt.GetGlobalProperties()
142             .GetInfiniteDecodingLoopVerificationThreshold();
143     m_extremely_large_decoding_threshold =
144         trace_intel_pt.GetGlobalProperties()
145             .GetExtremelyLargeDecodingThreshold();
146     m_next_infinite_decoding_loop_threshold =
147         m_infinite_decoding_loop_threshold;
148   }
149 
150   /// \return
151   ///   An \a llvm::Error if an anomaly that includes the last instruction item
152   ///   in the trace, or \a llvm::Error::success otherwise.
153   Error DetectAnomaly() {
154     RefreshPacketOffset();
155     uint64_t insn_added_since_last_packet_offset =
156         m_decoded_thread.GetTotalInstructionCount() -
157         m_insn_count_at_last_packet_offset;
158 
159     // We want to check if we might have fallen in an infinite loop. As this
160     // check is not a no-op, we want to do it when we have a strong suggestion
161     // that things went wrong. First, we check how many instructions we have
162     // decoded since we processed an Intel PT packet for the last time. This
163     // number should be low, because at some point we should see branches, jumps
164     // or interrupts that require a new packet to be processed. Once we reach
165     // certain threshold we start analyzing the trace.
166     //
167     // We use the number of decoded instructions since the last Intel PT packet
168     // as a proxy because, in fact, we don't expect a single packet to give,
169     // say, 100k instructions. That would mean that there are 100k sequential
170     // instructions without any single branch, which is highly unlikely, or that
171     // we found an infinite loop using direct jumps, e.g.
172     //
173     //   0x0A: nop or pause
174     //   0x0C: jump to 0x0A
175     //
176     // which is indeed code that is found in the kernel. I presume we reach
177     // this kind of code in the decoder because we don't handle self-modified
178     // code in post-mortem kernel traces.
179     //
180     // We are right now only signaling the anomaly as a trace error, but it
181     // would be more conservative to also discard all the trace items found in
182     // this PSB. I prefer not to do that for the time being to give more
183     // exposure to this kind of anomalies and help debugging. Discarding the
184     // trace items would just make investigation harded.
185     //
186     // Finally, if the user wants to see if a specific thread has an anomaly,
187     // it's enough to run the `thread trace dump info` command and look for the
188     // count of this kind of errors.
189 
190     if (insn_added_since_last_packet_offset >=
191         m_extremely_large_decoding_threshold) {
192       // In this case, we have decoded a massive amount of sequential
193       // instructions that don't loop. Honestly I wonder if this will ever
194       // happen, but better safe than sorry.
195       return createStringError(
196           inconvertibleErrorCode(),
197           "anomalous trace: possible infinite trace detected");
198     }
199     if (insn_added_since_last_packet_offset ==
200         m_next_infinite_decoding_loop_threshold) {
201       if (Optional<uint64_t> loop_size = TryIdentifyInfiniteLoop()) {
202         return createStringError(
203             inconvertibleErrorCode(),
204             "anomalous trace: possible infinite loop detected of size %" PRIu64,
205             *loop_size);
206       }
207       m_next_infinite_decoding_loop_threshold *= 2;
208     }
209     return Error::success();
210   }
211 
212 private:
213   Optional<uint64_t> TryIdentifyInfiniteLoop() {
214     // The infinite decoding loops we'll encounter are due to sequential
215     // instructions that repeat themselves due to direct jumps, therefore in a
216     // cycle each individual address will only appear once. We use this
217     // information to detect cycles by finding the last 2 ocurrences of the last
218     // instruction added to the trace. Then we traverse the trace making sure
219     // that these two instructions where the ends of a repeating loop.
220 
221     // This is a utility that returns the most recent instruction index given a
222     // position in the trace. If the given position is an instruction, that
223     // position is returned. It skips non-instruction items.
224     auto most_recent_insn_index =
225         [&](uint64_t item_index) -> Optional<uint64_t> {
226       while (true) {
227         if (m_decoded_thread.GetItemKindByIndex(item_index) ==
228             lldb::eTraceItemKindInstruction) {
229           return item_index;
230         }
231         if (item_index == 0)
232           return std::nullopt;
233         item_index--;
234       }
235       return std::nullopt;
236     };
237     // Similar to most_recent_insn_index but skips the starting position.
238     auto prev_insn_index = [&](uint64_t item_index) -> Optional<uint64_t> {
239       if (item_index == 0)
240         return std::nullopt;
241       return most_recent_insn_index(item_index - 1);
242     };
243 
244     // We first find the most recent instruction.
245     Optional<uint64_t> last_insn_index_opt =
246         *prev_insn_index(m_decoded_thread.GetItemsCount());
247     if (!last_insn_index_opt)
248       return std::nullopt;
249     uint64_t last_insn_index = *last_insn_index_opt;
250 
251     // We then find the most recent previous occurrence of that last
252     // instruction.
253     Optional<uint64_t> last_insn_copy_index = prev_insn_index(last_insn_index);
254     uint64_t loop_size = 1;
255     while (last_insn_copy_index &&
256            m_decoded_thread.GetInstructionLoadAddress(*last_insn_copy_index) !=
257                m_decoded_thread.GetInstructionLoadAddress(last_insn_index)) {
258       last_insn_copy_index = prev_insn_index(*last_insn_copy_index);
259       loop_size++;
260     }
261     if (!last_insn_copy_index)
262       return std::nullopt;
263 
264     // Now we check if the segment between these last positions of the last
265     // instruction address is in fact a repeating loop.
266     uint64_t loop_elements_visited = 1;
267     uint64_t insn_index_a = last_insn_index,
268              insn_index_b = *last_insn_copy_index;
269     while (loop_elements_visited < loop_size) {
270       if (Optional<uint64_t> prev = prev_insn_index(insn_index_a))
271         insn_index_a = *prev;
272       else
273         return std::nullopt;
274       if (Optional<uint64_t> prev = prev_insn_index(insn_index_b))
275         insn_index_b = *prev;
276       else
277         return std::nullopt;
278       if (m_decoded_thread.GetInstructionLoadAddress(insn_index_a) !=
279           m_decoded_thread.GetInstructionLoadAddress(insn_index_b))
280         return std::nullopt;
281       loop_elements_visited++;
282     }
283     return loop_size;
284   }
285 
286   // Refresh the internal counters if a new packet offset has been visited
287   void RefreshPacketOffset() {
288     lldb::addr_t new_packet_offset;
289     if (!IsLibiptError(pt_insn_get_offset(&m_decoder, &new_packet_offset)) &&
290         new_packet_offset != m_last_packet_offset) {
291       m_last_packet_offset = new_packet_offset;
292       m_next_infinite_decoding_loop_threshold =
293           m_infinite_decoding_loop_threshold;
294       m_insn_count_at_last_packet_offset =
295           m_decoded_thread.GetTotalInstructionCount();
296     }
297   }
298 
299   pt_insn_decoder &m_decoder;
300   DecodedThread &m_decoded_thread;
301   lldb::addr_t m_last_packet_offset = LLDB_INVALID_ADDRESS;
302   uint64_t m_insn_count_at_last_packet_offset = 0;
303   uint64_t m_infinite_decoding_loop_threshold;
304   uint64_t m_next_infinite_decoding_loop_threshold;
305   uint64_t m_extremely_large_decoding_threshold;
306 };
307 
308 /// Class that decodes a raw buffer for a single PSB block using the low level
309 /// libipt library. It assumes that kernel and user mode instructions are not
310 /// mixed in the same PSB block.
311 ///
312 /// Throughout this code, the status of the decoder will be used to identify
313 /// events needed to be processed or errors in the decoder. The values can be
314 /// - negative: actual errors
315 /// - positive or zero: not an error, but a list of bits signaling the status
316 /// of the decoder, e.g. whether there are events that need to be decoded or
317 /// not.
318 class PSBBlockDecoder {
319 public:
320   /// \param[in] decoder
321   ///     A decoder configured to start and end within the boundaries of the
322   ///     given \p psb_block.
323   ///
324   /// \param[in] psb_block
325   ///     The PSB block to decode.
326   ///
327   /// \param[in] next_block_ip
328   ///     The starting ip at the next PSB block of the same thread if available.
329   ///
330   /// \param[in] decoded_thread
331   ///     A \a DecodedThread object where the decoded instructions will be
332   ///     appended to. It might have already some instructions.
333   ///
334   /// \param[in] tsc_upper_bound
335   ///   Maximum allowed value of TSCs decoded from this PSB block.
336   ///   Any of this PSB's data occurring after this TSC will be excluded.
337   PSBBlockDecoder(PtInsnDecoderUP &&decoder_up, const PSBBlock &psb_block,
338                   Optional<lldb::addr_t> next_block_ip,
339                   DecodedThread &decoded_thread, TraceIntelPT &trace_intel_pt,
340                   llvm::Optional<DecodedThread::TSC> tsc_upper_bound)
341       : m_decoder_up(std::move(decoder_up)), m_psb_block(psb_block),
342         m_next_block_ip(next_block_ip), m_decoded_thread(decoded_thread),
343         m_anomaly_detector(*m_decoder_up, trace_intel_pt, decoded_thread),
344         m_tsc_upper_bound(tsc_upper_bound) {}
345 
346   /// \param[in] trace_intel_pt
347   ///     The main Trace object that own the PSB block.
348   ///
349   /// \param[in] decoder
350   ///     A decoder configured to start and end within the boundaries of the
351   ///     given \p psb_block.
352   ///
353   /// \param[in] psb_block
354   ///     The PSB block to decode.
355   ///
356   /// \param[in] buffer
357   ///     The raw intel pt trace for this block.
358   ///
359   /// \param[in] process
360   ///     The process to decode. It provides the memory image to use for
361   ///     decoding.
362   ///
363   /// \param[in] next_block_ip
364   ///     The starting ip at the next PSB block of the same thread if available.
365   ///
366   /// \param[in] decoded_thread
367   ///     A \a DecodedThread object where the decoded instructions will be
368   ///     appended to. It might have already some instructions.
369   static Expected<PSBBlockDecoder>
370   Create(TraceIntelPT &trace_intel_pt, const PSBBlock &psb_block,
371          ArrayRef<uint8_t> buffer, Process &process,
372          Optional<lldb::addr_t> next_block_ip, DecodedThread &decoded_thread,
373          llvm::Optional<DecodedThread::TSC> tsc_upper_bound) {
374     Expected<PtInsnDecoderUP> decoder_up =
375         CreateInstructionDecoder(trace_intel_pt, buffer, process);
376     if (!decoder_up)
377       return decoder_up.takeError();
378 
379     return PSBBlockDecoder(std::move(*decoder_up), psb_block, next_block_ip,
380                            decoded_thread, trace_intel_pt, tsc_upper_bound);
381   }
382 
383   void DecodePSBBlock() {
384     int status = pt_insn_sync_forward(m_decoder_up.get());
385     assert(status >= 0 &&
386            "Synchronization shouldn't fail because this PSB was previously "
387            "decoded correctly.");
388 
389     // We emit a TSC before a sync event to more easily associate a timestamp to
390     // the sync event. If present, the current block's TSC would be the first
391     // TSC we'll see when processing events.
392     if (m_psb_block.tsc)
393       m_decoded_thread.NotifyTsc(*m_psb_block.tsc);
394 
395     m_decoded_thread.NotifySyncPoint(m_psb_block.psb_offset);
396 
397     DecodeInstructionsAndEvents(status);
398   }
399 
400 private:
401   /// Append an instruction and return \b false if and only if a serious anomaly
402   /// has been detected.
403   bool AppendInstructionAndDetectAnomalies(const pt_insn &insn) {
404     m_decoded_thread.AppendInstruction(insn);
405 
406     if (Error err = m_anomaly_detector.DetectAnomaly()) {
407       m_decoded_thread.AppendCustomError(toString(std::move(err)),
408                                          /*fatal=*/true);
409       return false;
410     }
411     return true;
412   }
413   /// Decode all the instructions and events of the given PSB block. The
414   /// decoding loop might stop abruptly if an infinite decoding loop is
415   /// detected.
416   void DecodeInstructionsAndEvents(int status) {
417     pt_insn insn;
418 
419     while (true) {
420       status = ProcessPTEvents(status);
421 
422       if (IsLibiptError(status))
423         return;
424       else if (IsEndOfStream(status))
425         break;
426 
427       // The status returned by pt_insn_next will need to be processed
428       // by ProcessPTEvents in the next loop if it is not an error.
429       std::memset(&insn, 0, sizeof insn);
430       status = pt_insn_next(m_decoder_up.get(), &insn, sizeof(insn));
431 
432       if (IsLibiptError(status)) {
433         m_decoded_thread.AppendError(IntelPTError(status, insn.ip));
434         return;
435       } else if (IsEndOfStream(status)) {
436         break;
437       }
438 
439       if (!AppendInstructionAndDetectAnomalies(insn))
440         return;
441     }
442 
443     // We need to keep querying non-branching instructions until we hit the
444     // starting point of the next PSB. We won't see events at this point. This
445     // is based on
446     // https://github.com/intel/libipt/blob/master/doc/howto_libipt.md#parallel-decode
447     if (m_next_block_ip && insn.ip != 0) {
448       while (insn.ip != *m_next_block_ip) {
449         if (!AppendInstructionAndDetectAnomalies(insn))
450           return;
451 
452         status = pt_insn_next(m_decoder_up.get(), &insn, sizeof(insn));
453 
454         if (IsLibiptError(status)) {
455           m_decoded_thread.AppendError(IntelPTError(status, insn.ip));
456           return;
457         }
458       }
459     }
460   }
461 
462   /// Process the TSC of a decoded PT event. Specifically, check if this TSC
463   /// is below the TSC upper bound for this PSB. If the TSC exceeds the upper
464   /// bound, return an error to abort decoding. Otherwise add the it to the
465   /// underlying DecodedThread and decoding should continue as expected.
466   ///
467   /// \param[in] tsc
468   ///   The TSC of the a decoded event.
469   Error ProcessPTEventTSC(DecodedThread::TSC tsc) {
470     if (m_tsc_upper_bound && tsc >= *m_tsc_upper_bound) {
471       // This event and all the remaining events of this PSB have a TSC
472       // outside the range of the "owning" ThreadContinuousExecution. For
473       // now we drop all of these events/instructions, future work can
474       // improve upon this by determining the "owning"
475       // ThreadContinuousExecution of the remaining PSB data.
476       std::string err_msg = formatv("decoding truncated: TSC {0} exceeds "
477                                     "maximum TSC value {1}, will skip decoding"
478                                     " the remaining data of the PSB",
479                                     tsc, *m_tsc_upper_bound)
480                                 .str();
481 
482       uint64_t offset;
483       int status = pt_insn_get_offset(m_decoder_up.get(), &offset);
484       if (!IsLibiptError(status)) {
485         err_msg = formatv("{2} (skipping {0} of {1} bytes)", offset,
486                           m_psb_block.size, err_msg)
487                       .str();
488       }
489       m_decoded_thread.AppendCustomError(err_msg);
490       return createStringError(inconvertibleErrorCode(), err_msg);
491     } else {
492       m_decoded_thread.NotifyTsc(tsc);
493       return Error::success();
494     }
495   }
496 
497   /// Before querying instructions, we need to query the events associated with
498   /// that instruction, e.g. timing and trace disablement events.
499   ///
500   /// \param[in] status
501   ///   The status gotten from the previous instruction decoding or PSB
502   ///   synchronization.
503   ///
504   /// \return
505   ///     The pte_status after decoding events.
506   int ProcessPTEvents(int status) {
507     while (HasEvents(status)) {
508       pt_event event;
509       std::memset(&event, 0, sizeof event);
510       status = pt_insn_event(m_decoder_up.get(), &event, sizeof(event));
511 
512       if (IsLibiptError(status)) {
513         m_decoded_thread.AppendError(IntelPTError(status));
514         return status;
515       }
516 
517       if (event.has_tsc) {
518         if (Error err = ProcessPTEventTSC(event.tsc)) {
519           consumeError(std::move(err));
520           return -pte_internal;
521         }
522       }
523 
524       switch (event.type) {
525       case ptev_disabled:
526         // The CPU paused tracing the program, e.g. due to ip filtering.
527         m_decoded_thread.AppendEvent(lldb::eTraceEventDisabledHW);
528         break;
529       case ptev_async_disabled:
530         // The kernel or user code paused tracing the program, e.g.
531         // a breakpoint or a ioctl invocation pausing the trace, or a
532         // context switch happened.
533         m_decoded_thread.AppendEvent(lldb::eTraceEventDisabledSW);
534         break;
535       case ptev_overflow:
536         // The CPU internal buffer had an overflow error and some instructions
537         // were lost. A OVF packet comes with an FUP packet (harcoded address)
538         // according to the documentation, so we'll continue seeing instructions
539         // after this event.
540         m_decoded_thread.AppendError(IntelPTError(-pte_overflow));
541         break;
542       default:
543         break;
544       }
545     }
546 
547     return status;
548   }
549 
550 private:
551   PtInsnDecoderUP m_decoder_up;
552   PSBBlock m_psb_block;
553   Optional<lldb::addr_t> m_next_block_ip;
554   DecodedThread &m_decoded_thread;
555   PSBBlockAnomalyDetector m_anomaly_detector;
556   llvm::Optional<DecodedThread::TSC> m_tsc_upper_bound;
557 };
558 
559 Error lldb_private::trace_intel_pt::DecodeSingleTraceForThread(
560     DecodedThread &decoded_thread, TraceIntelPT &trace_intel_pt,
561     ArrayRef<uint8_t> buffer) {
562   Expected<std::vector<PSBBlock>> blocks =
563       SplitTraceIntoPSBBlock(trace_intel_pt, buffer, /*expect_tscs=*/false);
564   if (!blocks)
565     return blocks.takeError();
566 
567   for (size_t i = 0; i < blocks->size(); i++) {
568     PSBBlock &block = blocks->at(i);
569 
570     Expected<PSBBlockDecoder> decoder = PSBBlockDecoder::Create(
571         trace_intel_pt, block, buffer.slice(block.psb_offset, block.size),
572         *decoded_thread.GetThread()->GetProcess(),
573         i + 1 < blocks->size() ? blocks->at(i + 1).starting_ip : None,
574         decoded_thread, std::nullopt);
575     if (!decoder)
576       return decoder.takeError();
577 
578     decoder->DecodePSBBlock();
579   }
580 
581   return Error::success();
582 }
583 
584 Error lldb_private::trace_intel_pt::DecodeSystemWideTraceForThread(
585     DecodedThread &decoded_thread, TraceIntelPT &trace_intel_pt,
586     const DenseMap<lldb::cpu_id_t, llvm::ArrayRef<uint8_t>> &buffers,
587     const std::vector<IntelPTThreadContinousExecution> &executions) {
588   bool has_seen_psbs = false;
589   for (size_t i = 0; i < executions.size(); i++) {
590     const IntelPTThreadContinousExecution &execution = executions[i];
591 
592     auto variant = execution.thread_execution.variant;
593 
594     // We emit the first valid tsc
595     if (execution.psb_blocks.empty()) {
596       decoded_thread.NotifyTsc(execution.thread_execution.GetLowestKnownTSC());
597     } else {
598       assert(execution.psb_blocks.front().tsc &&
599              "per cpu decoding expects TSCs");
600       decoded_thread.NotifyTsc(
601           std::min(execution.thread_execution.GetLowestKnownTSC(),
602                    *execution.psb_blocks.front().tsc));
603     }
604 
605     // We then emit the CPU, which will be correctly associated with a tsc.
606     decoded_thread.NotifyCPU(execution.thread_execution.cpu_id);
607 
608     // If we haven't seen a PSB yet, then it's fine not to show errors
609     if (has_seen_psbs) {
610       if (execution.psb_blocks.empty()) {
611         decoded_thread.AppendCustomError(
612             formatv("Unable to find intel pt data a thread "
613                     "execution on cpu id = {0}",
614                     execution.thread_execution.cpu_id)
615                 .str());
616       }
617 
618       // A hinted start is a non-initial execution that doesn't have a switch
619       // in. An only end is an initial execution that doesn't have a switch in.
620       // Any of those cases represent a gap because we have seen a PSB before.
621       if (variant == ThreadContinuousExecution::Variant::HintedStart ||
622           variant == ThreadContinuousExecution::Variant::OnlyEnd) {
623         decoded_thread.AppendCustomError(
624             formatv("Unable to find the context switch in for a thread "
625                     "execution on cpu id = {0}",
626                     execution.thread_execution.cpu_id)
627                 .str());
628       }
629     }
630 
631     for (size_t j = 0; j < execution.psb_blocks.size(); j++) {
632       const PSBBlock &psb_block = execution.psb_blocks[j];
633 
634       Expected<PSBBlockDecoder> decoder = PSBBlockDecoder::Create(
635           trace_intel_pt, psb_block,
636           buffers.lookup(execution.thread_execution.cpu_id)
637               .slice(psb_block.psb_offset, psb_block.size),
638           *decoded_thread.GetThread()->GetProcess(),
639           j + 1 < execution.psb_blocks.size()
640               ? execution.psb_blocks[j + 1].starting_ip
641               : None,
642           decoded_thread, execution.thread_execution.GetEndTSC());
643       if (!decoder)
644         return decoder.takeError();
645 
646       has_seen_psbs = true;
647       decoder->DecodePSBBlock();
648     }
649 
650     // If we haven't seen a PSB yet, then it's fine not to show errors
651     if (has_seen_psbs) {
652       // A hinted end is a non-ending execution that doesn't have a switch out.
653       // An only start is an ending execution that doesn't have a switch out.
654       // Any of those cases represent a gap if we still have executions to
655       // process and we have seen a PSB before.
656       if (i + 1 != executions.size() &&
657           (variant == ThreadContinuousExecution::Variant::OnlyStart ||
658            variant == ThreadContinuousExecution::Variant::HintedEnd)) {
659         decoded_thread.AppendCustomError(
660             formatv("Unable to find the context switch out for a thread "
661                     "execution on cpu id = {0}",
662                     execution.thread_execution.cpu_id)
663                 .str());
664       }
665     }
666   }
667   return Error::success();
668 }
669 
670 bool IntelPTThreadContinousExecution::operator<(
671     const IntelPTThreadContinousExecution &o) const {
672   // As the context switch might be incomplete, we look first for the first real
673   // PSB packet, which is a valid TSC. Otherwise, We query the thread execution
674   // itself for some tsc.
675   auto get_tsc = [](const IntelPTThreadContinousExecution &exec) {
676     return exec.psb_blocks.empty() ? exec.thread_execution.GetLowestKnownTSC()
677                                    : exec.psb_blocks.front().tsc;
678   };
679 
680   return get_tsc(*this) < get_tsc(o);
681 }
682 
683 Expected<std::vector<PSBBlock>>
684 lldb_private::trace_intel_pt::SplitTraceIntoPSBBlock(
685     TraceIntelPT &trace_intel_pt, llvm::ArrayRef<uint8_t> buffer,
686     bool expect_tscs) {
687   // This follows
688   // https://github.com/intel/libipt/blob/master/doc/howto_libipt.md#parallel-decode
689 
690   Expected<PtQueryDecoderUP> decoder_up =
691       CreateQueryDecoder(trace_intel_pt, buffer);
692   if (!decoder_up)
693     return decoder_up.takeError();
694 
695   pt_query_decoder *decoder = decoder_up.get().get();
696 
697   std::vector<PSBBlock> executions;
698 
699   while (true) {
700     uint64_t maybe_ip = LLDB_INVALID_ADDRESS;
701     int decoding_status = pt_qry_sync_forward(decoder, &maybe_ip);
702     if (IsLibiptError(decoding_status))
703       break;
704 
705     uint64_t psb_offset;
706     int offset_status = pt_qry_get_sync_offset(decoder, &psb_offset);
707     assert(offset_status >= 0 &&
708            "This can't fail because we were able to synchronize");
709 
710     Optional<uint64_t> ip;
711     if (!(pts_ip_suppressed & decoding_status))
712       ip = maybe_ip;
713 
714     Optional<uint64_t> tsc;
715     // Now we fetch the first TSC that comes after the PSB.
716     while (HasEvents(decoding_status)) {
717       pt_event event;
718       decoding_status = pt_qry_event(decoder, &event, sizeof(event));
719       if (IsLibiptError(decoding_status))
720         break;
721       if (event.has_tsc) {
722         tsc = event.tsc;
723         break;
724       }
725     }
726     if (IsLibiptError(decoding_status)) {
727       // We continue to the next PSB. This effectively merges this PSB with the
728       // previous one, and that should be fine because this PSB might be the
729       // direct continuation of the previous thread and it's better to show an
730       // error in the decoded thread than to hide it. If this is the first PSB,
731       // we are okay losing it. Besides that, an error at processing events
732       // means that we wouldn't be able to get any instruction out of it.
733       continue;
734     }
735 
736     if (expect_tscs && !tsc)
737       return createStringError(inconvertibleErrorCode(),
738                                "Found a PSB without TSC.");
739 
740     executions.push_back({
741         psb_offset,
742         tsc,
743         0,
744         ip,
745     });
746   }
747   if (!executions.empty()) {
748     // We now adjust the sizes of each block
749     executions.back().size = buffer.size() - executions.back().psb_offset;
750     for (int i = (int)executions.size() - 2; i >= 0; i--) {
751       executions[i].size =
752           executions[i + 1].psb_offset - executions[i].psb_offset;
753     }
754   }
755   return executions;
756 }
757 
758 Expected<Optional<uint64_t>>
759 lldb_private::trace_intel_pt::FindLowestTSCInTrace(TraceIntelPT &trace_intel_pt,
760                                                    ArrayRef<uint8_t> buffer) {
761   Expected<PtQueryDecoderUP> decoder_up =
762       CreateQueryDecoder(trace_intel_pt, buffer);
763   if (!decoder_up)
764     return decoder_up.takeError();
765 
766   pt_query_decoder *decoder = decoder_up.get().get();
767   uint64_t ip = LLDB_INVALID_ADDRESS;
768   int status = pt_qry_sync_forward(decoder, &ip);
769   if (IsLibiptError(status))
770     return std::nullopt;
771 
772   while (HasEvents(status)) {
773     pt_event event;
774     status = pt_qry_event(decoder, &event, sizeof(event));
775     if (IsLibiptError(status))
776       return std::nullopt;
777     if (event.has_tsc)
778       return event.tsc;
779   }
780   return std::nullopt;
781 }
782