1 //===-- LLDBUtils.h ---------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLDB_TOOLS_LLDB_DAP_LLDBUTILS_H
10 #define LLDB_TOOLS_LLDB_DAP_LLDBUTILS_H
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"
23 /// Run a list of LLDB commands in the LLDB command interpreter.
25 /// All output from every command, including the prompt + the command
26 /// is placed into the "strm" argument.
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.
34 /// \param[in] debugger
35 /// The debugger that will execute the lldb commands.
38 /// A string that will be printed into \a strm prior to emitting
39 /// the prompt + command and command output. Can be NULL.
41 /// \param[in] commands
42 /// An array of LLDB commands to execute.
45 /// The stream that will receive the prefix, prompt + command and
46 /// all command output.
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.
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
);
59 /// Run a list of LLDB commands in the LLDB command interpreter.
61 /// All output from every command, including the prompt + the command
62 /// is returned in the std::string return value.
64 /// \param[in] debugger
65 /// The debugger that will execute the lldb commands.
68 /// A string that will be printed into \a strm prior to emitting
69 /// the prompt + command and command output. Can be NULL.
71 /// \param[in] commands
72 /// An array of LLDB commands to execute.
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.
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.
83 /// A std::string that contains the prefix and all commands and
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);
90 /// Similar to the method above, but without parsing command directives.
92 RunLLDBCommandsVerbatim(lldb::SBDebugger
&debugger
, llvm::StringRef prefix
,
93 const llvm::ArrayRef
<std::string
> &commands
);
95 /// Check if a thread has a stop reason.
98 /// The LLDB thread object to check
101 /// \b True if the thread has a valid stop reason, \b false
103 bool ThreadHasStopReason(lldb::SBThread
&thread
);
105 /// Given a LLDB frame, make a frame ID that is unique to a specific
106 /// thread and frame.
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
113 /// The LLDB stack frame object generate the ID for
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
);
120 /// Given a DAP frame ID, convert to a LLDB thread index id.
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.
126 /// \param[in] dap_frame_id
127 /// The DAP frame ID to convert to a thread index ID.
130 /// The LLDB thread index ID.
131 uint32_t GetLLDBThreadIndexID(uint64_t dap_frame_id
);
133 /// Given a DAP frame ID, convert to a LLDB frame ID.
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.
139 /// \param[in] dap_frame_id
140 /// The DAP frame ID to convert to a frame ID.
143 /// The LLDB frame index ID.
144 uint32_t GetLLDBFrameID(uint64_t dap_frame_id
);
146 /// Gets all the environment variables from the json object depending on if the
147 /// kind is an object or an array.
149 /// \param[in] arguments
150 /// The json object with the launch options
153 /// The environment variables stored in the env key
155 GetEnvironmentFromArguments(const llvm::json::Object
&arguments
);
157 } // namespace lldb_dap