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_VSCODE_LLDBUTILS_H
10 #define LLDB_TOOLS_LLDB_VSCODE_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 } // namespace lldb_vscode
110 
111 #endif
112