blob: 0096541d7410a3d138073b55d8178fd4bc9c7d2c [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),
Enrico Granata2f02fe02014-12-05 20:59:08 +000052 m_cmd_name (name ? name : ""),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053 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 {
Jason Molendae59b0d22014-09-20 09:14:31 +0000239 if (!m_exe_ctx.HasTargetScope())
240 result.AppendError (GetInvalidTargetDescription());
241 else
242 result.AppendError (GetInvalidProcessDescription());
Greg Claytonf9fc6092013-01-09 19:44:40 +0000243 return false;
244 }
245
246 if ((flags & eFlagRequiresThread) && !m_exe_ctx.HasThreadScope())
247 {
Jason Molendae59b0d22014-09-20 09:14:31 +0000248 if (!m_exe_ctx.HasTargetScope())
249 result.AppendError (GetInvalidTargetDescription());
250 else if (!m_exe_ctx.HasProcessScope())
251 result.AppendError (GetInvalidProcessDescription());
252 else
253 result.AppendError (GetInvalidThreadDescription());
Greg Claytonf9fc6092013-01-09 19:44:40 +0000254 return false;
255 }
256
257 if ((flags & eFlagRequiresFrame) && !m_exe_ctx.HasFrameScope())
258 {
Jason Molendae59b0d22014-09-20 09:14:31 +0000259 if (!m_exe_ctx.HasTargetScope())
260 result.AppendError (GetInvalidTargetDescription());
261 else if (!m_exe_ctx.HasProcessScope())
262 result.AppendError (GetInvalidProcessDescription());
263 else if (!m_exe_ctx.HasThreadScope())
264 result.AppendError (GetInvalidThreadDescription());
265 else
266 result.AppendError (GetInvalidFrameDescription());
Greg Claytonf9fc6092013-01-09 19:44:40 +0000267 return false;
268 }
269
Ed Masted78c9572014-04-20 00:31:37 +0000270 if ((flags & eFlagRequiresRegContext) && (m_exe_ctx.GetRegisterContext() == nullptr))
Greg Claytonf9fc6092013-01-09 19:44:40 +0000271 {
272 result.AppendError (GetInvalidRegContextDescription());
273 return false;
274 }
275
276 if (flags & eFlagTryTargetAPILock)
277 {
278 Target *target = m_exe_ctx.GetTargetPtr();
279 if (target)
Greg Clayton9b5442a2014-02-07 22:31:20 +0000280 m_api_locker.Lock (target->GetAPIMutex());
Greg Claytonf9fc6092013-01-09 19:44:40 +0000281 }
282 }
283
Greg Claytonb766a732011-02-04 01:58:07 +0000284 if (GetFlags().AnySet (CommandObject::eFlagProcessMustBeLaunched | CommandObject::eFlagProcessMustBePaused))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000285 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000286 Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
Ed Masted78c9572014-04-20 00:31:37 +0000287 if (process == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000288 {
Jim Inghamb8e8a5f2011-07-09 00:55:34 +0000289 // A process that is not running is considered paused.
290 if (GetFlags().Test(CommandObject::eFlagProcessMustBeLaunched))
291 {
292 result.AppendError ("Process must exist.");
293 result.SetStatus (eReturnStatusFailed);
294 return false;
295 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000296 }
Greg Claytonb766a732011-02-04 01:58:07 +0000297 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000298 {
Greg Claytonb766a732011-02-04 01:58:07 +0000299 StateType state = process->GetState();
Greg Claytonb766a732011-02-04 01:58:07 +0000300 switch (state)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000301 {
Greg Clayton7a5388b2011-03-20 04:57:14 +0000302 case eStateInvalid:
Greg Claytonb766a732011-02-04 01:58:07 +0000303 case eStateSuspended:
304 case eStateCrashed:
305 case eStateStopped:
306 break;
307
308 case eStateConnected:
309 case eStateAttaching:
310 case eStateLaunching:
311 case eStateDetached:
312 case eStateExited:
313 case eStateUnloaded:
314 if (GetFlags().Test(CommandObject::eFlagProcessMustBeLaunched))
315 {
316 result.AppendError ("Process must be launched.");
317 result.SetStatus (eReturnStatusFailed);
318 return false;
319 }
320 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000321
Greg Claytonb766a732011-02-04 01:58:07 +0000322 case eStateRunning:
323 case eStateStepping:
324 if (GetFlags().Test(CommandObject::eFlagProcessMustBePaused))
325 {
326 result.AppendError ("Process is running. Use 'process interrupt' to pause execution.");
327 result.SetStatus (eReturnStatusFailed);
328 return false;
329 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000330 }
331 }
332 }
Jim Ingham5a988412012-06-08 21:56:10 +0000333 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000334}
335
Greg Claytonf9fc6092013-01-09 19:44:40 +0000336void
337CommandObject::Cleanup ()
338{
339 m_exe_ctx.Clear();
340 m_api_locker.Unlock();
341}
342
343
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000344class CommandDictCommandPartialMatch
345{
346 public:
347 CommandDictCommandPartialMatch (const char *match_str)
348 {
349 m_match_str = match_str;
350 }
351 bool operator() (const std::pair<std::string, lldb::CommandObjectSP> map_element) const
352 {
353 // A NULL or empty string matches everything.
Ed Masted78c9572014-04-20 00:31:37 +0000354 if (m_match_str == nullptr || *m_match_str == '\0')
Greg Claytonc7bece562013-01-25 18:06:21 +0000355 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000356
Greg Claytonc7bece562013-01-25 18:06:21 +0000357 return map_element.first.find (m_match_str, 0) == 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000358 }
359
360 private:
361 const char *m_match_str;
362};
363
364int
365CommandObject::AddNamesMatchingPartialString (CommandObject::CommandMap &in_map, const char *cmd_str,
366 StringList &matches)
367{
368 int number_added = 0;
369 CommandDictCommandPartialMatch matcher(cmd_str);
370
371 CommandObject::CommandMap::iterator matching_cmds = std::find_if (in_map.begin(), in_map.end(), matcher);
372
373 while (matching_cmds != in_map.end())
374 {
375 ++number_added;
376 matches.AppendString((*matching_cmds).first.c_str());
377 matching_cmds = std::find_if (++matching_cmds, in_map.end(), matcher);;
378 }
379 return number_added;
380}
381
382int
383CommandObject::HandleCompletion
384(
385 Args &input,
386 int &cursor_index,
387 int &cursor_char_position,
388 int match_start_point,
389 int max_return_elements,
Jim Ingham558ce122010-06-30 05:02:46 +0000390 bool &word_complete,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000391 StringList &matches
392)
393{
Johnny Chen6561d152012-01-20 00:59:19 +0000394 // Default implmentation of WantsCompletion() is !WantsRawCommandString().
395 // Subclasses who want raw command string but desire, for example,
396 // argument completion should override WantsCompletion() to return true,
397 // instead.
Johnny Chen6f99b632012-01-19 22:16:06 +0000398 if (WantsRawCommandString() && !WantsCompletion())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000399 {
400 // FIXME: Abstract telling the completion to insert the completion character.
401 matches.Clear();
402 return -1;
403 }
404 else
405 {
406 // Can we do anything generic with the options?
407 Options *cur_options = GetOptions();
408 CommandReturnObject result;
409 OptionElementVector opt_element_vector;
410
Ed Masted78c9572014-04-20 00:31:37 +0000411 if (cur_options != nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000412 {
413 // Re-insert the dummy command name string which will have been
414 // stripped off:
415 input.Unshift ("dummy-string");
416 cursor_index++;
417
418
419 // I stick an element on the end of the input, because if the last element is
Greg Claytonb7ad58a2013-04-04 20:35:24 +0000420 // option that requires an argument, getopt_long_only will freak out.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000421
422 input.AppendArgument ("<FAKE-VALUE>");
423
Jim Inghamd43e0092010-06-24 20:31:04 +0000424 input.ParseArgsForCompletion (*cur_options, opt_element_vector, cursor_index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000425
426 input.DeleteArgumentAtIndex(input.GetArgumentCount() - 1);
427
428 bool handled_by_options;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000429 handled_by_options = cur_options->HandleOptionCompletion (input,
Greg Clayton66111032010-06-23 01:19:29 +0000430 opt_element_vector,
431 cursor_index,
432 cursor_char_position,
433 match_start_point,
434 max_return_elements,
Jim Ingham558ce122010-06-30 05:02:46 +0000435 word_complete,
Greg Clayton66111032010-06-23 01:19:29 +0000436 matches);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000437 if (handled_by_options)
438 return matches.GetSize();
439 }
440
441 // If we got here, the last word is not an option or an option argument.
Greg Claytona7015092010-09-18 01:14:36 +0000442 return HandleArgumentCompletion (input,
Greg Clayton66111032010-06-23 01:19:29 +0000443 cursor_index,
444 cursor_char_position,
445 opt_element_vector,
446 match_start_point,
447 max_return_elements,
Jim Ingham558ce122010-06-30 05:02:46 +0000448 word_complete,
Greg Clayton66111032010-06-23 01:19:29 +0000449 matches);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000450 }
451}
452
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000453bool
Greg Claytona7015092010-09-18 01:14:36 +0000454CommandObject::HelpTextContainsWord (const char *search_word)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000455{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000456 std::string options_usage_help;
457
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000458 bool found_word = false;
459
Greg Clayton998255b2012-10-13 02:07:45 +0000460 const char *short_help = GetHelp();
461 const char *long_help = GetHelpLong();
462 const char *syntax_help = GetSyntax();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000463
Greg Clayton998255b2012-10-13 02:07:45 +0000464 if (short_help && strcasestr (short_help, search_word))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000465 found_word = true;
Greg Clayton998255b2012-10-13 02:07:45 +0000466 else if (long_help && strcasestr (long_help, search_word))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000467 found_word = true;
Greg Clayton998255b2012-10-13 02:07:45 +0000468 else if (syntax_help && strcasestr (syntax_help, search_word))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000469 found_word = true;
470
471 if (!found_word
Ed Masted78c9572014-04-20 00:31:37 +0000472 && GetOptions() != nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000473 {
474 StreamString usage_help;
Greg Claytoneb0103f2011-04-07 22:46:35 +0000475 GetOptions()->GenerateOptionUsage (usage_help, this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000476 if (usage_help.GetSize() > 0)
477 {
478 const char *usage_text = usage_help.GetData();
Caroline Tice4b6fbf32010-10-12 22:16:53 +0000479 if (strcasestr (usage_text, search_word))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000480 found_word = true;
481 }
482 }
483
484 return found_word;
485}
Caroline Ticee139cf22010-10-01 17:46:38 +0000486
487int
488CommandObject::GetNumArgumentEntries ()
489{
490 return m_arguments.size();
491}
492
493CommandObject::CommandArgumentEntry *
494CommandObject::GetArgumentEntryAtIndex (int idx)
495{
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +0000496 if (static_cast<size_t>(idx) < m_arguments.size())
Caroline Ticee139cf22010-10-01 17:46:38 +0000497 return &(m_arguments[idx]);
498
Ed Masted78c9572014-04-20 00:31:37 +0000499 return nullptr;
Caroline Ticee139cf22010-10-01 17:46:38 +0000500}
501
502CommandObject::ArgumentTableEntry *
503CommandObject::FindArgumentDataByType (CommandArgumentType arg_type)
504{
505 const ArgumentTableEntry *table = CommandObject::GetArgumentTable();
506
507 for (int i = 0; i < eArgTypeLastArg; ++i)
508 if (table[i].arg_type == arg_type)
509 return (ArgumentTableEntry *) &(table[i]);
510
Ed Masted78c9572014-04-20 00:31:37 +0000511 return nullptr;
Caroline Ticee139cf22010-10-01 17:46:38 +0000512}
513
514void
515CommandObject::GetArgumentHelp (Stream &str, CommandArgumentType arg_type, CommandInterpreter &interpreter)
516{
517 const ArgumentTableEntry* table = CommandObject::GetArgumentTable();
518 ArgumentTableEntry *entry = (ArgumentTableEntry *) &(table[arg_type]);
519
520 // The table is *supposed* to be kept in arg_type order, but someone *could* have messed it up...
521
522 if (entry->arg_type != arg_type)
523 entry = CommandObject::FindArgumentDataByType (arg_type);
524
525 if (!entry)
526 return;
527
528 StreamString name_str;
529 name_str.Printf ("<%s>", entry->arg_name);
530
Enrico Granatafc7a7f32011-07-08 02:51:01 +0000531 if (entry->help_function)
Enrico Granata82a7d982011-07-07 00:38:40 +0000532 {
Enrico Granatafc7a7f32011-07-08 02:51:01 +0000533 const char* help_text = entry->help_function();
Enrico Granata82a7d982011-07-07 00:38:40 +0000534 if (!entry->help_function.self_formatting)
535 {
536 interpreter.OutputFormattedHelpText (str, name_str.GetData(), "--", help_text,
537 name_str.GetSize());
538 }
539 else
540 {
541 interpreter.OutputHelpText(str, name_str.GetData(), "--", help_text,
542 name_str.GetSize());
543 }
544 }
Caroline Ticee139cf22010-10-01 17:46:38 +0000545 else
546 interpreter.OutputFormattedHelpText (str, name_str.GetData(), "--", entry->help_text, name_str.GetSize());
547}
548
549const char *
550CommandObject::GetArgumentName (CommandArgumentType arg_type)
551{
Caroline Ticedeaab222010-10-01 19:59:14 +0000552 ArgumentTableEntry *entry = (ArgumentTableEntry *) &(CommandObject::GetArgumentTable()[arg_type]);
553
554 // The table is *supposed* to be kept in arg_type order, but someone *could* have messed it up...
555
556 if (entry->arg_type != arg_type)
557 entry = CommandObject::FindArgumentDataByType (arg_type);
558
Johnny Chene6acf352010-10-08 22:01:52 +0000559 if (entry)
560 return entry->arg_name;
561
562 StreamString str;
563 str << "Arg name for type (" << arg_type << ") not in arg table!";
564 return str.GetData();
Caroline Ticee139cf22010-10-01 17:46:38 +0000565}
566
Caroline Tice405fe672010-10-04 22:28:36 +0000567bool
Greg Claytone0d378b2011-03-24 21:19:54 +0000568CommandObject::IsPairType (ArgumentRepetitionType arg_repeat_type)
Caroline Tice405fe672010-10-04 22:28:36 +0000569{
570 if ((arg_repeat_type == eArgRepeatPairPlain)
571 || (arg_repeat_type == eArgRepeatPairOptional)
572 || (arg_repeat_type == eArgRepeatPairPlus)
573 || (arg_repeat_type == eArgRepeatPairStar)
574 || (arg_repeat_type == eArgRepeatPairRange)
575 || (arg_repeat_type == eArgRepeatPairRangeOptional))
576 return true;
577
578 return false;
579}
580
Johnny Chen34ddc8d2012-02-08 01:13:31 +0000581static CommandObject::CommandArgumentEntry
582OptSetFiltered(uint32_t opt_set_mask, CommandObject::CommandArgumentEntry &cmd_arg_entry)
583{
584 CommandObject::CommandArgumentEntry ret_val;
585 for (unsigned i = 0; i < cmd_arg_entry.size(); ++i)
586 if (opt_set_mask & cmd_arg_entry[i].arg_opt_set_association)
587 ret_val.push_back(cmd_arg_entry[i]);
588 return ret_val;
589}
590
591// Default parameter value of opt_set_mask is LLDB_OPT_SET_ALL, which means take
592// all the argument data into account. On rare cases where some argument sticks
593// with certain option sets, this function returns the option set filtered args.
Caroline Ticee139cf22010-10-01 17:46:38 +0000594void
Johnny Chen34ddc8d2012-02-08 01:13:31 +0000595CommandObject::GetFormattedCommandArguments (Stream &str, uint32_t opt_set_mask)
Caroline Ticee139cf22010-10-01 17:46:38 +0000596{
597 int num_args = m_arguments.size();
598 for (int i = 0; i < num_args; ++i)
599 {
600 if (i > 0)
601 str.Printf (" ");
Johnny Chen34ddc8d2012-02-08 01:13:31 +0000602 CommandArgumentEntry arg_entry =
603 opt_set_mask == LLDB_OPT_SET_ALL ? m_arguments[i]
604 : OptSetFiltered(opt_set_mask, m_arguments[i]);
Caroline Ticee139cf22010-10-01 17:46:38 +0000605 int num_alternatives = arg_entry.size();
Caroline Tice405fe672010-10-04 22:28:36 +0000606
607 if ((num_alternatives == 2)
608 && IsPairType (arg_entry[0].arg_repetition))
Caroline Ticee139cf22010-10-01 17:46:38 +0000609 {
Caroline Tice405fe672010-10-04 22:28:36 +0000610 const char *first_name = GetArgumentName (arg_entry[0].arg_type);
611 const char *second_name = GetArgumentName (arg_entry[1].arg_type);
612 switch (arg_entry[0].arg_repetition)
613 {
614 case eArgRepeatPairPlain:
615 str.Printf ("<%s> <%s>", first_name, second_name);
616 break;
617 case eArgRepeatPairOptional:
618 str.Printf ("[<%s> <%s>]", first_name, second_name);
619 break;
620 case eArgRepeatPairPlus:
621 str.Printf ("<%s> <%s> [<%s> <%s> [...]]", first_name, second_name, first_name, second_name);
622 break;
623 case eArgRepeatPairStar:
624 str.Printf ("[<%s> <%s> [<%s> <%s> [...]]]", first_name, second_name, first_name, second_name);
625 break;
626 case eArgRepeatPairRange:
627 str.Printf ("<%s_1> <%s_1> ... <%s_n> <%s_n>", first_name, second_name, first_name, second_name);
628 break;
629 case eArgRepeatPairRangeOptional:
630 str.Printf ("[<%s_1> <%s_1> ... <%s_n> <%s_n>]", first_name, second_name, first_name, second_name);
631 break;
Caroline Ticeca1176a2011-03-23 22:31:13 +0000632 // Explicitly test for all the rest of the cases, so if new types get added we will notice the
633 // missing case statement(s).
634 case eArgRepeatPlain:
635 case eArgRepeatOptional:
636 case eArgRepeatPlus:
637 case eArgRepeatStar:
638 case eArgRepeatRange:
639 // These should not be reached, as they should fail the IsPairType test above.
640 break;
Caroline Tice405fe672010-10-04 22:28:36 +0000641 }
Caroline Ticee139cf22010-10-01 17:46:38 +0000642 }
Caroline Tice405fe672010-10-04 22:28:36 +0000643 else
Caroline Ticee139cf22010-10-01 17:46:38 +0000644 {
Caroline Tice405fe672010-10-04 22:28:36 +0000645 StreamString names;
646 for (int j = 0; j < num_alternatives; ++j)
647 {
648 if (j > 0)
649 names.Printf (" | ");
650 names.Printf ("%s", GetArgumentName (arg_entry[j].arg_type));
651 }
652 switch (arg_entry[0].arg_repetition)
653 {
654 case eArgRepeatPlain:
655 str.Printf ("<%s>", names.GetData());
656 break;
657 case eArgRepeatPlus:
658 str.Printf ("<%s> [<%s> [...]]", names.GetData(), names.GetData());
659 break;
660 case eArgRepeatStar:
661 str.Printf ("[<%s> [<%s> [...]]]", names.GetData(), names.GetData());
662 break;
663 case eArgRepeatOptional:
664 str.Printf ("[<%s>]", names.GetData());
665 break;
666 case eArgRepeatRange:
Jason Molendafd54b362011-09-20 21:44:10 +0000667 str.Printf ("<%s_1> .. <%s_n>", names.GetData(), names.GetData());
Caroline Ticeca1176a2011-03-23 22:31:13 +0000668 break;
669 // Explicitly test for all the rest of the cases, so if new types get added we will notice the
670 // missing case statement(s).
671 case eArgRepeatPairPlain:
672 case eArgRepeatPairOptional:
673 case eArgRepeatPairPlus:
674 case eArgRepeatPairStar:
675 case eArgRepeatPairRange:
676 case eArgRepeatPairRangeOptional:
677 // These should not be hit, as they should pass the IsPairType test above, and control should
678 // have gone into the other branch of the if statement.
679 break;
Caroline Tice405fe672010-10-04 22:28:36 +0000680 }
Caroline Ticee139cf22010-10-01 17:46:38 +0000681 }
682 }
683}
684
Stephen Wilson0c16aa62011-03-23 02:12:10 +0000685CommandArgumentType
Caroline Ticee139cf22010-10-01 17:46:38 +0000686CommandObject::LookupArgumentName (const char *arg_name)
687{
688 CommandArgumentType return_type = eArgTypeLastArg;
689
690 std::string arg_name_str (arg_name);
691 size_t len = arg_name_str.length();
692 if (arg_name[0] == '<'
693 && arg_name[len-1] == '>')
694 arg_name_str = arg_name_str.substr (1, len-2);
695
Johnny Chen331eff32011-07-14 22:20:12 +0000696 const ArgumentTableEntry *table = GetArgumentTable();
Caroline Ticee139cf22010-10-01 17:46:38 +0000697 for (int i = 0; i < eArgTypeLastArg; ++i)
Johnny Chen331eff32011-07-14 22:20:12 +0000698 if (arg_name_str.compare (table[i].arg_name) == 0)
Caroline Ticee139cf22010-10-01 17:46:38 +0000699 return_type = g_arguments_data[i].arg_type;
700
701 return return_type;
702}
703
704static const char *
Jim Ingham931e6742012-08-23 23:37:31 +0000705RegisterNameHelpTextCallback ()
706{
707 return "Register names can be specified using the architecture specific names. "
Jim Ingham84c7bd72012-08-23 23:47:08 +0000708 "They can also be specified using generic names. Not all generic entities have "
709 "registers backing them on all architectures. When they don't the generic name "
710 "will return an error.\n"
Jim Ingham931e6742012-08-23 23:37:31 +0000711 "The generic names defined in lldb are:\n"
712 "\n"
713 "pc - program counter register\n"
714 "ra - return address register\n"
715 "fp - frame pointer register\n"
716 "sp - stack pointer register\n"
Jim Ingham84c7bd72012-08-23 23:47:08 +0000717 "flags - the flags register\n"
Jim Ingham931e6742012-08-23 23:37:31 +0000718 "arg{1-6} - integer argument passing registers.\n";
719}
720
721static const char *
Caroline Ticee139cf22010-10-01 17:46:38 +0000722BreakpointIDHelpTextCallback ()
723{
Greg Clayton86edbf42011-10-26 00:56:27 +0000724 return "Breakpoint ID's consist major and minor numbers; the major number "
725 "corresponds to the single entity that was created with a 'breakpoint set' "
726 "command; the minor numbers correspond to all the locations that were actually "
727 "found/set based on the major breakpoint. A full breakpoint ID might look like "
728 "3.14, meaning the 14th location set for the 3rd breakpoint. You can specify "
729 "all the locations of a breakpoint by just indicating the major breakpoint "
730 "number. A valid breakpoint id consists either of just the major id number, "
731 "or the major number, a dot, and the location number (e.g. 3 or 3.2 could "
732 "both be valid breakpoint ids).";
Caroline Ticee139cf22010-10-01 17:46:38 +0000733}
734
735static const char *
736BreakpointIDRangeHelpTextCallback ()
737{
Greg Clayton86edbf42011-10-26 00:56:27 +0000738 return "A 'breakpoint id list' is a manner of specifying multiple breakpoints. "
739 "This can be done through several mechanisms. The easiest way is to just "
740 "enter a space-separated list of breakpoint ids. To specify all the "
741 "breakpoint locations under a major breakpoint, you can use the major "
742 "breakpoint number followed by '.*', eg. '5.*' means all the locations under "
743 "breakpoint 5. You can also indicate a range of breakpoints by using "
744 "<start-bp-id> - <end-bp-id>. The start-bp-id and end-bp-id for a range can "
745 "be any valid breakpoint ids. It is not legal, however, to specify a range "
746 "using specific locations that cross major breakpoint numbers. I.e. 3.2 - 3.7"
747 " is legal; 2 - 5 is legal; but 3.2 - 4.4 is not legal.";
Caroline Ticee139cf22010-10-01 17:46:38 +0000748}
749
Enrico Granata0a3958e2011-07-02 00:25:22 +0000750static const char *
Greg Clayton86edbf42011-10-26 00:56:27 +0000751GDBFormatHelpTextCallback ()
752{
Greg Claytonf91381e2011-10-26 18:35:21 +0000753 return "A GDB format consists of a repeat count, a format letter and a size letter. "
754 "The repeat count is optional and defaults to 1. The format letter is optional "
755 "and defaults to the previous format that was used. The size letter is optional "
756 "and defaults to the previous size that was used.\n"
757 "\n"
758 "Format letters include:\n"
759 "o - octal\n"
760 "x - hexadecimal\n"
761 "d - decimal\n"
762 "u - unsigned decimal\n"
763 "t - binary\n"
764 "f - float\n"
765 "a - address\n"
766 "i - instruction\n"
767 "c - char\n"
768 "s - string\n"
769 "T - OSType\n"
770 "A - float as hex\n"
771 "\n"
772 "Size letters include:\n"
773 "b - 1 byte (byte)\n"
774 "h - 2 bytes (halfword)\n"
775 "w - 4 bytes (word)\n"
776 "g - 8 bytes (giant)\n"
777 "\n"
778 "Example formats:\n"
779 "32xb - show 32 1 byte hexadecimal integer values\n"
780 "16xh - show 16 2 byte hexadecimal integer values\n"
781 "64 - show 64 2 byte hexadecimal integer values (format and size from the last format)\n"
782 "dw - show 1 4 byte decimal integer value\n"
783 ;
Greg Clayton86edbf42011-10-26 00:56:27 +0000784}
785
786static const char *
Enrico Granata0a3958e2011-07-02 00:25:22 +0000787FormatHelpTextCallback ()
788{
Enrico Granata82a7d982011-07-07 00:38:40 +0000789
Ed Masted78c9572014-04-20 00:31:37 +0000790 static char* help_text_ptr = nullptr;
Enrico Granata82a7d982011-07-07 00:38:40 +0000791
792 if (help_text_ptr)
793 return help_text_ptr;
794
Enrico Granata0a3958e2011-07-02 00:25:22 +0000795 StreamString sstr;
796 sstr << "One of the format names (or one-character names) that can be used to show a variable's value:\n";
797 for (Format f = eFormatDefault; f < kNumFormats; f = Format(f+1))
798 {
Enrico Granata82a7d982011-07-07 00:38:40 +0000799 if (f != eFormatDefault)
800 sstr.PutChar('\n');
801
Enrico Granata0a3958e2011-07-02 00:25:22 +0000802 char format_char = FormatManager::GetFormatAsFormatChar(f);
803 if (format_char)
804 sstr.Printf("'%c' or ", format_char);
805
Enrico Granata82a7d982011-07-07 00:38:40 +0000806 sstr.Printf ("\"%s\"", FormatManager::GetFormatAsCString(f));
Enrico Granata0a3958e2011-07-02 00:25:22 +0000807 }
808
809 sstr.Flush();
810
811 std::string data = sstr.GetString();
812
Enrico Granata82a7d982011-07-07 00:38:40 +0000813 help_text_ptr = new char[data.length()+1];
Enrico Granata0a3958e2011-07-02 00:25:22 +0000814
Enrico Granata82a7d982011-07-07 00:38:40 +0000815 data.copy(help_text_ptr, data.length());
Enrico Granata0a3958e2011-07-02 00:25:22 +0000816
Enrico Granata82a7d982011-07-07 00:38:40 +0000817 return help_text_ptr;
Enrico Granata0a3958e2011-07-02 00:25:22 +0000818}
819
820static const char *
Sean Callanand9477392012-10-23 00:50:09 +0000821LanguageTypeHelpTextCallback ()
822{
Ed Masted78c9572014-04-20 00:31:37 +0000823 static char* help_text_ptr = nullptr;
Sean Callanand9477392012-10-23 00:50:09 +0000824
825 if (help_text_ptr)
826 return help_text_ptr;
827
828 StreamString sstr;
829 sstr << "One of the following languages:\n";
830
Daniel Malea48947c72012-12-04 00:23:45 +0000831 for (unsigned int l = eLanguageTypeUnknown; l < eNumLanguageTypes; ++l)
Sean Callanand9477392012-10-23 00:50:09 +0000832 {
Daniel Malea48947c72012-12-04 00:23:45 +0000833 sstr << " " << LanguageRuntime::GetNameForLanguageType(static_cast<LanguageType>(l)) << "\n";
Sean Callanand9477392012-10-23 00:50:09 +0000834 }
835
836 sstr.Flush();
837
838 std::string data = sstr.GetString();
839
840 help_text_ptr = new char[data.length()+1];
841
842 data.copy(help_text_ptr, data.length());
843
844 return help_text_ptr;
845}
846
847static const char *
Enrico Granata82a7d982011-07-07 00:38:40 +0000848SummaryStringHelpTextCallback()
Enrico Granata0a3958e2011-07-02 00:25:22 +0000849{
Enrico Granata82a7d982011-07-07 00:38:40 +0000850 return
851 "A summary string is a way to extract information from variables in order to present them using a summary.\n"
852 "Summary strings contain static text, variables, scopes and control sequences:\n"
853 " - Static text can be any sequence of non-special characters, i.e. anything but '{', '}', '$', or '\\'.\n"
854 " - Variables are sequences of characters beginning with ${, ending with } and that contain symbols in the format described below.\n"
855 " - 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"
856 " - Control sequences are the usual C/C++ '\\a', '\\n', ..., plus '\\$', '\\{' and '\\}'.\n"
857 "A summary string works by copying static text verbatim, turning control sequences into their character counterpart, expanding variables and trying to expand scopes.\n"
858 "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"
859 "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"
860 " (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 +0000861 " ${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."
862 " 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 +0000863 "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."
864 "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"
865 " path refers to:\n"
866 " - 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"
867 " and displayed as an individual variable\n"
868 " - 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"
869 " 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 +0000870 "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"
871 "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"
872 " special symbols only allowed as part of a variable:\n"
873 " %V: show the value of the object by default\n"
874 " %S: show the summary of the object by default\n"
875 " %@: show the runtime-provided object description (for Objective-C, it calls NSPrintForDebugger; for C/C++ it does nothing)\n"
876 " %L: show the location of the object (memory address or a register name)\n"
877 " %#: show the number of children of the object\n"
878 " %T: show the type of the object\n"
879 "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"
880 " 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"
881 " count the number of actual elements stored in an std::list:\n"
882 "type summary add -s \"${svar%#}\" -x \"std::list<\"";
883}
884
885static const char *
886ExprPathHelpTextCallback()
887{
888 return
889 "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"
890 "For instance, given a class:\n"
891 " class foo {\n"
892 " int a;\n"
893 " int b; .\n"
894 " foo* next;\n"
895 " };\n"
896 "the expression to read item b in the item pointed to by next for foo aFoo would be aFoo.next->b.\n"
897 "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"
898 "Expression paths in LLDB include dot (.) and arrow (->) operators, and most commands using expression paths have ways to also accept the star (*) operator.\n"
899 "The meaning of these operators is the same as the usual one given to them by the C/C++ standards.\n"
900 "LLDB also has support for indexing ([ ]) in expression paths, and extends the traditional meaning of the square brackets operator to allow bitfield extraction:\n"
901 "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"
902 " 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"
903 " 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"
904 " meaning of array slicing (taking elements n thru m inside the array or pointed-to memory).";
Enrico Granata0a3958e2011-07-02 00:25:22 +0000905}
906
Johnny Chen184d7a72011-09-21 01:00:02 +0000907void
Enrico Granata9b62d1d2013-06-12 01:50:57 +0000908CommandObject::GenerateHelpText (CommandReturnObject &result)
909{
910 GenerateHelpText(result.GetOutputStream());
911
912 result.SetStatus (eReturnStatusSuccessFinishNoResult);
913}
914
915void
916CommandObject::GenerateHelpText (Stream &output_strm)
917{
918 CommandInterpreter& interpreter = GetCommandInterpreter();
Ed Masted78c9572014-04-20 00:31:37 +0000919 if (GetOptions() != nullptr)
Enrico Granata9b62d1d2013-06-12 01:50:57 +0000920 {
921 if (WantsRawCommandString())
922 {
923 std::string help_text (GetHelp());
924 help_text.append (" This command takes 'raw' input (no need to quote stuff).");
925 interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1);
926 }
927 else
928 interpreter.OutputFormattedHelpText (output_strm, "", "", GetHelp(), 1);
929 output_strm.Printf ("\nSyntax: %s\n", GetSyntax());
930 GetOptions()->GenerateOptionUsage (output_strm, this);
931 const char *long_help = GetHelpLong();
Ed Masted78c9572014-04-20 00:31:37 +0000932 if ((long_help != nullptr)
Enrico Granata9b62d1d2013-06-12 01:50:57 +0000933 && (strlen (long_help) > 0))
934 output_strm.Printf ("\n%s", long_help);
935 if (WantsRawCommandString() && !WantsCompletion())
936 {
937 // Emit the message about using ' -- ' between the end of the command options and the raw input
938 // conditionally, i.e., only if the command object does not want completion.
939 interpreter.OutputFormattedHelpText (output_strm, "", "",
940 "\nIMPORTANT NOTE: Because this command takes 'raw' input, if you use any command options"
941 " you must use ' -- ' between the end of the command options and the beginning of the raw input.", 1);
942 }
943 else if (GetNumArgumentEntries() > 0
944 && GetOptions()
945 && GetOptions()->NumCommandOptions() > 0)
946 {
947 // Also emit a warning about using "--" in case you are using a command that takes options and arguments.
948 interpreter.OutputFormattedHelpText (output_strm, "", "",
949 "\nThis command takes options and free-form arguments. If your arguments resemble"
950 " option specifiers (i.e., they start with a - or --), you must use ' -- ' between"
951 " the end of the command options and the beginning of the arguments.", 1);
952 }
953 }
954 else if (IsMultiwordObject())
955 {
956 if (WantsRawCommandString())
957 {
958 std::string help_text (GetHelp());
959 help_text.append (" This command takes 'raw' input (no need to quote stuff).");
960 interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1);
961 }
962 else
963 interpreter.OutputFormattedHelpText (output_strm, "", "", GetHelp(), 1);
964 GenerateHelpText (output_strm);
965 }
966 else
967 {
968 const char *long_help = GetHelpLong();
Ed Masted78c9572014-04-20 00:31:37 +0000969 if ((long_help != nullptr)
Enrico Granata9b62d1d2013-06-12 01:50:57 +0000970 && (strlen (long_help) > 0))
971 output_strm.Printf ("%s", long_help);
972 else if (WantsRawCommandString())
973 {
974 std::string help_text (GetHelp());
975 help_text.append (" This command takes 'raw' input (no need to quote stuff).");
976 interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1);
977 }
978 else
979 interpreter.OutputFormattedHelpText (output_strm, "", "", GetHelp(), 1);
980 output_strm.Printf ("\nSyntax: %s\n", GetSyntax());
981 }
982}
983
984void
Johnny Chende753462011-09-22 22:34:09 +0000985CommandObject::AddIDsArgumentData(CommandArgumentEntry &arg, CommandArgumentType ID, CommandArgumentType IDRange)
Johnny Chen184d7a72011-09-21 01:00:02 +0000986{
987 CommandArgumentData id_arg;
988 CommandArgumentData id_range_arg;
989
990 // Create the first variant for the first (and only) argument for this command.
Johnny Chende753462011-09-22 22:34:09 +0000991 id_arg.arg_type = ID;
Johnny Chen184d7a72011-09-21 01:00:02 +0000992 id_arg.arg_repetition = eArgRepeatOptional;
993
994 // Create the second variant for the first (and only) argument for this command.
Johnny Chende753462011-09-22 22:34:09 +0000995 id_range_arg.arg_type = IDRange;
Johnny Chen184d7a72011-09-21 01:00:02 +0000996 id_range_arg.arg_repetition = eArgRepeatOptional;
997
Johnny Chena3234732011-09-21 01:04:49 +0000998 // The first (and only) argument for this command could be either an id or an id_range.
Johnny Chen184d7a72011-09-21 01:00:02 +0000999 // Push both variants into the entry for the first argument for this command.
1000 arg.push_back(id_arg);
1001 arg.push_back(id_range_arg);
1002}
1003
Greg Clayton9d0402b2011-02-20 02:15:07 +00001004const char *
1005CommandObject::GetArgumentTypeAsCString (const lldb::CommandArgumentType arg_type)
1006{
1007 if (arg_type >=0 && arg_type < eArgTypeLastArg)
1008 return g_arguments_data[arg_type].arg_name;
Ed Masted78c9572014-04-20 00:31:37 +00001009 return nullptr;
Greg Clayton9d0402b2011-02-20 02:15:07 +00001010
1011}
1012
1013const char *
1014CommandObject::GetArgumentDescriptionAsCString (const lldb::CommandArgumentType arg_type)
1015{
1016 if (arg_type >=0 && arg_type < eArgTypeLastArg)
1017 return g_arguments_data[arg_type].help_text;
Ed Masted78c9572014-04-20 00:31:37 +00001018 return nullptr;
Greg Clayton9d0402b2011-02-20 02:15:07 +00001019}
1020
Jim Ingham893c9322014-11-22 01:42:44 +00001021Target *
1022CommandObject::GetDummyTarget()
1023{
1024 return m_interpreter.GetDebugger().GetDummyTarget();
1025}
1026
1027Target *
Jim Ingham33df7cd2014-12-06 01:28:03 +00001028CommandObject::GetSelectedOrDummyTarget(bool prefer_dummy)
Jim Ingham893c9322014-11-22 01:42:44 +00001029{
Jim Ingham33df7cd2014-12-06 01:28:03 +00001030 return m_interpreter.GetDebugger().GetSelectedOrDummyTarget(prefer_dummy);
Jim Ingham893c9322014-11-22 01:42:44 +00001031}
1032
Jim Ingham5a988412012-06-08 21:56:10 +00001033bool
1034CommandObjectParsed::Execute (const char *args_string, CommandReturnObject &result)
1035{
Jim Ingham5a988412012-06-08 21:56:10 +00001036 bool handled = false;
1037 Args cmd_args (args_string);
Jim Ingham3b652622014-08-06 00:10:12 +00001038 if (HasOverrideCallback())
Jim Ingham5a988412012-06-08 21:56:10 +00001039 {
1040 Args full_args (GetCommandName ());
1041 full_args.AppendArguments(cmd_args);
Jim Ingham3b652622014-08-06 00:10:12 +00001042 handled = InvokeOverrideCallback (full_args.GetConstArgumentVector(), result);
Jim Ingham5a988412012-06-08 21:56:10 +00001043 }
1044 if (!handled)
1045 {
1046 for (size_t i = 0; i < cmd_args.GetArgumentCount(); ++i)
1047 {
1048 const char *tmp_str = cmd_args.GetArgumentAtIndex (i);
1049 if (tmp_str[0] == '`') // back-quote
1050 cmd_args.ReplaceArgumentAtIndex (i, m_interpreter.ProcessEmbeddedScriptCommands (tmp_str));
1051 }
1052
Greg Claytonf9fc6092013-01-09 19:44:40 +00001053 if (CheckRequirements(result))
1054 {
1055 if (ParseOptions (cmd_args, result))
1056 {
1057 // Call the command-specific version of 'Execute', passing it the already processed arguments.
1058 handled = DoExecute (cmd_args, result);
1059 }
1060 }
Jim Ingham5a988412012-06-08 21:56:10 +00001061
Greg Claytonf9fc6092013-01-09 19:44:40 +00001062 Cleanup();
Jim Ingham5a988412012-06-08 21:56:10 +00001063 }
1064 return handled;
1065}
1066
1067bool
1068CommandObjectRaw::Execute (const char *args_string, CommandReturnObject &result)
1069{
Jim Ingham5a988412012-06-08 21:56:10 +00001070 bool handled = false;
Jim Ingham3b652622014-08-06 00:10:12 +00001071 if (HasOverrideCallback())
Jim Ingham5a988412012-06-08 21:56:10 +00001072 {
1073 std::string full_command (GetCommandName ());
1074 full_command += ' ';
1075 full_command += args_string;
Ed Masted78c9572014-04-20 00:31:37 +00001076 const char *argv[2] = { nullptr, nullptr };
Jim Ingham5a988412012-06-08 21:56:10 +00001077 argv[0] = full_command.c_str();
Jim Ingham3b652622014-08-06 00:10:12 +00001078 handled = InvokeOverrideCallback (argv, result);
Jim Ingham5a988412012-06-08 21:56:10 +00001079 }
1080 if (!handled)
1081 {
Greg Claytonf9fc6092013-01-09 19:44:40 +00001082 if (CheckRequirements(result))
Jim Ingham5a988412012-06-08 21:56:10 +00001083 handled = DoExecute (args_string, result);
Greg Claytonf9fc6092013-01-09 19:44:40 +00001084
1085 Cleanup();
Jim Ingham5a988412012-06-08 21:56:10 +00001086 }
1087 return handled;
1088}
1089
Johnny Chenca7835c2012-05-26 00:32:39 +00001090static
1091const char *arch_helper()
1092{
Greg Claytond70b14e2012-05-26 17:21:14 +00001093 static StreamString g_archs_help;
Johnny Chen797a1b32012-05-29 20:04:10 +00001094 if (g_archs_help.Empty())
Greg Claytond70b14e2012-05-26 17:21:14 +00001095 {
1096 StringList archs;
Ed Masted78c9572014-04-20 00:31:37 +00001097 ArchSpec::AutoComplete(nullptr, archs);
Greg Claytond70b14e2012-05-26 17:21:14 +00001098 g_archs_help.Printf("These are the supported architecture names:\n");
Johnny Chen797a1b32012-05-29 20:04:10 +00001099 archs.Join("\n", g_archs_help);
Greg Claytond70b14e2012-05-26 17:21:14 +00001100 }
1101 return g_archs_help.GetData();
Johnny Chenca7835c2012-05-26 00:32:39 +00001102}
1103
Caroline Ticee139cf22010-10-01 17:46:38 +00001104CommandObject::ArgumentTableEntry
1105CommandObject::g_arguments_data[] =
1106{
Ed Masted78c9572014-04-20 00:31:37 +00001107 { eArgTypeAddress, "address", CommandCompletions::eNoCompletion, { nullptr, false }, "A valid address in the target program's execution space." },
1108 { eArgTypeAddressOrExpression, "address-expression", CommandCompletions::eNoCompletion, { nullptr, false }, "An expression that resolves to an address." },
1109 { eArgTypeAliasName, "alias-name", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of an abbreviation (alias) for a debugger command." },
1110 { 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 +00001111 { eArgTypeArchitecture, "arch", CommandCompletions::eArchitectureCompletion, { arch_helper, true }, "The architecture name, e.g. i386 or x86_64." },
Ed Masted78c9572014-04-20 00:31:37 +00001112 { eArgTypeBoolean, "boolean", CommandCompletions::eNoCompletion, { nullptr, false }, "A Boolean value: 'true' or 'false'" },
1113 { eArgTypeBreakpointID, "breakpt-id", CommandCompletions::eNoCompletion, { BreakpointIDHelpTextCallback, false }, nullptr },
1114 { eArgTypeBreakpointIDRange, "breakpt-id-list", CommandCompletions::eNoCompletion, { BreakpointIDRangeHelpTextCallback, false }, nullptr },
1115 { eArgTypeByteSize, "byte-size", CommandCompletions::eNoCompletion, { nullptr, false }, "Number of bytes to use." },
1116 { eArgTypeClassName, "class-name", CommandCompletions::eNoCompletion, { nullptr, false }, "Then name of a class from the debug information in the program." },
1117 { eArgTypeCommandName, "cmd-name", CommandCompletions::eNoCompletion, { nullptr, false }, "A debugger command (may be multiple words), without any options or arguments." },
1118 { eArgTypeCount, "count", CommandCompletions::eNoCompletion, { nullptr, false }, "An unsigned integer." },
1119 { eArgTypeDirectoryName, "directory", CommandCompletions::eDiskDirectoryCompletion, { nullptr, false }, "A directory name." },
1120 { 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" },
1121 { eArgTypeDescriptionVerbosity, "description-verbosity", CommandCompletions::eNoCompletion, { nullptr, false }, "How verbose the output of 'po' should be." },
1122 { eArgTypeEndAddress, "end-address", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1123 { eArgTypeExpression, "expr", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1124 { eArgTypeExpressionPath, "expr-path", CommandCompletions::eNoCompletion, { ExprPathHelpTextCallback, true }, nullptr },
1125 { eArgTypeExprFormat, "expression-format", CommandCompletions::eNoCompletion, { nullptr, false }, "[ [bool|b] | [bin] | [char|c] | [oct|o] | [dec|i|d|u] | [hex|x] | [float|f] | [cstr|s] ]" },
1126 { eArgTypeFilename, "filename", CommandCompletions::eDiskFileCompletion, { nullptr, false }, "The name of a file (can include path)." },
1127 { eArgTypeFormat, "format", CommandCompletions::eNoCompletion, { FormatHelpTextCallback, true }, nullptr },
1128 { eArgTypeFrameIndex, "frame-index", CommandCompletions::eNoCompletion, { nullptr, false }, "Index into a thread's list of frames." },
1129 { eArgTypeFullName, "fullname", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1130 { eArgTypeFunctionName, "function-name", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of a function." },
1131 { eArgTypeFunctionOrSymbol, "function-or-symbol", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of a function or symbol." },
1132 { eArgTypeGDBFormat, "gdb-format", CommandCompletions::eNoCompletion, { GDBFormatHelpTextCallback, true }, nullptr },
Enrico Granata735152e2014-09-15 17:52:44 +00001133 { 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 +00001134 { eArgTypeIndex, "index", CommandCompletions::eNoCompletion, { nullptr, false }, "An index into a list." },
1135 { eArgTypeLanguage, "language", CommandCompletions::eNoCompletion, { LanguageTypeHelpTextCallback, true }, nullptr },
1136 { eArgTypeLineNum, "linenum", CommandCompletions::eNoCompletion, { nullptr, false }, "Line number in a source file." },
1137 { 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." },
1138 { 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)." },
1139 { eArgTypeMethod, "method", CommandCompletions::eNoCompletion, { nullptr, false }, "A C++ method name." },
1140 { eArgTypeName, "name", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1141 { eArgTypeNewPathPrefix, "new-path-prefix", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1142 { eArgTypeNumLines, "num-lines", CommandCompletions::eNoCompletion, { nullptr, false }, "The number of lines to use." },
1143 { eArgTypeNumberPerLine, "number-per-line", CommandCompletions::eNoCompletion, { nullptr, false }, "The number of items per line to display." },
1144 { eArgTypeOffset, "offset", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1145 { eArgTypeOldPathPrefix, "old-path-prefix", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1146 { eArgTypeOneLiner, "one-line-command", CommandCompletions::eNoCompletion, { nullptr, false }, "A command that is entered as a single line of text." },
1147 { eArgTypePath, "path", CommandCompletions::eDiskFileCompletion, { nullptr, false }, "Path." },
1148 { eArgTypePermissionsNumber, "perms-numeric", CommandCompletions::eNoCompletion, { nullptr, false }, "Permissions given as an octal number (e.g. 755)." },
1149 { eArgTypePermissionsString, "perms=string", CommandCompletions::eNoCompletion, { nullptr, false }, "Permissions given as a string value (e.g. rw-r-xr--)." },
1150 { eArgTypePid, "pid", CommandCompletions::eNoCompletion, { nullptr, false }, "The process ID number." },
1151 { eArgTypePlugin, "plugin", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1152 { eArgTypeProcessName, "process-name", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of the process." },
1153 { eArgTypePythonClass, "python-class", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of a Python class." },
1154 { eArgTypePythonFunction, "python-function", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of a Python function." },
1155 { eArgTypePythonScript, "python-script", CommandCompletions::eNoCompletion, { nullptr, false }, "Source code written in Python." },
1156 { eArgTypeQueueName, "queue-name", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of the thread queue." },
1157 { eArgTypeRegisterName, "register-name", CommandCompletions::eNoCompletion, { RegisterNameHelpTextCallback, true }, nullptr },
1158 { eArgTypeRegularExpression, "regular-expression", CommandCompletions::eNoCompletion, { nullptr, false }, "A regular expression." },
1159 { eArgTypeRunArgs, "run-args", CommandCompletions::eNoCompletion, { nullptr, false }, "Arguments to be passed to the target program when it starts executing." },
1160 { eArgTypeRunMode, "run-mode", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1161 { eArgTypeScriptedCommandSynchronicity, "script-cmd-synchronicity", CommandCompletions::eNoCompletion, { nullptr, false }, "The synchronicity to use to run scripted commands with regard to LLDB event system." },
1162 { eArgTypeScriptLang, "script-language", CommandCompletions::eNoCompletion, { nullptr, false }, "The scripting language to be used for script-based commands. Currently only Python is valid." },
1163 { eArgTypeSearchWord, "search-word", CommandCompletions::eNoCompletion, { nullptr, false }, "The word for which you wish to search for information about." },
1164 { eArgTypeSelector, "selector", CommandCompletions::eNoCompletion, { nullptr, false }, "An Objective-C selector name." },
1165 { 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)." },
1166 { 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)." },
1167 { eArgTypeSettingPrefix, "setting-prefix", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of a settable internal debugger variable up to a dot ('.'), e.g. 'target.process.'" },
1168 { 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." },
1169 { eArgTypeShlibName, "shlib-name", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of a shared library." },
1170 { eArgTypeSourceFile, "source-file", CommandCompletions::eSourceFileCompletion, { nullptr, false }, "The name of a source file.." },
1171 { eArgTypeSortOrder, "sort-order", CommandCompletions::eNoCompletion, { nullptr, false }, "Specify a sort order when dumping lists." },
1172 { eArgTypeStartAddress, "start-address", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1173 { eArgTypeSummaryString, "summary-string", CommandCompletions::eNoCompletion, { SummaryStringHelpTextCallback, true }, nullptr },
1174 { eArgTypeSymbol, "symbol", CommandCompletions::eSymbolCompletion, { nullptr, false }, "Any symbol name (function name, variable, argument, etc.)" },
1175 { eArgTypeThreadID, "thread-id", CommandCompletions::eNoCompletion, { nullptr, false }, "Thread ID number." },
1176 { eArgTypeThreadIndex, "thread-index", CommandCompletions::eNoCompletion, { nullptr, false }, "Index into the process' list of threads." },
1177 { eArgTypeThreadName, "thread-name", CommandCompletions::eNoCompletion, { nullptr, false }, "The thread's name." },
1178 { eArgTypeUnsignedInteger, "unsigned-integer", CommandCompletions::eNoCompletion, { nullptr, false }, "An unsigned integer." },
1179 { eArgTypeUnixSignal, "unix-signal", CommandCompletions::eNoCompletion, { nullptr, false }, "A valid Unix signal name or number (e.g. SIGKILL, KILL or 9)." },
1180 { eArgTypeVarName, "variable-name", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of a variable in your program." },
1181 { eArgTypeValue, "value", CommandCompletions::eNoCompletion, { nullptr, false }, "A value could be anything, depending on where and how it is used." },
1182 { eArgTypeWidth, "width", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1183 { eArgTypeNone, "none", CommandCompletions::eNoCompletion, { nullptr, false }, "No help available for this." },
1184 { 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." },
1185 { eArgTypeWatchpointID, "watchpt-id", CommandCompletions::eNoCompletion, { nullptr, false }, "Watchpoint IDs are positive integers." },
1186 { eArgTypeWatchpointIDRange, "watchpt-id-list", CommandCompletions::eNoCompletion, { nullptr, false }, "For example, '1-3' or '1 to 3'." },
1187 { eArgTypeWatchType, "watch-type", CommandCompletions::eNoCompletion, { nullptr, false }, "Specify the type for a watchpoint." }
Caroline Ticee139cf22010-10-01 17:46:38 +00001188};
1189
1190const CommandObject::ArgumentTableEntry*
1191CommandObject::GetArgumentTable ()
1192{
Greg Clayton9d0402b2011-02-20 02:15:07 +00001193 // If this assertion fires, then the table above is out of date with the CommandArgumentType enumeration
1194 assert ((sizeof (CommandObject::g_arguments_data) / sizeof (CommandObject::ArgumentTableEntry)) == eArgTypeLastArg);
Caroline Ticee139cf22010-10-01 17:46:38 +00001195 return CommandObject::g_arguments_data;
1196}
1197
1198