1 //===-- SBTrace.cpp -------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Target/Process.h"
10 #include "lldb/Utility/Instrumentation.h"
12 #include "lldb/API/SBDebugger.h"
13 #include "lldb/API/SBStructuredData.h"
14 #include "lldb/API/SBThread.h"
15 #include "lldb/API/SBTrace.h"
17 #include "lldb/Core/StructuredDataImpl.h"
22 using namespace lldb_private
;
25 SBTrace::SBTrace() { LLDB_INSTRUMENT_VA(this); }
27 SBTrace::SBTrace(const lldb::TraceSP
&trace_sp
) : m_opaque_sp(trace_sp
) {
28 LLDB_INSTRUMENT_VA(this, trace_sp
);
31 SBTrace
SBTrace::LoadTraceFromFile(SBError
&error
, SBDebugger
&debugger
,
32 const SBFileSpec
&trace_description_file
) {
33 LLDB_INSTRUMENT_VA(error
, debugger
, trace_description_file
);
35 Expected
<lldb::TraceSP
> trace_or_err
= Trace::LoadPostMortemTraceFromFile(
36 debugger
.ref(), trace_description_file
.ref());
39 error
= Status::FromErrorString(toString(trace_or_err
.takeError()).c_str());
43 return SBTrace(trace_or_err
.get());
46 SBTraceCursor
SBTrace::CreateNewCursor(SBError
&error
, SBThread
&thread
) {
47 LLDB_INSTRUMENT_VA(this, error
, thread
);
50 error
= Status::FromErrorString("error: invalid trace");
51 return SBTraceCursor();
54 error
= Status::FromErrorString("error: invalid thread");
55 return SBTraceCursor();
58 if (llvm::Expected
<lldb::TraceCursorSP
> trace_cursor_sp
=
59 m_opaque_sp
->CreateNewCursor(*thread
.get())) {
60 return SBTraceCursor(std::move(*trace_cursor_sp
));
62 error
= Status::FromErrorString(
63 llvm::toString(trace_cursor_sp
.takeError()).c_str());
64 return SBTraceCursor();
68 SBFileSpec
SBTrace::SaveToDisk(SBError
&error
, const SBFileSpec
&bundle_dir
,
70 LLDB_INSTRUMENT_VA(this, error
, bundle_dir
, compact
);
76 error
= Status::FromErrorString("error: invalid trace");
77 else if (Expected
<FileSpec
> desc_file
=
78 m_opaque_sp
->SaveToDisk(bundle_dir
.ref(), compact
))
79 file_spec
.SetFileSpec(*desc_file
);
82 Status::FromErrorString(llvm::toString(desc_file
.takeError()).c_str());
87 const char *SBTrace::GetStartConfigurationHelp() {
88 LLDB_INSTRUMENT_VA(this);
92 return ConstString(m_opaque_sp
->GetStartConfigurationHelp()).GetCString();
95 SBError
SBTrace::Start(const SBStructuredData
&configuration
) {
96 LLDB_INSTRUMENT_VA(this, configuration
);
99 error
= Status::FromErrorString("error: invalid trace");
100 else if (llvm::Error err
=
101 m_opaque_sp
->Start(configuration
.m_impl_up
->GetObjectSP()))
102 error
= Status::FromErrorString(llvm::toString(std::move(err
)).c_str());
106 SBError
SBTrace::Start(const SBThread
&thread
,
107 const SBStructuredData
&configuration
) {
108 LLDB_INSTRUMENT_VA(this, thread
, configuration
);
112 error
= Status::FromErrorString("error: invalid trace");
114 if (llvm::Error err
=
115 m_opaque_sp
->Start(std::vector
<lldb::tid_t
>{thread
.GetThreadID()},
116 configuration
.m_impl_up
->GetObjectSP()))
117 error
= Status::FromErrorString(llvm::toString(std::move(err
)).c_str());
123 SBError
SBTrace::Stop() {
124 LLDB_INSTRUMENT_VA(this);
127 error
= Status::FromErrorString("error: invalid trace");
128 else if (llvm::Error err
= m_opaque_sp
->Stop())
129 error
= Status::FromErrorString(llvm::toString(std::move(err
)).c_str());
133 SBError
SBTrace::Stop(const SBThread
&thread
) {
134 LLDB_INSTRUMENT_VA(this, thread
);
137 error
= Status::FromErrorString("error: invalid trace");
138 else if (llvm::Error err
= m_opaque_sp
->Stop({thread
.GetThreadID()}))
139 error
= Status::FromErrorString(llvm::toString(std::move(err
)).c_str());
143 bool SBTrace::IsValid() {
144 LLDB_INSTRUMENT_VA(this);
145 return this->operator bool();
148 SBTrace::operator bool() const {
149 LLDB_INSTRUMENT_VA(this);
150 return (bool)m_opaque_sp
;