LLDB  mainline
ProcessorTrace.h
Go to the documentation of this file.
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"
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 
40 typedef std::unique_ptr<ProcessorTraceMonitor> ProcessorTraceMonitorUP;
41 
43 
44  class munmap_delete {
45  size_t m_length;
46 
47  public:
48  munmap_delete(size_t length) : m_length(length) {}
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;
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 
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 
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 
93  void SetThreadID(lldb::tid_t tid) { m_thread_id = tid; }
94 
95 public:
96  static Status GetCPUType(TraceOptions &config);
97 
98  static llvm::Expected<ProcessorTraceMonitorUP>
99  Create(lldb::pid_t pid, lldb::tid_t tid, const TraceOptions &config,
100  bool useProcessSettings);
101 
102  Status ReadPerfTraceAux(llvm::MutableArrayRef<uint8_t> &buffer,
103  size_t offset = 0);
104 
105  Status ReadPerfTraceData(llvm::MutableArrayRef<uint8_t> &buffer,
106  size_t offset = 0);
107 
108  ~ProcessorTraceMonitor() = default;
109 
110  lldb::tid_t GetThreadID() const { return m_thread_id; }
111 
112  lldb::user_id_t GetTraceID() const { return m_traceid; }
113 
114  Status GetTraceConfig(TraceOptions &config) const;
115 
116  /// Read data from a cyclic buffer
117  ///
118  /// \param[in] [out] buf
119  /// Destination buffer, the buffer will be truncated to written size.
120  ///
121  /// \param[in] src
122  /// Source buffer which must be a cyclic buffer.
123  ///
124  /// \param[in] src_cyc_index
125  /// The index pointer (start of the valid data in the cyclic
126  /// buffer).
127  ///
128  /// \param[in] offset
129  /// The offset to begin reading the data in the cyclic buffer.
130  static void ReadCyclicBuffer(llvm::MutableArrayRef<uint8_t> &dst,
131  llvm::MutableArrayRef<uint8_t> src,
132  size_t src_cyc_index, size_t offset);
133 };
134 } // namespace process_linux
135 } // namespace lldb_private
136 #endif
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
Status GetTraceConfig(TraceOptions &config) const
#define LLDB_INVALID_UID
Definition: lldb-defines.h:91
Status ReadPerfTraceData(llvm::MutableArrayRef< uint8_t > &buffer, size_t offset=0)
uint64_t user_id_t
Definition: lldb-types.h:84
uint64_t tid_t
Definition: lldb-types.h:86
std::unique_ptr< ProcessorTraceMonitor > ProcessorTraceMonitorUP
static Status GetCPUType(TraceOptions &config)
#define LLDB_INVALID_THREAD_ID
Definition: lldb-defines.h:93
static void ReadCyclicBuffer(llvm::MutableArrayRef< uint8_t > &dst, llvm::MutableArrayRef< uint8_t > src, size_t src_cyc_index, size_t offset)
Read data from a cyclic buffer.
uint64_t pid_t
Definition: lldb-types.h:85
static llvm::Expected< ProcessorTraceMonitorUP > Create(lldb::pid_t pid, lldb::tid_t tid, const TraceOptions &config, bool useProcessSettings)
An error handling class.
Definition: Status.h:44
Status ReadPerfTraceAux(llvm::MutableArrayRef< uint8_t > &buffer, size_t offset=0)