Coverage Report

Created: 2022-01-25 06:29

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