1fe6060f1SDimitry Andric //===-- CommandObjectThreadTraceExportCTF.cpp -----------------------------===//
2fe6060f1SDimitry Andric //
3fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6fe6060f1SDimitry Andric //
7fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
8fe6060f1SDimitry Andric
9fe6060f1SDimitry Andric #include "CommandObjectThreadTraceExportCTF.h"
10fe6060f1SDimitry Andric
11349cc55cSDimitry Andric #include "../common/TraceHTR.h"
12fe6060f1SDimitry Andric #include "lldb/Host/OptionParser.h"
13fcaf7f86SDimitry Andric #include "lldb/Interpreter/CommandOptionArgumentTable.h"
14349cc55cSDimitry Andric #include "lldb/Target/Process.h"
15349cc55cSDimitry Andric #include "lldb/Target/Trace.h"
16fe6060f1SDimitry Andric
17fe6060f1SDimitry Andric using namespace lldb;
18fe6060f1SDimitry Andric using namespace lldb_private;
19fe6060f1SDimitry Andric using namespace lldb_private::ctf;
20fe6060f1SDimitry Andric using namespace llvm;
21fe6060f1SDimitry Andric
22fe6060f1SDimitry Andric // CommandObjectThreadTraceExportCTF
23fe6060f1SDimitry Andric
24fe6060f1SDimitry Andric #define LLDB_OPTIONS_thread_trace_export_ctf
25fe6060f1SDimitry Andric #include "TraceExporterCTFCommandOptions.inc"
26fe6060f1SDimitry Andric
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)27fe6060f1SDimitry Andric Status CommandObjectThreadTraceExportCTF::CommandOptions::SetOptionValue(
28fe6060f1SDimitry Andric uint32_t option_idx, llvm::StringRef option_arg,
29fe6060f1SDimitry Andric ExecutionContext *execution_context) {
30fe6060f1SDimitry Andric Status error;
31fe6060f1SDimitry Andric const int short_option = m_getopt_table[option_idx].val;
32fe6060f1SDimitry Andric
33fe6060f1SDimitry Andric switch (short_option) {
34349cc55cSDimitry Andric case 'f': {
35349cc55cSDimitry Andric m_file.assign(std::string(option_arg));
36349cc55cSDimitry Andric break;
37349cc55cSDimitry Andric }
38fe6060f1SDimitry Andric case 't': {
39fe6060f1SDimitry Andric int64_t thread_index;
40fe6060f1SDimitry Andric if (option_arg.empty() || option_arg.getAsInteger(0, thread_index) ||
41fe6060f1SDimitry Andric thread_index < 0)
42fe6060f1SDimitry Andric error.SetErrorStringWithFormat("invalid integer value for option '%s'",
43fe6060f1SDimitry Andric option_arg.str().c_str());
44fe6060f1SDimitry Andric else
45fe6060f1SDimitry Andric m_thread_index = thread_index;
46fe6060f1SDimitry Andric break;
47fe6060f1SDimitry Andric }
48fe6060f1SDimitry Andric default:
49fe6060f1SDimitry Andric llvm_unreachable("Unimplemented option");
50fe6060f1SDimitry Andric }
51fe6060f1SDimitry Andric return error;
52fe6060f1SDimitry Andric }
53fe6060f1SDimitry Andric
OptionParsingStarting(ExecutionContext * execution_context)54fe6060f1SDimitry Andric void CommandObjectThreadTraceExportCTF::CommandOptions::OptionParsingStarting(
55fe6060f1SDimitry Andric ExecutionContext *execution_context) {
56349cc55cSDimitry Andric m_file.clear();
57bdd1243dSDimitry Andric m_thread_index = std::nullopt;
58fe6060f1SDimitry Andric }
59fe6060f1SDimitry Andric
60fe6060f1SDimitry Andric llvm::ArrayRef<OptionDefinition>
GetDefinitions()61fe6060f1SDimitry Andric CommandObjectThreadTraceExportCTF::CommandOptions::GetDefinitions() {
62bdd1243dSDimitry Andric return llvm::ArrayRef(g_thread_trace_export_ctf_options);
63fe6060f1SDimitry Andric }
64fe6060f1SDimitry Andric
DoExecute(Args & command,CommandReturnObject & result)65*5f757f3fSDimitry Andric void CommandObjectThreadTraceExportCTF::DoExecute(Args &command,
66fe6060f1SDimitry Andric CommandReturnObject &result) {
67349cc55cSDimitry Andric const TraceSP &trace_sp = m_exe_ctx.GetTargetSP()->GetTrace();
68349cc55cSDimitry Andric Process *process = m_exe_ctx.GetProcessPtr();
69349cc55cSDimitry Andric Thread *thread = m_options.m_thread_index
70349cc55cSDimitry Andric ? process->GetThreadList()
71349cc55cSDimitry Andric .FindThreadByIndexID(*m_options.m_thread_index)
72349cc55cSDimitry Andric .get()
73349cc55cSDimitry Andric : GetDefaultThread();
74fe6060f1SDimitry Andric
75349cc55cSDimitry Andric if (thread == nullptr) {
76349cc55cSDimitry Andric const uint32_t num_threads = process->GetThreadList().GetSize();
7781ad6265SDimitry Andric size_t tid = m_options.m_thread_index.value_or(LLDB_INVALID_THREAD_ID);
78349cc55cSDimitry Andric result.AppendErrorWithFormatv(
79349cc55cSDimitry Andric "Thread index {0} is out of range (valid values are 1 - {1}).\n", tid,
80349cc55cSDimitry Andric num_threads);
81349cc55cSDimitry Andric } else {
8281ad6265SDimitry Andric auto do_work = [&]() -> Error {
83bdd1243dSDimitry Andric Expected<TraceCursorSP> cursor = trace_sp->CreateNewCursor(*thread);
8481ad6265SDimitry Andric if (!cursor)
8581ad6265SDimitry Andric return cursor.takeError();
8681ad6265SDimitry Andric TraceHTR htr(*thread, **cursor);
87349cc55cSDimitry Andric htr.ExecutePasses();
8881ad6265SDimitry Andric return htr.Export(m_options.m_file);
8981ad6265SDimitry Andric };
9081ad6265SDimitry Andric
9181ad6265SDimitry Andric if (llvm::Error err = do_work()) {
92349cc55cSDimitry Andric result.AppendErrorWithFormat("%s\n", toString(std::move(err)).c_str());
93349cc55cSDimitry Andric }
94349cc55cSDimitry Andric }
95fe6060f1SDimitry Andric }
96