1 //===-- SBTrace.cpp ---------------------------------------------*- C++ -*-===//
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 "SBReproducerPrivate.h"
10 #include "lldb/Target/Process.h"
11 #include "lldb/Utility/Log.h"
12 
13 #include "lldb/API/SBTrace.h"
14 #include "lldb/API/SBTraceOptions.h"
15 
16 #include <memory>
17 
18 using namespace lldb;
19 using namespace lldb_private;
20 
21 class TraceImpl {
22 public:
23   lldb::user_id_t uid;
24 };
25 
26 lldb::ProcessSP SBTrace::GetSP() const { return m_opaque_wp.lock(); }
27 
28 size_t SBTrace::GetTraceData(SBError &error, void *buf, size_t size,
29                              size_t offset, lldb::tid_t thread_id) {
30   ProcessSP process_sp(GetSP());
31   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
32   llvm::MutableArrayRef<uint8_t> buffer(static_cast<uint8_t *>(buf), size);
33   error.Clear();
34 
35   if (!process_sp) {
36     error.SetErrorString("invalid process");
37   } else {
38     error.SetError(
39         process_sp->GetData(GetTraceUID(), thread_id, buffer, offset));
40     LLDB_LOG(log, "SBTrace::bytes_read - {0}", buffer.size());
41   }
42   return buffer.size();
43 }
44 
45 size_t SBTrace::GetMetaData(SBError &error, void *buf, size_t size,
46                             size_t offset, lldb::tid_t thread_id) {
47   ProcessSP process_sp(GetSP());
48   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
49   llvm::MutableArrayRef<uint8_t> buffer(static_cast<uint8_t *>(buf), size);
50   error.Clear();
51 
52   if (!process_sp) {
53     error.SetErrorString("invalid process");
54   } else {
55 
56     error.SetError(
57         process_sp->GetMetaData(GetTraceUID(), thread_id, buffer, offset));
58     LLDB_LOG(log, "SBTrace::bytes_read - {0}", buffer.size());
59   }
60   return buffer.size();
61 }
62 
63 void SBTrace::StopTrace(SBError &error, lldb::tid_t thread_id) {
64   LLDB_RECORD_METHOD(void, SBTrace, StopTrace, (lldb::SBError &, lldb::tid_t),
65                      error, thread_id);
66 
67   ProcessSP process_sp(GetSP());
68   error.Clear();
69 
70   if (!process_sp) {
71     error.SetErrorString("invalid process");
72     return;
73   }
74   error.SetError(process_sp->StopTrace(GetTraceUID(), thread_id));
75 }
76 
77 void SBTrace::GetTraceConfig(SBTraceOptions &options, SBError &error) {
78   LLDB_RECORD_METHOD(void, SBTrace, GetTraceConfig,
79                      (lldb::SBTraceOptions &, lldb::SBError &), options, error);
80 
81   ProcessSP process_sp(GetSP());
82   error.Clear();
83 
84   if (!process_sp) {
85     error.SetErrorString("invalid process");
86   } else {
87     error.SetError(process_sp->GetTraceConfig(GetTraceUID(),
88                                               *(options.m_traceoptions_sp)));
89   }
90 }
91 
92 lldb::user_id_t SBTrace::GetTraceUID() {
93   LLDB_RECORD_METHOD_NO_ARGS(lldb::user_id_t, SBTrace, GetTraceUID);
94 
95   if (m_trace_impl_sp)
96     return m_trace_impl_sp->uid;
97   return LLDB_INVALID_UID;
98 }
99 
100 void SBTrace::SetTraceUID(lldb::user_id_t uid) {
101   if (m_trace_impl_sp)
102     m_trace_impl_sp->uid = uid;
103 }
104 
105 SBTrace::SBTrace() {
106   LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBTrace);
107 
108   m_trace_impl_sp = std::make_shared<TraceImpl>();
109   if (m_trace_impl_sp)
110     m_trace_impl_sp->uid = LLDB_INVALID_UID;
111 }
112 
113 void SBTrace::SetSP(const ProcessSP &process_sp) { m_opaque_wp = process_sp; }
114 
115 bool SBTrace::IsValid() {
116   LLDB_RECORD_METHOD_NO_ARGS(bool, SBTrace, IsValid);
117 
118   if (!m_trace_impl_sp)
119     return false;
120   if (!GetSP())
121     return false;
122   return true;
123 }
124