blob: 3fdbf994fe7aebd688dbaeaab31ff2cca012546a [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"
28#include "lldb/Target/Process.h"
29#include "lldb/Target/Target.h"
30
31#include "lldb/Interpreter/CommandInterpreter.h"
32#include "lldb/Interpreter/CommandReturnObject.h"
33#include "lldb/Interpreter/ScriptInterpreter.h"
34#include "lldb/Interpreter/ScriptInterpreterPython.h"
35
36using namespace lldb;
37using namespace lldb_private;
38
39//-------------------------------------------------------------------------
40// CommandObject
41//-------------------------------------------------------------------------
42
Greg Claytona7015092010-09-18 01:14:36 +000043CommandObject::CommandObject
44(
45 CommandInterpreter &interpreter,
46 const char *name,
47 const char *help,
48 const char *syntax,
49 uint32_t flags
50) :
51 m_interpreter (interpreter),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052 m_cmd_name (name),
53 m_cmd_help_short (),
54 m_cmd_help_long (),
55 m_cmd_syntax (),
Jim Ingham279a6c22010-07-06 22:46:59 +000056 m_is_alias (false),
Caroline Ticee139cf22010-10-01 17:46:38 +000057 m_flags (flags),
Greg Claytona9f7b792012-02-29 04:21:24 +000058 m_arguments(),
Jim Ingham6d8873f2014-08-06 00:24:38 +000059 m_deprecated_command_override_callback (nullptr),
Ed Masted78c9572014-04-20 00:31:37 +000060 m_command_override_callback (nullptr),
61 m_command_override_baton (nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000062{
63 if (help && help[0])
64 m_cmd_help_short = help;
65 if (syntax && syntax[0])
66 m_cmd_syntax = syntax;
67}
68
69CommandObject::~CommandObject ()
70{
71}
72
73const char *
74CommandObject::GetHelp ()
75{
76 return m_cmd_help_short.c_str();
77}
78
79const char *
80CommandObject::GetHelpLong ()
81{
82 return m_cmd_help_long.c_str();
83}
84
85const char *
86CommandObject::GetSyntax ()
87{
Caroline Ticee139cf22010-10-01 17:46:38 +000088 if (m_cmd_syntax.length() == 0)
89 {
90 StreamString syntax_str;
91 syntax_str.Printf ("%s", GetCommandName());
Ed Masted78c9572014-04-20 00:31:37 +000092 if (GetOptions() != nullptr)
Caroline Tice405fe672010-10-04 22:28:36 +000093 syntax_str.Printf (" <cmd-options>");
Caroline Ticee139cf22010-10-01 17:46:38 +000094 if (m_arguments.size() > 0)
95 {
96 syntax_str.Printf (" ");
Enrico Granataca5acdb2013-06-18 01:17:46 +000097 if (WantsRawCommandString() && GetOptions() && GetOptions()->NumCommandOptions())
Sean Callanana4c6ad12012-01-04 19:11:25 +000098 syntax_str.Printf("-- ");
Caroline Ticee139cf22010-10-01 17:46:38 +000099 GetFormattedCommandArguments (syntax_str);
100 }
101 m_cmd_syntax = syntax_str.GetData ();
102 }
103
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000104 return m_cmd_syntax.c_str();
105}
106
107const char *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000108CommandObject::GetCommandName ()
109{
110 return m_cmd_name.c_str();
111}
112
113void
114CommandObject::SetCommandName (const char *name)
115{
116 m_cmd_name = name;
117}
118
119void
120CommandObject::SetHelp (const char *cstr)
121{
122 m_cmd_help_short = cstr;
123}
124
125void
126CommandObject::SetHelpLong (const char *cstr)
127{
128 m_cmd_help_long = cstr;
129}
130
131void
Enrico Granata99f0b8f2011-08-17 01:30:04 +0000132CommandObject::SetHelpLong (std::string str)
133{
134 m_cmd_help_long = str;
135}
136
137void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000138CommandObject::SetSyntax (const char *cstr)
139{
140 m_cmd_syntax = cstr;
141}
142
143Options *
144CommandObject::GetOptions ()
145{
146 // By default commands don't have options unless this virtual function
147 // is overridden by base classes.
Ed Masted78c9572014-04-20 00:31:37 +0000148 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000149}
150
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000151bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000152CommandObject::ParseOptions
153(
154 Args& args,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000155 CommandReturnObject &result
156)
157{
158 // See if the subclass has options?
159 Options *options = GetOptions();
Ed Masted78c9572014-04-20 00:31:37 +0000160 if (options != nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000161 {
162 Error error;
Greg Claytonf6b8b582011-04-13 00:18:08 +0000163 options->NotifyOptionParsingStarting();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000164
Greg Claytonb7ad58a2013-04-04 20:35:24 +0000165 // 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 +0000166 // so we need to push a dummy value into position zero.
167 args.Unshift("dummy_string");
168 error = args.ParseOptions (*options);
169
170 // The "dummy_string" will have already been removed by ParseOptions,
171 // so no need to remove it.
172
Greg Claytonf6b8b582011-04-13 00:18:08 +0000173 if (error.Success())
174 error = options->NotifyOptionParsingFinished();
175
176 if (error.Success())
177 {
178 if (options->VerifyOptions (result))
179 return true;
180 }
181 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000182 {
183 const char *error_cstr = error.AsCString();
184 if (error_cstr)
185 {
186 // We got an error string, lets use that
Greg Clayton86edbf42011-10-26 00:56:27 +0000187 result.AppendError(error_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000188 }
189 else
190 {
191 // No error string, output the usage information into result
Greg Claytoneb0103f2011-04-07 22:46:35 +0000192 options->GenerateOptionUsage (result.GetErrorStream(), this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000193 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000194 }
Greg Claytonf6b8b582011-04-13 00:18:08 +0000195 result.SetStatus (eReturnStatusFailed);
196 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000197 }
198 return true;
199}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000200
Jim Ingham5a988412012-06-08 21:56:10 +0000201
202
203bool
Greg Claytonf9fc6092013-01-09 19:44:40 +0000204CommandObject::CheckRequirements (CommandReturnObject &result)
Jim Ingham5a988412012-06-08 21:56:10 +0000205{
Greg Claytonf9fc6092013-01-09 19:44:40 +0000206#ifdef LLDB_CONFIGURATION_DEBUG
207 // Nothing should be stored in m_exe_ctx between running commands as m_exe_ctx
208 // has shared pointers to the target, process, thread and frame and we don't
209 // want any CommandObject instances to keep any of these objects around
210 // longer than for a single command. Every command should call
211 // CommandObject::Cleanup() after it has completed
212 assert (m_exe_ctx.GetTargetPtr() == NULL);
213 assert (m_exe_ctx.GetProcessPtr() == NULL);
214 assert (m_exe_ctx.GetThreadPtr() == NULL);
215 assert (m_exe_ctx.GetFramePtr() == NULL);
216#endif
217
218 // Lock down the interpreter's execution context prior to running the
219 // command so we guarantee the selected target, process, thread and frame
220 // can't go away during the execution
221 m_exe_ctx = m_interpreter.GetExecutionContext();
222
223 const uint32_t flags = GetFlags().Get();
224 if (flags & (eFlagRequiresTarget |
225 eFlagRequiresProcess |
226 eFlagRequiresThread |
227 eFlagRequiresFrame |
228 eFlagTryTargetAPILock ))
229 {
230
231 if ((flags & eFlagRequiresTarget) && !m_exe_ctx.HasTargetScope())
232 {
233 result.AppendError (GetInvalidTargetDescription());
234 return false;
235 }
236
237 if ((flags & eFlagRequiresProcess) && !m_exe_ctx.HasProcessScope())
238 {
239 result.AppendError (GetInvalidProcessDescription());
240 return false;
241 }
242
243 if ((flags & eFlagRequiresThread) && !m_exe_ctx.HasThreadScope())
244 {
245 result.AppendError (GetInvalidThreadDescription());
246 return false;
247 }
248
249 if ((flags & eFlagRequiresFrame) && !m_exe_ctx.HasFrameScope())
250 {
251 result.AppendError (GetInvalidFrameDescription());
252 return false;
253 }
254
Ed Masted78c9572014-04-20 00:31:37 +0000255 if ((flags & eFlagRequiresRegContext) && (m_exe_ctx.GetRegisterContext() == nullptr))
Greg Claytonf9fc6092013-01-09 19:44:40 +0000256 {
257 result.AppendError (GetInvalidRegContextDescription());
258 return false;
259 }
260
261 if (flags & eFlagTryTargetAPILock)
262 {
263 Target *target = m_exe_ctx.GetTargetPtr();
264 if (target)
Greg Clayton9b5442a2014-02-07 22:31:20 +0000265 m_api_locker.Lock (target->GetAPIMutex());
Greg Claytonf9fc6092013-01-09 19:44:40 +0000266 }
267 }
268
Greg Claytonb766a732011-02-04 01:58:07 +0000269 if (GetFlags().AnySet (CommandObject::eFlagProcessMustBeLaunched | CommandObject::eFlagProcessMustBePaused))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000270 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000271 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Ed Masted78c9572014-04-20 00:31:37 +0000272 if (process == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000273 {
Jim Inghamb8e8a5f2011-07-09 00:55:34 +0000274 // A process that is not running is considered paused.
275 if (GetFlags().Test(CommandObject::eFlagProcessMustBeLaunched))
276 {
277 result.AppendError ("Process must exist.");
278 result.SetStatus (eReturnStatusFailed);
279 return false;
280 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000281 }
Greg Claytonb766a732011-02-04 01:58:07 +0000282 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000283 {
Greg Claytonb766a732011-02-04 01:58:07 +0000284 StateType state = process->GetState();
Greg Claytonb766a732011-02-04 01:58:07 +0000285 switch (state)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000286 {
Greg Clayton7a5388b2011-03-20 04:57:14 +0000287 case eStateInvalid:
Greg Claytonb766a732011-02-04 01:58:07 +0000288 case eStateSuspended:
289 case eStateCrashed:
290 case eStateStopped:
291 break;
292
293 case eStateConnected:
294 case eStateAttaching:
295 case eStateLaunching:
296 case eStateDetached:
297 case eStateExited:
298 case eStateUnloaded:
299 if (GetFlags().Test(CommandObject::eFlagProcessMustBeLaunched))
300 {
301 result.AppendError ("Process must be launched.");
302 result.SetStatus (eReturnStatusFailed);
303 return false;
304 }
305 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000306
Greg Claytonb766a732011-02-04 01:58:07 +0000307 case eStateRunning:
308 case eStateStepping:
309 if (GetFlags().Test(CommandObject::eFlagProcessMustBePaused))
310 {
311 result.AppendError ("Process is running. Use 'process interrupt' to pause execution.");
312 result.SetStatus (eReturnStatusFailed);
313 return false;
314 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000315 }
316 }
317 }
Jim Ingham5a988412012-06-08 21:56:10 +0000318 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000319}
320
Greg Claytonf9fc6092013-01-09 19:44:40 +0000321void
322CommandObject::Cleanup ()
323{
324 m_exe_ctx.Clear();
325 m_api_locker.Unlock();
326}
327
328
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000329class CommandDictCommandPartialMatch
330{
331 public:
332 CommandDictCommandPartialMatch (const char *match_str)
333 {
334 m_match_str = match_str;
335 }
336 bool operator() (const std::pair<std::string, lldb::CommandObjectSP> map_element) const
337 {
338 // A NULL or empty string matches everything.
Ed Masted78c9572014-04-20 00:31:37 +0000339 if (m_match_str == nullptr || *m_match_str == '\0')
Greg Claytonc7bece562013-01-25 18:06:21 +0000340 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000341
Greg Claytonc7bece562013-01-25 18:06:21 +0000342 return map_element.first.find (m_match_str, 0) == 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000343 }
344
345 private:
346 const char *m_match_str;
347};
348
349int
350CommandObject::AddNamesMatchingPartialString (CommandObject::CommandMap &in_map, const char *cmd_str,
351 StringList &matches)
352{
353 int number_added = 0;
354 CommandDictCommandPartialMatch matcher(cmd_str);
355
356 CommandObject::CommandMap::iterator matching_cmds = std::find_if (in_map.begin(), in_map.end(), matcher);
357
358 while (matching_cmds != in_map.end())
359 {
360 ++number_added;
361 matches.AppendString((*matching_cmds).first.c_str());
362 matching_cmds = std::find_if (++matching_cmds, in_map.end(), matcher);;
363 }
364 return number_added;
365}
366
367int
368CommandObject::HandleCompletion
369(
370 Args &input,
371 int &cursor_index,
372 int &cursor_char_position,
373 int match_start_point,
374 int max_return_elements,
Jim Ingham558ce122010-06-30 05:02:46 +0000375 bool &word_complete,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000376 StringList &matches
377)
378{
Johnny Chen6561d152012-01-20 00:59:19 +0000379 // Default implmentation of WantsCompletion() is !WantsRawCommandString().
380 // Subclasses who want raw command string but desire, for example,
381 // argument completion should override WantsCompletion() to return true,
382 // instead.
Johnny Chen6f99b632012-01-19 22:16:06 +0000383 if (WantsRawCommandString() && !WantsCompletion())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000384 {
385 // FIXME: Abstract telling the completion to insert the completion character.
386 matches.Clear();
387 return -1;
388 }
389 else
390 {
391 // Can we do anything generic with the options?
392 Options *cur_options = GetOptions();
393 CommandReturnObject result;
394 OptionElementVector opt_element_vector;
395
Ed Masted78c9572014-04-20 00:31:37 +0000396 if (cur_options != nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000397 {
398 // Re-insert the dummy command name string which will have been
399 // stripped off:
400 input.Unshift ("dummy-string");
401 cursor_index++;
402
403
404 // I stick an element on the end of the input, because if the last element is
Greg Claytonb7ad58a2013-04-04 20:35:24 +0000405 // option that requires an argument, getopt_long_only will freak out.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000406
407 input.AppendArgument ("<FAKE-VALUE>");
408
Jim Inghamd43e0092010-06-24 20:31:04 +0000409 input.ParseArgsForCompletion (*cur_options, opt_element_vector, cursor_index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000410
411 input.DeleteArgumentAtIndex(input.GetArgumentCount() - 1);
412
413 bool handled_by_options;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000414 handled_by_options = cur_options->HandleOptionCompletion (input,
Greg Clayton66111032010-06-23 01:19:29 +0000415 opt_element_vector,
416 cursor_index,
417 cursor_char_position,
418 match_start_point,
419 max_return_elements,
Jim Ingham558ce122010-06-30 05:02:46 +0000420 word_complete,
Greg Clayton66111032010-06-23 01:19:29 +0000421 matches);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000422 if (handled_by_options)
423 return matches.GetSize();
424 }
425
426 // If we got here, the last word is not an option or an option argument.
Greg Claytona7015092010-09-18 01:14:36 +0000427 return HandleArgumentCompletion (input,
Greg Clayton66111032010-06-23 01:19:29 +0000428 cursor_index,
429 cursor_char_position,
430 opt_element_vector,
431 match_start_point,
432 max_return_elements,
Jim Ingham558ce122010-06-30 05:02:46 +0000433 word_complete,
Greg Clayton66111032010-06-23 01:19:29 +0000434 matches);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000435 }
436}
437
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000438bool
Greg Claytona7015092010-09-18 01:14:36 +0000439CommandObject::HelpTextContainsWord (const char *search_word)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000440{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000441 std::string options_usage_help;
442
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000443 bool found_word = false;
444
Greg Clayton998255b2012-10-13 02:07:45 +0000445 const char *short_help = GetHelp();
446 const char *long_help = GetHelpLong();
447 const char *syntax_help = GetSyntax();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000448
Greg Clayton998255b2012-10-13 02:07:45 +0000449 if (short_help && strcasestr (short_help, search_word))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000450 found_word = true;
Greg Clayton998255b2012-10-13 02:07:45 +0000451 else if (long_help && strcasestr (long_help, search_word))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000452 found_word = true;
Greg Clayton998255b2012-10-13 02:07:45 +0000453 else if (syntax_help && strcasestr (syntax_help, search_word))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000454 found_word = true;
455
456 if (!found_word
Ed Masted78c9572014-04-20 00:31:37 +0000457 && GetOptions() != nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000458 {
459 StreamString usage_help;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000460 GetOptions()->GenerateOptionUsage (usage_help, this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000461 if (usage_help.GetSize() > 0)
462 {
463 const char *usage_text = usage_help.GetData();
Caroline Tice4b6fbf32010-10-12 22:16:53 +0000464 if (strcasestr (usage_text, search_word))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000465 found_word = true;
466 }
467 }
468
469 return found_word;
470}
Caroline Ticee139cf22010-10-01 17:46:38 +0000471
472int
473CommandObject::GetNumArgumentEntries ()
474{
475 return m_arguments.size();
476}
477
478CommandObject::CommandArgumentEntry *
479CommandObject::GetArgumentEntryAtIndex (int idx)
480{
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +0000481 if (static_cast<size_t>(idx) < m_arguments.size())
Caroline Ticee139cf22010-10-01 17:46:38 +0000482 return &(m_arguments[idx]);
483
Ed Masted78c9572014-04-20 00:31:37 +0000484 return nullptr;
Caroline Ticee139cf22010-10-01 17:46:38 +0000485}
486
487CommandObject::ArgumentTableEntry *
488CommandObject::FindArgumentDataByType (CommandArgumentType arg_type)
489{
490 const ArgumentTableEntry *table = CommandObject::GetArgumentTable();
491
492 for (int i = 0; i < eArgTypeLastArg; ++i)
493 if (table[i].arg_type == arg_type)
494 return (ArgumentTableEntry *) &(table[i]);
495
Ed Masted78c9572014-04-20 00:31:37 +0000496 return nullptr;
Caroline Ticee139cf22010-10-01 17:46:38 +0000497}
498
499void
500CommandObject::GetArgumentHelp (Stream &str, CommandArgumentType arg_type, CommandInterpreter &interpreter)
501{
502 const ArgumentTableEntry* table = CommandObject::GetArgumentTable();
503 ArgumentTableEntry *entry = (ArgumentTableEntry *) &(table[arg_type]);
504
505 // The table is *supposed* to be kept in arg_type order, but someone *could* have messed it up...
506
507 if (entry->arg_type != arg_type)
508 entry = CommandObject::FindArgumentDataByType (arg_type);
509
510 if (!entry)
511 return;
512
513 StreamString name_str;
514 name_str.Printf ("<%s>", entry->arg_name);
515
Enrico Granatafc7a7f32011-07-08 02:51:01 +0000516 if (entry->help_function)
Enrico Granata82a7d982011-07-07 00:38:40 +0000517 {
Enrico Granatafc7a7f32011-07-08 02:51:01 +0000518 const char* help_text = entry->help_function();
Enrico Granata82a7d982011-07-07 00:38:40 +0000519 if (!entry->help_function.self_formatting)
520 {
521 interpreter.OutputFormattedHelpText (str, name_str.GetData(), "--", help_text,
522 name_str.GetSize());
523 }
524 else
525 {
526 interpreter.OutputHelpText(str, name_str.GetData(), "--", help_text,
527 name_str.GetSize());
528 }
529 }
Caroline Ticee139cf22010-10-01 17:46:38 +0000530 else
531 interpreter.OutputFormattedHelpText (str, name_str.GetData(), "--", entry->help_text, name_str.GetSize());
532}
533
534const char *
535CommandObject::GetArgumentName (CommandArgumentType arg_type)
536{
Caroline Ticedeaab222010-10-01 19:59:14 +0000537 ArgumentTableEntry *entry = (ArgumentTableEntry *) &(CommandObject::GetArgumentTable()[arg_type]);
538
539 // The table is *supposed* to be kept in arg_type order, but someone *could* have messed it up...
540
541 if (entry->arg_type != arg_type)
542 entry = CommandObject::FindArgumentDataByType (arg_type);
543
Johnny Chene6acf352010-10-08 22:01:52 +0000544 if (entry)
545 return entry->arg_name;
546
547 StreamString str;
548 str << "Arg name for type (" << arg_type << ") not in arg table!";
549 return str.GetData();
Caroline Ticee139cf22010-10-01 17:46:38 +0000550}
551
Caroline Tice405fe672010-10-04 22:28:36 +0000552bool
Greg Claytone0d378b2011-03-24 21:19:54 +0000553CommandObject::IsPairType (ArgumentRepetitionType arg_repeat_type)
Caroline Tice405fe672010-10-04 22:28:36 +0000554{
555 if ((arg_repeat_type == eArgRepeatPairPlain)
556 || (arg_repeat_type == eArgRepeatPairOptional)
557 || (arg_repeat_type == eArgRepeatPairPlus)
558 || (arg_repeat_type == eArgRepeatPairStar)
559 || (arg_repeat_type == eArgRepeatPairRange)
560 || (arg_repeat_type == eArgRepeatPairRangeOptional))
561 return true;
562
563 return false;
564}
565
Johnny Chen34ddc8d2012-02-08 01:13:31 +0000566static CommandObject::CommandArgumentEntry
567OptSetFiltered(uint32_t opt_set_mask, CommandObject::CommandArgumentEntry &cmd_arg_entry)
568{
569 CommandObject::CommandArgumentEntry ret_val;
570 for (unsigned i = 0; i < cmd_arg_entry.size(); ++i)
571 if (opt_set_mask & cmd_arg_entry[i].arg_opt_set_association)
572 ret_val.push_back(cmd_arg_entry[i]);
573 return ret_val;
574}
575
576// Default parameter value of opt_set_mask is LLDB_OPT_SET_ALL, which means take
577// all the argument data into account. On rare cases where some argument sticks
578// with certain option sets, this function returns the option set filtered args.
Caroline Ticee139cf22010-10-01 17:46:38 +0000579void
Johnny Chen34ddc8d2012-02-08 01:13:31 +0000580CommandObject::GetFormattedCommandArguments (Stream &str, uint32_t opt_set_mask)
Caroline Ticee139cf22010-10-01 17:46:38 +0000581{
582 int num_args = m_arguments.size();
583 for (int i = 0; i < num_args; ++i)
584 {
585 if (i > 0)
586 str.Printf (" ");
Johnny Chen34ddc8d2012-02-08 01:13:31 +0000587 CommandArgumentEntry arg_entry =
588 opt_set_mask == LLDB_OPT_SET_ALL ? m_arguments[i]
589 : OptSetFiltered(opt_set_mask, m_arguments[i]);
Caroline Ticee139cf22010-10-01 17:46:38 +0000590 int num_alternatives = arg_entry.size();
Caroline Tice405fe672010-10-04 22:28:36 +0000591
592 if ((num_alternatives == 2)
593 && IsPairType (arg_entry[0].arg_repetition))
Caroline Ticee139cf22010-10-01 17:46:38 +0000594 {
Caroline Tice405fe672010-10-04 22:28:36 +0000595 const char *first_name = GetArgumentName (arg_entry[0].arg_type);
596 const char *second_name = GetArgumentName (arg_entry[1].arg_type);
597 switch (arg_entry[0].arg_repetition)
598 {
599 case eArgRepeatPairPlain:
600 str.Printf ("<%s> <%s>", first_name, second_name);
601 break;
602 case eArgRepeatPairOptional:
603 str.Printf ("[<%s> <%s>]", first_name, second_name);
604 break;
605 case eArgRepeatPairPlus:
606 str.Printf ("<%s> <%s> [<%s> <%s> [...]]", first_name, second_name, first_name, second_name);
607 break;
608 case eArgRepeatPairStar:
609 str.Printf ("[<%s> <%s> [<%s> <%s> [...]]]", first_name, second_name, first_name, second_name);
610 break;
611 case eArgRepeatPairRange:
612 str.Printf ("<%s_1> <%s_1> ... <%s_n> <%s_n>", first_name, second_name, first_name, second_name);
613 break;
614 case eArgRepeatPairRangeOptional:
615 str.Printf ("[<%s_1> <%s_1> ... <%s_n> <%s_n>]", first_name, second_name, first_name, second_name);
616 break;
Caroline Ticeca1176a2011-03-23 22:31:13 +0000617 // Explicitly test for all the rest of the cases, so if new types get added we will notice the
618 // missing case statement(s).
619 case eArgRepeatPlain:
620 case eArgRepeatOptional:
621 case eArgRepeatPlus:
622 case eArgRepeatStar:
623 case eArgRepeatRange:
624 // These should not be reached, as they should fail the IsPairType test above.
625 break;
Caroline Tice405fe672010-10-04 22:28:36 +0000626 }
Caroline Ticee139cf22010-10-01 17:46:38 +0000627 }
Caroline Tice405fe672010-10-04 22:28:36 +0000628 else
Caroline Ticee139cf22010-10-01 17:46:38 +0000629 {
Caroline Tice405fe672010-10-04 22:28:36 +0000630 StreamString names;
631 for (int j = 0; j < num_alternatives; ++j)
632 {
633 if (j > 0)
634 names.Printf (" | ");
635 names.Printf ("%s", GetArgumentName (arg_entry[j].arg_type));
636 }
637 switch (arg_entry[0].arg_repetition)
638 {
639 case eArgRepeatPlain:
640 str.Printf ("<%s>", names.GetData());
641 break;
642 case eArgRepeatPlus:
643 str.Printf ("<%s> [<%s> [...]]", names.GetData(), names.GetData());
644 break;
645 case eArgRepeatStar:
646 str.Printf ("[<%s> [<%s> [...]]]", names.GetData(), names.GetData());
647 break;
648 case eArgRepeatOptional:
649 str.Printf ("[<%s>]", names.GetData());
650 break;
651 case eArgRepeatRange:
Jason Molendafd54b362011-09-20 21:44:10 +0000652 str.Printf ("<%s_1> .. <%s_n>", names.GetData(), names.GetData());
Caroline Ticeca1176a2011-03-23 22:31:13 +0000653 break;
654 // Explicitly test for all the rest of the cases, so if new types get added we will notice the
655 // missing case statement(s).
656 case eArgRepeatPairPlain:
657 case eArgRepeatPairOptional:
658 case eArgRepeatPairPlus:
659 case eArgRepeatPairStar:
660 case eArgRepeatPairRange:
661 case eArgRepeatPairRangeOptional:
662 // These should not be hit, as they should pass the IsPairType test above, and control should
663 // have gone into the other branch of the if statement.
664 break;
Caroline Tice405fe672010-10-04 22:28:36 +0000665 }
Caroline Ticee139cf22010-10-01 17:46:38 +0000666 }
667 }
668}
669
Stephen Wilson0c16aa62011-03-23 02:12:10 +0000670CommandArgumentType
Caroline Ticee139cf22010-10-01 17:46:38 +0000671CommandObject::LookupArgumentName (const char *arg_name)
672{
673 CommandArgumentType return_type = eArgTypeLastArg;
674
675 std::string arg_name_str (arg_name);
676 size_t len = arg_name_str.length();
677 if (arg_name[0] == '<'
678 && arg_name[len-1] == '>')
679 arg_name_str = arg_name_str.substr (1, len-2);
680
Johnny Chen331eff32011-07-14 22:20:12 +0000681 const ArgumentTableEntry *table = GetArgumentTable();
Caroline Ticee139cf22010-10-01 17:46:38 +0000682 for (int i = 0; i < eArgTypeLastArg; ++i)
Johnny Chen331eff32011-07-14 22:20:12 +0000683 if (arg_name_str.compare (table[i].arg_name) == 0)
Caroline Ticee139cf22010-10-01 17:46:38 +0000684 return_type = g_arguments_data[i].arg_type;
685
686 return return_type;
687}
688
689static const char *
Jim Ingham931e6742012-08-23 23:37:31 +0000690RegisterNameHelpTextCallback ()
691{
692 return "Register names can be specified using the architecture specific names. "
Jim Ingham84c7bd72012-08-23 23:47:08 +0000693 "They can also be specified using generic names. Not all generic entities have "
694 "registers backing them on all architectures. When they don't the generic name "
695 "will return an error.\n"
Jim Ingham931e6742012-08-23 23:37:31 +0000696 "The generic names defined in lldb are:\n"
697 "\n"
698 "pc - program counter register\n"
699 "ra - return address register\n"
700 "fp - frame pointer register\n"
701 "sp - stack pointer register\n"
Jim Ingham84c7bd72012-08-23 23:47:08 +0000702 "flags - the flags register\n"
Jim Ingham931e6742012-08-23 23:37:31 +0000703 "arg{1-6} - integer argument passing registers.\n";
704}
705
706static const char *
Caroline Ticee139cf22010-10-01 17:46:38 +0000707BreakpointIDHelpTextCallback ()
708{
Greg Clayton86edbf42011-10-26 00:56:27 +0000709 return "Breakpoint ID's consist major and minor numbers; the major number "
710 "corresponds to the single entity that was created with a 'breakpoint set' "
711 "command; the minor numbers correspond to all the locations that were actually "
712 "found/set based on the major breakpoint. A full breakpoint ID might look like "
713 "3.14, meaning the 14th location set for the 3rd breakpoint. You can specify "
714 "all the locations of a breakpoint by just indicating the major breakpoint "
715 "number. A valid breakpoint id consists either of just the major id number, "
716 "or the major number, a dot, and the location number (e.g. 3 or 3.2 could "
717 "both be valid breakpoint ids).";
Caroline Ticee139cf22010-10-01 17:46:38 +0000718}
719
720static const char *
721BreakpointIDRangeHelpTextCallback ()
722{
Greg Clayton86edbf42011-10-26 00:56:27 +0000723 return "A 'breakpoint id list' is a manner of specifying multiple breakpoints. "
724 "This can be done through several mechanisms. The easiest way is to just "
725 "enter a space-separated list of breakpoint ids. To specify all the "
726 "breakpoint locations under a major breakpoint, you can use the major "
727 "breakpoint number followed by '.*', eg. '5.*' means all the locations under "
728 "breakpoint 5. You can also indicate a range of breakpoints by using "
729 "<start-bp-id> - <end-bp-id>. The start-bp-id and end-bp-id for a range can "
730 "be any valid breakpoint ids. It is not legal, however, to specify a range "
731 "using specific locations that cross major breakpoint numbers. I.e. 3.2 - 3.7"
732 " is legal; 2 - 5 is legal; but 3.2 - 4.4 is not legal.";
Caroline Ticee139cf22010-10-01 17:46:38 +0000733}
734
Enrico Granata0a3958e2011-07-02 00:25:22 +0000735static const char *
Greg Clayton86edbf42011-10-26 00:56:27 +0000736GDBFormatHelpTextCallback ()
737{
Greg Claytonf91381e2011-10-26 18:35:21 +0000738 return "A GDB format consists of a repeat count, a format letter and a size letter. "
739 "The repeat count is optional and defaults to 1. The format letter is optional "
740 "and defaults to the previous format that was used. The size letter is optional "
741 "and defaults to the previous size that was used.\n"
742 "\n"
743 "Format letters include:\n"
744 "o - octal\n"
745 "x - hexadecimal\n"
746 "d - decimal\n"
747 "u - unsigned decimal\n"
748 "t - binary\n"
749 "f - float\n"
750 "a - address\n"
751 "i - instruction\n"
752 "c - char\n"
753 "s - string\n"
754 "T - OSType\n"
755 "A - float as hex\n"
756 "\n"
757 "Size letters include:\n"
758 "b - 1 byte (byte)\n"
759 "h - 2 bytes (halfword)\n"
760 "w - 4 bytes (word)\n"
761 "g - 8 bytes (giant)\n"
762 "\n"
763 "Example formats:\n"
764 "32xb - show 32 1 byte hexadecimal integer values\n"
765 "16xh - show 16 2 byte hexadecimal integer values\n"
766 "64 - show 64 2 byte hexadecimal integer values (format and size from the last format)\n"
767 "dw - show 1 4 byte decimal integer value\n"
768 ;
Greg Clayton86edbf42011-10-26 00:56:27 +0000769}
770
771static const char *
Enrico Granata0a3958e2011-07-02 00:25:22 +0000772FormatHelpTextCallback ()
773{
Enrico Granata82a7d982011-07-07 00:38:40 +0000774
Ed Masted78c9572014-04-20 00:31:37 +0000775 static char* help_text_ptr = nullptr;
Enrico Granata82a7d982011-07-07 00:38:40 +0000776
777 if (help_text_ptr)
778 return help_text_ptr;
779
Enrico Granata0a3958e2011-07-02 00:25:22 +0000780 StreamString sstr;
781 sstr << "One of the format names (or one-character names) that can be used to show a variable's value:\n";
782 for (Format f = eFormatDefault; f < kNumFormats; f = Format(f+1))
783 {
Enrico Granata82a7d982011-07-07 00:38:40 +0000784 if (f != eFormatDefault)
785 sstr.PutChar('\n');
786
Enrico Granata0a3958e2011-07-02 00:25:22 +0000787 char format_char = FormatManager::GetFormatAsFormatChar(f);
788 if (format_char)
789 sstr.Printf("'%c' or ", format_char);
790
Enrico Granata82a7d982011-07-07 00:38:40 +0000791 sstr.Printf ("\"%s\"", FormatManager::GetFormatAsCString(f));
Enrico Granata0a3958e2011-07-02 00:25:22 +0000792 }
793
794 sstr.Flush();
795
796 std::string data = sstr.GetString();
797
Enrico Granata82a7d982011-07-07 00:38:40 +0000798 help_text_ptr = new char[data.length()+1];
Enrico Granata0a3958e2011-07-02 00:25:22 +0000799
Enrico Granata82a7d982011-07-07 00:38:40 +0000800 data.copy(help_text_ptr, data.length());
Enrico Granata0a3958e2011-07-02 00:25:22 +0000801
Enrico Granata82a7d982011-07-07 00:38:40 +0000802 return help_text_ptr;
Enrico Granata0a3958e2011-07-02 00:25:22 +0000803}
804
805static const char *
Sean Callanand9477392012-10-23 00:50:09 +0000806LanguageTypeHelpTextCallback ()
807{
Ed Masted78c9572014-04-20 00:31:37 +0000808 static char* help_text_ptr = nullptr;
Sean Callanand9477392012-10-23 00:50:09 +0000809
810 if (help_text_ptr)
811 return help_text_ptr;
812
813 StreamString sstr;
814 sstr << "One of the following languages:\n";
815
Daniel Malea48947c72012-12-04 00:23:45 +0000816 for (unsigned int l = eLanguageTypeUnknown; l < eNumLanguageTypes; ++l)
Sean Callanand9477392012-10-23 00:50:09 +0000817 {
Daniel Malea48947c72012-12-04 00:23:45 +0000818 sstr << " " << LanguageRuntime::GetNameForLanguageType(static_cast<LanguageType>(l)) << "\n";
Sean Callanand9477392012-10-23 00:50:09 +0000819 }
820
821 sstr.Flush();
822
823 std::string data = sstr.GetString();
824
825 help_text_ptr = new char[data.length()+1];
826
827 data.copy(help_text_ptr, data.length());
828
829 return help_text_ptr;
830}
831
832static const char *
Enrico Granata82a7d982011-07-07 00:38:40 +0000833SummaryStringHelpTextCallback()
Enrico Granata0a3958e2011-07-02 00:25:22 +0000834{
Enrico Granata82a7d982011-07-07 00:38:40 +0000835 return
836 "A summary string is a way to extract information from variables in order to present them using a summary.\n"
837 "Summary strings contain static text, variables, scopes and control sequences:\n"
838 " - Static text can be any sequence of non-special characters, i.e. anything but '{', '}', '$', or '\\'.\n"
839 " - Variables are sequences of characters beginning with ${, ending with } and that contain symbols in the format described below.\n"
840 " - 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"
841 " - Control sequences are the usual C/C++ '\\a', '\\n', ..., plus '\\$', '\\{' and '\\}'.\n"
842 "A summary string works by copying static text verbatim, turning control sequences into their character counterpart, expanding variables and trying to expand scopes.\n"
843 "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"
844 "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"
845 " (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 +0000846 " ${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."
847 " 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 +0000848 "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."
849 "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"
850 " path refers to:\n"
851 " - 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"
852 " and displayed as an individual variable\n"
853 " - 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"
854 " 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 +0000855 "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"
856 "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"
857 " special symbols only allowed as part of a variable:\n"
858 " %V: show the value of the object by default\n"
859 " %S: show the summary of the object by default\n"
860 " %@: show the runtime-provided object description (for Objective-C, it calls NSPrintForDebugger; for C/C++ it does nothing)\n"
861 " %L: show the location of the object (memory address or a register name)\n"
862 " %#: show the number of children of the object\n"
863 " %T: show the type of the object\n"
864 "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"
865 " 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"
866 " count the number of actual elements stored in an std::list:\n"
867 "type summary add -s \"${svar%#}\" -x \"std::list<\"";
868}
869
870static const char *
871ExprPathHelpTextCallback()
872{
873 return
874 "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"
875 "For instance, given a class:\n"
876 " class foo {\n"
877 " int a;\n"
878 " int b; .\n"
879 " foo* next;\n"
880 " };\n"
881 "the expression to read item b in the item pointed to by next for foo aFoo would be aFoo.next->b.\n"
882 "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"
883 "Expression paths in LLDB include dot (.) and arrow (->) operators, and most commands using expression paths have ways to also accept the star (*) operator.\n"
884 "The meaning of these operators is the same as the usual one given to them by the C/C++ standards.\n"
885 "LLDB also has support for indexing ([ ]) in expression paths, and extends the traditional meaning of the square brackets operator to allow bitfield extraction:\n"
886 "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"
887 " 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"
888 " 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"
889 " meaning of array slicing (taking elements n thru m inside the array or pointed-to memory).";
Enrico Granata0a3958e2011-07-02 00:25:22 +0000890}
891
Johnny Chen184d7a72011-09-21 01:00:02 +0000892void
Enrico Granata9b62d1d2013-06-12 01:50:57 +0000893CommandObject::GenerateHelpText (CommandReturnObject &result)
894{
895 GenerateHelpText(result.GetOutputStream());
896
897 result.SetStatus (eReturnStatusSuccessFinishNoResult);
898}
899
900void
901CommandObject::GenerateHelpText (Stream &output_strm)
902{
903 CommandInterpreter& interpreter = GetCommandInterpreter();
Ed Masted78c9572014-04-20 00:31:37 +0000904 if (GetOptions() != nullptr)
Enrico Granata9b62d1d2013-06-12 01:50:57 +0000905 {
906 if (WantsRawCommandString())
907 {
908 std::string help_text (GetHelp());
909 help_text.append (" This command takes 'raw' input (no need to quote stuff).");
910 interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1);
911 }
912 else
913 interpreter.OutputFormattedHelpText (output_strm, "", "", GetHelp(), 1);
914 output_strm.Printf ("\nSyntax: %s\n", GetSyntax());
915 GetOptions()->GenerateOptionUsage (output_strm, this);
916 const char *long_help = GetHelpLong();
Ed Masted78c9572014-04-20 00:31:37 +0000917 if ((long_help != nullptr)
Enrico Granata9b62d1d2013-06-12 01:50:57 +0000918 && (strlen (long_help) > 0))
919 output_strm.Printf ("\n%s", long_help);
920 if (WantsRawCommandString() && !WantsCompletion())
921 {
922 // Emit the message about using ' -- ' between the end of the command options and the raw input
923 // conditionally, i.e., only if the command object does not want completion.
924 interpreter.OutputFormattedHelpText (output_strm, "", "",
925 "\nIMPORTANT NOTE: Because this command takes 'raw' input, if you use any command options"
926 " you must use ' -- ' between the end of the command options and the beginning of the raw input.", 1);
927 }
928 else if (GetNumArgumentEntries() > 0
929 && GetOptions()
930 && GetOptions()->NumCommandOptions() > 0)
931 {
932 // Also emit a warning about using "--" in case you are using a command that takes options and arguments.
933 interpreter.OutputFormattedHelpText (output_strm, "", "",
934 "\nThis command takes options and free-form arguments. If your arguments resemble"
935 " option specifiers (i.e., they start with a - or --), you must use ' -- ' between"
936 " the end of the command options and the beginning of the arguments.", 1);
937 }
938 }
939 else if (IsMultiwordObject())
940 {
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 GenerateHelpText (output_strm);
950 }
951 else
952 {
953 const char *long_help = GetHelpLong();
Ed Masted78c9572014-04-20 00:31:37 +0000954 if ((long_help != nullptr)
Enrico Granata9b62d1d2013-06-12 01:50:57 +0000955 && (strlen (long_help) > 0))
956 output_strm.Printf ("%s", long_help);
957 else if (WantsRawCommandString())
958 {
959 std::string help_text (GetHelp());
960 help_text.append (" This command takes 'raw' input (no need to quote stuff).");
961 interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1);
962 }
963 else
964 interpreter.OutputFormattedHelpText (output_strm, "", "", GetHelp(), 1);
965 output_strm.Printf ("\nSyntax: %s\n", GetSyntax());
966 }
967}
968
969void
Johnny Chende753462011-09-22 22:34:09 +0000970CommandObject::AddIDsArgumentData(CommandArgumentEntry &arg, CommandArgumentType ID, CommandArgumentType IDRange)
Johnny Chen184d7a72011-09-21 01:00:02 +0000971{
972 CommandArgumentData id_arg;
973 CommandArgumentData id_range_arg;
974
975 // Create the first variant for the first (and only) argument for this command.
Johnny Chende753462011-09-22 22:34:09 +0000976 id_arg.arg_type = ID;
Johnny Chen184d7a72011-09-21 01:00:02 +0000977 id_arg.arg_repetition = eArgRepeatOptional;
978
979 // Create the second variant for the first (and only) argument for this command.
Johnny Chende753462011-09-22 22:34:09 +0000980 id_range_arg.arg_type = IDRange;
Johnny Chen184d7a72011-09-21 01:00:02 +0000981 id_range_arg.arg_repetition = eArgRepeatOptional;
982
Johnny Chena3234732011-09-21 01:04:49 +0000983 // The first (and only) argument for this command could be either an id or an id_range.
Johnny Chen184d7a72011-09-21 01:00:02 +0000984 // Push both variants into the entry for the first argument for this command.
985 arg.push_back(id_arg);
986 arg.push_back(id_range_arg);
987}
988
Greg Clayton9d0402b2011-02-20 02:15:07 +0000989const char *
990CommandObject::GetArgumentTypeAsCString (const lldb::CommandArgumentType arg_type)
991{
992 if (arg_type >=0 && arg_type < eArgTypeLastArg)
993 return g_arguments_data[arg_type].arg_name;
Ed Masted78c9572014-04-20 00:31:37 +0000994 return nullptr;
Greg Clayton9d0402b2011-02-20 02:15:07 +0000995
996}
997
998const char *
999CommandObject::GetArgumentDescriptionAsCString (const lldb::CommandArgumentType arg_type)
1000{
1001 if (arg_type >=0 && arg_type < eArgTypeLastArg)
1002 return g_arguments_data[arg_type].help_text;
Ed Masted78c9572014-04-20 00:31:37 +00001003 return nullptr;
Greg Clayton9d0402b2011-02-20 02:15:07 +00001004}
1005
Jim Ingham5a988412012-06-08 21:56:10 +00001006bool
1007CommandObjectParsed::Execute (const char *args_string, CommandReturnObject &result)
1008{
Jim Ingham5a988412012-06-08 21:56:10 +00001009 bool handled = false;
1010 Args cmd_args (args_string);
Jim Ingham3b652622014-08-06 00:10:12 +00001011 if (HasOverrideCallback())
Jim Ingham5a988412012-06-08 21:56:10 +00001012 {
1013 Args full_args (GetCommandName ());
1014 full_args.AppendArguments(cmd_args);
Jim Ingham3b652622014-08-06 00:10:12 +00001015 handled = InvokeOverrideCallback (full_args.GetConstArgumentVector(), result);
Jim Ingham5a988412012-06-08 21:56:10 +00001016 }
1017 if (!handled)
1018 {
1019 for (size_t i = 0; i < cmd_args.GetArgumentCount(); ++i)
1020 {
1021 const char *tmp_str = cmd_args.GetArgumentAtIndex (i);
1022 if (tmp_str[0] == '`') // back-quote
1023 cmd_args.ReplaceArgumentAtIndex (i, m_interpreter.ProcessEmbeddedScriptCommands (tmp_str));
1024 }
1025
Greg Claytonf9fc6092013-01-09 19:44:40 +00001026 if (CheckRequirements(result))
1027 {
1028 if (ParseOptions (cmd_args, result))
1029 {
1030 // Call the command-specific version of 'Execute', passing it the already processed arguments.
1031 handled = DoExecute (cmd_args, result);
1032 }
1033 }
Jim Ingham5a988412012-06-08 21:56:10 +00001034
Greg Claytonf9fc6092013-01-09 19:44:40 +00001035 Cleanup();
Jim Ingham5a988412012-06-08 21:56:10 +00001036 }
1037 return handled;
1038}
1039
1040bool
1041CommandObjectRaw::Execute (const char *args_string, CommandReturnObject &result)
1042{
Jim Ingham5a988412012-06-08 21:56:10 +00001043 bool handled = false;
Jim Ingham3b652622014-08-06 00:10:12 +00001044 if (HasOverrideCallback())
Jim Ingham5a988412012-06-08 21:56:10 +00001045 {
1046 std::string full_command (GetCommandName ());
1047 full_command += ' ';
1048 full_command += args_string;
Ed Masted78c9572014-04-20 00:31:37 +00001049 const char *argv[2] = { nullptr, nullptr };
Jim Ingham5a988412012-06-08 21:56:10 +00001050 argv[0] = full_command.c_str();
Jim Ingham3b652622014-08-06 00:10:12 +00001051 handled = InvokeOverrideCallback (argv, result);
Jim Ingham5a988412012-06-08 21:56:10 +00001052 }
1053 if (!handled)
1054 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00001055 if (CheckRequirements(result))
Jim Ingham5a988412012-06-08 21:56:10 +00001056 handled = DoExecute (args_string, result);
Greg Claytonf9fc6092013-01-09 19:44:40 +00001057
1058 Cleanup();
Jim Ingham5a988412012-06-08 21:56:10 +00001059 }
1060 return handled;
1061}
1062
Johnny Chenca7835c2012-05-26 00:32:39 +00001063static
1064const char *arch_helper()
1065{
Greg Claytond70b14e2012-05-26 17:21:14 +00001066 static StreamString g_archs_help;
Johnny Chen797a1b32012-05-29 20:04:10 +00001067 if (g_archs_help.Empty())
Greg Claytond70b14e2012-05-26 17:21:14 +00001068 {
1069 StringList archs;
Ed Masted78c9572014-04-20 00:31:37 +00001070 ArchSpec::AutoComplete(nullptr, archs);
Greg Claytond70b14e2012-05-26 17:21:14 +00001071 g_archs_help.Printf("These are the supported architecture names:\n");
Johnny Chen797a1b32012-05-29 20:04:10 +00001072 archs.Join("\n", g_archs_help);
Greg Claytond70b14e2012-05-26 17:21:14 +00001073 }
1074 return g_archs_help.GetData();
Johnny Chenca7835c2012-05-26 00:32:39 +00001075}
1076
Caroline Ticee139cf22010-10-01 17:46:38 +00001077CommandObject::ArgumentTableEntry
1078CommandObject::g_arguments_data[] =
1079{
Ed Masted78c9572014-04-20 00:31:37 +00001080 { eArgTypeAddress, "address", CommandCompletions::eNoCompletion, { nullptr, false }, "A valid address in the target program's execution space." },
1081 { eArgTypeAddressOrExpression, "address-expression", CommandCompletions::eNoCompletion, { nullptr, false }, "An expression that resolves to an address." },
1082 { eArgTypeAliasName, "alias-name", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of an abbreviation (alias) for a debugger command." },
1083 { 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 +00001084 { eArgTypeArchitecture, "arch", CommandCompletions::eArchitectureCompletion, { arch_helper, true }, "The architecture name, e.g. i386 or x86_64." },
Ed Masted78c9572014-04-20 00:31:37 +00001085 { eArgTypeBoolean, "boolean", CommandCompletions::eNoCompletion, { nullptr, false }, "A Boolean value: 'true' or 'false'" },
1086 { eArgTypeBreakpointID, "breakpt-id", CommandCompletions::eNoCompletion, { BreakpointIDHelpTextCallback, false }, nullptr },
1087 { eArgTypeBreakpointIDRange, "breakpt-id-list", CommandCompletions::eNoCompletion, { BreakpointIDRangeHelpTextCallback, false }, nullptr },
1088 { eArgTypeByteSize, "byte-size", CommandCompletions::eNoCompletion, { nullptr, false }, "Number of bytes to use." },
1089 { eArgTypeClassName, "class-name", CommandCompletions::eNoCompletion, { nullptr, false }, "Then name of a class from the debug information in the program." },
1090 { eArgTypeCommandName, "cmd-name", CommandCompletions::eNoCompletion, { nullptr, false }, "A debugger command (may be multiple words), without any options or arguments." },
1091 { eArgTypeCount, "count", CommandCompletions::eNoCompletion, { nullptr, false }, "An unsigned integer." },
1092 { eArgTypeDirectoryName, "directory", CommandCompletions::eDiskDirectoryCompletion, { nullptr, false }, "A directory name." },
1093 { 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" },
1094 { eArgTypeDescriptionVerbosity, "description-verbosity", CommandCompletions::eNoCompletion, { nullptr, false }, "How verbose the output of 'po' should be." },
1095 { eArgTypeEndAddress, "end-address", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1096 { eArgTypeExpression, "expr", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1097 { eArgTypeExpressionPath, "expr-path", CommandCompletions::eNoCompletion, { ExprPathHelpTextCallback, true }, nullptr },
1098 { eArgTypeExprFormat, "expression-format", CommandCompletions::eNoCompletion, { nullptr, false }, "[ [bool|b] | [bin] | [char|c] | [oct|o] | [dec|i|d|u] | [hex|x] | [float|f] | [cstr|s] ]" },
1099 { eArgTypeFilename, "filename", CommandCompletions::eDiskFileCompletion, { nullptr, false }, "The name of a file (can include path)." },
1100 { eArgTypeFormat, "format", CommandCompletions::eNoCompletion, { FormatHelpTextCallback, true }, nullptr },
1101 { eArgTypeFrameIndex, "frame-index", CommandCompletions::eNoCompletion, { nullptr, false }, "Index into a thread's list of frames." },
1102 { eArgTypeFullName, "fullname", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1103 { eArgTypeFunctionName, "function-name", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of a function." },
1104 { eArgTypeFunctionOrSymbol, "function-or-symbol", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of a function or symbol." },
1105 { eArgTypeGDBFormat, "gdb-format", CommandCompletions::eNoCompletion, { GDBFormatHelpTextCallback, true }, nullptr },
1106 { eArgTypeIndex, "index", CommandCompletions::eNoCompletion, { nullptr, false }, "An index into a list." },
1107 { eArgTypeLanguage, "language", CommandCompletions::eNoCompletion, { LanguageTypeHelpTextCallback, true }, nullptr },
1108 { eArgTypeLineNum, "linenum", CommandCompletions::eNoCompletion, { nullptr, false }, "Line number in a source file." },
1109 { 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." },
1110 { 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)." },
1111 { eArgTypeMethod, "method", CommandCompletions::eNoCompletion, { nullptr, false }, "A C++ method name." },
1112 { eArgTypeName, "name", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1113 { eArgTypeNewPathPrefix, "new-path-prefix", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1114 { eArgTypeNumLines, "num-lines", CommandCompletions::eNoCompletion, { nullptr, false }, "The number of lines to use." },
1115 { eArgTypeNumberPerLine, "number-per-line", CommandCompletions::eNoCompletion, { nullptr, false }, "The number of items per line to display." },
1116 { eArgTypeOffset, "offset", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1117 { eArgTypeOldPathPrefix, "old-path-prefix", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1118 { eArgTypeOneLiner, "one-line-command", CommandCompletions::eNoCompletion, { nullptr, false }, "A command that is entered as a single line of text." },
1119 { eArgTypePath, "path", CommandCompletions::eDiskFileCompletion, { nullptr, false }, "Path." },
1120 { eArgTypePermissionsNumber, "perms-numeric", CommandCompletions::eNoCompletion, { nullptr, false }, "Permissions given as an octal number (e.g. 755)." },
1121 { eArgTypePermissionsString, "perms=string", CommandCompletions::eNoCompletion, { nullptr, false }, "Permissions given as a string value (e.g. rw-r-xr--)." },
1122 { eArgTypePid, "pid", CommandCompletions::eNoCompletion, { nullptr, false }, "The process ID number." },
1123 { eArgTypePlugin, "plugin", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1124 { eArgTypeProcessName, "process-name", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of the process." },
1125 { eArgTypePythonClass, "python-class", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of a Python class." },
1126 { eArgTypePythonFunction, "python-function", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of a Python function." },
1127 { eArgTypePythonScript, "python-script", CommandCompletions::eNoCompletion, { nullptr, false }, "Source code written in Python." },
1128 { eArgTypeQueueName, "queue-name", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of the thread queue." },
1129 { eArgTypeRegisterName, "register-name", CommandCompletions::eNoCompletion, { RegisterNameHelpTextCallback, true }, nullptr },
1130 { eArgTypeRegularExpression, "regular-expression", CommandCompletions::eNoCompletion, { nullptr, false }, "A regular expression." },
1131 { eArgTypeRunArgs, "run-args", CommandCompletions::eNoCompletion, { nullptr, false }, "Arguments to be passed to the target program when it starts executing." },
1132 { eArgTypeRunMode, "run-mode", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1133 { eArgTypeScriptedCommandSynchronicity, "script-cmd-synchronicity", CommandCompletions::eNoCompletion, { nullptr, false }, "The synchronicity to use to run scripted commands with regard to LLDB event system." },
1134 { eArgTypeScriptLang, "script-language", CommandCompletions::eNoCompletion, { nullptr, false }, "The scripting language to be used for script-based commands. Currently only Python is valid." },
1135 { eArgTypeSearchWord, "search-word", CommandCompletions::eNoCompletion, { nullptr, false }, "The word for which you wish to search for information about." },
1136 { eArgTypeSelector, "selector", CommandCompletions::eNoCompletion, { nullptr, false }, "An Objective-C selector name." },
1137 { 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)." },
1138 { 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)." },
1139 { eArgTypeSettingPrefix, "setting-prefix", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of a settable internal debugger variable up to a dot ('.'), e.g. 'target.process.'" },
1140 { 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." },
1141 { eArgTypeShlibName, "shlib-name", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of a shared library." },
1142 { eArgTypeSourceFile, "source-file", CommandCompletions::eSourceFileCompletion, { nullptr, false }, "The name of a source file.." },
1143 { eArgTypeSortOrder, "sort-order", CommandCompletions::eNoCompletion, { nullptr, false }, "Specify a sort order when dumping lists." },
1144 { eArgTypeStartAddress, "start-address", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1145 { eArgTypeSummaryString, "summary-string", CommandCompletions::eNoCompletion, { SummaryStringHelpTextCallback, true }, nullptr },
1146 { eArgTypeSymbol, "symbol", CommandCompletions::eSymbolCompletion, { nullptr, false }, "Any symbol name (function name, variable, argument, etc.)" },
1147 { eArgTypeThreadID, "thread-id", CommandCompletions::eNoCompletion, { nullptr, false }, "Thread ID number." },
1148 { eArgTypeThreadIndex, "thread-index", CommandCompletions::eNoCompletion, { nullptr, false }, "Index into the process' list of threads." },
1149 { eArgTypeThreadName, "thread-name", CommandCompletions::eNoCompletion, { nullptr, false }, "The thread's name." },
1150 { eArgTypeUnsignedInteger, "unsigned-integer", CommandCompletions::eNoCompletion, { nullptr, false }, "An unsigned integer." },
1151 { eArgTypeUnixSignal, "unix-signal", CommandCompletions::eNoCompletion, { nullptr, false }, "A valid Unix signal name or number (e.g. SIGKILL, KILL or 9)." },
1152 { eArgTypeVarName, "variable-name", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of a variable in your program." },
1153 { eArgTypeValue, "value", CommandCompletions::eNoCompletion, { nullptr, false }, "A value could be anything, depending on where and how it is used." },
1154 { eArgTypeWidth, "width", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1155 { eArgTypeNone, "none", CommandCompletions::eNoCompletion, { nullptr, false }, "No help available for this." },
1156 { 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." },
1157 { eArgTypeWatchpointID, "watchpt-id", CommandCompletions::eNoCompletion, { nullptr, false }, "Watchpoint IDs are positive integers." },
1158 { eArgTypeWatchpointIDRange, "watchpt-id-list", CommandCompletions::eNoCompletion, { nullptr, false }, "For example, '1-3' or '1 to 3'." },
1159 { eArgTypeWatchType, "watch-type", CommandCompletions::eNoCompletion, { nullptr, false }, "Specify the type for a watchpoint." }
Caroline Ticee139cf22010-10-01 17:46:38 +00001160};
1161
1162const CommandObject::ArgumentTableEntry*
1163CommandObject::GetArgumentTable ()
1164{
Greg Clayton9d0402b2011-02-20 02:15:07 +00001165 // If this assertion fires, then the table above is out of date with the CommandArgumentType enumeration
1166 assert ((sizeof (CommandObject::g_arguments_data) / sizeof (CommandObject::ArgumentTableEntry)) == eArgTypeLastArg);
Caroline Ticee139cf22010-10-01 17:46:38 +00001167 return CommandObject::g_arguments_data;
1168}
1169
1170