From 22077627ae20d5a6e50f43337e8ad2e23f1fa3ff Mon Sep 17 00:00:00 2001 From: Jakob Johnson Date: Tue, 15 Mar 2022 06:06:55 -0700 Subject: [PATCH] Minor refactor and renaming: - Rename IntelPTManager class and files to IntelPTCollector - Change GetTimestampCounter API to general trace counter API, GetCounter Differential Revision: https://reviews.llvm.org/D121711 --- lldb/include/lldb/Target/TraceCursor.h | 14 +++++----- lldb/include/lldb/lldb-enumerations.h | 6 +++++ lldb/source/Plugins/Process/Linux/CMakeLists.txt | 2 +- .../{IntelPTManager.cpp => IntelPTCollector.cpp} | 30 +++++++++++----------- .../Linux/{IntelPTManager.h => IntelPTCollector.h} | 12 ++++----- .../Plugins/Process/Linux/NativeProcessLinux.cpp | 18 ++++++------- .../Plugins/Process/Linux/NativeProcessLinux.h | 4 +-- .../Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp | 7 +++-- .../Plugins/Trace/intel-pt/TraceCursorIntelPT.h | 2 +- lldb/source/Target/TraceInstructionDumper.cpp | 2 +- lldb/unittests/Process/Linux/CMakeLists.txt | 2 +- ...TManagerTests.cpp => IntelPTCollectorTests.cpp} | 4 +-- 12 files changed, 57 insertions(+), 46 deletions(-) rename lldb/source/Plugins/Process/Linux/{IntelPTManager.cpp => IntelPTCollector.cpp} (96%) rename lldb/source/Plugins/Process/Linux/{IntelPTManager.h => IntelPTCollector.h} (96%) rename lldb/unittests/Process/Linux/{IntelPTManagerTests.cpp => IntelPTCollectorTests.cpp} (97%) diff --git a/lldb/include/lldb/Target/TraceCursor.h b/lldb/include/lldb/Target/TraceCursor.h index 14fc00d..83ab22b 100644 --- a/lldb/include/lldb/Target/TraceCursor.h +++ b/lldb/include/lldb/Target/TraceCursor.h @@ -180,14 +180,16 @@ public: /// 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 GetTimestampCounter() = 0; + /// The value of the counter or \b llvm::None if not available. + virtual llvm::Optional GetCounter(lldb::TraceCounter counter_type) = 0; /// \return /// The \a lldb::TraceInstructionControlFlowType categories the diff --git a/lldb/include/lldb/lldb-enumerations.h b/lldb/include/lldb/lldb-enumerations.h index 529ab00..294c68d 100644 --- a/lldb/include/lldb/lldb-enumerations.h +++ b/lldb/include/lldb/lldb-enumerations.h @@ -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 diff --git a/lldb/source/Plugins/Process/Linux/CMakeLists.txt b/lldb/source/Plugins/Process/Linux/CMakeLists.txt index c4edc57..60958bb9 100644 --- a/lldb/source/Plugins/Process/Linux/CMakeLists.txt +++ b/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 diff --git a/lldb/source/Plugins/Process/Linux/IntelPTManager.cpp b/lldb/source/Plugins/Process/Linux/IntelPTCollector.cpp similarity index 96% rename from lldb/source/Plugins/Process/Linux/IntelPTManager.cpp rename to lldb/source/Plugins/Process/Linux/IntelPTCollector.cpp index 794689b..0e65c88 100644 --- a/lldb/source/Plugins/Process/Linux/IntelPTManager.cpp +++ b/lldb/source/Plugins/Process/Linux/IntelPTCollector.cpp @@ -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. @@ -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" @@ -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(); @@ -585,7 +585,7 @@ Error IntelPTManager::TraceStop(const TraceStopRequest &request) { } } -Error IntelPTManager::TraceStart( +Error IntelPTCollector::TraceStart( const TraceIntelPTStartRequest &request, const std::vector &process_threads) { if (request.IsProcessTracing()) { @@ -609,13 +609,13 @@ 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)) @@ -623,7 +623,7 @@ Error IntelPTManager::OnThreadDestroyed(lldb::tid_t tid) { return Error::success(); } -Expected IntelPTManager::GetState() const { +Expected IntelPTCollector::GetState() const { Expected> cpu_info = IntelPTThreadTrace::GetCPUInfo(); if (!cpu_info) return cpu_info.takeError(); @@ -646,14 +646,14 @@ Expected IntelPTManager::GetState() const { } Expected -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> -IntelPTManager::GetBinaryData(const TraceGetBinaryDataRequest &request) const { +IntelPTCollector::GetBinaryData(const TraceGetBinaryDataRequest &request) const { if (request.kind == "threadTraceBuffer") { if (Expected trace = GetTracedThread(*request.tid)) @@ -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 intel_pt_type = GetOSEventType(); if (!intel_pt_type) { llvm::consumeError(intel_pt_type.takeError()); @@ -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(); } diff --git a/lldb/source/Plugins/Process/Linux/IntelPTManager.h b/lldb/source/Plugins/Process/Linux/IntelPTCollector.h similarity index 96% rename from lldb/source/Plugins/Process/Linux/IntelPTManager.h rename to lldb/source/Plugins/Process/Linux/IntelPTCollector.h index 38566a2..051b1ad 100644 --- a/lldb/source/Plugins/Process/Linux/IntelPTManager.h +++ b/lldb/source/Plugins/Process/Linux/IntelPTCollector.h @@ -1,4 +1,4 @@ -//===-- 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. @@ -6,8 +6,8 @@ // //===----------------------------------------------------------------------===// -#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" @@ -203,9 +203,9 @@ private: }; /// 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(); @@ -260,4 +260,4 @@ private: } // namespace process_linux } // namespace lldb_private -#endif // liblldb_IntelPTManager_H_ +#endif // liblldb_IntelPTCollector_H_ diff --git a/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp b/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp index 1930af6..4bc5711 100644 --- a/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp +++ b/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp @@ -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()); @@ -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; } @@ -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()); @@ -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}", @@ -1938,7 +1938,7 @@ Status NativeProcessLinux::PtraceWrapper(int req, lldb::pid_t pid, void *addr, } llvm::Expected NativeProcessLinux::TraceSupported() { - if (IntelPTManager::IsSupported()) + if (IntelPTCollector::IsSupported()) return TraceSupportedResponse{"intel-pt", "Intel Processor Trace"}; return NativeProcessProtocol::TraceSupported(); } @@ -1951,7 +1951,7 @@ Error NativeProcessLinux::TraceStart(StringRef json_request, StringRef type) { std::vector 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(); } @@ -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 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> 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); } diff --git a/lldb/source/Plugins/Process/Linux/NativeProcessLinux.h b/lldb/source/Plugins/Process/Linux/NativeProcessLinux.h index 407062a..368b4a3 100644 --- a/lldb/source/Plugins/Process/Linux/NativeProcessLinux.h +++ b/lldb/source/Plugins/Process/Linux/NativeProcessLinux.h @@ -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" @@ -241,7 +241,7 @@ private: 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, diff --git a/lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp b/lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp index edefdd0..f6b26e0 100644 --- a/lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp +++ b/lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp @@ -85,8 +85,11 @@ lldb::addr_t TraceCursorIntelPT::GetLoadAddress() { return m_decoded_thread_sp->GetInstructions()[m_pos].GetLoadAddress(); } -Optional TraceCursorIntelPT::GetTimestampCounter() { - return m_decoded_thread_sp->GetInstructions()[m_pos].GetTimestampCounter(); +Optional TraceCursorIntelPT::GetCounter(lldb::TraceCounter counter_type) { + switch (counter_type) { + case lldb::eTraceCounterTSC: + return m_decoded_thread_sp->GetInstructions()[m_pos].GetTimestampCounter(); + } } TraceInstructionControlFlowType diff --git a/lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h b/lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h index 29d3792..8ec5594 100644 --- a/lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h +++ b/lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h @@ -28,7 +28,7 @@ public: lldb::addr_t GetLoadAddress() override; - llvm::Optional GetTimestampCounter() override; + llvm::Optional GetCounter(lldb::TraceCounter counter_type) override; lldb::TraceInstructionControlFlowType GetInstructionControlFlowType() override; diff --git a/lldb/source/Target/TraceInstructionDumper.cpp b/lldb/source/Target/TraceInstructionDumper.cpp index d58d2df..20b3a47 100644 --- a/lldb/source/Target/TraceInstructionDumper.cpp +++ b/lldb/source/Target/TraceInstructionDumper.cpp @@ -183,7 +183,7 @@ void TraceInstructionDumper::DumpInstructions(Stream &s, size_t count) { if (m_show_tsc) { s.Printf("[tsc="); - if (Optional timestamp = m_cursor_up->GetTimestampCounter()) + if (Optional timestamp = m_cursor_up->GetCounter(lldb::eTraceCounterTSC)) s.Printf("0x%016" PRIx64, *timestamp); else s.Printf("unavailable"); diff --git a/lldb/unittests/Process/Linux/CMakeLists.txt b/lldb/unittests/Process/Linux/CMakeLists.txt index ae02102..81f1e4b 100644 --- a/lldb/unittests/Process/Linux/CMakeLists.txt +++ b/lldb/unittests/Process/Linux/CMakeLists.txt @@ -1,5 +1,5 @@ add_lldb_unittest(TraceIntelPTTests - IntelPTManagerTests.cpp + IntelPTCollectorTests.cpp LINK_LIBS lldbPluginProcessLinux diff --git a/lldb/unittests/Process/Linux/IntelPTManagerTests.cpp b/lldb/unittests/Process/Linux/IntelPTCollectorTests.cpp similarity index 97% rename from lldb/unittests/Process/Linux/IntelPTManagerTests.cpp rename to lldb/unittests/Process/Linux/IntelPTCollectorTests.cpp index 76eb78a..392961b 100644 --- a/lldb/unittests/Process/Linux/IntelPTManagerTests.cpp +++ b/lldb/unittests/Process/Linux/IntelPTCollectorTests.cpp @@ -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. @@ -8,7 +8,7 @@ #include "gtest/gtest.h" -#include "IntelPTManager.h" +#include "IntelPTCollector.h" #include "llvm/ADT/ArrayRef.h" -- 2.7.4