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