1 //===-- CommandObjectScript.cpp ---------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "CommandObjectScript.h" 11 12 // C Includes 13 // C++ Includes 14 // Other libraries and framework includes 15 // Project includes 16 17 #include "lldb/Core/Debugger.h" 18 19 #include "lldb/DataFormatters/DataVisualization.h" 20 21 #include "lldb/Interpreter/Args.h" 22 #include "lldb/Interpreter/CommandInterpreter.h" 23 #include "lldb/Interpreter/CommandReturnObject.h" 24 #include "lldb/Interpreter/ScriptInterpreter.h" 25 26 using namespace lldb; 27 using namespace lldb_private; 28 29 //------------------------------------------------------------------------- 30 // CommandObjectScript 31 //------------------------------------------------------------------------- 32 33 CommandObjectScript::CommandObjectScript(CommandInterpreter &interpreter, ScriptLanguage script_lang) 34 : CommandObjectRaw(interpreter, "script", "Invoke the script interpreter with provided code and display any " 35 "results. Start the interactive interpreter if no code is supplied.", 36 "script [<script-code>]") 37 { 38 } 39 40 CommandObjectScript::~CommandObjectScript () 41 { 42 } 43 44 bool 45 CommandObjectScript::DoExecute 46 ( 47 const char *command, 48 CommandReturnObject &result 49 ) 50 { 51 #ifdef LLDB_DISABLE_PYTHON 52 // if we ever support languages other than Python this simple #ifdef won't work 53 result.AppendError("your copy of LLDB does not support scripting."); 54 result.SetStatus (eReturnStatusFailed); 55 return false; 56 #else 57 if (m_interpreter.GetDebugger().GetScriptLanguage() == lldb::eScriptLanguageNone) 58 { 59 result.AppendError("the script-lang setting is set to none - scripting not available"); 60 result.SetStatus (eReturnStatusFailed); 61 return false; 62 } 63 64 ScriptInterpreter *script_interpreter = m_interpreter.GetScriptInterpreter (); 65 66 if (script_interpreter == nullptr) 67 { 68 result.AppendError("no script interpreter"); 69 result.SetStatus (eReturnStatusFailed); 70 return false; 71 } 72 73 DataVisualization::ForceUpdate(); // script might change Python code we use for formatting.. make sure we keep up to date with it 74 75 if (command == nullptr || command[0] == '\0') 76 { 77 script_interpreter->ExecuteInterpreterLoop (); 78 result.SetStatus (eReturnStatusSuccessFinishNoResult); 79 return result.Succeeded(); 80 } 81 82 // We can do better when reporting the status of one-liner script execution. 83 if (script_interpreter->ExecuteOneLine (command, &result)) 84 result.SetStatus(eReturnStatusSuccessFinishNoResult); 85 else 86 result.SetStatus(eReturnStatusFailed); 87 88 return result.Succeeded(); 89 #endif 90 } 91