Skip to content

Commit 410de0c

Browse files
authored
[lldb/Commands] Alias script command to scripting run (#97263)
This patch introduces a new top-level `scripting` command with an `run` sub-command, that basically replaces the `script` raw command. To avoid breaking the `script` command usages, this patch also adds an `script` alias to the `scripting run` sub-command. The reason behind this change is to have a top-level command that will cover scripting related subcommands. Signed-off-by: Med Ismail Bennani <[email protected]>
1 parent 471ca94 commit 410de0c

File tree

8 files changed

+189
-160
lines changed

8 files changed

+189
-160
lines changed

lldb/source/Commands/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ add_lldb_library(lldbCommands NO_PLUGIN_DEPENDENCIES
2626
CommandObjectQuit.cpp
2727
CommandObjectRegexCommand.cpp
2828
CommandObjectRegister.cpp
29-
CommandObjectScript.cpp
29+
CommandObjectScripting.cpp
3030
CommandObjectSession.cpp
3131
CommandObjectSettings.cpp
3232
CommandObjectSource.cpp

lldb/source/Commands/CommandObjectScript.cpp

Lines changed: 0 additions & 113 deletions
This file was deleted.

lldb/source/Commands/CommandObjectScript.h

Lines changed: 0 additions & 42 deletions
This file was deleted.
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
//===-- CommandObjectScripting.cpp ----------------------------------------===//
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 "CommandObjectScripting.h"
10+
#include "lldb/Core/Debugger.h"
11+
#include "lldb/DataFormatters/DataVisualization.h"
12+
#include "lldb/Host/Config.h"
13+
#include "lldb/Host/OptionParser.h"
14+
#include "lldb/Interpreter/CommandInterpreter.h"
15+
#include "lldb/Interpreter/CommandOptionArgumentTable.h"
16+
#include "lldb/Interpreter/CommandReturnObject.h"
17+
#include "lldb/Interpreter/OptionArgParser.h"
18+
#include "lldb/Interpreter/ScriptInterpreter.h"
19+
#include "lldb/Utility/Args.h"
20+
21+
using namespace lldb;
22+
using namespace lldb_private;
23+
24+
#define LLDB_OPTIONS_scripting_run
25+
#include "CommandOptions.inc"
26+
27+
class CommandObjectScriptingRun : public CommandObjectRaw {
28+
public:
29+
CommandObjectScriptingRun(CommandInterpreter &interpreter)
30+
: CommandObjectRaw(
31+
interpreter, "scripting run",
32+
"Invoke the script interpreter with provided code and display any "
33+
"results. Start the interactive interpreter if no code is "
34+
"supplied.",
35+
"scripting run [--language <scripting-language> --] "
36+
"[<script-code>]") {}
37+
38+
~CommandObjectScriptingRun() override = default;
39+
40+
Options *GetOptions() override { return &m_options; }
41+
42+
class CommandOptions : public Options {
43+
public:
44+
CommandOptions() = default;
45+
~CommandOptions() override = default;
46+
Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
47+
ExecutionContext *execution_context) override {
48+
Status error;
49+
const int short_option = m_getopt_table[option_idx].val;
50+
51+
switch (short_option) {
52+
case 'l':
53+
language = (lldb::ScriptLanguage)OptionArgParser::ToOptionEnum(
54+
option_arg, GetDefinitions()[option_idx].enum_values,
55+
eScriptLanguageNone, error);
56+
if (!error.Success())
57+
error.SetErrorStringWithFormat("unrecognized value for language '%s'",
58+
option_arg.str().c_str());
59+
break;
60+
default:
61+
llvm_unreachable("Unimplemented option");
62+
}
63+
64+
return error;
65+
}
66+
67+
void OptionParsingStarting(ExecutionContext *execution_context) override {
68+
language = lldb::eScriptLanguageNone;
69+
}
70+
71+
llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
72+
return llvm::ArrayRef(g_scripting_run_options);
73+
}
74+
75+
lldb::ScriptLanguage language = lldb::eScriptLanguageNone;
76+
};
77+
78+
protected:
79+
void DoExecute(llvm::StringRef command,
80+
CommandReturnObject &result) override {
81+
// Try parsing the language option but when the command contains a raw part
82+
// separated by the -- delimiter.
83+
OptionsWithRaw raw_args(command);
84+
if (raw_args.HasArgs()) {
85+
if (!ParseOptions(raw_args.GetArgs(), result))
86+
return;
87+
command = raw_args.GetRawPart();
88+
}
89+
90+
lldb::ScriptLanguage language =
91+
(m_options.language == lldb::eScriptLanguageNone)
92+
? m_interpreter.GetDebugger().GetScriptLanguage()
93+
: m_options.language;
94+
95+
if (language == lldb::eScriptLanguageNone) {
96+
result.AppendError(
97+
"the script-lang setting is set to none - scripting not available");
98+
return;
99+
}
100+
101+
ScriptInterpreter *script_interpreter =
102+
GetDebugger().GetScriptInterpreter(true, language);
103+
104+
if (script_interpreter == nullptr) {
105+
result.AppendError("no script interpreter");
106+
return;
107+
}
108+
109+
// Script might change Python code we use for formatting. Make sure we keep
110+
// up to date with it.
111+
DataVisualization::ForceUpdate();
112+
113+
if (command.empty()) {
114+
script_interpreter->ExecuteInterpreterLoop();
115+
result.SetStatus(eReturnStatusSuccessFinishNoResult);
116+
return;
117+
}
118+
119+
// We can do better when reporting the status of one-liner script execution.
120+
if (script_interpreter->ExecuteOneLine(command, &result))
121+
result.SetStatus(eReturnStatusSuccessFinishNoResult);
122+
else
123+
result.SetStatus(eReturnStatusFailed);
124+
}
125+
126+
private:
127+
CommandOptions m_options;
128+
};
129+
130+
#pragma mark CommandObjectMultiwordScripting
131+
132+
// CommandObjectMultiwordScripting
133+
134+
CommandObjectMultiwordScripting::CommandObjectMultiwordScripting(
135+
CommandInterpreter &interpreter)
136+
: CommandObjectMultiword(
137+
interpreter, "scripting",
138+
"Commands for operating on the scripting functionnalities.",
139+
"scripting <subcommand> [<subcommand-options>]") {
140+
LoadSubCommand("run",
141+
CommandObjectSP(new CommandObjectScriptingRun(interpreter)));
142+
}
143+
144+
CommandObjectMultiwordScripting::~CommandObjectMultiwordScripting() = default;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===-- CommandObjectScripting.h --------------------------------*- 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+
#ifndef LLDB_SOURCE_INTERPRETER_COMMANDOBJECTSCRIPTING_H
10+
#define LLDB_SOURCE_INTERPRETER_COMMANDOBJECTSCRIPTING_H
11+
12+
#include "lldb/Interpreter/CommandObjectMultiword.h"
13+
14+
namespace lldb_private {
15+
16+
class CommandObjectMultiwordScripting : public CommandObjectMultiword {
17+
public:
18+
CommandObjectMultiwordScripting(CommandInterpreter &interpreter);
19+
20+
~CommandObjectMultiwordScripting() override;
21+
};
22+
23+
} // namespace lldb_private
24+
25+
#endif // LLDB_SOURCE_INTERPRETER_COMMANDOBJECTSCRIPTING_H

