LLDB mainline
CommandObjectThreadTraceExportCTF.cpp
Go to the documentation of this file.
1//===-- CommandObjectThreadTraceExportCTF.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
10
11#include "../common/TraceHTR.h"
14#include "lldb/Target/Process.h"
15#include "lldb/Target/Trace.h"
16
17using namespace lldb;
18using namespace lldb_private;
19using namespace lldb_private::ctf;
20using namespace llvm;
21
22// CommandObjectThreadTraceExportCTF
23
24#define LLDB_OPTIONS_thread_trace_export_ctf
25#include "TraceExporterCTFCommandOptions.inc"
26
28 uint32_t option_idx, llvm::StringRef option_arg,
29 ExecutionContext *execution_context) {
31 const int short_option = m_getopt_table[option_idx].val;
32
33 switch (short_option) {
34 case 'f': {
35 m_file.assign(std::string(option_arg));
36 break;
37 }
38 case 't': {
39 int64_t thread_index;
40 if (option_arg.empty() || option_arg.getAsInteger(0, thread_index) ||
41 thread_index < 0)
42 error.SetErrorStringWithFormat("invalid integer value for option '%s'",
43 option_arg.str().c_str());
44 else
45 m_thread_index = thread_index;
46 break;
47 }
48 default:
49 llvm_unreachable("Unimplemented option");
50 }
51 return error;
52}
53
55 ExecutionContext *execution_context) {
56 m_file.clear();
57 m_thread_index = std::nullopt;
58}
59
60llvm::ArrayRef<OptionDefinition>
62 return llvm::ArrayRef(g_thread_trace_export_ctf_options);
63}
64
66 CommandReturnObject &result) {
67 const TraceSP &trace_sp = m_exe_ctx.GetTargetSP()->GetTrace();
68 Process *process = m_exe_ctx.GetProcessPtr();
70 ? process->GetThreadList()
72 .get()
74
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);
79 "Thread index {0} is out of range (valid values are 1 - {1}).\n", tid,
80 num_threads);
81 return false;
82 } else {
83 auto do_work = [&]() -> Error {
84 Expected<TraceCursorSP> cursor = trace_sp->CreateNewCursor(*thread);
85 if (!cursor)
86 return cursor.takeError();
87 TraceHTR htr(*thread, **cursor);
88 htr.ExecutePasses();
89 return htr.Export(m_options.m_file);
90 };
91
92 if (llvm::Error err = do_work()) {
93 result.AppendErrorWithFormat("%s\n", toString(std::move(err)).c_str());
94 return false;
95 } else {
96 return true;
97 }
98 }
99}
static llvm::raw_ostream & error(Stream &strm)
llvm::Error Error
A command line argument class.
Definition: Args.h:33
ExecutionContext m_exe_ctx
void AppendErrorWithFormatv(const char *format, Args &&... args)
void AppendErrorWithFormat(const char *format,...) __attribute__((format(printf
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
const lldb::TargetSP & GetTargetSP() const
Get accessor to get the target shared pointer.
Process * GetProcessPtr() const
Returns a pointer to the process object.
std::vector< Option > m_getopt_table
Definition: Options.h:197
A plug-in interface definition class for debugging a process.
Definition: Process.h:342
ThreadList & GetThreadList()
Definition: Process.h:2095
An error handling class.
Definition: Status.h:44
uint32_t GetSize(bool can_update=true)
Definition: ThreadList.cpp:83
lldb::ThreadSP FindThreadByIndexID(uint32_t index_id, bool can_update=true)
Definition: ThreadList.cpp:209
Top-level HTR class See lldb/docs/htr.rst for comprehensive HTR documentation.
Definition: TraceHTR.h:305
llvm::Error Export(std::string outfile)
Export HTR layers to the specified format and outfile.
Definition: TraceHTR.cpp:233
void ExecutePasses()
Executes passes on the HTR layers until no further summarization/compression is achieved.
Definition: TraceHTR.cpp:211
Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, ExecutionContext *execution_context) override
Set the value of an option.
bool DoExecute(Args &command, CommandReturnObject &result) override
#define LLDB_INVALID_THREAD_ID
Definition: lldb-defines.h:82
A class that represents a running process on the host machine.
const char * toString(AppleArm64ExceptionClass EC)
Definition: SBAddress.h:15
Definition: Debugger.h:51