1fe6060f1SDimitry Andric //===-- TraceCursor.h -------------------------------------------*- C++ -*-===// 2fe6060f1SDimitry Andric // 3fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6fe6060f1SDimitry Andric // 7fe6060f1SDimitry Andric //===----------------------------------------------------------------------===// 8fe6060f1SDimitry Andric 9fe6060f1SDimitry Andric #ifndef LLDB_TARGET_TRACE_CURSOR_H 10fe6060f1SDimitry Andric #define LLDB_TARGET_TRACE_CURSOR_H 11fe6060f1SDimitry Andric 12fe6060f1SDimitry Andric #include "lldb/lldb-private.h" 13fe6060f1SDimitry Andric 14fe6060f1SDimitry Andric #include "lldb/Target/ExecutionContext.h" 15bdd1243dSDimitry Andric #include <optional> 16fe6060f1SDimitry Andric 17fe6060f1SDimitry Andric namespace lldb_private { 18fe6060f1SDimitry Andric 1981ad6265SDimitry Andric /// Class used for iterating over the instructions of a thread's trace, among 2081ad6265SDimitry Andric /// other kinds of information. 21fe6060f1SDimitry Andric /// 22fe6060f1SDimitry Andric /// This class attempts to be a generic interface for accessing the instructions 23fe6060f1SDimitry Andric /// of the trace so that each Trace plug-in can reconstruct, represent and store 24fe6060f1SDimitry Andric /// the instruction data in an flexible way that is efficient for the given 25fe6060f1SDimitry Andric /// technology. 26fe6060f1SDimitry Andric /// 27fe6060f1SDimitry Andric /// Live processes: 2881ad6265SDimitry Andric /// In the case of a live process trace, an instance of a \a TraceCursor 2981ad6265SDimitry Andric /// should point to the trace at the moment it was collected. If the process 3081ad6265SDimitry Andric /// is later resumed and new trace data is collected, then it's up to each 3181ad6265SDimitry Andric /// trace plug-in to decide whether to leave the old cursor unaffected or not. 32fe6060f1SDimitry Andric /// 3381ad6265SDimitry Andric /// Cursor items: 3481ad6265SDimitry Andric /// A \a TraceCursor can point at one of the following items: 35fe6060f1SDimitry Andric /// 3681ad6265SDimitry Andric /// Errors: 3781ad6265SDimitry Andric /// As there could be errors when reconstructing the instructions of a 3881ad6265SDimitry Andric /// trace, these errors are represented as failed instructions, and the 3981ad6265SDimitry Andric /// cursor can point at them. 4081ad6265SDimitry Andric /// 4181ad6265SDimitry Andric /// Events: 4281ad6265SDimitry Andric /// The cursor can also point at events in the trace, which aren't errors 4381ad6265SDimitry Andric /// nor instructions. An example of an event could be a context switch in 4481ad6265SDimitry Andric /// between two instructions. 4581ad6265SDimitry Andric /// 4681ad6265SDimitry Andric /// Instruction: 4781ad6265SDimitry Andric /// An actual instruction with a memory address. 48fe6060f1SDimitry Andric /// 49fe6060f1SDimitry Andric /// Defaults: 5081ad6265SDimitry Andric /// By default, the cursor points at the most recent item in the trace and is 5181ad6265SDimitry Andric /// set up to iterate backwards. See the \a TraceCursor::Next() method for 5281ad6265SDimitry Andric /// more documentation. 53fe6060f1SDimitry Andric /// 54fe6060f1SDimitry Andric /// Sample usage: 55fe6060f1SDimitry Andric /// 56bdd1243dSDimitry Andric /// TraceCursorSP cursor = trace.GetTrace(thread); 57fe6060f1SDimitry Andric /// 5881ad6265SDimitry Andric /// for (; cursor->HasValue(); cursor->Next()) { 5981ad6265SDimitry Andric /// TraceItemKind kind = cursor->GetItemKind(); 6081ad6265SDimitry Andric /// switch (cursor->GetItemKind()): 6181ad6265SDimitry Andric /// case eTraceItemKindError: 6281ad6265SDimitry Andric /// cout << "error found: " << cursor->GetError() << endl; 6381ad6265SDimitry Andric /// break; 6481ad6265SDimitry Andric /// case eTraceItemKindEvent: 6581ad6265SDimitry Andric /// cout << "event found: " << cursor->GetEventTypeAsString() << endl; 6681ad6265SDimitry Andric /// break; 6781ad6265SDimitry Andric /// case eTraceItemKindInstruction: 6881ad6265SDimitry Andric /// std::cout << "instructions found at " << cursor->GetLoadAddress() << 6981ad6265SDimitry Andric /// std::endl; break; 7081ad6265SDimitry Andric /// } 7181ad6265SDimitry Andric /// } 72fe6060f1SDimitry Andric /// 7381ad6265SDimitry Andric /// As the trace might be empty or the cursor might have reached the end of the 7481ad6265SDimitry Andric /// trace, you should always invoke \a HasValue() to make sure you don't access 7581ad6265SDimitry Andric /// invalid memory. 76fe6060f1SDimitry Andric /// 7781ad6265SDimitry Andric /// Random accesses: 7881ad6265SDimitry Andric /// 7981ad6265SDimitry Andric /// The Trace Cursor offer random acesses in the trace via two APIs: 8081ad6265SDimitry Andric /// 8181ad6265SDimitry Andric /// TraceCursor::Seek(): 8281ad6265SDimitry Andric /// Unlike the \a TraceCursor::Next() API, which moves instruction by 8381ad6265SDimitry Andric /// instruction, the \a TraceCursor::Seek() method can be used to 8481ad6265SDimitry Andric /// reposition the cursor to an offset of the end, beginning, or current 8581ad6265SDimitry Andric /// position of the trace. 8681ad6265SDimitry Andric /// 8781ad6265SDimitry Andric /// TraceCursor::GetId() / TraceCursor::SetId(id): 8881ad6265SDimitry Andric /// Each item (error or instruction) in the trace has a numeric identifier 8981ad6265SDimitry Andric /// which is defined by the trace plug-in. It's possible to access the id 9081ad6265SDimitry Andric /// of the current item using GetId(), and to reposition the cursor to a 9181ad6265SDimitry Andric /// given id using SetId(id). 9281ad6265SDimitry Andric /// 9381ad6265SDimitry Andric /// You can read more in the documentation of these methods. 94fe6060f1SDimitry Andric class TraceCursor { 95fe6060f1SDimitry Andric public: 96fe6060f1SDimitry Andric /// Create a cursor that initially points to the end of the trace, i.e. the 97fe6060f1SDimitry Andric /// most recent item. 98fe6060f1SDimitry Andric TraceCursor(lldb::ThreadSP thread_sp); 99fe6060f1SDimitry Andric 100fe6060f1SDimitry Andric virtual ~TraceCursor() = default; 101fe6060f1SDimitry Andric 102fe6060f1SDimitry Andric /// Set the direction to use in the \a TraceCursor::Next() method. 103fe6060f1SDimitry Andric /// 104fe6060f1SDimitry Andric /// \param[in] forwards 105fe6060f1SDimitry Andric /// If \b true, then the traversal will be forwards, otherwise backwards. 106fe6060f1SDimitry Andric void SetForwards(bool forwards); 107fe6060f1SDimitry Andric 108fe6060f1SDimitry Andric /// Check if the direction to use in the \a TraceCursor::Next() method is 109fe6060f1SDimitry Andric /// forwards. 110fe6060f1SDimitry Andric /// 111fe6060f1SDimitry Andric /// \return 112fe6060f1SDimitry Andric /// \b true if the current direction is forwards, \b false if backwards. 113fe6060f1SDimitry Andric bool IsForwards() const; 114fe6060f1SDimitry Andric 11581ad6265SDimitry Andric /// Move the cursor to the next item (instruction or error). 116fe6060f1SDimitry Andric /// 117fe6060f1SDimitry Andric /// Direction: 118fe6060f1SDimitry Andric /// The traversal is done following the current direction of the trace. If 119fe6060f1SDimitry Andric /// it is forwards, the instructions are visited forwards 120fe6060f1SDimitry Andric /// chronologically. Otherwise, the traversal is done in 121fe6060f1SDimitry Andric /// the opposite direction. By default, a cursor moves backwards unless 122fe6060f1SDimitry Andric /// changed with \a TraceCursor::SetForwards(). 12381ad6265SDimitry Andric virtual void Next() = 0; 12481ad6265SDimitry Andric 12581ad6265SDimitry Andric /// \return 12681ad6265SDimitry Andric /// \b true if the cursor is pointing to a valid item. \b false if the 12781ad6265SDimitry Andric /// cursor has reached the end of the trace. 12881ad6265SDimitry Andric virtual bool HasValue() const = 0; 12981ad6265SDimitry Andric 13081ad6265SDimitry Andric /// Instruction identifiers: 131fe6060f1SDimitry Andric /// 13281ad6265SDimitry Andric /// When building complex higher level tools, fast random accesses in the 13381ad6265SDimitry Andric /// trace might be needed, for which each instruction requires a unique 13481ad6265SDimitry Andric /// identifier within its thread trace. For example, a tool might want to 13581ad6265SDimitry Andric /// repeatedly inspect random consecutive portions of a trace. This means that 13681ad6265SDimitry Andric /// it will need to first move quickly to the beginning of each section and 13781ad6265SDimitry Andric /// then start its iteration. Given that the number of instructions can be in 13881ad6265SDimitry Andric /// the order of hundreds of millions, fast random access is necessary. 139fe6060f1SDimitry Andric /// 14081ad6265SDimitry Andric /// An example of such a tool could be an inspector of the call graph of a 14181ad6265SDimitry Andric /// trace, where each call is represented with its start and end instructions. 14281ad6265SDimitry Andric /// Inspecting all the instructions of a call requires moving to its first 14381ad6265SDimitry Andric /// instruction and then iterating until the last instruction, which following 14481ad6265SDimitry Andric /// the pattern explained above. 14581ad6265SDimitry Andric /// 14681ad6265SDimitry Andric /// Instead of using 0-based indices as identifiers, each Trace plug-in can 14781ad6265SDimitry Andric /// decide the nature of these identifiers and thus no assumptions can be made 14881ad6265SDimitry Andric /// regarding their ordering and sequentiality. The reason is that an 14981ad6265SDimitry Andric /// instruction might be encoded by the plug-in in a way that hides its actual 15081ad6265SDimitry Andric /// 0-based index in the trace, but it's still possible to efficiently find 151fe6060f1SDimitry Andric /// it. 152fe6060f1SDimitry Andric /// 15381ad6265SDimitry Andric /// Requirements: 15481ad6265SDimitry Andric /// - For a given thread, no two instructions have the same id. 15581ad6265SDimitry Andric /// - In terms of efficiency, moving the cursor to a given id should be as 15681ad6265SDimitry Andric /// fast as possible, but not necessarily O(1). That's why the recommended 15781ad6265SDimitry Andric /// way to traverse sequential instructions is to use the \a 15881ad6265SDimitry Andric /// TraceCursor::Next() method and only use \a TraceCursor::GoToId(id) 15981ad6265SDimitry Andric /// sparingly. 16081ad6265SDimitry Andric 16181ad6265SDimitry Andric /// Make the cursor point to the item whose identifier is \p id. 16281ad6265SDimitry Andric /// 163fe6060f1SDimitry Andric /// \return 16481ad6265SDimitry Andric /// \b true if the given identifier exists and the cursor effectively 16581ad6265SDimitry Andric /// moved to it. Otherwise, \b false is returned and the cursor now points 16681ad6265SDimitry Andric /// to an invalid item, i.e. calling \a HasValue() will return \b false. 16781ad6265SDimitry Andric virtual bool GoToId(lldb::user_id_t id) = 0; 16881ad6265SDimitry Andric 16981ad6265SDimitry Andric /// \return 17081ad6265SDimitry Andric /// \b true if and only if there's an instruction item with the given \p 17181ad6265SDimitry Andric /// id. 17281ad6265SDimitry Andric virtual bool HasId(lldb::user_id_t id) const = 0; 17381ad6265SDimitry Andric 17481ad6265SDimitry Andric /// \return 17581ad6265SDimitry Andric /// A unique identifier for the instruction or error this cursor is 17681ad6265SDimitry Andric /// pointing to. 17781ad6265SDimitry Andric virtual lldb::user_id_t GetId() const = 0; 17881ad6265SDimitry Andric /// \} 179fe6060f1SDimitry Andric 180fe6060f1SDimitry Andric /// Make the cursor point to an item in the trace based on an origin point and 18181ad6265SDimitry Andric /// an offset. 182fe6060f1SDimitry Andric /// 183fe6060f1SDimitry Andric /// The resulting position of the trace is 184fe6060f1SDimitry Andric /// origin + offset 185fe6060f1SDimitry Andric /// 18681ad6265SDimitry Andric /// If this resulting position would be out of bounds, the trace then points 18781ad6265SDimitry Andric /// to an invalid item, i.e. calling \a HasValue() returns \b false. 188fe6060f1SDimitry Andric /// 189fe6060f1SDimitry Andric /// \param[in] offset 190fe6060f1SDimitry Andric /// How many items to move forwards (if positive) or backwards (if 19181ad6265SDimitry Andric /// negative) from the given origin point. For example, if origin is \b 19281ad6265SDimitry Andric /// End, then a negative offset would move backward in the trace, but a 19381ad6265SDimitry Andric /// positive offset would move past the trace to an invalid item. 194fe6060f1SDimitry Andric /// 195fe6060f1SDimitry Andric /// \param[in] origin 196fe6060f1SDimitry Andric /// The reference point to use when moving the cursor. 197fe6060f1SDimitry Andric /// 198fe6060f1SDimitry Andric /// \return 19981ad6265SDimitry Andric /// \b true if and only if the cursor ends up pointing to a valid item. 200bdd1243dSDimitry Andric virtual bool Seek(int64_t offset, lldb::TraceCursorSeekType origin) = 0; 201fe6060f1SDimitry Andric 202fe6060f1SDimitry Andric /// \return 203fe6060f1SDimitry Andric /// The \a ExecutionContextRef of the backing thread from the creation time 204fe6060f1SDimitry Andric /// of this cursor. 205fe6060f1SDimitry Andric ExecutionContextRef &GetExecutionContextRef(); 206fe6060f1SDimitry Andric 207972a253aSDimitry Andric /// Trace item information (instructions, errors and events) 208fe6060f1SDimitry Andric /// \{ 209fe6060f1SDimitry Andric 210fe6060f1SDimitry Andric /// \return 21181ad6265SDimitry Andric /// The kind of item the cursor is pointing at. 21281ad6265SDimitry Andric virtual lldb::TraceItemKind GetItemKind() const = 0; 21381ad6265SDimitry Andric 21481ad6265SDimitry Andric /// \return 215fe6060f1SDimitry Andric /// Whether the cursor points to an error or not. 21681ad6265SDimitry Andric bool IsError() const; 217fe6060f1SDimitry Andric 21881ad6265SDimitry Andric /// \return 21981ad6265SDimitry Andric /// The error message the cursor is pointing at. 220*06c3fb27SDimitry Andric virtual llvm::StringRef GetError() const = 0; 22181ad6265SDimitry Andric 22281ad6265SDimitry Andric /// \return 22381ad6265SDimitry Andric /// Whether the cursor points to an event or not. 22481ad6265SDimitry Andric bool IsEvent() const; 22581ad6265SDimitry Andric 22681ad6265SDimitry Andric /// \return 227bdd1243dSDimitry Andric /// The specific kind of event the cursor is pointing at. 22881ad6265SDimitry Andric virtual lldb::TraceEvent GetEventType() const = 0; 22981ad6265SDimitry Andric 23081ad6265SDimitry Andric /// \return 23181ad6265SDimitry Andric /// A human-readable description of the event this cursor is pointing at. 23281ad6265SDimitry Andric const char *GetEventTypeAsString() const; 23381ad6265SDimitry Andric 23481ad6265SDimitry Andric /// \return 23581ad6265SDimitry Andric /// A human-readable description of the given event. 23681ad6265SDimitry Andric static const char *EventKindToString(lldb::TraceEvent event_kind); 23781ad6265SDimitry Andric 23881ad6265SDimitry Andric /// \return 23981ad6265SDimitry Andric /// Whether the cursor points to an instruction. 24081ad6265SDimitry Andric bool IsInstruction() const; 24181ad6265SDimitry Andric 24281ad6265SDimitry Andric /// \return 24381ad6265SDimitry Andric /// The load address of the instruction the cursor is pointing at. 24481ad6265SDimitry Andric virtual lldb::addr_t GetLoadAddress() const = 0; 24581ad6265SDimitry Andric 246753f127fSDimitry Andric /// Get the CPU associated with the current trace item. 247753f127fSDimitry Andric /// 248753f127fSDimitry Andric /// This call might not be O(1), so it's suggested to invoke this method 249972a253aSDimitry Andric /// whenever an eTraceEventCPUChanged event is fired. 250753f127fSDimitry Andric /// 251753f127fSDimitry Andric /// \return 252bdd1243dSDimitry Andric /// The requested CPU id, or LLDB_INVALID_CPU_ID if this information is 253753f127fSDimitry Andric /// not available for the current item. 254bdd1243dSDimitry Andric virtual lldb::cpu_id_t GetCPU() const = 0; 255972a253aSDimitry Andric 256972a253aSDimitry Andric /// Get the last hardware clock value that was emitted before the current 257972a253aSDimitry Andric /// trace item. 258972a253aSDimitry Andric /// 259972a253aSDimitry Andric /// This call might not be O(1), so it's suggested to invoke this method 260972a253aSDimitry Andric /// whenever an eTraceEventHWClockTick event is fired. 261972a253aSDimitry Andric /// 262972a253aSDimitry Andric /// \return 263bdd1243dSDimitry Andric /// The requested HW clock value, or \a std::nullopt if this information 264bdd1243dSDimitry Andric /// is not available for the current item. 265bdd1243dSDimitry Andric virtual std::optional<uint64_t> GetHWClock() const = 0; 266972a253aSDimitry Andric 267972a253aSDimitry Andric /// Get the approximate wall clock time in nanoseconds at which the current 268972a253aSDimitry Andric /// trace item was executed. Each trace plug-in has a different definition for 269972a253aSDimitry Andric /// what time 0 means. 270972a253aSDimitry Andric /// 271972a253aSDimitry Andric /// \return 272bdd1243dSDimitry Andric /// The approximate wall clock time for the trace item, or \a std::nullopt 273972a253aSDimitry Andric /// if not available. 274bdd1243dSDimitry Andric virtual std::optional<double> GetWallClockTime() const = 0; 275bdd1243dSDimitry Andric 276bdd1243dSDimitry Andric /// Get some metadata associated with a synchronization point event. As 277bdd1243dSDimitry Andric /// different trace technologies might have different values for this, 278bdd1243dSDimitry Andric /// we return a string for flexibility. 279bdd1243dSDimitry Andric /// 280bdd1243dSDimitry Andric /// \return 281bdd1243dSDimitry Andric /// A string representing some metadata associated with a 282bdd1243dSDimitry Andric /// \a eTraceEventSyncPoint event. \b std::nullopt if no metadata is 283bdd1243dSDimitry Andric /// available. 284bdd1243dSDimitry Andric virtual std::optional<std::string> GetSyncPointMetadata() const = 0; 285fe6060f1SDimitry Andric /// \} 286fe6060f1SDimitry Andric 287fe6060f1SDimitry Andric protected: 288fe6060f1SDimitry Andric ExecutionContextRef m_exe_ctx_ref; 289fe6060f1SDimitry Andric bool m_forwards = false; 290fe6060f1SDimitry Andric }; 291fe6060f1SDimitry Andric } // namespace lldb_private 292fe6060f1SDimitry Andric 293fe6060f1SDimitry Andric #endif // LLDB_TARGET_TRACE_CURSOR_H 294