Skip to content

Commit

Permalink
Minor refactor and renaming:
Browse files Browse the repository at this point in the history
  - Rename IntelPTManager class and files to IntelPTCollector
  - Change GetTimestampCounter API to general trace counter API,
    GetCounter

Differential Revision: https://reviews.llvm.org/D121711
  • Loading branch information
Jakob Johnson committed Mar 16, 2022
1 parent 5aab45f commit 2207762
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 46 deletions.
14 changes: 8 additions & 6 deletions lldb/include/lldb/Target/TraceCursor.h
Expand Up @@ -180,14 +180,16 @@ class TraceCursor {
/// LLDB_INVALID_ADDRESS.
virtual lldb::addr_t GetLoadAddress() = 0;

/// Get the timestamp counter associated with the current instruction.
/// Modern Intel, ARM and AMD processors support this counter. However, a
/// trace plugin might decide to use a different time unit instead of an
/// actual TSC.
/// Get the hardware counter of a given type associated with the current
/// instruction. Each architecture might support different counters. It might
/// happen that only some instructions of an entire trace have a given counter
/// associated with them.
///
/// \param[in] counter_type
/// The counter type.
/// \return
/// The timestamp or \b llvm::None if not available.
virtual llvm::Optional<uint64_t> GetTimestampCounter() = 0;
/// The value of the counter or \b llvm::None if not available.
virtual llvm::Optional<uint64_t> GetCounter(lldb::TraceCounter counter_type) = 0;

/// \return
/// The \a lldb::TraceInstructionControlFlowType categories the
Expand Down
6 changes: 6 additions & 0 deletions lldb/include/lldb/lldb-enumerations.h
Expand Up @@ -1141,6 +1141,12 @@ enum SaveCoreStyle {
eSaveCoreStackOnly = 3,
};

// Type of counter values associated with instructions in a trace.
enum TraceCounter {
// Timestamp counter, like the one offered by Intel CPUs (TSC).
eTraceCounterTSC,
};

} // namespace lldb

