15ffd83dbSDimitry Andric //===-- SBTrace.cpp -------------------------------------------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #include "lldb/Target/Process.h" 1004eeddc0SDimitry Andric #include "lldb/Utility/Instrumentation.h" 110b57cec5SDimitry Andric 12*81ad6265SDimitry Andric #include "lldb/API/SBDebugger.h" 13fe6060f1SDimitry Andric #include "lldb/API/SBStructuredData.h" 14fe6060f1SDimitry Andric #include "lldb/API/SBThread.h" 150b57cec5SDimitry Andric #include "lldb/API/SBTrace.h" 16fe6060f1SDimitry Andric 17fe6060f1SDimitry Andric #include "lldb/Core/StructuredDataImpl.h" 180b57cec5SDimitry Andric 190b57cec5SDimitry Andric #include <memory> 200b57cec5SDimitry Andric 210b57cec5SDimitry Andric using namespace lldb; 220b57cec5SDimitry Andric using namespace lldb_private; 23*81ad6265SDimitry Andric using namespace llvm; 240b57cec5SDimitry Andric 2504eeddc0SDimitry Andric SBTrace::SBTrace() { LLDB_INSTRUMENT_VA(this); } 260b57cec5SDimitry Andric 27fe6060f1SDimitry Andric SBTrace::SBTrace(const lldb::TraceSP &trace_sp) : m_opaque_sp(trace_sp) { 2804eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, trace_sp); 290b57cec5SDimitry Andric } 300b57cec5SDimitry Andric 31*81ad6265SDimitry Andric SBTrace SBTrace::LoadTraceFromFile(SBError &error, SBDebugger &debugger, 32*81ad6265SDimitry Andric const SBFileSpec &trace_description_file) { 33*81ad6265SDimitry Andric LLDB_INSTRUMENT_VA(error, debugger, trace_description_file); 34*81ad6265SDimitry Andric 35*81ad6265SDimitry Andric Expected<lldb::TraceSP> trace_or_err = Trace::LoadPostMortemTraceFromFile( 36*81ad6265SDimitry Andric debugger.ref(), trace_description_file.ref()); 37*81ad6265SDimitry Andric 38*81ad6265SDimitry Andric if (!trace_or_err) { 39*81ad6265SDimitry Andric error.SetErrorString(toString(trace_or_err.takeError()).c_str()); 40*81ad6265SDimitry Andric return SBTrace(); 41*81ad6265SDimitry Andric } 42*81ad6265SDimitry Andric 43*81ad6265SDimitry Andric return SBTrace(trace_or_err.get()); 44*81ad6265SDimitry Andric } 45*81ad6265SDimitry Andric 46fe6060f1SDimitry Andric const char *SBTrace::GetStartConfigurationHelp() { 4704eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this); 4804eeddc0SDimitry Andric return m_opaque_sp ? m_opaque_sp->GetStartConfigurationHelp() : nullptr; 490b57cec5SDimitry Andric } 500b57cec5SDimitry Andric 51fe6060f1SDimitry Andric SBError SBTrace::Start(const SBStructuredData &configuration) { 5204eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, configuration); 53fe6060f1SDimitry Andric SBError error; 54fe6060f1SDimitry Andric if (!m_opaque_sp) 55fe6060f1SDimitry Andric error.SetErrorString("error: invalid trace"); 56fe6060f1SDimitry Andric else if (llvm::Error err = 57fe6060f1SDimitry Andric m_opaque_sp->Start(configuration.m_impl_up->GetObjectSP())) 58fe6060f1SDimitry Andric error.SetErrorString(llvm::toString(std::move(err)).c_str()); 5904eeddc0SDimitry Andric return error; 600b57cec5SDimitry Andric } 610b57cec5SDimitry Andric 62fe6060f1SDimitry Andric SBError SBTrace::Start(const SBThread &thread, 63fe6060f1SDimitry Andric const SBStructuredData &configuration) { 6404eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, thread, configuration); 650b57cec5SDimitry Andric 66fe6060f1SDimitry Andric SBError error; 67fe6060f1SDimitry Andric if (!m_opaque_sp) 68fe6060f1SDimitry Andric error.SetErrorString("error: invalid trace"); 69fe6060f1SDimitry Andric else { 70fe6060f1SDimitry Andric if (llvm::Error err = 71fe6060f1SDimitry Andric m_opaque_sp->Start(std::vector<lldb::tid_t>{thread.GetThreadID()}, 72fe6060f1SDimitry Andric configuration.m_impl_up->GetObjectSP())) 73fe6060f1SDimitry Andric error.SetErrorString(llvm::toString(std::move(err)).c_str()); 740b57cec5SDimitry Andric } 750b57cec5SDimitry Andric 7604eeddc0SDimitry Andric return error; 770b57cec5SDimitry Andric } 780b57cec5SDimitry Andric 79fe6060f1SDimitry Andric SBError SBTrace::Stop() { 8004eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this); 81fe6060f1SDimitry Andric SBError error; 82fe6060f1SDimitry Andric if (!m_opaque_sp) 83fe6060f1SDimitry Andric error.SetErrorString("error: invalid trace"); 84fe6060f1SDimitry Andric else if (llvm::Error err = m_opaque_sp->Stop()) 85fe6060f1SDimitry Andric error.SetErrorString(llvm::toString(std::move(err)).c_str()); 8604eeddc0SDimitry Andric return error; 870b57cec5SDimitry Andric } 880b57cec5SDimitry Andric 89fe6060f1SDimitry Andric SBError SBTrace::Stop(const SBThread &thread) { 9004eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, thread); 91fe6060f1SDimitry Andric SBError error; 92fe6060f1SDimitry Andric if (!m_opaque_sp) 93fe6060f1SDimitry Andric error.SetErrorString("error: invalid trace"); 94fe6060f1SDimitry Andric else if (llvm::Error err = m_opaque_sp->Stop({thread.GetThreadID()})) 95fe6060f1SDimitry Andric error.SetErrorString(llvm::toString(std::move(err)).c_str()); 9604eeddc0SDimitry Andric return error; 970b57cec5SDimitry Andric } 980b57cec5SDimitry Andric 990b57cec5SDimitry Andric bool SBTrace::IsValid() { 10004eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this); 1010b57cec5SDimitry Andric return this->operator bool(); 1020b57cec5SDimitry Andric } 103fe6060f1SDimitry Andric 1040b57cec5SDimitry Andric SBTrace::operator bool() const { 10504eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this); 106fe6060f1SDimitry Andric return (bool)m_opaque_sp; 1070b57cec5SDimitry Andric } 108