xref: /freebsd/contrib/llvm-project/lldb/source/API/SBTrace.cpp (revision 753f127f3ace09432b2baeffd71a308760641a62)
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 
1281ad6265SDimitry 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;
2381ad6265SDimitry 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 
3181ad6265SDimitry Andric SBTrace SBTrace::LoadTraceFromFile(SBError &error, SBDebugger &debugger,
3281ad6265SDimitry Andric                                    const SBFileSpec &trace_description_file) {
3381ad6265SDimitry Andric   LLDB_INSTRUMENT_VA(error, debugger, trace_description_file);
3481ad6265SDimitry Andric 
3581ad6265SDimitry Andric   Expected<lldb::TraceSP> trace_or_err = Trace::LoadPostMortemTraceFromFile(
3681ad6265SDimitry Andric       debugger.ref(), trace_description_file.ref());
3781ad6265SDimitry Andric 
3881ad6265SDimitry Andric   if (!trace_or_err) {
3981ad6265SDimitry Andric     error.SetErrorString(toString(trace_or_err.takeError()).c_str());
4081ad6265SDimitry Andric     return SBTrace();
4181ad6265SDimitry Andric   }
4281ad6265SDimitry Andric 
4381ad6265SDimitry Andric   return SBTrace(trace_or_err.get());
4481ad6265SDimitry Andric }
4581ad6265SDimitry Andric 
46*753f127fSDimitry Andric SBFileSpec SBTrace::SaveToDisk(SBError &error, const SBFileSpec &bundle_dir,
47*753f127fSDimitry Andric                                bool compact) {
48*753f127fSDimitry Andric   LLDB_INSTRUMENT_VA(this, error, bundle_dir, compact);
49*753f127fSDimitry Andric 
50*753f127fSDimitry Andric   error.Clear();
51*753f127fSDimitry Andric   SBFileSpec file_spec;
52*753f127fSDimitry Andric 
53*753f127fSDimitry Andric   if (!m_opaque_sp)
54*753f127fSDimitry Andric     error.SetErrorString("error: invalid trace");
55*753f127fSDimitry Andric   else if (Expected<FileSpec> desc_file =
56*753f127fSDimitry Andric                m_opaque_sp->SaveToDisk(bundle_dir.ref(), compact))
57*753f127fSDimitry Andric     file_spec.SetFileSpec(*desc_file);
58*753f127fSDimitry Andric   else
59*753f127fSDimitry Andric     error.SetErrorString(llvm::toString(desc_file.takeError()).c_str());
60*753f127fSDimitry Andric 
61*753f127fSDimitry Andric   return file_spec;
62*753f127fSDimitry Andric }
63*753f127fSDimitry Andric 
64fe6060f1SDimitry Andric const char *SBTrace::GetStartConfigurationHelp() {
6504eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
6604eeddc0SDimitry Andric   return m_opaque_sp ? m_opaque_sp->GetStartConfigurationHelp() : nullptr;
670b57cec5SDimitry Andric }
680b57cec5SDimitry Andric 
69fe6060f1SDimitry Andric SBError SBTrace::Start(const SBStructuredData &configuration) {
7004eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, configuration);
71fe6060f1SDimitry Andric   SBError error;
72fe6060f1SDimitry Andric   if (!m_opaque_sp)
73fe6060f1SDimitry Andric     error.SetErrorString("error: invalid trace");
74fe6060f1SDimitry Andric   else if (llvm::Error err =
75fe6060f1SDimitry Andric                m_opaque_sp->Start(configuration.m_impl_up->GetObjectSP()))
76fe6060f1SDimitry Andric     error.SetErrorString(llvm::toString(std::move(err)).c_str());
7704eeddc0SDimitry Andric   return error;
780b57cec5SDimitry Andric }
790b57cec5SDimitry Andric 
80fe6060f1SDimitry Andric SBError SBTrace::Start(const SBThread &thread,
81fe6060f1SDimitry Andric                        const SBStructuredData &configuration) {
8204eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, thread, configuration);
830b57cec5SDimitry Andric 
84fe6060f1SDimitry Andric   SBError error;
85fe6060f1SDimitry Andric   if (!m_opaque_sp)
86fe6060f1SDimitry Andric     error.SetErrorString("error: invalid trace");
87fe6060f1SDimitry Andric   else {
88fe6060f1SDimitry Andric     if (llvm::Error err =
89fe6060f1SDimitry Andric             m_opaque_sp->Start(std::vector<lldb::tid_t>{thread.GetThreadID()},
90fe6060f1SDimitry Andric                                configuration.m_impl_up->GetObjectSP()))
91fe6060f1SDimitry Andric       error.SetErrorString(llvm::toString(std::move(err)).c_str());
920b57cec5SDimitry Andric   }
930b57cec5SDimitry Andric 
9404eeddc0SDimitry Andric   return error;
950b57cec5SDimitry Andric }
960b57cec5SDimitry Andric 
97fe6060f1SDimitry Andric SBError SBTrace::Stop() {
9804eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
99fe6060f1SDimitry Andric   SBError error;
100fe6060f1SDimitry Andric   if (!m_opaque_sp)
101fe6060f1SDimitry Andric     error.SetErrorString("error: invalid trace");
102fe6060f1SDimitry Andric   else if (llvm::Error err = m_opaque_sp->Stop())
103fe6060f1SDimitry Andric     error.SetErrorString(llvm::toString(std::move(err)).c_str());
10404eeddc0SDimitry Andric   return error;
1050b57cec5SDimitry Andric }
1060b57cec5SDimitry Andric 
107fe6060f1SDimitry Andric SBError SBTrace::Stop(const SBThread &thread) {
10804eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, thread);
109fe6060f1SDimitry Andric   SBError error;
110fe6060f1SDimitry Andric   if (!m_opaque_sp)
111fe6060f1SDimitry Andric     error.SetErrorString("error: invalid trace");
112fe6060f1SDimitry Andric   else if (llvm::Error err = m_opaque_sp->Stop({thread.GetThreadID()}))
113fe6060f1SDimitry Andric     error.SetErrorString(llvm::toString(std::move(err)).c_str());
11404eeddc0SDimitry Andric   return error;
1150b57cec5SDimitry Andric }
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric bool SBTrace::IsValid() {
11804eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
1190b57cec5SDimitry Andric   return this->operator bool();
1200b57cec5SDimitry Andric }
121fe6060f1SDimitry Andric 
1220b57cec5SDimitry Andric SBTrace::operator bool() const {
12304eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
124fe6060f1SDimitry Andric   return (bool)m_opaque_sp;
1250b57cec5SDimitry Andric }
126