LLDB mainline
ScriptInterpreter.cpp
Go to the documentation of this file.
1//===-- ScriptInterpreter.cpp ---------------------------------------------===//
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
10#include "lldb/Core/Debugger.h"
12#include "lldb/Host/Pipe.h"
15#include "lldb/Utility/Status.h"
16#include "lldb/Utility/Stream.h"
18#if defined(_WIN32)
20#endif
21#include <cstdio>
22#include <cstdlib>
23#include <memory>
24#include <optional>
25#include <string>
26
27using namespace lldb;
28using namespace lldb_private;
29
31 lldb::ScriptLanguage script_lang)
32 : m_debugger(debugger), m_script_lang(script_lang) {}
33
35 std::vector<std::reference_wrapper<BreakpointOptions>> &bp_options_vec,
36 CommandReturnObject &result) {
37 result.AppendError(
38 "This script interpreter does not support breakpoint callbacks.");
39}
40
42 WatchpointOptions *bp_options, CommandReturnObject &result) {
43 result.AppendError(
44 "This script interpreter does not support watchpoint callbacks.");
45}
46
48 return nullptr;
49}
50
52 const LoadScriptOptions &options,
54 StructuredData::ObjectSP *module_sp,
55 FileSpec extra_search_dir) {
56 error.SetErrorString(
57 "This script interpreter does not support importing modules.");
58 return false;
59}
60
62 switch (language) {
64 return "None";
66 return "Python";
68 return "Lua";
70 return "Unknown";
71 }
72 llvm_unreachable("Unhandled ScriptInterpreter!");
73}
74
77 return data.m_opaque_sp;
78}
79
81 const lldb::SBBreakpoint &breakpoint) const {
82 return breakpoint.m_opaque_wp.lock();
83}
84
86 const lldb::SBAttachInfo &attach_info) const {
87 return attach_info.m_opaque_sp;
88}
89
91 const lldb::SBLaunchInfo &launch_info) const {
92 return std::make_shared<ProcessLaunchInfo>(
93 *reinterpret_cast<ProcessLaunchInfo *>(launch_info.m_opaque_sp.get()));
94}
95
98 if (error.m_opaque_up)
99 return *error.m_opaque_up;
100
101 return Status();
102}
103
104Event *
106 return event.m_opaque_ptr;
107}
108
110 const lldb::SBStream &stream) const {
111 if (stream.m_opaque_up) {
112 lldb::StreamSP s = std::make_shared<lldb_private::StreamString>();
113 *s << reinterpret_cast<StreamString *>(stream.m_opaque_up.get())->m_packet;
114 return s;
115 }
116
117 return nullptr;
118}
119
120std::optional<MemoryRegionInfo>
122 const lldb::SBMemoryRegionInfo &mem_region) const {
123 if (!mem_region.m_opaque_up)
124 return std::nullopt;
125 return *mem_region.m_opaque_up.get();
126}
127
129ScriptInterpreter::StringToLanguage(const llvm::StringRef &language) {
130 if (language.equals_insensitive(LanguageToString(eScriptLanguageNone)))
131 return eScriptLanguageNone;
132 if (language.equals_insensitive(LanguageToString(eScriptLanguagePython)))
134 if (language.equals_insensitive(LanguageToString(eScriptLanguageLua)))
135 return eScriptLanguageLua;
137}
138
140 std::vector<std::reference_wrapper<BreakpointOptions>> &bp_options_vec,
141 const char *callback_text) {
143 for (BreakpointOptions &bp_options : bp_options_vec) {
144 error = SetBreakpointCommandCallback(bp_options, callback_text,
145 /*is_callback=*/false);
146 if (!error.Success())
147 break;
148 }
149 return error;
150}
151
153 std::vector<std::reference_wrapper<BreakpointOptions>> &bp_options_vec,
154 const char *function_name, StructuredData::ObjectSP extra_args_sp) {
156 for (BreakpointOptions &bp_options : bp_options_vec) {
157 error = SetBreakpointCommandCallbackFunction(bp_options, function_name,
158 extra_args_sp);
159 if (!error.Success())
160 return error;
161 }
162 return error;
163}
164
165std::unique_ptr<ScriptInterpreterLocker>
167 return std::make_unique<ScriptInterpreterLocker>();
168}
169
170static void ReadThreadBytesReceived(void *baton, const void *src,
171 size_t src_len) {
172 if (src && src_len) {
173 Stream *strm = (Stream *)baton;
174 strm->Write(src, src_len);
175 strm->Flush();
176 }
177}
178
179llvm::Expected<std::unique_ptr<ScriptInterpreterIORedirect>>
181 CommandReturnObject *result) {
182 if (enable_io)
183 return std::unique_ptr<ScriptInterpreterIORedirect>(
184 new ScriptInterpreterIORedirect(debugger, result));
185
188 if (!nullin)
189 return nullin.takeError();
190
193 if (!nullout)
194 return nullin.takeError();
195
196 return std::unique_ptr<ScriptInterpreterIORedirect>(
197 new ScriptInterpreterIORedirect(std::move(*nullin), std::move(*nullout)));
198}
199
201 std::unique_ptr<File> input, std::unique_ptr<File> output)
202 : m_input_file_sp(std::move(input)),
203 m_output_file_sp(std::make_shared<StreamFile>(std::move(output))),
204 m_error_file_sp(m_output_file_sp),
205 m_communication("lldb.ScriptInterpreterIORedirect.comm"),
206 m_disconnect(false) {}
207
209 Debugger &debugger, CommandReturnObject *result)
210 : m_communication("lldb.ScriptInterpreterIORedirect.comm"),
211 m_disconnect(false) {
212
213 if (result) {
214 m_input_file_sp = debugger.GetInputFileSP();
215
216 Pipe pipe;
217 Status pipe_result = pipe.CreateNew(false);
218#if defined(_WIN32)
219 lldb::file_t read_file = pipe.GetReadNativeHandle();
221 std::unique_ptr<ConnectionGenericFile> conn_up =
222 std::make_unique<ConnectionGenericFile>(read_file, true);
223#else
224 std::unique_ptr<ConnectionFileDescriptor> conn_up =
225 std::make_unique<ConnectionFileDescriptor>(
226 pipe.ReleaseReadFileDescriptor(), true);
227#endif
228
229 if (conn_up->IsConnected()) {
230 m_communication.SetConnection(std::move(conn_up));
234 m_disconnect = true;
235
236 FILE *outfile_handle = fdopen(pipe.ReleaseWriteFileDescriptor(), "w");
237 m_output_file_sp = std::make_shared<StreamFile>(outfile_handle, true);
239 if (outfile_handle)
240 ::setbuf(outfile_handle, nullptr);
241
243 result->SetImmediateErrorFile(debugger.GetErrorStream().GetFileSP());
244 }
245 }
246
250}
251
254 m_output_file_sp->Flush();
255 if (m_error_file_sp)
256 m_error_file_sp->Flush();
257}
258
260 if (!m_disconnect)
261 return;
262
263 assert(m_output_file_sp);
264 assert(m_error_file_sp);
266
267 // Close the write end of the pipe since we are done with our one line
268 // script. This should cause the read thread that output_comm is using to
269 // exit.
270 m_output_file_sp->GetFile().Close();
271 // The close above should cause this thread to exit when it gets to the end
272 // of file, so let it get all its data.
274 // Now we can close the read end of the pipe.
276}
static llvm::raw_ostream & error(Stream &strm)
static void ReadThreadBytesReceived(void *baton, const void *src, size_t src_len)
ProcessAttachInfoSP m_opaque_sp
Definition: SBAttachInfo.h:206
lldb::BreakpointWP m_opaque_wp
Definition: SBBreakpoint.h:170
lldb::DataExtractorSP m_opaque_sp
Definition: SBData.h:159
std::shared_ptr< lldb_private::SBLaunchInfoImpl > m_opaque_sp
Definition: SBLaunchInfo.h:218
lldb::MemoryRegionInfoUP m_opaque_up
std::unique_ptr< lldb_private::Stream > m_opaque_up
Definition: SBStream.h:121
"lldb/Breakpoint/BreakpointOptions.h" Class that manages the options on a breakpoint or breakpoint lo...
void SetImmediateErrorFile(lldb::FileSP file_sp)
void void AppendError(llvm::StringRef in_string)
void SetImmediateOutputFile(lldb::FileSP file_sp)
A class to manage flag bits.
Definition: Debugger.h:80
StreamFile & GetOutputStream()
Definition: Debugger.h:146
lldb::FileSP GetInputFileSP()
Definition: Debugger.h:134
StreamFile & GetErrorStream()
Definition: Debugger.h:148
void AdoptTopIOHandlerFilesIfInvalid(lldb::FileSP &in, lldb::StreamFileSP &out, lldb::StreamFileSP &err)
Definition: Debugger.cpp:1194
A file utility class.
Definition: FileSpec.h:56
static const char * DEV_NULL
Definition: FileSystem.h:32
int Open(const char *path, int flags, int mode=0600)
Wraps ::open in a platform-independent way.
static FileSystem & Instance()
@ eOpenOptionReadOnly
Definition: File.h:51
@ eOpenOptionWriteOnly
Definition: File.h:52
A posix-based implementation of Pipe, a class that abtracts unix style pipes.
Definition: PipePosix.h:21
int ReleaseReadFileDescriptor() override
Definition: PipePosix.cpp:226
int ReleaseWriteFileDescriptor() override
Definition: PipePosix.cpp:237
Status CreateNew(bool child_process_inherit) override
Definition: PipePosix.cpp:80
void Flush()
Flush our output and error file handles.
ScriptInterpreterIORedirect(std::unique_ptr< File > input, std::unique_ptr< File > output)
static llvm::Expected< std::unique_ptr< ScriptInterpreterIORedirect > > Create(bool enable_io, Debugger &debugger, CommandReturnObject *result)
Create an IO redirect.
static lldb::ScriptLanguage StringToLanguage(const llvm::StringRef &string)
virtual void CollectDataForBreakpointCommandCallback(std::vector< std::reference_wrapper< BreakpointOptions > > &options, CommandReturnObject &result)
lldb::ProcessAttachInfoSP GetOpaqueTypeFromSBAttachInfo(const lldb::SBAttachInfo &attach_info) const
virtual bool LoadScriptingModule(const char *filename, const LoadScriptOptions &options, lldb_private::Status &error, StructuredData::ObjectSP *module_sp=nullptr, FileSpec extra_search_dir={})
lldb::StreamSP GetOpaqueTypeFromSBStream(const lldb::SBStream &stream) const
Event * GetOpaqueTypeFromSBEvent(const lldb::SBEvent &event) const
Status SetBreakpointCommandCallback(std::vector< std::reference_wrapper< BreakpointOptions > > &bp_options_vec, const char *callback_text)
Set the specified text as the callback for the breakpoint.
lldb::ProcessLaunchInfoSP GetOpaqueTypeFromSBLaunchInfo(const lldb::SBLaunchInfo &launch_info) const
static std::string LanguageToString(lldb::ScriptLanguage language)
Status GetStatusFromSBError(const lldb::SBError &error) const
std::optional< MemoryRegionInfo > GetOpaqueTypeFromSBMemoryRegionInfo(const lldb::SBMemoryRegionInfo &mem_region) const
virtual std::unique_ptr< ScriptInterpreterLocker > AcquireInterpreterLock()
virtual StructuredData::DictionarySP GetInterpreterInfo()
Status SetBreakpointCommandCallbackFunction(std::vector< std::reference_wrapper< BreakpointOptions > > &bp_options_vec, const char *function_name, StructuredData::ObjectSP extra_args_sp)
lldb::BreakpointSP GetOpaqueTypeFromSBBreakpoint(const lldb::SBBreakpoint &breakpoint) const
lldb::DataExtractorSP GetDataExtractorFromSBData(const lldb::SBData &data) const
ScriptInterpreter(Debugger &debugger, lldb::ScriptLanguage script_lang)
virtual void CollectDataForWatchpointCommandCallback(WatchpointOptions *wp_options, CommandReturnObject &result)
An error handling class.
Definition: Status.h:44
std::shared_ptr< File > GetFileSP()
Definition: StreamFile.h:41
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
size_t Write(const void *src, size_t src_len)
Output character bytes to the stream.
Definition: Stream.h:112
virtual void Flush()=0
Flush the stream.
std::shared_ptr< Dictionary > DictionarySP
std::shared_ptr< Object > ObjectSP
void SetConnection(std::unique_ptr< Connection > connection) override
Sets the connection that it to be used by this class.
lldb::ConnectionStatus Disconnect(Status *error_ptr=nullptr) override
Disconnect the communications connection if one is currently connected.
virtual bool StartReadThread(Status *error_ptr=nullptr)
Starts a read thread whose sole purpose it to read bytes from the current connection.
virtual bool JoinReadThread(Status *error_ptr=nullptr)
void SetReadThreadBytesReceivedCallback(ReadThreadBytesReceived callback, void *callback_baton)
"lldb/Breakpoint/WatchpointOptions.h" Class that manages the options on a watchpoint.
A class that represents a running process on the host machine.
Definition: SBAddress.h:15
int file_t
Definition: lldb-types.h:59
ScriptLanguage
Script interpreter types.
@ eScriptLanguageUnknown
@ eScriptLanguageLua
@ eScriptLanguageNone
@ eScriptLanguagePython
std::shared_ptr< lldb_private::ProcessAttachInfo > ProcessAttachInfoSP
Definition: lldb-forward.h:388
std::shared_ptr< lldb_private::Stream > StreamSP
Definition: lldb-forward.h:428
std::shared_ptr< lldb_private::Breakpoint > BreakpointSP
Definition: lldb-forward.h:319
std::shared_ptr< lldb_private::DataExtractor > DataExtractorSP
Definition: lldb-forward.h:336
std::shared_ptr< lldb_private::ProcessLaunchInfo > ProcessLaunchInfoSP
Definition: lldb-forward.h:389