1 //===-- ProcessorTrace.h -------------------------------------- -*- 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 #ifndef liblldb_ProcessorTrace_H_
10 #define liblldb_ProcessorTrace_H_
11 
12 #include "lldb/Utility/Status.h"
13 #include "lldb/Utility/TraceOptions.h"
14 #include "lldb/lldb-types.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/DenseSet.h"
17 
18 #include <linux/perf_event.h>
19 #include <sys/mman.h>
20 #include <unistd.h>
21 
22 namespace lldb_private {
23 
24 namespace process_linux {
25 
26 // This class keeps track of one tracing instance of
27 // Intel(R) Processor Trace on Linux OS. There is a map keeping track
28 // of different tracing instances on each thread, which enables trace
29 // gathering on a per thread level.
30 //
31 // The tracing instance is linked with a trace id. The trace id acts like
32 // a key to the tracing instance and trace manipulations could be
33 // performed using the trace id.
34 //
35 // The trace id could map to trace instances for a group of threads
36 // (spanning to all the threads in the process) or a single thread.
37 // The kernel interface for us is the perf_event_open.
38 
39 class ProcessorTraceMonitor;
40 typedef std::unique_ptr<ProcessorTraceMonitor> ProcessorTraceMonitorUP;
41 
42 class ProcessorTraceMonitor {
43 
44   class munmap_delete {
45     size_t m_length;
46 
47   public:
munmap_delete(size_t length)48     munmap_delete(size_t length) : m_length(length) {}
operator()49     void operator()(void *ptr) {
50       if (m_length)
51         munmap(ptr, m_length);
52     }
53   };
54 
55   class file_close {
56 
57   public:
58     file_close() = default;
operator()59     void operator()(int *ptr) {
60       if (ptr == nullptr)
61         return;
62       if (*ptr == -1)
63         return;
64       close(*ptr);
65       std::default_delete<int>()(ptr);
66     }
67   };
68 
69   std::unique_ptr<perf_event_mmap_page, munmap_delete> m_mmap_meta;
70   std::unique_ptr<uint8_t, munmap_delete> m_mmap_aux;
71   std::unique_ptr<int, file_close> m_fd;
72 
73   // perf_event_mmap_page *m_mmap_base;
74   lldb::user_id_t m_traceid;
75   lldb::tid_t m_thread_id;
76 
77   // Counter to track trace instances.
78   static lldb::user_id_t m_trace_num;
79 
SetTraceID(lldb::user_id_t traceid)80   void SetTraceID(lldb::user_id_t traceid) { m_traceid = traceid; }
81 
82   Status StartTrace(lldb::pid_t pid, lldb::tid_t tid,
83                     const TraceOptions &config);
84 
85   llvm::MutableArrayRef<uint8_t> GetAuxBuffer();
86   llvm::MutableArrayRef<uint8_t> GetDataBuffer();
87 
ProcessorTraceMonitor()88   ProcessorTraceMonitor()
89       : m_mmap_meta(nullptr, munmap_delete(0)),
90         m_mmap_aux(nullptr, munmap_delete(0)), m_fd(nullptr, file_close()),
91         m_traceid(LLDB_INVALID_UID), m_thread_id(LLDB_INVALID_THREAD_ID){};
92 
SetThreadID(lldb::tid_t tid)93   void SetThreadID(lldb::tid_t tid) { m_thread_id = tid; }
94 
95 public:
96   static llvm::Expected<uint32_t> GetOSEventType();
97 
98   static bool IsSupported();
99 
100   static Status GetCPUType(TraceOptions &config);
101 
102   static llvm::Expected<ProcessorTraceMonitorUP>
103   Create(lldb::pid_t pid, lldb::tid_t tid, const TraceOptions &config,
104          bool useProcessSettings);
105 
106   Status ReadPerfTraceAux(llvm::MutableArrayRef<uint8_t> &buffer,
107                           size_t offset = 0);
108 
109   Status ReadPerfTraceData(llvm::MutableArrayRef<uint8_t> &buffer,
110                            size_t offset = 0);
111 
112   ~ProcessorTraceMonitor() = default;
113 
GetThreadID()114   lldb::tid_t GetThreadID() const { return m_thread_id; }
115 
GetTraceID()116   lldb::user_id_t GetTraceID() const { return m_traceid; }
117 
118   Status GetTraceConfig(TraceOptions &config) const;
119 
120   /// Read data from a cyclic buffer
121   ///
122   /// \param[in] [out] buf
123   ///     Destination buffer, the buffer will be truncated to written size.
124   ///
125   /// \param[in] src
126   ///     Source buffer which must be a cyclic buffer.
127   ///
128   /// \param[in] src_cyc_index
129   ///     The index pointer (start of the valid data in the cyclic
130   ///     buffer).
131   ///
132   /// \param[in] offset
133   ///     The offset to begin reading the data in the cyclic buffer.
134   static void ReadCyclicBuffer(llvm::MutableArrayRef<uint8_t> &dst,
135                                llvm::MutableArrayRef<uint8_t> src,
136                                size_t src_cyc_index, size_t offset);
137 };
138 } // namespace process_linux
139 } // namespace lldb_private
140 #endif
141