1 //===-- TraceDumper.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 #include "lldb/Target/TraceCursor.h" 10 11 #include "lldb/Symbol/SymbolContext.h" 12 13 #ifndef LLDB_TARGET_TRACE_INSTRUCTION_DUMPER_H 14 #define LLDB_TARGET_TRACE_INSTRUCTION_DUMPER_H 15 16 namespace lldb_private { 17 18 /// Class that holds the configuration used by \a TraceDumper for 19 /// traversing and dumping instructions. 20 struct TraceDumperOptions { 21 /// If \b true, the cursor will be iterated forwards starting from the 22 /// oldest instruction. Otherwise, the iteration starts from the most 23 /// recent instruction. 24 bool forwards = false; 25 /// Dump only instruction addresses without disassembly nor symbol 26 /// information. 27 bool raw = false; 28 /// Dump in json format. 29 bool json = false; 30 /// When dumping in JSON format, pretty print the output. 31 bool pretty_print_json = false; 32 /// For each instruction, print the corresponding timestamp counter if 33 /// available. 34 bool show_tsc = false; 35 /// Dump the events that happened between instructions. 36 bool show_events = false; 37 /// Optional custom id to start traversing from. 38 llvm::Optional<uint64_t> id = llvm::None; 39 /// Optional number of instructions to skip from the starting position 40 /// of the cursor. 41 llvm::Optional<size_t> skip = llvm::None; 42 }; 43 44 /// Class used to dump the instructions of a \a TraceCursor using its current 45 /// state and granularity. 46 class TraceDumper { 47 public: 48 /// Helper struct that holds symbol, disassembly and address information of an 49 /// instruction. 50 struct SymbolInfo { 51 SymbolContext sc; 52 Address address; 53 lldb::DisassemblerSP disassembler; 54 lldb::InstructionSP instruction; 55 lldb_private::ExecutionContext exe_ctx; 56 }; 57 58 /// Helper struct that holds all the information we know about a trace item 59 struct TraceItem { 60 lldb::user_id_t id; 61 lldb::addr_t load_address; 62 llvm::Optional<uint64_t> tsc; 63 llvm::Optional<llvm::StringRef> error; 64 llvm::Optional<lldb::TraceEvent> event; 65 llvm::Optional<SymbolInfo> symbol_info; 66 llvm::Optional<SymbolInfo> prev_symbol_info; 67 }; 68 69 /// Interface used to abstract away the format in which the instruction 70 /// information will be dumped. 71 class OutputWriter { 72 public: 73 virtual ~OutputWriter() = default; 74 75 /// Notify this writer that the cursor ran out of data. 76 virtual void NoMoreData() {} 77 78 /// Dump a trace item (instruction, error or event). 79 virtual void TraceItem(const TraceItem &item) = 0; 80 }; 81 82 /// Create a instruction dumper for the cursor. 83 /// 84 /// \param[in] cursor 85 /// The cursor whose instructions will be dumped. 86 /// 87 /// \param[in] s 88 /// The stream where to dump the instructions to. 89 /// 90 /// \param[in] options 91 /// Additional options for configuring the dumping. 92 TraceDumper(lldb::TraceCursorUP &&cursor_up, Stream &s, 93 const TraceDumperOptions &options); 94 95 /// Dump \a count instructions of the thread trace starting at the current 96 /// cursor position. 97 /// 98 /// This effectively moves the cursor to the next unvisited position, so that 99 /// a subsequent call to this method continues where it left off. 100 /// 101 /// \param[in] count 102 /// The number of instructions to print. 103 /// 104 /// \return 105 /// The instruction id of the last traversed instruction, or \b llvm::None 106 /// if no instructions were visited. 107 llvm::Optional<lldb::user_id_t> DumpInstructions(size_t count); 108 109 private: 110 /// Create a trace item for the current position without symbol information. 111 TraceItem CreatRawTraceItem(); 112 113 lldb::TraceCursorUP m_cursor_up; 114 TraceDumperOptions m_options; 115 std::unique_ptr<OutputWriter> m_writer_up; 116 }; 117 118 } // namespace lldb_private 119 120 #endif // LLDB_TARGET_TRACE_INSTRUCTION_DUMPER_H 121