Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 1 | //===-- CommandObjectSource.cpp ---------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 10 | // C Includes |
| 11 | // C++ Includes |
| 12 | // Other libraries and framework includes |
Greg Clayton | 0e5e5a7 | 2011-04-20 22:55:21 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/StringRef.h" |
| 14 | |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 15 | // Project includes |
Eugene Zelenko | 6e3d8e7 | 2016-02-22 23:46:47 +0000 | [diff] [blame] | 16 | #include "CommandObjectCommands.h" |
Enrico Granata | 46d4aa2 | 2016-02-29 23:22:53 +0000 | [diff] [blame] | 17 | #include "CommandObjectHelp.h" |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 18 | #include "lldb/Core/Debugger.h" |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 19 | #include "lldb/Core/IOHandler.h" |
Enrico Granata | be93a35 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 20 | #include "lldb/Core/StringList.h" |
Greg Clayton | de164aa | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 21 | #include "lldb/Interpreter/Args.h" |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame] | 22 | #include "lldb/Interpreter/CommandHistory.h" |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 23 | #include "lldb/Interpreter/CommandInterpreter.h" |
Greg Clayton | de164aa | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 24 | #include "lldb/Interpreter/CommandObjectRegexCommand.h" |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 25 | #include "lldb/Interpreter/CommandReturnObject.h" |
Enrico Granata | 012d4fc | 2013-06-11 01:26:35 +0000 | [diff] [blame] | 26 | #include "lldb/Interpreter/OptionValueBoolean.h" |
Enrico Granata | 45d0e23 | 2016-03-31 01:10:54 +0000 | [diff] [blame] | 27 | #include "lldb/Interpreter/OptionValueString.h" |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 +0000 | [diff] [blame] | 28 | #include "lldb/Interpreter/OptionValueUInt64.h" |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 29 | #include "lldb/Interpreter/Options.h" |
Enrico Granata | 99f0b8f | 2011-08-17 01:30:04 +0000 | [diff] [blame] | 30 | #include "lldb/Interpreter/ScriptInterpreter.h" |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 31 | |
| 32 | using namespace lldb; |
| 33 | using namespace lldb_private; |
| 34 | |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 35 | //------------------------------------------------------------------------- |
| 36 | // CommandObjectCommandsSource |
| 37 | //------------------------------------------------------------------------- |
| 38 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 39 | static OptionDefinition g_history_options[] = { |
| 40 | // clang-format off |
| 41 | { LLDB_OPT_SET_1, false, "count", 'c', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeUnsignedInteger, "How many history commands to print." }, |
| 42 | { 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)." }, |
| 43 | { LLDB_OPT_SET_1, false, "end-index", 'e', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeUnsignedInteger, "Index at which to stop printing history commands." }, |
| 44 | { LLDB_OPT_SET_2, false, "clear", 'C', OptionParser::eNoArgument, nullptr, nullptr, 0, eArgTypeBoolean, "Clears the current command history." }, |
| 45 | // clang-format on |
| 46 | }; |
| 47 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 48 | class CommandObjectCommandsHistory : public CommandObjectParsed { |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 49 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 50 | CommandObjectCommandsHistory(CommandInterpreter &interpreter) |
| 51 | : CommandObjectParsed(interpreter, "command history", |
Eugene Zelenko | 6e3d8e7 | 2016-02-22 23:46:47 +0000 | [diff] [blame] | 52 | "Dump the history of commands in this session.", |
| 53 | nullptr), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 54 | m_options() {} |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 55 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 56 | ~CommandObjectCommandsHistory() override = default; |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 57 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 58 | Options *GetOptions() override { return &m_options; } |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 59 | |
| 60 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 61 | class CommandOptions : public Options { |
| 62 | public: |
| 63 | CommandOptions() |
| 64 | : Options(), m_start_idx(0), m_stop_idx(0), m_count(0), m_clear(false) { |
Jim Ingham | a5a97eb | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 65 | } |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 66 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 67 | ~CommandOptions() override = default; |
| 68 | |
| 69 | Error SetOptionValue(uint32_t option_idx, const char *option_arg, |
| 70 | ExecutionContext *execution_context) override { |
| 71 | Error error; |
| 72 | const int short_option = m_getopt_table[option_idx].val; |
Zachary Turner | 8cef4b0 | 2016-09-23 17:48:13 +0000 | [diff] [blame] | 73 | llvm::StringRef option_strref = |
| 74 | llvm::StringRef::withNullAsEmpty(option_arg); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 75 | |
| 76 | switch (short_option) { |
| 77 | case 'c': |
Zachary Turner | 8cef4b0 | 2016-09-23 17:48:13 +0000 | [diff] [blame] | 78 | error = |
| 79 | m_count.SetValueFromString(option_strref, eVarSetOperationAssign); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 80 | break; |
| 81 | case 's': |
| 82 | if (option_arg && strcmp("end", option_arg) == 0) { |
| 83 | m_start_idx.SetCurrentValue(UINT64_MAX); |
| 84 | m_start_idx.SetOptionWasSet(); |
| 85 | } else |
Zachary Turner | 8cef4b0 | 2016-09-23 17:48:13 +0000 | [diff] [blame] | 86 | error = m_start_idx.SetValueFromString(option_strref, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 87 | eVarSetOperationAssign); |
| 88 | break; |
| 89 | case 'e': |
Zachary Turner | 8cef4b0 | 2016-09-23 17:48:13 +0000 | [diff] [blame] | 90 | error = m_stop_idx.SetValueFromString(option_strref, |
| 91 | eVarSetOperationAssign); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 92 | break; |
| 93 | case 'C': |
| 94 | m_clear.SetCurrentValue(true); |
| 95 | m_clear.SetOptionWasSet(); |
| 96 | break; |
| 97 | default: |
| 98 | error.SetErrorStringWithFormat("unrecognized option '%c'", |
| 99 | short_option); |
| 100 | break; |
| 101 | } |
| 102 | |
| 103 | return error; |
| 104 | } |
| 105 | |
| 106 | void OptionParsingStarting(ExecutionContext *execution_context) override { |
| 107 | m_start_idx.Clear(); |
| 108 | m_stop_idx.Clear(); |
| 109 | m_count.Clear(); |
| 110 | m_clear.Clear(); |
| 111 | } |
| 112 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 113 | llvm::ArrayRef<OptionDefinition> GetDefinitions() override { |
Zachary Turner | 7060243 | 2016-09-22 21:06:13 +0000 | [diff] [blame] | 114 | return llvm::makeArrayRef(g_history_options); |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 115 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 116 | |
| 117 | // Instance variables to hold the values for command options. |
| 118 | |
| 119 | OptionValueUInt64 m_start_idx; |
| 120 | OptionValueUInt64 m_stop_idx; |
| 121 | OptionValueUInt64 m_count; |
| 122 | OptionValueBoolean m_clear; |
| 123 | }; |
| 124 | |
| 125 | bool DoExecute(Args &command, CommandReturnObject &result) override { |
| 126 | if (m_options.m_clear.GetCurrentValue() && |
| 127 | m_options.m_clear.OptionWasSet()) { |
| 128 | m_interpreter.GetCommandHistory().Clear(); |
| 129 | result.SetStatus(lldb::eReturnStatusSuccessFinishNoResult); |
| 130 | } else { |
| 131 | if (m_options.m_start_idx.OptionWasSet() && |
| 132 | m_options.m_stop_idx.OptionWasSet() && |
| 133 | m_options.m_count.OptionWasSet()) { |
| 134 | result.AppendError("--count, --start-index and --end-index cannot be " |
| 135 | "all specified in the same invocation"); |
| 136 | result.SetStatus(lldb::eReturnStatusFailed); |
| 137 | } else { |
| 138 | std::pair<bool, uint64_t> start_idx( |
| 139 | m_options.m_start_idx.OptionWasSet(), |
| 140 | m_options.m_start_idx.GetCurrentValue()); |
| 141 | std::pair<bool, uint64_t> stop_idx( |
| 142 | m_options.m_stop_idx.OptionWasSet(), |
| 143 | m_options.m_stop_idx.GetCurrentValue()); |
| 144 | std::pair<bool, uint64_t> count(m_options.m_count.OptionWasSet(), |
| 145 | m_options.m_count.GetCurrentValue()); |
| 146 | |
| 147 | const CommandHistory &history(m_interpreter.GetCommandHistory()); |
| 148 | |
| 149 | if (start_idx.first && start_idx.second == UINT64_MAX) { |
| 150 | if (count.first) { |
| 151 | start_idx.second = history.GetSize() - count.second; |
| 152 | stop_idx.second = history.GetSize() - 1; |
| 153 | } else if (stop_idx.first) { |
| 154 | start_idx.second = stop_idx.second; |
| 155 | stop_idx.second = history.GetSize() - 1; |
| 156 | } else { |
| 157 | start_idx.second = 0; |
| 158 | stop_idx.second = history.GetSize() - 1; |
| 159 | } |
| 160 | } else { |
| 161 | if (!start_idx.first && !stop_idx.first && !count.first) { |
| 162 | start_idx.second = 0; |
| 163 | stop_idx.second = history.GetSize() - 1; |
| 164 | } else if (start_idx.first) { |
| 165 | if (count.first) { |
| 166 | stop_idx.second = start_idx.second + count.second - 1; |
| 167 | } else if (!stop_idx.first) { |
| 168 | stop_idx.second = history.GetSize() - 1; |
| 169 | } |
| 170 | } else if (stop_idx.first) { |
| 171 | if (count.first) { |
| 172 | if (stop_idx.second >= count.second) |
| 173 | start_idx.second = stop_idx.second - count.second + 1; |
| 174 | else |
| 175 | start_idx.second = 0; |
| 176 | } |
| 177 | } else /* if (count.first) */ |
| 178 | { |
| 179 | start_idx.second = 0; |
| 180 | stop_idx.second = count.second - 1; |
| 181 | } |
| 182 | } |
| 183 | history.Dump(result.GetOutputStream(), start_idx.second, |
| 184 | stop_idx.second); |
| 185 | } |
| 186 | } |
| 187 | return result.Succeeded(); |
| 188 | } |
| 189 | |
| 190 | CommandOptions m_options; |
Jim Ingham | a5a97eb | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 191 | }; |
| 192 | |
Jim Ingham | a5a97eb | 2011-07-12 03:12:18 +0000 | [diff] [blame] | 193 | //------------------------------------------------------------------------- |
| 194 | // CommandObjectCommandsSource |
| 195 | //------------------------------------------------------------------------- |
| 196 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 197 | static OptionDefinition g_source_options[] = { |
| 198 | // clang-format off |
| 199 | { LLDB_OPT_SET_ALL, false, "stop-on-error", 'e', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "If true, stop executing commands on error." }, |
| 200 | { LLDB_OPT_SET_ALL, false, "stop-on-continue", 'c', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "If true, stop executing commands on continue." }, |
| 201 | { LLDB_OPT_SET_ALL, false, "silent-run", 's', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeBoolean, "If true don't echo commands while executing." }, |
| 202 | // clang-format on |
| 203 | }; |
| 204 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 205 | class CommandObjectCommandsSource : public CommandObjectParsed { |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 206 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 207 | CommandObjectCommandsSource(CommandInterpreter &interpreter) |
| 208 | : CommandObjectParsed( |
| 209 | interpreter, "command source", |
| 210 | "Read and execute LLDB commands from the file <filename>.", |
| 211 | nullptr), |
| 212 | m_options() { |
| 213 | CommandArgumentEntry arg; |
| 214 | CommandArgumentData file_arg; |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 215 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 216 | // Define the first (and only) variant of this arg. |
| 217 | file_arg.arg_type = eArgTypeFilename; |
| 218 | file_arg.arg_repetition = eArgRepeatPlain; |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 219 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 220 | // There is only one variant this argument could be; put it into the |
| 221 | // argument entry. |
| 222 | arg.push_back(file_arg); |
Todd Fiala | e1cfbc7 | 2016-08-11 23:51:28 +0000 | [diff] [blame] | 223 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 224 | // Push the data for the first argument into the m_arguments vector. |
| 225 | m_arguments.push_back(arg); |
| 226 | } |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 227 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 228 | ~CommandObjectCommandsSource() override = default; |
| 229 | |
| 230 | const char *GetRepeatCommand(Args ¤t_command_args, |
| 231 | uint32_t index) override { |
| 232 | return ""; |
| 233 | } |
| 234 | |
| 235 | int HandleArgumentCompletion(Args &input, int &cursor_index, |
| 236 | int &cursor_char_position, |
| 237 | OptionElementVector &opt_element_vector, |
| 238 | int match_start_point, int max_return_elements, |
| 239 | bool &word_complete, |
| 240 | StringList &matches) override { |
| 241 | std::string completion_str(input.GetArgumentAtIndex(cursor_index)); |
| 242 | completion_str.erase(cursor_char_position); |
| 243 | |
| 244 | CommandCompletions::InvokeCommonCompletionCallbacks( |
| 245 | GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion, |
| 246 | completion_str.c_str(), match_start_point, max_return_elements, nullptr, |
| 247 | word_complete, matches); |
| 248 | return matches.GetSize(); |
| 249 | } |
| 250 | |
| 251 | Options *GetOptions() override { return &m_options; } |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 252 | |
| 253 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 254 | class CommandOptions : public Options { |
| 255 | public: |
| 256 | CommandOptions() |
| 257 | : Options(), m_stop_on_error(true), m_silent_run(false), |
| 258 | m_stop_on_continue(true) {} |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 259 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 260 | ~CommandOptions() override = default; |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 261 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 262 | Error SetOptionValue(uint32_t option_idx, const char *option_arg, |
| 263 | ExecutionContext *execution_context) override { |
| 264 | Error error; |
| 265 | const int short_option = m_getopt_table[option_idx].val; |
Zachary Turner | 8cef4b0 | 2016-09-23 17:48:13 +0000 | [diff] [blame] | 266 | llvm::StringRef option_strref = |
| 267 | llvm::StringRef::withNullAsEmpty(option_arg); |
Greg Clayton | 340b030 | 2014-02-05 17:57:57 +0000 | [diff] [blame] | 268 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 269 | switch (short_option) { |
| 270 | case 'e': |
Zachary Turner | 8cef4b0 | 2016-09-23 17:48:13 +0000 | [diff] [blame] | 271 | error = m_stop_on_error.SetValueFromString(option_strref); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 272 | break; |
Greg Clayton | 340b030 | 2014-02-05 17:57:57 +0000 | [diff] [blame] | 273 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 274 | case 'c': |
Zachary Turner | 8cef4b0 | 2016-09-23 17:48:13 +0000 | [diff] [blame] | 275 | error = m_stop_on_continue.SetValueFromString(option_strref); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 276 | break; |
Greg Clayton | 340b030 | 2014-02-05 17:57:57 +0000 | [diff] [blame] | 277 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 278 | case 's': |
Zachary Turner | 8cef4b0 | 2016-09-23 17:48:13 +0000 | [diff] [blame] | 279 | error = m_silent_run.SetValueFromString(option_strref); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 280 | break; |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 281 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 282 | default: |
| 283 | error.SetErrorStringWithFormat("unrecognized option '%c'", |
| 284 | short_option); |
| 285 | break; |
| 286 | } |
Jim Ingham | e16c50a | 2011-02-18 00:54:25 +0000 | [diff] [blame] | 287 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 288 | return error; |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 289 | } |
Eugene Zelenko | 6e3d8e7 | 2016-02-22 23:46:47 +0000 | [diff] [blame] | 290 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 291 | void OptionParsingStarting(ExecutionContext *execution_context) override { |
| 292 | m_stop_on_error.Clear(); |
| 293 | m_silent_run.Clear(); |
| 294 | m_stop_on_continue.Clear(); |
| 295 | } |
| 296 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 297 | llvm::ArrayRef<OptionDefinition> GetDefinitions() override { |
Zachary Turner | 7060243 | 2016-09-22 21:06:13 +0000 | [diff] [blame] | 298 | return llvm::makeArrayRef(g_source_options); |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 299 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 300 | |
| 301 | // Instance variables to hold the values for command options. |
| 302 | |
| 303 | OptionValueBoolean m_stop_on_error; |
| 304 | OptionValueBoolean m_silent_run; |
| 305 | OptionValueBoolean m_stop_on_continue; |
| 306 | }; |
| 307 | |
| 308 | bool DoExecute(Args &command, CommandReturnObject &result) override { |
Zachary Turner | 11eb9c6 | 2016-10-05 20:03:37 +0000 | [diff] [blame] | 309 | if (command.GetArgumentCount() == 1) { |
| 310 | llvm::StringRef filename = command.GetArgumentAtIndex(0); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 311 | |
| 312 | FileSpec cmd_file(filename, true); |
| 313 | ExecutionContext *exe_ctx = nullptr; // Just use the default context. |
| 314 | |
| 315 | // If any options were set, then use them |
| 316 | if (m_options.m_stop_on_error.OptionWasSet() || |
| 317 | m_options.m_silent_run.OptionWasSet() || |
| 318 | m_options.m_stop_on_continue.OptionWasSet()) { |
| 319 | // Use user set settings |
| 320 | CommandInterpreterRunOptions options; |
| 321 | options.SetStopOnContinue( |
| 322 | m_options.m_stop_on_continue.GetCurrentValue()); |
| 323 | options.SetStopOnError(m_options.m_stop_on_error.GetCurrentValue()); |
| 324 | options.SetEchoCommands(!m_options.m_silent_run.GetCurrentValue()); |
| 325 | options.SetPrintResults(!m_options.m_silent_run.GetCurrentValue()); |
| 326 | |
| 327 | m_interpreter.HandleCommandsFromFile(cmd_file, exe_ctx, options, |
| 328 | result); |
| 329 | } else { |
| 330 | // No options were set, inherit any settings from nested "command |
| 331 | // source" commands, |
| 332 | // or set to sane default settings... |
| 333 | CommandInterpreterRunOptions options; |
| 334 | m_interpreter.HandleCommandsFromFile(cmd_file, exe_ctx, options, |
| 335 | result); |
| 336 | } |
| 337 | } else { |
| 338 | result.AppendErrorWithFormat( |
| 339 | "'%s' takes exactly one executable filename argument.\n", |
Zachary Turner | a449698 | 2016-10-05 21:14:38 +0000 | [diff] [blame^] | 340 | GetCommandName().str().c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 341 | result.SetStatus(eReturnStatusFailed); |
| 342 | } |
| 343 | return result.Succeeded(); |
| 344 | } |
| 345 | |
| 346 | CommandOptions m_options; |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 347 | }; |
| 348 | |
| 349 | #pragma mark CommandObjectCommandsAlias |
| 350 | //------------------------------------------------------------------------- |
| 351 | // CommandObjectCommandsAlias |
| 352 | //------------------------------------------------------------------------- |
| 353 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 354 | static OptionDefinition g_alias_options[] = { |
| 355 | // clang-format off |
| 356 | { LLDB_OPT_SET_ALL, false, "help", 'h', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeHelpText, "Help text for this command" }, |
| 357 | { LLDB_OPT_SET_ALL, false, "long-help", 'H', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeHelpText, "Long help text for this command" }, |
| 358 | // clang-format on |
| 359 | }; |
| 360 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 361 | static const char *g_python_command_instructions = |
| 362 | "Enter your Python command(s). Type 'DONE' to end.\n" |
| 363 | "You must define a Python function with this signature:\n" |
| 364 | "def my_command_impl(debugger, args, result, internal_dict):\n"; |
Enrico Granata | be93a35 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 365 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 366 | class CommandObjectCommandsAlias : public CommandObjectRaw { |
Enrico Granata | 45d0e23 | 2016-03-31 01:10:54 +0000 | [diff] [blame] | 367 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 368 | class CommandOptions : public OptionGroup { |
| 369 | public: |
| 370 | CommandOptions() : OptionGroup(), m_help(), m_long_help() {} |
Enrico Granata | 45d0e23 | 2016-03-31 01:10:54 +0000 | [diff] [blame] | 371 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 372 | ~CommandOptions() override = default; |
| 373 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 374 | llvm::ArrayRef<OptionDefinition> GetDefinitions() override { |
Zachary Turner | 7060243 | 2016-09-22 21:06:13 +0000 | [diff] [blame] | 375 | return llvm::makeArrayRef(g_alias_options); |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 376 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 377 | |
Zachary Turner | 8cef4b0 | 2016-09-23 17:48:13 +0000 | [diff] [blame] | 378 | Error SetOptionValue(uint32_t option_idx, llvm::StringRef option_value, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 379 | ExecutionContext *execution_context) override { |
| 380 | Error error; |
| 381 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 382 | const int short_option = GetDefinitions()[option_idx].short_option; |
Zachary Turner | 8cef4b0 | 2016-09-23 17:48:13 +0000 | [diff] [blame] | 383 | std::string option_str(option_value); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 384 | |
| 385 | switch (short_option) { |
| 386 | case 'h': |
Zachary Turner | 8cef4b0 | 2016-09-23 17:48:13 +0000 | [diff] [blame] | 387 | m_help.SetCurrentValue(option_str); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 388 | m_help.SetOptionWasSet(); |
| 389 | break; |
| 390 | |
| 391 | case 'H': |
Zachary Turner | 8cef4b0 | 2016-09-23 17:48:13 +0000 | [diff] [blame] | 392 | m_long_help.SetCurrentValue(option_str); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 393 | m_long_help.SetOptionWasSet(); |
| 394 | break; |
| 395 | |
| 396 | default: |
| 397 | error.SetErrorStringWithFormat("invalid short option character '%c'", |
| 398 | short_option); |
| 399 | break; |
| 400 | } |
| 401 | |
| 402 | return error; |
Enrico Granata | 45d0e23 | 2016-03-31 01:10:54 +0000 | [diff] [blame] | 403 | } |
Zachary Turner | 8cef4b0 | 2016-09-23 17:48:13 +0000 | [diff] [blame] | 404 | Error SetOptionValue(uint32_t, const char *, ExecutionContext *) = delete; |
Enrico Granata | 45d0e23 | 2016-03-31 01:10:54 +0000 | [diff] [blame] | 405 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 406 | void OptionParsingStarting(ExecutionContext *execution_context) override { |
| 407 | m_help.Clear(); |
| 408 | m_long_help.Clear(); |
| 409 | } |
Enrico Granata | 45d0e23 | 2016-03-31 01:10:54 +0000 | [diff] [blame] | 410 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 411 | OptionValueString m_help; |
| 412 | OptionValueString m_long_help; |
| 413 | }; |
| 414 | |
| 415 | OptionGroupOptions m_option_group; |
| 416 | CommandOptions m_command_options; |
| 417 | |
| 418 | public: |
| 419 | Options *GetOptions() override { return &m_option_group; } |
| 420 | |
| 421 | CommandObjectCommandsAlias(CommandInterpreter &interpreter) |
| 422 | : CommandObjectRaw( |
| 423 | interpreter, "command alias", |
Zachary Turner | a449698 | 2016-10-05 21:14:38 +0000 | [diff] [blame^] | 424 | "Define a custom command in terms of an existing command."), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 425 | m_option_group(), m_command_options() { |
| 426 | m_option_group.Append(&m_command_options); |
| 427 | m_option_group.Finalize(); |
| 428 | |
| 429 | SetHelpLong( |
| 430 | "'alias' allows the user to create a short-cut or abbreviation for long \ |
Kate Stone | ea671fb | 2015-07-14 05:48:36 +0000 | [diff] [blame] | 431 | commands, multi-word commands, and commands that take particular options. \ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 432 | Below are some simple examples of how one might use the 'alias' command:" |
| 433 | R"( |
Kate Stone | ea671fb | 2015-07-14 05:48:36 +0000 | [diff] [blame] | 434 | |
| 435 | (lldb) command alias sc script |
| 436 | |
| 437 | Creates the abbreviation 'sc' for the 'script' command. |
| 438 | |
| 439 | (lldb) command alias bp breakpoint |
| 440 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 441 | )" |
| 442 | " Creates the abbreviation 'bp' for the 'breakpoint' command. Since \ |
Kate Stone | ea671fb | 2015-07-14 05:48:36 +0000 | [diff] [blame] | 443 | breakpoint commands are two-word commands, the user would still need to \ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 444 | enter the second word after 'bp', e.g. 'bp enable' or 'bp delete'." |
| 445 | R"( |
Kate Stone | ea671fb | 2015-07-14 05:48:36 +0000 | [diff] [blame] | 446 | |
| 447 | (lldb) command alias bpl breakpoint list |
| 448 | |
| 449 | Creates the abbreviation 'bpl' for the two-word command 'breakpoint list'. |
| 450 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 451 | )" |
| 452 | "An alias can include some options for the command, with the values either \ |
Kate Stone | ea671fb | 2015-07-14 05:48:36 +0000 | [diff] [blame] | 453 | filled in at the time the alias is created, or specified as positional \ |
| 454 | arguments, to be filled in when the alias is invoked. The following example \ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 455 | shows how to create aliases with options:" |
| 456 | R"( |
Kate Stone | ea671fb | 2015-07-14 05:48:36 +0000 | [diff] [blame] | 457 | |
| 458 | (lldb) command alias bfl breakpoint set -f %1 -l %2 |
| 459 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 460 | )" |
| 461 | " Creates the abbreviation 'bfl' (for break-file-line), with the -f and -l \ |
Kate Stone | ea671fb | 2015-07-14 05:48:36 +0000 | [diff] [blame] | 462 | options already part of the alias. So if the user wants to set a breakpoint \ |
| 463 | by file and line without explicitly having to use the -f and -l options, the \ |
| 464 | user can now use 'bfl' instead. The '%1' and '%2' are positional placeholders \ |
| 465 | for the actual arguments that will be passed when the alias command is used. \ |
| 466 | The number in the placeholder refers to the position/order the actual value \ |
| 467 | occupies when the alias is used. All the occurrences of '%1' in the alias \ |
| 468 | will be replaced with the first argument, all the occurrences of '%2' in the \ |
| 469 | alias will be replaced with the second argument, and so on. This also allows \ |
| 470 | actual arguments to be used multiple times within an alias (see 'process \ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 471 | launch' example below)." |
| 472 | R"( |
Kate Stone | ea671fb | 2015-07-14 05:48:36 +0000 | [diff] [blame] | 473 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 474 | )" |
| 475 | "Note: the positional arguments must substitute as whole words in the resultant \ |
Kate Stone | ea671fb | 2015-07-14 05:48:36 +0000 | [diff] [blame] | 476 | command, so you can't at present do something like this to append the file extension \ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 477 | \".cpp\":" |
| 478 | R"( |
Kate Stone | ea671fb | 2015-07-14 05:48:36 +0000 | [diff] [blame] | 479 | |
| 480 | (lldb) command alias bcppfl breakpoint set -f %1.cpp -l %2 |
| 481 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 482 | )" |
| 483 | "For more complex aliasing, use the \"command regex\" command instead. In the \ |
Kate Stone | ea671fb | 2015-07-14 05:48:36 +0000 | [diff] [blame] | 484 | 'bfl' case above, the actual file value will be filled in with the first argument \ |
| 485 | following 'bfl' and the actual line number value will be filled in with the second \ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 486 | argument. The user would use this alias as follows:" |
| 487 | R"( |
Kate Stone | ea671fb | 2015-07-14 05:48:36 +0000 | [diff] [blame] | 488 | |
| 489 | (lldb) command alias bfl breakpoint set -f %1 -l %2 |
| 490 | (lldb) bfl my-file.c 137 |
| 491 | |
| 492 | This would be the same as if the user had entered 'breakpoint set -f my-file.c -l 137'. |
| 493 | |
| 494 | Another example: |
| 495 | |
| 496 | (lldb) command alias pltty process launch -s -o %1 -e %1 |
| 497 | (lldb) pltty /dev/tty0 |
| 498 | |
| 499 | Interpreted as 'process launch -s -o /dev/tty0 -e /dev/tty0' |
| 500 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 501 | )" |
| 502 | "If the user always wanted to pass the same value to a particular option, the \ |
Kate Stone | ea671fb | 2015-07-14 05:48:36 +0000 | [diff] [blame] | 503 | alias could be defined with that value directly in the alias as a constant, \ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 504 | rather than using a positional placeholder:" |
| 505 | R"( |
Kate Stone | ea671fb | 2015-07-14 05:48:36 +0000 | [diff] [blame] | 506 | |
| 507 | (lldb) command alias bl3 breakpoint set -f %1 -l 3 |
| 508 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 509 | Always sets a breakpoint on line 3 of whatever file is indicated.)"); |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 510 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 511 | CommandArgumentEntry arg1; |
| 512 | CommandArgumentEntry arg2; |
| 513 | CommandArgumentEntry arg3; |
| 514 | CommandArgumentData alias_arg; |
| 515 | CommandArgumentData cmd_arg; |
| 516 | CommandArgumentData options_arg; |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 517 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 518 | // Define the first (and only) variant of this arg. |
| 519 | alias_arg.arg_type = eArgTypeAliasName; |
| 520 | alias_arg.arg_repetition = eArgRepeatPlain; |
| 521 | |
| 522 | // There is only one variant this argument could be; put it into the |
| 523 | // argument entry. |
| 524 | arg1.push_back(alias_arg); |
| 525 | |
| 526 | // Define the first (and only) variant of this arg. |
| 527 | cmd_arg.arg_type = eArgTypeCommandName; |
| 528 | cmd_arg.arg_repetition = eArgRepeatPlain; |
| 529 | |
| 530 | // There is only one variant this argument could be; put it into the |
| 531 | // argument entry. |
| 532 | arg2.push_back(cmd_arg); |
| 533 | |
| 534 | // Define the first (and only) variant of this arg. |
| 535 | options_arg.arg_type = eArgTypeAliasOptions; |
| 536 | options_arg.arg_repetition = eArgRepeatOptional; |
| 537 | |
| 538 | // There is only one variant this argument could be; put it into the |
| 539 | // argument entry. |
| 540 | arg3.push_back(options_arg); |
| 541 | |
| 542 | // Push the data for the first argument into the m_arguments vector. |
| 543 | m_arguments.push_back(arg1); |
| 544 | m_arguments.push_back(arg2); |
| 545 | m_arguments.push_back(arg3); |
| 546 | } |
| 547 | |
| 548 | ~CommandObjectCommandsAlias() override = default; |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 549 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 550 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 551 | bool DoExecute(const char *raw_command_line, |
| 552 | CommandReturnObject &result) override { |
| 553 | if (!raw_command_line || !raw_command_line[0]) { |
| 554 | result.AppendError("'command alias' requires at least two arguments"); |
| 555 | return false; |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 558 | ExecutionContext exe_ctx = GetCommandInterpreter().GetExecutionContext(); |
| 559 | m_option_group.NotifyOptionParsingStarting(&exe_ctx); |
Caroline Tice | ca90c47 | 2011-05-06 21:37:15 +0000 | [diff] [blame] | 560 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 561 | const char *remainder = nullptr; |
Enrico Granata | 212130a | 2016-03-08 05:37:15 +0000 | [diff] [blame] | 562 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 563 | if (raw_command_line[0] == '-') { |
| 564 | // We have some options and these options MUST end with --. |
| 565 | const char *end_options = nullptr; |
| 566 | const char *s = raw_command_line; |
| 567 | while (s && s[0]) { |
| 568 | end_options = ::strstr(s, "--"); |
| 569 | if (end_options) { |
| 570 | end_options += 2; // Get past the "--" |
| 571 | if (::isspace(end_options[0])) { |
| 572 | remainder = end_options; |
| 573 | while (::isspace(*remainder)) |
| 574 | ++remainder; |
| 575 | break; |
| 576 | } |
| 577 | } |
| 578 | s = end_options; |
| 579 | } |
| 580 | |
| 581 | if (end_options) { |
| 582 | Args args( |
| 583 | llvm::StringRef(raw_command_line, end_options - raw_command_line)); |
| 584 | if (!ParseOptions(args, result)) |
| 585 | return false; |
| 586 | |
| 587 | Error error(m_option_group.NotifyOptionParsingFinished(&exe_ctx)); |
| 588 | if (error.Fail()) { |
| 589 | result.AppendError(error.AsCString()); |
| 590 | result.SetStatus(eReturnStatusFailed); |
| 591 | return false; |
| 592 | } |
| 593 | } |
Caroline Tice | 844d230 | 2010-12-09 22:52:49 +0000 | [diff] [blame] | 594 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 595 | if (nullptr == remainder) |
| 596 | remainder = raw_command_line; |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 597 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 598 | std::string raw_command_string(remainder); |
| 599 | Args args(raw_command_string.c_str()); |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 600 | |
Zachary Turner | 11eb9c6 | 2016-10-05 20:03:37 +0000 | [diff] [blame] | 601 | if (args.GetArgumentCount() < 2) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 602 | result.AppendError("'command alias' requires at least two arguments"); |
| 603 | result.SetStatus(eReturnStatusFailed); |
| 604 | return false; |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 605 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 606 | |
| 607 | // Get the alias command. |
| 608 | |
Zachary Turner | 11eb9c6 | 2016-10-05 20:03:37 +0000 | [diff] [blame] | 609 | // TODO: Convert this function to use StringRef. Requires converting |
| 610 | // GetCommandObjectForCommand. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 611 | const std::string alias_command = args.GetArgumentAtIndex(0); |
| 612 | if (alias_command.size() > 1 && alias_command[0] == '-') { |
| 613 | result.AppendError("aliases starting with a dash are not supported"); |
| 614 | if (alias_command == "--help" || alias_command == "--long-help") { |
| 615 | result.AppendWarning("if trying to pass options to 'command alias' add " |
| 616 | "a -- at the end of the options"); |
| 617 | } |
| 618 | result.SetStatus(eReturnStatusFailed); |
| 619 | return false; |
| 620 | } |
| 621 | |
| 622 | // Strip the new alias name off 'raw_command_string' (leave it on args, |
| 623 | // which gets passed to 'Execute', which |
| 624 | // does the stripping itself. |
| 625 | size_t pos = raw_command_string.find(alias_command); |
| 626 | if (pos == 0) { |
| 627 | raw_command_string = raw_command_string.substr(alias_command.size()); |
| 628 | pos = raw_command_string.find_first_not_of(' '); |
| 629 | if ((pos != std::string::npos) && (pos > 0)) |
| 630 | raw_command_string = raw_command_string.substr(pos); |
| 631 | } else { |
| 632 | result.AppendError("Error parsing command string. No alias created."); |
| 633 | result.SetStatus(eReturnStatusFailed); |
| 634 | return false; |
| 635 | } |
| 636 | |
| 637 | // Verify that the command is alias-able. |
| 638 | if (m_interpreter.CommandExists(alias_command.c_str())) { |
| 639 | result.AppendErrorWithFormat( |
| 640 | "'%s' is a permanent debugger command and cannot be redefined.\n", |
| 641 | alias_command.c_str()); |
| 642 | result.SetStatus(eReturnStatusFailed); |
| 643 | return false; |
| 644 | } |
| 645 | |
| 646 | // Get CommandObject that is being aliased. The command name is read from |
| 647 | // the front of raw_command_string. |
| 648 | // raw_command_string is returned with the name of the command object |
| 649 | // stripped off the front. |
| 650 | std::string original_raw_command_string(raw_command_string); |
| 651 | CommandObject *cmd_obj = |
| 652 | m_interpreter.GetCommandObjectForCommand(raw_command_string); |
| 653 | |
| 654 | if (!cmd_obj) { |
| 655 | result.AppendErrorWithFormat("invalid command given to 'command alias'. " |
| 656 | "'%s' does not begin with a valid command." |
| 657 | " No alias created.", |
| 658 | original_raw_command_string.c_str()); |
| 659 | result.SetStatus(eReturnStatusFailed); |
| 660 | return false; |
| 661 | } else if (!cmd_obj->WantsRawCommandString()) { |
| 662 | // Note that args was initialized with the original command, and has not |
| 663 | // been updated to this point. |
| 664 | // Therefore can we pass it to the version of Execute that does not |
| 665 | // need/expect raw input in the alias. |
| 666 | return HandleAliasingNormalCommand(args, result); |
| 667 | } else { |
| 668 | return HandleAliasingRawCommand(alias_command, raw_command_string, |
| 669 | *cmd_obj, result); |
| 670 | } |
| 671 | return result.Succeeded(); |
| 672 | } |
| 673 | |
| 674 | bool HandleAliasingRawCommand(const std::string &alias_command, |
| 675 | std::string &raw_command_string, |
| 676 | CommandObject &cmd_obj, |
| 677 | CommandReturnObject &result) { |
| 678 | // Verify & handle any options/arguments passed to the alias command |
| 679 | |
| 680 | OptionArgVectorSP option_arg_vector_sp = |
| 681 | OptionArgVectorSP(new OptionArgVector); |
| 682 | |
| 683 | if (CommandObjectSP cmd_obj_sp = |
| 684 | m_interpreter.GetCommandSPExact(cmd_obj.GetCommandName(), false)) { |
| 685 | if (m_interpreter.AliasExists(alias_command.c_str()) || |
| 686 | m_interpreter.UserCommandExists(alias_command.c_str())) { |
| 687 | result.AppendWarningWithFormat( |
| 688 | "Overwriting existing definition for '%s'.\n", |
| 689 | alias_command.c_str()); |
| 690 | } |
| 691 | if (CommandAlias *alias = m_interpreter.AddAlias( |
| 692 | alias_command.c_str(), cmd_obj_sp, raw_command_string.c_str())) { |
| 693 | if (m_command_options.m_help.OptionWasSet()) |
| 694 | alias->SetHelp(m_command_options.m_help.GetCurrentValue()); |
| 695 | if (m_command_options.m_long_help.OptionWasSet()) |
| 696 | alias->SetHelpLong(m_command_options.m_long_help.GetCurrentValue()); |
| 697 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 698 | } else { |
| 699 | result.AppendError("Unable to create requested alias.\n"); |
| 700 | result.SetStatus(eReturnStatusFailed); |
| 701 | } |
| 702 | |
| 703 | } else { |
| 704 | result.AppendError("Unable to create requested alias.\n"); |
| 705 | result.SetStatus(eReturnStatusFailed); |
| 706 | } |
| 707 | |
| 708 | return result.Succeeded(); |
| 709 | } |
| 710 | |
| 711 | bool HandleAliasingNormalCommand(Args &args, CommandReturnObject &result) { |
| 712 | size_t argc = args.GetArgumentCount(); |
| 713 | |
| 714 | if (argc < 2) { |
| 715 | result.AppendError("'command alias' requires at least two arguments"); |
| 716 | result.SetStatus(eReturnStatusFailed); |
| 717 | return false; |
| 718 | } |
| 719 | |
Zachary Turner | 11eb9c6 | 2016-10-05 20:03:37 +0000 | [diff] [blame] | 720 | // TODO: Convert these to StringRefs. Should convert other dependent |
| 721 | // functions (CommandExists, UserCommandExists, AliasExists, AddAlias, |
| 722 | // etc at the same time. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 723 | const std::string alias_command = args.GetArgumentAtIndex(0); |
| 724 | const std::string actual_command = args.GetArgumentAtIndex(1); |
| 725 | |
| 726 | args.Shift(); // Shift the alias command word off the argument vector. |
| 727 | args.Shift(); // Shift the old command word off the argument vector. |
| 728 | |
| 729 | // Verify that the command is alias'able, and get the appropriate command |
| 730 | // object. |
| 731 | |
| 732 | if (m_interpreter.CommandExists(alias_command.c_str())) { |
| 733 | result.AppendErrorWithFormat( |
| 734 | "'%s' is a permanent debugger command and cannot be redefined.\n", |
| 735 | alias_command.c_str()); |
| 736 | result.SetStatus(eReturnStatusFailed); |
| 737 | } else { |
| 738 | CommandObjectSP command_obj_sp( |
Zachary Turner | a449698 | 2016-10-05 21:14:38 +0000 | [diff] [blame^] | 739 | m_interpreter.GetCommandSPExact(actual_command, true)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 740 | CommandObjectSP subcommand_obj_sp; |
| 741 | bool use_subcommand = false; |
| 742 | if (command_obj_sp) { |
| 743 | CommandObject *cmd_obj = command_obj_sp.get(); |
| 744 | CommandObject *sub_cmd_obj = nullptr; |
| 745 | OptionArgVectorSP option_arg_vector_sp = |
| 746 | OptionArgVectorSP(new OptionArgVector); |
| 747 | |
Zachary Turner | 11eb9c6 | 2016-10-05 20:03:37 +0000 | [diff] [blame] | 748 | while (cmd_obj->IsMultiwordObject() && !args.empty()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 749 | if (argc >= 3) { |
Zachary Turner | a449698 | 2016-10-05 21:14:38 +0000 | [diff] [blame^] | 750 | const std::string sub_command = args.GetArgumentAtIndex(0); |
Zachary Turner | 11eb9c6 | 2016-10-05 20:03:37 +0000 | [diff] [blame] | 751 | assert(!sub_command.empty()); |
| 752 | subcommand_obj_sp = cmd_obj->GetSubcommandSP(sub_command.data()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 753 | if (subcommand_obj_sp) { |
| 754 | sub_cmd_obj = subcommand_obj_sp.get(); |
| 755 | use_subcommand = true; |
| 756 | args.Shift(); // Shift the sub_command word off the argument |
| 757 | // vector. |
| 758 | cmd_obj = sub_cmd_obj; |
| 759 | } else { |
| 760 | result.AppendErrorWithFormat( |
| 761 | "'%s' is not a valid sub-command of '%s'. " |
| 762 | "Unable to create alias.\n", |
Zachary Turner | a449698 | 2016-10-05 21:14:38 +0000 | [diff] [blame^] | 763 | sub_command.c_str(), actual_command.c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 764 | result.SetStatus(eReturnStatusFailed); |
| 765 | return false; |
| 766 | } |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | // Verify & handle any options/arguments passed to the alias command |
| 771 | |
| 772 | std::string args_string; |
| 773 | |
Zachary Turner | 11eb9c6 | 2016-10-05 20:03:37 +0000 | [diff] [blame] | 774 | if (!args.empty()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 775 | CommandObjectSP tmp_sp = |
| 776 | m_interpreter.GetCommandSPExact(cmd_obj->GetCommandName(), false); |
| 777 | if (use_subcommand) |
| 778 | tmp_sp = m_interpreter.GetCommandSPExact( |
| 779 | sub_cmd_obj->GetCommandName(), false); |
| 780 | |
| 781 | args.GetCommandString(args_string); |
| 782 | } |
| 783 | |
| 784 | if (m_interpreter.AliasExists(alias_command.c_str()) || |
| 785 | m_interpreter.UserCommandExists(alias_command.c_str())) { |
| 786 | result.AppendWarningWithFormat( |
| 787 | "Overwriting existing definition for '%s'.\n", |
| 788 | alias_command.c_str()); |
| 789 | } |
| 790 | |
| 791 | if (CommandAlias *alias = m_interpreter.AddAlias( |
| 792 | alias_command.c_str(), |
| 793 | use_subcommand ? subcommand_obj_sp : command_obj_sp, |
| 794 | args_string.c_str())) { |
| 795 | if (m_command_options.m_help.OptionWasSet()) |
| 796 | alias->SetHelp(m_command_options.m_help.GetCurrentValue()); |
| 797 | if (m_command_options.m_long_help.OptionWasSet()) |
| 798 | alias->SetHelpLong(m_command_options.m_long_help.GetCurrentValue()); |
| 799 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 800 | } else { |
| 801 | result.AppendError("Unable to create requested alias.\n"); |
| 802 | result.SetStatus(eReturnStatusFailed); |
| 803 | return false; |
| 804 | } |
| 805 | } else { |
| 806 | result.AppendErrorWithFormat("'%s' is not an existing command.\n", |
| 807 | actual_command.c_str()); |
| 808 | result.SetStatus(eReturnStatusFailed); |
| 809 | return false; |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | return result.Succeeded(); |
| 814 | } |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 815 | }; |
| 816 | |
| 817 | #pragma mark CommandObjectCommandsUnalias |
| 818 | //------------------------------------------------------------------------- |
| 819 | // CommandObjectCommandsUnalias |
| 820 | //------------------------------------------------------------------------- |
| 821 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 822 | class CommandObjectCommandsUnalias : public CommandObjectParsed { |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 823 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 824 | CommandObjectCommandsUnalias(CommandInterpreter &interpreter) |
| 825 | : CommandObjectParsed( |
| 826 | interpreter, "command unalias", |
| 827 | "Delete one or more custom commands defined by 'command alias'.", |
| 828 | nullptr) { |
| 829 | CommandArgumentEntry arg; |
| 830 | CommandArgumentData alias_arg; |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 831 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 832 | // Define the first (and only) variant of this arg. |
| 833 | alias_arg.arg_type = eArgTypeAliasName; |
| 834 | alias_arg.arg_repetition = eArgRepeatPlain; |
| 835 | |
| 836 | // There is only one variant this argument could be; put it into the |
| 837 | // argument entry. |
| 838 | arg.push_back(alias_arg); |
| 839 | |
| 840 | // Push the data for the first argument into the m_arguments vector. |
| 841 | m_arguments.push_back(arg); |
| 842 | } |
| 843 | |
| 844 | ~CommandObjectCommandsUnalias() override = default; |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 845 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 846 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 847 | bool DoExecute(Args &args, CommandReturnObject &result) override { |
| 848 | CommandObject::CommandMap::iterator pos; |
| 849 | CommandObject *cmd_obj; |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 850 | |
Zachary Turner | 11eb9c6 | 2016-10-05 20:03:37 +0000 | [diff] [blame] | 851 | if (args.empty()) { |
| 852 | result.AppendError("must call 'unalias' with a valid alias"); |
| 853 | result.SetStatus(eReturnStatusFailed); |
| 854 | return false; |
| 855 | } |
| 856 | |
| 857 | // TODO: Convert this function to return a StringRef. Should also convert |
| 858 | // dependent functions GetCommandObject, CommandExists, RemoveAlias, |
| 859 | // AliasExists, etc. |
| 860 | const char *command_name = args.GetArgumentAtIndex(0); |
| 861 | cmd_obj = m_interpreter.GetCommandObject(command_name); |
| 862 | if (cmd_obj) { |
| 863 | if (m_interpreter.CommandExists(command_name)) { |
| 864 | if (cmd_obj->IsRemovable()) { |
| 865 | result.AppendErrorWithFormat( |
| 866 | "'%s' is not an alias, it is a debugger command which can be " |
| 867 | "removed using the 'command delete' command.\n", |
| 868 | command_name); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 869 | } else { |
Zachary Turner | 11eb9c6 | 2016-10-05 20:03:37 +0000 | [diff] [blame] | 870 | result.AppendErrorWithFormat( |
| 871 | "'%s' is a permanent debugger command and cannot be removed.\n", |
| 872 | command_name); |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 873 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 874 | result.SetStatus(eReturnStatusFailed); |
Zachary Turner | 11eb9c6 | 2016-10-05 20:03:37 +0000 | [diff] [blame] | 875 | } else { |
| 876 | if (!m_interpreter.RemoveAlias(command_name)) { |
| 877 | if (m_interpreter.AliasExists(command_name)) |
| 878 | result.AppendErrorWithFormat( |
| 879 | "Error occurred while attempting to unalias '%s'.\n", |
| 880 | command_name); |
| 881 | else |
| 882 | result.AppendErrorWithFormat("'%s' is not an existing alias.\n", |
| 883 | command_name); |
| 884 | result.SetStatus(eReturnStatusFailed); |
| 885 | } else |
| 886 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 887 | } |
| 888 | } else { |
Zachary Turner | 11eb9c6 | 2016-10-05 20:03:37 +0000 | [diff] [blame] | 889 | result.AppendErrorWithFormat( |
| 890 | "'%s' is not a known command.\nTry 'help' to see a " |
| 891 | "current list of commands.\n", |
| 892 | command_name); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 893 | result.SetStatus(eReturnStatusFailed); |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 894 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 895 | |
| 896 | return result.Succeeded(); |
| 897 | } |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 898 | }; |
| 899 | |
Greg Clayton | b547278 | 2015-01-09 19:08:20 +0000 | [diff] [blame] | 900 | #pragma mark CommandObjectCommandsDelete |
| 901 | //------------------------------------------------------------------------- |
| 902 | // CommandObjectCommandsDelete |
| 903 | //------------------------------------------------------------------------- |
| 904 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 905 | class CommandObjectCommandsDelete : public CommandObjectParsed { |
Greg Clayton | b547278 | 2015-01-09 19:08:20 +0000 | [diff] [blame] | 906 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 907 | CommandObjectCommandsDelete(CommandInterpreter &interpreter) |
| 908 | : CommandObjectParsed( |
| 909 | interpreter, "command delete", |
| 910 | "Delete one or more custom commands defined by 'command regex'.", |
| 911 | nullptr) { |
| 912 | CommandArgumentEntry arg; |
| 913 | CommandArgumentData alias_arg; |
Greg Clayton | b547278 | 2015-01-09 19:08:20 +0000 | [diff] [blame] | 914 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 915 | // Define the first (and only) variant of this arg. |
| 916 | alias_arg.arg_type = eArgTypeCommandName; |
| 917 | alias_arg.arg_repetition = eArgRepeatPlain; |
Greg Clayton | b547278 | 2015-01-09 19:08:20 +0000 | [diff] [blame] | 918 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 919 | // There is only one variant this argument could be; put it into the |
| 920 | // argument entry. |
| 921 | arg.push_back(alias_arg); |
Greg Clayton | b547278 | 2015-01-09 19:08:20 +0000 | [diff] [blame] | 922 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 923 | // Push the data for the first argument into the m_arguments vector. |
| 924 | m_arguments.push_back(arg); |
| 925 | } |
Greg Clayton | b547278 | 2015-01-09 19:08:20 +0000 | [diff] [blame] | 926 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 927 | ~CommandObjectCommandsDelete() override = default; |
Greg Clayton | b547278 | 2015-01-09 19:08:20 +0000 | [diff] [blame] | 928 | |
| 929 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 930 | bool DoExecute(Args &args, CommandReturnObject &result) override { |
| 931 | CommandObject::CommandMap::iterator pos; |
Greg Clayton | b547278 | 2015-01-09 19:08:20 +0000 | [diff] [blame] | 932 | |
Zachary Turner | 11eb9c6 | 2016-10-05 20:03:37 +0000 | [diff] [blame] | 933 | if (args.empty()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 934 | result.AppendErrorWithFormat("must call '%s' with one or more valid user " |
| 935 | "defined regular expression command names", |
Zachary Turner | a449698 | 2016-10-05 21:14:38 +0000 | [diff] [blame^] | 936 | GetCommandName().str().c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 937 | result.SetStatus(eReturnStatusFailed); |
Greg Clayton | b547278 | 2015-01-09 19:08:20 +0000 | [diff] [blame] | 938 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 939 | |
Zachary Turner | 11eb9c6 | 2016-10-05 20:03:37 +0000 | [diff] [blame] | 940 | // TODO: Convert this to accept a stringRef. |
| 941 | const char *command_name = args.GetArgumentAtIndex(0); |
| 942 | if (m_interpreter.CommandExists(command_name)) { |
| 943 | if (m_interpreter.RemoveCommand(command_name)) { |
| 944 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 945 | } else { |
| 946 | result.AppendErrorWithFormat( |
| 947 | "'%s' is a permanent debugger command and cannot be removed.\n", |
| 948 | command_name); |
| 949 | result.SetStatus(eReturnStatusFailed); |
| 950 | } |
| 951 | } else { |
| 952 | StreamString error_msg_stream; |
| 953 | const bool generate_apropos = true; |
| 954 | const bool generate_type_lookup = false; |
| 955 | CommandObjectHelp::GenerateAdditionalHelpAvenuesMessage( |
| 956 | &error_msg_stream, command_name, nullptr, nullptr, generate_apropos, |
| 957 | generate_type_lookup); |
| 958 | result.AppendErrorWithFormat("%s", error_msg_stream.GetData()); |
| 959 | result.SetStatus(eReturnStatusFailed); |
| 960 | } |
| 961 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 962 | return result.Succeeded(); |
| 963 | } |
Greg Clayton | b547278 | 2015-01-09 19:08:20 +0000 | [diff] [blame] | 964 | }; |
| 965 | |
Greg Clayton | de164aa | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 966 | //------------------------------------------------------------------------- |
| 967 | // CommandObjectCommandsAddRegex |
| 968 | //------------------------------------------------------------------------- |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 969 | |
| 970 | static OptionDefinition g_regex_options[] = { |
| 971 | // clang-format off |
| 972 | { LLDB_OPT_SET_1, false, "help" , 'h', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeNone, "The help text to display for this command." }, |
| 973 | { LLDB_OPT_SET_1, false, "syntax", 's', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeNone, "A syntax string showing the typical usage syntax." }, |
| 974 | // clang-format on |
| 975 | }; |
| 976 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 977 | #pragma mark CommandObjectCommandsAddRegex |
Greg Clayton | de164aa | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 978 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 979 | class CommandObjectCommandsAddRegex : public CommandObjectParsed, |
| 980 | public IOHandlerDelegateMultiline { |
Greg Clayton | de164aa | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 981 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 982 | CommandObjectCommandsAddRegex(CommandInterpreter &interpreter) |
| 983 | : CommandObjectParsed( |
| 984 | interpreter, "command regex", "Define a custom command in terms of " |
| 985 | "existing commands by matching " |
| 986 | "regular expressions.", |
| 987 | "command regex <cmd-name> [s/<regex>/<subst>/ ...]"), |
| 988 | IOHandlerDelegateMultiline("", |
| 989 | IOHandlerDelegate::Completion::LLDBCommand), |
| 990 | m_options() { |
| 991 | SetHelpLong( |
| 992 | R"( |
| 993 | )" |
| 994 | "This command allows the user to create powerful regular expression commands \ |
Kate Stone | ea671fb | 2015-07-14 05:48:36 +0000 | [diff] [blame] | 995 | with substitutions. The regular expressions and substitutions are specified \ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 996 | using the regular expression substitution format of:" |
| 997 | R"( |
Kate Stone | ea671fb | 2015-07-14 05:48:36 +0000 | [diff] [blame] | 998 | |
| 999 | s/<regex>/<subst>/ |
| 1000 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1001 | )" |
| 1002 | "<regex> is a regular expression that can use parenthesis to capture regular \ |
Kate Stone | ea671fb | 2015-07-14 05:48:36 +0000 | [diff] [blame] | 1003 | expression input and substitute the captured matches in the output using %1 \ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1004 | for the first match, %2 for the second, and so on." |
| 1005 | R"( |
Kate Stone | ea671fb | 2015-07-14 05:48:36 +0000 | [diff] [blame] | 1006 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1007 | )" |
| 1008 | "The regular expressions can all be specified on the command line if more than \ |
Kate Stone | ea671fb | 2015-07-14 05:48:36 +0000 | [diff] [blame] | 1009 | one argument is provided. If just the command name is provided on the command \ |
| 1010 | line, then the regular expressions and substitutions can be entered on separate \ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1011 | lines, followed by an empty line to terminate the command definition." |
| 1012 | R"( |
Kate Stone | ea671fb | 2015-07-14 05:48:36 +0000 | [diff] [blame] | 1013 | |
| 1014 | EXAMPLES |
| 1015 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1016 | )" |
| 1017 | "The following example will define a regular expression command named 'f' that \ |
Kate Stone | ea671fb | 2015-07-14 05:48:36 +0000 | [diff] [blame] | 1018 | will call 'finish' if there are no arguments, or 'frame select <frame-idx>' if \ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1019 | a number follows 'f':" |
| 1020 | R"( |
Kate Stone | ea671fb | 2015-07-14 05:48:36 +0000 | [diff] [blame] | 1021 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1022 | (lldb) command regex f s/^$/finish/ 's/([0-9]+)/frame select %1/')"); |
| 1023 | } |
Eugene Zelenko | 6e3d8e7 | 2016-02-22 23:46:47 +0000 | [diff] [blame] | 1024 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1025 | ~CommandObjectCommandsAddRegex() override = default; |
Eugene Zelenko | 6e3d8e7 | 2016-02-22 23:46:47 +0000 | [diff] [blame] | 1026 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1027 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1028 | void IOHandlerActivated(IOHandler &io_handler) override { |
| 1029 | StreamFileSP output_sp(io_handler.GetOutputStreamFile()); |
| 1030 | if (output_sp) { |
| 1031 | output_sp->PutCString("Enter one of more sed substitution commands in " |
| 1032 | "the form: 's/<regex>/<subst>/'.\nTerminate the " |
| 1033 | "substitution list with an empty line.\n"); |
| 1034 | output_sp->Flush(); |
| 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | void IOHandlerInputComplete(IOHandler &io_handler, |
| 1039 | std::string &data) override { |
| 1040 | io_handler.SetIsDone(true); |
| 1041 | if (m_regex_cmd_ap) { |
| 1042 | StringList lines; |
| 1043 | if (lines.SplitIntoLines(data)) { |
| 1044 | const size_t num_lines = lines.GetSize(); |
| 1045 | bool check_only = false; |
| 1046 | for (size_t i = 0; i < num_lines; ++i) { |
| 1047 | llvm::StringRef bytes_strref(lines[i]); |
| 1048 | Error error = AppendRegexSubstitution(bytes_strref, check_only); |
| 1049 | if (error.Fail()) { |
| 1050 | if (!m_interpreter.GetDebugger() |
| 1051 | .GetCommandInterpreter() |
| 1052 | .GetBatchCommandMode()) { |
| 1053 | StreamSP out_stream = |
| 1054 | m_interpreter.GetDebugger().GetAsyncOutputStream(); |
| 1055 | out_stream->Printf("error: %s\n", error.AsCString()); |
| 1056 | } |
| 1057 | } |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1058 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1059 | } |
| 1060 | if (m_regex_cmd_ap->HasRegexEntries()) { |
| 1061 | CommandObjectSP cmd_sp(m_regex_cmd_ap.release()); |
| 1062 | m_interpreter.AddCommand(cmd_sp->GetCommandName(), cmd_sp, true); |
| 1063 | } |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | bool DoExecute(Args &command, CommandReturnObject &result) override { |
| 1068 | const size_t argc = command.GetArgumentCount(); |
| 1069 | if (argc == 0) { |
| 1070 | result.AppendError("usage: 'command regex <command-name> " |
| 1071 | "[s/<regex1>/<subst1>/ s/<regex2>/<subst2>/ ...]'\n"); |
| 1072 | result.SetStatus(eReturnStatusFailed); |
Zachary Turner | 11eb9c6 | 2016-10-05 20:03:37 +0000 | [diff] [blame] | 1073 | return false; |
| 1074 | } |
| 1075 | |
| 1076 | Error error; |
| 1077 | const char *name = command.GetArgumentAtIndex(0); |
| 1078 | m_regex_cmd_ap.reset( |
| 1079 | new CommandObjectRegexCommand(m_interpreter, name, m_options.GetHelp(), |
| 1080 | m_options.GetSyntax(), 10, 0, true)); |
| 1081 | |
| 1082 | if (argc == 1) { |
| 1083 | Debugger &debugger = m_interpreter.GetDebugger(); |
| 1084 | bool color_prompt = debugger.GetUseColor(); |
| 1085 | const bool multiple_lines = true; // Get multiple lines |
| 1086 | IOHandlerSP io_handler_sp(new IOHandlerEditline( |
| 1087 | debugger, IOHandler::Type::Other, |
| 1088 | "lldb-regex", // Name of input reader for history |
| 1089 | llvm::StringRef("> "), // Prompt |
| 1090 | llvm::StringRef(), // Continuation prompt |
| 1091 | multiple_lines, color_prompt, |
| 1092 | 0, // Don't show line numbers |
| 1093 | *this)); |
| 1094 | |
| 1095 | if (io_handler_sp) { |
| 1096 | debugger.PushIOHandler(io_handler_sp); |
| 1097 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 1098 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1099 | } else { |
Zachary Turner | 11eb9c6 | 2016-10-05 20:03:37 +0000 | [diff] [blame] | 1100 | for (size_t arg_idx = 1; arg_idx < argc; ++arg_idx) { |
| 1101 | llvm::StringRef arg_strref(command.GetArgumentAtIndex(arg_idx)); |
| 1102 | bool check_only = false; |
| 1103 | error = AppendRegexSubstitution(arg_strref, check_only); |
| 1104 | if (error.Fail()) |
| 1105 | break; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1106 | } |
Zachary Turner | 11eb9c6 | 2016-10-05 20:03:37 +0000 | [diff] [blame] | 1107 | |
| 1108 | if (error.Success()) { |
| 1109 | AddRegexCommandToInterpreter(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1110 | } |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1111 | } |
Zachary Turner | 11eb9c6 | 2016-10-05 20:03:37 +0000 | [diff] [blame] | 1112 | if (error.Fail()) { |
| 1113 | result.AppendError(error.AsCString()); |
| 1114 | result.SetStatus(eReturnStatusFailed); |
| 1115 | } |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1116 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1117 | return result.Succeeded(); |
| 1118 | } |
| 1119 | |
| 1120 | Error AppendRegexSubstitution(const llvm::StringRef ®ex_sed, |
| 1121 | bool check_only) { |
| 1122 | Error error; |
| 1123 | |
| 1124 | if (!m_regex_cmd_ap) { |
| 1125 | error.SetErrorStringWithFormat( |
| 1126 | "invalid regular expression command object for: '%.*s'", |
| 1127 | (int)regex_sed.size(), regex_sed.data()); |
| 1128 | return error; |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1129 | } |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1130 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1131 | size_t regex_sed_size = regex_sed.size(); |
Greg Clayton | 0e5e5a7 | 2011-04-20 22:55:21 +0000 | [diff] [blame] | 1132 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1133 | if (regex_sed_size <= 1) { |
| 1134 | error.SetErrorStringWithFormat( |
| 1135 | "regular expression substitution string is too short: '%.*s'", |
| 1136 | (int)regex_sed.size(), regex_sed.data()); |
| 1137 | return error; |
Greg Clayton | de164aa | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 1138 | } |
Greg Clayton | 0e5e5a7 | 2011-04-20 22:55:21 +0000 | [diff] [blame] | 1139 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1140 | if (regex_sed[0] != 's') { |
| 1141 | error.SetErrorStringWithFormat("regular expression substitution string " |
| 1142 | "doesn't start with 's': '%.*s'", |
| 1143 | (int)regex_sed.size(), regex_sed.data()); |
| 1144 | return error; |
| 1145 | } |
| 1146 | const size_t first_separator_char_pos = 1; |
| 1147 | // use the char that follows 's' as the regex separator character |
| 1148 | // so we can have "s/<regex>/<subst>/" or "s|<regex>|<subst>|" |
| 1149 | const char separator_char = regex_sed[first_separator_char_pos]; |
| 1150 | const size_t second_separator_char_pos = |
| 1151 | regex_sed.find(separator_char, first_separator_char_pos + 1); |
Greg Clayton | 0e5e5a7 | 2011-04-20 22:55:21 +0000 | [diff] [blame] | 1152 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1153 | if (second_separator_char_pos == std::string::npos) { |
| 1154 | error.SetErrorStringWithFormat( |
| 1155 | "missing second '%c' separator char after '%.*s' in '%.*s'", |
| 1156 | separator_char, |
| 1157 | (int)(regex_sed.size() - first_separator_char_pos - 1), |
| 1158 | regex_sed.data() + (first_separator_char_pos + 1), |
| 1159 | (int)regex_sed.size(), regex_sed.data()); |
| 1160 | return error; |
| 1161 | } |
Greg Clayton | 0e5e5a7 | 2011-04-20 22:55:21 +0000 | [diff] [blame] | 1162 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1163 | const size_t third_separator_char_pos = |
| 1164 | regex_sed.find(separator_char, second_separator_char_pos + 1); |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1165 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1166 | if (third_separator_char_pos == std::string::npos) { |
| 1167 | error.SetErrorStringWithFormat( |
| 1168 | "missing third '%c' separator char after '%.*s' in '%.*s'", |
| 1169 | separator_char, |
| 1170 | (int)(regex_sed.size() - second_separator_char_pos - 1), |
| 1171 | regex_sed.data() + (second_separator_char_pos + 1), |
| 1172 | (int)regex_sed.size(), regex_sed.data()); |
| 1173 | return error; |
| 1174 | } |
| 1175 | |
| 1176 | if (third_separator_char_pos != regex_sed_size - 1) { |
| 1177 | // Make sure that everything that follows the last regex |
| 1178 | // separator char |
| 1179 | if (regex_sed.find_first_not_of("\t\n\v\f\r ", |
| 1180 | third_separator_char_pos + 1) != |
| 1181 | std::string::npos) { |
| 1182 | error.SetErrorStringWithFormat( |
| 1183 | "extra data found after the '%.*s' regular expression substitution " |
| 1184 | "string: '%.*s'", |
| 1185 | (int)third_separator_char_pos + 1, regex_sed.data(), |
| 1186 | (int)(regex_sed.size() - third_separator_char_pos - 1), |
| 1187 | regex_sed.data() + (third_separator_char_pos + 1)); |
Greg Clayton | 0e5e5a7 | 2011-04-20 22:55:21 +0000 | [diff] [blame] | 1188 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1189 | } |
| 1190 | } else if (first_separator_char_pos + 1 == second_separator_char_pos) { |
| 1191 | error.SetErrorStringWithFormat( |
| 1192 | "<regex> can't be empty in 's%c<regex>%c<subst>%c' string: '%.*s'", |
| 1193 | separator_char, separator_char, separator_char, (int)regex_sed.size(), |
| 1194 | regex_sed.data()); |
| 1195 | return error; |
| 1196 | } else if (second_separator_char_pos + 1 == third_separator_char_pos) { |
| 1197 | error.SetErrorStringWithFormat( |
| 1198 | "<subst> can't be empty in 's%c<regex>%c<subst>%c' string: '%.*s'", |
| 1199 | separator_char, separator_char, separator_char, (int)regex_sed.size(), |
| 1200 | regex_sed.data()); |
| 1201 | return error; |
Greg Clayton | de164aa | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 1202 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1203 | |
| 1204 | if (!check_only) { |
| 1205 | std::string regex(regex_sed.substr(first_separator_char_pos + 1, |
| 1206 | second_separator_char_pos - |
| 1207 | first_separator_char_pos - 1)); |
| 1208 | std::string subst(regex_sed.substr(second_separator_char_pos + 1, |
| 1209 | third_separator_char_pos - |
| 1210 | second_separator_char_pos - 1)); |
| 1211 | m_regex_cmd_ap->AddRegexCommand(regex.c_str(), subst.c_str()); |
Greg Clayton | de164aa | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 1212 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1213 | return error; |
| 1214 | } |
| 1215 | |
| 1216 | void AddRegexCommandToInterpreter() { |
| 1217 | if (m_regex_cmd_ap) { |
| 1218 | if (m_regex_cmd_ap->HasRegexEntries()) { |
| 1219 | CommandObjectSP cmd_sp(m_regex_cmd_ap.release()); |
| 1220 | m_interpreter.AddCommand(cmd_sp->GetCommandName(), cmd_sp, true); |
| 1221 | } |
| 1222 | } |
| 1223 | } |
Greg Clayton | de164aa | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 1224 | |
Greg Clayton | de164aa | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 1225 | private: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1226 | std::unique_ptr<CommandObjectRegexCommand> m_regex_cmd_ap; |
Greg Clayton | de164aa | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 1227 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1228 | class CommandOptions : public Options { |
| 1229 | public: |
| 1230 | CommandOptions() : Options() {} |
Eugene Zelenko | 6e3d8e7 | 2016-02-22 23:46:47 +0000 | [diff] [blame] | 1231 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1232 | ~CommandOptions() override = default; |
Eugene Zelenko | 6e3d8e7 | 2016-02-22 23:46:47 +0000 | [diff] [blame] | 1233 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1234 | Error SetOptionValue(uint32_t option_idx, const char *option_arg, |
| 1235 | ExecutionContext *execution_context) override { |
| 1236 | Error error; |
| 1237 | const int short_option = m_getopt_table[option_idx].val; |
Eugene Zelenko | 6e3d8e7 | 2016-02-22 23:46:47 +0000 | [diff] [blame] | 1238 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1239 | switch (short_option) { |
| 1240 | case 'h': |
| 1241 | m_help.assign(option_arg); |
| 1242 | break; |
| 1243 | case 's': |
| 1244 | m_syntax.assign(option_arg); |
| 1245 | break; |
| 1246 | default: |
| 1247 | error.SetErrorStringWithFormat("unrecognized option '%c'", |
| 1248 | short_option); |
| 1249 | break; |
| 1250 | } |
Eugene Zelenko | 6e3d8e7 | 2016-02-22 23:46:47 +0000 | [diff] [blame] | 1251 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1252 | return error; |
| 1253 | } |
Eugene Zelenko | 6e3d8e7 | 2016-02-22 23:46:47 +0000 | [diff] [blame] | 1254 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1255 | void OptionParsingStarting(ExecutionContext *execution_context) override { |
| 1256 | m_help.clear(); |
| 1257 | m_syntax.clear(); |
| 1258 | } |
| 1259 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 1260 | llvm::ArrayRef<OptionDefinition> GetDefinitions() override { |
Zachary Turner | 7060243 | 2016-09-22 21:06:13 +0000 | [diff] [blame] | 1261 | return llvm::makeArrayRef(g_regex_options); |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 1262 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1263 | |
Zachary Turner | 11eb9c6 | 2016-10-05 20:03:37 +0000 | [diff] [blame] | 1264 | // TODO: Convert these functions to return StringRefs. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1265 | const char *GetHelp() { |
| 1266 | return (m_help.empty() ? nullptr : m_help.c_str()); |
| 1267 | } |
| 1268 | |
| 1269 | const char *GetSyntax() { |
| 1270 | return (m_syntax.empty() ? nullptr : m_syntax.c_str()); |
| 1271 | } |
| 1272 | |
| 1273 | protected: |
| 1274 | // Instance variables to hold the values for command options. |
| 1275 | |
| 1276 | std::string m_help; |
| 1277 | std::string m_syntax; |
| 1278 | }; |
| 1279 | |
| 1280 | Options *GetOptions() override { return &m_options; } |
| 1281 | |
| 1282 | CommandOptions m_options; |
Greg Clayton | de164aa | 2011-04-20 16:37:46 +0000 | [diff] [blame] | 1283 | }; |
| 1284 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1285 | class CommandObjectPythonFunction : public CommandObjectRaw { |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1286 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1287 | CommandObjectPythonFunction(CommandInterpreter &interpreter, std::string name, |
| 1288 | std::string funct, std::string help, |
| 1289 | ScriptedCommandSynchronicity synch) |
Zachary Turner | a449698 | 2016-10-05 21:14:38 +0000 | [diff] [blame^] | 1290 | : CommandObjectRaw(interpreter, name), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1291 | m_function_name(funct), m_synchro(synch), m_fetched_help_long(false) { |
| 1292 | if (!help.empty()) |
| 1293 | SetHelp(help.c_str()); |
| 1294 | else { |
| 1295 | StreamString stream; |
| 1296 | stream.Printf("For more information run 'help %s'", name.c_str()); |
| 1297 | SetHelp(stream.GetData()); |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1298 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1299 | } |
Eugene Zelenko | 6e3d8e7 | 2016-02-22 23:46:47 +0000 | [diff] [blame] | 1300 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1301 | ~CommandObjectPythonFunction() override = default; |
Eugene Zelenko | 6e3d8e7 | 2016-02-22 23:46:47 +0000 | [diff] [blame] | 1302 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1303 | bool IsRemovable() const override { return true; } |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1304 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1305 | const std::string &GetFunctionName() { return m_function_name; } |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1306 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1307 | ScriptedCommandSynchronicity GetSynchronicity() { return m_synchro; } |
| 1308 | |
| 1309 | const char *GetHelpLong() override { |
| 1310 | if (!m_fetched_help_long) { |
| 1311 | ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter(); |
| 1312 | if (scripter) { |
| 1313 | std::string docstring; |
| 1314 | m_fetched_help_long = scripter->GetDocumentationForItem( |
| 1315 | m_function_name.c_str(), docstring); |
| 1316 | if (!docstring.empty()) |
| 1317 | SetHelpLong(docstring.c_str()); |
| 1318 | } |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1319 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1320 | return CommandObjectRaw::GetHelpLong(); |
| 1321 | } |
| 1322 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1323 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1324 | bool DoExecute(const char *raw_command_line, |
| 1325 | CommandReturnObject &result) override { |
| 1326 | ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter(); |
| 1327 | |
| 1328 | Error error; |
| 1329 | |
| 1330 | result.SetStatus(eReturnStatusInvalid); |
| 1331 | |
| 1332 | if (!scripter || |
| 1333 | !scripter->RunScriptBasedCommand(m_function_name.c_str(), |
| 1334 | raw_command_line, m_synchro, result, |
| 1335 | error, m_exe_ctx)) { |
| 1336 | result.AppendError(error.AsCString()); |
| 1337 | result.SetStatus(eReturnStatusFailed); |
| 1338 | } else { |
| 1339 | // Don't change the status if the command already set it... |
| 1340 | if (result.GetStatus() == eReturnStatusInvalid) { |
| 1341 | if (result.GetOutputData() == nullptr || |
| 1342 | result.GetOutputData()[0] == '\0') |
| 1343 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1344 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1345 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 1346 | } |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1347 | } |
Eugene Zelenko | 6e3d8e7 | 2016-02-22 23:46:47 +0000 | [diff] [blame] | 1348 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1349 | return result.Succeeded(); |
| 1350 | } |
| 1351 | |
Eugene Zelenko | 6e3d8e7 | 2016-02-22 23:46:47 +0000 | [diff] [blame] | 1352 | private: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1353 | std::string m_function_name; |
| 1354 | ScriptedCommandSynchronicity m_synchro; |
| 1355 | bool m_fetched_help_long; |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1356 | }; |
| 1357 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1358 | class CommandObjectScriptingObject : public CommandObjectRaw { |
Enrico Granata | 9fe00e5 | 2015-03-13 02:20:41 +0000 | [diff] [blame] | 1359 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1360 | CommandObjectScriptingObject(CommandInterpreter &interpreter, |
| 1361 | std::string name, |
| 1362 | StructuredData::GenericSP cmd_obj_sp, |
| 1363 | ScriptedCommandSynchronicity synch) |
Zachary Turner | a449698 | 2016-10-05 21:14:38 +0000 | [diff] [blame^] | 1364 | : CommandObjectRaw(interpreter, name), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1365 | m_cmd_obj_sp(cmd_obj_sp), m_synchro(synch), m_fetched_help_short(false), |
| 1366 | m_fetched_help_long(false) { |
| 1367 | StreamString stream; |
| 1368 | stream.Printf("For more information run 'help %s'", name.c_str()); |
| 1369 | SetHelp(stream.GetData()); |
| 1370 | if (ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter()) |
| 1371 | GetFlags().Set(scripter->GetFlagsForCommandObject(cmd_obj_sp)); |
| 1372 | } |
Eugene Zelenko | 6e3d8e7 | 2016-02-22 23:46:47 +0000 | [diff] [blame] | 1373 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1374 | ~CommandObjectScriptingObject() override = default; |
Eugene Zelenko | 6e3d8e7 | 2016-02-22 23:46:47 +0000 | [diff] [blame] | 1375 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1376 | bool IsRemovable() const override { return true; } |
Enrico Granata | 6f79bb2 | 2015-03-13 22:22:28 +0000 | [diff] [blame] | 1377 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1378 | StructuredData::GenericSP GetImplementingObject() { return m_cmd_obj_sp; } |
| 1379 | |
| 1380 | ScriptedCommandSynchronicity GetSynchronicity() { return m_synchro; } |
| 1381 | |
| 1382 | const char *GetHelp() override { |
| 1383 | if (!m_fetched_help_short) { |
| 1384 | ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter(); |
| 1385 | if (scripter) { |
| 1386 | std::string docstring; |
| 1387 | m_fetched_help_short = |
| 1388 | scripter->GetShortHelpForCommandObject(m_cmd_obj_sp, docstring); |
| 1389 | if (!docstring.empty()) |
| 1390 | SetHelp(docstring.c_str()); |
| 1391 | } |
Enrico Granata | 6f79bb2 | 2015-03-13 22:22:28 +0000 | [diff] [blame] | 1392 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1393 | return CommandObjectRaw::GetHelp(); |
| 1394 | } |
| 1395 | |
| 1396 | const char *GetHelpLong() override { |
| 1397 | if (!m_fetched_help_long) { |
| 1398 | ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter(); |
| 1399 | if (scripter) { |
| 1400 | std::string docstring; |
| 1401 | m_fetched_help_long = |
| 1402 | scripter->GetLongHelpForCommandObject(m_cmd_obj_sp, docstring); |
| 1403 | if (!docstring.empty()) |
| 1404 | SetHelpLong(docstring.c_str()); |
| 1405 | } |
Enrico Granata | 9fe00e5 | 2015-03-13 02:20:41 +0000 | [diff] [blame] | 1406 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1407 | return CommandObjectRaw::GetHelpLong(); |
| 1408 | } |
| 1409 | |
Enrico Granata | 9fe00e5 | 2015-03-13 02:20:41 +0000 | [diff] [blame] | 1410 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1411 | bool DoExecute(const char *raw_command_line, |
| 1412 | CommandReturnObject &result) override { |
| 1413 | ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter(); |
| 1414 | |
| 1415 | Error error; |
| 1416 | |
| 1417 | result.SetStatus(eReturnStatusInvalid); |
| 1418 | |
| 1419 | if (!scripter || |
| 1420 | !scripter->RunScriptBasedCommand(m_cmd_obj_sp, raw_command_line, |
| 1421 | m_synchro, result, error, m_exe_ctx)) { |
| 1422 | result.AppendError(error.AsCString()); |
| 1423 | result.SetStatus(eReturnStatusFailed); |
| 1424 | } else { |
| 1425 | // Don't change the status if the command already set it... |
| 1426 | if (result.GetStatus() == eReturnStatusInvalid) { |
| 1427 | if (result.GetOutputData() == nullptr || |
| 1428 | result.GetOutputData()[0] == '\0') |
| 1429 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
Enrico Granata | 9fe00e5 | 2015-03-13 02:20:41 +0000 | [diff] [blame] | 1430 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1431 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 1432 | } |
Enrico Granata | 9fe00e5 | 2015-03-13 02:20:41 +0000 | [diff] [blame] | 1433 | } |
Eugene Zelenko | 6e3d8e7 | 2016-02-22 23:46:47 +0000 | [diff] [blame] | 1434 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1435 | return result.Succeeded(); |
| 1436 | } |
| 1437 | |
Eugene Zelenko | 6e3d8e7 | 2016-02-22 23:46:47 +0000 | [diff] [blame] | 1438 | private: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1439 | StructuredData::GenericSP m_cmd_obj_sp; |
| 1440 | ScriptedCommandSynchronicity m_synchro; |
| 1441 | bool m_fetched_help_short : 1; |
| 1442 | bool m_fetched_help_long : 1; |
Enrico Granata | 9fe00e5 | 2015-03-13 02:20:41 +0000 | [diff] [blame] | 1443 | }; |
| 1444 | |
Enrico Granata | a9dbf43 | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 1445 | //------------------------------------------------------------------------- |
| 1446 | // CommandObjectCommandsScriptImport |
| 1447 | //------------------------------------------------------------------------- |
| 1448 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 1449 | OptionDefinition g_script_import_options[] = { |
| 1450 | // clang-format off |
| 1451 | { 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." }, |
| 1452 | // clang-format on |
| 1453 | }; |
| 1454 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1455 | class CommandObjectCommandsScriptImport : public CommandObjectParsed { |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1456 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1457 | CommandObjectCommandsScriptImport(CommandInterpreter &interpreter) |
| 1458 | : CommandObjectParsed(interpreter, "command script import", |
| 1459 | "Import a scripting module in LLDB.", nullptr), |
| 1460 | m_options() { |
| 1461 | CommandArgumentEntry arg1; |
| 1462 | CommandArgumentData cmd_arg; |
Eugene Zelenko | 6e3d8e7 | 2016-02-22 23:46:47 +0000 | [diff] [blame] | 1463 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1464 | // Define the first (and only) variant of this arg. |
| 1465 | cmd_arg.arg_type = eArgTypeFilename; |
| 1466 | cmd_arg.arg_repetition = eArgRepeatPlus; |
Eugene Zelenko | 6e3d8e7 | 2016-02-22 23:46:47 +0000 | [diff] [blame] | 1467 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1468 | // There is only one variant this argument could be; put it into the |
| 1469 | // argument entry. |
| 1470 | arg1.push_back(cmd_arg); |
Todd Fiala | e1cfbc7 | 2016-08-11 23:51:28 +0000 | [diff] [blame] | 1471 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1472 | // Push the data for the first argument into the m_arguments vector. |
| 1473 | m_arguments.push_back(arg1); |
| 1474 | } |
| 1475 | |
| 1476 | ~CommandObjectCommandsScriptImport() override = default; |
| 1477 | |
| 1478 | int HandleArgumentCompletion(Args &input, int &cursor_index, |
| 1479 | int &cursor_char_position, |
| 1480 | OptionElementVector &opt_element_vector, |
| 1481 | int match_start_point, int max_return_elements, |
| 1482 | bool &word_complete, |
| 1483 | StringList &matches) override { |
| 1484 | std::string completion_str(input.GetArgumentAtIndex(cursor_index)); |
| 1485 | completion_str.erase(cursor_char_position); |
| 1486 | |
| 1487 | CommandCompletions::InvokeCommonCompletionCallbacks( |
| 1488 | GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion, |
| 1489 | completion_str.c_str(), match_start_point, max_return_elements, nullptr, |
| 1490 | word_complete, matches); |
| 1491 | return matches.GetSize(); |
| 1492 | } |
| 1493 | |
| 1494 | Options *GetOptions() override { return &m_options; } |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1495 | |
| 1496 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1497 | class CommandOptions : public Options { |
| 1498 | public: |
| 1499 | CommandOptions() : Options() {} |
Eugene Zelenko | 6e3d8e7 | 2016-02-22 23:46:47 +0000 | [diff] [blame] | 1500 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1501 | ~CommandOptions() override = default; |
Eugene Zelenko | 6e3d8e7 | 2016-02-22 23:46:47 +0000 | [diff] [blame] | 1502 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1503 | Error SetOptionValue(uint32_t option_idx, const char *option_arg, |
| 1504 | ExecutionContext *execution_context) override { |
| 1505 | Error error; |
| 1506 | const int short_option = m_getopt_table[option_idx].val; |
Enrico Granata | 0a305db | 2011-11-07 22:57:04 +0000 | [diff] [blame] | 1507 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1508 | switch (short_option) { |
| 1509 | case 'r': |
| 1510 | m_allow_reload = true; |
| 1511 | break; |
| 1512 | default: |
| 1513 | error.SetErrorStringWithFormat("unrecognized option '%c'", |
| 1514 | short_option); |
| 1515 | break; |
| 1516 | } |
| 1517 | |
| 1518 | return error; |
Enrico Granata | a9dbf43 | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 1519 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1520 | |
| 1521 | void OptionParsingStarting(ExecutionContext *execution_context) override { |
| 1522 | m_allow_reload = true; |
| 1523 | } |
| 1524 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 1525 | llvm::ArrayRef<OptionDefinition> GetDefinitions() override { |
Zachary Turner | 7060243 | 2016-09-22 21:06:13 +0000 | [diff] [blame] | 1526 | return llvm::makeArrayRef(g_script_import_options); |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 1527 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1528 | |
| 1529 | // Instance variables to hold the values for command options. |
| 1530 | |
| 1531 | bool m_allow_reload; |
| 1532 | }; |
| 1533 | |
| 1534 | bool DoExecute(Args &command, CommandReturnObject &result) override { |
| 1535 | if (m_interpreter.GetDebugger().GetScriptLanguage() != |
| 1536 | lldb::eScriptLanguagePython) { |
| 1537 | result.AppendError("only scripting language supported for module " |
| 1538 | "importing is currently Python"); |
| 1539 | result.SetStatus(eReturnStatusFailed); |
| 1540 | return false; |
| 1541 | } |
| 1542 | |
Zachary Turner | 11eb9c6 | 2016-10-05 20:03:37 +0000 | [diff] [blame] | 1543 | if (command.empty()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1544 | result.AppendError("command script import needs one or more arguments"); |
| 1545 | result.SetStatus(eReturnStatusFailed); |
| 1546 | return false; |
| 1547 | } |
| 1548 | |
Zachary Turner | 11eb9c6 | 2016-10-05 20:03:37 +0000 | [diff] [blame] | 1549 | for (auto &entry : command.entries()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1550 | Error error; |
| 1551 | |
| 1552 | const bool init_session = true; |
| 1553 | // FIXME: this is necessary because CommandObject::CheckRequirements() |
Zachary Turner | 11eb9c6 | 2016-10-05 20:03:37 +0000 | [diff] [blame] | 1554 | // assumes that commands won't ever be recursively invoked, but it's |
| 1555 | // actually possible to craft a Python script that does other "command |
| 1556 | // script imports" in __lldb_init_module the real fix is to have recursive |
| 1557 | // commands possible with a CommandInvocation object separate from the |
| 1558 | // CommandObject itself, so that recursive command invocations won't stomp |
| 1559 | // on each other (wrt to execution contents, options, and more) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1560 | m_exe_ctx.Clear(); |
| 1561 | if (m_interpreter.GetScriptInterpreter()->LoadScriptingModule( |
Zachary Turner | 11eb9c6 | 2016-10-05 20:03:37 +0000 | [diff] [blame] | 1562 | entry.c_str(), m_options.m_allow_reload, init_session, error)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1563 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 1564 | } else { |
| 1565 | result.AppendErrorWithFormat("module importing failed: %s", |
| 1566 | error.AsCString()); |
| 1567 | result.SetStatus(eReturnStatusFailed); |
| 1568 | } |
| 1569 | } |
| 1570 | |
| 1571 | return result.Succeeded(); |
| 1572 | } |
| 1573 | |
| 1574 | CommandOptions m_options; |
Enrico Granata | a9dbf43 | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 1575 | }; |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1576 | |
| 1577 | //------------------------------------------------------------------------- |
| 1578 | // CommandObjectCommandsScriptAdd |
| 1579 | //------------------------------------------------------------------------- |
| 1580 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 1581 | static OptionEnumValueElement g_script_synchro_type[] = { |
| 1582 | {eScriptedCommandSynchronicitySynchronous, "synchronous", |
| 1583 | "Run synchronous"}, |
| 1584 | {eScriptedCommandSynchronicityAsynchronous, "asynchronous", |
| 1585 | "Run asynchronous"}, |
| 1586 | {eScriptedCommandSynchronicityCurrentValue, "current", |
| 1587 | "Do not alter current setting"}, |
| 1588 | {0, nullptr, nullptr}}; |
| 1589 | |
| 1590 | static OptionDefinition g_script_add_options[] = { |
| 1591 | // clang-format off |
| 1592 | { LLDB_OPT_SET_1, false, "function", 'f', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypePythonFunction, "Name of the Python function to bind to this command name." }, |
| 1593 | { LLDB_OPT_SET_2, false, "class", 'c', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypePythonClass, "Name of the Python class to bind to this command name." }, |
| 1594 | { LLDB_OPT_SET_1, false, "help" , 'h', OptionParser::eRequiredArgument, nullptr, nullptr, 0, eArgTypeHelpText, "The help text to display for this command." }, |
| 1595 | { 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." }, |
| 1596 | // clang-format on |
| 1597 | }; |
| 1598 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1599 | class CommandObjectCommandsScriptAdd : public CommandObjectParsed, |
| 1600 | public IOHandlerDelegateMultiline { |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1601 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1602 | CommandObjectCommandsScriptAdd(CommandInterpreter &interpreter) |
| 1603 | : CommandObjectParsed(interpreter, "command script add", |
Eugene Zelenko | 6e3d8e7 | 2016-02-22 23:46:47 +0000 | [diff] [blame] | 1604 | "Add a scripted function as an LLDB command.", |
| 1605 | nullptr), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1606 | IOHandlerDelegateMultiline("DONE"), m_options() { |
| 1607 | CommandArgumentEntry arg1; |
| 1608 | CommandArgumentData cmd_arg; |
Eugene Zelenko | 6e3d8e7 | 2016-02-22 23:46:47 +0000 | [diff] [blame] | 1609 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1610 | // Define the first (and only) variant of this arg. |
| 1611 | cmd_arg.arg_type = eArgTypeCommandName; |
| 1612 | cmd_arg.arg_repetition = eArgRepeatPlain; |
Eugene Zelenko | 6e3d8e7 | 2016-02-22 23:46:47 +0000 | [diff] [blame] | 1613 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1614 | // There is only one variant this argument could be; put it into the |
| 1615 | // argument entry. |
| 1616 | arg1.push_back(cmd_arg); |
Eugene Zelenko | 6e3d8e7 | 2016-02-22 23:46:47 +0000 | [diff] [blame] | 1617 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1618 | // Push the data for the first argument into the m_arguments vector. |
| 1619 | m_arguments.push_back(arg1); |
| 1620 | } |
Eugene Zelenko | 6e3d8e7 | 2016-02-22 23:46:47 +0000 | [diff] [blame] | 1621 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1622 | ~CommandObjectCommandsScriptAdd() override = default; |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1623 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1624 | Options *GetOptions() override { return &m_options; } |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1625 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1626 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1627 | class CommandOptions : public Options { |
| 1628 | public: |
| 1629 | CommandOptions() |
| 1630 | : Options(), m_class_name(), m_funct_name(), m_short_help(), |
| 1631 | m_synchronicity(eScriptedCommandSynchronicitySynchronous) {} |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1632 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1633 | ~CommandOptions() override = default; |
| 1634 | |
| 1635 | Error SetOptionValue(uint32_t option_idx, const char *option_arg, |
| 1636 | ExecutionContext *execution_context) override { |
| 1637 | Error error; |
| 1638 | const int short_option = m_getopt_table[option_idx].val; |
| 1639 | |
| 1640 | switch (short_option) { |
| 1641 | case 'f': |
| 1642 | if (option_arg) |
| 1643 | m_funct_name.assign(option_arg); |
| 1644 | break; |
| 1645 | case 'c': |
| 1646 | if (option_arg) |
| 1647 | m_class_name.assign(option_arg); |
| 1648 | break; |
| 1649 | case 'h': |
| 1650 | if (option_arg) |
| 1651 | m_short_help.assign(option_arg); |
| 1652 | break; |
| 1653 | case 's': |
| 1654 | m_synchronicity = |
| 1655 | (ScriptedCommandSynchronicity)Args::StringToOptionEnum( |
Zachary Turner | 8cef4b0 | 2016-09-23 17:48:13 +0000 | [diff] [blame] | 1656 | llvm::StringRef::withNullAsEmpty(option_arg), |
| 1657 | GetDefinitions()[option_idx].enum_values, 0, error); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1658 | if (!error.Success()) |
| 1659 | error.SetErrorStringWithFormat( |
| 1660 | "unrecognized value for synchronicity '%s'", option_arg); |
| 1661 | break; |
| 1662 | default: |
| 1663 | error.SetErrorStringWithFormat("unrecognized option '%c'", |
| 1664 | short_option); |
| 1665 | break; |
| 1666 | } |
| 1667 | |
| 1668 | return error; |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1669 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1670 | |
| 1671 | void OptionParsingStarting(ExecutionContext *execution_context) override { |
| 1672 | m_class_name.clear(); |
| 1673 | m_funct_name.clear(); |
| 1674 | m_short_help.clear(); |
| 1675 | m_synchronicity = eScriptedCommandSynchronicitySynchronous; |
| 1676 | } |
| 1677 | |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 1678 | llvm::ArrayRef<OptionDefinition> GetDefinitions() override { |
Zachary Turner | 7060243 | 2016-09-22 21:06:13 +0000 | [diff] [blame] | 1679 | return llvm::makeArrayRef(g_script_add_options); |
Zachary Turner | 1f0f5b5 | 2016-09-22 20:22:55 +0000 | [diff] [blame] | 1680 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1681 | |
| 1682 | // Instance variables to hold the values for command options. |
| 1683 | |
| 1684 | std::string m_class_name; |
| 1685 | std::string m_funct_name; |
Enrico Granata | 735152e | 2014-09-15 17:52:44 +0000 | [diff] [blame] | 1686 | std::string m_short_help; |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1687 | ScriptedCommandSynchronicity m_synchronicity; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1688 | }; |
| 1689 | |
| 1690 | void IOHandlerActivated(IOHandler &io_handler) override { |
| 1691 | StreamFileSP output_sp(io_handler.GetOutputStreamFile()); |
| 1692 | if (output_sp) { |
| 1693 | output_sp->PutCString(g_python_command_instructions); |
| 1694 | output_sp->Flush(); |
| 1695 | } |
| 1696 | } |
| 1697 | |
| 1698 | void IOHandlerInputComplete(IOHandler &io_handler, |
| 1699 | std::string &data) override { |
| 1700 | StreamFileSP error_sp = io_handler.GetErrorStreamFile(); |
| 1701 | |
| 1702 | ScriptInterpreter *interpreter = m_interpreter.GetScriptInterpreter(); |
| 1703 | if (interpreter) { |
| 1704 | |
| 1705 | StringList lines; |
| 1706 | lines.SplitIntoLines(data); |
| 1707 | if (lines.GetSize() > 0) { |
| 1708 | std::string funct_name_str; |
| 1709 | if (interpreter->GenerateScriptAliasFunction(lines, funct_name_str)) { |
| 1710 | if (funct_name_str.empty()) { |
| 1711 | error_sp->Printf("error: unable to obtain a function name, didn't " |
| 1712 | "add python command.\n"); |
| 1713 | error_sp->Flush(); |
| 1714 | } else { |
| 1715 | // everything should be fine now, let's add this alias |
| 1716 | |
| 1717 | CommandObjectSP command_obj_sp(new CommandObjectPythonFunction( |
| 1718 | m_interpreter, m_cmd_name, funct_name_str.c_str(), m_short_help, |
| 1719 | m_synchronicity)); |
| 1720 | |
| 1721 | if (!m_interpreter.AddUserCommand(m_cmd_name, command_obj_sp, |
| 1722 | true)) { |
| 1723 | error_sp->Printf("error: unable to add selected command, didn't " |
| 1724 | "add python command.\n"); |
| 1725 | error_sp->Flush(); |
| 1726 | } |
| 1727 | } |
| 1728 | } else { |
| 1729 | error_sp->Printf( |
| 1730 | "error: unable to create function, didn't add python command.\n"); |
| 1731 | error_sp->Flush(); |
| 1732 | } |
| 1733 | } else { |
| 1734 | error_sp->Printf("error: empty function, didn't add python command.\n"); |
| 1735 | error_sp->Flush(); |
| 1736 | } |
| 1737 | } else { |
| 1738 | error_sp->Printf( |
| 1739 | "error: script interpreter missing, didn't add python command.\n"); |
| 1740 | error_sp->Flush(); |
| 1741 | } |
| 1742 | |
| 1743 | io_handler.SetIsDone(true); |
| 1744 | } |
| 1745 | |
| 1746 | protected: |
| 1747 | bool DoExecute(Args &command, CommandReturnObject &result) override { |
| 1748 | if (m_interpreter.GetDebugger().GetScriptLanguage() != |
| 1749 | lldb::eScriptLanguagePython) { |
| 1750 | result.AppendError("only scripting language supported for scripted " |
| 1751 | "commands is currently Python"); |
| 1752 | result.SetStatus(eReturnStatusFailed); |
| 1753 | return false; |
| 1754 | } |
| 1755 | |
Zachary Turner | 11eb9c6 | 2016-10-05 20:03:37 +0000 | [diff] [blame] | 1756 | if (command.GetArgumentCount() != 1) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1757 | result.AppendError("'command script add' requires one argument"); |
| 1758 | result.SetStatus(eReturnStatusFailed); |
| 1759 | return false; |
| 1760 | } |
| 1761 | |
| 1762 | // Store the options in case we get multi-line input |
| 1763 | m_cmd_name = command.GetArgumentAtIndex(0); |
| 1764 | m_short_help.assign(m_options.m_short_help); |
| 1765 | m_synchronicity = m_options.m_synchronicity; |
| 1766 | |
| 1767 | if (m_options.m_class_name.empty()) { |
| 1768 | if (m_options.m_funct_name.empty()) { |
| 1769 | m_interpreter.GetPythonCommandsFromIOHandler( |
| 1770 | " ", // Prompt |
| 1771 | *this, // IOHandlerDelegate |
| 1772 | true, // Run IOHandler in async mode |
| 1773 | nullptr); // Baton for the "io_handler" that will be passed back |
| 1774 | // into our IOHandlerDelegate functions |
| 1775 | } else { |
| 1776 | CommandObjectSP new_cmd(new CommandObjectPythonFunction( |
| 1777 | m_interpreter, m_cmd_name, m_options.m_funct_name, |
| 1778 | m_options.m_short_help, m_synchronicity)); |
| 1779 | if (m_interpreter.AddUserCommand(m_cmd_name, new_cmd, true)) { |
| 1780 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 1781 | } else { |
| 1782 | result.AppendError("cannot add command"); |
| 1783 | result.SetStatus(eReturnStatusFailed); |
| 1784 | } |
| 1785 | } |
| 1786 | } else { |
| 1787 | ScriptInterpreter *interpreter = |
| 1788 | GetCommandInterpreter().GetScriptInterpreter(); |
| 1789 | if (!interpreter) { |
| 1790 | result.AppendError("cannot find ScriptInterpreter"); |
| 1791 | result.SetStatus(eReturnStatusFailed); |
| 1792 | return false; |
| 1793 | } |
| 1794 | |
| 1795 | auto cmd_obj_sp = interpreter->CreateScriptCommandObject( |
| 1796 | m_options.m_class_name.c_str()); |
| 1797 | if (!cmd_obj_sp) { |
| 1798 | result.AppendError("cannot create helper object"); |
| 1799 | result.SetStatus(eReturnStatusFailed); |
| 1800 | return false; |
| 1801 | } |
| 1802 | |
| 1803 | CommandObjectSP new_cmd(new CommandObjectScriptingObject( |
| 1804 | m_interpreter, m_cmd_name, cmd_obj_sp, m_synchronicity)); |
| 1805 | if (m_interpreter.AddUserCommand(m_cmd_name, new_cmd, true)) { |
| 1806 | result.SetStatus(eReturnStatusSuccessFinishNoResult); |
| 1807 | } else { |
| 1808 | result.AppendError("cannot add command"); |
| 1809 | result.SetStatus(eReturnStatusFailed); |
| 1810 | } |
| 1811 | } |
| 1812 | |
| 1813 | return result.Succeeded(); |
| 1814 | } |
| 1815 | |
| 1816 | CommandOptions m_options; |
| 1817 | std::string m_cmd_name; |
| 1818 | std::string m_short_help; |
| 1819 | ScriptedCommandSynchronicity m_synchronicity; |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1820 | }; |
| 1821 | |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1822 | //------------------------------------------------------------------------- |
| 1823 | // CommandObjectCommandsScriptList |
| 1824 | //------------------------------------------------------------------------- |
| 1825 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1826 | class CommandObjectCommandsScriptList : public CommandObjectParsed { |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1827 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1828 | CommandObjectCommandsScriptList(CommandInterpreter &interpreter) |
| 1829 | : CommandObjectParsed(interpreter, "command script list", |
| 1830 | "List defined scripted commands.", nullptr) {} |
Eugene Zelenko | 6e3d8e7 | 2016-02-22 23:46:47 +0000 | [diff] [blame] | 1831 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1832 | ~CommandObjectCommandsScriptList() override = default; |
Eugene Zelenko | 6e3d8e7 | 2016-02-22 23:46:47 +0000 | [diff] [blame] | 1833 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1834 | bool DoExecute(Args &command, CommandReturnObject &result) override { |
| 1835 | m_interpreter.GetHelp(result, CommandInterpreter::eCommandTypesUserDef); |
| 1836 | |
| 1837 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 1838 | |
| 1839 | return true; |
| 1840 | } |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1841 | }; |
| 1842 | |
| 1843 | //------------------------------------------------------------------------- |
| 1844 | // CommandObjectCommandsScriptClear |
| 1845 | //------------------------------------------------------------------------- |
| 1846 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1847 | class CommandObjectCommandsScriptClear : public CommandObjectParsed { |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1848 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1849 | CommandObjectCommandsScriptClear(CommandInterpreter &interpreter) |
| 1850 | : CommandObjectParsed(interpreter, "command script clear", |
| 1851 | "Delete all scripted commands.", nullptr) {} |
Eugene Zelenko | 6e3d8e7 | 2016-02-22 23:46:47 +0000 | [diff] [blame] | 1852 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1853 | ~CommandObjectCommandsScriptClear() override = default; |
Eugene Zelenko | 6e3d8e7 | 2016-02-22 23:46:47 +0000 | [diff] [blame] | 1854 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1855 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1856 | bool DoExecute(Args &command, CommandReturnObject &result) override { |
| 1857 | m_interpreter.RemoveAllUser(); |
| 1858 | |
| 1859 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 1860 | |
| 1861 | return true; |
| 1862 | } |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1863 | }; |
| 1864 | |
| 1865 | //------------------------------------------------------------------------- |
| 1866 | // CommandObjectCommandsScriptDelete |
| 1867 | //------------------------------------------------------------------------- |
| 1868 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1869 | class CommandObjectCommandsScriptDelete : public CommandObjectParsed { |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1870 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1871 | CommandObjectCommandsScriptDelete(CommandInterpreter &interpreter) |
| 1872 | : CommandObjectParsed(interpreter, "command script delete", |
| 1873 | "Delete a scripted command.", nullptr) { |
| 1874 | CommandArgumentEntry arg1; |
| 1875 | CommandArgumentData cmd_arg; |
Eugene Zelenko | 6e3d8e7 | 2016-02-22 23:46:47 +0000 | [diff] [blame] | 1876 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1877 | // Define the first (and only) variant of this arg. |
| 1878 | cmd_arg.arg_type = eArgTypeCommandName; |
| 1879 | cmd_arg.arg_repetition = eArgRepeatPlain; |
| 1880 | |
| 1881 | // There is only one variant this argument could be; put it into the |
| 1882 | // argument entry. |
| 1883 | arg1.push_back(cmd_arg); |
| 1884 | |
| 1885 | // Push the data for the first argument into the m_arguments vector. |
| 1886 | m_arguments.push_back(arg1); |
| 1887 | } |
| 1888 | |
| 1889 | ~CommandObjectCommandsScriptDelete() override = default; |
Eugene Zelenko | 6e3d8e7 | 2016-02-22 23:46:47 +0000 | [diff] [blame] | 1890 | |
Jim Ingham | 5a98841 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 1891 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1892 | bool DoExecute(Args &command, CommandReturnObject &result) override { |
| 1893 | |
Zachary Turner | 11eb9c6 | 2016-10-05 20:03:37 +0000 | [diff] [blame] | 1894 | if (command.GetArgumentCount() != 1) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1895 | result.AppendError("'command script delete' requires one argument"); |
| 1896 | result.SetStatus(eReturnStatusFailed); |
| 1897 | return false; |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1898 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1899 | |
| 1900 | const char *cmd_name = command.GetArgumentAtIndex(0); |
| 1901 | |
| 1902 | if (cmd_name && *cmd_name && m_interpreter.HasUserCommands() && |
| 1903 | m_interpreter.UserCommandExists(cmd_name)) { |
| 1904 | m_interpreter.RemoveUser(cmd_name); |
| 1905 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 1906 | } else { |
| 1907 | result.AppendErrorWithFormat("command %s not found", cmd_name); |
| 1908 | result.SetStatus(eReturnStatusFailed); |
| 1909 | } |
| 1910 | |
| 1911 | return result.Succeeded(); |
| 1912 | } |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1913 | }; |
| 1914 | |
| 1915 | #pragma mark CommandObjectMultiwordCommandsScript |
| 1916 | |
| 1917 | //------------------------------------------------------------------------- |
| 1918 | // CommandObjectMultiwordCommandsScript |
| 1919 | //------------------------------------------------------------------------- |
| 1920 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1921 | class CommandObjectMultiwordCommandsScript : public CommandObjectMultiword { |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1922 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1923 | CommandObjectMultiwordCommandsScript(CommandInterpreter &interpreter) |
| 1924 | : CommandObjectMultiword( |
| 1925 | interpreter, "command script", "Commands for managing custom " |
| 1926 | "commands implemented by " |
| 1927 | "interpreter scripts.", |
| 1928 | "command script <subcommand> [<subcommand-options>]") { |
| 1929 | LoadSubCommand("add", CommandObjectSP( |
| 1930 | new CommandObjectCommandsScriptAdd(interpreter))); |
| 1931 | LoadSubCommand( |
| 1932 | "delete", |
| 1933 | CommandObjectSP(new CommandObjectCommandsScriptDelete(interpreter))); |
| 1934 | LoadSubCommand( |
| 1935 | "clear", |
| 1936 | CommandObjectSP(new CommandObjectCommandsScriptClear(interpreter))); |
| 1937 | LoadSubCommand("list", CommandObjectSP(new CommandObjectCommandsScriptList( |
| 1938 | interpreter))); |
| 1939 | LoadSubCommand( |
| 1940 | "import", |
| 1941 | CommandObjectSP(new CommandObjectCommandsScriptImport(interpreter))); |
| 1942 | } |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1943 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1944 | ~CommandObjectMultiwordCommandsScript() override = default; |
Enrico Granata | 223383e | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 1945 | }; |
| 1946 | |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 1947 | #pragma mark CommandObjectMultiwordCommands |
| 1948 | |
| 1949 | //------------------------------------------------------------------------- |
| 1950 | // CommandObjectMultiwordCommands |
| 1951 | //------------------------------------------------------------------------- |
| 1952 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1953 | CommandObjectMultiwordCommands::CommandObjectMultiwordCommands( |
| 1954 | CommandInterpreter &interpreter) |
| 1955 | : CommandObjectMultiword(interpreter, "command", |
| 1956 | "Commands for managing custom LLDB commands.", |
| 1957 | "command <subcommand> [<subcommand-options>]") { |
| 1958 | LoadSubCommand("source", |
| 1959 | CommandObjectSP(new CommandObjectCommandsSource(interpreter))); |
| 1960 | LoadSubCommand("alias", |
| 1961 | CommandObjectSP(new CommandObjectCommandsAlias(interpreter))); |
| 1962 | LoadSubCommand("unalias", CommandObjectSP( |
| 1963 | new CommandObjectCommandsUnalias(interpreter))); |
| 1964 | LoadSubCommand("delete", |
| 1965 | CommandObjectSP(new CommandObjectCommandsDelete(interpreter))); |
| 1966 | LoadSubCommand( |
| 1967 | "regex", CommandObjectSP(new CommandObjectCommandsAddRegex(interpreter))); |
| 1968 | LoadSubCommand("history", CommandObjectSP( |
| 1969 | new CommandObjectCommandsHistory(interpreter))); |
| 1970 | LoadSubCommand( |
| 1971 | "script", |
| 1972 | CommandObjectSP(new CommandObjectMultiwordCommandsScript(interpreter))); |
Jim Ingham | ebc09c3 | 2010-07-07 03:36:20 +0000 | [diff] [blame] | 1973 | } |
| 1974 | |
Eugene Zelenko | 6e3d8e7 | 2016-02-22 23:46:47 +0000 | [diff] [blame] | 1975 | CommandObjectMultiwordCommands::~CommandObjectMultiwordCommands() = default; |