summaryrefslogtreecommitdiff
path: root/gnu/llvm/lldb/source/API/SBTrace.cpp
blob: 5e9d53e3d18639597c7f8afd1499c664c5e25a54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//===-- SBTrace.cpp -------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "lldb/Target/Process.h"
#include "lldb/Utility/Instrumentation.h"

#include "lldb/API/SBDebugger.h"
#include "lldb/API/SBStructuredData.h"
#include "lldb/API/SBThread.h"
#include "lldb/API/SBTrace.h"

#include "lldb/Core/StructuredDataImpl.h"

#include <memory>

using namespace lldb;
using namespace lldb_private;
using namespace llvm;

SBTrace::SBTrace() { LLDB_INSTRUMENT_VA(this); }

SBTrace::SBTrace(const lldb::TraceSP &trace_sp) : m_opaque_sp(trace_sp) {
  LLDB_INSTRUMENT_VA(this, trace_sp);
}

SBTrace SBTrace::LoadTraceFromFile(SBError &error, SBDebugger &debugger,
                                   const SBFileSpec &trace_description_file) {
  LLDB_INSTRUMENT_VA(error, debugger, trace_description_file);

  Expected<lldb::TraceSP> trace_or_err = Trace::LoadPostMortemTraceFromFile(
      debugger.ref(), trace_description_file.ref());

  if (!trace_or_err) {
    error.SetErrorString(toString(trace_or_err.takeError()).c_str());
    return SBTrace();
  }

  return SBTrace(trace_or_err.get());
}

SBTraceCursor SBTrace::CreateNewCursor(SBError &error, SBThread &thread) {
  LLDB_INSTRUMENT_VA(this, error, thread);

  if (!m_opaque_sp) {
    error.SetErrorString("error: invalid trace");
    return SBTraceCursor();
  }
  if (!thread.get()) {
    error.SetErrorString("error: invalid thread");
    return SBTraceCursor();
  }

  if (llvm::Expected<lldb::TraceCursorSP> trace_cursor_sp =
          m_opaque_sp->CreateNewCursor(*thread.get())) {
    return SBTraceCursor(std::move(*trace_cursor_sp));
  } else {
    error.SetErrorString(llvm::toString(trace_cursor_sp.takeError()).c_str());
    return SBTraceCursor();
  }
}

SBFileSpec SBTrace::SaveToDisk(SBError &error, const SBFileSpec &bundle_dir,
                               bool compact) {
  LLDB_INSTRUMENT_VA(this, error, bundle_dir, compact);

  error.Clear();
  SBFileSpec file_spec;

  if (!m_opaque_sp)
    error.SetErrorString("error: invalid trace");
  else if (Expected<FileSpec> desc_file =
               m_opaque_sp->SaveToDisk(bundle_dir.ref(), compact))
    file_spec.SetFileSpec(*desc_file);
  else
    error.SetErrorString(llvm::toString(desc_file.takeError()).c_str());

  return file_spec;
}

const char *SBTrace::GetStartConfigurationHelp() {
  LLDB_INSTRUMENT_VA(this);
  return m_opaque_sp ? m_opaque_sp->GetStartConfigurationHelp() : nullptr;
}

SBError SBTrace::Start(const SBStructuredData &configuration) {
  LLDB_INSTRUMENT_VA(this, configuration);
  SBError error;
  if (!m_opaque_sp)
    error.SetErrorString("error: invalid trace");
  else if (llvm::Error err =
               m_opaque_sp->Start(configuration.m_impl_up->GetObjectSP()))
    error.SetErrorString(llvm::toString(std::move(err)).c_str());
  return error;
}

SBError SBTrace::Start(const SBThread &thread,
                       const SBStructuredData &configuration) {
  LLDB_INSTRUMENT_VA(this, thread, configuration);

  SBError error;
  if (!m_opaque_sp)
    error.SetErrorString("error: invalid trace");
  else {
    if (llvm::Error err =
            m_opaque_sp->Start(std::vector<lldb::tid_t>{thread.GetThreadID()},
                               configuration.m_impl_up->GetObjectSP()))
      error.SetErrorString(llvm::toString(std::move(err)).c_str());
  }

  return error;
}

SBError SBTrace::Stop() {
  LLDB_INSTRUMENT_VA(this);
  SBError error;
  if (!m_opaque_sp)
    error.SetErrorString("error: invalid trace");
  else if (llvm::Error err = m_opaque_sp->Stop())
    error.SetErrorString(llvm::toString(std::move(err)).c_str());
  return error;
}

SBError SBTrace::Stop(const SBThread &thread) {
  LLDB_INSTRUMENT_VA(this, thread);
  SBError error;
  if (!m_opaque_sp)
    error.SetErrorString("error: invalid trace");
  else if (llvm::Error err = m_opaque_sp->Stop({thread.GetThreadID()}))
    error.SetErrorString(llvm::toString(std::move(err)).c_str());
  return error;
}

bool SBTrace::IsValid() {
  LLDB_INSTRUMENT_VA(this);
  return this->operator bool();
}

SBTrace::operator bool() const {
  LLDB_INSTRUMENT_VA(this);
  return (bool)m_opaque_sp;
}