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