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