1*0b57cec5SDimitry Andric //===-- ScriptInterpreter.cpp -----------------------------------*- C++ -*-===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric 
9*0b57cec5SDimitry Andric #include "lldb/Interpreter/ScriptInterpreter.h"
10*0b57cec5SDimitry Andric 
11*0b57cec5SDimitry Andric #include <stdio.h>
12*0b57cec5SDimitry Andric #include <stdlib.h>
13*0b57cec5SDimitry Andric #include <string>
14*0b57cec5SDimitry Andric 
15*0b57cec5SDimitry Andric #include "lldb/Host/PseudoTerminal.h"
16*0b57cec5SDimitry Andric #include "lldb/Interpreter/CommandReturnObject.h"
17*0b57cec5SDimitry Andric #include "lldb/Utility/Status.h"
18*0b57cec5SDimitry Andric #include "lldb/Utility/Stream.h"
19*0b57cec5SDimitry Andric #include "lldb/Utility/StringList.h"
20*0b57cec5SDimitry Andric 
21*0b57cec5SDimitry Andric using namespace lldb;
22*0b57cec5SDimitry Andric using namespace lldb_private;
23*0b57cec5SDimitry Andric 
24*0b57cec5SDimitry Andric ScriptInterpreter::ScriptInterpreter(Debugger &debugger,
25*0b57cec5SDimitry Andric                                      lldb::ScriptLanguage script_lang)
26*0b57cec5SDimitry Andric     : m_debugger(debugger), m_script_lang(script_lang) {}
27*0b57cec5SDimitry Andric 
28*0b57cec5SDimitry Andric ScriptInterpreter::~ScriptInterpreter() {}
29*0b57cec5SDimitry Andric 
30*0b57cec5SDimitry Andric void ScriptInterpreter::CollectDataForBreakpointCommandCallback(
31*0b57cec5SDimitry Andric     std::vector<BreakpointOptions *> &bp_options_vec,
32*0b57cec5SDimitry Andric     CommandReturnObject &result) {
33*0b57cec5SDimitry Andric   result.SetStatus(eReturnStatusFailed);
34*0b57cec5SDimitry Andric   result.AppendError(
35480093f4SDimitry Andric       "This script interpreter does not support breakpoint callbacks.");
36*0b57cec5SDimitry Andric }
37*0b57cec5SDimitry Andric 
38*0b57cec5SDimitry Andric void ScriptInterpreter::CollectDataForWatchpointCommandCallback(
39*0b57cec5SDimitry Andric     WatchpointOptions *bp_options, CommandReturnObject &result) {
40*0b57cec5SDimitry Andric   result.SetStatus(eReturnStatusFailed);
41*0b57cec5SDimitry Andric   result.AppendError(
42480093f4SDimitry Andric       "This script interpreter does not support watchpoint callbacks.");
43480093f4SDimitry Andric }
44480093f4SDimitry Andric 
45480093f4SDimitry Andric bool ScriptInterpreter::LoadScriptingModule(
46480093f4SDimitry Andric     const char *filename, bool init_session, lldb_private::Status &error,
47480093f4SDimitry Andric     StructuredData::ObjectSP *module_sp) {
48480093f4SDimitry Andric   error.SetErrorString(
49480093f4SDimitry Andric       "This script interpreter does not support importing modules.");
50480093f4SDimitry Andric   return false;
51*0b57cec5SDimitry Andric }
52*0b57cec5SDimitry Andric 
53*0b57cec5SDimitry Andric std::string ScriptInterpreter::LanguageToString(lldb::ScriptLanguage language) {
54*0b57cec5SDimitry Andric   switch (language) {
55*0b57cec5SDimitry Andric   case eScriptLanguageNone:
56480093f4SDimitry Andric     return "None";
57*0b57cec5SDimitry Andric   case eScriptLanguagePython:
58480093f4SDimitry Andric     return "Python";
59480093f4SDimitry Andric   case eScriptLanguageLua:
60480093f4SDimitry Andric     return "Lua";
61*0b57cec5SDimitry Andric   case eScriptLanguageUnknown:
62480093f4SDimitry Andric     return "Unknown";
63*0b57cec5SDimitry Andric   }
64480093f4SDimitry Andric   llvm_unreachable("Unhandled ScriptInterpreter!");
65*0b57cec5SDimitry Andric }
66*0b57cec5SDimitry Andric 
67*0b57cec5SDimitry Andric lldb::ScriptLanguage
68*0b57cec5SDimitry Andric ScriptInterpreter::StringToLanguage(const llvm::StringRef &language) {
69*0b57cec5SDimitry Andric   if (language.equals_lower(LanguageToString(eScriptLanguageNone)))
70*0b57cec5SDimitry Andric     return eScriptLanguageNone;
71*0b57cec5SDimitry Andric   if (language.equals_lower(LanguageToString(eScriptLanguagePython)))
72*0b57cec5SDimitry Andric     return eScriptLanguagePython;
73480093f4SDimitry Andric   if (language.equals_lower(LanguageToString(eScriptLanguageLua)))
74480093f4SDimitry Andric     return eScriptLanguageLua;
75*0b57cec5SDimitry Andric   return eScriptLanguageUnknown;
76*0b57cec5SDimitry Andric }
77*0b57cec5SDimitry Andric 
78*0b57cec5SDimitry Andric Status ScriptInterpreter::SetBreakpointCommandCallback(
79*0b57cec5SDimitry Andric     std::vector<BreakpointOptions *> &bp_options_vec,
80*0b57cec5SDimitry Andric     const char *callback_text) {
81*0b57cec5SDimitry Andric   Status return_error;
82*0b57cec5SDimitry Andric   for (BreakpointOptions *bp_options : bp_options_vec) {
83*0b57cec5SDimitry Andric     return_error = SetBreakpointCommandCallback(bp_options, callback_text);
84*0b57cec5SDimitry Andric     if (return_error.Success())
85*0b57cec5SDimitry Andric       break;
86*0b57cec5SDimitry Andric   }
87*0b57cec5SDimitry Andric   return return_error;
88*0b57cec5SDimitry Andric }
89*0b57cec5SDimitry Andric 
90480093f4SDimitry Andric Status ScriptInterpreter::SetBreakpointCommandCallbackFunction(
91480093f4SDimitry Andric     std::vector<BreakpointOptions *> &bp_options_vec, const char *function_name,
92480093f4SDimitry Andric     StructuredData::ObjectSP extra_args_sp) {
93480093f4SDimitry Andric   Status error;
94*0b57cec5SDimitry Andric   for (BreakpointOptions *bp_options : bp_options_vec) {
95480093f4SDimitry Andric     error = SetBreakpointCommandCallbackFunction(bp_options, function_name,
96480093f4SDimitry Andric                                                  extra_args_sp);
97480093f4SDimitry Andric     if (!error.Success())
98480093f4SDimitry Andric       return error;
99*0b57cec5SDimitry Andric   }
100480093f4SDimitry Andric   return error;
101*0b57cec5SDimitry Andric }
102*0b57cec5SDimitry Andric 
103*0b57cec5SDimitry Andric std::unique_ptr<ScriptInterpreterLocker>
104*0b57cec5SDimitry Andric ScriptInterpreter::AcquireInterpreterLock() {
105*0b57cec5SDimitry Andric   return std::unique_ptr<ScriptInterpreterLocker>(
106*0b57cec5SDimitry Andric       new ScriptInterpreterLocker());
107*0b57cec5SDimitry Andric }
108