1 //===-- CommandObject.h -----------------------------------------*- 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 #ifndef liblldb_CommandObject_h_ 11 #define liblldb_CommandObject_h_ 12 13 #include <map> 14 #include <set> 15 #include <string> 16 #include <vector> 17 18 #include "lldb/lldb-private.h" 19 #include "lldb/Interpreter/Args.h" 20 #include "lldb/Interpreter/CommandCompletions.h" 21 #include "lldb/Core/StringList.h" 22 #include "lldb/Core/Flags.h" 23 #include "lldb/Host/Mutex.h" 24 #include "lldb/Target/ExecutionContext.h" 25 26 namespace lldb_private { 27 28 class CommandObject 29 { 30 public: 31 32 typedef const char *(ArgumentHelpCallbackFunction) (); 33 34 struct ArgumentHelpCallback 35 { 36 ArgumentHelpCallbackFunction *help_callback; 37 bool self_formatting; 38 39 const char* operatorArgumentHelpCallback40 operator () () const 41 { 42 return (*help_callback)(); 43 } 44 45 explicit operator bool() const 46 { 47 return (help_callback != NULL); 48 } 49 50 }; 51 52 struct ArgumentTableEntry // Entries in the main argument information table 53 { 54 lldb::CommandArgumentType arg_type; 55 const char *arg_name; 56 CommandCompletions::CommonCompletionTypes completion_type; 57 ArgumentHelpCallback help_function; 58 const char *help_text; 59 }; 60 61 struct CommandArgumentData // Used to build individual command argument lists 62 { 63 lldb::CommandArgumentType arg_type; 64 ArgumentRepetitionType arg_repetition; 65 uint32_t arg_opt_set_association; // This arg might be associated only with some particular option set(s). CommandArgumentDataCommandArgumentData66 CommandArgumentData(): 67 arg_type(lldb::eArgTypeNone), 68 arg_repetition(eArgRepeatPlain), 69 arg_opt_set_association(LLDB_OPT_SET_ALL) // By default, the arg associates to all option sets. 70 {} 71 }; 72 73 typedef std::vector<CommandArgumentData> CommandArgumentEntry; // Used to build individual command argument lists 74 75 static ArgumentTableEntry g_arguments_data[lldb::eArgTypeLastArg]; // Main argument information table 76 77 typedef std::map<std::string, lldb::CommandObjectSP> CommandMap; 78 79 CommandObject (CommandInterpreter &interpreter, 80 const char *name, 81 const char *help = NULL, 82 const char *syntax = NULL, 83 uint32_t flags = 0); 84 85 virtual 86 ~CommandObject (); 87 88 89 static const char * 90 GetArgumentTypeAsCString (const lldb::CommandArgumentType arg_type); 91 92 static const char * 93 GetArgumentDescriptionAsCString (const lldb::CommandArgumentType arg_type); 94 95 CommandInterpreter & GetCommandInterpreter()96 GetCommandInterpreter () 97 { 98 return m_interpreter; 99 } 100 101 virtual const char * 102 GetHelp (); 103 104 virtual const char * 105 GetHelpLong (); 106 107 const char * 108 GetSyntax (); 109 110 const char * 111 GetCommandName (); 112 113 void 114 SetHelp (const char * str); 115 116 void 117 SetHelp (std::string str); 118 119 void 120 SetHelpLong (const char * str); 121 122 void 123 SetHelpLong (std::string str); 124 125 void 126 SetSyntax (const char *str); 127 128 // override this to return true if you want to enable the user to delete 129 // the Command object from the Command dictionary (aliases have their own 130 // deletion scheme, so they do not need to care about this) 131 virtual bool IsRemovable()132 IsRemovable () const { return false; } 133 134 bool IsAlias()135 IsAlias () { return m_is_alias; } 136 137 void SetIsAlias(bool value)138 SetIsAlias (bool value) { m_is_alias = value; } 139 140 virtual bool IsMultiwordObject()141 IsMultiwordObject () { return false; } 142 143 virtual lldb::CommandObjectSP 144 GetSubcommandSP (const char *sub_cmd, StringList *matches = NULL) 145 { 146 return lldb::CommandObjectSP(); 147 } 148 149 virtual CommandObject * 150 GetSubcommandObject (const char *sub_cmd, StringList *matches = NULL) 151 { 152 return NULL; 153 } 154 155 virtual void AproposAllSubCommands(const char * prefix,const char * search_word,StringList & commands_found,StringList & commands_help)156 AproposAllSubCommands (const char *prefix, 157 const char *search_word, 158 StringList &commands_found, 159 StringList &commands_help) 160 { 161 } 162 163 void 164 FormatLongHelpText (Stream &output_strm, const char *long_help); 165 166 void 167 GenerateHelpText (CommandReturnObject &result); 168 169 virtual void 170 GenerateHelpText (Stream &result); 171 172 // this is needed in order to allow the SBCommand class to 173 // transparently try and load subcommands - it will fail on 174 // anything but a multiword command, but it avoids us doing 175 // type checkings and casts 176 virtual bool LoadSubCommand(const char * cmd_name,const lldb::CommandObjectSP & command_obj)177 LoadSubCommand (const char *cmd_name, 178 const lldb::CommandObjectSP& command_obj) 179 { 180 return false; 181 } 182 183 virtual bool 184 WantsRawCommandString() = 0; 185 186 // By default, WantsCompletion = !WantsRawCommandString. 187 // Subclasses who want raw command string but desire, for example, 188 // argument completion should override this method to return true. 189 virtual bool WantsCompletion()190 WantsCompletion() { return !WantsRawCommandString(); } 191 192 virtual Options * 193 GetOptions (); 194 195 static const ArgumentTableEntry* 196 GetArgumentTable (); 197 198 static lldb::CommandArgumentType 199 LookupArgumentName (const char *arg_name); 200 201 static const ArgumentTableEntry * 202 FindArgumentDataByType (lldb::CommandArgumentType arg_type); 203 204 int 205 GetNumArgumentEntries (); 206 207 CommandArgumentEntry * 208 GetArgumentEntryAtIndex (int idx); 209 210 static void 211 GetArgumentHelp (Stream &str, lldb::CommandArgumentType arg_type, CommandInterpreter &interpreter); 212 213 static const char * 214 GetArgumentName (lldb::CommandArgumentType arg_type); 215 216 // Generates a nicely formatted command args string for help command output. 217 // By default, all possible args are taken into account, for example, 218 // '<expr | variable-name>'. This can be refined by passing a second arg 219 // specifying which option set(s) we are interested, which could then, for 220 // example, produce either '<expr>' or '<variable-name>'. 221 void 222 GetFormattedCommandArguments (Stream &str, uint32_t opt_set_mask = LLDB_OPT_SET_ALL); 223 224 bool 225 IsPairType (ArgumentRepetitionType arg_repeat_type); 226 227 bool 228 ParseOptions (Args& args, CommandReturnObject &result); 229 230 void 231 SetCommandName (const char *name); 232 233 // This function really deals with CommandObjectLists, but we didn't make a 234 // CommandObjectList class, so I'm sticking it here. But we really should have 235 // such a class. Anyway, it looks up the commands in the map that match the partial 236 // string cmd_str, inserts the matches into matches, and returns the number added. 237 238 static int 239 AddNamesMatchingPartialString (CommandMap &in_map, const char *cmd_str, StringList &matches); 240 241 //------------------------------------------------------------------ 242 /// The input array contains a parsed version of the line. The insertion 243 /// point is given by cursor_index (the index in input of the word containing 244 /// the cursor) and cursor_char_position (the position of the cursor in that word.) 245 /// This default version handles calling option argument completions and then calls 246 /// HandleArgumentCompletion if the cursor is on an argument, not an option. 247 /// Don't override this method, override HandleArgumentCompletion instead unless 248 /// you have special reasons. 249 /// 250 /// @param[in] interpreter 251 /// The command interpreter doing the completion. 252 /// 253 /// @param[in] input 254 /// The command line parsed into words 255 /// 256 /// @param[in] cursor_index 257 /// The index in \ainput of the word in which the cursor lies. 258 /// 259 /// @param[in] cursor_char_pos 260 /// The character position of the cursor in its argument word. 261 /// 262 /// @param[in] match_start_point 263 /// @param[in] match_return_elements 264 /// FIXME: Not yet implemented... If there is a match that is expensive to compute, these are 265 /// here to allow you to compute the completions in batches. Start the completion from \amatch_start_point, 266 /// and return \amatch_return_elements elements. 267 /// 268 /// @param[out] word_complete 269 /// \btrue if this is a complete option value (a space will be inserted after the 270 /// completion.) \bfalse otherwise. 271 /// 272 /// @param[out] matches 273 /// The array of matches returned. 274 /// 275 /// FIXME: This is the wrong return value, since we also need to make a distinction between 276 /// total number of matches, and the window the user wants returned. 277 /// 278 /// @return 279 /// \btrue if we were in an option, \bfalse otherwise. 280 //------------------------------------------------------------------ 281 virtual int 282 HandleCompletion (Args &input, 283 int &cursor_index, 284 int &cursor_char_position, 285 int match_start_point, 286 int max_return_elements, 287 bool &word_complete, 288 StringList &matches); 289 290 //------------------------------------------------------------------ 291 /// The input array contains a parsed version of the line. The insertion 292 /// point is given by cursor_index (the index in input of the word containing 293 /// the cursor) and cursor_char_position (the position of the cursor in that word.) 294 /// We've constructed the map of options and their arguments as well if that is 295 /// helpful for the completion. 296 /// 297 /// @param[in] interpreter 298 /// The command interpreter doing the completion. 299 /// 300 /// @param[in] input 301 /// The command line parsed into words 302 /// 303 /// @param[in] cursor_index 304 /// The index in \ainput of the word in which the cursor lies. 305 /// 306 /// @param[in] cursor_char_pos 307 /// The character position of the cursor in its argument word. 308 /// 309 /// @param[in] opt_element_vector 310 /// The results of the options parse of \a input. 311 /// 312 /// @param[in] match_start_point 313 /// @param[in] match_return_elements 314 /// See CommandObject::HandleCompletions for a description of how these work. 315 /// 316 /// @param[out] word_complete 317 /// \btrue if this is a complete option value (a space will be inserted after the 318 /// completion.) \bfalse otherwise. 319 /// 320 /// @param[out] matches 321 /// The array of matches returned. 322 /// 323 /// FIXME: This is the wrong return value, since we also need to make a distinction between 324 /// total number of matches, and the window the user wants returned. 325 /// 326 /// @return 327 /// The number of completions. 328 //------------------------------------------------------------------ 329 330 virtual int HandleArgumentCompletion(Args & input,int & cursor_index,int & cursor_char_position,OptionElementVector & opt_element_vector,int match_start_point,int max_return_elements,bool & word_complete,StringList & matches)331 HandleArgumentCompletion (Args &input, 332 int &cursor_index, 333 int &cursor_char_position, 334 OptionElementVector &opt_element_vector, 335 int match_start_point, 336 int max_return_elements, 337 bool &word_complete, 338 StringList &matches) 339 { 340 return 0; 341 } 342 343 bool 344 HelpTextContainsWord (const char *search_word); 345 346 //------------------------------------------------------------------ 347 /// The flags accessor. 348 /// 349 /// @return 350 /// A reference to the Flags member variable. 351 //------------------------------------------------------------------ 352 Flags& GetFlags()353 GetFlags() 354 { 355 return m_flags; 356 } 357 358 //------------------------------------------------------------------ 359 /// The flags const accessor. 360 /// 361 /// @return 362 /// A const reference to the Flags member variable. 363 //------------------------------------------------------------------ 364 const Flags& GetFlags()365 GetFlags() const 366 { 367 return m_flags; 368 } 369 370 //------------------------------------------------------------------ 371 /// Get the command that appropriate for a "repeat" of the current command. 372 /// 373 /// @param[in] current_command_line 374 /// The complete current command line. 375 /// 376 /// @return 377 /// NULL if there is no special repeat command - it will use the current command line. 378 /// Otherwise a pointer to the command to be repeated. 379 /// If the returned string is the empty string, the command won't be repeated. 380 //------------------------------------------------------------------ GetRepeatCommand(Args & current_command_args,uint32_t index)381 virtual const char *GetRepeatCommand (Args ¤t_command_args, uint32_t index) 382 { 383 return NULL; 384 } 385 386 bool HasOverrideCallback()387 HasOverrideCallback () const 388 { 389 return m_command_override_callback || m_deprecated_command_override_callback; 390 } 391 392 void SetOverrideCallback(lldb::CommandOverrideCallback callback,void * baton)393 SetOverrideCallback (lldb::CommandOverrideCallback callback, void *baton) 394 { 395 m_deprecated_command_override_callback = callback; 396 m_command_override_baton = baton; 397 } 398 399 void SetOverrideCallback(lldb::CommandOverrideCallbackWithResult callback,void * baton)400 SetOverrideCallback (lldb::CommandOverrideCallbackWithResult callback, void *baton) 401 { 402 m_command_override_callback = callback; 403 m_command_override_baton = baton; 404 } 405 406 bool InvokeOverrideCallback(const char ** argv,CommandReturnObject & result)407 InvokeOverrideCallback (const char **argv, CommandReturnObject &result) 408 { 409 if (m_command_override_callback) 410 return m_command_override_callback(m_command_override_baton, argv, result); 411 else if (m_deprecated_command_override_callback) 412 return m_deprecated_command_override_callback(m_command_override_baton, argv); 413 else 414 return false; 415 } 416 417 virtual bool 418 Execute (const char *args_string, CommandReturnObject &result) = 0; 419 420 protected: 421 virtual const char * GetInvalidTargetDescription()422 GetInvalidTargetDescription() 423 { 424 return "invalid target, create a target using the 'target create' command"; 425 } 426 427 virtual const char * GetInvalidProcessDescription()428 GetInvalidProcessDescription() 429 { 430 return "invalid process"; 431 } 432 433 virtual const char * GetInvalidThreadDescription()434 GetInvalidThreadDescription() 435 { 436 return "invalid thread"; 437 } 438 439 virtual const char * GetInvalidFrameDescription()440 GetInvalidFrameDescription() 441 { 442 return "invalid frame"; 443 } 444 445 virtual const char * GetInvalidRegContextDescription()446 GetInvalidRegContextDescription () 447 { 448 return "invalid frame, no registers"; 449 } 450 451 // This is for use in the command interpreter, when you either want the selected target, or if no target 452 // is present you want to prime the dummy target with entities that will be copied over to new targets. 453 Target *GetSelectedOrDummyTarget(bool prefer_dummy = false); 454 Target *GetDummyTarget(); 455 456 //------------------------------------------------------------------ 457 /// Check the command to make sure anything required by this 458 /// command is available. 459 /// 460 /// @param[out] result 461 /// A command result object, if it is not okay to run the command 462 /// this will be filled in with a suitable error. 463 /// 464 /// @return 465 /// \b true if it is okay to run this command, \b false otherwise. 466 //------------------------------------------------------------------ 467 bool 468 CheckRequirements (CommandReturnObject &result); 469 470 void 471 Cleanup (); 472 473 CommandInterpreter &m_interpreter; 474 ExecutionContext m_exe_ctx; 475 Mutex::Locker m_api_locker; 476 std::string m_cmd_name; 477 std::string m_cmd_help_short; 478 std::string m_cmd_help_long; 479 std::string m_cmd_syntax; 480 bool m_is_alias; 481 Flags m_flags; 482 std::vector<CommandArgumentEntry> m_arguments; 483 lldb::CommandOverrideCallback m_deprecated_command_override_callback; 484 lldb::CommandOverrideCallbackWithResult m_command_override_callback; 485 void * m_command_override_baton; 486 487 // Helper function to populate IDs or ID ranges as the command argument data 488 // to the specified command argument entry. 489 static void 490 AddIDsArgumentData(CommandArgumentEntry &arg, lldb::CommandArgumentType ID, lldb::CommandArgumentType IDRange); 491 492 }; 493 494 class CommandObjectParsed : public CommandObject 495 { 496 public: 497 498 CommandObjectParsed (CommandInterpreter &interpreter, 499 const char *name, 500 const char *help = NULL, 501 const char *syntax = NULL, 502 uint32_t flags = 0) : CommandObject(interpreter,name,help,syntax,flags)503 CommandObject (interpreter, name, help, syntax, flags) {} 504 505 virtual ~CommandObjectParsed()506 ~CommandObjectParsed () {}; 507 508 virtual bool 509 Execute (const char *args_string, CommandReturnObject &result); 510 511 protected: 512 virtual bool 513 DoExecute (Args& command, 514 CommandReturnObject &result) = 0; 515 516 virtual bool WantsRawCommandString()517 WantsRawCommandString() { return false; }; 518 }; 519 520 class CommandObjectRaw : public CommandObject 521 { 522 public: 523 524 CommandObjectRaw (CommandInterpreter &interpreter, 525 const char *name, 526 const char *help = NULL, 527 const char *syntax = NULL, 528 uint32_t flags = 0) : CommandObject(interpreter,name,help,syntax,flags)529 CommandObject (interpreter, name, help, syntax, flags) {} 530 531 virtual ~CommandObjectRaw()532 ~CommandObjectRaw () {}; 533 534 virtual bool 535 Execute (const char *args_string, CommandReturnObject &result); 536 537 protected: 538 virtual bool 539 DoExecute (const char *command, CommandReturnObject &result) = 0; 540 541 virtual bool WantsRawCommandString()542 WantsRawCommandString() { return true; }; 543 }; 544 545 546 } // namespace lldb_private 547 548 549 #endif // liblldb_CommandObject_h_ 550