15ffd83dbSDimitry Andric //===-- ScriptInterpreter.cpp ---------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "lldb/Interpreter/ScriptInterpreter.h"
105ffd83dbSDimitry Andric #include "lldb/Core/Debugger.h"
115ffd83dbSDimitry Andric #include "lldb/Host/ConnectionFileDescriptor.h"
125ffd83dbSDimitry Andric #include "lldb/Host/Pipe.h"
130b57cec5SDimitry Andric #include "lldb/Host/PseudoTerminal.h"
140b57cec5SDimitry Andric #include "lldb/Interpreter/CommandReturnObject.h"
150b57cec5SDimitry Andric #include "lldb/Utility/Status.h"
160b57cec5SDimitry Andric #include "lldb/Utility/Stream.h"
170b57cec5SDimitry Andric #include "lldb/Utility/StringList.h"
185ffd83dbSDimitry Andric #if defined(_WIN32)
195ffd83dbSDimitry Andric #include "lldb/Host/windows/ConnectionGenericFileWindows.h"
205ffd83dbSDimitry Andric #endif
21fe6060f1SDimitry Andric #include <cstdio>
22fe6060f1SDimitry Andric #include <cstdlib>
235ffd83dbSDimitry Andric #include <memory>
24bdd1243dSDimitry Andric #include <optional>
255ffd83dbSDimitry Andric #include <string>
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric using namespace lldb;
280b57cec5SDimitry Andric using namespace lldb_private;
290b57cec5SDimitry Andric 
ScriptInterpreter(Debugger & debugger,lldb::ScriptLanguage script_lang)30*c9157d92SDimitry Andric ScriptInterpreter::ScriptInterpreter(Debugger &debugger,
31*c9157d92SDimitry Andric                                      lldb::ScriptLanguage script_lang)
32*c9157d92SDimitry Andric     : m_debugger(debugger), m_script_lang(script_lang) {}
330b57cec5SDimitry Andric 
CollectDataForBreakpointCommandCallback(std::vector<std::reference_wrapper<BreakpointOptions>> & bp_options_vec,CommandReturnObject & result)340b57cec5SDimitry Andric void ScriptInterpreter::CollectDataForBreakpointCommandCallback(
35fe6060f1SDimitry Andric     std::vector<std::reference_wrapper<BreakpointOptions>> &bp_options_vec,
360b57cec5SDimitry Andric     CommandReturnObject &result) {
370b57cec5SDimitry Andric   result.AppendError(
38480093f4SDimitry Andric       "This script interpreter does not support breakpoint callbacks.");
390b57cec5SDimitry Andric }
400b57cec5SDimitry Andric 
CollectDataForWatchpointCommandCallback(WatchpointOptions * bp_options,CommandReturnObject & result)410b57cec5SDimitry Andric void ScriptInterpreter::CollectDataForWatchpointCommandCallback(
420b57cec5SDimitry Andric     WatchpointOptions *bp_options, CommandReturnObject &result) {
430b57cec5SDimitry Andric   result.AppendError(
44480093f4SDimitry Andric       "This script interpreter does not support watchpoint callbacks.");
45480093f4SDimitry Andric }
46480093f4SDimitry Andric 
GetInterpreterInfo()47349cc55cSDimitry Andric StructuredData::DictionarySP ScriptInterpreter::GetInterpreterInfo() {
48349cc55cSDimitry Andric   return nullptr;
49349cc55cSDimitry Andric }
50349cc55cSDimitry Andric 
LoadScriptingModule(const char * filename,const LoadScriptOptions & options,lldb_private::Status & error,StructuredData::ObjectSP * module_sp,FileSpec extra_search_dir)51e8d8bef9SDimitry Andric bool ScriptInterpreter::LoadScriptingModule(const char *filename,
52fe6060f1SDimitry Andric                                             const LoadScriptOptions &options,
53e8d8bef9SDimitry Andric                                             lldb_private::Status &error,
54e8d8bef9SDimitry Andric                                             StructuredData::ObjectSP *module_sp,
55e8d8bef9SDimitry Andric                                             FileSpec extra_search_dir) {
56480093f4SDimitry Andric   error.SetErrorString(
57480093f4SDimitry Andric       "This script interpreter does not support importing modules.");
58480093f4SDimitry Andric   return false;
590b57cec5SDimitry Andric }
600b57cec5SDimitry Andric 
LanguageToString(lldb::ScriptLanguage language)610b57cec5SDimitry Andric std::string ScriptInterpreter::LanguageToString(lldb::ScriptLanguage language) {
620b57cec5SDimitry Andric   switch (language) {
630b57cec5SDimitry Andric   case eScriptLanguageNone:
64480093f4SDimitry Andric     return "None";
650b57cec5SDimitry Andric   case eScriptLanguagePython:
66480093f4SDimitry Andric     return "Python";
67480093f4SDimitry Andric   case eScriptLanguageLua:
68480093f4SDimitry Andric     return "Lua";
690b57cec5SDimitry Andric   case eScriptLanguageUnknown:
70480093f4SDimitry Andric     return "Unknown";
710b57cec5SDimitry Andric   }
72480093f4SDimitry Andric   llvm_unreachable("Unhandled ScriptInterpreter!");
730b57cec5SDimitry Andric }
740b57cec5SDimitry Andric 
75fe6060f1SDimitry Andric lldb::DataExtractorSP
GetDataExtractorFromSBData(const lldb::SBData & data) const76fe6060f1SDimitry Andric ScriptInterpreter::GetDataExtractorFromSBData(const lldb::SBData &data) const {
77fe6060f1SDimitry Andric   return data.m_opaque_sp;
78fe6060f1SDimitry Andric }
79fe6060f1SDimitry Andric 
GetOpaqueTypeFromSBBreakpoint(const lldb::SBBreakpoint & breakpoint) const80fe013be4SDimitry Andric lldb::BreakpointSP ScriptInterpreter::GetOpaqueTypeFromSBBreakpoint(
81fe013be4SDimitry Andric     const lldb::SBBreakpoint &breakpoint) const {
82fe013be4SDimitry Andric   return breakpoint.m_opaque_wp.lock();
83fe013be4SDimitry Andric }
84fe013be4SDimitry Andric 
GetOpaqueTypeFromSBAttachInfo(const lldb::SBAttachInfo & attach_info) const85fe013be4SDimitry Andric lldb::ProcessAttachInfoSP ScriptInterpreter::GetOpaqueTypeFromSBAttachInfo(
86fe013be4SDimitry Andric     const lldb::SBAttachInfo &attach_info) const {
87fe013be4SDimitry Andric   return attach_info.m_opaque_sp;
88fe013be4SDimitry Andric }
89fe013be4SDimitry Andric 
GetOpaqueTypeFromSBLaunchInfo(const lldb::SBLaunchInfo & launch_info) const90fe013be4SDimitry Andric lldb::ProcessLaunchInfoSP ScriptInterpreter::GetOpaqueTypeFromSBLaunchInfo(
91fe013be4SDimitry Andric     const lldb::SBLaunchInfo &launch_info) const {
92fe013be4SDimitry Andric   return std::make_shared<ProcessLaunchInfo>(
93fe013be4SDimitry Andric       *reinterpret_cast<ProcessLaunchInfo *>(launch_info.m_opaque_sp.get()));
94fe013be4SDimitry Andric }
95fe013be4SDimitry Andric 
96fe6060f1SDimitry Andric Status
GetStatusFromSBError(const lldb::SBError & error) const97fe6060f1SDimitry Andric ScriptInterpreter::GetStatusFromSBError(const lldb::SBError &error) const {
98fe6060f1SDimitry Andric   if (error.m_opaque_up)
99bdd1243dSDimitry Andric     return *error.m_opaque_up;
100fe6060f1SDimitry Andric 
101fe6060f1SDimitry Andric   return Status();
102fe6060f1SDimitry Andric }
103fe6060f1SDimitry Andric 
104bdd1243dSDimitry Andric std::optional<MemoryRegionInfo>
GetOpaqueTypeFromSBMemoryRegionInfo(const lldb::SBMemoryRegionInfo & mem_region) const105349cc55cSDimitry Andric ScriptInterpreter::GetOpaqueTypeFromSBMemoryRegionInfo(
106349cc55cSDimitry Andric     const lldb::SBMemoryRegionInfo &mem_region) const {
107349cc55cSDimitry Andric   if (!mem_region.m_opaque_up)
108bdd1243dSDimitry Andric     return std::nullopt;
109349cc55cSDimitry Andric   return *mem_region.m_opaque_up.get();
110349cc55cSDimitry Andric }
111349cc55cSDimitry Andric 
1120b57cec5SDimitry Andric lldb::ScriptLanguage
StringToLanguage(const llvm::StringRef & language)1130b57cec5SDimitry Andric ScriptInterpreter::StringToLanguage(const llvm::StringRef &language) {
114fe6060f1SDimitry Andric   if (language.equals_insensitive(LanguageToString(eScriptLanguageNone)))
1150b57cec5SDimitry Andric     return eScriptLanguageNone;
116fe6060f1SDimitry Andric   if (language.equals_insensitive(LanguageToString(eScriptLanguagePython)))
1170b57cec5SDimitry Andric     return eScriptLanguagePython;
118fe6060f1SDimitry Andric   if (language.equals_insensitive(LanguageToString(eScriptLanguageLua)))
119480093f4SDimitry Andric     return eScriptLanguageLua;
1200b57cec5SDimitry Andric   return eScriptLanguageUnknown;
1210b57cec5SDimitry Andric }
1220b57cec5SDimitry Andric 
SetBreakpointCommandCallback(std::vector<std::reference_wrapper<BreakpointOptions>> & bp_options_vec,const char * callback_text)1230b57cec5SDimitry Andric Status ScriptInterpreter::SetBreakpointCommandCallback(
124fe6060f1SDimitry Andric     std::vector<std::reference_wrapper<BreakpointOptions>> &bp_options_vec,
1250b57cec5SDimitry Andric     const char *callback_text) {
12681ad6265SDimitry Andric   Status error;
127fe6060f1SDimitry Andric   for (BreakpointOptions &bp_options : bp_options_vec) {
128fe013be4SDimitry Andric     error = SetBreakpointCommandCallback(bp_options, callback_text,
129fe013be4SDimitry Andric                                          /*is_callback=*/false);
13081ad6265SDimitry Andric     if (!error.Success())
1310b57cec5SDimitry Andric       break;
1320b57cec5SDimitry Andric   }
13381ad6265SDimitry Andric   return error;
1340b57cec5SDimitry Andric }
1350b57cec5SDimitry Andric 
SetBreakpointCommandCallbackFunction(std::vector<std::reference_wrapper<BreakpointOptions>> & bp_options_vec,const char * function_name,StructuredData::ObjectSP extra_args_sp)136480093f4SDimitry Andric Status ScriptInterpreter::SetBreakpointCommandCallbackFunction(
137fe6060f1SDimitry Andric     std::vector<std::reference_wrapper<BreakpointOptions>> &bp_options_vec,
138fe6060f1SDimitry Andric     const char *function_name, StructuredData::ObjectSP extra_args_sp) {
139480093f4SDimitry Andric   Status error;
140fe6060f1SDimitry Andric   for (BreakpointOptions &bp_options : bp_options_vec) {
141480093f4SDimitry Andric     error = SetBreakpointCommandCallbackFunction(bp_options, function_name,
142480093f4SDimitry Andric                                                  extra_args_sp);
143480093f4SDimitry Andric     if (!error.Success())
144480093f4SDimitry Andric       return error;
1450b57cec5SDimitry Andric   }
146480093f4SDimitry Andric   return error;
1470b57cec5SDimitry Andric }
1480b57cec5SDimitry Andric 
1490b57cec5SDimitry Andric std::unique_ptr<ScriptInterpreterLocker>
AcquireInterpreterLock()1500b57cec5SDimitry Andric ScriptInterpreter::AcquireInterpreterLock() {
1515ffd83dbSDimitry Andric   return std::make_unique<ScriptInterpreterLocker>();
1525ffd83dbSDimitry Andric }
1535ffd83dbSDimitry Andric 
ReadThreadBytesReceived(void * baton,const void * src,size_t src_len)1545ffd83dbSDimitry Andric static void ReadThreadBytesReceived(void *baton, const void *src,
1555ffd83dbSDimitry Andric                                     size_t src_len) {
1565ffd83dbSDimitry Andric   if (src && src_len) {
1575ffd83dbSDimitry Andric     Stream *strm = (Stream *)baton;
1585ffd83dbSDimitry Andric     strm->Write(src, src_len);
1595ffd83dbSDimitry Andric     strm->Flush();
1605ffd83dbSDimitry Andric   }
1615ffd83dbSDimitry Andric }
1625ffd83dbSDimitry Andric 
1635ffd83dbSDimitry Andric llvm::Expected<std::unique_ptr<ScriptInterpreterIORedirect>>
Create(bool enable_io,Debugger & debugger,CommandReturnObject * result)1645ffd83dbSDimitry Andric ScriptInterpreterIORedirect::Create(bool enable_io, Debugger &debugger,
1655ffd83dbSDimitry Andric                                     CommandReturnObject *result) {
1665ffd83dbSDimitry Andric   if (enable_io)
1675ffd83dbSDimitry Andric     return std::unique_ptr<ScriptInterpreterIORedirect>(
1685ffd83dbSDimitry Andric         new ScriptInterpreterIORedirect(debugger, result));
1695ffd83dbSDimitry Andric 
1705ffd83dbSDimitry Andric   auto nullin = FileSystem::Instance().Open(FileSpec(FileSystem::DEV_NULL),
171349cc55cSDimitry Andric                                             File::eOpenOptionReadOnly);
1725ffd83dbSDimitry Andric   if (!nullin)
1735ffd83dbSDimitry Andric     return nullin.takeError();
1745ffd83dbSDimitry Andric 
1755ffd83dbSDimitry Andric   auto nullout = FileSystem::Instance().Open(FileSpec(FileSystem::DEV_NULL),
176349cc55cSDimitry Andric                                              File::eOpenOptionWriteOnly);
1775ffd83dbSDimitry Andric   if (!nullout)
1785ffd83dbSDimitry Andric     return nullin.takeError();
1795ffd83dbSDimitry Andric 
1805ffd83dbSDimitry Andric   return std::unique_ptr<ScriptInterpreterIORedirect>(
1815ffd83dbSDimitry Andric       new ScriptInterpreterIORedirect(std::move(*nullin), std::move(*nullout)));
1825ffd83dbSDimitry Andric }
1835ffd83dbSDimitry Andric 
ScriptInterpreterIORedirect(std::unique_ptr<File> input,std::unique_ptr<File> output)1845ffd83dbSDimitry Andric ScriptInterpreterIORedirect::ScriptInterpreterIORedirect(
1855ffd83dbSDimitry Andric     std::unique_ptr<File> input, std::unique_ptr<File> output)
1865ffd83dbSDimitry Andric     : m_input_file_sp(std::move(input)),
1875ffd83dbSDimitry Andric       m_output_file_sp(std::make_shared<StreamFile>(std::move(output))),
1885ffd83dbSDimitry Andric       m_error_file_sp(m_output_file_sp),
1895ffd83dbSDimitry Andric       m_communication("lldb.ScriptInterpreterIORedirect.comm"),
1905ffd83dbSDimitry Andric       m_disconnect(false) {}
1915ffd83dbSDimitry Andric 
ScriptInterpreterIORedirect(Debugger & debugger,CommandReturnObject * result)1925ffd83dbSDimitry Andric ScriptInterpreterIORedirect::ScriptInterpreterIORedirect(
1935ffd83dbSDimitry Andric     Debugger &debugger, CommandReturnObject *result)
1945ffd83dbSDimitry Andric     : m_communication("lldb.ScriptInterpreterIORedirect.comm"),
1955ffd83dbSDimitry Andric       m_disconnect(false) {
1965ffd83dbSDimitry Andric 
1975ffd83dbSDimitry Andric   if (result) {
1985ffd83dbSDimitry Andric     m_input_file_sp = debugger.GetInputFileSP();
1995ffd83dbSDimitry Andric 
2005ffd83dbSDimitry Andric     Pipe pipe;
2015ffd83dbSDimitry Andric     Status pipe_result = pipe.CreateNew(false);
2025ffd83dbSDimitry Andric #if defined(_WIN32)
2035ffd83dbSDimitry Andric     lldb::file_t read_file = pipe.GetReadNativeHandle();
2045ffd83dbSDimitry Andric     pipe.ReleaseReadFileDescriptor();
2055ffd83dbSDimitry Andric     std::unique_ptr<ConnectionGenericFile> conn_up =
2065ffd83dbSDimitry Andric         std::make_unique<ConnectionGenericFile>(read_file, true);
2075ffd83dbSDimitry Andric #else
2085ffd83dbSDimitry Andric     std::unique_ptr<ConnectionFileDescriptor> conn_up =
2095ffd83dbSDimitry Andric         std::make_unique<ConnectionFileDescriptor>(
2105ffd83dbSDimitry Andric             pipe.ReleaseReadFileDescriptor(), true);
2115ffd83dbSDimitry Andric #endif
2125ffd83dbSDimitry Andric 
2135ffd83dbSDimitry Andric     if (conn_up->IsConnected()) {
2145ffd83dbSDimitry Andric       m_communication.SetConnection(std::move(conn_up));
2155ffd83dbSDimitry Andric       m_communication.SetReadThreadBytesReceivedCallback(
2165ffd83dbSDimitry Andric           ReadThreadBytesReceived, &result->GetOutputStream());
2175ffd83dbSDimitry Andric       m_communication.StartReadThread();
2185ffd83dbSDimitry Andric       m_disconnect = true;
2195ffd83dbSDimitry Andric 
2205ffd83dbSDimitry Andric       FILE *outfile_handle = fdopen(pipe.ReleaseWriteFileDescriptor(), "w");
2215ffd83dbSDimitry Andric       m_output_file_sp = std::make_shared<StreamFile>(outfile_handle, true);
2225ffd83dbSDimitry Andric       m_error_file_sp = m_output_file_sp;
2235ffd83dbSDimitry Andric       if (outfile_handle)
2245ffd83dbSDimitry Andric         ::setbuf(outfile_handle, nullptr);
2255ffd83dbSDimitry Andric 
2265ffd83dbSDimitry Andric       result->SetImmediateOutputFile(debugger.GetOutputStream().GetFileSP());
2275ffd83dbSDimitry Andric       result->SetImmediateErrorFile(debugger.GetErrorStream().GetFileSP());
2285ffd83dbSDimitry Andric     }
2295ffd83dbSDimitry Andric   }
2305ffd83dbSDimitry Andric 
2315ffd83dbSDimitry Andric   if (!m_input_file_sp || !m_output_file_sp || !m_error_file_sp)
2325ffd83dbSDimitry Andric     debugger.AdoptTopIOHandlerFilesIfInvalid(m_input_file_sp, m_output_file_sp,
2335ffd83dbSDimitry Andric                                              m_error_file_sp);
2345ffd83dbSDimitry Andric }
2355ffd83dbSDimitry Andric 
Flush()2365ffd83dbSDimitry Andric void ScriptInterpreterIORedirect::Flush() {
2375ffd83dbSDimitry Andric   if (m_output_file_sp)
2385ffd83dbSDimitry Andric     m_output_file_sp->Flush();
2395ffd83dbSDimitry Andric   if (m_error_file_sp)
2405ffd83dbSDimitry Andric     m_error_file_sp->Flush();
2415ffd83dbSDimitry Andric }
2425ffd83dbSDimitry Andric 
~ScriptInterpreterIORedirect()2435ffd83dbSDimitry Andric ScriptInterpreterIORedirect::~ScriptInterpreterIORedirect() {
2445ffd83dbSDimitry Andric   if (!m_disconnect)
2455ffd83dbSDimitry Andric     return;
2465ffd83dbSDimitry Andric 
2475ffd83dbSDimitry Andric   assert(m_output_file_sp);
2485ffd83dbSDimitry Andric   assert(m_error_file_sp);
2495ffd83dbSDimitry Andric   assert(m_output_file_sp == m_error_file_sp);
2505ffd83dbSDimitry Andric 
2515ffd83dbSDimitry Andric   // Close the write end of the pipe since we are done with our one line
2525ffd83dbSDimitry Andric   // script. This should cause the read thread that output_comm is using to
2535ffd83dbSDimitry Andric   // exit.
2545ffd83dbSDimitry Andric   m_output_file_sp->GetFile().Close();
2555ffd83dbSDimitry Andric   // The close above should cause this thread to exit when it gets to the end
2565ffd83dbSDimitry Andric   // of file, so let it get all its data.
2575ffd83dbSDimitry Andric   m_communication.JoinReadThread();
2585ffd83dbSDimitry Andric   // Now we can close the read end of the pipe.
2595ffd83dbSDimitry Andric   m_communication.Disconnect();
2600b57cec5SDimitry Andric }
261