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
65 lines
2.0 KiB
C++
65 lines
2.0 KiB
C++
//===-- DecodedThread.cpp -------------------------------------------------===//
|
|
//
|
|
// 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "DecodedThread.h"
|
|
|
|
#include "lldb/Utility/StreamString.h"
|
|
|
|
using namespace lldb_private;
|
|
using namespace lldb_private::trace_intel_pt;
|
|
using namespace llvm;
|
|
|
|
char IntelPTError::ID;
|
|
|
|
IntelPTError::IntelPTError(int libipt_error_code, lldb::addr_t address)
|
|
: m_libipt_error_code(libipt_error_code), m_address(address) {
|
|
assert(libipt_error_code < 0);
|
|
}
|
|
|
|
void IntelPTError::log(llvm::raw_ostream &OS) const {
|
|
const char *libipt_error_message = pt_errstr(pt_errcode(m_libipt_error_code));
|
|
if (m_address != LLDB_INVALID_ADDRESS && m_address > 0) {
|
|
write_hex(OS, m_address, HexPrintStyle::PrefixLower, 18);
|
|
OS << " ";
|
|
}
|
|
OS << "error: " << libipt_error_message;
|
|
}
|
|
|
|
bool IntelPTInstruction::IsError() const { return (bool)m_error; }
|
|
|
|
Expected<lldb::addr_t> IntelPTInstruction::GetLoadAddress() const {
|
|
if (IsError())
|
|
return ToError();
|
|
return m_pt_insn.ip;
|
|
}
|
|
|
|
Error IntelPTInstruction::ToError() const {
|
|
if (!IsError())
|
|
return Error::success();
|
|
|
|
if (m_error->isA<IntelPTError>())
|
|
return make_error<IntelPTError>(static_cast<IntelPTError &>(*m_error));
|
|
return make_error<StringError>(m_error->message(),
|
|
m_error->convertToErrorCode());
|
|
}
|
|
|
|
size_t DecodedThread::GetLastPosition() const {
|
|
return m_instructions.empty() ? 0 : m_instructions.size() - 1;
|
|
}
|
|
|
|
ArrayRef<IntelPTInstruction> DecodedThread::GetInstructions() const {
|
|
return makeArrayRef(m_instructions);
|
|
}
|
|
|
|
size_t DecodedThread::GetCursorPosition() const { return m_position; }
|
|
|
|
size_t DecodedThread::SetCursorPosition(size_t new_position) {
|
|
m_position = std::min(new_position, GetLastPosition());
|
|
return m_position;
|
|
}
|