Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- CommandObject.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 | |
| 10 | #include "lldb/Interpreter/CommandObject.h" |
| 11 | |
| 12 | #include <string> |
| 13 | #include <map> |
| 14 | |
| 15 | #include <getopt.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <ctype.h> |
| 18 | |
| 19 | #include "lldb/Core/Address.h" |
Jim Ingham | 84cdc15 | 2010-06-15 19:49:27 +0000 | [diff] [blame] | 20 | #include "lldb/Interpreter/Options.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 21 | |
| 22 | // These are for the Sourcename completers. |
| 23 | // FIXME: Make a separate file for the completers. |
| 24 | #include "lldb/Core/FileSpec.h" |
| 25 | #include "lldb/Core/FileSpecList.h" |
| 26 | #include "lldb/Target/Process.h" |
| 27 | #include "lldb/Target/Target.h" |
| 28 | |
| 29 | #include "lldb/Interpreter/CommandInterpreter.h" |
| 30 | #include "lldb/Interpreter/CommandReturnObject.h" |
| 31 | #include "lldb/Interpreter/ScriptInterpreter.h" |
| 32 | #include "lldb/Interpreter/ScriptInterpreterPython.h" |
| 33 | |
| 34 | using namespace lldb; |
| 35 | using namespace lldb_private; |
| 36 | |
| 37 | //------------------------------------------------------------------------- |
| 38 | // CommandObject |
| 39 | //------------------------------------------------------------------------- |
| 40 | |
| 41 | CommandObject::CommandObject (const char *name, const char *help, const char *syntax, uint32_t flags) : |
| 42 | m_cmd_name (name), |
| 43 | m_cmd_help_short (), |
| 44 | m_cmd_help_long (), |
| 45 | m_cmd_syntax (), |
Jim Ingham | d40f8a6 | 2010-07-06 22:46:59 +0000 | [diff] [blame^] | 46 | m_is_alias (false), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 47 | m_flags (flags) |
| 48 | { |
| 49 | if (help && help[0]) |
| 50 | m_cmd_help_short = help; |
| 51 | if (syntax && syntax[0]) |
| 52 | m_cmd_syntax = syntax; |
| 53 | } |
| 54 | |
| 55 | CommandObject::~CommandObject () |
| 56 | { |
| 57 | } |
| 58 | |
| 59 | const char * |
| 60 | CommandObject::GetHelp () |
| 61 | { |
| 62 | return m_cmd_help_short.c_str(); |
| 63 | } |
| 64 | |
| 65 | const char * |
| 66 | CommandObject::GetHelpLong () |
| 67 | { |
| 68 | return m_cmd_help_long.c_str(); |
| 69 | } |
| 70 | |
| 71 | const char * |
| 72 | CommandObject::GetSyntax () |
| 73 | { |
| 74 | return m_cmd_syntax.c_str(); |
| 75 | } |
| 76 | |
| 77 | const char * |
| 78 | CommandObject::Translate () |
| 79 | { |
| 80 | //return m_cmd_func_name.c_str(); |
| 81 | return "This function is currently not implemented."; |
| 82 | } |
| 83 | |
| 84 | const char * |
| 85 | CommandObject::GetCommandName () |
| 86 | { |
| 87 | return m_cmd_name.c_str(); |
| 88 | } |
| 89 | |
| 90 | void |
| 91 | CommandObject::SetCommandName (const char *name) |
| 92 | { |
| 93 | m_cmd_name = name; |
| 94 | } |
| 95 | |
| 96 | void |
| 97 | CommandObject::SetHelp (const char *cstr) |
| 98 | { |
| 99 | m_cmd_help_short = cstr; |
| 100 | } |
| 101 | |
| 102 | void |
| 103 | CommandObject::SetHelpLong (const char *cstr) |
| 104 | { |
| 105 | m_cmd_help_long = cstr; |
| 106 | } |
| 107 | |
| 108 | void |
| 109 | CommandObject::SetSyntax (const char *cstr) |
| 110 | { |
| 111 | m_cmd_syntax = cstr; |
| 112 | } |
| 113 | |
| 114 | Options * |
| 115 | CommandObject::GetOptions () |
| 116 | { |
| 117 | // By default commands don't have options unless this virtual function |
| 118 | // is overridden by base classes. |
| 119 | return NULL; |
| 120 | } |
| 121 | |
| 122 | Flags& |
| 123 | CommandObject::GetFlags() |
| 124 | { |
| 125 | return m_flags; |
| 126 | } |
| 127 | |
| 128 | const Flags& |
| 129 | CommandObject::GetFlags() const |
| 130 | { |
| 131 | return m_flags; |
| 132 | } |
| 133 | |
| 134 | bool |
| 135 | CommandObject::ExecuteCommandString |
| 136 | ( |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 137 | CommandInterpreter &interpreter, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 138 | const char *command_line, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 139 | CommandReturnObject &result |
| 140 | ) |
| 141 | { |
| 142 | Args command_args(command_line); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 143 | return ExecuteWithOptions (interpreter, command_args, result); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | bool |
| 147 | CommandObject::ParseOptions |
| 148 | ( |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 149 | CommandInterpreter &interpreter, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 150 | Args& args, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 151 | CommandReturnObject &result |
| 152 | ) |
| 153 | { |
| 154 | // See if the subclass has options? |
| 155 | Options *options = GetOptions(); |
| 156 | if (options != NULL) |
| 157 | { |
| 158 | Error error; |
| 159 | options->ResetOptionValues(); |
| 160 | |
| 161 | // ParseOptions calls getopt_long, which always skips the zero'th item in the array and starts at position 1, |
| 162 | // so we need to push a dummy value into position zero. |
| 163 | args.Unshift("dummy_string"); |
| 164 | error = args.ParseOptions (*options); |
| 165 | |
| 166 | // The "dummy_string" will have already been removed by ParseOptions, |
| 167 | // so no need to remove it. |
| 168 | |
| 169 | if (error.Fail() || !options->VerifyOptions (result)) |
| 170 | { |
| 171 | const char *error_cstr = error.AsCString(); |
| 172 | if (error_cstr) |
| 173 | { |
| 174 | // We got an error string, lets use that |
| 175 | result.GetErrorStream().PutCString(error_cstr); |
| 176 | } |
| 177 | else |
| 178 | { |
| 179 | // No error string, output the usage information into result |
| 180 | options->GenerateOptionUsage (result.GetErrorStream(), this); |
| 181 | } |
| 182 | // Set the return status to failed (this was an error). |
| 183 | result.SetStatus (eReturnStatusFailed); |
| 184 | return false; |
| 185 | } |
| 186 | } |
| 187 | return true; |
| 188 | } |
| 189 | bool |
| 190 | CommandObject::ExecuteWithOptions |
| 191 | ( |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 192 | CommandInterpreter &interpreter, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 193 | Args& args, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 194 | CommandReturnObject &result |
| 195 | ) |
| 196 | { |
| 197 | for (size_t i = 0; i < args.GetArgumentCount(); ++i) |
| 198 | { |
| 199 | const char *tmp_str = args.GetArgumentAtIndex (i); |
| 200 | if (tmp_str[0] == '`') // back-quote |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 201 | args.ReplaceArgumentAtIndex (i, interpreter.ProcessEmbeddedScriptCommands (tmp_str)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 204 | Process *process = interpreter.GetDebugger().GetExecutionContext().process; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 205 | if (process == NULL) |
| 206 | { |
| 207 | if (GetFlags().IsSet(CommandObject::eFlagProcessMustBeLaunched | CommandObject::eFlagProcessMustBePaused)) |
| 208 | { |
| 209 | result.AppendError ("Process must exist."); |
| 210 | result.SetStatus (eReturnStatusFailed); |
| 211 | return false; |
| 212 | } |
| 213 | } |
| 214 | else |
| 215 | { |
| 216 | StateType state = process->GetState(); |
| 217 | |
| 218 | switch (state) |
| 219 | { |
| 220 | |
| 221 | case eStateAttaching: |
| 222 | case eStateLaunching: |
| 223 | case eStateSuspended: |
| 224 | case eStateCrashed: |
| 225 | case eStateStopped: |
| 226 | break; |
| 227 | |
| 228 | case eStateDetached: |
| 229 | case eStateExited: |
| 230 | case eStateUnloaded: |
| 231 | if (GetFlags().IsSet(CommandObject::eFlagProcessMustBeLaunched)) |
| 232 | { |
| 233 | result.AppendError ("Process must be launched."); |
| 234 | result.SetStatus (eReturnStatusFailed); |
| 235 | return false; |
| 236 | } |
| 237 | break; |
| 238 | |
| 239 | case eStateRunning: |
| 240 | case eStateStepping: |
| 241 | if (GetFlags().IsSet(CommandObject::eFlagProcessMustBePaused)) |
| 242 | { |
| 243 | result.AppendError ("Process is running. Use 'process interrupt' to pause execution."); |
| 244 | result.SetStatus (eReturnStatusFailed); |
| 245 | return false; |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 250 | if (!ParseOptions (interpreter, args, result)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 251 | return false; |
| 252 | |
| 253 | // Call the command-specific version of 'Execute', passing it the already processed arguments. |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 254 | return Execute (interpreter, args, result); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | class CommandDictCommandPartialMatch |
| 258 | { |
| 259 | public: |
| 260 | CommandDictCommandPartialMatch (const char *match_str) |
| 261 | { |
| 262 | m_match_str = match_str; |
| 263 | } |
| 264 | bool operator() (const std::pair<std::string, lldb::CommandObjectSP> map_element) const |
| 265 | { |
| 266 | // A NULL or empty string matches everything. |
| 267 | if (m_match_str == NULL || *m_match_str == '\0') |
| 268 | return 1; |
| 269 | |
| 270 | size_t found = map_element.first.find (m_match_str, 0); |
| 271 | if (found == std::string::npos) |
| 272 | return 0; |
| 273 | else |
| 274 | return found == 0; |
| 275 | } |
| 276 | |
| 277 | private: |
| 278 | const char *m_match_str; |
| 279 | }; |
| 280 | |
| 281 | int |
| 282 | CommandObject::AddNamesMatchingPartialString (CommandObject::CommandMap &in_map, const char *cmd_str, |
| 283 | StringList &matches) |
| 284 | { |
| 285 | int number_added = 0; |
| 286 | CommandDictCommandPartialMatch matcher(cmd_str); |
| 287 | |
| 288 | CommandObject::CommandMap::iterator matching_cmds = std::find_if (in_map.begin(), in_map.end(), matcher); |
| 289 | |
| 290 | while (matching_cmds != in_map.end()) |
| 291 | { |
| 292 | ++number_added; |
| 293 | matches.AppendString((*matching_cmds).first.c_str()); |
| 294 | matching_cmds = std::find_if (++matching_cmds, in_map.end(), matcher);; |
| 295 | } |
| 296 | return number_added; |
| 297 | } |
| 298 | |
| 299 | int |
| 300 | CommandObject::HandleCompletion |
| 301 | ( |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 302 | CommandInterpreter &interpreter, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 303 | Args &input, |
| 304 | int &cursor_index, |
| 305 | int &cursor_char_position, |
| 306 | int match_start_point, |
| 307 | int max_return_elements, |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 308 | bool &word_complete, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 309 | StringList &matches |
| 310 | ) |
| 311 | { |
| 312 | if (WantsRawCommandString()) |
| 313 | { |
| 314 | // FIXME: Abstract telling the completion to insert the completion character. |
| 315 | matches.Clear(); |
| 316 | return -1; |
| 317 | } |
| 318 | else |
| 319 | { |
| 320 | // Can we do anything generic with the options? |
| 321 | Options *cur_options = GetOptions(); |
| 322 | CommandReturnObject result; |
| 323 | OptionElementVector opt_element_vector; |
| 324 | |
| 325 | if (cur_options != NULL) |
| 326 | { |
| 327 | // Re-insert the dummy command name string which will have been |
| 328 | // stripped off: |
| 329 | input.Unshift ("dummy-string"); |
| 330 | cursor_index++; |
| 331 | |
| 332 | |
| 333 | // I stick an element on the end of the input, because if the last element is |
| 334 | // option that requires an argument, getopt_long will freak out. |
| 335 | |
| 336 | input.AppendArgument ("<FAKE-VALUE>"); |
| 337 | |
Jim Ingham | adb8429 | 2010-06-24 20:31:04 +0000 | [diff] [blame] | 338 | input.ParseArgsForCompletion (*cur_options, opt_element_vector, cursor_index); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 339 | |
| 340 | input.DeleteArgumentAtIndex(input.GetArgumentCount() - 1); |
| 341 | |
| 342 | bool handled_by_options; |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 343 | handled_by_options = cur_options->HandleOptionCompletion (interpreter, |
| 344 | input, |
| 345 | opt_element_vector, |
| 346 | cursor_index, |
| 347 | cursor_char_position, |
| 348 | match_start_point, |
| 349 | max_return_elements, |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 350 | word_complete, |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 351 | matches); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 352 | if (handled_by_options) |
| 353 | return matches.GetSize(); |
| 354 | } |
| 355 | |
| 356 | // If we got here, the last word is not an option or an option argument. |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 357 | return HandleArgumentCompletion (interpreter, |
| 358 | input, |
| 359 | cursor_index, |
| 360 | cursor_char_position, |
| 361 | opt_element_vector, |
| 362 | match_start_point, |
| 363 | max_return_elements, |
Jim Ingham | 802f8b0 | 2010-06-30 05:02:46 +0000 | [diff] [blame] | 364 | word_complete, |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 365 | matches); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 369 | // Case insensitive version of ::strstr() |
| 370 | // Returns true if s2 is contained within s1. |
| 371 | |
| 372 | static bool |
| 373 | contains_string (const char *s1, const char *s2) |
| 374 | { |
| 375 | char *locase_s1 = (char *) malloc (strlen (s1) + 1); |
| 376 | char *locase_s2 = (char *) malloc (strlen (s2) + 1); |
| 377 | int i; |
| 378 | for (i = 0; s1 && s1[i] != '\0'; i++) |
| 379 | locase_s1[i] = ::tolower (s1[i]); |
| 380 | locase_s1[i] = '\0'; |
| 381 | for (i = 0; s2 && s2[i] != '\0'; i++) |
| 382 | locase_s2[i] = ::tolower (s2[i]); |
| 383 | locase_s2[i] = '\0'; |
| 384 | |
| 385 | const char *result = ::strstr (locase_s1, locase_s2); |
| 386 | free (locase_s1); |
| 387 | free (locase_s2); |
| 388 | // 'result' points into freed memory - but we're not |
| 389 | // deref'ing it so hopefully current/future compilers |
| 390 | // won't complain.. |
| 391 | |
| 392 | if (result == NULL) |
| 393 | return false; |
| 394 | else |
| 395 | return true; |
| 396 | } |
| 397 | |
| 398 | bool |
| 399 | CommandObject::HelpTextContainsWord (const char *search_word) |
| 400 | { |
| 401 | const char *short_help; |
| 402 | const char *long_help; |
| 403 | const char *syntax_help; |
| 404 | std::string options_usage_help; |
| 405 | |
| 406 | |
| 407 | bool found_word = false; |
| 408 | |
| 409 | short_help = GetHelp(); |
| 410 | long_help = GetHelpLong(); |
| 411 | syntax_help = GetSyntax(); |
| 412 | |
| 413 | if (contains_string (short_help, search_word)) |
| 414 | found_word = true; |
| 415 | else if (contains_string (long_help, search_word)) |
| 416 | found_word = true; |
| 417 | else if (contains_string (syntax_help, search_word)) |
| 418 | found_word = true; |
| 419 | |
| 420 | if (!found_word |
| 421 | && GetOptions() != NULL) |
| 422 | { |
| 423 | StreamString usage_help; |
| 424 | GetOptions()->GenerateOptionUsage (usage_help, this); |
| 425 | if (usage_help.GetSize() > 0) |
| 426 | { |
| 427 | const char *usage_text = usage_help.GetData(); |
| 428 | if (contains_string (usage_text, search_word)) |
| 429 | found_word = true; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | return found_word; |
| 434 | } |