xref: /freebsd/contrib/llvm-project/lldb/source/API/SBTrace.cpp (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
1*5ffd83dbSDimitry Andric //===-- SBTrace.cpp -------------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "SBReproducerPrivate.h"
100b57cec5SDimitry Andric #include "lldb/Target/Process.h"
110b57cec5SDimitry Andric 
120b57cec5SDimitry Andric #include "lldb/API/SBTrace.h"
130b57cec5SDimitry Andric #include "lldb/API/SBTraceOptions.h"
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric #include <memory>
160b57cec5SDimitry Andric 
170b57cec5SDimitry Andric using namespace lldb;
180b57cec5SDimitry Andric using namespace lldb_private;
190b57cec5SDimitry Andric 
200b57cec5SDimitry Andric class TraceImpl {
210b57cec5SDimitry Andric public:
220b57cec5SDimitry Andric   lldb::user_id_t uid;
230b57cec5SDimitry Andric };
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric lldb::ProcessSP SBTrace::GetSP() const { return m_opaque_wp.lock(); }
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric size_t SBTrace::GetTraceData(SBError &error, void *buf, size_t size,
280b57cec5SDimitry Andric                              size_t offset, lldb::tid_t thread_id) {
290b57cec5SDimitry Andric   LLDB_RECORD_DUMMY(size_t, SBTrace, GetTraceData,
300b57cec5SDimitry Andric                     (lldb::SBError &, void *, size_t, size_t, lldb::tid_t),
310b57cec5SDimitry Andric                     error, buf, size, offset, thread_id);
320b57cec5SDimitry Andric 
330b57cec5SDimitry Andric   ProcessSP process_sp(GetSP());
340b57cec5SDimitry Andric   llvm::MutableArrayRef<uint8_t> buffer(static_cast<uint8_t *>(buf), size);
350b57cec5SDimitry Andric   error.Clear();
360b57cec5SDimitry Andric 
370b57cec5SDimitry Andric   if (!process_sp) {
380b57cec5SDimitry Andric     error.SetErrorString("invalid process");
390b57cec5SDimitry Andric   } else {
400b57cec5SDimitry Andric     error.SetError(
410b57cec5SDimitry Andric         process_sp->GetData(GetTraceUID(), thread_id, buffer, offset));
420b57cec5SDimitry Andric   }
430b57cec5SDimitry Andric   return buffer.size();
440b57cec5SDimitry Andric }
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric size_t SBTrace::GetMetaData(SBError &error, void *buf, size_t size,
470b57cec5SDimitry Andric                             size_t offset, lldb::tid_t thread_id) {
480b57cec5SDimitry Andric   LLDB_RECORD_DUMMY(size_t, SBTrace, GetMetaData,
490b57cec5SDimitry Andric                     (lldb::SBError &, void *, size_t, size_t, lldb::tid_t),
500b57cec5SDimitry Andric                     error, buf, size, offset, thread_id);
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric   ProcessSP process_sp(GetSP());
530b57cec5SDimitry Andric   llvm::MutableArrayRef<uint8_t> buffer(static_cast<uint8_t *>(buf), size);
540b57cec5SDimitry Andric   error.Clear();
550b57cec5SDimitry Andric 
560b57cec5SDimitry Andric   if (!process_sp) {
570b57cec5SDimitry Andric     error.SetErrorString("invalid process");
580b57cec5SDimitry Andric   } else {
590b57cec5SDimitry Andric     error.SetError(
600b57cec5SDimitry Andric         process_sp->GetMetaData(GetTraceUID(), thread_id, buffer, offset));
610b57cec5SDimitry Andric   }
620b57cec5SDimitry Andric   return buffer.size();
630b57cec5SDimitry Andric }
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric void SBTrace::StopTrace(SBError &error, lldb::tid_t thread_id) {
660b57cec5SDimitry Andric   LLDB_RECORD_METHOD(void, SBTrace, StopTrace, (lldb::SBError &, lldb::tid_t),
670b57cec5SDimitry Andric                      error, thread_id);
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric   ProcessSP process_sp(GetSP());
700b57cec5SDimitry Andric   error.Clear();
710b57cec5SDimitry Andric 
720b57cec5SDimitry Andric   if (!process_sp) {
730b57cec5SDimitry Andric     error.SetErrorString("invalid process");
740b57cec5SDimitry Andric     return;
750b57cec5SDimitry Andric   }
760b57cec5SDimitry Andric   error.SetError(process_sp->StopTrace(GetTraceUID(), thread_id));
770b57cec5SDimitry Andric }
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric void SBTrace::GetTraceConfig(SBTraceOptions &options, SBError &error) {
800b57cec5SDimitry Andric   LLDB_RECORD_METHOD(void, SBTrace, GetTraceConfig,
810b57cec5SDimitry Andric                      (lldb::SBTraceOptions &, lldb::SBError &), options, error);
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric   ProcessSP process_sp(GetSP());
840b57cec5SDimitry Andric   error.Clear();
850b57cec5SDimitry Andric 
860b57cec5SDimitry Andric   if (!process_sp) {
870b57cec5SDimitry Andric     error.SetErrorString("invalid process");
880b57cec5SDimitry Andric   } else {
890b57cec5SDimitry Andric     error.SetError(process_sp->GetTraceConfig(GetTraceUID(),
900b57cec5SDimitry Andric                                               *(options.m_traceoptions_sp)));
910b57cec5SDimitry Andric   }
920b57cec5SDimitry Andric }
930b57cec5SDimitry Andric 
940b57cec5SDimitry Andric lldb::user_id_t SBTrace::GetTraceUID() {
950b57cec5SDimitry Andric   LLDB_RECORD_METHOD_NO_ARGS(lldb::user_id_t, SBTrace, GetTraceUID);
960b57cec5SDimitry Andric 
970b57cec5SDimitry Andric   if (m_trace_impl_sp)
980b57cec5SDimitry Andric     return m_trace_impl_sp->uid;
990b57cec5SDimitry Andric   return LLDB_INVALID_UID;
1000b57cec5SDimitry Andric }
1010b57cec5SDimitry Andric 
1020b57cec5SDimitry Andric void SBTrace::SetTraceUID(lldb::user_id_t uid) {
1030b57cec5SDimitry Andric   if (m_trace_impl_sp)
1040b57cec5SDimitry Andric     m_trace_impl_sp->uid = uid;
1050b57cec5SDimitry Andric }
1060b57cec5SDimitry Andric 
1070b57cec5SDimitry Andric SBTrace::SBTrace() {
1080b57cec5SDimitry Andric   LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBTrace);
1090b57cec5SDimitry Andric 
1100b57cec5SDimitry Andric   m_trace_impl_sp = std::make_shared<TraceImpl>();
1110b57cec5SDimitry Andric   if (m_trace_impl_sp)
1120b57cec5SDimitry Andric     m_trace_impl_sp->uid = LLDB_INVALID_UID;
1130b57cec5SDimitry Andric }
1140b57cec5SDimitry Andric 
1150b57cec5SDimitry Andric void SBTrace::SetSP(const ProcessSP &process_sp) { m_opaque_wp = process_sp; }
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric bool SBTrace::IsValid() {
1180b57cec5SDimitry Andric   LLDB_RECORD_METHOD_NO_ARGS(bool, SBTrace, IsValid);
1190b57cec5SDimitry Andric   return this->operator bool();
1200b57cec5SDimitry Andric }
1210b57cec5SDimitry Andric SBTrace::operator bool() const {
1220b57cec5SDimitry Andric   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBTrace, operator bool);
1230b57cec5SDimitry Andric 
1240b57cec5SDimitry Andric   if (!m_trace_impl_sp)
1250b57cec5SDimitry Andric     return false;
1260b57cec5SDimitry Andric   if (!GetSP())
1270b57cec5SDimitry Andric     return false;
1280b57cec5SDimitry Andric   return true;
1290b57cec5SDimitry Andric }
1300b57cec5SDimitry Andric 
1310b57cec5SDimitry Andric namespace lldb_private {
1320b57cec5SDimitry Andric namespace repro {
1330b57cec5SDimitry Andric 
1340b57cec5SDimitry Andric template <>
1350b57cec5SDimitry Andric void RegisterMethods<SBTrace>(Registry &R) {
1360b57cec5SDimitry Andric   LLDB_REGISTER_METHOD(void, SBTrace, StopTrace,
1370b57cec5SDimitry Andric                        (lldb::SBError &, lldb::tid_t));
1380b57cec5SDimitry Andric   LLDB_REGISTER_METHOD(void, SBTrace, GetTraceConfig,
1390b57cec5SDimitry Andric                        (lldb::SBTraceOptions &, lldb::SBError &));
1400b57cec5SDimitry Andric   LLDB_REGISTER_METHOD(lldb::user_id_t, SBTrace, GetTraceUID, ());
1410b57cec5SDimitry Andric   LLDB_REGISTER_CONSTRUCTOR(SBTrace, ());
1420b57cec5SDimitry Andric   LLDB_REGISTER_METHOD(bool, SBTrace, IsValid, ());
1430b57cec5SDimitry Andric   LLDB_REGISTER_METHOD_CONST(bool, SBTrace, operator bool, ());
1440b57cec5SDimitry Andric }
1450b57cec5SDimitry Andric 
1460b57cec5SDimitry Andric }
1470b57cec5SDimitry Andric }
148