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