blob: 494a008892be27f14eb186ef2bcb8b3604a54088 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "lldb/Interpreter/CommandObject.h"
13
14#include <string>
15#include <map>
16
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include <stdlib.h>
18#include <ctype.h>
19
20#include "lldb/Core/Address.h"
Johnny Chenca7835c2012-05-26 00:32:39 +000021#include "lldb/Core/ArchSpec.h"
Jim Ingham40af72e2010-06-15 19:49:27 +000022#include "lldb/Interpreter/Options.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023
24// These are for the Sourcename completers.
25// FIXME: Make a separate file for the completers.
Greg Clayton53239f02011-02-08 05:05:52 +000026#include "lldb/Host/FileSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027#include "lldb/Core/FileSpecList.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000028#include "lldb/DataFormatters/FormatManager.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029#include "lldb/Target/Process.h"
30#include "lldb/Target/Target.h"
31
32#include "lldb/Interpreter/CommandInterpreter.h"
33#include "lldb/Interpreter/CommandReturnObject.h"
34#include "lldb/Interpreter/ScriptInterpreter.h"
35#include "lldb/Interpreter/ScriptInterpreterPython.h"
36
37using namespace lldb;
38using namespace lldb_private;
39
40//-------------------------------------------------------------------------
41// CommandObject
42//-------------------------------------------------------------------------
43
Greg Claytona7015092010-09-18 01:14:36 +000044CommandObject::CommandObject
45(
46 CommandInterpreter &interpreter,
47 const char *name,
48 const char *help,
49 const char *syntax,
50 uint32_t flags
51) :
52 m_interpreter (interpreter),
Enrico Granata2f02fe02014-12-05 20:59:08 +000053 m_cmd_name (name ? name : ""),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000054 m_cmd_help_short (),
55 m_cmd_help_long (),
56 m_cmd_syntax (),
Jim Ingham279a6c22010-07-06 22:46:59 +000057 m_is_alias (false),
Caroline Ticee139cf22010-10-01 17:46:38 +000058 m_flags (flags),
Greg Claytona9f7b792012-02-29 04:21:24 +000059 m_arguments(),
Jim Ingham6d8873f2014-08-06 00:24:38 +000060 m_deprecated_command_override_callback (nullptr),
Ed Masted78c9572014-04-20 00:31:37 +000061 m_command_override_callback (nullptr),
62 m_command_override_baton (nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063{
64 if (help && help[0])
65 m_cmd_help_short = help;
66 if (syntax && syntax[0])
67 m_cmd_syntax = syntax;
68}
69
70CommandObject::~CommandObject ()
71{
72}
73
74const char *
75CommandObject::GetHelp ()
76{
77 return m_cmd_help_short.c_str();
78}
79
80const char *
81CommandObject::GetHelpLong ()
82{
83 return m_cmd_help_long.c_str();
84}
85
86const char *
87CommandObject::GetSyntax ()
88{
Caroline Ticee139cf22010-10-01 17:46:38 +000089 if (m_cmd_syntax.length() == 0)
90 {
91 StreamString syntax_str;
92 syntax_str.Printf ("%s", GetCommandName());
Ed Masted78c9572014-04-20 00:31:37 +000093 if (GetOptions() != nullptr)
Caroline Tice405fe672010-10-04 22:28:36 +000094 syntax_str.Printf (" <cmd-options>");
Caroline Ticee139cf22010-10-01 17:46:38 +000095 if (m_arguments.size() > 0)
96 {
97 syntax_str.Printf (" ");
Enrico Granataca5acdb2013-06-18 01:17:46 +000098 if (WantsRawCommandString() && GetOptions() && GetOptions()->NumCommandOptions())
Sean Callanana4c6ad12012-01-04 19:11:25 +000099 syntax_str.Printf("-- ");
Caroline Ticee139cf22010-10-01 17:46:38 +0000100 GetFormattedCommandArguments (syntax_str);
101 }
102 m_cmd_syntax = syntax_str.GetData ();
103 }
104
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000105 return m_cmd_syntax.c_str();
106}
107
108const char *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000109CommandObject::GetCommandName ()
110{
111 return m_cmd_name.c_str();
112}
113
114void
115CommandObject::SetCommandName (const char *name)
116{
117 m_cmd_name = name;
118}
119
120void
121CommandObject::SetHelp (const char *cstr)
122{
123 m_cmd_help_short = cstr;
124}
125
126void
Enrico Granata6f79bb22015-03-13 22:22:28 +0000127CommandObject::SetHelp (std::string str)
128{
129 m_cmd_help_short = str;
130}
131
132void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000133CommandObject::SetHelpLong (const char *cstr)
134{
135 m_cmd_help_long = cstr;
136}
137
138void
Enrico Granata99f0b8f2011-08-17 01:30:04 +0000139CommandObject::SetHelpLong (std::string str)
140{
141 m_cmd_help_long = str;
142}
143
144void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000145CommandObject::SetSyntax (const char *cstr)
146{
147 m_cmd_syntax = cstr;
148}
149
150Options *
151CommandObject::GetOptions ()
152{
153 // By default commands don't have options unless this virtual function
154 // is overridden by base classes.
Ed Masted78c9572014-04-20 00:31:37 +0000155 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000156}
157
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000158bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000159CommandObject::ParseOptions
160(
161 Args& args,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000162 CommandReturnObject &result
163)
164{
165 // See if the subclass has options?
166 Options *options = GetOptions();
Ed Masted78c9572014-04-20 00:31:37 +0000167 if (options != nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000168 {
169 Error error;
Greg Claytonf6b8b582011-04-13 00:18:08 +0000170 options->NotifyOptionParsingStarting();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000171
Greg Claytonb7ad58a2013-04-04 20:35:24 +0000172 // ParseOptions calls getopt_long_only, which always skips the zero'th item in the array and starts at position 1,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000173 // so we need to push a dummy value into position zero.
174 args.Unshift("dummy_string");
175 error = args.ParseOptions (*options);
176
177 // The "dummy_string" will have already been removed by ParseOptions,
178 // so no need to remove it.
179
Greg Claytonf6b8b582011-04-13 00:18:08 +0000180 if (error.Success())
181 error = options->NotifyOptionParsingFinished();
182
183 if (error.Success())
184 {
185 if (options->VerifyOptions (result))
186 return true;
187 }
188 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000189 {
190 const char *error_cstr = error.AsCString();
191 if (error_cstr)
192 {
193 // We got an error string, lets use that
Greg Clayton86edbf42011-10-26 00:56:27 +0000194 result.AppendError(error_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000195 }
196 else
197 {
198 // No error string, output the usage information into result
Greg Claytoneb0103f2011-04-07 22:46:35 +0000199 options->GenerateOptionUsage (result.GetErrorStream(), this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000200 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000201 }
Greg Claytonf6b8b582011-04-13 00:18:08 +0000202 result.SetStatus (eReturnStatusFailed);
203 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000204 }
205 return true;
206}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000207
Jim Ingham5a988412012-06-08 21:56:10 +0000208
209
210bool
Greg Claytonf9fc6092013-01-09 19:44:40 +0000211CommandObject::CheckRequirements (CommandReturnObject &result)
Jim Ingham5a988412012-06-08 21:56:10 +0000212{
Greg Claytonf9fc6092013-01-09 19:44:40 +0000213#ifdef LLDB_CONFIGURATION_DEBUG
214 // Nothing should be stored in m_exe_ctx between running commands as m_exe_ctx
215 // has shared pointers to the target, process, thread and frame and we don't
216 // want any CommandObject instances to keep any of these objects around
217 // longer than for a single command. Every command should call
218 // CommandObject::Cleanup() after it has completed
219 assert (m_exe_ctx.GetTargetPtr() == NULL);
220 assert (m_exe_ctx.GetProcessPtr() == NULL);
221 assert (m_exe_ctx.GetThreadPtr() == NULL);
222 assert (m_exe_ctx.GetFramePtr() == NULL);
223#endif
224
225 // Lock down the interpreter's execution context prior to running the
226 // command so we guarantee the selected target, process, thread and frame
227 // can't go away during the execution
228 m_exe_ctx = m_interpreter.GetExecutionContext();
229
230 const uint32_t flags = GetFlags().Get();
231 if (flags & (eFlagRequiresTarget |
232 eFlagRequiresProcess |
233 eFlagRequiresThread |
234 eFlagRequiresFrame |
235 eFlagTryTargetAPILock ))
236 {
237
238 if ((flags & eFlagRequiresTarget) && !m_exe_ctx.HasTargetScope())
239 {
240 result.AppendError (GetInvalidTargetDescription());
241 return false;
242 }
243
244 if ((flags & eFlagRequiresProcess) && !m_exe_ctx.HasProcessScope())
245 {
Jason Molendae59b0d22014-09-20 09:14:31 +0000246 if (!m_exe_ctx.HasTargetScope())
247 result.AppendError (GetInvalidTargetDescription());
248 else
249 result.AppendError (GetInvalidProcessDescription());
Greg Claytonf9fc6092013-01-09 19:44:40 +0000250 return false;
251 }
252
253 if ((flags & eFlagRequiresThread) && !m_exe_ctx.HasThreadScope())
254 {
Jason Molendae59b0d22014-09-20 09:14:31 +0000255 if (!m_exe_ctx.HasTargetScope())
256 result.AppendError (GetInvalidTargetDescription());
257 else if (!m_exe_ctx.HasProcessScope())
258 result.AppendError (GetInvalidProcessDescription());
259 else
260 result.AppendError (GetInvalidThreadDescription());
Greg Claytonf9fc6092013-01-09 19:44:40 +0000261 return false;
262 }
263
264 if ((flags & eFlagRequiresFrame) && !m_exe_ctx.HasFrameScope())
265 {
Jason Molendae59b0d22014-09-20 09:14:31 +0000266 if (!m_exe_ctx.HasTargetScope())
267 result.AppendError (GetInvalidTargetDescription());
268 else if (!m_exe_ctx.HasProcessScope())
269 result.AppendError (GetInvalidProcessDescription());
270 else if (!m_exe_ctx.HasThreadScope())
271 result.AppendError (GetInvalidThreadDescription());
272 else
273 result.AppendError (GetInvalidFrameDescription());
Greg Claytonf9fc6092013-01-09 19:44:40 +0000274 return false;
275 }
276
Ed Masted78c9572014-04-20 00:31:37 +0000277 if ((flags & eFlagRequiresRegContext) && (m_exe_ctx.GetRegisterContext() == nullptr))
Greg Claytonf9fc6092013-01-09 19:44:40 +0000278 {
279 result.AppendError (GetInvalidRegContextDescription());
280 return false;
281 }
282
283 if (flags & eFlagTryTargetAPILock)
284 {
285 Target *target = m_exe_ctx.GetTargetPtr();
286 if (target)
Greg Clayton9b5442a2014-02-07 22:31:20 +0000287 m_api_locker.Lock (target->GetAPIMutex());
Greg Claytonf9fc6092013-01-09 19:44:40 +0000288 }
289 }
290
Greg Claytonb766a732011-02-04 01:58:07 +0000291 if (GetFlags().AnySet (CommandObject::eFlagProcessMustBeLaunched | CommandObject::eFlagProcessMustBePaused))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000292 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000293 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Ed Masted78c9572014-04-20 00:31:37 +0000294 if (process == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000295 {
Jim Inghamb8e8a5f2011-07-09 00:55:34 +0000296 // A process that is not running is considered paused.
297 if (GetFlags().Test(CommandObject::eFlagProcessMustBeLaunched))
298 {
299 result.AppendError ("Process must exist.");
300 result.SetStatus (eReturnStatusFailed);
301 return false;
302 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000303 }
Greg Claytonb766a732011-02-04 01:58:07 +0000304 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000305 {
Greg Claytonb766a732011-02-04 01:58:07 +0000306 StateType state = process->GetState();
Greg Claytonb766a732011-02-04 01:58:07 +0000307 switch (state)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000308 {
Greg Clayton7a5388b2011-03-20 04:57:14 +0000309 case eStateInvalid:
Greg Claytonb766a732011-02-04 01:58:07 +0000310 case eStateSuspended:
311 case eStateCrashed:
312 case eStateStopped:
313 break;
314
315 case eStateConnected:
316 case eStateAttaching:
317 case eStateLaunching:
318 case eStateDetached:
319 case eStateExited:
320 case eStateUnloaded:
321 if (GetFlags().Test(CommandObject::eFlagProcessMustBeLaunched))
322 {
323 result.AppendError ("Process must be launched.");
324 result.SetStatus (eReturnStatusFailed);
325 return false;
326 }
327 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000328
Greg Claytonb766a732011-02-04 01:58:07 +0000329 case eStateRunning:
330 case eStateStepping:
331 if (GetFlags().Test(CommandObject::eFlagProcessMustBePaused))
332 {
333 result.AppendError ("Process is running. Use 'process interrupt' to pause execution.");
334 result.SetStatus (eReturnStatusFailed);
335 return false;
336 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000337 }
338 }
339 }
Jim Ingham5a988412012-06-08 21:56:10 +0000340 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000341}
342
Greg Claytonf9fc6092013-01-09 19:44:40 +0000343void
344CommandObject::Cleanup ()
345{
346 m_exe_ctx.Clear();
347 m_api_locker.Unlock();
348}
349
350
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000351class CommandDictCommandPartialMatch
352{
353 public:
354 CommandDictCommandPartialMatch (const char *match_str)
355 {
356 m_match_str = match_str;
357 }
358 bool operator() (const std::pair<std::string, lldb::CommandObjectSP> map_element) const
359 {
360 // A NULL or empty string matches everything.
Ed Masted78c9572014-04-20 00:31:37 +0000361 if (m_match_str == nullptr || *m_match_str == '\0')
Greg Claytonc7bece562013-01-25 18:06:21 +0000362 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000363
Greg Claytonc7bece562013-01-25 18:06:21 +0000364 return map_element.first.find (m_match_str, 0) == 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000365 }
366
367 private:
368 const char *m_match_str;
369};
370
371int
372CommandObject::AddNamesMatchingPartialString (CommandObject::CommandMap &in_map, const char *cmd_str,
373 StringList &matches)
374{
375 int number_added = 0;
376 CommandDictCommandPartialMatch matcher(cmd_str);
377
378 CommandObject::CommandMap::iterator matching_cmds = std::find_if (in_map.begin(), in_map.end(), matcher);
379
380 while (matching_cmds != in_map.end())
381 {
382 ++number_added;
383 matches.AppendString((*matching_cmds).first.c_str());
384 matching_cmds = std::find_if (++matching_cmds, in_map.end(), matcher);;
385 }
386 return number_added;
387}
388
389int
390CommandObject::HandleCompletion
391(
392 Args &input,
393 int &cursor_index,
394 int &cursor_char_position,
395 int match_start_point,
396 int max_return_elements,
Jim Ingham558ce122010-06-30 05:02:46 +0000397 bool &word_complete,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000398 StringList &matches
399)
400{
Johnny Chen6561d152012-01-20 00:59:19 +0000401 // Default implmentation of WantsCompletion() is !WantsRawCommandString().
402 // Subclasses who want raw command string but desire, for example,
403 // argument completion should override WantsCompletion() to return true,
404 // instead.
Johnny Chen6f99b632012-01-19 22:16:06 +0000405 if (WantsRawCommandString() && !WantsCompletion())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000406 {
407 // FIXME: Abstract telling the completion to insert the completion character.
408 matches.Clear();
409 return -1;
410 }
411 else
412 {
413 // Can we do anything generic with the options?
414 Options *cur_options = GetOptions();
415 CommandReturnObject result;
416 OptionElementVector opt_element_vector;
417
Ed Masted78c9572014-04-20 00:31:37 +0000418 if (cur_options != nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000419 {
420 // Re-insert the dummy command name string which will have been
421 // stripped off:
422 input.Unshift ("dummy-string");
423 cursor_index++;
424
425
426 // I stick an element on the end of the input, because if the last element is
Greg Claytonb7ad58a2013-04-04 20:35:24 +0000427 // option that requires an argument, getopt_long_only will freak out.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000428
429 input.AppendArgument ("<FAKE-VALUE>");
430
Jim Inghamd43e0092010-06-24 20:31:04 +0000431 input.ParseArgsForCompletion (*cur_options, opt_element_vector, cursor_index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000432
433 input.DeleteArgumentAtIndex(input.GetArgumentCount() - 1);
434
435 bool handled_by_options;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000436 handled_by_options = cur_options->HandleOptionCompletion (input,
Greg Clayton66111032010-06-23 01:19:29 +0000437 opt_element_vector,
438 cursor_index,
439 cursor_char_position,
440 match_start_point,
441 max_return_elements,
Jim Ingham558ce122010-06-30 05:02:46 +0000442 word_complete,
Greg Clayton66111032010-06-23 01:19:29 +0000443 matches);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000444 if (handled_by_options)
445 return matches.GetSize();
446 }
447
448 // If we got here, the last word is not an option or an option argument.
Greg Claytona7015092010-09-18 01:14:36 +0000449 return HandleArgumentCompletion (input,
Greg Clayton66111032010-06-23 01:19:29 +0000450 cursor_index,
451 cursor_char_position,
452 opt_element_vector,
453 match_start_point,
454 max_return_elements,
Jim Ingham558ce122010-06-30 05:02:46 +0000455 word_complete,
Greg Clayton66111032010-06-23 01:19:29 +0000456 matches);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000457 }
458}
459
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000460bool
Greg Claytona7015092010-09-18 01:14:36 +0000461CommandObject::HelpTextContainsWord (const char *search_word)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000462{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000463 std::string options_usage_help;
464
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000465 bool found_word = false;
466
Greg Clayton998255b2012-10-13 02:07:45 +0000467 const char *short_help = GetHelp();
468 const char *long_help = GetHelpLong();
469 const char *syntax_help = GetSyntax();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000470
Greg Clayton998255b2012-10-13 02:07:45 +0000471 if (short_help && strcasestr (short_help, search_word))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000472 found_word = true;
Greg Clayton998255b2012-10-13 02:07:45 +0000473 else if (long_help && strcasestr (long_help, search_word))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000474 found_word = true;
Greg Clayton998255b2012-10-13 02:07:45 +0000475 else if (syntax_help && strcasestr (syntax_help, search_word))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000476 found_word = true;
477
478 if (!found_word
Ed Masted78c9572014-04-20 00:31:37 +0000479 && GetOptions() != nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000480 {
481 StreamString usage_help;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000482 GetOptions()->GenerateOptionUsage (usage_help, this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000483 if (usage_help.GetSize() > 0)
484 {
485 const char *usage_text = usage_help.GetData();
Caroline Tice4b6fbf32010-10-12 22:16:53 +0000486 if (strcasestr (usage_text, search_word))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000487 found_word = true;
488 }
489 }
490
491 return found_word;
492}
Caroline Ticee139cf22010-10-01 17:46:38 +0000493
494int
495CommandObject::GetNumArgumentEntries ()
496{
497 return m_arguments.size();
498}
499
500CommandObject::CommandArgumentEntry *
501CommandObject::GetArgumentEntryAtIndex (int idx)
502{
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +0000503 if (static_cast<size_t>(idx) < m_arguments.size())
Caroline Ticee139cf22010-10-01 17:46:38 +0000504 return &(m_arguments[idx]);
505
Ed Masted78c9572014-04-20 00:31:37 +0000506 return nullptr;
Caroline Ticee139cf22010-10-01 17:46:38 +0000507}
508
509CommandObject::ArgumentTableEntry *
510CommandObject::FindArgumentDataByType (CommandArgumentType arg_type)
511{
512 const ArgumentTableEntry *table = CommandObject::GetArgumentTable();
513
514 for (int i = 0; i < eArgTypeLastArg; ++i)
515 if (table[i].arg_type == arg_type)
516 return (ArgumentTableEntry *) &(table[i]);
517
Ed Masted78c9572014-04-20 00:31:37 +0000518 return nullptr;
Caroline Ticee139cf22010-10-01 17:46:38 +0000519}
520
521void
522CommandObject::GetArgumentHelp (Stream &str, CommandArgumentType arg_type, CommandInterpreter &interpreter)
523{
524 const ArgumentTableEntry* table = CommandObject::GetArgumentTable();
525 ArgumentTableEntry *entry = (ArgumentTableEntry *) &(table[arg_type]);
526
527 // The table is *supposed* to be kept in arg_type order, but someone *could* have messed it up...
528
529 if (entry->arg_type != arg_type)
530 entry = CommandObject::FindArgumentDataByType (arg_type);
531
532 if (!entry)
533 return;
534
535 StreamString name_str;
536 name_str.Printf ("<%s>", entry->arg_name);
537
Enrico Granatafc7a7f32011-07-08 02:51:01 +0000538 if (entry->help_function)
Enrico Granata82a7d982011-07-07 00:38:40 +0000539 {
Enrico Granatafc7a7f32011-07-08 02:51:01 +0000540 const char* help_text = entry->help_function();
Enrico Granata82a7d982011-07-07 00:38:40 +0000541 if (!entry->help_function.self_formatting)
542 {
543 interpreter.OutputFormattedHelpText (str, name_str.GetData(), "--", help_text,
544 name_str.GetSize());
545 }
546 else
547 {
548 interpreter.OutputHelpText(str, name_str.GetData(), "--", help_text,
549 name_str.GetSize());
550 }
551 }
Caroline Ticee139cf22010-10-01 17:46:38 +0000552 else
553 interpreter.OutputFormattedHelpText (str, name_str.GetData(), "--", entry->help_text, name_str.GetSize());
554}
555
556const char *
557CommandObject::GetArgumentName (CommandArgumentType arg_type)
558{
Caroline Ticedeaab222010-10-01 19:59:14 +0000559 ArgumentTableEntry *entry = (ArgumentTableEntry *) &(CommandObject::GetArgumentTable()[arg_type]);
560
561 // The table is *supposed* to be kept in arg_type order, but someone *could* have messed it up...
562
563 if (entry->arg_type != arg_type)
564 entry = CommandObject::FindArgumentDataByType (arg_type);
565
Johnny Chene6acf352010-10-08 22:01:52 +0000566 if (entry)
567 return entry->arg_name;
568
569 StreamString str;
570 str << "Arg name for type (" << arg_type << ") not in arg table!";
571 return str.GetData();
Caroline Ticee139cf22010-10-01 17:46:38 +0000572}
573
Caroline Tice405fe672010-10-04 22:28:36 +0000574bool
Greg Claytone0d378b2011-03-24 21:19:54 +0000575CommandObject::IsPairType (ArgumentRepetitionType arg_repeat_type)
Caroline Tice405fe672010-10-04 22:28:36 +0000576{
577 if ((arg_repeat_type == eArgRepeatPairPlain)
578 || (arg_repeat_type == eArgRepeatPairOptional)
579 || (arg_repeat_type == eArgRepeatPairPlus)
580 || (arg_repeat_type == eArgRepeatPairStar)
581 || (arg_repeat_type == eArgRepeatPairRange)
582 || (arg_repeat_type == eArgRepeatPairRangeOptional))
583 return true;
584
585 return false;
586}
587
Johnny Chen34ddc8d2012-02-08 01:13:31 +0000588static CommandObject::CommandArgumentEntry
589OptSetFiltered(uint32_t opt_set_mask, CommandObject::CommandArgumentEntry &cmd_arg_entry)
590{
591 CommandObject::CommandArgumentEntry ret_val;
592 for (unsigned i = 0; i < cmd_arg_entry.size(); ++i)
593 if (opt_set_mask & cmd_arg_entry[i].arg_opt_set_association)
594 ret_val.push_back(cmd_arg_entry[i]);
595 return ret_val;
596}
597
598// Default parameter value of opt_set_mask is LLDB_OPT_SET_ALL, which means take
599// all the argument data into account. On rare cases where some argument sticks
600// with certain option sets, this function returns the option set filtered args.
Caroline Ticee139cf22010-10-01 17:46:38 +0000601void
Johnny Chen34ddc8d2012-02-08 01:13:31 +0000602CommandObject::GetFormattedCommandArguments (Stream &str, uint32_t opt_set_mask)
Caroline Ticee139cf22010-10-01 17:46:38 +0000603{
604 int num_args = m_arguments.size();
605 for (int i = 0; i < num_args; ++i)
606 {
607 if (i > 0)
608 str.Printf (" ");
Johnny Chen34ddc8d2012-02-08 01:13:31 +0000609 CommandArgumentEntry arg_entry =
610 opt_set_mask == LLDB_OPT_SET_ALL ? m_arguments[i]
611 : OptSetFiltered(opt_set_mask, m_arguments[i]);
Caroline Ticee139cf22010-10-01 17:46:38 +0000612 int num_alternatives = arg_entry.size();
Caroline Tice405fe672010-10-04 22:28:36 +0000613
614 if ((num_alternatives == 2)
615 && IsPairType (arg_entry[0].arg_repetition))
Caroline Ticee139cf22010-10-01 17:46:38 +0000616 {
Caroline Tice405fe672010-10-04 22:28:36 +0000617 const char *first_name = GetArgumentName (arg_entry[0].arg_type);
618 const char *second_name = GetArgumentName (arg_entry[1].arg_type);
619 switch (arg_entry[0].arg_repetition)
620 {
621 case eArgRepeatPairPlain:
622 str.Printf ("<%s> <%s>", first_name, second_name);
623 break;
624 case eArgRepeatPairOptional:
625 str.Printf ("[<%s> <%s>]", first_name, second_name);
626 break;
627 case eArgRepeatPairPlus:
628 str.Printf ("<%s> <%s> [<%s> <%s> [...]]", first_name, second_name, first_name, second_name);
629 break;
630 case eArgRepeatPairStar:
631 str.Printf ("[<%s> <%s> [<%s> <%s> [...]]]", first_name, second_name, first_name, second_name);
632 break;
633 case eArgRepeatPairRange:
634 str.Printf ("<%s_1> <%s_1> ... <%s_n> <%s_n>", first_name, second_name, first_name, second_name);
635 break;
636 case eArgRepeatPairRangeOptional:
637 str.Printf ("[<%s_1> <%s_1> ... <%s_n> <%s_n>]", first_name, second_name, first_name, second_name);
638 break;
Caroline Ticeca1176a2011-03-23 22:31:13 +0000639 // Explicitly test for all the rest of the cases, so if new types get added we will notice the
640 // missing case statement(s).
641 case eArgRepeatPlain:
642 case eArgRepeatOptional:
643 case eArgRepeatPlus:
644 case eArgRepeatStar:
645 case eArgRepeatRange:
646 // These should not be reached, as they should fail the IsPairType test above.
647 break;
Caroline Tice405fe672010-10-04 22:28:36 +0000648 }
Caroline Ticee139cf22010-10-01 17:46:38 +0000649 }
Caroline Tice405fe672010-10-04 22:28:36 +0000650 else
Caroline Ticee139cf22010-10-01 17:46:38 +0000651 {
Caroline Tice405fe672010-10-04 22:28:36 +0000652 StreamString names;
653 for (int j = 0; j < num_alternatives; ++j)
654 {
655 if (j > 0)
656 names.Printf (" | ");
657 names.Printf ("%s", GetArgumentName (arg_entry[j].arg_type));
658 }
659 switch (arg_entry[0].arg_repetition)
660 {
661 case eArgRepeatPlain:
662 str.Printf ("<%s>", names.GetData());
663 break;
664 case eArgRepeatPlus:
665 str.Printf ("<%s> [<%s> [...]]", names.GetData(), names.GetData());
666 break;
667 case eArgRepeatStar:
668 str.Printf ("[<%s> [<%s> [...]]]", names.GetData(), names.GetData());
669 break;
670 case eArgRepeatOptional:
671 str.Printf ("[<%s>]", names.GetData());
672 break;
673 case eArgRepeatRange:
Jason Molendafd54b362011-09-20 21:44:10 +0000674 str.Printf ("<%s_1> .. <%s_n>", names.GetData(), names.GetData());
Caroline Ticeca1176a2011-03-23 22:31:13 +0000675 break;
676 // Explicitly test for all the rest of the cases, so if new types get added we will notice the
677 // missing case statement(s).
678 case eArgRepeatPairPlain:
679 case eArgRepeatPairOptional:
680 case eArgRepeatPairPlus:
681 case eArgRepeatPairStar:
682 case eArgRepeatPairRange:
683 case eArgRepeatPairRangeOptional:
684 // These should not be hit, as they should pass the IsPairType test above, and control should
685 // have gone into the other branch of the if statement.
686 break;
Caroline Tice405fe672010-10-04 22:28:36 +0000687 }
Caroline Ticee139cf22010-10-01 17:46:38 +0000688 }
689 }
690}
691
Stephen Wilson0c16aa62011-03-23 02:12:10 +0000692CommandArgumentType
Caroline Ticee139cf22010-10-01 17:46:38 +0000693CommandObject::LookupArgumentName (const char *arg_name)
694{
695 CommandArgumentType return_type = eArgTypeLastArg;
696
697 std::string arg_name_str (arg_name);
698 size_t len = arg_name_str.length();
699 if (arg_name[0] == '<'
700 && arg_name[len-1] == '>')
701 arg_name_str = arg_name_str.substr (1, len-2);
702
Johnny Chen331eff32011-07-14 22:20:12 +0000703 const ArgumentTableEntry *table = GetArgumentTable();
Caroline Ticee139cf22010-10-01 17:46:38 +0000704 for (int i = 0; i < eArgTypeLastArg; ++i)
Johnny Chen331eff32011-07-14 22:20:12 +0000705 if (arg_name_str.compare (table[i].arg_name) == 0)
Caroline Ticee139cf22010-10-01 17:46:38 +0000706 return_type = g_arguments_data[i].arg_type;
707
708 return return_type;
709}
710
711static const char *
Jim Ingham931e6742012-08-23 23:37:31 +0000712RegisterNameHelpTextCallback ()
713{
714 return "Register names can be specified using the architecture specific names. "
Jim Ingham84c7bd72012-08-23 23:47:08 +0000715 "They can also be specified using generic names. Not all generic entities have "
716 "registers backing them on all architectures. When they don't the generic name "
717 "will return an error.\n"
Jim Ingham931e6742012-08-23 23:37:31 +0000718 "The generic names defined in lldb are:\n"
719 "\n"
720 "pc - program counter register\n"
721 "ra - return address register\n"
722 "fp - frame pointer register\n"
723 "sp - stack pointer register\n"
Jim Ingham84c7bd72012-08-23 23:47:08 +0000724 "flags - the flags register\n"
Jim Ingham931e6742012-08-23 23:37:31 +0000725 "arg{1-6} - integer argument passing registers.\n";
726}
727
728static const char *
Caroline Ticee139cf22010-10-01 17:46:38 +0000729BreakpointIDHelpTextCallback ()
730{
Greg Clayton86edbf42011-10-26 00:56:27 +0000731 return "Breakpoint ID's consist major and minor numbers; the major number "
732 "corresponds to the single entity that was created with a 'breakpoint set' "
733 "command; the minor numbers correspond to all the locations that were actually "
734 "found/set based on the major breakpoint. A full breakpoint ID might look like "
735 "3.14, meaning the 14th location set for the 3rd breakpoint. You can specify "
736 "all the locations of a breakpoint by just indicating the major breakpoint "
737 "number. A valid breakpoint id consists either of just the major id number, "
738 "or the major number, a dot, and the location number (e.g. 3 or 3.2 could "
739 "both be valid breakpoint ids).";
Caroline Ticee139cf22010-10-01 17:46:38 +0000740}
741
742static const char *
743BreakpointIDRangeHelpTextCallback ()
744{
Greg Clayton86edbf42011-10-26 00:56:27 +0000745 return "A 'breakpoint id list' is a manner of specifying multiple breakpoints. "
746 "This can be done through several mechanisms. The easiest way is to just "
747 "enter a space-separated list of breakpoint ids. To specify all the "
748 "breakpoint locations under a major breakpoint, you can use the major "
749 "breakpoint number followed by '.*', eg. '5.*' means all the locations under "
750 "breakpoint 5. You can also indicate a range of breakpoints by using "
751 "<start-bp-id> - <end-bp-id>. The start-bp-id and end-bp-id for a range can "
752 "be any valid breakpoint ids. It is not legal, however, to specify a range "
753 "using specific locations that cross major breakpoint numbers. I.e. 3.2 - 3.7"
754 " is legal; 2 - 5 is legal; but 3.2 - 4.4 is not legal.";
Caroline Ticee139cf22010-10-01 17:46:38 +0000755}
756
Enrico Granata0a3958e2011-07-02 00:25:22 +0000757static const char *
Jim Ingham5e09c8c2014-12-16 23:40:14 +0000758BreakpointNameHelpTextCallback ()
759{
760 return "A name that can be added to a breakpoint when it is created, or later "
761 "on with the \"breakpoint name add\" command. "
762 "Breakpoint names can be used to specify breakpoints in all the places breakpoint ID's "
763 "and breakpoint ID ranges can be used. As such they provide a convenient way to group breakpoints, "
764 "and to operate on breakpoints you create without having to track the breakpoint number. "
765 "Note, the attributes you set when using a breakpoint name in a breakpoint command don't "
766 "adhere to the name, but instead are set individually on all the breakpoints currently tagged with that name. Future breakpoints "
767 "tagged with that name will not pick up the attributes previously given using that name. "
768 "In order to distinguish breakpoint names from breakpoint ID's and ranges, "
769 "names must start with a letter from a-z or A-Z and cannot contain spaces, \".\" or \"-\". "
770 "Also, breakpoint names can only be applied to breakpoints, not to breakpoint locations.";
771}
772
773static const char *
Greg Clayton86edbf42011-10-26 00:56:27 +0000774GDBFormatHelpTextCallback ()
775{
Greg Claytonf91381e2011-10-26 18:35:21 +0000776 return "A GDB format consists of a repeat count, a format letter and a size letter. "
777 "The repeat count is optional and defaults to 1. The format letter is optional "
778 "and defaults to the previous format that was used. The size letter is optional "
779 "and defaults to the previous size that was used.\n"
780 "\n"
781 "Format letters include:\n"
782 "o - octal\n"
783 "x - hexadecimal\n"
784 "d - decimal\n"
785 "u - unsigned decimal\n"
786 "t - binary\n"
787 "f - float\n"
788 "a - address\n"
789 "i - instruction\n"
790 "c - char\n"
791 "s - string\n"
792 "T - OSType\n"
793 "A - float as hex\n"
794 "\n"
795 "Size letters include:\n"
796 "b - 1 byte (byte)\n"
797 "h - 2 bytes (halfword)\n"
798 "w - 4 bytes (word)\n"
799 "g - 8 bytes (giant)\n"
800 "\n"
801 "Example formats:\n"
802 "32xb - show 32 1 byte hexadecimal integer values\n"
803 "16xh - show 16 2 byte hexadecimal integer values\n"
804 "64 - show 64 2 byte hexadecimal integer values (format and size from the last format)\n"
805 "dw - show 1 4 byte decimal integer value\n"
806 ;
Greg Clayton86edbf42011-10-26 00:56:27 +0000807}
808
809static const char *
Enrico Granata0a3958e2011-07-02 00:25:22 +0000810FormatHelpTextCallback ()
811{
Enrico Granata82a7d982011-07-07 00:38:40 +0000812
Ed Masted78c9572014-04-20 00:31:37 +0000813 static char* help_text_ptr = nullptr;
Enrico Granata82a7d982011-07-07 00:38:40 +0000814
815 if (help_text_ptr)
816 return help_text_ptr;
817
Enrico Granata0a3958e2011-07-02 00:25:22 +0000818 StreamString sstr;
819 sstr << "One of the format names (or one-character names) that can be used to show a variable's value:\n";
820 for (Format f = eFormatDefault; f < kNumFormats; f = Format(f+1))
821 {
Enrico Granata82a7d982011-07-07 00:38:40 +0000822 if (f != eFormatDefault)
823 sstr.PutChar('\n');
824
Enrico Granata0a3958e2011-07-02 00:25:22 +0000825 char format_char = FormatManager::GetFormatAsFormatChar(f);
826 if (format_char)
827 sstr.Printf("'%c' or ", format_char);
828
Enrico Granata82a7d982011-07-07 00:38:40 +0000829 sstr.Printf ("\"%s\"", FormatManager::GetFormatAsCString(f));
Enrico Granata0a3958e2011-07-02 00:25:22 +0000830 }
831
832 sstr.Flush();
833
834 std::string data = sstr.GetString();
835
Enrico Granata82a7d982011-07-07 00:38:40 +0000836 help_text_ptr = new char[data.length()+1];
Enrico Granata0a3958e2011-07-02 00:25:22 +0000837
Enrico Granata82a7d982011-07-07 00:38:40 +0000838 data.copy(help_text_ptr, data.length());
Enrico Granata0a3958e2011-07-02 00:25:22 +0000839
Enrico Granata82a7d982011-07-07 00:38:40 +0000840 return help_text_ptr;
Enrico Granata0a3958e2011-07-02 00:25:22 +0000841}
842
843static const char *
Sean Callanand9477392012-10-23 00:50:09 +0000844LanguageTypeHelpTextCallback ()
845{
Ed Masted78c9572014-04-20 00:31:37 +0000846 static char* help_text_ptr = nullptr;
Sean Callanand9477392012-10-23 00:50:09 +0000847
848 if (help_text_ptr)
849 return help_text_ptr;
850
851 StreamString sstr;
852 sstr << "One of the following languages:\n";
Jim Inghamde50d362015-04-17 00:44:36 +0000853
854 LanguageRuntime::PrintAllLanguages(sstr, " ", "\n");
855
Sean Callanand9477392012-10-23 00:50:09 +0000856 sstr.Flush();
857
858 std::string data = sstr.GetString();
859
860 help_text_ptr = new char[data.length()+1];
861
862 data.copy(help_text_ptr, data.length());
863
864 return help_text_ptr;
865}
866
867static const char *
Enrico Granata82a7d982011-07-07 00:38:40 +0000868SummaryStringHelpTextCallback()
Enrico Granata0a3958e2011-07-02 00:25:22 +0000869{
Enrico Granata82a7d982011-07-07 00:38:40 +0000870 return
871 "A summary string is a way to extract information from variables in order to present them using a summary.\n"
872 "Summary strings contain static text, variables, scopes and control sequences:\n"
873 " - Static text can be any sequence of non-special characters, i.e. anything but '{', '}', '$', or '\\'.\n"
874 " - Variables are sequences of characters beginning with ${, ending with } and that contain symbols in the format described below.\n"
875 " - Scopes are any sequence of text between { and }. Anything included in a scope will only appear in the output summary if there were no errors.\n"
876 " - Control sequences are the usual C/C++ '\\a', '\\n', ..., plus '\\$', '\\{' and '\\}'.\n"
877 "A summary string works by copying static text verbatim, turning control sequences into their character counterpart, expanding variables and trying to expand scopes.\n"
878 "A variable is expanded by giving it a value other than its textual representation, and the way this is done depends on what comes after the ${ marker.\n"
879 "The most common sequence if ${var followed by an expression path, which is the text one would type to access a member of an aggregate types, given a variable of that type"
880 " (e.g. if type T has a member named x, which has a member named y, and if t is of type T, the expression path would be .x.y and the way to fit that into a summary string would be"
Enrico Granata9128ee22011-09-06 19:20:51 +0000881 " ${var.x.y}). You can also use ${*var followed by an expression path and in that case the object referred by the path will be dereferenced before being displayed."
882 " If the object is not a pointer, doing so will cause an error. For additional details on expression paths, you can type 'help expr-path'. \n"
Enrico Granata82a7d982011-07-07 00:38:40 +0000883 "By default, summary strings attempt to display the summary for any variable they reference, and if that fails the value. If neither can be shown, nothing is displayed."
884 "In a summary string, you can also use an array index [n], or a slice-like range [n-m]. This can have two different meanings depending on what kind of object the expression"
885 " path refers to:\n"
886 " - if it is a scalar type (any basic type like int, float, ...) the expression is a bitfield, i.e. the bits indicated by the indexing operator are extracted out of the number"
887 " and displayed as an individual variable\n"
888 " - if it is an array or pointer the array items indicated by the indexing operator are shown as the result of the variable. if the expression is an array, real array items are"
889 " printed; if it is a pointer, the pointer-as-array syntax is used to obtain the values (this means, the latter case can have no range checking)\n"
Enrico Granata9128ee22011-09-06 19:20:51 +0000890 "If you are trying to display an array for which the size is known, you can also use [] instead of giving an exact range. This has the effect of showing items 0 thru size - 1.\n"
891 "Additionally, a variable can contain an (optional) format code, as in ${var.x.y%code}, where code can be any of the valid formats described in 'help format', or one of the"
892 " special symbols only allowed as part of a variable:\n"
893 " %V: show the value of the object by default\n"
894 " %S: show the summary of the object by default\n"
895 " %@: show the runtime-provided object description (for Objective-C, it calls NSPrintForDebugger; for C/C++ it does nothing)\n"
896 " %L: show the location of the object (memory address or a register name)\n"
897 " %#: show the number of children of the object\n"
898 " %T: show the type of the object\n"
899 "Another variable that you can use in summary strings is ${svar . This sequence works exactly like ${var, including the fact that ${*svar is an allowed sequence, but uses"
900 " the object's synthetic children provider instead of the actual objects. For instance, if you are using STL synthetic children providers, the following summary string would"
901 " count the number of actual elements stored in an std::list:\n"
902 "type summary add -s \"${svar%#}\" -x \"std::list<\"";
903}
904
905static const char *
906ExprPathHelpTextCallback()
907{
908 return
909 "An expression path is the sequence of symbols that is used in C/C++ to access a member variable of an aggregate object (class).\n"
910 "For instance, given a class:\n"
911 " class foo {\n"
912 " int a;\n"
913 " int b; .\n"
914 " foo* next;\n"
915 " };\n"
916 "the expression to read item b in the item pointed to by next for foo aFoo would be aFoo.next->b.\n"
917 "Given that aFoo could just be any object of type foo, the string '.next->b' is the expression path, because it can be attached to any foo instance to achieve the effect.\n"
918 "Expression paths in LLDB include dot (.) and arrow (->) operators, and most commands using expression paths have ways to also accept the star (*) operator.\n"
919 "The meaning of these operators is the same as the usual one given to them by the C/C++ standards.\n"
920 "LLDB also has support for indexing ([ ]) in expression paths, and extends the traditional meaning of the square brackets operator to allow bitfield extraction:\n"
921 "for objects of native types (int, float, char, ...) saying '[n-m]' as an expression path (where n and m are any positive integers, e.g. [3-5]) causes LLDB to extract"
922 " bits n thru m from the value of the variable. If n == m, [n] is also allowed as a shortcut syntax. For arrays and pointers, expression paths can only contain one index"
923 " and the meaning of the operation is the same as the one defined by C/C++ (item extraction). Some commands extend bitfield-like syntax for arrays and pointers with the"
924 " meaning of array slicing (taking elements n thru m inside the array or pointed-to memory).";
Enrico Granata0a3958e2011-07-02 00:25:22 +0000925}
926
Johnny Chen184d7a72011-09-21 01:00:02 +0000927void
Enrico Granata9b62d1d2013-06-12 01:50:57 +0000928CommandObject::GenerateHelpText (CommandReturnObject &result)
929{
930 GenerateHelpText(result.GetOutputStream());
931
932 result.SetStatus (eReturnStatusSuccessFinishNoResult);
933}
934
935void
936CommandObject::GenerateHelpText (Stream &output_strm)
937{
938 CommandInterpreter& interpreter = GetCommandInterpreter();
Ed Masted78c9572014-04-20 00:31:37 +0000939 if (GetOptions() != nullptr)
Enrico Granata9b62d1d2013-06-12 01:50:57 +0000940 {
941 if (WantsRawCommandString())
942 {
943 std::string help_text (GetHelp());
944 help_text.append (" This command takes 'raw' input (no need to quote stuff).");
945 interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1);
946 }
947 else
948 interpreter.OutputFormattedHelpText (output_strm, "", "", GetHelp(), 1);
949 output_strm.Printf ("\nSyntax: %s\n", GetSyntax());
950 GetOptions()->GenerateOptionUsage (output_strm, this);
951 const char *long_help = GetHelpLong();
Ed Masted78c9572014-04-20 00:31:37 +0000952 if ((long_help != nullptr)
Enrico Granata9b62d1d2013-06-12 01:50:57 +0000953 && (strlen (long_help) > 0))
954 output_strm.Printf ("\n%s", long_help);
955 if (WantsRawCommandString() && !WantsCompletion())
956 {
957 // Emit the message about using ' -- ' between the end of the command options and the raw input
958 // conditionally, i.e., only if the command object does not want completion.
959 interpreter.OutputFormattedHelpText (output_strm, "", "",
960 "\nIMPORTANT NOTE: Because this command takes 'raw' input, if you use any command options"
961 " you must use ' -- ' between the end of the command options and the beginning of the raw input.", 1);
962 }
963 else if (GetNumArgumentEntries() > 0
964 && GetOptions()
965 && GetOptions()->NumCommandOptions() > 0)
966 {
967 // Also emit a warning about using "--" in case you are using a command that takes options and arguments.
968 interpreter.OutputFormattedHelpText (output_strm, "", "",
969 "\nThis command takes options and free-form arguments. If your arguments resemble"
970 " option specifiers (i.e., they start with a - or --), you must use ' -- ' between"
971 " the end of the command options and the beginning of the arguments.", 1);
972 }
973 }
974 else if (IsMultiwordObject())
975 {
976 if (WantsRawCommandString())
977 {
978 std::string help_text (GetHelp());
979 help_text.append (" This command takes 'raw' input (no need to quote stuff).");
980 interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1);
981 }
982 else
983 interpreter.OutputFormattedHelpText (output_strm, "", "", GetHelp(), 1);
984 GenerateHelpText (output_strm);
985 }
986 else
987 {
988 const char *long_help = GetHelpLong();
Ed Masted78c9572014-04-20 00:31:37 +0000989 if ((long_help != nullptr)
Enrico Granata9b62d1d2013-06-12 01:50:57 +0000990 && (strlen (long_help) > 0))
991 output_strm.Printf ("%s", long_help);
992 else if (WantsRawCommandString())
993 {
994 std::string help_text (GetHelp());
995 help_text.append (" This command takes 'raw' input (no need to quote stuff).");
996 interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1);
997 }
998 else
999 interpreter.OutputFormattedHelpText (output_strm, "", "", GetHelp(), 1);
1000 output_strm.Printf ("\nSyntax: %s\n", GetSyntax());
1001 }
1002}
1003
1004void
Johnny Chende753462011-09-22 22:34:09 +00001005CommandObject::AddIDsArgumentData(CommandArgumentEntry &arg, CommandArgumentType ID, CommandArgumentType IDRange)
Johnny Chen184d7a72011-09-21 01:00:02 +00001006{
1007 CommandArgumentData id_arg;
1008 CommandArgumentData id_range_arg;
1009
1010 // Create the first variant for the first (and only) argument for this command.
Johnny Chende753462011-09-22 22:34:09 +00001011 id_arg.arg_type = ID;
Johnny Chen184d7a72011-09-21 01:00:02 +00001012 id_arg.arg_repetition = eArgRepeatOptional;
1013
1014 // Create the second variant for the first (and only) argument for this command.
Johnny Chende753462011-09-22 22:34:09 +00001015 id_range_arg.arg_type = IDRange;
Johnny Chen184d7a72011-09-21 01:00:02 +00001016 id_range_arg.arg_repetition = eArgRepeatOptional;
1017
Johnny Chena3234732011-09-21 01:04:49 +00001018 // The first (and only) argument for this command could be either an id or an id_range.
Johnny Chen184d7a72011-09-21 01:00:02 +00001019 // Push both variants into the entry for the first argument for this command.
1020 arg.push_back(id_arg);
1021 arg.push_back(id_range_arg);
1022}
1023
Greg Clayton9d0402b2011-02-20 02:15:07 +00001024const char *
1025CommandObject::GetArgumentTypeAsCString (const lldb::CommandArgumentType arg_type)
1026{
Zachary Turner48b475c2015-04-02 20:57:38 +00001027 assert(arg_type < eArgTypeLastArg && "Invalid argument type passed to GetArgumentTypeAsCString");
1028 return g_arguments_data[arg_type].arg_name;
Greg Clayton9d0402b2011-02-20 02:15:07 +00001029}
1030
1031const char *
1032CommandObject::GetArgumentDescriptionAsCString (const lldb::CommandArgumentType arg_type)
1033{
Zachary Turner48b475c2015-04-02 20:57:38 +00001034 assert(arg_type < eArgTypeLastArg && "Invalid argument type passed to GetArgumentDescriptionAsCString");
1035 return g_arguments_data[arg_type].help_text;
Greg Clayton9d0402b2011-02-20 02:15:07 +00001036}
1037
Jim Ingham893c9322014-11-22 01:42:44 +00001038Target *
1039CommandObject::GetDummyTarget()
1040{
1041 return m_interpreter.GetDebugger().GetDummyTarget();
1042}
1043
1044Target *
Jim Ingham33df7cd2014-12-06 01:28:03 +00001045CommandObject::GetSelectedOrDummyTarget(bool prefer_dummy)
Jim Ingham893c9322014-11-22 01:42:44 +00001046{
Jim Ingham33df7cd2014-12-06 01:28:03 +00001047 return m_interpreter.GetDebugger().GetSelectedOrDummyTarget(prefer_dummy);
Jim Ingham893c9322014-11-22 01:42:44 +00001048}
1049
Jim Ingham5a988412012-06-08 21:56:10 +00001050bool
1051CommandObjectParsed::Execute (const char *args_string, CommandReturnObject &result)
1052{
Jim Ingham5a988412012-06-08 21:56:10 +00001053 bool handled = false;
1054 Args cmd_args (args_string);
Jim Ingham3b652622014-08-06 00:10:12 +00001055 if (HasOverrideCallback())
Jim Ingham5a988412012-06-08 21:56:10 +00001056 {
1057 Args full_args (GetCommandName ());
1058 full_args.AppendArguments(cmd_args);
Jim Ingham3b652622014-08-06 00:10:12 +00001059 handled = InvokeOverrideCallback (full_args.GetConstArgumentVector(), result);
Jim Ingham5a988412012-06-08 21:56:10 +00001060 }
1061 if (!handled)
1062 {
1063 for (size_t i = 0; i < cmd_args.GetArgumentCount(); ++i)
1064 {
1065 const char *tmp_str = cmd_args.GetArgumentAtIndex (i);
1066 if (tmp_str[0] == '`') // back-quote
1067 cmd_args.ReplaceArgumentAtIndex (i, m_interpreter.ProcessEmbeddedScriptCommands (tmp_str));
1068 }
1069
Greg Claytonf9fc6092013-01-09 19:44:40 +00001070 if (CheckRequirements(result))
1071 {
1072 if (ParseOptions (cmd_args, result))
1073 {
1074 // Call the command-specific version of 'Execute', passing it the already processed arguments.
1075 handled = DoExecute (cmd_args, result);
1076 }
1077 }
Jim Ingham5a988412012-06-08 21:56:10 +00001078
Greg Claytonf9fc6092013-01-09 19:44:40 +00001079 Cleanup();
Jim Ingham5a988412012-06-08 21:56:10 +00001080 }
1081 return handled;
1082}
1083
1084bool
1085CommandObjectRaw::Execute (const char *args_string, CommandReturnObject &result)
1086{
Jim Ingham5a988412012-06-08 21:56:10 +00001087 bool handled = false;
Jim Ingham3b652622014-08-06 00:10:12 +00001088 if (HasOverrideCallback())
Jim Ingham5a988412012-06-08 21:56:10 +00001089 {
1090 std::string full_command (GetCommandName ());
1091 full_command += ' ';
1092 full_command += args_string;
Ed Masted78c9572014-04-20 00:31:37 +00001093 const char *argv[2] = { nullptr, nullptr };
Jim Ingham5a988412012-06-08 21:56:10 +00001094 argv[0] = full_command.c_str();
Jim Ingham3b652622014-08-06 00:10:12 +00001095 handled = InvokeOverrideCallback (argv, result);
Jim Ingham5a988412012-06-08 21:56:10 +00001096 }
1097 if (!handled)
1098 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00001099 if (CheckRequirements(result))
Jim Ingham5a988412012-06-08 21:56:10 +00001100 handled = DoExecute (args_string, result);
Greg Claytonf9fc6092013-01-09 19:44:40 +00001101
1102 Cleanup();
Jim Ingham5a988412012-06-08 21:56:10 +00001103 }
1104 return handled;
1105}
1106
Johnny Chenca7835c2012-05-26 00:32:39 +00001107static
1108const char *arch_helper()
1109{
Greg Claytond70b14e2012-05-26 17:21:14 +00001110 static StreamString g_archs_help;
Johnny Chen797a1b32012-05-29 20:04:10 +00001111 if (g_archs_help.Empty())
Greg Claytond70b14e2012-05-26 17:21:14 +00001112 {
1113 StringList archs;
Ed Masted78c9572014-04-20 00:31:37 +00001114 ArchSpec::AutoComplete(nullptr, archs);
Greg Claytond70b14e2012-05-26 17:21:14 +00001115 g_archs_help.Printf("These are the supported architecture names:\n");
Johnny Chen797a1b32012-05-29 20:04:10 +00001116 archs.Join("\n", g_archs_help);
Greg Claytond70b14e2012-05-26 17:21:14 +00001117 }
1118 return g_archs_help.GetData();
Johnny Chenca7835c2012-05-26 00:32:39 +00001119}
1120
Caroline Ticee139cf22010-10-01 17:46:38 +00001121CommandObject::ArgumentTableEntry
1122CommandObject::g_arguments_data[] =
1123{
Ed Masted78c9572014-04-20 00:31:37 +00001124 { eArgTypeAddress, "address", CommandCompletions::eNoCompletion, { nullptr, false }, "A valid address in the target program's execution space." },
1125 { eArgTypeAddressOrExpression, "address-expression", CommandCompletions::eNoCompletion, { nullptr, false }, "An expression that resolves to an address." },
1126 { eArgTypeAliasName, "alias-name", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of an abbreviation (alias) for a debugger command." },
1127 { eArgTypeAliasOptions, "options-for-aliased-command", CommandCompletions::eNoCompletion, { nullptr, false }, "Command options to be used as part of an alias (abbreviation) definition. (See 'help commands alias' for more information.)" },
Johnny Chenca7835c2012-05-26 00:32:39 +00001128 { eArgTypeArchitecture, "arch", CommandCompletions::eArchitectureCompletion, { arch_helper, true }, "The architecture name, e.g. i386 or x86_64." },
Ed Masted78c9572014-04-20 00:31:37 +00001129 { eArgTypeBoolean, "boolean", CommandCompletions::eNoCompletion, { nullptr, false }, "A Boolean value: 'true' or 'false'" },
1130 { eArgTypeBreakpointID, "breakpt-id", CommandCompletions::eNoCompletion, { BreakpointIDHelpTextCallback, false }, nullptr },
1131 { eArgTypeBreakpointIDRange, "breakpt-id-list", CommandCompletions::eNoCompletion, { BreakpointIDRangeHelpTextCallback, false }, nullptr },
Jim Ingham5e09c8c2014-12-16 23:40:14 +00001132 { eArgTypeBreakpointName, "breakpoint-name", CommandCompletions::eNoCompletion, { BreakpointNameHelpTextCallback, false }, nullptr },
Ed Masted78c9572014-04-20 00:31:37 +00001133 { eArgTypeByteSize, "byte-size", CommandCompletions::eNoCompletion, { nullptr, false }, "Number of bytes to use." },
1134 { eArgTypeClassName, "class-name", CommandCompletions::eNoCompletion, { nullptr, false }, "Then name of a class from the debug information in the program." },
1135 { eArgTypeCommandName, "cmd-name", CommandCompletions::eNoCompletion, { nullptr, false }, "A debugger command (may be multiple words), without any options or arguments." },
1136 { eArgTypeCount, "count", CommandCompletions::eNoCompletion, { nullptr, false }, "An unsigned integer." },
1137 { eArgTypeDirectoryName, "directory", CommandCompletions::eDiskDirectoryCompletion, { nullptr, false }, "A directory name." },
1138 { eArgTypeDisassemblyFlavor, "disassembly-flavor", CommandCompletions::eNoCompletion, { nullptr, false }, "A disassembly flavor recognized by your disassembly plugin. Currently the only valid options are \"att\" and \"intel\" for Intel targets" },
1139 { eArgTypeDescriptionVerbosity, "description-verbosity", CommandCompletions::eNoCompletion, { nullptr, false }, "How verbose the output of 'po' should be." },
1140 { eArgTypeEndAddress, "end-address", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1141 { eArgTypeExpression, "expr", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1142 { eArgTypeExpressionPath, "expr-path", CommandCompletions::eNoCompletion, { ExprPathHelpTextCallback, true }, nullptr },
1143 { eArgTypeExprFormat, "expression-format", CommandCompletions::eNoCompletion, { nullptr, false }, "[ [bool|b] | [bin] | [char|c] | [oct|o] | [dec|i|d|u] | [hex|x] | [float|f] | [cstr|s] ]" },
1144 { eArgTypeFilename, "filename", CommandCompletions::eDiskFileCompletion, { nullptr, false }, "The name of a file (can include path)." },
1145 { eArgTypeFormat, "format", CommandCompletions::eNoCompletion, { FormatHelpTextCallback, true }, nullptr },
1146 { eArgTypeFrameIndex, "frame-index", CommandCompletions::eNoCompletion, { nullptr, false }, "Index into a thread's list of frames." },
1147 { eArgTypeFullName, "fullname", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1148 { eArgTypeFunctionName, "function-name", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of a function." },
1149 { eArgTypeFunctionOrSymbol, "function-or-symbol", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of a function or symbol." },
1150 { eArgTypeGDBFormat, "gdb-format", CommandCompletions::eNoCompletion, { GDBFormatHelpTextCallback, true }, nullptr },
Enrico Granata735152e2014-09-15 17:52:44 +00001151 { eArgTypeHelpText, "help-text", CommandCompletions::eNoCompletion, { nullptr, false }, "Text to be used as help for some other entity in LLDB" },
Ed Masted78c9572014-04-20 00:31:37 +00001152 { eArgTypeIndex, "index", CommandCompletions::eNoCompletion, { nullptr, false }, "An index into a list." },
1153 { eArgTypeLanguage, "language", CommandCompletions::eNoCompletion, { LanguageTypeHelpTextCallback, true }, nullptr },
1154 { eArgTypeLineNum, "linenum", CommandCompletions::eNoCompletion, { nullptr, false }, "Line number in a source file." },
1155 { eArgTypeLogCategory, "log-category", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of a category within a log channel, e.g. all (try \"log list\" to see a list of all channels and their categories." },
1156 { eArgTypeLogChannel, "log-channel", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of a log channel, e.g. process.gdb-remote (try \"log list\" to see a list of all channels and their categories)." },
1157 { eArgTypeMethod, "method", CommandCompletions::eNoCompletion, { nullptr, false }, "A C++ method name." },
1158 { eArgTypeName, "name", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1159 { eArgTypeNewPathPrefix, "new-path-prefix", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1160 { eArgTypeNumLines, "num-lines", CommandCompletions::eNoCompletion, { nullptr, false }, "The number of lines to use." },
1161 { eArgTypeNumberPerLine, "number-per-line", CommandCompletions::eNoCompletion, { nullptr, false }, "The number of items per line to display." },
1162 { eArgTypeOffset, "offset", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1163 { eArgTypeOldPathPrefix, "old-path-prefix", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1164 { eArgTypeOneLiner, "one-line-command", CommandCompletions::eNoCompletion, { nullptr, false }, "A command that is entered as a single line of text." },
1165 { eArgTypePath, "path", CommandCompletions::eDiskFileCompletion, { nullptr, false }, "Path." },
1166 { eArgTypePermissionsNumber, "perms-numeric", CommandCompletions::eNoCompletion, { nullptr, false }, "Permissions given as an octal number (e.g. 755)." },
1167 { eArgTypePermissionsString, "perms=string", CommandCompletions::eNoCompletion, { nullptr, false }, "Permissions given as a string value (e.g. rw-r-xr--)." },
1168 { eArgTypePid, "pid", CommandCompletions::eNoCompletion, { nullptr, false }, "The process ID number." },
1169 { eArgTypePlugin, "plugin", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1170 { eArgTypeProcessName, "process-name", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of the process." },
1171 { eArgTypePythonClass, "python-class", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of a Python class." },
1172 { eArgTypePythonFunction, "python-function", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of a Python function." },
1173 { eArgTypePythonScript, "python-script", CommandCompletions::eNoCompletion, { nullptr, false }, "Source code written in Python." },
1174 { eArgTypeQueueName, "queue-name", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of the thread queue." },
1175 { eArgTypeRegisterName, "register-name", CommandCompletions::eNoCompletion, { RegisterNameHelpTextCallback, true }, nullptr },
1176 { eArgTypeRegularExpression, "regular-expression", CommandCompletions::eNoCompletion, { nullptr, false }, "A regular expression." },
1177 { eArgTypeRunArgs, "run-args", CommandCompletions::eNoCompletion, { nullptr, false }, "Arguments to be passed to the target program when it starts executing." },
1178 { eArgTypeRunMode, "run-mode", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1179 { eArgTypeScriptedCommandSynchronicity, "script-cmd-synchronicity", CommandCompletions::eNoCompletion, { nullptr, false }, "The synchronicity to use to run scripted commands with regard to LLDB event system." },
1180 { eArgTypeScriptLang, "script-language", CommandCompletions::eNoCompletion, { nullptr, false }, "The scripting language to be used for script-based commands. Currently only Python is valid." },
1181 { eArgTypeSearchWord, "search-word", CommandCompletions::eNoCompletion, { nullptr, false }, "The word for which you wish to search for information about." },
1182 { eArgTypeSelector, "selector", CommandCompletions::eNoCompletion, { nullptr, false }, "An Objective-C selector name." },
1183 { eArgTypeSettingIndex, "setting-index", CommandCompletions::eNoCompletion, { nullptr, false }, "An index into a settings variable that is an array (try 'settings list' to see all the possible settings variables and their types)." },
1184 { eArgTypeSettingKey, "setting-key", CommandCompletions::eNoCompletion, { nullptr, false }, "A key into a settings variables that is a dictionary (try 'settings list' to see all the possible settings variables and their types)." },
1185 { eArgTypeSettingPrefix, "setting-prefix", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of a settable internal debugger variable up to a dot ('.'), e.g. 'target.process.'" },
1186 { eArgTypeSettingVariableName, "setting-variable-name", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of a settable internal debugger variable. Type 'settings list' to see a complete list of such variables." },
1187 { eArgTypeShlibName, "shlib-name", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of a shared library." },
1188 { eArgTypeSourceFile, "source-file", CommandCompletions::eSourceFileCompletion, { nullptr, false }, "The name of a source file.." },
1189 { eArgTypeSortOrder, "sort-order", CommandCompletions::eNoCompletion, { nullptr, false }, "Specify a sort order when dumping lists." },
1190 { eArgTypeStartAddress, "start-address", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1191 { eArgTypeSummaryString, "summary-string", CommandCompletions::eNoCompletion, { SummaryStringHelpTextCallback, true }, nullptr },
1192 { eArgTypeSymbol, "symbol", CommandCompletions::eSymbolCompletion, { nullptr, false }, "Any symbol name (function name, variable, argument, etc.)" },
1193 { eArgTypeThreadID, "thread-id", CommandCompletions::eNoCompletion, { nullptr, false }, "Thread ID number." },
1194 { eArgTypeThreadIndex, "thread-index", CommandCompletions::eNoCompletion, { nullptr, false }, "Index into the process' list of threads." },
1195 { eArgTypeThreadName, "thread-name", CommandCompletions::eNoCompletion, { nullptr, false }, "The thread's name." },
Jim Inghama72b31c2015-04-22 19:42:18 +00001196 { eArgTypeTypeName, "type-name", CommandCompletions::eNoCompletion, { nullptr, false }, "A type name." },
Ed Masted78c9572014-04-20 00:31:37 +00001197 { eArgTypeUnsignedInteger, "unsigned-integer", CommandCompletions::eNoCompletion, { nullptr, false }, "An unsigned integer." },
1198 { eArgTypeUnixSignal, "unix-signal", CommandCompletions::eNoCompletion, { nullptr, false }, "A valid Unix signal name or number (e.g. SIGKILL, KILL or 9)." },
1199 { eArgTypeVarName, "variable-name", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of a variable in your program." },
1200 { eArgTypeValue, "value", CommandCompletions::eNoCompletion, { nullptr, false }, "A value could be anything, depending on where and how it is used." },
1201 { eArgTypeWidth, "width", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1202 { eArgTypeNone, "none", CommandCompletions::eNoCompletion, { nullptr, false }, "No help available for this." },
1203 { eArgTypePlatform, "platform-name", CommandCompletions::ePlatformPluginCompletion, { nullptr, false }, "The name of an installed platform plug-in . Type 'platform list' to see a complete list of installed platforms." },
1204 { eArgTypeWatchpointID, "watchpt-id", CommandCompletions::eNoCompletion, { nullptr, false }, "Watchpoint IDs are positive integers." },
1205 { eArgTypeWatchpointIDRange, "watchpt-id-list", CommandCompletions::eNoCompletion, { nullptr, false }, "For example, '1-3' or '1 to 3'." },
1206 { eArgTypeWatchType, "watch-type", CommandCompletions::eNoCompletion, { nullptr, false }, "Specify the type for a watchpoint." }
Caroline Ticee139cf22010-10-01 17:46:38 +00001207};
1208
1209const CommandObject::ArgumentTableEntry*
1210CommandObject::GetArgumentTable ()
1211{
Greg Clayton9d0402b2011-02-20 02:15:07 +00001212 // If this assertion fires, then the table above is out of date with the CommandArgumentType enumeration
1213 assert ((sizeof (CommandObject::g_arguments_data) / sizeof (CommandObject::ArgumentTableEntry)) == eArgTypeLastArg);
Caroline Ticee139cf22010-10-01 17:46:38 +00001214 return CommandObject::g_arguments_data;
1215}
1216
1217