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 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 LLDB_RECORD_DUMMY(size_t, SBTrace, GetTraceData, 30 (lldb::SBError &, void *, size_t, size_t, lldb::tid_t), 31 error, buf, size, offset, thread_id); 32 33 ProcessSP process_sp(GetSP()); 34 llvm::MutableArrayRef<uint8_t> buffer(static_cast<uint8_t *>(buf), size); 35 error.Clear(); 36 37 if (!process_sp) { 38 error.SetErrorString("invalid process"); 39 } else { 40 error.SetError( 41 process_sp->GetData(GetTraceUID(), thread_id, buffer, offset)); 42 } 43 return buffer.size(); 44 } 45 46 size_t SBTrace::GetMetaData(SBError &error, void *buf, size_t size, 47 size_t offset, lldb::tid_t thread_id) { 48 LLDB_RECORD_DUMMY(size_t, SBTrace, GetMetaData, 49 (lldb::SBError &, void *, size_t, size_t, lldb::tid_t), 50 error, buf, size, offset, thread_id); 51 52 ProcessSP process_sp(GetSP()); 53 llvm::MutableArrayRef<uint8_t> buffer(static_cast<uint8_t *>(buf), size); 54 error.Clear(); 55 56 if (!process_sp) { 57 error.SetErrorString("invalid process"); 58 } else { 59 error.SetError( 60 process_sp->GetMetaData(GetTraceUID(), thread_id, buffer, offset)); 61 } 62 return buffer.size(); 63 } 64 65 void SBTrace::StopTrace(SBError &error, lldb::tid_t thread_id) { 66 LLDB_RECORD_METHOD(void, SBTrace, StopTrace, (lldb::SBError &, lldb::tid_t), 67 error, thread_id); 68 69 ProcessSP process_sp(GetSP()); 70 error.Clear(); 71 72 if (!process_sp) { 73 error.SetErrorString("invalid process"); 74 return; 75 } 76 error.SetError(process_sp->StopTrace(GetTraceUID(), thread_id)); 77 } 78 79 void SBTrace::GetTraceConfig(SBTraceOptions &options, SBError &error) { 80 LLDB_RECORD_METHOD(void, SBTrace, GetTraceConfig, 81 (lldb::SBTraceOptions &, lldb::SBError &), options, error); 82 83 ProcessSP process_sp(GetSP()); 84 error.Clear(); 85 86 if (!process_sp) { 87 error.SetErrorString("invalid process"); 88 } else { 89 error.SetError(process_sp->GetTraceConfig(GetTraceUID(), 90 *(options.m_traceoptions_sp))); 91 } 92 } 93 94 lldb::user_id_t SBTrace::GetTraceUID() { 95 LLDB_RECORD_METHOD_NO_ARGS(lldb::user_id_t, SBTrace, GetTraceUID); 96 97 if (m_trace_impl_sp) 98 return m_trace_impl_sp->uid; 99 return LLDB_INVALID_UID; 100 } 101 102 void SBTrace::SetTraceUID(lldb::user_id_t uid) { 103 if (m_trace_impl_sp) 104 m_trace_impl_sp->uid = uid; 105 } 106 107 SBTrace::SBTrace() { 108 LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBTrace); 109 110 m_trace_impl_sp = std::make_shared<TraceImpl>(); 111 if (m_trace_impl_sp) 112 m_trace_impl_sp->uid = LLDB_INVALID_UID; 113 } 114 115 void SBTrace::SetSP(const ProcessSP &process_sp) { m_opaque_wp = process_sp; } 116 117 bool SBTrace::IsValid() { 118 LLDB_RECORD_METHOD_NO_ARGS(bool, SBTrace, IsValid); 119 return this->operator bool(); 120 } 121 SBTrace::operator bool() const { 122 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBTrace, operator bool); 123 124 if (!m_trace_impl_sp) 125 return false; 126 if (!GetSP()) 127 return false; 128 return true; 129 } 130 131 namespace lldb_private { 132 namespace repro { 133 134 template <> 135 void RegisterMethods<SBTrace>(Registry &R) { 136 LLDB_REGISTER_METHOD(void, SBTrace, StopTrace, 137 (lldb::SBError &, lldb::tid_t)); 138 LLDB_REGISTER_METHOD(void, SBTrace, GetTraceConfig, 139 (lldb::SBTraceOptions &, lldb::SBError &)); 140 LLDB_REGISTER_METHOD(lldb::user_id_t, SBTrace, GetTraceUID, ()); 141 LLDB_REGISTER_CONSTRUCTOR(SBTrace, ()); 142 LLDB_REGISTER_METHOD(bool, SBTrace, IsValid, ()); 143 LLDB_REGISTER_METHOD_CONST(bool, SBTrace, operator bool, ()); 144 } 145 146 } 147 } 148