aboutsummaryrefslogtreecommitdiff
path: root/lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
blob: edefdd0d3486e6903d57d6f17f9f976b311cead4 (plain) (blame)
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
96
97
98
99
100
//===-- TraceCursorIntelPT.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 "TraceCursorIntelPT.h"
#include "DecodedThread.h"
#include "TraceIntelPT.h"

#include <cstdlib>

using namespace lldb;
using namespace lldb_private;
using namespace lldb_private::trace_intel_pt;
using namespace llvm;

TraceCursorIntelPT::TraceCursorIntelPT(ThreadSP thread_sp,
                                       DecodedThreadSP decoded_thread_sp)
    : TraceCursor(thread_sp), m_decoded_thread_sp(decoded_thread_sp) {
  assert(!m_decoded_thread_sp->GetInstructions().empty() &&
         "a trace should have at least one instruction or error");
  m_pos = m_decoded_thread_sp->GetInstructions().size() - 1;
}

size_t TraceCursorIntelPT::GetInternalInstructionSize() {
  return m_decoded_thread_sp->GetInstructions().size();
}

bool TraceCursorIntelPT::Next() {
  auto canMoveOne = [&]() {
    if (IsForwards())
      return m_pos + 1 < GetInternalInstructionSize();
    return m_pos > 0;
  };

  size_t initial_pos = m_pos;

  while (canMoveOne()) {
    m_pos += IsForwards() ? 1 : -1;
    if (!m_ignore_errors && IsError())
      return true;
    if (GetInstructionControlFlowType() & m_granularity)
      return true;
  }

  // Didn't find any matching instructions
  m_pos = initial_pos;
  return false;
}

size_t TraceCursorIntelPT::Seek(int64_t offset, SeekType origin) {
  int64_t last_index = GetInternalInstructionSize() - 1;

  auto fitPosToBounds = [&](int64_t raw_pos) -> int64_t {
    return std::min(std::max((int64_t)0, raw_pos), last_index);
  };

  switch (origin) {
  case TraceCursor::SeekType::Set:
    m_pos = fitPosToBounds(offset);
    return m_pos;
  case TraceCursor::SeekType::End:
    m_pos = fitPosToBounds(offset + last_index);
    return last_index - m_pos;
  case TraceCursor::SeekType::Current:
    int64_t new_pos = fitPosToBounds(offset + m_pos);
    int64_t dist = m_pos - new_pos;
    m_pos = new_pos;
    return std::abs(dist);
  }
}

bool TraceCursorIntelPT::IsError() {
  return m_decoded_thread_sp->GetInstructions()[m_pos].IsError();
}

Error TraceCursorIntelPT::GetError() {
  return m_decoded_thread_sp->GetInstructions()[m_pos].ToError();
}

lldb::addr_t TraceCursorIntelPT::GetLoadAddress() {
  return m_decoded_thread_sp->GetInstructions()[m_pos].GetLoadAddress();
}

Optional<uint64_t> TraceCursorIntelPT::GetTimestampCounter() {
  return m_decoded_thread_sp->GetInstructions()[m_pos].GetTimestampCounter();
}

TraceInstructionControlFlowType
TraceCursorIntelPT::GetInstructionControlFlowType() {
  lldb::addr_t next_load_address =
      m_pos + 1 < GetInternalInstructionSize()
          ? m_decoded_thread_sp->GetInstructions()[m_pos + 1].GetLoadAddress()
          : LLDB_INVALID_ADDRESS;
  return m_decoded_thread_sp->GetInstructions()[m_pos].GetControlFlowType(
      next_load_address);
}