1 //===-- LLDBUtils.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 LLDBVSCODE_LLDBUTILS_H_ 10 #define LLDBVSCODE_LLDBUTILS_H_ 11 12 #include "VSCodeForward.h" 13 #include "llvm/ADT/ArrayRef.h" 14 #include "llvm/ADT/StringRef.h" 15 #include "llvm/Support/raw_ostream.h" 16 #include <string> 17 #include <vector> 18 19 namespace lldb_vscode { 20 21 /// Run a list of LLDB commands in the LLDB command interpreter. 22 /// 23 /// All output from every command, including the prompt + the command 24 /// is placed into the "strm" argument. 25 /// 26 /// \param[in] prefix 27 /// A string that will be printed into \a strm prior to emitting 28 /// the prompt + command and command output. Can be NULL. 29 /// 30 /// \param[in] commands 31 /// An array of LLDB commands to execute. 32 /// 33 /// \param[in] strm 34 /// The stream that will receive the prefix, prompt + command and 35 /// all command output. 36 void RunLLDBCommands(llvm::StringRef prefix, 37 const llvm::ArrayRef<std::string> &commands, 38 llvm::raw_ostream &strm); 39 40 /// Run a list of LLDB commands in the LLDB command interpreter. 41 /// 42 /// All output from every command, including the prompt + the command 43 /// is returned in the std::string return value. 44 /// 45 /// \param[in] prefix 46 /// A string that will be printed into \a strm prior to emitting 47 /// the prompt + command and command output. Can be NULL. 48 /// 49 /// \param[in] commands 50 /// An array of LLDB commands to execute. 51 /// 52 /// \return 53 /// A std::string that contains the prefix and all commands and 54 /// command output 55 std::string RunLLDBCommands(llvm::StringRef prefix, 56 const llvm::ArrayRef<std::string> &commands); 57 58 /// Check if a thread has a stop reason. 59 /// 60 /// \param[in] thread 61 /// The LLDB thread object to check 62 /// 63 /// \return 64 /// \b True if the thread has a valid stop reason, \b false 65 /// otherwise. 66 bool ThreadHasStopReason(lldb::SBThread &thread); 67 68 /// Given a LLDB frame, make a frame ID that is unique to a specific 69 /// thread and frame. 70 /// 71 /// VSCode requires a Stackframe "id" to be unique, so we use the frame 72 /// index in the lower 32 bits and the thread index ID in the upper 32 73 /// bits. 74 /// 75 /// \param[in] frame 76 /// The LLDB stack frame object generate the ID for 77 /// 78 /// \return 79 /// A unique integer that allows us to easily find the right 80 /// stack frame within a thread on subsequent VS code requests. 81 int64_t MakeVSCodeFrameID(lldb::SBFrame &frame); 82 83 /// Given a VSCode frame ID, convert to a LLDB thread index id. 84 /// 85 /// VSCode requires a Stackframe "id" to be unique, so we use the frame 86 /// index in the lower THREAD_INDEX_SHIFT bits and the thread index ID in 87 /// the upper 32 - THREAD_INDEX_SHIFT bits. 88 /// 89 /// \param[in] dap_frame_id 90 /// The VSCode frame ID to convert to a thread index ID. 91 /// 92 /// \return 93 /// The LLDB thread index ID. 94 uint32_t GetLLDBThreadIndexID(uint64_t dap_frame_id); 95 96 /// Given a VSCode frame ID, convert to a LLDB frame ID. 97 /// 98 /// VSCode requires a Stackframe "id" to be unique, so we use the frame 99 /// index in the lower THREAD_INDEX_SHIFT bits and the thread index ID in 100 /// the upper 32 - THREAD_INDEX_SHIFT bits. 101 /// 102 /// \param[in] dap_frame_id 103 /// The VSCode frame ID to convert to a frame ID. 104 /// 105 /// \return 106 /// The LLDB frame index ID. 107 uint32_t GetLLDBFrameID(uint64_t dap_frame_id); 108 109 /// Given a LLDB breakpoint, make a breakpoint ID that is unique to a 110 /// specific breakpoint and breakpoint location. 111 /// 112 /// VSCode requires a Breakpoint "id" to be unique, so we use the 113 /// breakpoint ID in the lower BREAKPOINT_ID_SHIFT bits and the 114 /// breakpoint location ID in the upper BREAKPOINT_ID_SHIFT bits. 115 /// 116 /// \param[in] bp_loc 117 /// The LLDB break point location. 118 /// 119 /// \return 120 /// A unique integer that allows us to easily find the right 121 /// stack frame within a thread on subsequent VS code requests. 122 int64_t MakeVSCodeBreakpointID(lldb::SBBreakpointLocation &bp_loc); 123 124 /// Given a VSCode breakpoint ID, convert to a LLDB breakpoint ID. 125 /// 126 /// VSCode requires a Breakpoint "id" to be unique, so we use the 127 /// breakpoint ID in the lower BREAKPOINT_ID_SHIFT bits and the 128 /// breakpoint location ID in the upper BREAKPOINT_ID_SHIFT bits. 129 /// 130 /// \param[in] dap_breakpoint_id 131 /// The VSCode breakpoint ID to convert to an LLDB breakpoint ID. 132 /// 133 /// \return 134 /// The LLDB breakpoint ID. 135 uint32_t GetLLDBBreakpointID(uint64_t dap_breakpoint_id); 136 137 /// Given a VSCode breakpoint ID, convert to a LLDB breakpoint location ID. 138 /// 139 /// VSCode requires a Breakpoint "id" to be unique, so we use the 140 /// breakpoint ID in the lower BREAKPOINT_ID_SHIFT bits and the 141 /// breakpoint location ID in the upper BREAKPOINT_ID_SHIFT bits. 142 /// 143 /// \param[in] dap_breakpoint_id 144 /// The VSCode frame ID to convert to a breakpoint location ID. 145 /// 146 /// \return 147 /// The LLDB breakpoint location ID. 148 uint32_t GetLLDBBreakpointLocationID(uint64_t dap_breakpoint_id); 149 } // namespace lldb_vscode 150 151 #endif 152