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
50
52 const char *filename, const LoadScriptOptions &options,
54 FileSpec extra_search_dir, lldb::TargetSP loaded_into_target_sp) {
56 "This script interpreter does not support importing modules.");
57 return false;
58}
59
61 switch (language) {
63 return "None";
65 return "Python";
67 return "Lua";
69 return "Unknown";
70 }
71 llvm_unreachable("Unhandled ScriptInterpreter!");
72}
73
78
80 const lldb::SBBreakpoint &breakpoint) const {
81 return breakpoint.m_opaque_wp.lock();
82}
83
86 const lldb::SBBreakpointLocation &break_loc) const {
87 return break_loc.m_opaque_wp.lock();
88}
89
94
96 const lldb::SBLaunchInfo &launch_info) const {
97 return std::make_shared<ProcessLaunchInfo>(
98 *reinterpret_cast<ProcessLaunchInfo *>(launch_info.m_opaque_sp.get()));
99}
100
101Status
103 if (error.m_opaque_up)
104 return error.m_opaque_up->Clone();
105
106 return Status();
107}
108
111 if (frame.m_opaque_sp)
112 return frame.m_opaque_sp->GetFrameSP();
113 return nullptr;
114}
115
116Event *
118 return event.m_opaque_ptr;
119}
120
122 const lldb::SBStream &stream) const {
123 if (stream.m_opaque_up) {
124 lldb::StreamSP s = std::make_shared<lldb_private::StreamString>();
125 *s << reinterpret_cast<StreamString *>(stream.m_opaque_up.get())->m_packet;
126 return s;
127 }
128
129 return nullptr;
130}
131
133 const lldb::SBSymbolContext &sb_sym_ctx) const {
134 if (sb_sym_ctx.m_opaque_up)
135 return *sb_sym_ctx.m_opaque_up;
136 return {};
137}
138
139std::optional<MemoryRegionInfo>
141 const lldb::SBMemoryRegionInfo &mem_region) const {
142 if (!mem_region.m_opaque_up)
143 return std::nullopt;
144 return *mem_region.m_opaque_up.get();
145}
146
152
154ScriptInterpreter::StringToLanguage(const llvm::StringRef &language) {
155 if (language.equals_insensitive(LanguageToString(eScriptLanguageNone)))
156 return eScriptLanguageNone;
157 if (language.equals_insensitive(LanguageToString(eScriptLanguagePython)))
159 if (language.equals_insensitive(LanguageToString(eScriptLanguageLua)))
160 return eScriptLanguageLua;
162}
163
165 std::vector<std::reference_wrapper<BreakpointOptions>> &bp_options_vec,
166 const char *callback_text) {
168 for (BreakpointOptions &bp_options : bp_options_vec) {
169 error = SetBreakpointCommandCallback(bp_options, callback_text,
170 /*is_callback=*/false);
171 if (!error.Success())
172 break;
173 }
174 return error;
175}
176
178 std::vector<std::reference_wrapper<BreakpointOptions>> &bp_options_vec,
179 const char *function_name, StructuredData::ObjectSP extra_args_sp) {
181 for (BreakpointOptions &bp_options : bp_options_vec) {
182 error = SetBreakpointCommandCallbackFunction(bp_options, function_name,
183 extra_args_sp);
184 if (!error.Success())
185 return error;
186 }
187 return error;
188}
189
190std::unique_ptr<ScriptInterpreterLocker>
192 return std::make_unique<ScriptInterpreterLocker>();
193}
194
195static void ReadThreadBytesReceived(void *baton, const void *src,
196 size_t src_len) {
197 if (src && src_len) {
198 Stream *strm = (Stream *)baton;
199 strm->Write(src, src_len);
200 strm->Flush();
201 }
202}
203
204llvm::Expected<std::unique_ptr<ScriptInterpreterIORedirect>>
206 CommandReturnObject *result) {
207 if (enable_io)
208 return std::unique_ptr<ScriptInterpreterIORedirect>(
209 new ScriptInterpreterIORedirect(debugger, result));
210
213 if (!nullin)
214 return nullin.takeError();
215
218 if (!nullout)
219 return nullin.takeError();
220
221 return std::unique_ptr<ScriptInterpreterIORedirect>(
222 new ScriptInterpreterIORedirect(std::move(*nullin), std::move(*nullout)));
223}
224
226 std::unique_ptr<File> input, std::unique_ptr<File> output)
227 : m_input_file_sp(std::move(input)),
228 m_output_file_sp(std::make_shared<LockableStreamFile>(std::move(output),
231 m_communication("lldb.ScriptInterpreterIORedirect.comm"),
232 m_disconnect(false) {}
233
235 Debugger &debugger, CommandReturnObject *result)
236 : m_communication("lldb.ScriptInterpreterIORedirect.comm"),
237 m_disconnect(false) {
238
239 if (result) {
240 m_input_file_sp = debugger.GetInputFileSP();
241
242 Pipe pipe;
243 Status pipe_result = pipe.CreateNew();
244#if defined(_WIN32)
245 lldb::file_t read_file = pipe.GetReadNativeHandle();
247 std::unique_ptr<ConnectionGenericFile> conn_up =
248 std::make_unique<ConnectionGenericFile>(read_file, true);
249#else
250 std::unique_ptr<ConnectionFileDescriptor> conn_up =
251 std::make_unique<ConnectionFileDescriptor>(
252 pipe.ReleaseReadFileDescriptor(), true);
253#endif
254
255 if (conn_up->IsConnected()) {
256 m_communication.SetConnection(std::move(conn_up));
257 m_communication.SetReadThreadBytesReceivedCallback(
259 m_communication.StartReadThread();
260 m_disconnect = true;
261
262 FILE *outfile_handle = fdopen(pipe.ReleaseWriteFileDescriptor(), "w");
263 m_output_file_sp = std::make_shared<LockableStreamFile>(
264 std::make_shared<StreamFile>(outfile_handle, NativeFile::Owned),
267 if (outfile_handle)
268 ::setbuf(outfile_handle, nullptr);
269
270 result->SetImmediateOutputFile(debugger.GetOutputFileSP());
271 result->SetImmediateErrorFile(debugger.GetErrorFileSP());
272 }
273 }
274
278}
279
282 m_output_file_sp->Lock().Flush();
283 if (m_error_file_sp)
284 m_error_file_sp->Lock().Flush();
285}
286
288 if (!m_disconnect)
289 return;
290
291 assert(m_output_file_sp);
292 assert(m_error_file_sp);
294
295 // Close the write end of the pipe since we are done with our one line
296 // script. This should cause the read thread that output_comm is using to
297 // exit.
298 m_output_file_sp->GetUnlockedFile().Close();
299 // The close above should cause this thread to exit when it gets to the end
300 // of file, so let it get all its data.
301 m_communication.JoinReadThread();
302 // Now we can close the read end of the pipe.
303 m_communication.Disconnect();
304}
static llvm::raw_ostream & error(Stream &strm)
static void ReadThreadBytesReceived(void *baton, const void *src, size_t src_len)
ProcessAttachInfoSP m_opaque_sp
lldb::BreakpointLocationWP m_opaque_wp
lldb::BreakpointWP m_opaque_wp
lldb::DataExtractorSP m_opaque_sp
Definition SBData.h:159
lldb::ExecutionContextRefSP m_exe_ctx_sp
lldb::ExecutionContextRefSP m_opaque_sp
Definition SBFrame.h:243
std::shared_ptr< lldb_private::SBLaunchInfoImpl > m_opaque_sp
lldb::MemoryRegionInfoUP m_opaque_up
std::unique_ptr< lldb_private::Stream > m_opaque_up
Definition SBStream.h:121
std::unique_ptr< lldb_private::SymbolContext > m_opaque_up
"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
lldb::FileSP GetInputFileSP()
Definition Debugger.h:136
lldb::FileSP GetErrorFileSP()
Definition Debugger.h:143
lldb::FileSP GetOutputFileSP()
Definition Debugger.h:139
void AdoptTopIOHandlerFilesIfInvalid(lldb::FileSP &in, lldb::LockableStreamFileSP &out, lldb::LockableStreamFileSP &err)
A file utility class.
Definition FileSpec.h:57
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
Status CreateNew() override
Definition PipePosix.cpp:82
int ReleaseReadFileDescriptor() override
int ReleaseWriteFileDescriptor() override
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
lldb::StreamSP GetOpaqueTypeFromSBStream(const lldb::SBStream &stream) const
lldb::ExecutionContextRefSP GetOpaqueTypeFromSBExecutionContext(const lldb::SBExecutionContext &exe_ctx) const
SymbolContext GetOpaqueTypeFromSBSymbolContext(const lldb::SBSymbolContext &sym_ctx) 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
virtual bool LoadScriptingModule(const char *filename, const LoadScriptOptions &options, lldb_private::Status &error, StructuredData::ObjectSP *module_sp=nullptr, FileSpec extra_search_dir={}, lldb::TargetSP loaded_into_target_sp={})
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()
lldb::BreakpointLocationSP GetOpaqueTypeFromSBBreakpointLocation(const lldb::SBBreakpointLocation &break_loc) const
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::StackFrameSP GetOpaqueTypeFromSBFrame(const lldb::SBFrame &frame) 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:118
static Status FromErrorString(const char *str)
Definition Status.h:141
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
Defines a symbol context baton that can be handed other debug core functions.
"lldb/Breakpoint/WatchpointOptions.h" Class that manages the options on a watchpoint.
A class that represents a running process on the host machine.
PipePosix Pipe
Definition Pipe.h:20
int file_t
Definition lldb-types.h:59
ScriptLanguage
Script interpreter types.
@ eScriptLanguageUnknown
@ eScriptLanguageNone
@ eScriptLanguagePython
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
std::shared_ptr< lldb_private::BreakpointLocation > BreakpointLocationSP
std::shared_ptr< lldb_private::ProcessAttachInfo > ProcessAttachInfoSP
std::shared_ptr< lldb_private::Stream > StreamSP
std::shared_ptr< lldb_private::Breakpoint > BreakpointSP
std::shared_ptr< lldb_private::Target > TargetSP
std::shared_ptr< lldb_private::DataExtractor > DataExtractorSP
std::shared_ptr< lldb_private::ProcessLaunchInfo > ProcessLaunchInfoSP
std::shared_ptr< lldb_private::ExecutionContextRef > ExecutionContextRefSP