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