1 //===-- ScriptInterpreter.cpp -----------------------------------*- 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 #include "lldb/Interpreter/ScriptInterpreter.h"
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string>
14 
15 #include "lldb/Host/PseudoTerminal.h"
16 #include "lldb/Interpreter/CommandReturnObject.h"
17 #include "lldb/Utility/Status.h"
18 #include "lldb/Utility/Stream.h"
19 #include "lldb/Utility/StringList.h"
20 
21 using namespace lldb;
22 using namespace lldb_private;
23 
24 ScriptInterpreter::ScriptInterpreter(Debugger &debugger,
25                                      lldb::ScriptLanguage script_lang)
26     : m_debugger(debugger), m_script_lang(script_lang) {}
27 
28 ScriptInterpreter::~ScriptInterpreter() {}
29 
30 void ScriptInterpreter::CollectDataForBreakpointCommandCallback(
31     std::vector<BreakpointOptions *> &bp_options_vec,
32     CommandReturnObject &result) {
33   result.SetStatus(eReturnStatusFailed);
34   result.AppendError(
35       "This script interpreter does not support breakpoint callbacks.");
36 }
37 
38 void ScriptInterpreter::CollectDataForWatchpointCommandCallback(
39     WatchpointOptions *bp_options, CommandReturnObject &result) {
40   result.SetStatus(eReturnStatusFailed);
41   result.AppendError(
42       "This script interpreter does not support watchpoint callbacks.");
43 }
44 
45 bool ScriptInterpreter::LoadScriptingModule(
46     const char *filename, bool can_reload, bool init_session,
47     lldb_private::Status &error, StructuredData::ObjectSP *module_sp) {
48   error.SetErrorString(
49       "This script interpreter does not support importing modules.");
50   return false;
51 }
52 
53 std::string ScriptInterpreter::LanguageToString(lldb::ScriptLanguage language) {
54   switch (language) {
55   case eScriptLanguageNone:
56     return "None";
57   case eScriptLanguagePython:
58     return "Python";
59   case eScriptLanguageLua:
60     return "Lua";
61   case eScriptLanguageUnknown:
62     return "Unknown";
63   }
64 }
65 
66 lldb::ScriptLanguage
67 ScriptInterpreter::StringToLanguage(const llvm::StringRef &language) {
68   if (language.equals_lower(LanguageToString(eScriptLanguageNone)))
69     return eScriptLanguageNone;
70   if (language.equals_lower(LanguageToString(eScriptLanguagePython)))
71     return eScriptLanguagePython;
72   if (language.equals_lower(LanguageToString(eScriptLanguageLua)))
73     return eScriptLanguageLua;
74   return eScriptLanguageUnknown;
75 }
76 
77 Status ScriptInterpreter::SetBreakpointCommandCallback(
78     std::vector<BreakpointOptions *> &bp_options_vec,
79     const char *callback_text) {
80   Status return_error;
81   for (BreakpointOptions *bp_options : bp_options_vec) {
82     return_error = SetBreakpointCommandCallback(bp_options, callback_text);
83     if (return_error.Success())
84       break;
85   }
86   return return_error;
87 }
88 
89 Status ScriptInterpreter::SetBreakpointCommandCallbackFunction(
90     std::vector<BreakpointOptions *> &bp_options_vec, const char *function_name,
91     StructuredData::ObjectSP extra_args_sp) {
92   Status error;
93   for (BreakpointOptions *bp_options : bp_options_vec) {
94     error = SetBreakpointCommandCallbackFunction(bp_options, function_name,
95                                                  extra_args_sp);
96     if (!error.Success())
97       return error;
98   }
99   return error;
100 }
101 
102 std::unique_ptr<ScriptInterpreterLocker>
103 ScriptInterpreter::AcquireInterpreterLock() {
104   return std::unique_ptr<ScriptInterpreterLocker>(
105       new ScriptInterpreterLocker());
106 }
107