|
| 1 | +//===-- SBTraceCursor.h -----------------------------------------*- C++ -*-===// |
| 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 | +#ifndef LLDB_API_SBTRACECURSOR_H |
| 10 | +#define LLDB_API_SBTRACECURSOR_H |
| 11 | + |
| 12 | +#include "lldb/API/SBDefines.h" |
| 13 | +#include "lldb/API/SBError.h" |
| 14 | +#include "lldb/API/SBExecutionContext.h" |
| 15 | +#include "lldb/Target/TraceCursor.h" |
| 16 | + |
| 17 | +namespace lldb { |
| 18 | + |
| 19 | +class LLDB_API SBTraceCursor { |
| 20 | +public: |
| 21 | + /// Default constructor for an invalid \a SBTraceCursor object. |
| 22 | + SBTraceCursor(); |
| 23 | + |
| 24 | + /// Create a cursor that initially points to the end of the trace, i.e. the |
| 25 | + /// most recent item. |
| 26 | + SBTraceCursor(lldb::TraceCursorSP trace_cursor_sp); |
| 27 | + |
| 28 | + /// Set the direction to use in the \a SBTraceCursor::Next() method. |
| 29 | + /// |
| 30 | + /// \param[in] forwards |
| 31 | + /// If \b true, then the traversal will be forwards, otherwise backwards. |
| 32 | + void SetForwards(bool forwards); |
| 33 | + |
| 34 | + /// Check if the direction to use in the \a SBTraceCursor::Next() method is |
| 35 | + /// forwards. |
| 36 | + /// |
| 37 | + /// \return |
| 38 | + /// \b true if the current direction is forwards, \b false if backwards. |
| 39 | + bool IsForwards() const; |
| 40 | + |
| 41 | + /// Move the cursor to the next item (instruction or error). |
| 42 | + /// |
| 43 | + /// Direction: |
| 44 | + /// The traversal is done following the current direction of the trace. If |
| 45 | + /// it is forwards, the instructions are visited forwards |
| 46 | + /// chronologically. Otherwise, the traversal is done in |
| 47 | + /// the opposite direction. By default, a cursor moves backwards unless |
| 48 | + /// changed with \a SBTraceCursor::SetForwards(). |
| 49 | + void Next(); |
| 50 | + |
| 51 | + /// \return |
| 52 | + /// \b true if the cursor is pointing to a valid item. \b false if the |
| 53 | + /// cursor has reached the end of the trace. |
| 54 | + bool HasValue() const; |
| 55 | + |
| 56 | + /// Instruction identifiers: |
| 57 | + /// |
| 58 | + /// When building complex higher level tools, fast random accesses in the |
| 59 | + /// trace might be needed, for which each instruction requires a unique |
| 60 | + /// identifier within its thread trace. For example, a tool might want to |
| 61 | + /// repeatedly inspect random consecutive portions of a trace. This means that |
| 62 | + /// it will need to first move quickly to the beginning of each section and |
| 63 | + /// then start its iteration. Given that the number of instructions can be in |
| 64 | + /// the order of hundreds of millions, fast random access is necessary. |
| 65 | + /// |
| 66 | + /// An example of such a tool could be an inspector of the call graph of a |
| 67 | + /// trace, where each call is represented with its start and end instructions. |
| 68 | + /// Inspecting all the instructions of a call requires moving to its first |
| 69 | + /// instruction and then iterating until the last instruction, which following |
| 70 | + /// the pattern explained above. |
| 71 | + /// |
| 72 | + /// Instead of using 0-based indices as identifiers, each Trace plug-in can |
| 73 | + /// decide the nature of these identifiers and thus no assumptions can be made |
| 74 | + /// regarding their ordering and sequentiality. The reason is that an |
| 75 | + /// instruction might be encoded by the plug-in in a way that hides its actual |
| 76 | + /// 0-based index in the trace, but it's still possible to efficiently find |
| 77 | + /// it. |
| 78 | + /// |
| 79 | + /// Requirements: |
| 80 | + /// - For a given thread, no two instructions have the same id. |
| 81 | + /// - In terms of efficiency, moving the cursor to a given id should be as |
| 82 | + /// fast as possible, but not necessarily O(1). That's why the recommended |
| 83 | + /// way to traverse sequential instructions is to use the \a |
| 84 | + /// SBTraceCursor::Next() method and only use \a SBTraceCursor::GoToId(id) |
| 85 | + /// sparingly. |
| 86 | + |
| 87 | + /// Make the cursor point to the item whose identifier is \p id. |
| 88 | + /// |
| 89 | + /// \return |
| 90 | + /// \b true if the given identifier exists and the cursor effectively |
| 91 | + /// moved to it. Otherwise, \b false is returned and the cursor now points |
| 92 | + /// to an invalid item, i.e. calling \a HasValue() will return \b false. |
| 93 | + bool GoToId(lldb::user_id_t id); |
| 94 | + |
| 95 | + /// \return |
| 96 | + /// \b true if and only if there's an instruction item with the given \p |
| 97 | + /// id. |
| 98 | + bool HasId(lldb::user_id_t id) const; |
| 99 | + |
| 100 | + /// \return |
| 101 | + /// A unique identifier for the instruction or error this cursor is |
| 102 | + /// pointing to. |
| 103 | + lldb::user_id_t GetId() const; |
| 104 | + /// \} |
| 105 | + |
| 106 | + /// Make the cursor point to an item in the trace based on an origin point and |
| 107 | + /// an offset. |
| 108 | + /// |
| 109 | + /// The resulting position of the trace is |
| 110 | + /// origin + offset |
| 111 | + /// |
| 112 | + /// If this resulting position would be out of bounds, the trace then points |
| 113 | + /// to an invalid item, i.e. calling \a HasValue() returns \b false. |
| 114 | + /// |
| 115 | + /// \param[in] offset |
| 116 | + /// How many items to move forwards (if positive) or backwards (if |
| 117 | + /// negative) from the given origin point. For example, if origin is \b |
| 118 | + /// End, then a negative offset would move backward in the trace, but a |
| 119 | + /// positive offset would move past the trace to an invalid item. |
| 120 | + /// |
| 121 | + /// \param[in] origin |
| 122 | + /// The reference point to use when moving the cursor. |
| 123 | + /// |
| 124 | + /// \return |
| 125 | + /// \b true if and only if the cursor ends up pointing to a valid item. |
| 126 | + bool Seek(int64_t offset, lldb::TraceCursorSeekType origin); |
| 127 | + |
| 128 | + /// \return |
| 129 | + /// The \a ExecutionContextRef of the backing thread from the creation time |
| 130 | + /// of this cursor. |
| 131 | + SBExecutionContext &GetExecutionContextRef(); |
| 132 | + |
| 133 | + /// Trace item information (instructions, errors and events) |
| 134 | + /// \{ |
| 135 | + |
| 136 | + /// \return |
| 137 | + /// The kind of item the cursor is pointing at. |
| 138 | + lldb::TraceItemKind GetItemKind() const; |
| 139 | + |
| 140 | + /// \return |
| 141 | + /// Whether the cursor points to an error or not. |
| 142 | + bool IsError() const; |
| 143 | + |
| 144 | + /// \return |
| 145 | + /// The error message the cursor is pointing at. |
| 146 | + const char *GetError() const; |
| 147 | + |
| 148 | + /// \return |
| 149 | + /// Whether the cursor points to an event or not. |
| 150 | + bool IsEvent() const; |
| 151 | + |
| 152 | + /// \return |
| 153 | + /// The specific kind of event the cursor is pointing at. |
| 154 | + lldb::TraceEvent GetEventType() const; |
| 155 | + |
| 156 | + /// \return |
| 157 | + /// A human-readable description of the event this cursor is pointing at. |
| 158 | + const char *GetEventTypeAsString() const; |
| 159 | + |
| 160 | + /// \return |
| 161 | + /// Whether the cursor points to an instruction. |
| 162 | + bool IsInstruction() const; |
| 163 | + |
| 164 | + /// \return |
| 165 | + /// The load address of the instruction the cursor is pointing at. |
| 166 | + lldb::addr_t GetLoadAddress() const; |
| 167 | + |
| 168 | + /// \return |
| 169 | + /// The requested CPU id, or LLDB_INVALID_CPU_ID if this information is |
| 170 | + /// not available for the current item. |
| 171 | + lldb::cpu_id_t GetCPU() const; |
| 172 | + |
| 173 | + bool IsValid() const; |
| 174 | + |
| 175 | + explicit operator bool() const; |
| 176 | + |
| 177 | +protected: |
| 178 | + lldb::TraceCursorSP m_opaque_sp; |
| 179 | +}; |
| 180 | +} // namespace lldb |
| 181 | + |
| 182 | +#endif // LLDB_API_SBTRACECURSOR_H |
0 commit comments