diff --git a/lldb/source/Plugins/Trace/intel-pt/LibiptDecoder.cpp b/lldb/source/Plugins/Trace/intel-pt/LibiptDecoder.cpp --- a/lldb/source/Plugins/Trace/intel-pt/LibiptDecoder.cpp +++ b/lldb/source/Plugins/Trace/intel-pt/LibiptDecoder.cpp @@ -155,9 +155,11 @@ /// appended to. It might have already some instructions. PSBBlockDecoder(PtInsnDecoderUP &&decoder_up, const PSBBlock &psb_block, Optional next_block_ip, - DecodedThread &decoded_thread) + DecodedThread &decoded_thread, + llvm::Optional tsc_upper_bound) : m_decoder_up(std::move(decoder_up)), m_psb_block(psb_block), - m_next_block_ip(next_block_ip), m_decoded_thread(decoded_thread) {} + m_next_block_ip(next_block_ip), m_decoded_thread(decoded_thread), + m_tsc_upper_bound(tsc_upper_bound) {} /// \param[in] trace_intel_pt /// The main Trace object that own the PSB block. @@ -185,14 +187,15 @@ static Expected Create(TraceIntelPT &trace_intel_pt, const PSBBlock &psb_block, ArrayRef buffer, Process &process, - Optional next_block_ip, DecodedThread &decoded_thread) { + Optional next_block_ip, DecodedThread &decoded_thread, + llvm::Optional tsc_upper_bound) { Expected decoder_up = CreateInstructionDecoder(trace_intel_pt, buffer, process); if (!decoder_up) return decoder_up.takeError(); return PSBBlockDecoder(std::move(*decoder_up), psb_block, next_block_ip, - decoded_thread); + decoded_thread, tsc_upper_bound); } void DecodePSBBlock() { @@ -279,8 +282,34 @@ return status; } - if (event.has_tsc) - m_decoded_thread.NotifyTsc(event.tsc); + if (event.has_tsc) { + if (m_tsc_upper_bound && event.tsc > m_tsc_upper_bound.value()) { + // This event and all the remaining events of this PSB have a TSC + // outside the range of the "owning" ThreadContinuousExecution. For + // now we drop all of these events/instructions, future work can + // improve upon this by determining the "owning" + // ThreadContinuousExecution of the remaining PSB data. + std::string err_msg = + formatv( + "TSC {0} exceeds maximum TSC value {1}. Will skip decoding" + " the remining the remaining data of the PSB.", + event.tsc, m_tsc_upper_bound.value()) + .str(); + + uint64_t offset; + int status = pt_insn_get_offset(m_decoder_up.get(), &offset); + if (!IsLibiptError(status)) { + err_msg = + formatv("At byte offset {0} of PSB with size {1} bytes, {2}", + offset, m_psb_block.size, err_msg) + .str(); + } + m_decoded_thread.AppendCustomError(err_msg); + return -1; + } else { + m_decoded_thread.NotifyTsc(event.tsc); + } + } switch (event.type) { case ptev_disabled: @@ -313,6 +342,8 @@ PSBBlock m_psb_block; Optional m_next_block_ip; DecodedThread &m_decoded_thread; + // Maximum allowed value of TSCs decoded from m_psb_block. + llvm::Optional m_tsc_upper_bound; }; Error lldb_private::trace_intel_pt::DecodeSingleTraceForThread( @@ -330,7 +361,7 @@ trace_intel_pt, block, buffer.slice(block.psb_offset, block.size), *decoded_thread.GetThread()->GetProcess(), i + 1 < blocks->size() ? blocks->at(i + 1).starting_ip : None, - decoded_thread); + decoded_thread, llvm::None); if (!decoder) return decoder.takeError(); @@ -392,13 +423,13 @@ Expected decoder = PSBBlockDecoder::Create( trace_intel_pt, psb_block, - buffers.lookup(executions[i].thread_execution.cpu_id) + buffers.lookup(execution.thread_execution.cpu_id) .slice(psb_block.psb_offset, psb_block.size), *decoded_thread.GetThread()->GetProcess(), j + 1 < execution.psb_blocks.size() ? execution.psb_blocks[j + 1].starting_ip : None, - decoded_thread); + decoded_thread, execution.thread_execution.GetEndTSC()); if (!decoder) return decoder.takeError();