xref: /llvm-project/lldb/source/Plugins/Trace/intel-pt/CommandObjectTraceStartIntelPT.cpp (revision 2aa1dd1c66dc3b1f6253eec9fc68c081a945b74d)
1 //===-- CommandObjectTraceStartIntelPT.cpp --------------------------------===//
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 "CommandObjectTraceStartIntelPT.h"
10 
11 #include "TraceIntelPT.h"
12 #include "TraceIntelPTConstants.h"
13 #include "lldb/Host/OptionParser.h"
14 #include "lldb/Target/Process.h"
15 #include "lldb/Target/Trace.h"
16 
17 using namespace lldb;
18 using namespace lldb_private;
19 using namespace lldb_private::trace_intel_pt;
20 using namespace llvm;
21 
22 // CommandObjectThreadTraceStartIntelPT
23 
24 #define LLDB_OPTIONS_thread_trace_start_intel_pt
25 #include "TraceIntelPTCommandOptions.inc"
26 
27 Status CommandObjectThreadTraceStartIntelPT::CommandOptions::SetOptionValue(
28     uint32_t option_idx, llvm::StringRef option_arg,
29     ExecutionContext *execution_context) {
30   Status error;
31   const int short_option = m_getopt_table[option_idx].val;
32 
33   switch (short_option) {
34   case 's': {
35     int64_t thread_buffer_size;
36     if (option_arg.empty() || option_arg.getAsInteger(0, thread_buffer_size) ||
37         thread_buffer_size < 0)
38       error.SetErrorStringWithFormat("invalid integer value for option '%s'",
39                                      option_arg.str().c_str());
40     else
41       m_thread_buffer_size = thread_buffer_size;
42     break;
43   }
44   default:
45     llvm_unreachable("Unimplemented option");
46   }
47   return error;
48 }
49 
50 void CommandObjectThreadTraceStartIntelPT::CommandOptions::
51     OptionParsingStarting(ExecutionContext *execution_context) {
52   m_thread_buffer_size = kThreadBufferSize;
53 }
54 
55 llvm::ArrayRef<OptionDefinition>
56 CommandObjectThreadTraceStartIntelPT::CommandOptions::GetDefinitions() {
57   return llvm::makeArrayRef(g_thread_trace_start_intel_pt_options);
58 }
59 
60 bool CommandObjectThreadTraceStartIntelPT::DoExecuteOnThreads(
61     Args &command, CommandReturnObject &result,
62     llvm::ArrayRef<lldb::tid_t> tids) {
63   if (Error err = m_trace.Start(tids, m_options.m_thread_buffer_size))
64     result.SetError(Status(std::move(err)));
65   else
66     result.SetStatus(eReturnStatusSuccessFinishResult);
67 
68   return result.Succeeded();
69 }
70 
71 /// CommandObjectProcessTraceStartIntelPT
72 
73 #define LLDB_OPTIONS_process_trace_start_intel_pt
74 #include "TraceIntelPTCommandOptions.inc"
75 
76 Status CommandObjectProcessTraceStartIntelPT::CommandOptions::SetOptionValue(
77     uint32_t option_idx, llvm::StringRef option_arg,
78     ExecutionContext *execution_context) {
79   Status error;
80   const int short_option = m_getopt_table[option_idx].val;
81 
82   switch (short_option) {
83   case 's': {
84     int64_t thread_buffer_size;
85     if (option_arg.empty() || option_arg.getAsInteger(0, thread_buffer_size) ||
86         thread_buffer_size < 0)
87       error.SetErrorStringWithFormat("invalid integer value for option '%s'",
88                                      option_arg.str().c_str());
89     else
90       m_thread_buffer_size = thread_buffer_size;
91     break;
92   }
93   case 'l': {
94     int64_t process_buffer_size_limit;
95     if (option_arg.empty() ||
96         option_arg.getAsInteger(0, process_buffer_size_limit) ||
97         process_buffer_size_limit < 0)
98       error.SetErrorStringWithFormat("invalid integer value for option '%s'",
99                                      option_arg.str().c_str());
100     else
101       m_process_buffer_size_limit = process_buffer_size_limit;
102     break;
103   }
104   default:
105     llvm_unreachable("Unimplemented option");
106   }
107   return error;
108 }
109 
110 void CommandObjectProcessTraceStartIntelPT::CommandOptions::
111     OptionParsingStarting(ExecutionContext *execution_context) {
112   m_thread_buffer_size = kThreadBufferSize;
113   m_process_buffer_size_limit = kProcessBufferSizeLimit;
114 }
115 
116 llvm::ArrayRef<OptionDefinition>
117 CommandObjectProcessTraceStartIntelPT::CommandOptions::GetDefinitions() {
118   return llvm::makeArrayRef(g_process_trace_start_intel_pt_options);
119 }
120 
121 bool CommandObjectProcessTraceStartIntelPT::DoExecute(
122     Args &command, CommandReturnObject &result) {
123   if (Error err = m_trace.Start(m_options.m_thread_buffer_size,
124                                 m_options.m_process_buffer_size_limit))
125     result.SetError(Status(std::move(err)));
126   else
127     result.SetStatus(eReturnStatusSuccessFinishResult);
128 
129   return result.Succeeded();
130 }
131