1 //===-- SBTrace.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 "lldb/Target/Process.h" 10 #include "lldb/Utility/Instrumentation.h" 11 12 #include "lldb/API/SBDebugger.h" 13 #include "lldb/API/SBStructuredData.h" 14 #include "lldb/API/SBThread.h" 15 #include "lldb/API/SBTrace.h" 16 17 #include "lldb/Core/StructuredDataImpl.h" 18 19 #include <memory> 20 21 using namespace lldb; 22 using namespace lldb_private; 23 using namespace llvm; 24 25 SBTrace::SBTrace() { LLDB_INSTRUMENT_VA(this); } 26 27 SBTrace::SBTrace(const lldb::TraceSP &trace_sp) : m_opaque_sp(trace_sp) { 28 LLDB_INSTRUMENT_VA(this, trace_sp); 29 } 30 31 SBTrace SBTrace::LoadTraceFromFile(SBError &error, SBDebugger &debugger, 32 const SBFileSpec &trace_description_file) { 33 LLDB_INSTRUMENT_VA(error, debugger, trace_description_file); 34 35 Expected<lldb::TraceSP> trace_or_err = Trace::LoadPostMortemTraceFromFile( 36 debugger.ref(), trace_description_file.ref()); 37 38 if (!trace_or_err) { 39 error.SetErrorString(toString(trace_or_err.takeError()).c_str()); 40 return SBTrace(); 41 } 42 43 return SBTrace(trace_or_err.get()); 44 } 45 46 SBFileSpec SBTrace::SaveToDisk(SBError &error, const SBFileSpec &bundle_dir, 47 bool compact) { 48 LLDB_INSTRUMENT_VA(this, error, bundle_dir, compact); 49 50 error.Clear(); 51 SBFileSpec file_spec; 52 53 if (!m_opaque_sp) 54 error.SetErrorString("error: invalid trace"); 55 else if (Expected<FileSpec> desc_file = 56 m_opaque_sp->SaveToDisk(bundle_dir.ref(), compact)) 57 file_spec.SetFileSpec(*desc_file); 58 else 59 error.SetErrorString(llvm::toString(desc_file.takeError()).c_str()); 60 61 return file_spec; 62 } 63 64 const char *SBTrace::GetStartConfigurationHelp() { 65 LLDB_INSTRUMENT_VA(this); 66 return m_opaque_sp ? m_opaque_sp->GetStartConfigurationHelp() : nullptr; 67 } 68 69 SBError SBTrace::Start(const SBStructuredData &configuration) { 70 LLDB_INSTRUMENT_VA(this, configuration); 71 SBError error; 72 if (!m_opaque_sp) 73 error.SetErrorString("error: invalid trace"); 74 else if (llvm::Error err = 75 m_opaque_sp->Start(configuration.m_impl_up->GetObjectSP())) 76 error.SetErrorString(llvm::toString(std::move(err)).c_str()); 77 return error; 78 } 79 80 SBError SBTrace::Start(const SBThread &thread, 81 const SBStructuredData &configuration) { 82 LLDB_INSTRUMENT_VA(this, thread, configuration); 83 84 SBError error; 85 if (!m_opaque_sp) 86 error.SetErrorString("error: invalid trace"); 87 else { 88 if (llvm::Error err = 89 m_opaque_sp->Start(std::vector<lldb::tid_t>{thread.GetThreadID()}, 90 configuration.m_impl_up->GetObjectSP())) 91 error.SetErrorString(llvm::toString(std::move(err)).c_str()); 92 } 93 94 return error; 95 } 96 97 SBError SBTrace::Stop() { 98 LLDB_INSTRUMENT_VA(this); 99 SBError error; 100 if (!m_opaque_sp) 101 error.SetErrorString("error: invalid trace"); 102 else if (llvm::Error err = m_opaque_sp->Stop()) 103 error.SetErrorString(llvm::toString(std::move(err)).c_str()); 104 return error; 105 } 106 107 SBError SBTrace::Stop(const SBThread &thread) { 108 LLDB_INSTRUMENT_VA(this, thread); 109 SBError error; 110 if (!m_opaque_sp) 111 error.SetErrorString("error: invalid trace"); 112 else if (llvm::Error err = m_opaque_sp->Stop({thread.GetThreadID()})) 113 error.SetErrorString(llvm::toString(std::move(err)).c_str()); 114 return error; 115 } 116 117 bool SBTrace::IsValid() { 118 LLDB_INSTRUMENT_VA(this); 119 return this->operator bool(); 120 } 121 122 SBTrace::operator bool() const { 123 LLDB_INSTRUMENT_VA(this); 124 return (bool)m_opaque_sp; 125 } 126