1ebc09c36SJim Ingham //===-- CommandObjectSource.cpp ---------------------------------*- C++ -*-===// 2ebc09c36SJim Ingham // 3ebc09c36SJim Ingham // The LLVM Compiler Infrastructure 4ebc09c36SJim Ingham // 5ebc09c36SJim Ingham // This file is distributed under the University of Illinois Open Source 6ebc09c36SJim Ingham // License. See LICENSE.TXT for details. 7ebc09c36SJim Ingham // 8ebc09c36SJim Ingham //===----------------------------------------------------------------------===// 9ebc09c36SJim Ingham 10ebc09c36SJim Ingham // C Includes 11ebc09c36SJim Ingham // C++ Includes 12ebc09c36SJim Ingham // Other libraries and framework includes 130e5e5a79SGreg Clayton #include "llvm/ADT/StringRef.h" 140e5e5a79SGreg Clayton 15ebc09c36SJim Ingham // Project includes 166e3d8e7fSEugene Zelenko #include "CommandObjectCommands.h" 1746d4aa21SEnrico Granata #include "CommandObjectHelp.h" 18ebc09c36SJim Ingham #include "lldb/Core/Debugger.h" 1944d93782SGreg Clayton #include "lldb/Core/IOHandler.h" 20be93a35aSEnrico Granata #include "lldb/Core/StringList.h" 21de164aaaSGreg Clayton #include "lldb/Interpreter/Args.h" 227594f14fSEnrico Granata #include "lldb/Interpreter/CommandHistory.h" 23ebc09c36SJim Ingham #include "lldb/Interpreter/CommandInterpreter.h" 24de164aaaSGreg Clayton #include "lldb/Interpreter/CommandObjectRegexCommand.h" 25ebc09c36SJim Ingham #include "lldb/Interpreter/CommandReturnObject.h" 26012d4fcaSEnrico Granata #include "lldb/Interpreter/OptionValueBoolean.h" 2745d0e238SEnrico Granata #include "lldb/Interpreter/OptionValueString.h" 287594f14fSEnrico Granata #include "lldb/Interpreter/OptionValueUInt64.h" 29ebc09c36SJim Ingham #include "lldb/Interpreter/Options.h" 3099f0b8f9SEnrico Granata #include "lldb/Interpreter/ScriptInterpreter.h" 31ebc09c36SJim Ingham 32ebc09c36SJim Ingham using namespace lldb; 33ebc09c36SJim Ingham using namespace lldb_private; 34ebc09c36SJim Ingham 35ebc09c36SJim Ingham //------------------------------------------------------------------------- 36ebc09c36SJim Ingham // CommandObjectCommandsSource 37ebc09c36SJim Ingham //------------------------------------------------------------------------- 38ebc09c36SJim Ingham 391f0f5b5bSZachary Turner static OptionDefinition g_history_options[] = { 401f0f5b5bSZachary Turner // clang-format off 411f0f5b5bSZachary Turner { LLDB_OPT_SET_1, false, "count", 'c', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeUnsignedInteger, "How many history commands to print." }, 421f0f5b5bSZachary Turner { LLDB_OPT_SET_1, false, "start-index", 's', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeUnsignedInteger, "Index at which to start printing history commands (or end to mean tail mode)." }, 431f0f5b5bSZachary Turner { LLDB_OPT_SET_1, false, "end-index", 'e', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeUnsignedInteger, "Index at which to stop printing history commands." }, 441f0f5b5bSZachary Turner { LLDB_OPT_SET_2, false, "clear", 'C', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeBoolean, "Clears the current command history." }, 451f0f5b5bSZachary Turner // clang-format on 461f0f5b5bSZachary Turner }; 471f0f5b5bSZachary Turner 48b9c1b51eSKate Stone class CommandObjectCommandsHistory : public CommandObjectParsed { 495a988416SJim Ingham public: 50b9c1b51eSKate Stone CommandObjectCommandsHistory(CommandInterpreter &interpreter) 51b9c1b51eSKate Stone : CommandObjectParsed(interpreter, "command history", 525a988416SJim Ingham "Dump the history of commands in this session.", 536e3d8e7fSEugene Zelenko nullptr), 54b9c1b51eSKate Stone m_options() {} 555a988416SJim Ingham 566e3d8e7fSEugene Zelenko ~CommandObjectCommandsHistory() override = default; 575a988416SJim Ingham 58b9c1b51eSKate Stone Options *GetOptions() override { return &m_options; } 595a988416SJim Ingham 605a988416SJim Ingham protected: 61b9c1b51eSKate Stone class CommandOptions : public Options { 62a5a97ebeSJim Ingham public: 63b9c1b51eSKate Stone CommandOptions() 64b9c1b51eSKate Stone : Options(), m_start_idx(0), m_stop_idx(0), m_count(0), m_clear(false) { 65a5a97ebeSJim Ingham } 66a5a97ebeSJim Ingham 676e3d8e7fSEugene Zelenko ~CommandOptions() override = default; 68a5a97ebeSJim Ingham 69b9c1b51eSKate Stone Error SetOptionValue(uint32_t option_idx, const char *option_arg, 70b9c1b51eSKate Stone ExecutionContext *execution_context) override { 71a5a97ebeSJim Ingham Error error; 723bcdfc0eSGreg Clayton const int short_option = m_getopt_table[option_idx].val; 738cef4b0bSZachary Turner llvm::StringRef option_strref = 748cef4b0bSZachary Turner llvm::StringRef::withNullAsEmpty(option_arg); 75a5a97ebeSJim Ingham 76b9c1b51eSKate Stone switch (short_option) { 77a5a97ebeSJim Ingham case 'c': 788cef4b0bSZachary Turner error = 798cef4b0bSZachary Turner m_count.SetValueFromString(option_strref, eVarSetOperationAssign); 80a5a97ebeSJim Ingham break; 81a5a97ebeSJim Ingham case 's': 82b9c1b51eSKate Stone if (option_arg && strcmp("end", option_arg) == 0) { 837594f14fSEnrico Granata m_start_idx.SetCurrentValue(UINT64_MAX); 847594f14fSEnrico Granata m_start_idx.SetOptionWasSet(); 85b9c1b51eSKate Stone } else 868cef4b0bSZachary Turner error = m_start_idx.SetValueFromString(option_strref, 87b9c1b51eSKate Stone eVarSetOperationAssign); 887594f14fSEnrico Granata break; 897594f14fSEnrico Granata case 'e': 908cef4b0bSZachary Turner error = m_stop_idx.SetValueFromString(option_strref, 918cef4b0bSZachary Turner eVarSetOperationAssign); 927594f14fSEnrico Granata break; 9363123b64SEnrico Granata case 'C': 9463123b64SEnrico Granata m_clear.SetCurrentValue(true); 9563123b64SEnrico Granata m_clear.SetOptionWasSet(); 96a5a97ebeSJim Ingham break; 97a5a97ebeSJim Ingham default: 98b9c1b51eSKate Stone error.SetErrorStringWithFormat("unrecognized option '%c'", 99b9c1b51eSKate Stone short_option); 100a5a97ebeSJim Ingham break; 101a5a97ebeSJim Ingham } 102a5a97ebeSJim Ingham 103a5a97ebeSJim Ingham return error; 104a5a97ebeSJim Ingham } 105a5a97ebeSJim Ingham 106b9c1b51eSKate Stone void OptionParsingStarting(ExecutionContext *execution_context) override { 1077594f14fSEnrico Granata m_start_idx.Clear(); 1087594f14fSEnrico Granata m_stop_idx.Clear(); 1097594f14fSEnrico Granata m_count.Clear(); 11063123b64SEnrico Granata m_clear.Clear(); 111a5a97ebeSJim Ingham } 112a5a97ebeSJim Ingham 1131f0f5b5bSZachary Turner llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 11470602439SZachary Turner return llvm::makeArrayRef(g_history_options); 1151f0f5b5bSZachary Turner } 116a5a97ebeSJim Ingham 117a5a97ebeSJim Ingham // Instance variables to hold the values for command options. 118a5a97ebeSJim Ingham 1197594f14fSEnrico Granata OptionValueUInt64 m_start_idx; 1207594f14fSEnrico Granata OptionValueUInt64 m_stop_idx; 1217594f14fSEnrico Granata OptionValueUInt64 m_count; 12263123b64SEnrico Granata OptionValueBoolean m_clear; 123a5a97ebeSJim Ingham }; 124a5a97ebeSJim Ingham 125b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override { 126b9c1b51eSKate Stone if (m_options.m_clear.GetCurrentValue() && 127b9c1b51eSKate Stone m_options.m_clear.OptionWasSet()) { 1287594f14fSEnrico Granata m_interpreter.GetCommandHistory().Clear(); 1297594f14fSEnrico Granata result.SetStatus(lldb::eReturnStatusSuccessFinishNoResult); 130b9c1b51eSKate Stone } else { 131b9c1b51eSKate Stone if (m_options.m_start_idx.OptionWasSet() && 132b9c1b51eSKate Stone m_options.m_stop_idx.OptionWasSet() && 133b9c1b51eSKate Stone m_options.m_count.OptionWasSet()) { 134b9c1b51eSKate Stone result.AppendError("--count, --start-index and --end-index cannot be " 135b9c1b51eSKate Stone "all specified in the same invocation"); 1367594f14fSEnrico Granata result.SetStatus(lldb::eReturnStatusFailed); 137b9c1b51eSKate Stone } else { 138b9c1b51eSKate Stone std::pair<bool, uint64_t> start_idx( 139b9c1b51eSKate Stone m_options.m_start_idx.OptionWasSet(), 140b9c1b51eSKate Stone m_options.m_start_idx.GetCurrentValue()); 141b9c1b51eSKate Stone std::pair<bool, uint64_t> stop_idx( 142b9c1b51eSKate Stone m_options.m_stop_idx.OptionWasSet(), 143b9c1b51eSKate Stone m_options.m_stop_idx.GetCurrentValue()); 144b9c1b51eSKate Stone std::pair<bool, uint64_t> count(m_options.m_count.OptionWasSet(), 145b9c1b51eSKate Stone m_options.m_count.GetCurrentValue()); 146a5a97ebeSJim Ingham 1477594f14fSEnrico Granata const CommandHistory &history(m_interpreter.GetCommandHistory()); 1487594f14fSEnrico Granata 149b9c1b51eSKate Stone if (start_idx.first && start_idx.second == UINT64_MAX) { 150b9c1b51eSKate Stone if (count.first) { 1517594f14fSEnrico Granata start_idx.second = history.GetSize() - count.second; 1527594f14fSEnrico Granata stop_idx.second = history.GetSize() - 1; 153b9c1b51eSKate Stone } else if (stop_idx.first) { 1547594f14fSEnrico Granata start_idx.second = stop_idx.second; 1557594f14fSEnrico Granata stop_idx.second = history.GetSize() - 1; 156b9c1b51eSKate Stone } else { 1577594f14fSEnrico Granata start_idx.second = 0; 1587594f14fSEnrico Granata stop_idx.second = history.GetSize() - 1; 1597594f14fSEnrico Granata } 160b9c1b51eSKate Stone } else { 161b9c1b51eSKate Stone if (!start_idx.first && !stop_idx.first && !count.first) { 1627594f14fSEnrico Granata start_idx.second = 0; 1637594f14fSEnrico Granata stop_idx.second = history.GetSize() - 1; 164b9c1b51eSKate Stone } else if (start_idx.first) { 165b9c1b51eSKate Stone if (count.first) { 1667594f14fSEnrico Granata stop_idx.second = start_idx.second + count.second - 1; 167b9c1b51eSKate Stone } else if (!stop_idx.first) { 1687594f14fSEnrico Granata stop_idx.second = history.GetSize() - 1; 1697594f14fSEnrico Granata } 170b9c1b51eSKate Stone } else if (stop_idx.first) { 171b9c1b51eSKate Stone if (count.first) { 1727594f14fSEnrico Granata if (stop_idx.second >= count.second) 1737594f14fSEnrico Granata start_idx.second = stop_idx.second - count.second + 1; 1747594f14fSEnrico Granata else 1757594f14fSEnrico Granata start_idx.second = 0; 1767594f14fSEnrico Granata } 177b9c1b51eSKate Stone } else /* if (count.first) */ 1787594f14fSEnrico Granata { 1797594f14fSEnrico Granata start_idx.second = 0; 1807594f14fSEnrico Granata stop_idx.second = count.second - 1; 1817594f14fSEnrico Granata } 1827594f14fSEnrico Granata } 183b9c1b51eSKate Stone history.Dump(result.GetOutputStream(), start_idx.second, 184b9c1b51eSKate Stone stop_idx.second); 1857594f14fSEnrico Granata } 1867594f14fSEnrico Granata } 187a5a97ebeSJim Ingham return result.Succeeded(); 188a5a97ebeSJim Ingham } 1895a988416SJim Ingham 1905a988416SJim Ingham CommandOptions m_options; 191a5a97ebeSJim Ingham }; 192a5a97ebeSJim Ingham 193a5a97ebeSJim Ingham //------------------------------------------------------------------------- 194a5a97ebeSJim Ingham // CommandObjectCommandsSource 195a5a97ebeSJim Ingham //------------------------------------------------------------------------- 196a5a97ebeSJim Ingham 1971f0f5b5bSZachary Turner static OptionDefinition g_source_options[] = { 1981f0f5b5bSZachary Turner // clang-format off 1991f0f5b5bSZachary Turner { LLDB_OPT_SET_ALL, false, "stop-on-error", 'e', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "If true, stop executing commands on error." }, 2001f0f5b5bSZachary Turner { LLDB_OPT_SET_ALL, false, "stop-on-continue", 'c', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "If true, stop executing commands on continue." }, 2011f0f5b5bSZachary Turner { LLDB_OPT_SET_ALL, false, "silent-run", 's', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "If true don't echo commands while executing." }, 2021f0f5b5bSZachary Turner // clang-format on 2031f0f5b5bSZachary Turner }; 2041f0f5b5bSZachary Turner 205b9c1b51eSKate Stone class CommandObjectCommandsSource : public CommandObjectParsed { 2065a988416SJim Ingham public: 2077428a18cSKate Stone CommandObjectCommandsSource(CommandInterpreter &interpreter) 208b9c1b51eSKate Stone : CommandObjectParsed( 209b9c1b51eSKate Stone interpreter, "command source", 210b9c1b51eSKate Stone "Read and execute LLDB commands from the file <filename>.", 2116e3d8e7fSEugene Zelenko nullptr), 212b9c1b51eSKate Stone m_options() { 2135a988416SJim Ingham CommandArgumentEntry arg; 2145a988416SJim Ingham CommandArgumentData file_arg; 2155a988416SJim Ingham 2165a988416SJim Ingham // Define the first (and only) variant of this arg. 2175a988416SJim Ingham file_arg.arg_type = eArgTypeFilename; 2185a988416SJim Ingham file_arg.arg_repetition = eArgRepeatPlain; 2195a988416SJim Ingham 220b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the 221b9c1b51eSKate Stone // argument entry. 2225a988416SJim Ingham arg.push_back(file_arg); 2235a988416SJim Ingham 2245a988416SJim Ingham // Push the data for the first argument into the m_arguments vector. 2255a988416SJim Ingham m_arguments.push_back(arg); 2265a988416SJim Ingham } 2275a988416SJim Ingham 2286e3d8e7fSEugene Zelenko ~CommandObjectCommandsSource() override = default; 2295a988416SJim Ingham 230b9c1b51eSKate Stone const char *GetRepeatCommand(Args ¤t_command_args, 231b9c1b51eSKate Stone uint32_t index) override { 2325a988416SJim Ingham return ""; 2335a988416SJim Ingham } 2345a988416SJim Ingham 235b9c1b51eSKate Stone int HandleArgumentCompletion(Args &input, int &cursor_index, 2365a988416SJim Ingham int &cursor_char_position, 2375a988416SJim Ingham OptionElementVector &opt_element_vector, 238b9c1b51eSKate Stone int match_start_point, int max_return_elements, 2395a988416SJim Ingham bool &word_complete, 240b9c1b51eSKate Stone StringList &matches) override { 2415a988416SJim Ingham std::string completion_str(input.GetArgumentAtIndex(cursor_index)); 2425a988416SJim Ingham completion_str.erase(cursor_char_position); 2435a988416SJim Ingham 244b9c1b51eSKate Stone CommandCompletions::InvokeCommonCompletionCallbacks( 245b9c1b51eSKate Stone GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion, 246b9c1b51eSKate Stone completion_str.c_str(), match_start_point, max_return_elements, nullptr, 247b9c1b51eSKate Stone word_complete, matches); 2485a988416SJim Ingham return matches.GetSize(); 2495a988416SJim Ingham } 2505a988416SJim Ingham 251b9c1b51eSKate Stone Options *GetOptions() override { return &m_options; } 2525a988416SJim Ingham 2535a988416SJim Ingham protected: 254b9c1b51eSKate Stone class CommandOptions : public Options { 255e16c50a1SJim Ingham public: 256b9c1b51eSKate Stone CommandOptions() 257b9c1b51eSKate Stone : Options(), m_stop_on_error(true), m_silent_run(false), 258b9c1b51eSKate Stone m_stop_on_continue(true) {} 259e16c50a1SJim Ingham 2606e3d8e7fSEugene Zelenko ~CommandOptions() override = default; 261e16c50a1SJim Ingham 262b9c1b51eSKate Stone Error SetOptionValue(uint32_t option_idx, const char *option_arg, 263b9c1b51eSKate Stone ExecutionContext *execution_context) override { 264e16c50a1SJim Ingham Error error; 2653bcdfc0eSGreg Clayton const int short_option = m_getopt_table[option_idx].val; 2668cef4b0bSZachary Turner llvm::StringRef option_strref = 2678cef4b0bSZachary Turner llvm::StringRef::withNullAsEmpty(option_arg); 268e16c50a1SJim Ingham 269b9c1b51eSKate Stone switch (short_option) { 270e16c50a1SJim Ingham case 'e': 2718cef4b0bSZachary Turner error = m_stop_on_error.SetValueFromString(option_strref); 272e16c50a1SJim Ingham break; 273340b0309SGreg Clayton 274e16c50a1SJim Ingham case 'c': 2758cef4b0bSZachary Turner error = m_stop_on_continue.SetValueFromString(option_strref); 276e16c50a1SJim Ingham break; 277340b0309SGreg Clayton 27860986174SMichael Sartain case 's': 2798cef4b0bSZachary Turner error = m_silent_run.SetValueFromString(option_strref); 28060986174SMichael Sartain break; 281340b0309SGreg Clayton 282e16c50a1SJim Ingham default: 283b9c1b51eSKate Stone error.SetErrorStringWithFormat("unrecognized option '%c'", 284b9c1b51eSKate Stone short_option); 285e16c50a1SJim Ingham break; 286e16c50a1SJim Ingham } 287e16c50a1SJim Ingham 288e16c50a1SJim Ingham return error; 289e16c50a1SJim Ingham } 290e16c50a1SJim Ingham 291b9c1b51eSKate Stone void OptionParsingStarting(ExecutionContext *execution_context) override { 292012d4fcaSEnrico Granata m_stop_on_error.Clear(); 293340b0309SGreg Clayton m_silent_run.Clear(); 294340b0309SGreg Clayton m_stop_on_continue.Clear(); 295e16c50a1SJim Ingham } 296e16c50a1SJim Ingham 2971f0f5b5bSZachary Turner llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 29870602439SZachary Turner return llvm::makeArrayRef(g_source_options); 2991f0f5b5bSZachary Turner } 300e16c50a1SJim Ingham 301e16c50a1SJim Ingham // Instance variables to hold the values for command options. 302e16c50a1SJim Ingham 303012d4fcaSEnrico Granata OptionValueBoolean m_stop_on_error; 304340b0309SGreg Clayton OptionValueBoolean m_silent_run; 305340b0309SGreg Clayton OptionValueBoolean m_stop_on_continue; 306e16c50a1SJim Ingham }; 307e16c50a1SJim Ingham 308b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override { 309c7bece56SGreg Clayton const size_t argc = command.GetArgumentCount(); 310b9c1b51eSKate Stone if (argc == 1) { 3115a988416SJim Ingham const char *filename = command.GetArgumentAtIndex(0); 312ebc09c36SJim Ingham 3131ee3853fSJohnny Chen FileSpec cmd_file(filename, true); 3146e3d8e7fSEugene Zelenko ExecutionContext *exe_ctx = nullptr; // Just use the default context. 315ebc09c36SJim Ingham 316340b0309SGreg Clayton // If any options were set, then use them 317340b0309SGreg Clayton if (m_options.m_stop_on_error.OptionWasSet() || 318340b0309SGreg Clayton m_options.m_silent_run.OptionWasSet() || 319b9c1b51eSKate Stone m_options.m_stop_on_continue.OptionWasSet()) { 320340b0309SGreg Clayton // Use user set settings 32126c7bf93SJim Ingham CommandInterpreterRunOptions options; 322b9c1b51eSKate Stone options.SetStopOnContinue( 323b9c1b51eSKate Stone m_options.m_stop_on_continue.GetCurrentValue()); 32426c7bf93SJim Ingham options.SetStopOnError(m_options.m_stop_on_error.GetCurrentValue()); 3257d8555c4SJim Ingham options.SetEchoCommands(!m_options.m_silent_run.GetCurrentValue()); 3267d8555c4SJim Ingham options.SetPrintResults(!m_options.m_silent_run.GetCurrentValue()); 32726c7bf93SJim Ingham 328b9c1b51eSKate Stone m_interpreter.HandleCommandsFromFile(cmd_file, exe_ctx, options, 329e16c50a1SJim Ingham result); 330b9c1b51eSKate Stone } else { 331b9c1b51eSKate Stone // No options were set, inherit any settings from nested "command 332b9c1b51eSKate Stone // source" commands, 333340b0309SGreg Clayton // or set to sane default settings... 33426c7bf93SJim Ingham CommandInterpreterRunOptions options; 335b9c1b51eSKate Stone m_interpreter.HandleCommandsFromFile(cmd_file, exe_ctx, options, 336340b0309SGreg Clayton result); 337340b0309SGreg Clayton } 338b9c1b51eSKate Stone } else { 339b9c1b51eSKate Stone result.AppendErrorWithFormat( 340b9c1b51eSKate Stone "'%s' takes exactly one executable filename argument.\n", 341b9c1b51eSKate Stone GetCommandName()); 342ebc09c36SJim Ingham result.SetStatus(eReturnStatusFailed); 343ebc09c36SJim Ingham } 344ebc09c36SJim Ingham return result.Succeeded(); 345ebc09c36SJim Ingham } 3466e3d8e7fSEugene Zelenko 3475a988416SJim Ingham CommandOptions m_options; 348ebc09c36SJim Ingham }; 349ebc09c36SJim Ingham 350ebc09c36SJim Ingham #pragma mark CommandObjectCommandsAlias 351ebc09c36SJim Ingham //------------------------------------------------------------------------- 352ebc09c36SJim Ingham // CommandObjectCommandsAlias 353ebc09c36SJim Ingham //------------------------------------------------------------------------- 354ebc09c36SJim Ingham 3551f0f5b5bSZachary Turner static OptionDefinition g_alias_options[] = { 3561f0f5b5bSZachary Turner // clang-format off 3571f0f5b5bSZachary Turner { LLDB_OPT_SET_ALL, false, "help", 'h', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeHelpText, "Help text for this command" }, 3581f0f5b5bSZachary Turner { LLDB_OPT_SET_ALL, false, "long-help", 'H', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeHelpText, "Long help text for this command" }, 3591f0f5b5bSZachary Turner // clang-format on 3601f0f5b5bSZachary Turner }; 3611f0f5b5bSZachary Turner 362b9c1b51eSKate Stone static const char *g_python_command_instructions = 363b9c1b51eSKate Stone "Enter your Python command(s). Type 'DONE' to end.\n" 364be93a35aSEnrico Granata "You must define a Python function with this signature:\n" 36544d93782SGreg Clayton "def my_command_impl(debugger, args, result, internal_dict):\n"; 366be93a35aSEnrico Granata 367b9c1b51eSKate Stone class CommandObjectCommandsAlias : public CommandObjectRaw { 36845d0e238SEnrico Granata protected: 369b9c1b51eSKate Stone class CommandOptions : public OptionGroup { 370ebc09c36SJim Ingham public: 371b9c1b51eSKate Stone CommandOptions() : OptionGroup(), m_help(), m_long_help() {} 37245d0e238SEnrico Granata 37345d0e238SEnrico Granata ~CommandOptions() override = default; 37445d0e238SEnrico Granata 3751f0f5b5bSZachary Turner llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 37670602439SZachary Turner return llvm::makeArrayRef(g_alias_options); 3771f0f5b5bSZachary Turner } 37845d0e238SEnrico Granata 3798cef4b0bSZachary Turner Error SetOptionValue(uint32_t option_idx, llvm::StringRef option_value, 380b9c1b51eSKate Stone ExecutionContext *execution_context) override { 38145d0e238SEnrico Granata Error error; 38245d0e238SEnrico Granata 3831f0f5b5bSZachary Turner const int short_option = GetDefinitions()[option_idx].short_option; 3848cef4b0bSZachary Turner std::string option_str(option_value); 38545d0e238SEnrico Granata 386b9c1b51eSKate Stone switch (short_option) { 38745d0e238SEnrico Granata case 'h': 3888cef4b0bSZachary Turner m_help.SetCurrentValue(option_str); 38945d0e238SEnrico Granata m_help.SetOptionWasSet(); 39045d0e238SEnrico Granata break; 39145d0e238SEnrico Granata 39245d0e238SEnrico Granata case 'H': 3938cef4b0bSZachary Turner m_long_help.SetCurrentValue(option_str); 39445d0e238SEnrico Granata m_long_help.SetOptionWasSet(); 39545d0e238SEnrico Granata break; 39645d0e238SEnrico Granata 39745d0e238SEnrico Granata default: 398b9c1b51eSKate Stone error.SetErrorStringWithFormat("invalid short option character '%c'", 399b9c1b51eSKate Stone short_option); 40045d0e238SEnrico Granata break; 40145d0e238SEnrico Granata } 40245d0e238SEnrico Granata 40345d0e238SEnrico Granata return error; 40445d0e238SEnrico Granata } 4058cef4b0bSZachary Turner Error SetOptionValue(uint32_t, const char *, ExecutionContext *) = delete; 40645d0e238SEnrico Granata 407b9c1b51eSKate Stone void OptionParsingStarting(ExecutionContext *execution_context) override { 40845d0e238SEnrico Granata m_help.Clear(); 40945d0e238SEnrico Granata m_long_help.Clear(); 41045d0e238SEnrico Granata } 41145d0e238SEnrico Granata 41245d0e238SEnrico Granata OptionValueString m_help; 41345d0e238SEnrico Granata OptionValueString m_long_help; 41445d0e238SEnrico Granata }; 41545d0e238SEnrico Granata 41645d0e238SEnrico Granata OptionGroupOptions m_option_group; 41745d0e238SEnrico Granata CommandOptions m_command_options; 41845d0e238SEnrico Granata 41945d0e238SEnrico Granata public: 420b9c1b51eSKate Stone Options *GetOptions() override { return &m_option_group; } 42145d0e238SEnrico Granata 4227428a18cSKate Stone CommandObjectCommandsAlias(CommandInterpreter &interpreter) 423b9c1b51eSKate Stone : CommandObjectRaw( 424b9c1b51eSKate Stone interpreter, "command alias", 425b9c1b51eSKate Stone "Define a custom command in terms of an existing command.", 42645d0e238SEnrico Granata nullptr), 427b9c1b51eSKate Stone m_option_group(), m_command_options() { 42845d0e238SEnrico Granata m_option_group.Append(&m_command_options); 42945d0e238SEnrico Granata m_option_group.Finalize(); 43045d0e238SEnrico Granata 431ebc09c36SJim Ingham SetHelpLong( 432ea671fbdSKate Stone "'alias' allows the user to create a short-cut or abbreviation for long \ 433ea671fbdSKate Stone commands, multi-word commands, and commands that take particular options. \ 434b9c1b51eSKate Stone Below are some simple examples of how one might use the 'alias' command:" 435b9c1b51eSKate Stone R"( 436ea671fbdSKate Stone 437ea671fbdSKate Stone (lldb) command alias sc script 438ea671fbdSKate Stone 439ea671fbdSKate Stone Creates the abbreviation 'sc' for the 'script' command. 440ea671fbdSKate Stone 441ea671fbdSKate Stone (lldb) command alias bp breakpoint 442ea671fbdSKate Stone 443b9c1b51eSKate Stone )" 444b9c1b51eSKate Stone " Creates the abbreviation 'bp' for the 'breakpoint' command. Since \ 445ea671fbdSKate Stone breakpoint commands are two-word commands, the user would still need to \ 446b9c1b51eSKate Stone enter the second word after 'bp', e.g. 'bp enable' or 'bp delete'." 447b9c1b51eSKate Stone R"( 448ea671fbdSKate Stone 449ea671fbdSKate Stone (lldb) command alias bpl breakpoint list 450ea671fbdSKate Stone 451ea671fbdSKate Stone Creates the abbreviation 'bpl' for the two-word command 'breakpoint list'. 452ea671fbdSKate Stone 453b9c1b51eSKate Stone )" 454b9c1b51eSKate Stone "An alias can include some options for the command, with the values either \ 455ea671fbdSKate Stone filled in at the time the alias is created, or specified as positional \ 456ea671fbdSKate Stone arguments, to be filled in when the alias is invoked. The following example \ 457b9c1b51eSKate Stone shows how to create aliases with options:" 458b9c1b51eSKate Stone R"( 459ea671fbdSKate Stone 460ea671fbdSKate Stone (lldb) command alias bfl breakpoint set -f %1 -l %2 461ea671fbdSKate Stone 462b9c1b51eSKate Stone )" 463b9c1b51eSKate Stone " Creates the abbreviation 'bfl' (for break-file-line), with the -f and -l \ 464ea671fbdSKate Stone options already part of the alias. So if the user wants to set a breakpoint \ 465ea671fbdSKate Stone by file and line without explicitly having to use the -f and -l options, the \ 466ea671fbdSKate Stone user can now use 'bfl' instead. The '%1' and '%2' are positional placeholders \ 467ea671fbdSKate Stone for the actual arguments that will be passed when the alias command is used. \ 468ea671fbdSKate Stone The number in the placeholder refers to the position/order the actual value \ 469ea671fbdSKate Stone occupies when the alias is used. All the occurrences of '%1' in the alias \ 470ea671fbdSKate Stone will be replaced with the first argument, all the occurrences of '%2' in the \ 471ea671fbdSKate Stone alias will be replaced with the second argument, and so on. This also allows \ 472ea671fbdSKate Stone actual arguments to be used multiple times within an alias (see 'process \ 473b9c1b51eSKate Stone launch' example below)." 474b9c1b51eSKate Stone R"( 475ea671fbdSKate Stone 476b9c1b51eSKate Stone )" 477b9c1b51eSKate Stone "Note: the positional arguments must substitute as whole words in the resultant \ 478ea671fbdSKate Stone command, so you can't at present do something like this to append the file extension \ 479b9c1b51eSKate Stone \".cpp\":" 480b9c1b51eSKate Stone R"( 481ea671fbdSKate Stone 482ea671fbdSKate Stone (lldb) command alias bcppfl breakpoint set -f %1.cpp -l %2 483ea671fbdSKate Stone 484b9c1b51eSKate Stone )" 485b9c1b51eSKate Stone "For more complex aliasing, use the \"command regex\" command instead. In the \ 486ea671fbdSKate Stone 'bfl' case above, the actual file value will be filled in with the first argument \ 487ea671fbdSKate Stone following 'bfl' and the actual line number value will be filled in with the second \ 488b9c1b51eSKate Stone argument. The user would use this alias as follows:" 489b9c1b51eSKate Stone R"( 490ea671fbdSKate Stone 491ea671fbdSKate Stone (lldb) command alias bfl breakpoint set -f %1 -l %2 492ea671fbdSKate Stone (lldb) bfl my-file.c 137 493ea671fbdSKate Stone 494ea671fbdSKate Stone This would be the same as if the user had entered 'breakpoint set -f my-file.c -l 137'. 495ea671fbdSKate Stone 496ea671fbdSKate Stone Another example: 497ea671fbdSKate Stone 498ea671fbdSKate Stone (lldb) command alias pltty process launch -s -o %1 -e %1 499ea671fbdSKate Stone (lldb) pltty /dev/tty0 500ea671fbdSKate Stone 501ea671fbdSKate Stone Interpreted as 'process launch -s -o /dev/tty0 -e /dev/tty0' 502ea671fbdSKate Stone 503b9c1b51eSKate Stone )" 504b9c1b51eSKate Stone "If the user always wanted to pass the same value to a particular option, the \ 505ea671fbdSKate Stone alias could be defined with that value directly in the alias as a constant, \ 506b9c1b51eSKate Stone rather than using a positional placeholder:" 507b9c1b51eSKate Stone R"( 508ea671fbdSKate Stone 509ea671fbdSKate Stone (lldb) command alias bl3 breakpoint set -f %1 -l 3 510ea671fbdSKate Stone 511b9c1b51eSKate Stone Always sets a breakpoint on line 3 of whatever file is indicated.)"); 512ebc09c36SJim Ingham 513405fe67fSCaroline Tice CommandArgumentEntry arg1; 514405fe67fSCaroline Tice CommandArgumentEntry arg2; 515405fe67fSCaroline Tice CommandArgumentEntry arg3; 516405fe67fSCaroline Tice CommandArgumentData alias_arg; 517405fe67fSCaroline Tice CommandArgumentData cmd_arg; 518405fe67fSCaroline Tice CommandArgumentData options_arg; 519405fe67fSCaroline Tice 520405fe67fSCaroline Tice // Define the first (and only) variant of this arg. 521405fe67fSCaroline Tice alias_arg.arg_type = eArgTypeAliasName; 522405fe67fSCaroline Tice alias_arg.arg_repetition = eArgRepeatPlain; 523405fe67fSCaroline Tice 524b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the 525b9c1b51eSKate Stone // argument entry. 526405fe67fSCaroline Tice arg1.push_back(alias_arg); 527405fe67fSCaroline Tice 528405fe67fSCaroline Tice // Define the first (and only) variant of this arg. 529405fe67fSCaroline Tice cmd_arg.arg_type = eArgTypeCommandName; 530405fe67fSCaroline Tice cmd_arg.arg_repetition = eArgRepeatPlain; 531405fe67fSCaroline Tice 532b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the 533b9c1b51eSKate Stone // argument entry. 534405fe67fSCaroline Tice arg2.push_back(cmd_arg); 535405fe67fSCaroline Tice 536405fe67fSCaroline Tice // Define the first (and only) variant of this arg. 537405fe67fSCaroline Tice options_arg.arg_type = eArgTypeAliasOptions; 538405fe67fSCaroline Tice options_arg.arg_repetition = eArgRepeatOptional; 539405fe67fSCaroline Tice 540b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the 541b9c1b51eSKate Stone // argument entry. 542405fe67fSCaroline Tice arg3.push_back(options_arg); 543405fe67fSCaroline Tice 544405fe67fSCaroline Tice // Push the data for the first argument into the m_arguments vector. 545405fe67fSCaroline Tice m_arguments.push_back(arg1); 546405fe67fSCaroline Tice m_arguments.push_back(arg2); 547405fe67fSCaroline Tice m_arguments.push_back(arg3); 548ebc09c36SJim Ingham } 549ebc09c36SJim Ingham 5506e3d8e7fSEugene Zelenko ~CommandObjectCommandsAlias() override = default; 551ebc09c36SJim Ingham 5525a988416SJim Ingham protected: 553b9c1b51eSKate Stone bool DoExecute(const char *raw_command_line, 554b9c1b51eSKate Stone CommandReturnObject &result) override { 555b9c1b51eSKate Stone if (!raw_command_line || !raw_command_line[0]) { 556d72e412fSEnrico Granata result.AppendError("'command alias' requires at least two arguments"); 55745d0e238SEnrico Granata return false; 55845d0e238SEnrico Granata } 55945d0e238SEnrico Granata 560e1cfbc79STodd Fiala ExecutionContext exe_ctx = GetCommandInterpreter().GetExecutionContext(); 561e1cfbc79STodd Fiala m_option_group.NotifyOptionParsingStarting(&exe_ctx); 56245d0e238SEnrico Granata 56345d0e238SEnrico Granata const char *remainder = nullptr; 56445d0e238SEnrico Granata 565b9c1b51eSKate Stone if (raw_command_line[0] == '-') { 56645d0e238SEnrico Granata // We have some options and these options MUST end with --. 56745d0e238SEnrico Granata const char *end_options = nullptr; 56845d0e238SEnrico Granata const char *s = raw_command_line; 569b9c1b51eSKate Stone while (s && s[0]) { 57045d0e238SEnrico Granata end_options = ::strstr(s, "--"); 571b9c1b51eSKate Stone if (end_options) { 57245d0e238SEnrico Granata end_options += 2; // Get past the "--" 573b9c1b51eSKate Stone if (::isspace(end_options[0])) { 57445d0e238SEnrico Granata remainder = end_options; 57545d0e238SEnrico Granata while (::isspace(*remainder)) 57645d0e238SEnrico Granata ++remainder; 57745d0e238SEnrico Granata break; 57845d0e238SEnrico Granata } 57945d0e238SEnrico Granata } 58045d0e238SEnrico Granata s = end_options; 58145d0e238SEnrico Granata } 58245d0e238SEnrico Granata 583b9c1b51eSKate Stone if (end_options) { 584b9c1b51eSKate Stone Args args( 585b9c1b51eSKate Stone llvm::StringRef(raw_command_line, end_options - raw_command_line)); 58645d0e238SEnrico Granata if (!ParseOptions(args, result)) 58745d0e238SEnrico Granata return false; 58845d0e238SEnrico Granata 589e1cfbc79STodd Fiala Error error(m_option_group.NotifyOptionParsingFinished(&exe_ctx)); 590b9c1b51eSKate Stone if (error.Fail()) { 59145d0e238SEnrico Granata result.AppendError(error.AsCString()); 59245d0e238SEnrico Granata result.SetStatus(eReturnStatusFailed); 59345d0e238SEnrico Granata return false; 59445d0e238SEnrico Granata } 59545d0e238SEnrico Granata } 59645d0e238SEnrico Granata } 59745d0e238SEnrico Granata if (nullptr == remainder) 59845d0e238SEnrico Granata remainder = raw_command_line; 59945d0e238SEnrico Granata 60045d0e238SEnrico Granata std::string raw_command_string(remainder); 60145d0e238SEnrico Granata Args args(raw_command_string.c_str()); 602844d2303SCaroline Tice 603844d2303SCaroline Tice size_t argc = args.GetArgumentCount(); 604844d2303SCaroline Tice 605b9c1b51eSKate Stone if (argc < 2) { 606d72e412fSEnrico Granata result.AppendError("'command alias' requires at least two arguments"); 607844d2303SCaroline Tice result.SetStatus(eReturnStatusFailed); 608844d2303SCaroline Tice return false; 609844d2303SCaroline Tice } 610844d2303SCaroline Tice 611844d2303SCaroline Tice // Get the alias command. 612844d2303SCaroline Tice 613844d2303SCaroline Tice const std::string alias_command = args.GetArgumentAtIndex(0); 614b9c1b51eSKate Stone if (alias_command.size() > 1 && alias_command[0] == '-') { 615d72e412fSEnrico Granata result.AppendError("aliases starting with a dash are not supported"); 616b9c1b51eSKate Stone if (alias_command == "--help" || alias_command == "--long-help") { 617b9c1b51eSKate Stone result.AppendWarning("if trying to pass options to 'command alias' add " 618b9c1b51eSKate Stone "a -- at the end of the options"); 619d72e412fSEnrico Granata } 620d72e412fSEnrico Granata result.SetStatus(eReturnStatusFailed); 621d72e412fSEnrico Granata return false; 622d72e412fSEnrico Granata } 623844d2303SCaroline Tice 624b9c1b51eSKate Stone // Strip the new alias name off 'raw_command_string' (leave it on args, 625b9c1b51eSKate Stone // which gets passed to 'Execute', which 626844d2303SCaroline Tice // does the stripping itself. 627844d2303SCaroline Tice size_t pos = raw_command_string.find(alias_command); 628b9c1b51eSKate Stone if (pos == 0) { 629844d2303SCaroline Tice raw_command_string = raw_command_string.substr(alias_command.size()); 630844d2303SCaroline Tice pos = raw_command_string.find_first_not_of(' '); 631844d2303SCaroline Tice if ((pos != std::string::npos) && (pos > 0)) 632844d2303SCaroline Tice raw_command_string = raw_command_string.substr(pos); 633b9c1b51eSKate Stone } else { 634844d2303SCaroline Tice result.AppendError("Error parsing command string. No alias created."); 635844d2303SCaroline Tice result.SetStatus(eReturnStatusFailed); 636844d2303SCaroline Tice return false; 637844d2303SCaroline Tice } 638844d2303SCaroline Tice 639844d2303SCaroline Tice // Verify that the command is alias-able. 640b9c1b51eSKate Stone if (m_interpreter.CommandExists(alias_command.c_str())) { 641b9c1b51eSKate Stone result.AppendErrorWithFormat( 642b9c1b51eSKate Stone "'%s' is a permanent debugger command and cannot be redefined.\n", 643844d2303SCaroline Tice alias_command.c_str()); 644844d2303SCaroline Tice result.SetStatus(eReturnStatusFailed); 645844d2303SCaroline Tice return false; 646844d2303SCaroline Tice } 647844d2303SCaroline Tice 648b9c1b51eSKate Stone // Get CommandObject that is being aliased. The command name is read from 649b9c1b51eSKate Stone // the front of raw_command_string. 650b9c1b51eSKate Stone // raw_command_string is returned with the name of the command object 651b9c1b51eSKate Stone // stripped off the front. 652d72e412fSEnrico Granata std::string original_raw_command_string(raw_command_string); 653b9c1b51eSKate Stone CommandObject *cmd_obj = 654b9c1b51eSKate Stone m_interpreter.GetCommandObjectForCommand(raw_command_string); 655844d2303SCaroline Tice 656b9c1b51eSKate Stone if (!cmd_obj) { 657b9c1b51eSKate Stone result.AppendErrorWithFormat("invalid command given to 'command alias'. " 658b9c1b51eSKate Stone "'%s' does not begin with a valid command." 659b9c1b51eSKate Stone " No alias created.", 660b9c1b51eSKate Stone original_raw_command_string.c_str()); 661844d2303SCaroline Tice result.SetStatus(eReturnStatusFailed); 662844d2303SCaroline Tice return false; 663b9c1b51eSKate Stone } else if (!cmd_obj->WantsRawCommandString()) { 664b9c1b51eSKate Stone // Note that args was initialized with the original command, and has not 665b9c1b51eSKate Stone // been updated to this point. 666b9c1b51eSKate Stone // Therefore can we pass it to the version of Execute that does not 667b9c1b51eSKate Stone // need/expect raw input in the alias. 6685a988416SJim Ingham return HandleAliasingNormalCommand(args, result); 669b9c1b51eSKate Stone } else { 670b9c1b51eSKate Stone return HandleAliasingRawCommand(alias_command, raw_command_string, 671b9c1b51eSKate Stone *cmd_obj, result); 6725a988416SJim Ingham } 6735a988416SJim Ingham return result.Succeeded(); 6745a988416SJim Ingham } 6755a988416SJim Ingham 676b9c1b51eSKate Stone bool HandleAliasingRawCommand(const std::string &alias_command, 677b9c1b51eSKate Stone std::string &raw_command_string, 678b9c1b51eSKate Stone CommandObject &cmd_obj, 679b9c1b51eSKate Stone CommandReturnObject &result) { 680844d2303SCaroline Tice // Verify & handle any options/arguments passed to the alias command 681844d2303SCaroline Tice 682b9c1b51eSKate Stone OptionArgVectorSP option_arg_vector_sp = 683b9c1b51eSKate Stone OptionArgVectorSP(new OptionArgVector); 684844d2303SCaroline Tice 685b9c1b51eSKate Stone if (CommandObjectSP cmd_obj_sp = 686b9c1b51eSKate Stone m_interpreter.GetCommandSPExact(cmd_obj.GetCommandName(), false)) { 687b9c1b51eSKate Stone if (m_interpreter.AliasExists(alias_command.c_str()) || 688b9c1b51eSKate Stone m_interpreter.UserCommandExists(alias_command.c_str())) { 689b9c1b51eSKate Stone result.AppendWarningWithFormat( 690b9c1b51eSKate Stone "Overwriting existing definition for '%s'.\n", 691844d2303SCaroline Tice alias_command.c_str()); 692844d2303SCaroline Tice } 693b9c1b51eSKate Stone if (CommandAlias *alias = m_interpreter.AddAlias( 694b9c1b51eSKate Stone alias_command.c_str(), cmd_obj_sp, raw_command_string.c_str())) { 69545d0e238SEnrico Granata if (m_command_options.m_help.OptionWasSet()) 69645d0e238SEnrico Granata alias->SetHelp(m_command_options.m_help.GetCurrentValue()); 69745d0e238SEnrico Granata if (m_command_options.m_long_help.OptionWasSet()) 69845d0e238SEnrico Granata alias->SetHelpLong(m_command_options.m_long_help.GetCurrentValue()); 699844d2303SCaroline Tice result.SetStatus(eReturnStatusSuccessFinishNoResult); 700b9c1b51eSKate Stone } else { 701472362e6SCaroline Tice result.AppendError("Unable to create requested alias.\n"); 702472362e6SCaroline Tice result.SetStatus(eReturnStatusFailed); 703472362e6SCaroline Tice } 704212130acSEnrico Granata 705b9c1b51eSKate Stone } else { 706212130acSEnrico Granata result.AppendError("Unable to create requested alias.\n"); 707212130acSEnrico Granata result.SetStatus(eReturnStatusFailed); 708212130acSEnrico Granata } 709212130acSEnrico Granata 710844d2303SCaroline Tice return result.Succeeded(); 711844d2303SCaroline Tice } 712ebc09c36SJim Ingham 713b9c1b51eSKate Stone bool HandleAliasingNormalCommand(Args &args, CommandReturnObject &result) { 714867b185dSCaroline Tice size_t argc = args.GetArgumentCount(); 715ebc09c36SJim Ingham 716b9c1b51eSKate Stone if (argc < 2) { 717d72e412fSEnrico Granata result.AppendError("'command alias' requires at least two arguments"); 718ebc09c36SJim Ingham result.SetStatus(eReturnStatusFailed); 719ebc09c36SJim Ingham return false; 720ebc09c36SJim Ingham } 721ebc09c36SJim Ingham 722ebc09c36SJim Ingham const std::string alias_command = args.GetArgumentAtIndex(0); 723ebc09c36SJim Ingham const std::string actual_command = args.GetArgumentAtIndex(1); 724ebc09c36SJim Ingham 725ebc09c36SJim Ingham args.Shift(); // Shift the alias command word off the argument vector. 726ebc09c36SJim Ingham args.Shift(); // Shift the old command word off the argument vector. 727ebc09c36SJim Ingham 728b9c1b51eSKate Stone // Verify that the command is alias'able, and get the appropriate command 729b9c1b51eSKate Stone // object. 730ebc09c36SJim Ingham 731b9c1b51eSKate Stone if (m_interpreter.CommandExists(alias_command.c_str())) { 732b9c1b51eSKate Stone result.AppendErrorWithFormat( 733b9c1b51eSKate Stone "'%s' is a permanent debugger command and cannot be redefined.\n", 734ebc09c36SJim Ingham alias_command.c_str()); 735ebc09c36SJim Ingham result.SetStatus(eReturnStatusFailed); 736b9c1b51eSKate Stone } else { 737b9c1b51eSKate Stone CommandObjectSP command_obj_sp( 738b9c1b51eSKate Stone m_interpreter.GetCommandSPExact(actual_command.c_str(), true)); 739ebc09c36SJim Ingham CommandObjectSP subcommand_obj_sp; 740ebc09c36SJim Ingham bool use_subcommand = false; 741b9c1b51eSKate Stone if (command_obj_sp) { 742ebc09c36SJim Ingham CommandObject *cmd_obj = command_obj_sp.get(); 7436e3d8e7fSEugene Zelenko CommandObject *sub_cmd_obj = nullptr; 744b9c1b51eSKate Stone OptionArgVectorSP option_arg_vector_sp = 745b9c1b51eSKate Stone OptionArgVectorSP(new OptionArgVector); 746ebc09c36SJim Ingham 747b9c1b51eSKate Stone while (cmd_obj->IsMultiwordObject() && args.GetArgumentCount() > 0) { 748b9c1b51eSKate Stone if (argc >= 3) { 749ebc09c36SJim Ingham const std::string sub_command = args.GetArgumentAtIndex(0); 750ebc09c36SJim Ingham assert(sub_command.length() != 0); 751998255bfSGreg Clayton subcommand_obj_sp = cmd_obj->GetSubcommandSP(sub_command.c_str()); 752b9c1b51eSKate Stone if (subcommand_obj_sp) { 753ebc09c36SJim Ingham sub_cmd_obj = subcommand_obj_sp.get(); 754ebc09c36SJim Ingham use_subcommand = true; 755b9c1b51eSKate Stone args.Shift(); // Shift the sub_command word off the argument 756b9c1b51eSKate Stone // vector. 757844d2303SCaroline Tice cmd_obj = sub_cmd_obj; 758b9c1b51eSKate Stone } else { 759b9c1b51eSKate Stone result.AppendErrorWithFormat( 760b9c1b51eSKate Stone "'%s' is not a valid sub-command of '%s'. " 761f415eeb4SCaroline Tice "Unable to create alias.\n", 762f415eeb4SCaroline Tice sub_command.c_str(), actual_command.c_str()); 763ebc09c36SJim Ingham result.SetStatus(eReturnStatusFailed); 764ebc09c36SJim Ingham return false; 765ebc09c36SJim Ingham } 766ebc09c36SJim Ingham } 767ebc09c36SJim Ingham } 768ebc09c36SJim Ingham 769ebc09c36SJim Ingham // Verify & handle any options/arguments passed to the alias command 770ebc09c36SJim Ingham 771212130acSEnrico Granata std::string args_string; 772212130acSEnrico Granata 773b9c1b51eSKate Stone if (args.GetArgumentCount() > 0) { 774b9c1b51eSKate Stone CommandObjectSP tmp_sp = 775b9c1b51eSKate Stone m_interpreter.GetCommandSPExact(cmd_obj->GetCommandName(), false); 776ebc09c36SJim Ingham if (use_subcommand) 777b9c1b51eSKate Stone tmp_sp = m_interpreter.GetCommandSPExact( 778b9c1b51eSKate Stone sub_cmd_obj->GetCommandName(), false); 779ca90c47eSCaroline Tice 780ca90c47eSCaroline Tice args.GetCommandString(args_string); 781867b185dSCaroline Tice } 782ebc09c36SJim Ingham 783b9c1b51eSKate Stone if (m_interpreter.AliasExists(alias_command.c_str()) || 784b9c1b51eSKate Stone m_interpreter.UserCommandExists(alias_command.c_str())) { 785b9c1b51eSKate Stone result.AppendWarningWithFormat( 786b9c1b51eSKate Stone "Overwriting existing definition for '%s'.\n", 787ebc09c36SJim Ingham alias_command.c_str()); 788ebc09c36SJim Ingham } 789ebc09c36SJim Ingham 790b9c1b51eSKate Stone if (CommandAlias *alias = m_interpreter.AddAlias( 791b9c1b51eSKate Stone alias_command.c_str(), 792212130acSEnrico Granata use_subcommand ? subcommand_obj_sp : command_obj_sp, 793b9c1b51eSKate Stone args_string.c_str())) { 79445d0e238SEnrico Granata if (m_command_options.m_help.OptionWasSet()) 79545d0e238SEnrico Granata alias->SetHelp(m_command_options.m_help.GetCurrentValue()); 79645d0e238SEnrico Granata if (m_command_options.m_long_help.OptionWasSet()) 79745d0e238SEnrico Granata alias->SetHelpLong(m_command_options.m_long_help.GetCurrentValue()); 798ebc09c36SJim Ingham result.SetStatus(eReturnStatusSuccessFinishNoResult); 799b9c1b51eSKate Stone } else { 800212130acSEnrico Granata result.AppendError("Unable to create requested alias.\n"); 801212130acSEnrico Granata result.SetStatus(eReturnStatusFailed); 802212130acSEnrico Granata return false; 803212130acSEnrico Granata } 804b9c1b51eSKate Stone } else { 805b9c1b51eSKate Stone result.AppendErrorWithFormat("'%s' is not an existing command.\n", 806b9c1b51eSKate Stone actual_command.c_str()); 807ebc09c36SJim Ingham result.SetStatus(eReturnStatusFailed); 808e7941795SCaroline Tice return false; 809ebc09c36SJim Ingham } 810ebc09c36SJim Ingham } 811ebc09c36SJim Ingham 812ebc09c36SJim Ingham return result.Succeeded(); 813ebc09c36SJim Ingham } 814ebc09c36SJim Ingham }; 815ebc09c36SJim Ingham 816ebc09c36SJim Ingham #pragma mark CommandObjectCommandsUnalias 817ebc09c36SJim Ingham //------------------------------------------------------------------------- 818ebc09c36SJim Ingham // CommandObjectCommandsUnalias 819ebc09c36SJim Ingham //------------------------------------------------------------------------- 820ebc09c36SJim Ingham 821b9c1b51eSKate Stone class CommandObjectCommandsUnalias : public CommandObjectParsed { 822ebc09c36SJim Ingham public: 8237428a18cSKate Stone CommandObjectCommandsUnalias(CommandInterpreter &interpreter) 824b9c1b51eSKate Stone : CommandObjectParsed( 825b9c1b51eSKate Stone interpreter, "command unalias", 826b9c1b51eSKate Stone "Delete one or more custom commands defined by 'command alias'.", 827b9c1b51eSKate Stone nullptr) { 828405fe67fSCaroline Tice CommandArgumentEntry arg; 829405fe67fSCaroline Tice CommandArgumentData alias_arg; 830405fe67fSCaroline Tice 831405fe67fSCaroline Tice // Define the first (and only) variant of this arg. 832405fe67fSCaroline Tice alias_arg.arg_type = eArgTypeAliasName; 833405fe67fSCaroline Tice alias_arg.arg_repetition = eArgRepeatPlain; 834405fe67fSCaroline Tice 835b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the 836b9c1b51eSKate Stone // argument entry. 837405fe67fSCaroline Tice arg.push_back(alias_arg); 838405fe67fSCaroline Tice 839405fe67fSCaroline Tice // Push the data for the first argument into the m_arguments vector. 840405fe67fSCaroline Tice m_arguments.push_back(arg); 841ebc09c36SJim Ingham } 842ebc09c36SJim Ingham 8436e3d8e7fSEugene Zelenko ~CommandObjectCommandsUnalias() override = default; 844ebc09c36SJim Ingham 8455a988416SJim Ingham protected: 846b9c1b51eSKate Stone bool DoExecute(Args &args, CommandReturnObject &result) override { 847ebc09c36SJim Ingham CommandObject::CommandMap::iterator pos; 848ebc09c36SJim Ingham CommandObject *cmd_obj; 849ebc09c36SJim Ingham 850b9c1b51eSKate Stone if (args.GetArgumentCount() != 0) { 851ebc09c36SJim Ingham const char *command_name = args.GetArgumentAtIndex(0); 852a7015092SGreg Clayton cmd_obj = m_interpreter.GetCommandObject(command_name); 853b9c1b51eSKate Stone if (cmd_obj) { 854b9c1b51eSKate Stone if (m_interpreter.CommandExists(command_name)) { 855b9c1b51eSKate Stone if (cmd_obj->IsRemovable()) { 856b9c1b51eSKate Stone result.AppendErrorWithFormat( 857b9c1b51eSKate Stone "'%s' is not an alias, it is a debugger command which can be " 858b9c1b51eSKate Stone "removed using the 'command delete' command.\n", 859b547278cSGreg Clayton command_name); 860b9c1b51eSKate Stone } else { 861b9c1b51eSKate Stone result.AppendErrorWithFormat( 862b9c1b51eSKate Stone "'%s' is a permanent debugger command and cannot be removed.\n", 863ebc09c36SJim Ingham command_name); 864b547278cSGreg Clayton } 865ebc09c36SJim Ingham result.SetStatus(eReturnStatusFailed); 866b9c1b51eSKate Stone } else { 867b9c1b51eSKate Stone if (!m_interpreter.RemoveAlias(command_name)) { 868a7015092SGreg Clayton if (m_interpreter.AliasExists(command_name)) 869b9c1b51eSKate Stone result.AppendErrorWithFormat( 870b9c1b51eSKate Stone "Error occurred while attempting to unalias '%s'.\n", 871ebc09c36SJim Ingham command_name); 872ebc09c36SJim Ingham else 873b9c1b51eSKate Stone result.AppendErrorWithFormat("'%s' is not an existing alias.\n", 874b9c1b51eSKate Stone command_name); 875ebc09c36SJim Ingham result.SetStatus(eReturnStatusFailed); 876b9c1b51eSKate Stone } else 877ebc09c36SJim Ingham result.SetStatus(eReturnStatusSuccessFinishNoResult); 878ebc09c36SJim Ingham } 879b9c1b51eSKate Stone } else { 880b9c1b51eSKate Stone result.AppendErrorWithFormat( 881b9c1b51eSKate Stone "'%s' is not a known command.\nTry 'help' to see a " 882ebc09c36SJim Ingham "current list of commands.\n", 883ebc09c36SJim Ingham command_name); 884ebc09c36SJim Ingham result.SetStatus(eReturnStatusFailed); 885ebc09c36SJim Ingham } 886b9c1b51eSKate Stone } else { 887ebc09c36SJim Ingham result.AppendError("must call 'unalias' with a valid alias"); 888ebc09c36SJim Ingham result.SetStatus(eReturnStatusFailed); 889ebc09c36SJim Ingham } 890ebc09c36SJim Ingham 891ebc09c36SJim Ingham return result.Succeeded(); 892ebc09c36SJim Ingham } 893ebc09c36SJim Ingham }; 894ebc09c36SJim Ingham 895b547278cSGreg Clayton #pragma mark CommandObjectCommandsDelete 896b547278cSGreg Clayton //------------------------------------------------------------------------- 897b547278cSGreg Clayton // CommandObjectCommandsDelete 898b547278cSGreg Clayton //------------------------------------------------------------------------- 899b547278cSGreg Clayton 900b9c1b51eSKate Stone class CommandObjectCommandsDelete : public CommandObjectParsed { 901b547278cSGreg Clayton public: 9027428a18cSKate Stone CommandObjectCommandsDelete(CommandInterpreter &interpreter) 903b9c1b51eSKate Stone : CommandObjectParsed( 904b9c1b51eSKate Stone interpreter, "command delete", 905b9c1b51eSKate Stone "Delete one or more custom commands defined by 'command regex'.", 906b9c1b51eSKate Stone nullptr) { 907b547278cSGreg Clayton CommandArgumentEntry arg; 908b547278cSGreg Clayton CommandArgumentData alias_arg; 909b547278cSGreg Clayton 910b547278cSGreg Clayton // Define the first (and only) variant of this arg. 911b547278cSGreg Clayton alias_arg.arg_type = eArgTypeCommandName; 912b547278cSGreg Clayton alias_arg.arg_repetition = eArgRepeatPlain; 913b547278cSGreg Clayton 914b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the 915b9c1b51eSKate Stone // argument entry. 916b547278cSGreg Clayton arg.push_back(alias_arg); 917b547278cSGreg Clayton 918b547278cSGreg Clayton // Push the data for the first argument into the m_arguments vector. 919b547278cSGreg Clayton m_arguments.push_back(arg); 920b547278cSGreg Clayton } 921b547278cSGreg Clayton 9226e3d8e7fSEugene Zelenko ~CommandObjectCommandsDelete() override = default; 923b547278cSGreg Clayton 924b547278cSGreg Clayton protected: 925b9c1b51eSKate Stone bool DoExecute(Args &args, CommandReturnObject &result) override { 926b547278cSGreg Clayton CommandObject::CommandMap::iterator pos; 927b547278cSGreg Clayton 928b9c1b51eSKate Stone if (args.GetArgumentCount() != 0) { 929b547278cSGreg Clayton const char *command_name = args.GetArgumentAtIndex(0); 930b9c1b51eSKate Stone if (m_interpreter.CommandExists(command_name)) { 931b9c1b51eSKate Stone if (m_interpreter.RemoveCommand(command_name)) { 932b547278cSGreg Clayton result.SetStatus(eReturnStatusSuccessFinishNoResult); 933b9c1b51eSKate Stone } else { 934b9c1b51eSKate Stone result.AppendErrorWithFormat( 935b9c1b51eSKate Stone "'%s' is a permanent debugger command and cannot be removed.\n", 936b547278cSGreg Clayton command_name); 937b547278cSGreg Clayton result.SetStatus(eReturnStatusFailed); 938b547278cSGreg Clayton } 939b9c1b51eSKate Stone } else { 94046d4aa21SEnrico Granata StreamString error_msg_stream; 94146d4aa21SEnrico Granata const bool generate_apropos = true; 94246d4aa21SEnrico Granata const bool generate_type_lookup = false; 943b9c1b51eSKate Stone CommandObjectHelp::GenerateAdditionalHelpAvenuesMessage( 944b9c1b51eSKate Stone &error_msg_stream, command_name, nullptr, nullptr, generate_apropos, 94546d4aa21SEnrico Granata generate_type_lookup); 94646d4aa21SEnrico Granata result.AppendErrorWithFormat("%s", error_msg_stream.GetData()); 947b547278cSGreg Clayton result.SetStatus(eReturnStatusFailed); 948b547278cSGreg Clayton } 949b9c1b51eSKate Stone } else { 950b9c1b51eSKate Stone result.AppendErrorWithFormat("must call '%s' with one or more valid user " 951b9c1b51eSKate Stone "defined regular expression command names", 9527428a18cSKate Stone GetCommandName()); 953b547278cSGreg Clayton result.SetStatus(eReturnStatusFailed); 954b547278cSGreg Clayton } 955b547278cSGreg Clayton 956b547278cSGreg Clayton return result.Succeeded(); 957b547278cSGreg Clayton } 958b547278cSGreg Clayton }; 959b547278cSGreg Clayton 960de164aaaSGreg Clayton //------------------------------------------------------------------------- 961de164aaaSGreg Clayton // CommandObjectCommandsAddRegex 962de164aaaSGreg Clayton //------------------------------------------------------------------------- 9631f0f5b5bSZachary Turner 9641f0f5b5bSZachary Turner static OptionDefinition g_regex_options[] = { 9651f0f5b5bSZachary Turner // clang-format off 9661f0f5b5bSZachary Turner { LLDB_OPT_SET_1, false, "help" , 'h', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeNone, "The help text to display for this command." }, 9671f0f5b5bSZachary Turner { LLDB_OPT_SET_1, false, "syntax", 's', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeNone, "A syntax string showing the typical usage syntax." }, 9681f0f5b5bSZachary Turner // clang-format on 9691f0f5b5bSZachary Turner }; 9701f0f5b5bSZachary Turner 9715a988416SJim Ingham #pragma mark CommandObjectCommandsAddRegex 972de164aaaSGreg Clayton 973b9c1b51eSKate Stone class CommandObjectCommandsAddRegex : public CommandObjectParsed, 974b9c1b51eSKate Stone public IOHandlerDelegateMultiline { 975de164aaaSGreg Clayton public: 9767428a18cSKate Stone CommandObjectCommandsAddRegex(CommandInterpreter &interpreter) 977b9c1b51eSKate Stone : CommandObjectParsed( 978b9c1b51eSKate Stone interpreter, "command regex", "Define a custom command in terms of " 979b9c1b51eSKate Stone "existing commands by matching " 980b9c1b51eSKate Stone "regular expressions.", 9810e5e5a79SGreg Clayton "command regex <cmd-name> [s/<regex>/<subst>/ ...]"), 982b9c1b51eSKate Stone IOHandlerDelegateMultiline("", 983b9c1b51eSKate Stone IOHandlerDelegate::Completion::LLDBCommand), 984b9c1b51eSKate Stone m_options() { 985b9c1b51eSKate Stone SetHelpLong( 986b9c1b51eSKate Stone R"( 987b9c1b51eSKate Stone )" 988b9c1b51eSKate Stone "This command allows the user to create powerful regular expression commands \ 989ea671fbdSKate Stone with substitutions. The regular expressions and substitutions are specified \ 990b9c1b51eSKate Stone using the regular expression substitution format of:" 991b9c1b51eSKate Stone R"( 992ea671fbdSKate Stone 993ea671fbdSKate Stone s/<regex>/<subst>/ 994ea671fbdSKate Stone 995b9c1b51eSKate Stone )" 996b9c1b51eSKate Stone "<regex> is a regular expression that can use parenthesis to capture regular \ 997ea671fbdSKate Stone expression input and substitute the captured matches in the output using %1 \ 998b9c1b51eSKate Stone for the first match, %2 for the second, and so on." 999b9c1b51eSKate Stone R"( 1000ea671fbdSKate Stone 1001b9c1b51eSKate Stone )" 1002b9c1b51eSKate Stone "The regular expressions can all be specified on the command line if more than \ 1003ea671fbdSKate Stone one argument is provided. If just the command name is provided on the command \ 1004ea671fbdSKate Stone line, then the regular expressions and substitutions can be entered on separate \ 1005b9c1b51eSKate Stone lines, followed by an empty line to terminate the command definition." 1006b9c1b51eSKate Stone R"( 1007ea671fbdSKate Stone 1008ea671fbdSKate Stone EXAMPLES 1009ea671fbdSKate Stone 1010b9c1b51eSKate Stone )" 1011b9c1b51eSKate Stone "The following example will define a regular expression command named 'f' that \ 1012ea671fbdSKate Stone will call 'finish' if there are no arguments, or 'frame select <frame-idx>' if \ 1013b9c1b51eSKate Stone a number follows 'f':" 1014b9c1b51eSKate Stone R"( 1015ea671fbdSKate Stone 1016b9c1b51eSKate Stone (lldb) command regex f s/^$/finish/ 's/([0-9]+)/frame select %1/')"); 1017de164aaaSGreg Clayton } 1018de164aaaSGreg Clayton 10196e3d8e7fSEugene Zelenko ~CommandObjectCommandsAddRegex() override = default; 1020de164aaaSGreg Clayton 10215a988416SJim Ingham protected: 1022b9c1b51eSKate Stone void IOHandlerActivated(IOHandler &io_handler) override { 102344d93782SGreg Clayton StreamFileSP output_sp(io_handler.GetOutputStreamFile()); 1024b9c1b51eSKate Stone if (output_sp) { 1025b9c1b51eSKate Stone output_sp->PutCString("Enter one of more sed substitution commands in " 1026b9c1b51eSKate Stone "the form: 's/<regex>/<subst>/'.\nTerminate the " 1027b9c1b51eSKate Stone "substitution list with an empty line.\n"); 102844d93782SGreg Clayton output_sp->Flush(); 102944d93782SGreg Clayton } 103044d93782SGreg Clayton } 103144d93782SGreg Clayton 1032b9c1b51eSKate Stone void IOHandlerInputComplete(IOHandler &io_handler, 1033b9c1b51eSKate Stone std::string &data) override { 103444d93782SGreg Clayton io_handler.SetIsDone(true); 1035b9c1b51eSKate Stone if (m_regex_cmd_ap) { 103644d93782SGreg Clayton StringList lines; 1037b9c1b51eSKate Stone if (lines.SplitIntoLines(data)) { 103844d93782SGreg Clayton const size_t num_lines = lines.GetSize(); 103944d93782SGreg Clayton bool check_only = false; 1040b9c1b51eSKate Stone for (size_t i = 0; i < num_lines; ++i) { 104144d93782SGreg Clayton llvm::StringRef bytes_strref(lines[i]); 104244d93782SGreg Clayton Error error = AppendRegexSubstitution(bytes_strref, check_only); 1043b9c1b51eSKate Stone if (error.Fail()) { 1044b9c1b51eSKate Stone if (!m_interpreter.GetDebugger() 1045b9c1b51eSKate Stone .GetCommandInterpreter() 1046b9c1b51eSKate Stone .GetBatchCommandMode()) { 1047b9c1b51eSKate Stone StreamSP out_stream = 1048b9c1b51eSKate Stone m_interpreter.GetDebugger().GetAsyncOutputStream(); 104944d93782SGreg Clayton out_stream->Printf("error: %s\n", error.AsCString()); 105044d93782SGreg Clayton } 105144d93782SGreg Clayton } 105244d93782SGreg Clayton } 105344d93782SGreg Clayton } 1054b9c1b51eSKate Stone if (m_regex_cmd_ap->HasRegexEntries()) { 105544d93782SGreg Clayton CommandObjectSP cmd_sp(m_regex_cmd_ap.release()); 105644d93782SGreg Clayton m_interpreter.AddCommand(cmd_sp->GetCommandName(), cmd_sp, true); 105744d93782SGreg Clayton } 105844d93782SGreg Clayton } 105944d93782SGreg Clayton } 106044d93782SGreg Clayton 1061b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override { 10625a988416SJim Ingham const size_t argc = command.GetArgumentCount(); 1063b9c1b51eSKate Stone if (argc == 0) { 1064b9c1b51eSKate Stone result.AppendError("usage: 'command regex <command-name> " 1065b9c1b51eSKate Stone "[s/<regex1>/<subst1>/ s/<regex2>/<subst2>/ ...]'\n"); 10660e5e5a79SGreg Clayton result.SetStatus(eReturnStatusFailed); 1067b9c1b51eSKate Stone } else { 10680e5e5a79SGreg Clayton Error error; 10695a988416SJim Ingham const char *name = command.GetArgumentAtIndex(0); 1070b9c1b51eSKate Stone m_regex_cmd_ap.reset(new CommandObjectRegexCommand( 1071b9c1b51eSKate Stone m_interpreter, name, m_options.GetHelp(), m_options.GetSyntax(), 10, 1072b9c1b51eSKate Stone 0, true)); 10730e5e5a79SGreg Clayton 1074b9c1b51eSKate Stone if (argc == 1) { 107544d93782SGreg Clayton Debugger &debugger = m_interpreter.GetDebugger(); 1076e30f11d9SKate Stone bool color_prompt = debugger.GetUseColor(); 107744d93782SGreg Clayton const bool multiple_lines = true; // Get multiple lines 1078b9c1b51eSKate Stone IOHandlerSP io_handler_sp(new IOHandlerEditline( 1079b9c1b51eSKate Stone debugger, IOHandler::Type::Other, 108073d80faaSGreg Clayton "lldb-regex", // Name of input reader for history 1081*514d8cd8SZachary Turner llvm::StringRef("> "), // Prompt 1082*514d8cd8SZachary Turner llvm::StringRef(), // Continuation prompt 1083b9c1b51eSKate Stone multiple_lines, color_prompt, 1084f6913cd7SGreg Clayton 0, // Don't show line numbers 108544d93782SGreg Clayton *this)); 108644d93782SGreg Clayton 1087b9c1b51eSKate Stone if (io_handler_sp) { 108844d93782SGreg Clayton debugger.PushIOHandler(io_handler_sp); 1089de164aaaSGreg Clayton result.SetStatus(eReturnStatusSuccessFinishNoResult); 1090de164aaaSGreg Clayton } 1091b9c1b51eSKate Stone } else { 1092b9c1b51eSKate Stone for (size_t arg_idx = 1; arg_idx < argc; ++arg_idx) { 10935a988416SJim Ingham llvm::StringRef arg_strref(command.GetArgumentAtIndex(arg_idx)); 109444d93782SGreg Clayton bool check_only = false; 109544d93782SGreg Clayton error = AppendRegexSubstitution(arg_strref, check_only); 10960e5e5a79SGreg Clayton if (error.Fail()) 10970e5e5a79SGreg Clayton break; 10980e5e5a79SGreg Clayton } 10990e5e5a79SGreg Clayton 1100b9c1b51eSKate Stone if (error.Success()) { 11010e5e5a79SGreg Clayton AddRegexCommandToInterpreter(); 11020e5e5a79SGreg Clayton } 11030e5e5a79SGreg Clayton } 1104b9c1b51eSKate Stone if (error.Fail()) { 11050e5e5a79SGreg Clayton result.AppendError(error.AsCString()); 1106de164aaaSGreg Clayton result.SetStatus(eReturnStatusFailed); 1107de164aaaSGreg Clayton } 11080e5e5a79SGreg Clayton } 11090e5e5a79SGreg Clayton 1110de164aaaSGreg Clayton return result.Succeeded(); 1111de164aaaSGreg Clayton } 1112de164aaaSGreg Clayton 1113b9c1b51eSKate Stone Error AppendRegexSubstitution(const llvm::StringRef ®ex_sed, 1114b9c1b51eSKate Stone bool check_only) { 11150e5e5a79SGreg Clayton Error error; 11160e5e5a79SGreg Clayton 1117b9c1b51eSKate Stone if (!m_regex_cmd_ap) { 1118b9c1b51eSKate Stone error.SetErrorStringWithFormat( 1119b9c1b51eSKate Stone "invalid regular expression command object for: '%.*s'", 1120b9c1b51eSKate Stone (int)regex_sed.size(), regex_sed.data()); 11210e5e5a79SGreg Clayton return error; 1122de164aaaSGreg Clayton } 11230e5e5a79SGreg Clayton 11240e5e5a79SGreg Clayton size_t regex_sed_size = regex_sed.size(); 11250e5e5a79SGreg Clayton 1126b9c1b51eSKate Stone if (regex_sed_size <= 1) { 1127b9c1b51eSKate Stone error.SetErrorStringWithFormat( 1128b9c1b51eSKate Stone "regular expression substitution string is too short: '%.*s'", 1129b9c1b51eSKate Stone (int)regex_sed.size(), regex_sed.data()); 11300e5e5a79SGreg Clayton return error; 11310e5e5a79SGreg Clayton } 11320e5e5a79SGreg Clayton 1133b9c1b51eSKate Stone if (regex_sed[0] != 's') { 1134b9c1b51eSKate Stone error.SetErrorStringWithFormat("regular expression substitution string " 1135b9c1b51eSKate Stone "doesn't start with 's': '%.*s'", 1136b9c1b51eSKate Stone (int)regex_sed.size(), regex_sed.data()); 11370e5e5a79SGreg Clayton return error; 11380e5e5a79SGreg Clayton } 11390e5e5a79SGreg Clayton const size_t first_separator_char_pos = 1; 11400e5e5a79SGreg Clayton // use the char that follows 's' as the regex separator character 11410e5e5a79SGreg Clayton // so we can have "s/<regex>/<subst>/" or "s|<regex>|<subst>|" 11420e5e5a79SGreg Clayton const char separator_char = regex_sed[first_separator_char_pos]; 1143b9c1b51eSKate Stone const size_t second_separator_char_pos = 1144b9c1b51eSKate Stone regex_sed.find(separator_char, first_separator_char_pos + 1); 11450e5e5a79SGreg Clayton 1146b9c1b51eSKate Stone if (second_separator_char_pos == std::string::npos) { 1147b9c1b51eSKate Stone error.SetErrorStringWithFormat( 1148b9c1b51eSKate Stone "missing second '%c' separator char after '%.*s' in '%.*s'", 11490e5e5a79SGreg Clayton separator_char, 11500e5e5a79SGreg Clayton (int)(regex_sed.size() - first_separator_char_pos - 1), 1151ea508635SGreg Clayton regex_sed.data() + (first_separator_char_pos + 1), 1152b9c1b51eSKate Stone (int)regex_sed.size(), regex_sed.data()); 11530e5e5a79SGreg Clayton return error; 11540e5e5a79SGreg Clayton } 11550e5e5a79SGreg Clayton 1156b9c1b51eSKate Stone const size_t third_separator_char_pos = 1157b9c1b51eSKate Stone regex_sed.find(separator_char, second_separator_char_pos + 1); 11580e5e5a79SGreg Clayton 1159b9c1b51eSKate Stone if (third_separator_char_pos == std::string::npos) { 1160b9c1b51eSKate Stone error.SetErrorStringWithFormat( 1161b9c1b51eSKate Stone "missing third '%c' separator char after '%.*s' in '%.*s'", 11620e5e5a79SGreg Clayton separator_char, 11630e5e5a79SGreg Clayton (int)(regex_sed.size() - second_separator_char_pos - 1), 1164ea508635SGreg Clayton regex_sed.data() + (second_separator_char_pos + 1), 1165b9c1b51eSKate Stone (int)regex_sed.size(), regex_sed.data()); 11660e5e5a79SGreg Clayton return error; 11670e5e5a79SGreg Clayton } 11680e5e5a79SGreg Clayton 1169b9c1b51eSKate Stone if (third_separator_char_pos != regex_sed_size - 1) { 11700e5e5a79SGreg Clayton // Make sure that everything that follows the last regex 11710e5e5a79SGreg Clayton // separator char 1172b9c1b51eSKate Stone if (regex_sed.find_first_not_of("\t\n\v\f\r ", 1173b9c1b51eSKate Stone third_separator_char_pos + 1) != 1174b9c1b51eSKate Stone std::string::npos) { 1175b9c1b51eSKate Stone error.SetErrorStringWithFormat( 1176b9c1b51eSKate Stone "extra data found after the '%.*s' regular expression substitution " 1177b9c1b51eSKate Stone "string: '%.*s'", 1178b9c1b51eSKate Stone (int)third_separator_char_pos + 1, regex_sed.data(), 11790e5e5a79SGreg Clayton (int)(regex_sed.size() - third_separator_char_pos - 1), 11800e5e5a79SGreg Clayton regex_sed.data() + (third_separator_char_pos + 1)); 11810e5e5a79SGreg Clayton return error; 11820e5e5a79SGreg Clayton } 1183b9c1b51eSKate Stone } else if (first_separator_char_pos + 1 == second_separator_char_pos) { 1184b9c1b51eSKate Stone error.SetErrorStringWithFormat( 1185b9c1b51eSKate Stone "<regex> can't be empty in 's%c<regex>%c<subst>%c' string: '%.*s'", 1186b9c1b51eSKate Stone separator_char, separator_char, separator_char, (int)regex_sed.size(), 11870e5e5a79SGreg Clayton regex_sed.data()); 11880e5e5a79SGreg Clayton return error; 1189b9c1b51eSKate Stone } else if (second_separator_char_pos + 1 == third_separator_char_pos) { 1190b9c1b51eSKate Stone error.SetErrorStringWithFormat( 1191b9c1b51eSKate Stone "<subst> can't be empty in 's%c<regex>%c<subst>%c' string: '%.*s'", 1192b9c1b51eSKate Stone separator_char, separator_char, separator_char, (int)regex_sed.size(), 11930e5e5a79SGreg Clayton regex_sed.data()); 11940e5e5a79SGreg Clayton return error; 11950e5e5a79SGreg Clayton } 119644d93782SGreg Clayton 1197b9c1b51eSKate Stone if (!check_only) { 1198b9c1b51eSKate Stone std::string regex(regex_sed.substr(first_separator_char_pos + 1, 1199b9c1b51eSKate Stone second_separator_char_pos - 1200b9c1b51eSKate Stone first_separator_char_pos - 1)); 1201b9c1b51eSKate Stone std::string subst(regex_sed.substr(second_separator_char_pos + 1, 1202b9c1b51eSKate Stone third_separator_char_pos - 1203b9c1b51eSKate Stone second_separator_char_pos - 1)); 1204b9c1b51eSKate Stone m_regex_cmd_ap->AddRegexCommand(regex.c_str(), subst.c_str()); 120544d93782SGreg Clayton } 12060e5e5a79SGreg Clayton return error; 1207de164aaaSGreg Clayton } 1208de164aaaSGreg Clayton 1209b9c1b51eSKate Stone void AddRegexCommandToInterpreter() { 1210b9c1b51eSKate Stone if (m_regex_cmd_ap) { 1211b9c1b51eSKate Stone if (m_regex_cmd_ap->HasRegexEntries()) { 1212de164aaaSGreg Clayton CommandObjectSP cmd_sp(m_regex_cmd_ap.release()); 1213de164aaaSGreg Clayton m_interpreter.AddCommand(cmd_sp->GetCommandName(), cmd_sp, true); 1214de164aaaSGreg Clayton } 1215de164aaaSGreg Clayton } 1216de164aaaSGreg Clayton } 1217de164aaaSGreg Clayton 1218de164aaaSGreg Clayton private: 12197b0992d9SGreg Clayton std::unique_ptr<CommandObjectRegexCommand> m_regex_cmd_ap; 1220de164aaaSGreg Clayton 1221b9c1b51eSKate Stone class CommandOptions : public Options { 1222de164aaaSGreg Clayton public: 1223b9c1b51eSKate Stone CommandOptions() : Options() {} 1224de164aaaSGreg Clayton 12256e3d8e7fSEugene Zelenko ~CommandOptions() override = default; 1226de164aaaSGreg Clayton 1227b9c1b51eSKate Stone Error SetOptionValue(uint32_t option_idx, const char *option_arg, 1228b9c1b51eSKate Stone ExecutionContext *execution_context) override { 1229de164aaaSGreg Clayton Error error; 12303bcdfc0eSGreg Clayton const int short_option = m_getopt_table[option_idx].val; 1231de164aaaSGreg Clayton 1232b9c1b51eSKate Stone switch (short_option) { 1233de164aaaSGreg Clayton case 'h': 1234de164aaaSGreg Clayton m_help.assign(option_arg); 1235de164aaaSGreg Clayton break; 1236de164aaaSGreg Clayton case 's': 1237de164aaaSGreg Clayton m_syntax.assign(option_arg); 1238de164aaaSGreg Clayton break; 1239de164aaaSGreg Clayton default: 1240b9c1b51eSKate Stone error.SetErrorStringWithFormat("unrecognized option '%c'", 1241b9c1b51eSKate Stone short_option); 1242de164aaaSGreg Clayton break; 1243de164aaaSGreg Clayton } 1244de164aaaSGreg Clayton 1245de164aaaSGreg Clayton return error; 1246de164aaaSGreg Clayton } 1247de164aaaSGreg Clayton 1248b9c1b51eSKate Stone void OptionParsingStarting(ExecutionContext *execution_context) override { 1249de164aaaSGreg Clayton m_help.clear(); 1250de164aaaSGreg Clayton m_syntax.clear(); 1251de164aaaSGreg Clayton } 1252de164aaaSGreg Clayton 12531f0f5b5bSZachary Turner llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 125470602439SZachary Turner return llvm::makeArrayRef(g_regex_options); 12551f0f5b5bSZachary Turner } 1256de164aaaSGreg Clayton 1257b9c1b51eSKate Stone const char *GetHelp() { 12586e3d8e7fSEugene Zelenko return (m_help.empty() ? nullptr : m_help.c_str()); 1259de164aaaSGreg Clayton } 12606e3d8e7fSEugene Zelenko 1261b9c1b51eSKate Stone const char *GetSyntax() { 12626e3d8e7fSEugene Zelenko return (m_syntax.empty() ? nullptr : m_syntax.c_str()); 1263de164aaaSGreg Clayton } 12646e3d8e7fSEugene Zelenko 1265de164aaaSGreg Clayton protected: 12666e3d8e7fSEugene Zelenko // Instance variables to hold the values for command options. 12676e3d8e7fSEugene Zelenko 1268de164aaaSGreg Clayton std::string m_help; 1269de164aaaSGreg Clayton std::string m_syntax; 1270de164aaaSGreg Clayton }; 1271de164aaaSGreg Clayton 1272b9c1b51eSKate Stone Options *GetOptions() override { return &m_options; } 1273de164aaaSGreg Clayton 12745a988416SJim Ingham CommandOptions m_options; 1275de164aaaSGreg Clayton }; 1276de164aaaSGreg Clayton 1277b9c1b51eSKate Stone class CommandObjectPythonFunction : public CommandObjectRaw { 1278223383edSEnrico Granata public: 1279b9c1b51eSKate Stone CommandObjectPythonFunction(CommandInterpreter &interpreter, std::string name, 1280b9c1b51eSKate Stone std::string funct, std::string help, 1281b9c1b51eSKate Stone ScriptedCommandSynchronicity synch) 1282b9c1b51eSKate Stone : CommandObjectRaw(interpreter, name.c_str(), nullptr, nullptr), 1283b9c1b51eSKate Stone m_function_name(funct), m_synchro(synch), m_fetched_help_long(false) { 1284735152e3SEnrico Granata if (!help.empty()) 1285735152e3SEnrico Granata SetHelp(help.c_str()); 1286b9c1b51eSKate Stone else { 1287735152e3SEnrico Granata StreamString stream; 1288735152e3SEnrico Granata stream.Printf("For more information run 'help %s'", name.c_str()); 1289735152e3SEnrico Granata SetHelp(stream.GetData()); 1290735152e3SEnrico Granata } 1291223383edSEnrico Granata } 1292223383edSEnrico Granata 12936e3d8e7fSEugene Zelenko ~CommandObjectPythonFunction() override = default; 1294223383edSEnrico Granata 1295b9c1b51eSKate Stone bool IsRemovable() const override { return true; } 12965a988416SJim Ingham 1297b9c1b51eSKate Stone const std::string &GetFunctionName() { return m_function_name; } 12985a988416SJim Ingham 1299b9c1b51eSKate Stone ScriptedCommandSynchronicity GetSynchronicity() { return m_synchro; } 13005a988416SJim Ingham 1301b9c1b51eSKate Stone const char *GetHelpLong() override { 1302b9c1b51eSKate Stone if (!m_fetched_help_long) { 1303fac939e9SEnrico Granata ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter(); 1304b9c1b51eSKate Stone if (scripter) { 1305fac939e9SEnrico Granata std::string docstring; 1306b9c1b51eSKate Stone m_fetched_help_long = scripter->GetDocumentationForItem( 1307b9c1b51eSKate Stone m_function_name.c_str(), docstring); 1308fac939e9SEnrico Granata if (!docstring.empty()) 1309bfb75e9bSEnrico Granata SetHelpLong(docstring.c_str()); 1310fac939e9SEnrico Granata } 1311fac939e9SEnrico Granata } 1312fac939e9SEnrico Granata return CommandObjectRaw::GetHelpLong(); 1313fac939e9SEnrico Granata } 1314fac939e9SEnrico Granata 13155a988416SJim Ingham protected: 1316b9c1b51eSKate Stone bool DoExecute(const char *raw_command_line, 1317b9c1b51eSKate Stone CommandReturnObject &result) override { 1318223383edSEnrico Granata ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter(); 1319223383edSEnrico Granata 1320223383edSEnrico Granata Error error; 1321223383edSEnrico Granata 132270f11f88SJim Ingham result.SetStatus(eReturnStatusInvalid); 132370f11f88SJim Ingham 1324b9c1b51eSKate Stone if (!scripter || 1325b9c1b51eSKate Stone !scripter->RunScriptBasedCommand(m_function_name.c_str(), 1326b9c1b51eSKate Stone raw_command_line, m_synchro, result, 1327b9c1b51eSKate Stone error, m_exe_ctx)) { 1328223383edSEnrico Granata result.AppendError(error.AsCString()); 1329223383edSEnrico Granata result.SetStatus(eReturnStatusFailed); 1330b9c1b51eSKate Stone } else { 133170f11f88SJim Ingham // Don't change the status if the command already set it... 1332b9c1b51eSKate Stone if (result.GetStatus() == eReturnStatusInvalid) { 1333b9c1b51eSKate Stone if (result.GetOutputData() == nullptr || 1334b9c1b51eSKate Stone result.GetOutputData()[0] == '\0') 1335223383edSEnrico Granata result.SetStatus(eReturnStatusSuccessFinishNoResult); 133670f11f88SJim Ingham else 133770f11f88SJim Ingham result.SetStatus(eReturnStatusSuccessFinishResult); 133870f11f88SJim Ingham } 133970f11f88SJim Ingham } 1340223383edSEnrico Granata 1341223383edSEnrico Granata return result.Succeeded(); 1342223383edSEnrico Granata } 1343223383edSEnrico Granata 13446e3d8e7fSEugene Zelenko private: 13456e3d8e7fSEugene Zelenko std::string m_function_name; 13466e3d8e7fSEugene Zelenko ScriptedCommandSynchronicity m_synchro; 13476e3d8e7fSEugene Zelenko bool m_fetched_help_long; 1348223383edSEnrico Granata }; 1349223383edSEnrico Granata 1350b9c1b51eSKate Stone class CommandObjectScriptingObject : public CommandObjectRaw { 13519fe00e52SEnrico Granata public: 13529fe00e52SEnrico Granata CommandObjectScriptingObject(CommandInterpreter &interpreter, 13539fe00e52SEnrico Granata std::string name, 13540641ca1aSZachary Turner StructuredData::GenericSP cmd_obj_sp, 1355b9c1b51eSKate Stone ScriptedCommandSynchronicity synch) 1356b9c1b51eSKate Stone : CommandObjectRaw(interpreter, name.c_str(), nullptr, nullptr), 1357b9c1b51eSKate Stone m_cmd_obj_sp(cmd_obj_sp), m_synchro(synch), m_fetched_help_short(false), 1358b9c1b51eSKate Stone m_fetched_help_long(false) { 13599fe00e52SEnrico Granata StreamString stream; 13609fe00e52SEnrico Granata stream.Printf("For more information run 'help %s'", name.c_str()); 13619fe00e52SEnrico Granata SetHelp(stream.GetData()); 1362e87764f2SEnrico Granata if (ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter()) 1363e87764f2SEnrico Granata GetFlags().Set(scripter->GetFlagsForCommandObject(cmd_obj_sp)); 13649fe00e52SEnrico Granata } 13659fe00e52SEnrico Granata 13666e3d8e7fSEugene Zelenko ~CommandObjectScriptingObject() override = default; 13679fe00e52SEnrico Granata 1368b9c1b51eSKate Stone bool IsRemovable() const override { return true; } 13699fe00e52SEnrico Granata 1370b9c1b51eSKate Stone StructuredData::GenericSP GetImplementingObject() { return m_cmd_obj_sp; } 13719fe00e52SEnrico Granata 1372b9c1b51eSKate Stone ScriptedCommandSynchronicity GetSynchronicity() { return m_synchro; } 13739fe00e52SEnrico Granata 1374b9c1b51eSKate Stone const char *GetHelp() override { 1375b9c1b51eSKate Stone if (!m_fetched_help_short) { 13766f79bb2dSEnrico Granata ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter(); 1377b9c1b51eSKate Stone if (scripter) { 13786f79bb2dSEnrico Granata std::string docstring; 1379b9c1b51eSKate Stone m_fetched_help_short = 1380b9c1b51eSKate Stone scripter->GetShortHelpForCommandObject(m_cmd_obj_sp, docstring); 13816f79bb2dSEnrico Granata if (!docstring.empty()) 1382bfb75e9bSEnrico Granata SetHelp(docstring.c_str()); 13836f79bb2dSEnrico Granata } 13846f79bb2dSEnrico Granata } 13856f79bb2dSEnrico Granata return CommandObjectRaw::GetHelp(); 13866f79bb2dSEnrico Granata } 13876f79bb2dSEnrico Granata 1388b9c1b51eSKate Stone const char *GetHelpLong() override { 1389b9c1b51eSKate Stone if (!m_fetched_help_long) { 13906f79bb2dSEnrico Granata ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter(); 1391b9c1b51eSKate Stone if (scripter) { 13926f79bb2dSEnrico Granata std::string docstring; 1393b9c1b51eSKate Stone m_fetched_help_long = 1394b9c1b51eSKate Stone scripter->GetLongHelpForCommandObject(m_cmd_obj_sp, docstring); 13956f79bb2dSEnrico Granata if (!docstring.empty()) 1396bfb75e9bSEnrico Granata SetHelpLong(docstring.c_str()); 13976f79bb2dSEnrico Granata } 13986f79bb2dSEnrico Granata } 13999fe00e52SEnrico Granata return CommandObjectRaw::GetHelpLong(); 14009fe00e52SEnrico Granata } 14019fe00e52SEnrico Granata 14029fe00e52SEnrico Granata protected: 1403b9c1b51eSKate Stone bool DoExecute(const char *raw_command_line, 1404b9c1b51eSKate Stone CommandReturnObject &result) override { 14059fe00e52SEnrico Granata ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter(); 14069fe00e52SEnrico Granata 14079fe00e52SEnrico Granata Error error; 14089fe00e52SEnrico Granata 14099fe00e52SEnrico Granata result.SetStatus(eReturnStatusInvalid); 14109fe00e52SEnrico Granata 1411b9c1b51eSKate Stone if (!scripter || 1412b9c1b51eSKate Stone !scripter->RunScriptBasedCommand(m_cmd_obj_sp, raw_command_line, 1413b9c1b51eSKate Stone m_synchro, result, error, m_exe_ctx)) { 14149fe00e52SEnrico Granata result.AppendError(error.AsCString()); 14159fe00e52SEnrico Granata result.SetStatus(eReturnStatusFailed); 1416b9c1b51eSKate Stone } else { 14179fe00e52SEnrico Granata // Don't change the status if the command already set it... 1418b9c1b51eSKate Stone if (result.GetStatus() == eReturnStatusInvalid) { 1419b9c1b51eSKate Stone if (result.GetOutputData() == nullptr || 1420b9c1b51eSKate Stone result.GetOutputData()[0] == '\0') 14219fe00e52SEnrico Granata result.SetStatus(eReturnStatusSuccessFinishNoResult); 14229fe00e52SEnrico Granata else 14239fe00e52SEnrico Granata result.SetStatus(eReturnStatusSuccessFinishResult); 14249fe00e52SEnrico Granata } 14259fe00e52SEnrico Granata } 14269fe00e52SEnrico Granata 14279fe00e52SEnrico Granata return result.Succeeded(); 14289fe00e52SEnrico Granata } 14299fe00e52SEnrico Granata 14306e3d8e7fSEugene Zelenko private: 14316e3d8e7fSEugene Zelenko StructuredData::GenericSP m_cmd_obj_sp; 14326e3d8e7fSEugene Zelenko ScriptedCommandSynchronicity m_synchro; 14336e3d8e7fSEugene Zelenko bool m_fetched_help_short : 1; 14346e3d8e7fSEugene Zelenko bool m_fetched_help_long : 1; 14359fe00e52SEnrico Granata }; 14369fe00e52SEnrico Granata 1437a9dbf432SEnrico Granata //------------------------------------------------------------------------- 1438a9dbf432SEnrico Granata // CommandObjectCommandsScriptImport 1439a9dbf432SEnrico Granata //------------------------------------------------------------------------- 1440a9dbf432SEnrico Granata 14411f0f5b5bSZachary Turner OptionDefinition g_script_import_options[] = { 14421f0f5b5bSZachary Turner // clang-format off 14431f0f5b5bSZachary Turner { LLDB_OPT_SET_1, false, "allow-reload", 'r', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeNone, "Allow the script to be loaded even if it was already loaded before. This argument exists for backwards compatibility, but reloading is always allowed, whether you specify it or not." }, 14441f0f5b5bSZachary Turner // clang-format on 14451f0f5b5bSZachary Turner }; 14461f0f5b5bSZachary Turner 1447b9c1b51eSKate Stone class CommandObjectCommandsScriptImport : public CommandObjectParsed { 14485a988416SJim Ingham public: 1449b9c1b51eSKate Stone CommandObjectCommandsScriptImport(CommandInterpreter &interpreter) 1450b9c1b51eSKate Stone : CommandObjectParsed(interpreter, "command script import", 1451b9c1b51eSKate Stone "Import a scripting module in LLDB.", nullptr), 1452b9c1b51eSKate Stone m_options() { 14535a988416SJim Ingham CommandArgumentEntry arg1; 14545a988416SJim Ingham CommandArgumentData cmd_arg; 14555a988416SJim Ingham 14565a988416SJim Ingham // Define the first (and only) variant of this arg. 14575a988416SJim Ingham cmd_arg.arg_type = eArgTypeFilename; 14583b00e35bSEnrico Granata cmd_arg.arg_repetition = eArgRepeatPlus; 14595a988416SJim Ingham 1460b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the 1461b9c1b51eSKate Stone // argument entry. 14625a988416SJim Ingham arg1.push_back(cmd_arg); 14635a988416SJim Ingham 14645a988416SJim Ingham // Push the data for the first argument into the m_arguments vector. 14655a988416SJim Ingham m_arguments.push_back(arg1); 14665a988416SJim Ingham } 14675a988416SJim Ingham 14686e3d8e7fSEugene Zelenko ~CommandObjectCommandsScriptImport() override = default; 14695a988416SJim Ingham 1470b9c1b51eSKate Stone int HandleArgumentCompletion(Args &input, int &cursor_index, 14715a988416SJim Ingham int &cursor_char_position, 14725a988416SJim Ingham OptionElementVector &opt_element_vector, 1473b9c1b51eSKate Stone int match_start_point, int max_return_elements, 14745a988416SJim Ingham bool &word_complete, 1475b9c1b51eSKate Stone StringList &matches) override { 14765a988416SJim Ingham std::string completion_str(input.GetArgumentAtIndex(cursor_index)); 14775a988416SJim Ingham completion_str.erase(cursor_char_position); 14785a988416SJim Ingham 1479b9c1b51eSKate Stone CommandCompletions::InvokeCommonCompletionCallbacks( 1480b9c1b51eSKate Stone GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion, 1481b9c1b51eSKate Stone completion_str.c_str(), match_start_point, max_return_elements, nullptr, 1482b9c1b51eSKate Stone word_complete, matches); 14835a988416SJim Ingham return matches.GetSize(); 14845a988416SJim Ingham } 14855a988416SJim Ingham 1486b9c1b51eSKate Stone Options *GetOptions() override { return &m_options; } 14875a988416SJim Ingham 14885a988416SJim Ingham protected: 1489b9c1b51eSKate Stone class CommandOptions : public Options { 14900a305db7SEnrico Granata public: 1491b9c1b51eSKate Stone CommandOptions() : Options() {} 14920a305db7SEnrico Granata 14936e3d8e7fSEugene Zelenko ~CommandOptions() override = default; 14940a305db7SEnrico Granata 1495b9c1b51eSKate Stone Error SetOptionValue(uint32_t option_idx, const char *option_arg, 1496b9c1b51eSKate Stone ExecutionContext *execution_context) override { 14970a305db7SEnrico Granata Error error; 14983bcdfc0eSGreg Clayton const int short_option = m_getopt_table[option_idx].val; 14990a305db7SEnrico Granata 1500b9c1b51eSKate Stone switch (short_option) { 15010a305db7SEnrico Granata case 'r': 15020a305db7SEnrico Granata m_allow_reload = true; 15030a305db7SEnrico Granata break; 15040a305db7SEnrico Granata default: 1505b9c1b51eSKate Stone error.SetErrorStringWithFormat("unrecognized option '%c'", 1506b9c1b51eSKate Stone short_option); 15070a305db7SEnrico Granata break; 15080a305db7SEnrico Granata } 15090a305db7SEnrico Granata 15100a305db7SEnrico Granata return error; 15110a305db7SEnrico Granata } 15120a305db7SEnrico Granata 1513b9c1b51eSKate Stone void OptionParsingStarting(ExecutionContext *execution_context) override { 1514e0c70f1bSEnrico Granata m_allow_reload = true; 15150a305db7SEnrico Granata } 15160a305db7SEnrico Granata 15171f0f5b5bSZachary Turner llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 151870602439SZachary Turner return llvm::makeArrayRef(g_script_import_options); 15191f0f5b5bSZachary Turner } 15200a305db7SEnrico Granata 15210a305db7SEnrico Granata // Instance variables to hold the values for command options. 15220a305db7SEnrico Granata 15230a305db7SEnrico Granata bool m_allow_reload; 15240a305db7SEnrico Granata }; 15250a305db7SEnrico Granata 1526b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override { 1527b9c1b51eSKate Stone if (m_interpreter.GetDebugger().GetScriptLanguage() != 1528b9c1b51eSKate Stone lldb::eScriptLanguagePython) { 1529b9c1b51eSKate Stone result.AppendError("only scripting language supported for module " 1530b9c1b51eSKate Stone "importing is currently Python"); 1531a9dbf432SEnrico Granata result.SetStatus(eReturnStatusFailed); 1532a9dbf432SEnrico Granata return false; 1533a9dbf432SEnrico Granata } 1534a9dbf432SEnrico Granata 15355a988416SJim Ingham size_t argc = command.GetArgumentCount(); 1536b9c1b51eSKate Stone if (0 == argc) { 15373b00e35bSEnrico Granata result.AppendError("command script import needs one or more arguments"); 1538a9dbf432SEnrico Granata result.SetStatus(eReturnStatusFailed); 1539a9dbf432SEnrico Granata return false; 1540a9dbf432SEnrico Granata } 1541a9dbf432SEnrico Granata 1542b9c1b51eSKate Stone for (size_t i = 0; i < argc; i++) { 15433b00e35bSEnrico Granata std::string path = command.GetArgumentAtIndex(i); 1544a9dbf432SEnrico Granata Error error; 1545a9dbf432SEnrico Granata 1546c9d645d3SGreg Clayton const bool init_session = true; 1547b9c1b51eSKate Stone // FIXME: this is necessary because CommandObject::CheckRequirements() 1548b9c1b51eSKate Stone // assumes that 1549b9c1b51eSKate Stone // commands won't ever be recursively invoked, but it's actually possible 1550b9c1b51eSKate Stone // to craft 1551b9c1b51eSKate Stone // a Python script that does other "command script imports" in 1552b9c1b51eSKate Stone // __lldb_init_module 1553b9c1b51eSKate Stone // the real fix is to have recursive commands possible with a 1554b9c1b51eSKate Stone // CommandInvocation object 1555b9c1b51eSKate Stone // separate from the CommandObject itself, so that recursive command 1556b9c1b51eSKate Stone // invocations 1557b9c1b51eSKate Stone // won't stomp on each other (wrt to execution contents, options, and 1558b9c1b51eSKate Stone // more) 1559078551c7SEnrico Granata m_exe_ctx.Clear(); 1560b9c1b51eSKate Stone if (m_interpreter.GetScriptInterpreter()->LoadScriptingModule( 1561b9c1b51eSKate Stone path.c_str(), m_options.m_allow_reload, init_session, error)) { 1562a9dbf432SEnrico Granata result.SetStatus(eReturnStatusSuccessFinishNoResult); 1563b9c1b51eSKate Stone } else { 1564b9c1b51eSKate Stone result.AppendErrorWithFormat("module importing failed: %s", 1565b9c1b51eSKate Stone error.AsCString()); 1566a9dbf432SEnrico Granata result.SetStatus(eReturnStatusFailed); 1567a9dbf432SEnrico Granata } 15683b00e35bSEnrico Granata } 1569a9dbf432SEnrico Granata 1570a9dbf432SEnrico Granata return result.Succeeded(); 1571a9dbf432SEnrico Granata } 15720a305db7SEnrico Granata 15735a988416SJim Ingham CommandOptions m_options; 1574a9dbf432SEnrico Granata }; 1575223383edSEnrico Granata 1576223383edSEnrico Granata //------------------------------------------------------------------------- 1577223383edSEnrico Granata // CommandObjectCommandsScriptAdd 1578223383edSEnrico Granata //------------------------------------------------------------------------- 1579223383edSEnrico Granata 15801f0f5b5bSZachary Turner static OptionEnumValueElement g_script_synchro_type[] = { 15811f0f5b5bSZachary Turner {eScriptedCommandSynchronicitySynchronous, "synchronous", 15821f0f5b5bSZachary Turner "Run synchronous"}, 15831f0f5b5bSZachary Turner {eScriptedCommandSynchronicityAsynchronous, "asynchronous", 15841f0f5b5bSZachary Turner "Run asynchronous"}, 15851f0f5b5bSZachary Turner {eScriptedCommandSynchronicityCurrentValue, "current", 15861f0f5b5bSZachary Turner "Do not alter current setting"}, 15871f0f5b5bSZachary Turner {0, nullptr, nullptr}}; 15881f0f5b5bSZachary Turner 15891f0f5b5bSZachary Turner static OptionDefinition g_script_add_options[] = { 15901f0f5b5bSZachary Turner // clang-format off 15911f0f5b5bSZachary Turner { LLDB_OPT_SET_1, false, "function", 'f', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypePythonFunction, "Name of the Python function to bind to this command name." }, 15921f0f5b5bSZachary Turner { LLDB_OPT_SET_2, false, "class", 'c', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypePythonClass, "Name of the Python class to bind to this command name." }, 15931f0f5b5bSZachary Turner { LLDB_OPT_SET_1, false, "help" , 'h', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeHelpText, "The help text to display for this command." }, 15941f0f5b5bSZachary Turner { LLDB_OPT_SET_ALL, false, "synchronicity", 's', OptionParser::eRequiredArgument, nullptr, g_script_synchro_type, 0, eArgTypeScriptedCommandSynchronicity, "Set the synchronicity of this command's executions with regard to LLDB event system." }, 15951f0f5b5bSZachary Turner // clang-format on 15961f0f5b5bSZachary Turner }; 15971f0f5b5bSZachary Turner 1598b9c1b51eSKate Stone class CommandObjectCommandsScriptAdd : public CommandObjectParsed, 1599b9c1b51eSKate Stone public IOHandlerDelegateMultiline { 16005a988416SJim Ingham public: 1601b9c1b51eSKate Stone CommandObjectCommandsScriptAdd(CommandInterpreter &interpreter) 1602b9c1b51eSKate Stone : CommandObjectParsed(interpreter, "command script add", 16035a988416SJim Ingham "Add a scripted function as an LLDB command.", 16046e3d8e7fSEugene Zelenko nullptr), 1605b9c1b51eSKate Stone IOHandlerDelegateMultiline("DONE"), m_options() { 16065a988416SJim Ingham CommandArgumentEntry arg1; 16075a988416SJim Ingham CommandArgumentData cmd_arg; 16085a988416SJim Ingham 16095a988416SJim Ingham // Define the first (and only) variant of this arg. 16105a988416SJim Ingham cmd_arg.arg_type = eArgTypeCommandName; 16115a988416SJim Ingham cmd_arg.arg_repetition = eArgRepeatPlain; 16125a988416SJim Ingham 1613b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the 1614b9c1b51eSKate Stone // argument entry. 16155a988416SJim Ingham arg1.push_back(cmd_arg); 16165a988416SJim Ingham 16175a988416SJim Ingham // Push the data for the first argument into the m_arguments vector. 16185a988416SJim Ingham m_arguments.push_back(arg1); 16195a988416SJim Ingham } 16205a988416SJim Ingham 16216e3d8e7fSEugene Zelenko ~CommandObjectCommandsScriptAdd() override = default; 16225a988416SJim Ingham 1623b9c1b51eSKate Stone Options *GetOptions() override { return &m_options; } 16245a988416SJim Ingham 16255a988416SJim Ingham protected: 1626b9c1b51eSKate Stone class CommandOptions : public Options { 1627223383edSEnrico Granata public: 1628b9c1b51eSKate Stone CommandOptions() 1629b9c1b51eSKate Stone : Options(), m_class_name(), m_funct_name(), m_short_help(), 1630b9c1b51eSKate Stone m_synchronicity(eScriptedCommandSynchronicitySynchronous) {} 1631223383edSEnrico Granata 16326e3d8e7fSEugene Zelenko ~CommandOptions() override = default; 1633223383edSEnrico Granata 1634b9c1b51eSKate Stone Error SetOptionValue(uint32_t option_idx, const char *option_arg, 1635b9c1b51eSKate Stone ExecutionContext *execution_context) override { 1636223383edSEnrico Granata Error error; 16373bcdfc0eSGreg Clayton const int short_option = m_getopt_table[option_idx].val; 1638223383edSEnrico Granata 1639b9c1b51eSKate Stone switch (short_option) { 1640223383edSEnrico Granata case 'f': 1641735152e3SEnrico Granata if (option_arg) 1642735152e3SEnrico Granata m_funct_name.assign(option_arg); 1643735152e3SEnrico Granata break; 16449fe00e52SEnrico Granata case 'c': 16459fe00e52SEnrico Granata if (option_arg) 16469fe00e52SEnrico Granata m_class_name.assign(option_arg); 16479fe00e52SEnrico Granata break; 1648735152e3SEnrico Granata case 'h': 1649735152e3SEnrico Granata if (option_arg) 1650735152e3SEnrico Granata m_short_help.assign(option_arg); 1651223383edSEnrico Granata break; 16520a305db7SEnrico Granata case 's': 1653b9c1b51eSKate Stone m_synchronicity = 1654b9c1b51eSKate Stone (ScriptedCommandSynchronicity)Args::StringToOptionEnum( 16558cef4b0bSZachary Turner llvm::StringRef::withNullAsEmpty(option_arg), 16568cef4b0bSZachary Turner GetDefinitions()[option_idx].enum_values, 0, error); 16570a305db7SEnrico Granata if (!error.Success()) 1658b9c1b51eSKate Stone error.SetErrorStringWithFormat( 1659b9c1b51eSKate Stone "unrecognized value for synchronicity '%s'", option_arg); 16600a305db7SEnrico Granata break; 1661223383edSEnrico Granata default: 1662b9c1b51eSKate Stone error.SetErrorStringWithFormat("unrecognized option '%c'", 1663b9c1b51eSKate Stone short_option); 1664223383edSEnrico Granata break; 1665223383edSEnrico Granata } 1666223383edSEnrico Granata 1667223383edSEnrico Granata return error; 1668223383edSEnrico Granata } 1669223383edSEnrico Granata 1670b9c1b51eSKate Stone void OptionParsingStarting(ExecutionContext *execution_context) override { 16719fe00e52SEnrico Granata m_class_name.clear(); 1672735152e3SEnrico Granata m_funct_name.clear(); 1673735152e3SEnrico Granata m_short_help.clear(); 167444d93782SGreg Clayton m_synchronicity = eScriptedCommandSynchronicitySynchronous; 1675223383edSEnrico Granata } 1676223383edSEnrico Granata 16771f0f5b5bSZachary Turner llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 167870602439SZachary Turner return llvm::makeArrayRef(g_script_add_options); 16791f0f5b5bSZachary Turner } 1680223383edSEnrico Granata 1681223383edSEnrico Granata // Instance variables to hold the values for command options. 1682223383edSEnrico Granata 16839fe00e52SEnrico Granata std::string m_class_name; 1684223383edSEnrico Granata std::string m_funct_name; 1685735152e3SEnrico Granata std::string m_short_help; 168644d93782SGreg Clayton ScriptedCommandSynchronicity m_synchronicity; 1687223383edSEnrico Granata }; 1688223383edSEnrico Granata 1689b9c1b51eSKate Stone void IOHandlerActivated(IOHandler &io_handler) override { 169044d93782SGreg Clayton StreamFileSP output_sp(io_handler.GetOutputStreamFile()); 1691b9c1b51eSKate Stone if (output_sp) { 169244d93782SGreg Clayton output_sp->PutCString(g_python_command_instructions); 169344d93782SGreg Clayton output_sp->Flush(); 1694223383edSEnrico Granata } 1695223383edSEnrico Granata } 1696223383edSEnrico Granata 1697b9c1b51eSKate Stone void IOHandlerInputComplete(IOHandler &io_handler, 1698b9c1b51eSKate Stone std::string &data) override { 169944d93782SGreg Clayton StreamFileSP error_sp = io_handler.GetErrorStreamFile(); 170044d93782SGreg Clayton 170144d93782SGreg Clayton ScriptInterpreter *interpreter = m_interpreter.GetScriptInterpreter(); 1702b9c1b51eSKate Stone if (interpreter) { 170344d93782SGreg Clayton 170444d93782SGreg Clayton StringList lines; 170544d93782SGreg Clayton lines.SplitIntoLines(data); 1706b9c1b51eSKate Stone if (lines.GetSize() > 0) { 1707a73b7df7SEnrico Granata std::string funct_name_str; 1708b9c1b51eSKate Stone if (interpreter->GenerateScriptAliasFunction(lines, funct_name_str)) { 1709b9c1b51eSKate Stone if (funct_name_str.empty()) { 1710b9c1b51eSKate Stone error_sp->Printf("error: unable to obtain a function name, didn't " 1711b9c1b51eSKate Stone "add python command.\n"); 171244d93782SGreg Clayton error_sp->Flush(); 1713b9c1b51eSKate Stone } else { 1714223383edSEnrico Granata // everything should be fine now, let's add this alias 1715223383edSEnrico Granata 1716b9c1b51eSKate Stone CommandObjectSP command_obj_sp(new CommandObjectPythonFunction( 1717b9c1b51eSKate Stone m_interpreter, m_cmd_name, funct_name_str.c_str(), m_short_help, 171844d93782SGreg Clayton m_synchronicity)); 1719223383edSEnrico Granata 1720b9c1b51eSKate Stone if (!m_interpreter.AddUserCommand(m_cmd_name, command_obj_sp, 1721b9c1b51eSKate Stone true)) { 1722b9c1b51eSKate Stone error_sp->Printf("error: unable to add selected command, didn't " 1723b9c1b51eSKate Stone "add python command.\n"); 172444d93782SGreg Clayton error_sp->Flush(); 1725223383edSEnrico Granata } 1726223383edSEnrico Granata } 1727b9c1b51eSKate Stone } else { 1728b9c1b51eSKate Stone error_sp->Printf( 1729b9c1b51eSKate Stone "error: unable to create function, didn't add python command.\n"); 173044d93782SGreg Clayton error_sp->Flush(); 173144d93782SGreg Clayton } 1732b9c1b51eSKate Stone } else { 173344d93782SGreg Clayton error_sp->Printf("error: empty function, didn't add python command.\n"); 173444d93782SGreg Clayton error_sp->Flush(); 173544d93782SGreg Clayton } 1736b9c1b51eSKate Stone } else { 1737b9c1b51eSKate Stone error_sp->Printf( 1738b9c1b51eSKate Stone "error: script interpreter missing, didn't add python command.\n"); 173944d93782SGreg Clayton error_sp->Flush(); 174044d93782SGreg Clayton } 174144d93782SGreg Clayton 174244d93782SGreg Clayton io_handler.SetIsDone(true); 174344d93782SGreg Clayton } 1744223383edSEnrico Granata 17455a988416SJim Ingham protected: 1746b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override { 1747b9c1b51eSKate Stone if (m_interpreter.GetDebugger().GetScriptLanguage() != 1748b9c1b51eSKate Stone lldb::eScriptLanguagePython) { 1749b9c1b51eSKate Stone result.AppendError("only scripting language supported for scripted " 1750b9c1b51eSKate Stone "commands is currently Python"); 175199f0b8f9SEnrico Granata result.SetStatus(eReturnStatusFailed); 175299f0b8f9SEnrico Granata return false; 175399f0b8f9SEnrico Granata } 175499f0b8f9SEnrico Granata 17555a988416SJim Ingham size_t argc = command.GetArgumentCount(); 1756223383edSEnrico Granata 1757b9c1b51eSKate Stone if (argc != 1) { 1758223383edSEnrico Granata result.AppendError("'command script add' requires one argument"); 1759223383edSEnrico Granata result.SetStatus(eReturnStatusFailed); 1760223383edSEnrico Granata return false; 1761223383edSEnrico Granata } 1762223383edSEnrico Granata 1763735152e3SEnrico Granata // Store the options in case we get multi-line input 176444d93782SGreg Clayton m_cmd_name = command.GetArgumentAtIndex(0); 1765735152e3SEnrico Granata m_short_help.assign(m_options.m_short_help); 176644d93782SGreg Clayton m_synchronicity = m_options.m_synchronicity; 1767223383edSEnrico Granata 1768b9c1b51eSKate Stone if (m_options.m_class_name.empty()) { 1769b9c1b51eSKate Stone if (m_options.m_funct_name.empty()) { 1770b9c1b51eSKate Stone m_interpreter.GetPythonCommandsFromIOHandler( 1771b9c1b51eSKate Stone " ", // Prompt 177244d93782SGreg Clayton *this, // IOHandlerDelegate 177344d93782SGreg Clayton true, // Run IOHandler in async mode 1774b9c1b51eSKate Stone nullptr); // Baton for the "io_handler" that will be passed back 1775b9c1b51eSKate Stone // into our IOHandlerDelegate functions 1776b9c1b51eSKate Stone } else { 1777b9c1b51eSKate Stone CommandObjectSP new_cmd(new CommandObjectPythonFunction( 1778b9c1b51eSKate Stone m_interpreter, m_cmd_name, m_options.m_funct_name, 1779b9c1b51eSKate Stone m_options.m_short_help, m_synchronicity)); 1780b9c1b51eSKate Stone if (m_interpreter.AddUserCommand(m_cmd_name, new_cmd, true)) { 1781223383edSEnrico Granata result.SetStatus(eReturnStatusSuccessFinishNoResult); 1782b9c1b51eSKate Stone } else { 1783223383edSEnrico Granata result.AppendError("cannot add command"); 1784223383edSEnrico Granata result.SetStatus(eReturnStatusFailed); 1785223383edSEnrico Granata } 1786223383edSEnrico Granata } 1787b9c1b51eSKate Stone } else { 1788b9c1b51eSKate Stone ScriptInterpreter *interpreter = 1789b9c1b51eSKate Stone GetCommandInterpreter().GetScriptInterpreter(); 1790b9c1b51eSKate Stone if (!interpreter) { 17919fe00e52SEnrico Granata result.AppendError("cannot find ScriptInterpreter"); 17929fe00e52SEnrico Granata result.SetStatus(eReturnStatusFailed); 17939fe00e52SEnrico Granata return false; 17949fe00e52SEnrico Granata } 17959fe00e52SEnrico Granata 1796b9c1b51eSKate Stone auto cmd_obj_sp = interpreter->CreateScriptCommandObject( 1797b9c1b51eSKate Stone m_options.m_class_name.c_str()); 1798b9c1b51eSKate Stone if (!cmd_obj_sp) { 17999fe00e52SEnrico Granata result.AppendError("cannot create helper object"); 18009fe00e52SEnrico Granata result.SetStatus(eReturnStatusFailed); 18019fe00e52SEnrico Granata return false; 18029fe00e52SEnrico Granata } 18039fe00e52SEnrico Granata 1804b9c1b51eSKate Stone CommandObjectSP new_cmd(new CommandObjectScriptingObject( 1805b9c1b51eSKate Stone m_interpreter, m_cmd_name, cmd_obj_sp, m_synchronicity)); 1806b9c1b51eSKate Stone if (m_interpreter.AddUserCommand(m_cmd_name, new_cmd, true)) { 18079fe00e52SEnrico Granata result.SetStatus(eReturnStatusSuccessFinishNoResult); 1808b9c1b51eSKate Stone } else { 18099fe00e52SEnrico Granata result.AppendError("cannot add command"); 18109fe00e52SEnrico Granata result.SetStatus(eReturnStatusFailed); 18119fe00e52SEnrico Granata } 18129fe00e52SEnrico Granata } 1813223383edSEnrico Granata 1814223383edSEnrico Granata return result.Succeeded(); 1815223383edSEnrico Granata } 18165a988416SJim Ingham 18175a988416SJim Ingham CommandOptions m_options; 181844d93782SGreg Clayton std::string m_cmd_name; 1819735152e3SEnrico Granata std::string m_short_help; 182044d93782SGreg Clayton ScriptedCommandSynchronicity m_synchronicity; 1821223383edSEnrico Granata }; 1822223383edSEnrico Granata 1823223383edSEnrico Granata //------------------------------------------------------------------------- 1824223383edSEnrico Granata // CommandObjectCommandsScriptList 1825223383edSEnrico Granata //------------------------------------------------------------------------- 1826223383edSEnrico Granata 1827b9c1b51eSKate Stone class CommandObjectCommandsScriptList : public CommandObjectParsed { 1828223383edSEnrico Granata public: 1829b9c1b51eSKate Stone CommandObjectCommandsScriptList(CommandInterpreter &interpreter) 1830b9c1b51eSKate Stone : CommandObjectParsed(interpreter, "command script list", 1831b9c1b51eSKate Stone "List defined scripted commands.", nullptr) {} 1832223383edSEnrico Granata 18336e3d8e7fSEugene Zelenko ~CommandObjectCommandsScriptList() override = default; 1834223383edSEnrico Granata 1835b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override { 1836b9c1b51eSKate Stone m_interpreter.GetHelp(result, CommandInterpreter::eCommandTypesUserDef); 1837223383edSEnrico Granata 1838223383edSEnrico Granata result.SetStatus(eReturnStatusSuccessFinishResult); 1839223383edSEnrico Granata 1840223383edSEnrico Granata return true; 1841223383edSEnrico Granata } 1842223383edSEnrico Granata }; 1843223383edSEnrico Granata 1844223383edSEnrico Granata //------------------------------------------------------------------------- 1845223383edSEnrico Granata // CommandObjectCommandsScriptClear 1846223383edSEnrico Granata //------------------------------------------------------------------------- 1847223383edSEnrico Granata 1848b9c1b51eSKate Stone class CommandObjectCommandsScriptClear : public CommandObjectParsed { 1849223383edSEnrico Granata public: 1850b9c1b51eSKate Stone CommandObjectCommandsScriptClear(CommandInterpreter &interpreter) 1851b9c1b51eSKate Stone : CommandObjectParsed(interpreter, "command script clear", 1852b9c1b51eSKate Stone "Delete all scripted commands.", nullptr) {} 1853223383edSEnrico Granata 18546e3d8e7fSEugene Zelenko ~CommandObjectCommandsScriptClear() override = default; 1855223383edSEnrico Granata 18565a988416SJim Ingham protected: 1857b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override { 1858223383edSEnrico Granata m_interpreter.RemoveAllUser(); 1859223383edSEnrico Granata 1860223383edSEnrico Granata result.SetStatus(eReturnStatusSuccessFinishResult); 1861223383edSEnrico Granata 1862223383edSEnrico Granata return true; 1863223383edSEnrico Granata } 1864223383edSEnrico Granata }; 1865223383edSEnrico Granata 1866223383edSEnrico Granata //------------------------------------------------------------------------- 1867223383edSEnrico Granata // CommandObjectCommandsScriptDelete 1868223383edSEnrico Granata //------------------------------------------------------------------------- 1869223383edSEnrico Granata 1870b9c1b51eSKate Stone class CommandObjectCommandsScriptDelete : public CommandObjectParsed { 1871223383edSEnrico Granata public: 1872b9c1b51eSKate Stone CommandObjectCommandsScriptDelete(CommandInterpreter &interpreter) 1873b9c1b51eSKate Stone : CommandObjectParsed(interpreter, "command script delete", 1874b9c1b51eSKate Stone "Delete a scripted command.", nullptr) { 1875223383edSEnrico Granata CommandArgumentEntry arg1; 1876223383edSEnrico Granata CommandArgumentData cmd_arg; 1877223383edSEnrico Granata 1878223383edSEnrico Granata // Define the first (and only) variant of this arg. 1879223383edSEnrico Granata cmd_arg.arg_type = eArgTypeCommandName; 1880223383edSEnrico Granata cmd_arg.arg_repetition = eArgRepeatPlain; 1881223383edSEnrico Granata 1882b9c1b51eSKate Stone // There is only one variant this argument could be; put it into the 1883b9c1b51eSKate Stone // argument entry. 1884223383edSEnrico Granata arg1.push_back(cmd_arg); 1885223383edSEnrico Granata 1886223383edSEnrico Granata // Push the data for the first argument into the m_arguments vector. 1887223383edSEnrico Granata m_arguments.push_back(arg1); 1888223383edSEnrico Granata } 1889223383edSEnrico Granata 18906e3d8e7fSEugene Zelenko ~CommandObjectCommandsScriptDelete() override = default; 1891223383edSEnrico Granata 18925a988416SJim Ingham protected: 1893b9c1b51eSKate Stone bool DoExecute(Args &command, CommandReturnObject &result) override { 1894223383edSEnrico Granata 18955a988416SJim Ingham size_t argc = command.GetArgumentCount(); 1896223383edSEnrico Granata 1897b9c1b51eSKate Stone if (argc != 1) { 1898223383edSEnrico Granata result.AppendError("'command script delete' requires one argument"); 1899223383edSEnrico Granata result.SetStatus(eReturnStatusFailed); 1900223383edSEnrico Granata return false; 1901223383edSEnrico Granata } 1902223383edSEnrico Granata 19035a988416SJim Ingham const char *cmd_name = command.GetArgumentAtIndex(0); 1904223383edSEnrico Granata 1905b9c1b51eSKate Stone if (cmd_name && *cmd_name && m_interpreter.HasUserCommands() && 1906b9c1b51eSKate Stone m_interpreter.UserCommandExists(cmd_name)) { 1907223383edSEnrico Granata m_interpreter.RemoveUser(cmd_name); 1908223383edSEnrico Granata result.SetStatus(eReturnStatusSuccessFinishResult); 1909b9c1b51eSKate Stone } else { 1910223383edSEnrico Granata result.AppendErrorWithFormat("command %s not found", cmd_name); 1911223383edSEnrico Granata result.SetStatus(eReturnStatusFailed); 1912223383edSEnrico Granata } 1913223383edSEnrico Granata 1914223383edSEnrico Granata return result.Succeeded(); 1915223383edSEnrico Granata } 1916223383edSEnrico Granata }; 1917223383edSEnrico Granata 1918223383edSEnrico Granata #pragma mark CommandObjectMultiwordCommandsScript 1919223383edSEnrico Granata 1920223383edSEnrico Granata //------------------------------------------------------------------------- 1921223383edSEnrico Granata // CommandObjectMultiwordCommandsScript 1922223383edSEnrico Granata //------------------------------------------------------------------------- 1923223383edSEnrico Granata 1924b9c1b51eSKate Stone class CommandObjectMultiwordCommandsScript : public CommandObjectMultiword { 1925223383edSEnrico Granata public: 19267428a18cSKate Stone CommandObjectMultiwordCommandsScript(CommandInterpreter &interpreter) 1927b9c1b51eSKate Stone : CommandObjectMultiword( 1928b9c1b51eSKate Stone interpreter, "command script", "Commands for managing custom " 1929b9c1b51eSKate Stone "commands implemented by " 1930b9c1b51eSKate Stone "interpreter scripts.", 1931b9c1b51eSKate Stone "command script <subcommand> [<subcommand-options>]") { 1932b9c1b51eSKate Stone LoadSubCommand("add", CommandObjectSP( 1933b9c1b51eSKate Stone new CommandObjectCommandsScriptAdd(interpreter))); 1934b9c1b51eSKate Stone LoadSubCommand( 1935b9c1b51eSKate Stone "delete", 1936b9c1b51eSKate Stone CommandObjectSP(new CommandObjectCommandsScriptDelete(interpreter))); 1937b9c1b51eSKate Stone LoadSubCommand( 1938b9c1b51eSKate Stone "clear", 1939b9c1b51eSKate Stone CommandObjectSP(new CommandObjectCommandsScriptClear(interpreter))); 1940b9c1b51eSKate Stone LoadSubCommand("list", CommandObjectSP(new CommandObjectCommandsScriptList( 1941b9c1b51eSKate Stone interpreter))); 1942b9c1b51eSKate Stone LoadSubCommand( 1943b9c1b51eSKate Stone "import", 1944b9c1b51eSKate Stone CommandObjectSP(new CommandObjectCommandsScriptImport(interpreter))); 1945223383edSEnrico Granata } 1946223383edSEnrico Granata 19476e3d8e7fSEugene Zelenko ~CommandObjectMultiwordCommandsScript() override = default; 1948223383edSEnrico Granata }; 1949223383edSEnrico Granata 1950ebc09c36SJim Ingham #pragma mark CommandObjectMultiwordCommands 1951ebc09c36SJim Ingham 1952ebc09c36SJim Ingham //------------------------------------------------------------------------- 1953ebc09c36SJim Ingham // CommandObjectMultiwordCommands 1954ebc09c36SJim Ingham //------------------------------------------------------------------------- 1955ebc09c36SJim Ingham 1956b9c1b51eSKate Stone CommandObjectMultiwordCommands::CommandObjectMultiwordCommands( 1957b9c1b51eSKate Stone CommandInterpreter &interpreter) 1958b9c1b51eSKate Stone : CommandObjectMultiword(interpreter, "command", 1959b9c1b51eSKate Stone "Commands for managing custom LLDB commands.", 1960b9c1b51eSKate Stone "command <subcommand> [<subcommand-options>]") { 1961b9c1b51eSKate Stone LoadSubCommand("source", 1962b9c1b51eSKate Stone CommandObjectSP(new CommandObjectCommandsSource(interpreter))); 1963b9c1b51eSKate Stone LoadSubCommand("alias", 1964b9c1b51eSKate Stone CommandObjectSP(new CommandObjectCommandsAlias(interpreter))); 1965b9c1b51eSKate Stone LoadSubCommand("unalias", CommandObjectSP( 1966b9c1b51eSKate Stone new CommandObjectCommandsUnalias(interpreter))); 1967b9c1b51eSKate Stone LoadSubCommand("delete", 1968b9c1b51eSKate Stone CommandObjectSP(new CommandObjectCommandsDelete(interpreter))); 1969b9c1b51eSKate Stone LoadSubCommand( 1970b9c1b51eSKate Stone "regex", CommandObjectSP(new CommandObjectCommandsAddRegex(interpreter))); 1971b9c1b51eSKate Stone LoadSubCommand("history", CommandObjectSP( 1972b9c1b51eSKate Stone new CommandObjectCommandsHistory(interpreter))); 1973b9c1b51eSKate Stone LoadSubCommand( 1974b9c1b51eSKate Stone "script", 1975b9c1b51eSKate Stone CommandObjectSP(new CommandObjectMultiwordCommandsScript(interpreter))); 1976ebc09c36SJim Ingham } 1977ebc09c36SJim Ingham 19786e3d8e7fSEugene Zelenko CommandObjectMultiwordCommands::~CommandObjectMultiwordCommands() = default; 1979