1 //===-- CommandObjectTraceStartIntelPT.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 LLDB_SOURCE_PLUGINS_TRACE_INTEL_PT_COMMANDOBJECTTRACESTARTINTELPT_H
10 #define LLDB_SOURCE_PLUGINS_TRACE_INTEL_PT_COMMANDOBJECTTRACESTARTINTELPT_H
11 
12 #include "../../../../source/Commands/CommandObjectTrace.h"
13 #include "TraceIntelPT.h"
14 #include "lldb/Interpreter/CommandInterpreter.h"
15 #include "lldb/Interpreter/CommandReturnObject.h"
16 #include <optional>
17 
18 namespace lldb_private {
19 namespace trace_intel_pt {
20 
21 class CommandObjectThreadTraceStartIntelPT
22     : public CommandObjectMultipleThreads {
23 public:
24   class CommandOptions : public Options {
25   public:
CommandOptions()26     CommandOptions() : Options() { OptionParsingStarting(nullptr); }
27 
28     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
29                           ExecutionContext *execution_context) override;
30 
31     void OptionParsingStarting(ExecutionContext *execution_context) override;
32 
33     llvm::ArrayRef<OptionDefinition> GetDefinitions() override;
34 
35     uint64_t m_ipt_trace_size;
36     bool m_enable_tsc;
37     std::optional<uint64_t> m_psb_period;
38   };
39 
CommandObjectThreadTraceStartIntelPT(TraceIntelPT & trace,CommandInterpreter & interpreter)40   CommandObjectThreadTraceStartIntelPT(TraceIntelPT &trace,
41                                        CommandInterpreter &interpreter)
42       : CommandObjectMultipleThreads(
43             interpreter, "thread trace start",
44             "Start tracing one or more threads with intel-pt. "
45             "Defaults to the current thread. Thread indices can be "
46             "specified as arguments.\n Use the thread-index \"all\" to trace "
47             "all threads including future threads.",
48             "thread trace start [<thread-index> <thread-index> ...] "
49             "[<intel-pt-options>]",
50             lldb::eCommandRequiresProcess | lldb::eCommandTryTargetAPILock |
51                 lldb::eCommandProcessMustBeLaunched |
52                 lldb::eCommandProcessMustBePaused),
53         m_trace(trace), m_options() {}
54 
GetOptions()55   Options *GetOptions() override { return &m_options; }
56 
57 protected:
58   bool DoExecuteOnThreads(Args &command, CommandReturnObject &result,
59                           llvm::ArrayRef<lldb::tid_t> tids) override;
60 
61   TraceIntelPT &m_trace;
62   CommandOptions m_options;
63 };
64 
65 class CommandObjectProcessTraceStartIntelPT : public CommandObjectParsed {
66 public:
67   class CommandOptions : public Options {
68   public:
CommandOptions()69     CommandOptions() : Options() { OptionParsingStarting(nullptr); }
70 
71     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
72                           ExecutionContext *execution_context) override;
73 
74     void OptionParsingStarting(ExecutionContext *execution_context) override;
75 
76     llvm::ArrayRef<OptionDefinition> GetDefinitions() override;
77 
78     uint64_t m_ipt_trace_size;
79     uint64_t m_process_buffer_size_limit;
80     bool m_enable_tsc;
81     std::optional<uint64_t> m_psb_period;
82     bool m_per_cpu_tracing;
83     bool m_disable_cgroup_filtering;
84   };
85 
CommandObjectProcessTraceStartIntelPT(TraceIntelPT & trace,CommandInterpreter & interpreter)86   CommandObjectProcessTraceStartIntelPT(TraceIntelPT &trace,
87                                         CommandInterpreter &interpreter)
88       : CommandObjectParsed(
89             interpreter, "process trace start",
90             "Start tracing this process with intel-pt, including future "
91             "threads. If --per-cpu-tracing is not provided, this traces each "
92             "thread independently, thus using a trace buffer per thread. "
93             "Threads traced with the \"thread trace start\" command are left "
94             "unaffected ant not retraced. This is the recommended option "
95             "unless the number of threads is huge. If --per-cpu-tracing is "
96             "passed, each cpu core is traced instead of each thread, which "
97             "uses a fixed number of trace buffers, but might result in less "
98             "data available for less frequent threads.",
99             "process trace start [<intel-pt-options>]",
100             lldb::eCommandRequiresProcess | lldb::eCommandTryTargetAPILock |
101                 lldb::eCommandProcessMustBeLaunched |
102                 lldb::eCommandProcessMustBePaused),
103         m_trace(trace), m_options() {}
104 
GetOptions()105   Options *GetOptions() override { return &m_options; }
106 
107 protected:
108   void DoExecute(Args &command, CommandReturnObject &result) override;
109 
110   TraceIntelPT &m_trace;
111   CommandOptions m_options;
112 };
113 
114 namespace ParsingUtils {
115 /// Convert an integral size expression like 12KiB or 4MB into bytes. The units
116 /// are taken loosely to help users input sizes into LLDB, e.g. KiB and KB are
117 /// considered the same (2^20 bytes) for simplicity.
118 ///
119 /// \param[in] size_expression
120 ///     String expression which is an integral number plus a unit that can be
121 ///     lower or upper case. Supported units: K, KB and KiB for 2^10 bytes; M,
122 ///     MB and MiB for 2^20 bytes; and B for bytes. A single integral number is
123 ///     considered bytes.
124 /// \return
125 ///   The converted number of bytes or \a std::nullopt if the expression is
126 ///   invalid.
127 std::optional<uint64_t>
128 ParseUserFriendlySizeExpression(llvm::StringRef size_expression);
129 } // namespace ParsingUtils
130 
131 } // namespace trace_intel_pt
132 } // namespace lldb_private
133 
134 #endif // LLDB_SOURCE_PLUGINS_TRACE_INTEL_PT_COMMANDOBJECTTRACESTARTINTELPT_H
135