lldb/source/Commands/Options.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,7 @@ let Command = "container add" in {
835835
Desc<"Overwrite an existing command at this node.">;
836836
}
837837

838-
let Command = "script" in {
838+
let Command = "scripting run" in {
839839
def script_language : Option<"language", "l">,
840840
EnumArg<"ScriptLang">, Desc<"Specify the scripting "
841841
" language. If none is specific the default scripting language is used.">;

lldb/source/Interpreter/CommandInterpreter.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
#include "Commands/CommandObjectQuit.h"
3434
#include "Commands/CommandObjectRegexCommand.h"
3535
#include "Commands/CommandObjectRegister.h"
36-
#include "Commands/CommandObjectScript.h"
36+
#include "Commands/CommandObjectScripting.h"
3737
#include "Commands/CommandObjectSession.h"
3838
#include "Commands/CommandObjectSettings.h"
3939
#include "Commands/CommandObjectSource.h"
@@ -518,6 +518,15 @@ void CommandInterpreter::Initialize() {
518518
AddAlias("re", cmd_obj_sp);
519519
}
520520

521+
cmd_obj_sp = GetCommandSPExact("scripting run");
522+
if (cmd_obj_sp) {
523+
AddAlias("sc", cmd_obj_sp);
524+
AddAlias("scr", cmd_obj_sp);
525+
AddAlias("scri", cmd_obj_sp);
526+
AddAlias("scrip", cmd_obj_sp);
527+
AddAlias("script", cmd_obj_sp);
528+
}
529+
521530
cmd_obj_sp = GetCommandSPExact("session history");
522531
if (cmd_obj_sp) {
523532
AddAlias("history", cmd_obj_sp);
@@ -569,7 +578,7 @@ void CommandInterpreter::LoadCommandDictionary() {
569578
REGISTER_COMMAND_OBJECT("process", CommandObjectMultiwordProcess);
570579
REGISTER_COMMAND_OBJECT("quit", CommandObjectQuit);
571580
REGISTER_COMMAND_OBJECT("register", CommandObjectRegister);
572-
REGISTER_COMMAND_OBJECT("script", CommandObjectScript);
581+
REGISTER_COMMAND_OBJECT("scripting", CommandObjectMultiwordScripting);
573582
REGISTER_COMMAND_OBJECT("settings", CommandObjectMultiwordSettings);
574583
REGISTER_COMMAND_OBJECT("session", CommandObjectSession);
575584
REGISTER_COMMAND_OBJECT("source", CommandObjectMultiwordSource);

lldb/test/API/functionalities/abbreviation/TestAbbreviations.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,13 @@ def test_command_abbreviations_and_aliases(self):
8080
# Check a command that wants the raw input.
8181
command_interpreter.ResolveCommand(r"""sc print("\n\n\tHello!\n")""", result)
8282
self.assertTrue(result.Succeeded())
83-
self.assertEqual(r"""script print("\n\n\tHello!\n")""", result.GetOutput())
83+
self.assertEqual(
84+
r"""scripting run print("\n\n\tHello!\n")""", result.GetOutput()
85+
)
86+
87+
command_interpreter.ResolveCommand("script 1+1", result)
88+
self.assertTrue(result.Succeeded())
89+
self.assertEqual("scripting run 1+1", result.GetOutput())
8490

8591
# Prompt changing stuff should be tested, but this doesn't seem like the
8692
# right test to do it in. It has nothing to do with aliases or abbreviations.

0 commit comments

Comments
 (0)