Coverage Report

Created: 2022-07-16 07:03

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Plugins/TraceExporter/ctf/CommandObjectThreadTraceExportCTF.cpp
Line
Count
Source (jump to first uncovered line)
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
9
#include "CommandObjectThreadTraceExportCTF.h"
10
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"
16
17
using namespace lldb;
18
using namespace lldb_private;
19
using namespace lldb_private::ctf;
20
using namespace llvm;
21
22
// CommandObjectThreadTraceExportCTF
23
24
#define LLDB_OPTIONS_thread_trace_export_ctf
25
#include "TraceExporterCTFCommandOptions.inc"
26
27
Status CommandObjectThreadTraceExportCTF::CommandOptions::SetOptionValue(
28
    uint32_t option_idx, llvm::StringRef option_arg,
29
0
    ExecutionContext *execution_context) {
30
0
  Status error;
31
0
  const int short_option = m_getopt_table[option_idx].val;
32
33
0
  switch (short_option) {
34
0
  case 'f': {
35
0
    m_file.assign(std::string(option_arg));
36
0
    break;
37
0
  }
38
0
  case 't': {
39
0
    int64_t thread_index;
40
0
    if (option_arg.empty() || option_arg.getAsInteger(0, thread_index) ||
41
0
        thread_index < 0)
42
0
      error.SetErrorStringWithFormat("invalid integer value for option '%s'",
43
0
                                     option_arg.str().c_str());
44
0
    else
45
0
      m_thread_index = thread_index;
46
0
    break;
47
0
  }
48
0
  default:
49
0
    llvm_unreachable("Unimplemented option");
50
0
  }
51
0
  return error;
52
0
}
53
54
void CommandObjectThreadTraceExportCTF::CommandOptions::OptionParsingStarting(
55
6.21k
    ExecutionContext *execution_context) {
56
6.21k
  m_file.clear();
57
6.21k
  m_thread_index = None;
58
6.21k
}
59
60
llvm::ArrayRef<OptionDefinition>
61
0
CommandObjectThreadTraceExportCTF::CommandOptions::GetDefinitions() {
62
0
  return llvm::makeArrayRef(g_thread_trace_export_ctf_options);
63
0
}
64
65
bool CommandObjectThreadTraceExportCTF::DoExecute(Args &command,
66
0
                                                  CommandReturnObject &result) {
67
0
  const TraceSP &trace_sp = m_exe_ctx.GetTargetSP()->GetTrace();
68
0
  Process *process = m_exe_ctx.GetProcessPtr();
69
0
  Thread *thread = m_options.m_thread_index
70
0
                       ? process->GetThreadList()
71
0
                             .FindThreadByIndexID(*m_options.m_thread_index)
72
0
                             .get()
73
0
                       : GetDefaultThread();
74
75
0
  if (thread == nullptr) {
76
0
    const uint32_t num_threads = process->GetThreadList().GetSize();
77
0
    size_t tid = m_options.m_thread_index.value_or(LLDB_INVALID_THREAD_ID);
78
0
    result.AppendErrorWithFormatv(
79
0
        "Thread index {0} is out of range (valid values are 1 - {1}).\n", tid,
80
0
        num_threads);
81
0
    return false;
82
0
  } else {
83
0
    auto do_work = [&]() -> Error {
84
0
      Expected<TraceCursorUP> cursor = trace_sp->CreateNewCursor(*thread);
85
0
      if (!cursor)
86
0
        return cursor.takeError();
87
0
      TraceHTR htr(*thread, **cursor);
88
0
      htr.ExecutePasses();
89
0
      return htr.Export(m_options.m_file);
90
0
    };
91
92
0
    if (llvm::Error err = do_work()) {
93
0
      result.AppendErrorWithFormat("%s\n", toString(std::move(err)).c_str());
94
0
      return false;
95
0
    } else {
96
0
      return true;
97
0
    }
98
0
  }
99
0
}