1 //===-- PTDecoder.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 "PTDecoder.h"
10 #include "Decoder.h"
11 
12 using namespace ptdecoder;
13 using namespace ptdecoder_private;
14 
15 // PTInstruction class member functions definitions
PTInstruction(const std::shared_ptr<ptdecoder_private::Instruction> & ptr)16 PTInstruction::PTInstruction(
17     const std::shared_ptr<ptdecoder_private::Instruction> &ptr)
18     : m_opaque_sp(ptr) {}
19 
~PTInstruction()20 PTInstruction::~PTInstruction() {}
21 
GetInsnAddress() const22 uint64_t PTInstruction::GetInsnAddress() const {
23   return (m_opaque_sp ? m_opaque_sp->GetInsnAddress() : 0);
24 }
25 
GetRawBytes(void * buf,size_t size) const26 size_t PTInstruction::GetRawBytes(void *buf, size_t size) const {
27   return (m_opaque_sp ? m_opaque_sp->GetRawBytes(buf, size) : 0);
28 }
29 
GetError() const30 std::string PTInstruction::GetError() const {
31   return (m_opaque_sp ? m_opaque_sp->GetError() : "null pointer");
32 }
33 
GetSpeculative() const34 bool PTInstruction::GetSpeculative() const {
35   return (m_opaque_sp ? m_opaque_sp->GetSpeculative() : 0);
36 }
37 
38 // PTInstructionList class member functions definitions
GetSize() const39 size_t PTInstructionList::GetSize() const {
40   return (m_opaque_sp ? m_opaque_sp->GetSize() : 0);
41 }
42 
GetInstructionAtIndex(uint32_t idx)43 PTInstruction PTInstructionList::GetInstructionAtIndex(uint32_t idx) {
44   if (m_opaque_sp)
45     return PTInstruction(std::shared_ptr<ptdecoder_private::Instruction>(
46         new Instruction(m_opaque_sp->GetInstructionAtIndex(idx))));
47 
48   return PTInstruction(std::shared_ptr<ptdecoder_private::Instruction>(
49       new Instruction("invalid instruction")));
50 }
51 
SetSP(const std::shared_ptr<ptdecoder_private::InstructionList> & ptr)52 void PTInstructionList::SetSP(
53     const std::shared_ptr<ptdecoder_private::InstructionList> &ptr) {
54   m_opaque_sp = ptr;
55 }
Clear()56 void PTInstructionList::Clear() {
57   if (!m_opaque_sp)
58     return;
59   m_opaque_sp.reset();
60 }
61 
62 // PTTraceOptions class member functions definitions
GetType() const63 lldb::TraceType PTTraceOptions::GetType() const {
64   return (m_opaque_sp ? m_opaque_sp->getType()
65                       : lldb::TraceType::eTraceTypeNone);
66 }
67 
GetTraceBufferSize() const68 uint64_t PTTraceOptions::GetTraceBufferSize() const {
69   return (m_opaque_sp ? m_opaque_sp->getTraceBufferSize() : 0);
70 }
71 
GetMetaDataBufferSize() const72 uint64_t PTTraceOptions::GetMetaDataBufferSize() const {
73   return (m_opaque_sp ? m_opaque_sp->getMetaDataBufferSize() : 0);
74 }
75 
GetTraceParams(lldb::SBError & error)76 lldb::SBStructuredData PTTraceOptions::GetTraceParams(lldb::SBError &error) {
77   if (!m_opaque_sp)
78     error.SetErrorString("null pointer");
79   return (m_opaque_sp ? m_opaque_sp->getTraceParams(error)
80                       : lldb::SBStructuredData());
81 }
82 
SetSP(const std::shared_ptr<ptdecoder_private::TraceOptions> & ptr)83 void PTTraceOptions::SetSP(
84     const std::shared_ptr<ptdecoder_private::TraceOptions> &ptr) {
85   m_opaque_sp = ptr;
86 }
87 
88 // PTDecoder class member functions definitions
PTDecoder(lldb::SBDebugger & sbdebugger)89 PTDecoder::PTDecoder(lldb::SBDebugger &sbdebugger)
90     : m_opaque_sp(new ptdecoder_private::Decoder(sbdebugger)) {}
91 
StartProcessorTrace(lldb::SBProcess & sbprocess,lldb::SBTraceOptions & sbtraceoptions,lldb::SBError & sberror)92 void PTDecoder::StartProcessorTrace(lldb::SBProcess &sbprocess,
93                                     lldb::SBTraceOptions &sbtraceoptions,
94                                     lldb::SBError &sberror) {
95   if (m_opaque_sp == nullptr) {
96     sberror.SetErrorStringWithFormat("invalid PTDecoder instance");
97     return;
98   }
99 
100   m_opaque_sp->StartProcessorTrace(sbprocess, sbtraceoptions, sberror);
101 }
102 
StopProcessorTrace(lldb::SBProcess & sbprocess,lldb::SBError & sberror,lldb::tid_t tid)103 void PTDecoder::StopProcessorTrace(lldb::SBProcess &sbprocess,
104                                    lldb::SBError &sberror, lldb::tid_t tid) {
105   if (m_opaque_sp == nullptr) {
106     sberror.SetErrorStringWithFormat("invalid PTDecoder instance");
107     return;
108   }
109 
110   m_opaque_sp->StopProcessorTrace(sbprocess, sberror, tid);
111 }
112 
GetInstructionLogAtOffset(lldb::SBProcess & sbprocess,lldb::tid_t tid,uint32_t offset,uint32_t count,PTInstructionList & result_list,lldb::SBError & sberror)113 void PTDecoder::GetInstructionLogAtOffset(lldb::SBProcess &sbprocess,
114                                           lldb::tid_t tid, uint32_t offset,
115                                           uint32_t count,
116                                           PTInstructionList &result_list,
117                                           lldb::SBError &sberror) {
118   if (m_opaque_sp == nullptr) {
119     sberror.SetErrorStringWithFormat("invalid PTDecoder instance");
120     return;
121   }
122 
123   std::shared_ptr<ptdecoder_private::InstructionList> insn_list_ptr(
124       new InstructionList());
125   m_opaque_sp->GetInstructionLogAtOffset(sbprocess, tid, offset, count,
126                                          *insn_list_ptr, sberror);
127   if (!sberror.Success())
128     return;
129 
130   result_list.SetSP(insn_list_ptr);
131 }
132 
GetProcessorTraceInfo(lldb::SBProcess & sbprocess,lldb::tid_t tid,PTTraceOptions & options,lldb::SBError & sberror)133 void PTDecoder::GetProcessorTraceInfo(lldb::SBProcess &sbprocess,
134                                       lldb::tid_t tid, PTTraceOptions &options,
135                                       lldb::SBError &sberror) {
136   if (m_opaque_sp == nullptr) {
137     sberror.SetErrorStringWithFormat("invalid PTDecoder instance");
138     return;
139   }
140 
141   std::shared_ptr<ptdecoder_private::TraceOptions> trace_options_ptr(
142       new TraceOptions());
143   m_opaque_sp->GetProcessorTraceInfo(sbprocess, tid, *trace_options_ptr,
144                                      sberror);
145   if (!sberror.Success())
146     return;
147 
148   options.SetSP(trace_options_ptr);
149 }
150