Files
clang-p2996/lldb/tools/intel-features/intel-pt/PTDecoder.h
Abhishek Aggarwal 307db0f897 Tool for using Intel(R) Processor Trace hardware feature
Summary:
1. Provide single library for all Intel specific hardware features instead
    of individual libraries for each feature
2. Added Intel(R) Processor Trace hardware feature in this single library.
    Details about the tool implementing this feature is as follows:

     Tool developed on top of LLDB to provide its users the execution
     trace of the debugged inferiors. Tool's API are exposed as C++ object
     oriented interface in a shared library. API are designed especially to be
     easily integrable with IDEs providing LLDB as an application debugger.
     Entire API is also available as Python functions through a script bridging
     interface allowing development of python modules.

     This patch also provides a CLI wrapper to use the Tool through LLDB's command
     line. Highlights of the Tool and the wrapper are given below:

  ******************************
  Intel(R) Processor Trace Tool:
  ******************************
       - Provides execution trace of the debugged application
       - Uses Intel(R) Processor Trace hardware feature (already implemented inside LLDB)
         for this purpose
           -- Collects trace packets generated by this feature from LLDB, decodes and
              post-processes them
           -- Constructs the execution trace of the application
           -- Presents execution trace as a list of assembly instructions
       - Provides 4 APIs (exposed as C++ object oriented interface)
           -- start trace with configuration options for a thread/process,
           -- stop trace for a thread/process,
           -- get the execution flow (assembly instructions) for a thread,
           -- get trace specific information for a thread
       - Easily integrable into IDEs providing LLDB as application debugger
       - Entire API available as Python functions through script bridging interface
           -- Allows developing python apps on top of Tool
       - README_TOOL.txt provides more details about the Tool, its dependencies, building
         steps and API usage
       - Tool ready to use through LLDB's command line
           -- CLI wrapper has been developed on top of the Tool for this purpose

  *********************************
  CLI wrapper: cli-wrapper-pt.cpp
  *********************************
       - Provides 4 commands (syntax similar to LLDB's CLI commands):
           -- processor-trace start
           -- processor-trace stop
           -- processor-trace show-trace-options
           -- processor-trace show-instr-log
       - README_CLI.txt provides more details about commands and their options

Signed-off-by: Abhishek Aggarwal <abhishek.a.aggarwal@intel.com>

Reviewers: clayborg, jingham, lldb-commits, labath

Reviewed By: clayborg

Subscribers: ravitheja, emaste, krytarowski, mgorny

Differential Revision: https://reviews.llvm.org/D33035

llvm-svn: 310261
2017-08-07 15:26:11 +00:00

311 lines
13 KiB
C++

//===-- PTDecoder.h ---------------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef PTDecoder_h_
#define PTDecoder_h_
// C/C++ Includes
#include <vector>
// Project includes, Other libraries and framework includes
#include "lldb/API/SBDebugger.h"
#include "lldb/API/SBError.h"
#include "lldb/API/SBProcess.h"
#include "lldb/API/SBStructuredData.h"
#include "lldb/API/SBTraceOptions.h"
#include "lldb/lldb-enumerations.h"
#include "lldb/lldb-types.h"
namespace ptdecoder_private {
class Instruction;
class InstructionList;
class TraceOptions;
class Decoder;
} // namespace ptdecoder_private
namespace ptdecoder {
//----------------------------------------------------------------------
/// @class PTInstruction
/// @brief Represents an assembly instruction containing raw
/// instruction bytes, instruction address along with information
/// regarding execution flow context and Intel(R) Processor Trace
/// context.
//----------------------------------------------------------------------
class PTInstruction {
public:
PTInstruction();
PTInstruction(const PTInstruction &insn);
PTInstruction(const std::shared_ptr<ptdecoder_private::Instruction> &ptr);
~PTInstruction();
// Get instruction address in inferior's memory image
uint64_t GetInsnAddress() const;
//------------------------------------------------------------------
/// Get raw bytes of the instruction in the buffer.
///
/// @param[out] buf
/// The buffer where the raw bytes will be written. This buffer should be
/// allocated by the caller of this API. Providing an unallocated buffer
/// is an error. In case of errors, the content of the buffer is not
/// valid.
///
/// @param[in] size
/// Number of raw bytes to be written to @buf. Atleast @size bytes of
/// memory should be allocated to @buf otherwise the behaviour of the API
/// is undefined. Providing 0 for this argument is an error.
///
/// @return
/// Number of bytes of the instruction actually written to @buf if API
/// succeeds. In case of errors, total number of raw bytes of the
/// instruction is returned.
//------------------------------------------------------------------
size_t GetRawBytes(void *buf, size_t size) const;
// Get error string if it represents an invalid instruction. For a valid
// instruction, an empty string is returned
std::string GetError() const;
// Instruction was executed speculatively or not
bool GetSpeculative() const;
private:
std::shared_ptr<ptdecoder_private::Instruction> m_opaque_sp;
};
//---------------------------------------------------------------------------
/// @class PTInstructionList
/// @brief Represents a list of assembly instructions. Each instruction is of
/// type PTInstruction.
//---------------------------------------------------------------------------
class PTInstructionList {
public:
PTInstructionList();
PTInstructionList(const PTInstructionList &insn_list);
~PTInstructionList();
// Get number of instructions in the list
size_t GetSize() const;
// Get instruction at index
PTInstruction GetInstructionAtIndex(uint32_t idx);
void Clear();
private:
friend class PTDecoder;
void SetSP(const std::shared_ptr<ptdecoder_private::InstructionList> &ptr);
std::shared_ptr<ptdecoder_private::InstructionList> m_opaque_sp;
};
//----------------------------------------------------------------------
/// @class PTTraceOptions
/// @brief Provides configuration options like trace type, trace buffer size,
/// meta data buffer size along with other Intel(R) Processor Trace
/// specific options.
//----------------------------------------------------------------------
class PTTraceOptions {
public:
PTTraceOptions();
PTTraceOptions(const PTTraceOptions &options);
~PTTraceOptions();
lldb::TraceType GetType() const;
uint64_t GetTraceBufferSize() const;
uint64_t GetMetaDataBufferSize() const;
//------------------------------------------------------------------
/// Get Intel(R) Processor Trace specific configuration options (apart from
/// trace buffer size, meta data buffer size and TraceType) formatted as json
/// text i.e. {"Name":Value,"Name":Value} pairs, where "Value" is a 64-bit
/// unsigned integer in hex format. For "Name", please refer to
/// SBProcess::StartTrace API description for setting SBTraceOptions.
///
/// @return
/// A string formatted as json text {"Name":Value,"Name":Value}
//------------------------------------------------------------------
lldb::SBStructuredData GetTraceParams(lldb::SBError &error);
private:
friend class PTDecoder;
void SetSP(const std::shared_ptr<ptdecoder_private::TraceOptions> &ptr);
std::shared_ptr<ptdecoder_private::TraceOptions> m_opaque_sp;
};
//----------------------------------------------------------------------
/// @class PTDecoder
/// @brief This class makes use of Intel(R) Processor Trace hardware feature
/// (implememted inside LLDB) to gather trace data for an inferior (being
/// debugged with LLDB) to provide meaningful information out of it.
///
/// Currently the meaningful information comprises of the execution flow
/// of the inferior (in terms of assembly instructions executed). The class
/// enables user to:
/// - start the trace with configuration options for a thread/process,
/// - stop the trace for a thread/process,
/// - get the execution flow (assembly instructions) for a thread and
/// - get trace specific information for a thread
//----------------------------------------------------------------------
class PTDecoder {
public:
PTDecoder(lldb::SBDebugger &sbdebugger);
PTDecoder(const PTDecoder &ptdecoder);
~PTDecoder();
//------------------------------------------------------------------
/// Start Intel(R) Processor Trace on a thread or complete process with
/// Intel(R) Processor Trace specific configuration options
///
/// @param[in] sbprocess
/// A valid process on which this operation will be performed. An error is
/// returned in case of an invalid process.
///
/// @param[in] sbtraceoptions
/// Contains thread id information and configuration options:
///
/// For tracing a single thread, provide a valid thread id. If sbprocess
/// doesn't contain this thread id, error will be returned. For tracing
/// complete process, set it to lldb::LLDB_INVALID_THREAD_ID
/// Configuration options comprises of:
/// a) trace buffer size, meta data buffer size, TraceType and
/// b) All other possible Intel(R) Processor Trace specific configuration
/// options (hereafter collectively referred as CUSTOM_OPTIONS), formatted
/// as json text i.e. {"Name":Value,"Name":Value,..} inside
/// sbtraceoptions, where "Value" should be a 64-bit unsigned integer in
/// hex format. For information regarding what all configuration options
/// are currently supported by LLDB and detailed information about
/// CUSTOM_OPTIONS usage, please refer to SBProcess::StartTrace() API
/// description. To know about all possible configuration options of
/// Intel(R) Processor Trace, please refer to Intel(R) 64 and IA-32
/// Architectures Software Developer's Manual.
///
/// TraceType should be set to lldb::TraceType::eTraceTypeProcessorTrace,
/// else error is returned. To find out any other requirement to start
/// tracing successfully, please refer to SBProcess::StartTrace() API
/// description. LLDB's current implementation of Intel(R) Processor Trace
/// feature may round off invalid values for configuration options.
/// Therefore, the configuration options with which the trace was actually
/// started, might be different to the ones with which trace was asked to
/// be started by user. The actual used configuration options can be
/// obtained from GetProcessorTraceInfo() API.
///
/// @param[out] sberror
/// An error with the failure reason if API fails. Else success.
//------------------------------------------------------------------
void StartProcessorTrace(lldb::SBProcess &sbprocess,
lldb::SBTraceOptions &sbtraceoptions,
lldb::SBError &sberror);
//------------------------------------------------------------------
/// Stop Intel(R) Processor Trace on a thread or complete process.
///
/// @param[in] sbprocess
/// A valid process on which this operation will be performed. An error is
/// returned in case of an invalid process.
///
/// @param[in] tid
/// Case 1: To stop tracing a single thread, provide a valid thread id. If
/// sbprocess doesn't contain the thread tid, error will be returned.
/// Case 2: To stop tracing complete process, use
/// lldb::LLDB_INVALID_THREAD_ID.
///
/// @param[out] sberror
/// An error with the failure reason if API fails. Else success.
//------------------------------------------------------------------
void StopProcessorTrace(lldb::SBProcess &sbprocess, lldb::SBError &sberror,
lldb::tid_t tid = LLDB_INVALID_THREAD_ID);
//------------------------------------------------------------------
/// Get instruction log containing the execution flow for a thread of a
/// process in terms of assembly instructions executed.
///
/// @param[in] sbprocess
/// A valid process on which this operation will be performed. An error is
/// returned in case of an invalid process.
///
/// @param[in] tid
/// A valid thread id of the thread for which instruction log is desired.
/// If sbprocess doesn't contain the thread tid, error will be returned.
///
/// @param[in] count
/// The number of instructions requested by the user to be returned from
/// the complete instruction log. Complete instruction log refers to all
/// the assembly instructions obtained after decoding the complete raw
/// trace data obtained from LLDB. The length of the complete instruction
/// log is dependent on the trace buffer size with which processor tracing
/// was started for this thread.
/// The number of instructions actually returned are dependent on 'count'
/// and 'offset' parameters of this API.
///
/// @param[in] offset
/// The offset in the complete instruction log from where 'count' number
/// of instructions are requested by the user. offset is counted from the
/// end of of this complete instruction log (which means the last executed
/// instruction is at offset 0 (zero)).
///
/// @param[out] result_list
/// Depending upon 'count' and 'offset' values, list will be overwritten
/// with the new instructions.
///
/// @param[out] sberror
/// An error with the failure reason if API fails. Else success.
//------------------------------------------------------------------
void GetInstructionLogAtOffset(lldb::SBProcess &sbprocess, lldb::tid_t tid,
uint32_t offset, uint32_t count,
PTInstructionList &result_list,
lldb::SBError &sberror);
//------------------------------------------------------------------
/// Get Intel(R) Processor Trace specific information for a thread of a
/// process. The information contains the actual configuration options with
/// which the trace was started for this thread.
///
/// @param[in] sbprocess
/// A valid process on which this operation will be performed. An error is
/// returned in case of an invalid process.
///
/// @param[in] tid
/// A valid thread id of the thread for which the trace specific
/// information is required. If sbprocess doesn't contain the thread tid,
/// an error will be returned.
///
/// @param[out] options
/// Contains actual configuration options (they may be different to the
/// ones with which tracing was asked to be started for this thread during
/// StartProcessorTrace() API call).
///
/// @param[out] sberror
/// An error with the failure reason if API fails. Else success.
//------------------------------------------------------------------
void GetProcessorTraceInfo(lldb::SBProcess &sbprocess, lldb::tid_t tid,
PTTraceOptions &options, lldb::SBError &sberror);
private:
std::shared_ptr<ptdecoder_private::Decoder> m_opaque_sp;
};
} // namespace ptdecoder
#endif // PTDecoder_h_