CommandObjectScript.cpp revision 353358
1//===-- CommandObjectScript.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 "CommandObjectScript.h"
10
11
12#include "lldb/Core/Debugger.h"
13
14#include "lldb/DataFormatters/DataVisualization.h"
15
16#include "lldb/Interpreter/CommandInterpreter.h"
17#include "lldb/Interpreter/CommandReturnObject.h"
18#include "lldb/Interpreter/ScriptInterpreter.h"
19#include "lldb/Utility/Args.h"
20
21using namespace lldb;
22using namespace lldb_private;
23
24// CommandObjectScript
25
26CommandObjectScript::CommandObjectScript(CommandInterpreter &interpreter,
27                                         ScriptLanguage script_lang)
28    : CommandObjectRaw(
29          interpreter, "script",
30          "Invoke the script interpreter with provided code and display any "
31          "results.  Start the interactive interpreter if no code is supplied.",
32          "script [<script-code>]") {}
33
34CommandObjectScript::~CommandObjectScript() {}
35
36bool CommandObjectScript::DoExecute(llvm::StringRef command,
37                                    CommandReturnObject &result) {
38#ifdef LLDB_DISABLE_PYTHON
39  // if we ever support languages other than Python this simple #ifdef won't
40  // work
41  result.AppendError("your copy of LLDB does not support scripting.");
42  result.SetStatus(eReturnStatusFailed);
43  return false;
44#else
45  if (m_interpreter.GetDebugger().GetScriptLanguage() ==
46      lldb::eScriptLanguageNone) {
47    result.AppendError(
48        "the script-lang setting is set to none - scripting not available");
49    result.SetStatus(eReturnStatusFailed);
50    return false;
51  }
52
53  ScriptInterpreter *script_interpreter = GetDebugger().GetScriptInterpreter();
54
55  if (script_interpreter == nullptr) {
56    result.AppendError("no script interpreter");
57    result.SetStatus(eReturnStatusFailed);
58    return false;
59  }
60
61  DataVisualization::ForceUpdate(); // script might change Python code we use
62                                    // for formatting.. make sure we keep up to
63                                    // date with it
64
65  if (command.empty()) {
66    script_interpreter->ExecuteInterpreterLoop();
67    result.SetStatus(eReturnStatusSuccessFinishNoResult);
68    return result.Succeeded();
69  }
70
71  // We can do better when reporting the status of one-liner script execution.
72  if (script_interpreter->ExecuteOneLine(command, &result))
73    result.SetStatus(eReturnStatusSuccessFinishNoResult);
74  else
75    result.SetStatus(eReturnStatusFailed);
76
77  return result.Succeeded();
78#endif
79}
80