Walter Erquinigo cfd96f057b [trace][intel-pt] Implement the basic decoding functionality
Depends on D89408.

This diff finally implements trace decoding!

The current interface is

  $ trace load /path/to/trace/session/file.json
  $ thread trace dump instructions

  thread #1: tid = 3842849, total instructions = 22
    [ 0] 0x40052d
    [ 1] 0x40052d
    ...
    [19] 0x400521

  $ # simply enter, which is a repeat command
    [20] 0x40052d
    [21] 0x400529
    ...

This doesn't do any disassembly, which will be done in the next diff.

Changes:
- Added an IntelPTDecoder class, that is a wrapper for libipt, which is the actual library that performs the decoding.
- Added TraceThreadDecoder class that decodes traces and memoizes the result to avoid repeating the decoding step.
- Added a DecodedThread class, which represents the output from decoding and that for the time being only stores the list of reconstructed instructions. Later it'll contain the function call hierarchy, which will enable reconstructing backtraces.
- Added basic APIs for accessing the trace in Trace.h:
  - GetInstructionCount, which counts the number of instructions traced for a given thread
  - IsTraceFailed, which returns an Error if decoding a thread failed
  - ForEachInstruction, which iterates on the instructions traced for a given thread, concealing the internal storage of threads, as plug-ins can decide to generate the instructions on the fly or to store them all in a vector, like I do.
- DumpTraceInstructions was updated to print the instructions or show an error message if decoding was impossible.
- Tests included

Differential Revision: https://reviews.llvm.org/D89283
2020-11-05 18:38:03 -08:00

53 lines
1.7 KiB
C++

//===-- IntelPTDecoder.h --======--------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef LLDB_SOURCE_PLUGINS_TRACE_INTEL_PT_DECODER_H
#define LLDB_SOURCE_PLUGINS_TRACE_INTEL_PT_DECODER_H
#include "intel-pt.h"
#include "DecodedThread.h"
#include "lldb/Target/Process.h"
#include "lldb/Utility/FileSpec.h"
namespace lldb_private {
namespace trace_intel_pt {
/// \a lldb_private::ThreadTrace decoder that stores the output from decoding,
/// avoiding recomputations, as decoding is expensive.
class ThreadTraceDecoder {
public:
/// \param[in] trace_thread
/// The thread whose trace file will be decoded.
///
/// \param[in] pt_cpu
/// The libipt cpu used when recording the trace.
ThreadTraceDecoder(const std::shared_ptr<ThreadTrace> &trace_thread,
const pt_cpu &pt_cpu)
: m_trace_thread(trace_thread), m_pt_cpu(pt_cpu), m_decoded_thread() {}
/// Decode the thread and store the result internally.
///
/// \return
/// A \a DecodedThread instance.
const DecodedThread &Decode();
private:
ThreadTraceDecoder(const ThreadTraceDecoder &other) = delete;
ThreadTraceDecoder &operator=(const ThreadTraceDecoder &other) = delete;
std::shared_ptr<ThreadTrace> m_trace_thread;
pt_cpu m_pt_cpu;
llvm::Optional<DecodedThread> m_decoded_thread;
};
} // namespace trace_intel_pt
} // namespace lldb_private
#endif // LLDB_SOURCE_PLUGINS_TRACE_INTEL_PT_DECODER_H