xref: /llvm-project/lldb/tools/lldb-dap/LLDBUtils.h (revision e5ba11727437456fbab7ce733c07843bf682fa0c)
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 LLDB_TOOLS_LLDB_DAP_LLDBUTILS_H
10 #define LLDB_TOOLS_LLDB_DAP_LLDBUTILS_H
11 
12 #include "DAPForward.h"
13 #include "lldb/API/SBDebugger.h"
14 #include "lldb/API/SBEnvironment.h"
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/Support/JSON.h"
18 #include "llvm/Support/raw_ostream.h"
19 #include <string>
20 
21 namespace lldb_dap {
22 
23 /// Run a list of LLDB commands in the LLDB command interpreter.
24 ///
25 /// All output from every command, including the prompt + the command
26 /// is placed into the "strm" argument.
27 ///
28 /// Each individual command can be prefixed with \b ! and/or \b ? in no
29 /// particular order. If \b ? is provided, then the output of that command is
30 /// only emitted if it fails, and if \b ! is provided, then the output is
31 /// emitted regardless, and \b false is returned without executing the
32 /// remaining commands.
33 ///
34 /// \param[in] debugger
35 ///     The debugger that will execute the lldb commands.
36 ///
37 /// \param[in] prefix
38 ///     A string that will be printed into \a strm prior to emitting
39 ///     the prompt + command and command output. Can be NULL.
40 ///
41 /// \param[in] commands
42 ///     An array of LLDB commands to execute.
43 ///
44 /// \param[in] strm
45 ///     The stream that will receive the prefix, prompt + command and
46 ///     all command output.
47 ///
48 /// \param[in] parse_command_directives
49 ///     If \b false, then command prefixes like \b ! or \b ? are not parsed and
50 ///     each command is executed verbatim.
51 ///
52 /// \return
53 ///     \b true, unless a command prefixed with \b ! fails and parsing of
54 ///     command directives is enabled.
55 bool RunLLDBCommands(lldb::SBDebugger &debugger, llvm::StringRef prefix,
56                      const llvm::ArrayRef<std::string> &commands,
57                      llvm::raw_ostream &strm, bool parse_command_directives);
58 
59 /// Run a list of LLDB commands in the LLDB command interpreter.
60 ///
61 /// All output from every command, including the prompt + the command
62 /// is returned in the std::string return value.
63 ///
64 /// \param[in] debugger
65 ///     The debugger that will execute the lldb commands.
66 ///
67 /// \param[in] prefix
68 ///     A string that will be printed into \a strm prior to emitting
69 ///     the prompt + command and command output. Can be NULL.
70 ///
71 /// \param[in] commands
72 ///     An array of LLDB commands to execute.
73 ///
74 /// \param[out] required_command_failed
75 ///     If parsing of command directives is enabled, this variable is set to
76 ///     \b true if one of the commands prefixed with \b ! fails.
77 ///
78 /// \param[in] parse_command_directives
79 ///     If \b false, then command prefixes like \b ! or \b ? are not parsed and
80 ///     each command is executed verbatim.
81 ///
82 /// \return
83 ///     A std::string that contains the prefix and all commands and
84 ///     command output.
85 std::string RunLLDBCommands(lldb::SBDebugger &debugger, llvm::StringRef prefix,
86                             const llvm::ArrayRef<std::string> &commands,
87                             bool &required_command_failed,
88                             bool parse_command_directives = true);
89 
90 /// Similar to the method above, but without parsing command directives.
91 std::string
92 RunLLDBCommandsVerbatim(lldb::SBDebugger &debugger, llvm::StringRef prefix,
93                         const llvm::ArrayRef<std::string> &commands);
94 
95 /// Check if a thread has a stop reason.
96 ///
97 /// \param[in] thread
98 ///     The LLDB thread object to check
99 ///
100 /// \return
101 ///     \b True if the thread has a valid stop reason, \b false
102 ///     otherwise.
103 bool ThreadHasStopReason(lldb::SBThread &thread);
104 
105 /// Given a LLDB frame, make a frame ID that is unique to a specific
106 /// thread and frame.
107 ///
108 /// DAP requires a Stackframe "id" to be unique, so we use the frame
109 /// index in the lower 32 bits and the thread index ID in the upper 32
110 /// bits.
111 ///
112 /// \param[in] frame
113 ///     The LLDB stack frame object generate the ID for
114 ///
115 /// \return
116 ///     A unique integer that allows us to easily find the right
117 ///     stack frame within a thread on subsequent VS code requests.
118 int64_t MakeDAPFrameID(lldb::SBFrame &frame);
119 
120 /// Given a DAP frame ID, convert to a LLDB thread index id.
121 ///
122 /// DAP requires a Stackframe "id" to be unique, so we use the frame
123 /// index in the lower THREAD_INDEX_SHIFT bits and the thread index ID in
124 /// the upper 32 - THREAD_INDEX_SHIFT bits.
125 ///
126 /// \param[in] dap_frame_id
127 ///     The DAP frame ID to convert to a thread index ID.
128 ///
129 /// \return
130 ///     The LLDB thread index ID.
131 uint32_t GetLLDBThreadIndexID(uint64_t dap_frame_id);
132 
133 /// Given a DAP frame ID, convert to a LLDB frame ID.
134 ///
135 /// DAP requires a Stackframe "id" to be unique, so we use the frame
136 /// index in the lower THREAD_INDEX_SHIFT bits and the thread index ID in
137 /// the upper 32 - THREAD_INDEX_SHIFT bits.
138 ///
139 /// \param[in] dap_frame_id
140 ///     The DAP frame ID to convert to a frame ID.
141 ///
142 /// \return
143 ///     The LLDB frame index ID.
144 uint32_t GetLLDBFrameID(uint64_t dap_frame_id);
145 
146 /// Gets all the environment variables from the json object depending on if the
147 /// kind is an object or an array.
148 ///
149 /// \param[in] arguments
150 ///     The json object with the launch options
151 ///
152 /// \return
153 ///     The environment variables stored in the env key
154 lldb::SBEnvironment
155 GetEnvironmentFromArguments(const llvm::json::Object &arguments);
156 
157 } // namespace lldb_dap
158 
159 #endif
160