88
99#include " CommandObjectScripting.h"
1010#include " lldb/Core/Debugger.h"
11+ #include " lldb/Core/PluginManager.h"
1112#include " lldb/DataFormatters/DataVisualization.h"
1213#include " lldb/Host/Config.h"
1314#include " lldb/Host/OptionParser.h"
1415#include " lldb/Interpreter/CommandInterpreter.h"
1516#include " lldb/Interpreter/CommandOptionArgumentTable.h"
1617#include " lldb/Interpreter/CommandReturnObject.h"
18+ #include " lldb/Interpreter/Interfaces/ScriptedInterfaceUsages.h"
1719#include " lldb/Interpreter/OptionArgParser.h"
1820#include " lldb/Interpreter/ScriptInterpreter.h"
1921#include " lldb/Utility/Args.h"
@@ -127,9 +129,127 @@ class CommandObjectScriptingRun : public CommandObjectRaw {
127129 CommandOptions m_options;
128130};
129131
130- #pragma mark CommandObjectMultiwordScripting
132+ #define LLDB_OPTIONS_scripting_template_list
133+ #include " CommandOptions.inc"
134+
135+ class CommandObjectScriptingTemplateList : public CommandObjectParsed {
136+ public:
137+ CommandObjectScriptingTemplateList (CommandInterpreter &interpreter)
138+ : CommandObjectParsed(
139+ interpreter, " scripting template list" ,
140+ " List all the available scripting extension templates. " ,
141+ " scripting template list [--language <scripting-language> --]" ) {}
142+
143+ ~CommandObjectScriptingTemplateList () override = default ;
144+
145+ Options *GetOptions () override { return &m_options; }
146+
147+ class CommandOptions : public Options {
148+ public:
149+ CommandOptions () = default ;
150+ ~CommandOptions () override = default ;
151+ Status SetOptionValue (uint32_t option_idx, llvm::StringRef option_arg,
152+ ExecutionContext *execution_context) override {
153+ Status error;
154+ const int short_option = m_getopt_table[option_idx].val ;
131155
132- // CommandObjectMultiwordScripting
156+ switch (short_option) {
157+ case ' l' :
158+ m_language = (lldb::ScriptLanguage)OptionArgParser::ToOptionEnum (
159+ option_arg, GetDefinitions ()[option_idx].enum_values ,
160+ eScriptLanguageNone, error);
161+ if (!error.Success ())
162+ error.SetErrorStringWithFormat (" unrecognized value for language '%s'" ,
163+ option_arg.str ().c_str ());
164+ break ;
165+ default :
166+ llvm_unreachable (" Unimplemented option" );
167+ }
168+
169+ return error;
170+ }
171+
172+ void OptionParsingStarting (ExecutionContext *execution_context) override {
173+ m_language = lldb::eScriptLanguageDefault;
174+ }
175+
176+ llvm::ArrayRef<OptionDefinition> GetDefinitions () override {
177+ return llvm::ArrayRef (g_scripting_template_list_options);
178+ }
179+
180+ lldb::ScriptLanguage m_language = lldb::eScriptLanguageDefault;
181+ };
182+
183+ protected:
184+ void DoExecute (Args &command, CommandReturnObject &result) override {
185+ Stream &s = result.GetOutputStream ();
186+ s.Printf (" Available scripted extension templates:" );
187+
188+ auto print_field = [&s](llvm::StringRef key, llvm::StringRef value,
189+ bool check_validity = false ) {
190+ if (!check_validity || !value.empty ()) {
191+ s.IndentMore ();
192+ s.Indent ();
193+ s << key << " : " << value << ' \n ' ;
194+ s.IndentLess ();
195+ }
196+ };
197+
198+ size_t num_listed_interface = 0 ;
199+ size_t num_templates = PluginManager::GetNumScriptedInterfaces ();
200+ for (size_t i = 0 ; i < num_templates; i++) {
201+ llvm::StringRef plugin_name =
202+ PluginManager::GetScriptedInterfaceNameAtIndex (i);
203+ if (plugin_name.empty ())
204+ break ;
205+
206+ lldb::ScriptLanguage lang =
207+ PluginManager::GetScriptedInterfaceLanguageAtIndex (i);
208+ if (lang != m_options.m_language )
209+ continue ;
210+
211+ if (!num_listed_interface)
212+ s.EOL ();
213+
214+ num_listed_interface++;
215+
216+ llvm::StringRef desc =
217+ PluginManager::GetScriptedInterfaceDescriptionAtIndex (i);
218+ ScriptedInterfaceUsages usages =
219+ PluginManager::GetScriptedInterfaceUsagesAtIndex (i);
220+
221+ print_field (" Name" , plugin_name);
222+ print_field (" Language" , ScriptInterpreter::LanguageToString (lang));
223+ print_field (" Description" , desc);
224+ usages.Dump (s, ScriptedInterfaceUsages::UsageKind::API);
225+ usages.Dump (s, ScriptedInterfaceUsages::UsageKind::CommandInterpreter);
226+
227+ if (i != num_templates - 1 )
228+ s.EOL ();
229+ }
230+
231+ if (!num_listed_interface)
232+ s << " None\n " ;
233+ }
234+
235+ private:
236+ CommandOptions m_options;
237+ };
238+
239+ class CommandObjectMultiwordScriptingTemplate : public CommandObjectMultiword {
240+ public:
241+ CommandObjectMultiwordScriptingTemplate (CommandInterpreter &interpreter)
242+ : CommandObjectMultiword(
243+ interpreter, " scripting template" ,
244+ " Commands for operating on the scripting templates." ,
245+ " scripting template [<subcommand-options>]" ) {
246+ LoadSubCommand (
247+ " list" ,
248+ CommandObjectSP (new CommandObjectScriptingTemplateList (interpreter)));
249+ }
250+
251+ ~CommandObjectMultiwordScriptingTemplate () override = default ;
252+ };
133253
134254CommandObjectMultiwordScripting::CommandObjectMultiwordScripting (
135255 CommandInterpreter &interpreter)
@@ -139,6 +259,9 @@ CommandObjectMultiwordScripting::CommandObjectMultiwordScripting(
139259 " scripting <subcommand> [<subcommand-options>]" ) {
140260 LoadSubCommand (" run" ,
141261 CommandObjectSP (new CommandObjectScriptingRun (interpreter)));
262+ LoadSubCommand (" template" ,
263+ CommandObjectSP (
264+ new CommandObjectMultiwordScriptingTemplate (interpreter)));
142265}
143266
144267CommandObjectMultiwordScripting::~CommandObjectMultiwordScripting () = default ;
0 commit comments