LLDB mainline
SBTrace.cpp
Go to the documentation of this file.
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
11
12#include "lldb/API/SBDebugger.h"
14#include "lldb/API/SBThread.h"
15#include "lldb/API/SBTrace.h"
16
18
19#include <memory>
20
21using namespace lldb;
22using namespace lldb_private;
23using namespace llvm;
24
26
27SBTrace::SBTrace(const lldb::TraceSP &trace_sp) : m_opaque_sp(trace_sp) {
28 LLDB_INSTRUMENT_VA(this, trace_sp);
29}
30
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 = Status::FromErrorString(toString(trace_or_err.takeError()).c_str());
40 return SBTrace();
41 }
42
43 return SBTrace(trace_or_err.get());
44}
45
47 LLDB_INSTRUMENT_VA(this, error, thread);
48
49 if (!m_opaque_sp) {
50 error = Status::FromErrorString("error: invalid trace");
51 return SBTraceCursor();
52 }
53 if (!thread.get()) {
54 error = Status::FromErrorString("error: invalid thread");
55 return SBTraceCursor();
56 }
57
58 if (llvm::Expected<lldb::TraceCursorSP> trace_cursor_sp =
59 m_opaque_sp->CreateNewCursor(*thread.get())) {
60 return SBTraceCursor(std::move(*trace_cursor_sp));
61 } else {
63 llvm::toString(trace_cursor_sp.takeError()).c_str());
64 return SBTraceCursor();
65 }
66}
67
69 bool compact) {
70 LLDB_INSTRUMENT_VA(this, error, bundle_dir, compact);
71
72 error.Clear();
73 SBFileSpec file_spec;
74
75 if (!m_opaque_sp)
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);
80 else
81 error =
82 Status::FromErrorString(llvm::toString(desc_file.takeError()).c_str());
83
84 return file_spec;
85}
86
89 if (!m_opaque_sp)
90 return nullptr;
91
92 return ConstString(m_opaque_sp->GetStartConfigurationHelp()).GetCString();
93}
94
96 LLDB_INSTRUMENT_VA(this, configuration);
98 if (!m_opaque_sp)
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());
103 return error;
104}
105
107 const SBStructuredData &configuration) {
108 LLDB_INSTRUMENT_VA(this, thread, configuration);
109
111 if (!m_opaque_sp)
112 error = Status::FromErrorString("error: invalid trace");
113 else {
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());
118 }
119
120 return error;
121}
122
124 LLDB_INSTRUMENT_VA(this);
126 if (!m_opaque_sp)
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());
130 return error;
131}
132
134 LLDB_INSTRUMENT_VA(this, thread);
136 if (!m_opaque_sp)
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());
140 return error;
141}
142
144 LLDB_INSTRUMENT_VA(this);
145 return this->operator bool();
146}
147
148SBTrace::operator bool() const {
149 LLDB_INSTRUMENT_VA(this);
150 return (bool)m_opaque_sp;
151}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_INSTRUMENT_VA(...)
lldb_private::Debugger & ref() const
void SetFileSpec(const lldb_private::FileSpec &fspec)
Definition: SBFileSpec.cpp:164
const lldb_private::FileSpec & ref() const
Definition: SBFileSpec.cpp:162
StructuredDataImplUP m_impl_up
lldb_private::Thread * get()
Definition: SBThread.cpp:1352
bool IsValid()
Definition: SBTrace.cpp:143
const char * GetStartConfigurationHelp()
Definition: SBTrace.cpp:87
SBTrace()
Default constructor for an invalid Trace object.
Definition: SBTrace.cpp:25
SBError Start(const SBStructuredData &configuration)
Start tracing all current and future threads in a live process using a provided configuration.
Definition: SBTrace.cpp:95
SBTraceCursor CreateNewCursor(SBError &error, SBThread &thread)
Get a TraceCursor for the given thread's trace.
Definition: SBTrace.cpp:46
SBError Stop()
Stop tracing all threads in a live process.
Definition: SBTrace.cpp:123
static SBTrace LoadTraceFromFile(SBError &error, SBDebugger &debugger, const SBFileSpec &trace_description_file)
See SBDebugger::LoadTraceFromFile.
Definition: SBTrace.cpp:31
SBFileSpec SaveToDisk(SBError &error, const SBFileSpec &bundle_dir, bool compact=false)
Save the trace to the specified directory, which will be created if needed.
Definition: SBTrace.cpp:68
lldb::TraceSP m_opaque_sp
Definition: SBTrace.h:142
A uniqued constant string class.
Definition: ConstString.h:40
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:216
static Status FromErrorString(const char *str)
Definition: Status.h:141
static llvm::Expected< lldb::TraceSP > LoadPostMortemTraceFromFile(Debugger &debugger, const FileSpec &trace_description_file)
Load a trace from a trace description file and create Targets, Processes and Threads based on the con...
Definition: Trace.cpp:96
A class that represents a running process on the host machine.
const char * toString(AppleArm64ExceptionClass EC)
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::Trace > TraceSP
Definition: lldb-forward.h:458
Definition: Debugger.h:54