1 //===-- TraceIntelPT.cpp --------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "TraceIntelPT.h" 10 11 #include "TraceIntelPTSessionFileParser.h" 12 #include "lldb/Core/PluginManager.h" 13 #include "lldb/Target/Process.h" 14 #include "lldb/Target/Target.h" 15 #include "lldb/Target/ThreadTrace.h" 16 17 using namespace lldb; 18 using namespace lldb_private; 19 using namespace lldb_private::trace_intel_pt; 20 using namespace llvm; 21 22 LLDB_PLUGIN_DEFINE(TraceIntelPT) 23 24 void TraceIntelPT::Initialize() { 25 PluginManager::RegisterPlugin(GetPluginNameStatic(), "Intel Processor Trace", 26 CreateInstance, 27 TraceIntelPTSessionFileParser::GetSchema()); 28 } 29 30 void TraceIntelPT::Terminate() { 31 PluginManager::UnregisterPlugin(CreateInstance); 32 } 33 34 ConstString TraceIntelPT::GetPluginNameStatic() { 35 static ConstString g_name("intel-pt"); 36 return g_name; 37 } 38 39 StringRef TraceIntelPT::GetSchema() { 40 return TraceIntelPTSessionFileParser::GetSchema(); 41 } 42 43 //------------------------------------------------------------------ 44 // PluginInterface protocol 45 //------------------------------------------------------------------ 46 47 ConstString TraceIntelPT::GetPluginName() { return GetPluginNameStatic(); } 48 49 uint32_t TraceIntelPT::GetPluginVersion() { return 1; } 50 51 void TraceIntelPT::Dump(Stream *s) const {} 52 53 Expected<TraceSP> 54 TraceIntelPT::CreateInstance(const json::Value &trace_session_file, 55 StringRef session_file_dir, Debugger &debugger) { 56 return TraceIntelPTSessionFileParser(debugger, trace_session_file, 57 session_file_dir) 58 .Parse(); 59 } 60 61 TraceIntelPT::TraceIntelPT( 62 const pt_cpu &pt_cpu, 63 const std::vector<std::shared_ptr<ThreadTrace>> &traced_threads) 64 : m_pt_cpu(pt_cpu) { 65 for (const std::shared_ptr<ThreadTrace> &thread : traced_threads) 66 m_trace_threads.emplace( 67 std::piecewise_construct, 68 std::forward_as_tuple(thread->GetProcess()->GetID(), thread->GetID()), 69 std::forward_as_tuple(thread, pt_cpu)); 70 } 71 72 const DecodedThread *TraceIntelPT::Decode(const Thread &thread) { 73 auto it = m_trace_threads.find( 74 std::make_pair(thread.GetProcess()->GetID(), thread.GetID())); 75 if (it == m_trace_threads.end()) 76 return nullptr; 77 return &it->second.Decode(); 78 } 79 80 size_t TraceIntelPT::GetCursorPosition(const Thread &thread) { 81 const DecodedThread *decoded_thread = Decode(thread); 82 if (!decoded_thread) 83 return 0; 84 return decoded_thread->GetCursorPosition(); 85 } 86 87 void TraceIntelPT::TraverseInstructions( 88 const Thread &thread, size_t position, TraceDirection direction, 89 std::function<bool(size_t index, Expected<lldb::addr_t> load_addr)> 90 callback) { 91 const DecodedThread *decoded_thread = Decode(thread); 92 if (!decoded_thread) 93 return; 94 95 ArrayRef<IntelPTInstruction> instructions = decoded_thread->GetInstructions(); 96 97 ssize_t delta = direction == TraceDirection::Forwards ? 1 : -1; 98 for (ssize_t i = position; i < (ssize_t)instructions.size() && i >= 0; 99 i += delta) 100 if (!callback(i, instructions[i].GetLoadAddress())) 101 break; 102 } 103 104 size_t TraceIntelPT::GetInstructionCount(const Thread &thread) { 105 if (const DecodedThread *decoded_thread = Decode(thread)) 106 return decoded_thread->GetInstructions().size(); 107 else 108 return 0; 109 } 110