File: CommandObjectThreadTraceExportCTF.cpp

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 1,998,520 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (95 lines) | stat: -rw-r--r-- 3,200 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//===-- CommandObjectThreadTraceExportCTF.cpp -----------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "CommandObjectThreadTraceExportCTF.h"

#include "../common/TraceHTR.h"
#include "lldb/Host/OptionParser.h"
#include "lldb/Interpreter/CommandOptionArgumentTable.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/Trace.h"

using namespace lldb;
using namespace lldb_private;
using namespace lldb_private::ctf;
using namespace llvm;

// CommandObjectThreadTraceExportCTF

#define LLDB_OPTIONS_thread_trace_export_ctf
#include "TraceExporterCTFCommandOptions.inc"

Status CommandObjectThreadTraceExportCTF::CommandOptions::SetOptionValue(
    uint32_t option_idx, llvm::StringRef option_arg,
    ExecutionContext *execution_context) {
  Status error;
  const int short_option = m_getopt_table[option_idx].val;

  switch (short_option) {
  case 'f': {
    m_file.assign(std::string(option_arg));
    break;
  }
  case 't': {
    int64_t thread_index;
    if (option_arg.empty() || option_arg.getAsInteger(0, thread_index) ||
        thread_index < 0)
      error.SetErrorStringWithFormat("invalid integer value for option '%s'",
                                     option_arg.str().c_str());
    else
      m_thread_index = thread_index;
    break;
  }
  default:
    llvm_unreachable("Unimplemented option");
  }
  return error;
}

void CommandObjectThreadTraceExportCTF::CommandOptions::OptionParsingStarting(
    ExecutionContext *execution_context) {
  m_file.clear();
  m_thread_index = std::nullopt;
}

llvm::ArrayRef<OptionDefinition>
CommandObjectThreadTraceExportCTF::CommandOptions::GetDefinitions() {
  return llvm::ArrayRef(g_thread_trace_export_ctf_options);
}

void CommandObjectThreadTraceExportCTF::DoExecute(Args &command,
                                                  CommandReturnObject &result) {
  const TraceSP &trace_sp = m_exe_ctx.GetTargetSP()->GetTrace();
  Process *process = m_exe_ctx.GetProcessPtr();
  Thread *thread = m_options.m_thread_index
                       ? process->GetThreadList()
                             .FindThreadByIndexID(*m_options.m_thread_index)
                             .get()
                       : GetDefaultThread();

  if (thread == nullptr) {
    const uint32_t num_threads = process->GetThreadList().GetSize();
    size_t tid = m_options.m_thread_index.value_or(LLDB_INVALID_THREAD_ID);
    result.AppendErrorWithFormatv(
        "Thread index {0} is out of range (valid values are 1 - {1}).\n", tid,
        num_threads);
  } else {
    auto do_work = [&]() -> Error {
      Expected<TraceCursorSP> cursor = trace_sp->CreateNewCursor(*thread);
      if (!cursor)
        return cursor.takeError();
      TraceHTR htr(*thread, **cursor);
      htr.ExecutePasses();
      return htr.Export(m_options.m_file);
    };

    if (llvm::Error err = do_work()) {
      result.AppendErrorWithFormat("%s\n", toString(std::move(err)).c_str());
    }
  }
}