1 //===-- CommandObjectThreadTraceExportCTF.cpp -----------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "CommandObjectThreadTraceExportCTF.h"
11 #include "../common/TraceHTR.h"
12 #include "lldb/Host/OptionParser.h"
13 #include "lldb/Interpreter/CommandOptionArgumentTable.h"
14 #include "lldb/Target/Process.h"
15 #include "lldb/Target/Trace.h"
18 using namespace lldb_private
;
19 using namespace lldb_private::ctf
;
22 // CommandObjectThreadTraceExportCTF
24 #define LLDB_OPTIONS_thread_trace_export_ctf
25 #include "TraceExporterCTFCommandOptions.inc"
27 Status
CommandObjectThreadTraceExportCTF::CommandOptions::SetOptionValue(
28 uint32_t option_idx
, llvm::StringRef option_arg
,
29 ExecutionContext
*execution_context
) {
31 const int short_option
= m_getopt_table
[option_idx
].val
;
33 switch (short_option
) {
35 m_file
.assign(std::string(option_arg
));
40 if (option_arg
.empty() || option_arg
.getAsInteger(0, thread_index
) ||
42 error
= Status::FromErrorStringWithFormatv(
43 "invalid integer value for option '{0}'", option_arg
);
45 m_thread_index
= thread_index
;
49 llvm_unreachable("Unimplemented option");
54 void CommandObjectThreadTraceExportCTF::CommandOptions::OptionParsingStarting(
55 ExecutionContext
*execution_context
) {
57 m_thread_index
= std::nullopt
;
60 llvm::ArrayRef
<OptionDefinition
>
61 CommandObjectThreadTraceExportCTF::CommandOptions::GetDefinitions() {
62 return llvm::ArrayRef(g_thread_trace_export_ctf_options
);
65 void CommandObjectThreadTraceExportCTF::DoExecute(Args
&command
,
66 CommandReturnObject
&result
) {
67 const TraceSP
&trace_sp
= m_exe_ctx
.GetTargetSP()->GetTrace();
68 Process
*process
= m_exe_ctx
.GetProcessPtr();
69 Thread
*thread
= m_options
.m_thread_index
70 ? process
->GetThreadList()
71 .FindThreadByIndexID(*m_options
.m_thread_index
)
75 if (thread
== nullptr) {
76 const uint32_t num_threads
= process
->GetThreadList().GetSize();
77 size_t tid
= m_options
.m_thread_index
.value_or(LLDB_INVALID_THREAD_ID
);
78 result
.AppendErrorWithFormatv(
79 "Thread index {0} is out of range (valid values are 1 - {1}).\n", tid
,
82 auto do_work
= [&]() -> Error
{
83 Expected
<TraceCursorSP
> cursor
= trace_sp
->CreateNewCursor(*thread
);
85 return cursor
.takeError();
86 TraceHTR
htr(*thread
, **cursor
);
88 return htr
.Export(m_options
.m_file
);
91 if (llvm::Error err
= do_work()) {
92 result
.AppendErrorWithFormat("%s\n", toString(std::move(err
)).c_str());