#endif // LLDB_LLDB_ENUMERATIONS_H
2 changes: 1 addition & 1 deletion lldb/source/Plugins/Process/Linux/CMakeLists.txt
@@ -1,5 +1,5 @@
add_lldb_library(lldbPluginProcessLinux
IntelPTManager.cpp
IntelPTCollector.cpp
NativeProcessLinux.cpp
NativeRegisterContextLinux.cpp
NativeRegisterContextLinux_arm.cpp
Expand Down
@@ -1,4 +1,4 @@
//===-- IntelPTManager.cpp ------------------------------------------------===//
//===-- IntelPTCollector.cpp ------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
Expand All @@ -14,7 +14,7 @@
#include "llvm/Support/Error.h"
#include "llvm/Support/MathExtras.h"

#include "IntelPTManager.h"
#include "IntelPTCollector.h"
#include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
#include "lldb/Host/linux/Support.h"
#include "lldb/Utility/StreamString.h"
Expand Down Expand Up @@ -564,15 +564,15 @@ IntelPTProcessTrace::GetThreadTraces() const {
return m_thread_traces;
}

/// IntelPTManager
/// IntelPTCollector

Error IntelPTManager::TraceStop(lldb::tid_t tid) {
Error IntelPTCollector::TraceStop(lldb::tid_t tid) {
if (IsProcessTracingEnabled() && m_process_trace->TracesThread(tid))
return m_process_trace->TraceStop(tid);
return m_thread_traces.TraceStop(tid);
}

Error IntelPTManager::TraceStop(const TraceStopRequest &request) {
Error IntelPTCollector::TraceStop(const TraceStopRequest &request) {
if (request.IsProcessTracing()) {
Clear();
return Error::success();
Expand All @@ -585,7 +585,7 @@ Error IntelPTManager::TraceStop(const TraceStopRequest &request) {
}
}

Error IntelPTManager::TraceStart(
Error IntelPTCollector::TraceStart(
const TraceIntelPTStartRequest &request,
const std::vector<lldb::tid_t> &process_threads) {
if (request.IsProcessTracing()) {
Expand All @@ -609,21 +609,21 @@ Error IntelPTManager::TraceStart(
}
}

Error IntelPTManager::OnThreadCreated(lldb::tid_t tid) {
Error IntelPTCollector::OnThreadCreated(lldb::tid_t tid) {
if (!IsProcessTracingEnabled())
return Error::success();
return m_process_trace->TraceStart(tid);
}

Error IntelPTManager::OnThreadDestroyed(lldb::tid_t tid) {
Error IntelPTCollector::OnThreadDestroyed(lldb::tid_t tid) {
if (IsProcessTracingEnabled() && m_process_trace->TracesThread(tid))
return m_process_trace->TraceStop(tid);
else if (m_thread_traces.TracesThread(tid))
return m_thread_traces.TraceStop(tid);
return Error::success();
}

Expected<json::Value> IntelPTManager::GetState() const {
Expected<json::Value> IntelPTCollector::GetState() const {
Expected<ArrayRef<uint8_t>> cpu_info = IntelPTThreadTrace::GetCPUInfo();
if (!cpu_info)
return cpu_info.takeError();
Expand All @@ -646,14 +646,14 @@ Expected<json::Value> IntelPTManager::GetState() const {
}

Expected<const IntelPTThreadTrace &>
IntelPTManager::GetTracedThread(lldb::tid_t tid) const {
IntelPTCollector::GetTracedThread(lldb::tid_t tid) const {
if (IsProcessTracingEnabled() && m_process_trace->TracesThread(tid))
return m_process_trace->GetThreadTraces().GetTracedThread(tid);
return m_thread_traces.GetTracedThread(tid);
}

Expected<std::vector<uint8_t>>
IntelPTManager::GetBinaryData(const TraceGetBinaryDataRequest &request) const {
IntelPTCollector::GetBinaryData(const TraceGetBinaryDataRequest &request) const {
if (request.kind == "threadTraceBuffer") {
if (Expected<const IntelPTThreadTrace &> trace =
GetTracedThread(*request.tid))
Expand All @@ -668,9 +668,9 @@ IntelPTManager::GetBinaryData(const TraceGetBinaryDataRequest &request) const {
request.kind.c_str());
}

void IntelPTManager::ClearProcessTracing() { m_process_trace = None; }
void IntelPTCollector::ClearProcessTracing() { m_process_trace = None; }

bool IntelPTManager::IsSupported() {
bool IntelPTCollector::IsSupported() {
Expected<uint32_t> intel_pt_type = GetOSEventType();
if (!intel_pt_type) {
llvm::consumeError(intel_pt_type.takeError());
Expand All @@ -679,11 +679,11 @@ bool IntelPTManager::IsSupported() {
return true;
}

bool IntelPTManager::IsProcessTracingEnabled() const {
bool IntelPTCollector::IsProcessTracingEnabled() const {
return (bool)m_process_trace;
}

void IntelPTManager::Clear() {
void IntelPTCollector::Clear() {
ClearProcessTracing();
m_thread_traces.Clear();
}
@@ -1,13 +1,13 @@
//===-- IntelPTManager.h -------------------------------------- -*- C++ -*-===//
//===-- IntelPTCollector.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 liblldb_IntelPTManager_H_
#define liblldb_IntelPTManager_H_
#ifndef liblldb_IntelPTCollector_H_
#define liblldb_IntelPTCollector_H_

#include "lldb/Utility/Status.h"
#include "lldb/Utility/TraceIntelPTGDBRemotePackets.h"
Expand Down Expand Up @@ -203,9 +203,9 @@ class IntelPTProcessTrace {
};

/// Main class that manages intel-pt process and thread tracing.
class IntelPTManager {
class IntelPTCollector {
public:
IntelPTManager(lldb::pid_t pid) : m_pid(pid), m_thread_traces(pid) {}
IntelPTCollector(lldb::pid_t pid) : m_pid(pid), m_thread_traces(pid) {}

static bool IsSupported();

Expand Down Expand Up @@ -260,4 +260,4 @@ class IntelPTManager {
} // namespace process_linux
} // namespace lldb_private

#endif // liblldb_IntelPTManager_H_
#endif // liblldb_IntelPTCollector_H_
18 changes: 9 additions & 9 deletions lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp
Expand Up @@ -312,7 +312,7 @@ NativeProcessLinux::NativeProcessLinux(::pid_t pid, int terminal_fd,
const ArchSpec &arch, MainLoop &mainloop,
llvm::ArrayRef<::pid_t> tids)
: NativeProcessELF(pid, terminal_fd, delegate), m_arch(arch),
m_main_loop(mainloop), m_intel_pt_manager(pid) {
m_main_loop(mainloop), m_intel_pt_collector(pid) {
if (m_terminal_fd != -1) {
Status status = EnsureFDFlags(m_terminal_fd, O_NONBLOCK);
assert(status.Success());
Expand Down Expand Up @@ -983,7 +983,7 @@ Status NativeProcessLinux::Detach() {
e; // Save the error, but still attempt to detach from other threads.
}

m_intel_pt_manager.Clear();
m_intel_pt_collector.Clear();

return error;
}
Expand Down Expand Up @@ -1666,7 +1666,7 @@ void NativeProcessLinux::StopTrackingThread(NativeThreadLinux &thread) {

Status NativeProcessLinux::NotifyTracersOfNewThread(lldb::tid_t tid) {
Log *log = GetLog(POSIXLog::Thread);
Status error(m_intel_pt_manager.OnThreadCreated(tid));
Status error(m_intel_pt_collector.OnThreadCreated(tid));
if (error.Fail())
LLDB_LOG(log, "Failed to trace a new thread with intel-pt, tid = {0}. {1}",
tid, error.AsCString());
Expand All @@ -1675,7 +1675,7 @@ Status NativeProcessLinux::NotifyTracersOfNewThread(lldb::tid_t tid) {

Status NativeProcessLinux::NotifyTracersOfThreadDestroyed(lldb::tid_t tid) {
Log *log = GetLog(POSIXLog::Thread);
Status error(m_intel_pt_manager.OnThreadDestroyed(tid));
Status error(m_intel_pt_collector.OnThreadDestroyed(tid));
if (error.Fail())
LLDB_LOG(log,
"Failed to stop a destroyed thread with intel-pt, tid = {0}. {1}",
Expand Down Expand Up @@ -1938,7 +1938,7 @@ Status NativeProcessLinux::PtraceWrapper(int req, lldb::pid_t pid, void *addr,
}

llvm::Expected<TraceSupportedResponse> NativeProcessLinux::TraceSupported() {
if (IntelPTManager::IsSupported())
if (IntelPTCollector::IsSupported())
return TraceSupportedResponse{"intel-pt", "Intel Processor Trace"};
return NativeProcessProtocol::TraceSupported();
}
Expand All @@ -1951,7 +1951,7 @@ Error NativeProcessLinux::TraceStart(StringRef json_request, StringRef type) {
std::vector<lldb::tid_t> process_threads;
for (auto &thread : m_threads)
process_threads.push_back(thread->GetID());
return m_intel_pt_manager.TraceStart(*request, process_threads);
return m_intel_pt_collector.TraceStart(*request, process_threads);
} else
return request.takeError();
}
Expand All @@ -1961,19 +1961,19 @@ Error NativeProcessLinux::TraceStart(StringRef json_request, StringRef type) {

Error NativeProcessLinux::TraceStop(const TraceStopRequest &request) {
if (request.type == "intel-pt")
return m_intel_pt_manager.TraceStop(request);
return m_intel_pt_collector.TraceStop(request);
return NativeProcessProtocol::TraceStop(request);
}

Expected<json::Value> NativeProcessLinux::TraceGetState(StringRef type) {
if (type == "intel-pt")
return m_intel_pt_manager.GetState();
return m_intel_pt_collector.GetState();
return NativeProcessProtocol::TraceGetState(type);
}

Expected<std::vector<uint8_t>> NativeProcessLinux::TraceGetBinaryData(
const TraceGetBinaryDataRequest &request) {
if (request.type == "intel-pt")
return m_intel_pt_manager.GetBinaryData(request);
return m_intel_pt_collector.GetBinaryData(request);
return NativeProcessProtocol::TraceGetBinaryData(request);
}
4 changes: 2 additions & 2 deletions lldb/source/Plugins/Process/Linux/NativeProcessLinux.h
Expand Up @@ -20,7 +20,7 @@
#include "lldb/Utility/FileSpec.h"
#include "lldb/lldb-types.h"

#include "IntelPTManager.h"
#include "IntelPTCollector.h"
#include "NativeThreadLinux.h"
#include "Plugins/Process/POSIX/NativeProcessELF.h"
#include "Plugins/Process/Utility/NativeProcessSoftwareSingleStep.h"
Expand Down Expand Up @@ -241,7 +241,7 @@ class NativeProcessLinux : public NativeProcessELF,
Status PopulateMemoryRegionCache();

/// Manages Intel PT process and thread traces.
IntelPTManager m_intel_pt_manager;
IntelPTCollector m_intel_pt_collector;

// Handle a clone()-like event.
bool MonitorClone(NativeThreadLinux &parent, lldb::pid_t child_pid,
Expand Down
7 changes: 5 additions & 2 deletions lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
Expand Up @@ -85,8 +85,11 @@ lldb::addr_t TraceCursorIntelPT::GetLoadAddress() {
return m_decoded_thread_sp->GetInstructions()[m_pos].GetLoadAddress();
}

Optional<uint64_t> TraceCursorIntelPT::GetTimestampCounter() {
return m_decoded_thread_sp->GetInstructions()[m_pos].GetTimestampCounter();
Optional<uint64_t> TraceCursorIntelPT::GetCounter(lldb::TraceCounter counter_type) {
switch (counter_type) {
case lldb::eTraceCounterTSC:
return m_decoded_thread_sp->GetInstructions()[m_pos].GetTimestampCounter();
}
}

TraceInstructionControlFlowType
Expand Down
2 changes: 1 addition & 1 deletion lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
Expand Up @@ -28,7 +28,7 @@ class TraceCursorIntelPT : public TraceCursor {

lldb::addr_t GetLoadAddress() override;

llvm::Optional<uint64_t> GetTimestampCounter() override;
llvm::Optional<uint64_t> GetCounter(lldb::TraceCounter counter_type) override;

lldb::TraceInstructionControlFlowType
GetInstructionControlFlowType() override;
Expand Down
2 changes: 1 addition & 1 deletion lldb/source/Target/TraceInstructionDumper.cpp
Expand Up @@ -183,7 +183,7 @@ void TraceInstructionDumper::DumpInstructions(Stream &s, size_t count) {
if (m_show_tsc) {
s.Printf("[tsc=");

if (Optional<uint64_t> timestamp = m_cursor_up->GetTimestampCounter())
if (Optional<uint64_t> timestamp = m_cursor_up->GetCounter(lldb::eTraceCounterTSC))
s.Printf("0x%016" PRIx64, *timestamp);
else
s.Printf("unavailable");
Expand Down
2 changes: 1 addition & 1 deletion lldb/unittests/Process/Linux/CMakeLists.txt
@@ -1,5 +1,5 @@
add_lldb_unittest(TraceIntelPTTests
IntelPTManagerTests.cpp
IntelPTCollectorTests.cpp

LINK_LIBS
lldbPluginProcessLinux
Expand Down
@@ -1,4 +1,4 @@
//===-- IntelPTManagerTests.cpp -------------------------------------------===//
//===-- IntelPTCollectorTests.cpp -------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
Expand All @@ -8,7 +8,7 @@

#include "gtest/gtest.h"

#include "IntelPTManager.h"
#include "IntelPTCollector.h"
#include "llvm/ADT/ArrayRef.h"


Expand Down

0 comments on commit 2207762

Please sign in to comment.