LLDB mainline
CommandObject.cpp
Go to the documentation of this file.
1//===-- CommandObject.cpp -------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10
11#include <map>
12#include <sstream>
13#include <string>
14
15#include <cctype>
16#include <cstdlib>
17
18#include "lldb/Core/Address.h"
22#include "llvm/ADT/ScopeExit.h"
23
24// These are for the Sourcename completers.
25// FIXME: Make a separate file for the completers.
27#include "lldb/Target/Process.h"
28#include "lldb/Target/Target.h"
31
33
37
38using namespace lldb;
39using namespace lldb_private;
40
41// CommandObject
42
44 llvm::StringRef name, llvm::StringRef help,
45 llvm::StringRef syntax, uint32_t flags)
46 : m_interpreter(interpreter), m_cmd_name(std::string(name)),
47 m_flags(flags), m_deprecated_command_override_callback(nullptr),
48 m_command_override_callback(nullptr), m_command_override_baton(nullptr) {
49 m_cmd_help_short = std::string(help);
50 m_cmd_syntax = std::string(syntax);
51}
52
54
55llvm::StringRef CommandObject::GetHelp() { return m_cmd_help_short; }
56
57llvm::StringRef CommandObject::GetHelpLong() { return m_cmd_help_long; }
58
59llvm::StringRef CommandObject::GetSyntax() {
60 if (!m_cmd_syntax.empty())
61 return m_cmd_syntax;
62
63 StreamString syntax_str;
64 syntax_str.PutCString(GetCommandName());
65
66 if (!IsDashDashCommand() && GetOptions() != nullptr)
67 syntax_str.PutCString(" <cmd-options>");
68
69 if (!m_arguments.empty()) {
70 syntax_str.PutCString(" ");
71
73 GetOptions()->NumCommandOptions())
74 syntax_str.PutCString("-- ");
76 }
77 m_cmd_syntax = std::string(syntax_str.GetString());
78
79 return m_cmd_syntax;
80}
81
82llvm::StringRef CommandObject::GetCommandName() const { return m_cmd_name; }
83
84void CommandObject::SetCommandName(llvm::StringRef name) {
85 m_cmd_name = std::string(name);
86}
87
88void CommandObject::SetHelp(llvm::StringRef str) {
89 m_cmd_help_short = std::string(str);
90}
91
92void CommandObject::SetHelpLong(llvm::StringRef str) {
93 m_cmd_help_long = std::string(str);
94}
95
96void CommandObject::SetSyntax(llvm::StringRef str) {
97 m_cmd_syntax = std::string(str);
98}
99
101 // By default commands don't have options unless this virtual function is
102 // overridden by base classes.
103 return nullptr;
104}
105
107 // See if the subclass has options?
108 Options *options = GetOptions();
109 if (options != nullptr) {
111
113 options->NotifyOptionParsingStarting(&exe_ctx);
114
115 const bool require_validation = true;
116 llvm::Expected<Args> args_or = options->Parse(
117 args, &exe_ctx, GetCommandInterpreter().GetPlatform(true),
118 require_validation);
119
120 if (args_or) {
121 args = std::move(*args_or);
122 error = options->NotifyOptionParsingFinished(&exe_ctx);
123 } else
124 error = Status::FromError(args_or.takeError());
125
126 if (error.Success()) {
127 if (options->VerifyOptions(result))
128 return true;
129 } else {
130 result.SetError(error.takeError());
131 }
133 return false;
134 }
135 return true;
136}
137
139 // Nothing should be stored in m_exe_ctx between running commands as
140 // m_exe_ctx has shared pointers to the target, process, thread and frame and
141 // we don't want any CommandObject instances to keep any of these objects
142 // around longer than for a single command. Every command should call
143 // CommandObject::Cleanup() after it has completed.
144 assert(!m_exe_ctx.GetTargetPtr());
145 assert(!m_exe_ctx.GetProcessPtr());
146 assert(!m_exe_ctx.GetThreadPtr());
147 assert(!m_exe_ctx.GetFramePtr());
148
149 // Lock down the interpreter's execution context prior to running the command
150 // so we guarantee the selected target, process, thread and frame can't go
151 // away during the execution
153
154 const uint32_t flags = GetFlags().Get();
155 if (flags & (eCommandRequiresTarget | eCommandRequiresProcess |
156 eCommandRequiresThread | eCommandRequiresFrame |
157 eCommandTryTargetAPILock)) {
158
159 if ((flags & eCommandRequiresTarget) && !m_exe_ctx.HasTargetScope()) {
161 return false;
162 }
163
164 if ((flags & eCommandRequiresProcess) && !m_exe_ctx.HasProcessScope()) {
167 else
169 return false;
170 }
171
172 if ((flags & eCommandRequiresThread) && !m_exe_ctx.HasThreadScope()) {
175 else if (!m_exe_ctx.HasProcessScope())
177 else
179 return false;
180 }
181
182 if ((flags & eCommandRequiresFrame) && !m_exe_ctx.HasFrameScope()) {
185 else if (!m_exe_ctx.HasProcessScope())
187 else if (!m_exe_ctx.HasThreadScope())
189 else
191 return false;
192 }
193
194 if ((flags & eCommandRequiresRegContext) &&
195 (m_exe_ctx.GetRegisterContext() == nullptr)) {
197 return false;
198 }
199
200 if (flags & eCommandTryTargetAPILock) {
201 Target *target = m_exe_ctx.GetTargetPtr();
202 if (target)
204 std::unique_lock<std::recursive_mutex>(target->GetAPIMutex());
205 }
206 }
207
208 if (GetFlags().AnySet(eCommandProcessMustBeLaunched |
209 eCommandProcessMustBePaused)) {
211 if (process == nullptr) {
212 // A process that is not running is considered paused.
213 if (GetFlags().Test(eCommandProcessMustBeLaunched)) {
214 result.AppendError("Process must exist.");
215 return false;
216 }
217 } else {
218 StateType state = process->GetState();
219 switch (state) {
220 case eStateInvalid:
221 case eStateSuspended:
222 case eStateCrashed:
223 case eStateStopped:
224 break;
225
226 case eStateConnected:
227 case eStateAttaching:
228 case eStateLaunching:
229 case eStateDetached:
230 case eStateExited:
231 case eStateUnloaded:
232 if (GetFlags().Test(eCommandProcessMustBeLaunched)) {
233 result.AppendError("Process must be launched.");
234 return false;
235 }
236 break;
237
238 case eStateRunning:
239 case eStateStepping:
240 if (GetFlags().Test(eCommandProcessMustBePaused)) {
241 result.AppendError("Process is running. Use 'process interrupt' to "
242 "pause execution.");
243 return false;
244 }
245 }
246 }
247 }
248
249 if (GetFlags().Test(eCommandProcessMustBeTraced)) {
250 Target *target = m_exe_ctx.GetTargetPtr();
251 if (target && !target->GetTrace()) {
252 result.AppendError("Process is not being traced.");
253 return false;
254 }
255 }
256
257 return true;
258}
259
262 if (m_api_locker.owns_lock())
263 m_api_locker.unlock();
264}
265
267
269 auto reset_ctx = llvm::make_scope_exit([this]() { Cleanup(); });
270
271 // Default implementation of WantsCompletion() is !WantsRawCommandString().
272 // Subclasses who want raw command string but desire, for example, argument
273 // completion should override WantsCompletion() to return true, instead.
275 // FIXME: Abstract telling the completion to insert the completion
276 // character.
277 return;
278 } else {
279 // Can we do anything generic with the options?
280 Options *cur_options = GetOptions();
282 OptionElementVector opt_element_vector;
283
284 if (cur_options != nullptr) {
285 opt_element_vector = cur_options->ParseForCompletion(
286 request.GetParsedLine(), request.GetCursorIndex());
287
288 bool handled_by_options = cur_options->HandleOptionCompletion(
289 request, opt_element_vector, GetCommandInterpreter());
290 if (handled_by_options)
291 return;
292 }
293
294 // If we got here, the last word is not an option or an option argument.
295 HandleArgumentCompletion(request, opt_element_vector);
296 }
297}
298
300 CompletionRequest &request, OptionElementVector &opt_element_vector) {
301 size_t num_arg_entries = GetNumArgumentEntries();
302 if (num_arg_entries != 1)
303 return;
304
306 if (!entry_ptr) {
307 assert(entry_ptr && "We said there was one entry, but there wasn't.");
308 return; // Not worth crashing if asserts are off...
309 }
310
311 CommandArgumentEntry &entry = *entry_ptr;
312 // For now, we only handle the simple case of one homogenous argument type.
313 if (entry.size() != 1)
314 return;
315
316 // Look up the completion type, and if it has one, invoke it:
317 const CommandObject::ArgumentTableEntry *arg_entry =
318 FindArgumentDataByType(entry[0].arg_type);
319 const ArgumentRepetitionType repeat = entry[0].arg_repetition;
320
321 if (arg_entry == nullptr || arg_entry->completion_type == lldb::eNoCompletion)
322 return;
323
324 // FIXME: This should be handled higher in the Command Parser.
325 // Check the case where this command only takes one argument, and don't do
326 // the completion if we aren't on the first entry:
327 if (repeat == eArgRepeatPlain && request.GetCursorIndex() != 0)
328 return;
329
331 GetCommandInterpreter(), arg_entry->completion_type, request, nullptr);
332
333}
334
335
336bool CommandObject::HelpTextContainsWord(llvm::StringRef search_word,
337 bool search_short_help,
338 bool search_long_help,
339 bool search_syntax,
340 bool search_options) {
341 std::string options_usage_help;
342
343 bool found_word = false;
344
345 llvm::StringRef short_help = GetHelp();
346 llvm::StringRef long_help = GetHelpLong();
347 llvm::StringRef syntax_help = GetSyntax();
348
349 if (search_short_help && short_help.contains_insensitive(search_word))
350 found_word = true;
351 else if (search_long_help && long_help.contains_insensitive(search_word))
352 found_word = true;
353 else if (search_syntax && syntax_help.contains_insensitive(search_word))
354 found_word = true;
355
356 if (!found_word && search_options && GetOptions() != nullptr) {
357 StreamString usage_help;
359 usage_help, *this,
360 GetCommandInterpreter().GetDebugger().GetTerminalWidth());
361 if (!usage_help.Empty()) {
362 llvm::StringRef usage_text = usage_help.GetString();
363 if (usage_text.contains_insensitive(search_word))
364 found_word = true;
365 }
366 }
367
368 return found_word;
369}
370
372 CommandReturnObject &result,
373 OptionGroupOptions &group_options,
374 ExecutionContext &exe_ctx) {
375 if (!ParseOptions(args, result))
376 return false;
377
378 Status error(group_options.NotifyOptionParsingFinished(&exe_ctx));
379 if (error.Fail()) {
380 result.AppendError(error.AsCString());
381 return false;
382 }
383 return true;
384}
385
387 CommandArgumentType arg_type, ArgumentRepetitionType repetition_type) {
388
389 CommandArgumentEntry arg_entry;
390 CommandArgumentData simple_arg;
391
392 // Define the first (and only) variant of this arg.
393 simple_arg.arg_type = arg_type;
394 simple_arg.arg_repetition = repetition_type;
395
396 // There is only one variant this argument could be; put it into the argument
397 // entry.
398 arg_entry.push_back(simple_arg);
399
400 // Push the data for the first argument into the m_arguments vector.
401 m_arguments.push_back(arg_entry);
402}
403
405
408 if (static_cast<size_t>(idx) < m_arguments.size())
409 return &(m_arguments[idx]);
410
411 return nullptr;
412}
413
416 for (int i = 0; i < eArgTypeLastArg; ++i)
417 if (g_argument_table[i].arg_type == arg_type)
418 return &(g_argument_table[i]);
419
420 return nullptr;
421}
422
424 CommandInterpreter &interpreter) {
425 const ArgumentTableEntry *entry = &(g_argument_table[arg_type]);
426
427 // The table is *supposed* to be kept in arg_type order, but someone *could*
428 // have messed it up...
429
430 if (entry->arg_type != arg_type)
432
433 if (!entry)
434 return;
435
436 StreamString name_str;
437 name_str.Printf("<%s>", entry->arg_name);
438
439 if (entry->help_function) {
440 llvm::StringRef help_text = entry->help_function();
441 if (!entry->help_function.self_formatting) {
442 interpreter.OutputFormattedHelpText(str, name_str.GetString(), "--",
443 help_text, name_str.GetSize());
444 } else {
445 interpreter.OutputHelpText(str, name_str.GetString(), "--", help_text,
446 name_str.GetSize());
447 }
448 } else {
449 interpreter.OutputFormattedHelpText(str, name_str.GetString(), "--",
450 entry->help_text, name_str.GetSize());
451
452 // Print enum values and their description if any.
453 OptionEnumValues enum_values = g_argument_table[arg_type].enum_values;
454 if (!enum_values.empty()) {
455 str.EOL();
456 size_t longest = 0;
457 for (const OptionEnumValueElement &element : enum_values)
458 longest =
459 std::max(longest, llvm::StringRef(element.string_value).size());
460 str.IndentMore(5);
461 for (const OptionEnumValueElement &element : enum_values) {
462 str.Indent();
463 interpreter.OutputHelpText(str, element.string_value, ":",
464 element.usage, longest);
465 }
466 str.IndentLess(5);
467 str.EOL();
468 }
469 }
470}
471
473 const ArgumentTableEntry *entry = &(g_argument_table[arg_type]);
474
475 // The table is *supposed* to be kept in arg_type order, but someone *could*
476 // have messed it up...
477
478 if (entry->arg_type != arg_type)
480
481 if (entry)
482 return entry->arg_name;
483
484 return nullptr;
485}
486
488 return (arg_repeat_type == eArgRepeatPairPlain) ||
489 (arg_repeat_type == eArgRepeatPairOptional) ||
490 (arg_repeat_type == eArgRepeatPairPlus) ||
491 (arg_repeat_type == eArgRepeatPairStar) ||
492 (arg_repeat_type == eArgRepeatPairRange) ||
493 (arg_repeat_type == eArgRepeatPairRangeOptional);
494}
495
496std::optional<ArgumentRepetitionType>
498 return llvm::StringSwitch<ArgumentRepetitionType>(string)
499 .Case("plain", eArgRepeatPlain)
500 .Case("optional", eArgRepeatOptional)
501 .Case("plus", eArgRepeatPlus)
502 .Case("star", eArgRepeatStar)
503 .Case("range", eArgRepeatRange)
504 .Case("pair-plain", eArgRepeatPairPlain)
505 .Case("pair-optional", eArgRepeatPairOptional)
506 .Case("pair-plus", eArgRepeatPairPlus)
507 .Case("pair-star", eArgRepeatPairStar)
508 .Case("pair-range", eArgRepeatPairRange)
509 .Case("pair-range-optional", eArgRepeatPairRangeOptional)
510 .Default({});
511}
512
514OptSetFiltered(uint32_t opt_set_mask,
517 for (unsigned i = 0; i < cmd_arg_entry.size(); ++i)
518 if (opt_set_mask & cmd_arg_entry[i].arg_opt_set_association)
519 ret_val.push_back(cmd_arg_entry[i]);
520 return ret_val;
521}
522
523// Default parameter value of opt_set_mask is LLDB_OPT_SET_ALL, which means
524// take all the argument data into account. On rare cases where some argument
525// sticks with certain option sets, this function returns the option set
526// filtered args.
528 uint32_t opt_set_mask) {
529 int num_args = m_arguments.size();
530 for (int i = 0; i < num_args; ++i) {
531 if (i > 0)
532 str.Printf(" ");
533 CommandArgumentEntry arg_entry =
534 opt_set_mask == LLDB_OPT_SET_ALL
535 ? m_arguments[i]
536 : OptSetFiltered(opt_set_mask, m_arguments[i]);
537 // This argument is not associated with the current option set, so skip it.
538 if (arg_entry.empty())
539 continue;
540 int num_alternatives = arg_entry.size();
541
542 if ((num_alternatives == 2) && IsPairType(arg_entry[0].arg_repetition)) {
543 const char *first_name = GetArgumentName(arg_entry[0].arg_type);
544 const char *second_name = GetArgumentName(arg_entry[1].arg_type);
545 switch (arg_entry[0].arg_repetition) {
547 str.Printf("<%s> <%s>", first_name, second_name);
548 break;
550 str.Printf("[<%s> <%s>]", first_name, second_name);
551 break;
553 str.Printf("<%s> <%s> [<%s> <%s> [...]]", first_name, second_name,
554 first_name, second_name);
555 break;
557 str.Printf("[<%s> <%s> [<%s> <%s> [...]]]", first_name, second_name,
558 first_name, second_name);
559 break;
561 str.Printf("<%s_1> <%s_1> ... <%s_n> <%s_n>", first_name, second_name,
562 first_name, second_name);
563 break;
565 str.Printf("[<%s_1> <%s_1> ... <%s_n> <%s_n>]", first_name, second_name,
566 first_name, second_name);
567 break;
568 // Explicitly test for all the rest of the cases, so if new types get
569 // added we will notice the missing case statement(s).
570 case eArgRepeatPlain:
572 case eArgRepeatPlus:
573 case eArgRepeatStar:
574 case eArgRepeatRange:
575 // These should not be reached, as they should fail the IsPairType test
576 // above.
577 break;
578 }
579 } else {
580 StreamString names;
581 for (int j = 0; j < num_alternatives; ++j) {
582 if (j > 0)
583 names.Printf(" | ");
584 names.Printf("%s", GetArgumentName(arg_entry[j].arg_type));
585 }
586
587 std::string name_str = std::string(names.GetString());
588 switch (arg_entry[0].arg_repetition) {
589 case eArgRepeatPlain:
590 str.Printf("<%s>", name_str.c_str());
591 break;
592 case eArgRepeatPlus:
593 str.Printf("<%s> [<%s> [...]]", name_str.c_str(), name_str.c_str());
594 break;
595 case eArgRepeatStar:
596 str.Printf("[<%s> [<%s> [...]]]", name_str.c_str(), name_str.c_str());
597 break;
599 str.Printf("[<%s>]", name_str.c_str());
600 break;
601 case eArgRepeatRange:
602 str.Printf("<%s_1> .. <%s_n>", name_str.c_str(), name_str.c_str());
603 break;
604 // Explicitly test for all the rest of the cases, so if new types get
605 // added we will notice the missing case statement(s).
612 // These should not be hit, as they should pass the IsPairType test
613 // above, and control should have gone into the other branch of the if
614 // statement.
615 break;
616 }
617 }
618 }
619}
620
622CommandObject::LookupArgumentName(llvm::StringRef arg_name) {
624
625 arg_name = arg_name.ltrim('<').rtrim('>');
626
627 for (int i = 0; i < eArgTypeLastArg; ++i)
628 if (arg_name == g_argument_table[i].arg_name)
629 return_type = g_argument_table[i].arg_type;
630
631 return return_type;
632}
633
635 llvm::StringRef long_help) {
637 std::stringstream lineStream{std::string(long_help)};
638 std::string line;
639 while (std::getline(lineStream, line)) {
640 if (line.empty()) {
641 output_strm << "\n";
642 continue;
643 }
644 size_t result = line.find_first_not_of(" \t");
645 if (result == std::string::npos) {
646 result = 0;
647 }
648 std::string whitespace_prefix = line.substr(0, result);
649 std::string remainder = line.substr(result);
650 interpreter.OutputFormattedHelpText(output_strm, whitespace_prefix,
651 remainder);
652 }
653}
654
657
659}
660
663 std::string help_text(GetHelp());
664 if (WantsRawCommandString()) {
665 help_text.append(" Expects 'raw' input (see 'help raw-input'.)");
666 }
667 interpreter.OutputFormattedHelpText(output_strm, "", help_text);
668 output_strm << "\nSyntax: " << GetSyntax() << "\n";
669 Options *options = GetOptions();
670 if (options != nullptr) {
671 options->GenerateOptionUsage(
672 output_strm, *this,
673 GetCommandInterpreter().GetDebugger().GetTerminalWidth());
674 }
675 llvm::StringRef long_help = GetHelpLong();
676 if (!long_help.empty()) {
677 FormatLongHelpText(output_strm, long_help);
678 }
679 if (!IsDashDashCommand() && options && options->NumCommandOptions() > 0) {
681 // Emit the message about using ' -- ' between the end of the command
682 // options and the raw input conditionally, i.e., only if the command
683 // object does not want completion.
684 interpreter.OutputFormattedHelpText(
685 output_strm, "", "",
686 "\nImportant Note: Because this command takes 'raw' input, if you "
687 "use any command options"
688 " you must use ' -- ' between the end of the command options and the "
689 "beginning of the raw input.",
690 1);
691 } else if (GetNumArgumentEntries() > 0) {
692 // Also emit a warning about using "--" in case you are using a command
693 // that takes options and arguments.
694 interpreter.OutputFormattedHelpText(
695 output_strm, "", "",
696 "\nThis command takes options and free-form arguments. If your "
697 "arguments resemble"
698 " option specifiers (i.e., they start with a - or --), you must use "
699 "' -- ' between"
700 " the end of the command options and the beginning of the arguments.",
701 1);
702 }
703 }
704}
705
708 CommandArgumentData id_arg;
709 CommandArgumentData id_range_arg;
710
711 // Create the first variant for the first (and only) argument for this
712 // command.
713 switch (type) {
714 case eBreakpointArgs:
716 id_range_arg.arg_type = eArgTypeBreakpointIDRange;
717 break;
718 case eWatchpointArgs:
720 id_range_arg.arg_type = eArgTypeWatchpointIDRange;
721 break;
722 }
724 id_range_arg.arg_repetition = eArgRepeatOptional;
725
726 // The first (and only) argument for this command could be either an id or an
727 // id_range. Push both variants into the entry for the first argument for
728 // this command.
729 arg.push_back(id_arg);
730 arg.push_back(id_range_arg);
731 m_arguments.push_back(arg);
732}
733
735 const lldb::CommandArgumentType arg_type) {
736 assert(arg_type < eArgTypeLastArg &&
737 "Invalid argument type passed to GetArgumentTypeAsCString");
738 return g_argument_table[arg_type].arg_name;
739}
740
742 const lldb::CommandArgumentType arg_type) {
743 assert(arg_type < eArgTypeLastArg &&
744 "Invalid argument type passed to GetArgumentDescriptionAsCString");
745 return g_argument_table[arg_type].help_text;
746}
747
750}
751
753 // Prefer the frozen execution context in the command object.
754 if (Target *target = m_exe_ctx.GetTargetPtr())
755 return *target;
756
757 // Fallback to the command interpreter's execution context in case we get
758 // called after DoExecute has finished. For example, when doing multi-line
759 // expression that uses an input reader or breakpoint callbacks.
761 return *target;
762
763 // Finally, if we have no other target, get the selected target.
765 return *target_sp;
766
767 // We only have the dummy target.
768 return GetDummyTarget();
769}
770
772 Thread *thread_to_use = m_exe_ctx.GetThreadPtr();
773 if (thread_to_use)
774 return thread_to_use;
775
776 Process *process = m_exe_ctx.GetProcessPtr();
777 if (!process) {
778 Target *target = m_exe_ctx.GetTargetPtr();
779 if (!target) {
781 }
782 if (target)
783 process = target->GetProcessSP().get();
784 }
785
786 if (process)
787 return process->GetThreadList().GetSelectedThread().get();
788 else
789 return nullptr;
790}
791
792void CommandObjectParsed::Execute(const char *args_string,
793 CommandReturnObject &result) {
794 bool handled = false;
795 Args cmd_args(args_string);
796 if (HasOverrideCallback()) {
797 Args full_args(GetCommandName());
798 full_args.AppendArguments(cmd_args);
799 handled =
801 }
802 if (!handled) {
803 for (auto entry : llvm::enumerate(cmd_args.entries())) {
804 const Args::ArgEntry &value = entry.value();
805 if (!value.ref().empty() && value.GetQuoteChar() == '`') {
806 // We have to put the backtick back in place for PreprocessCommand.
807 std::string opt_string = value.c_str();
809 error = m_interpreter.PreprocessToken(opt_string);
810 if (error.Success())
811 cmd_args.ReplaceArgumentAtIndex(entry.index(), opt_string);
812 }
813 }
814
815 if (CheckRequirements(result)) {
816 if (ParseOptions(cmd_args, result)) {
817 // Call the command-specific version of 'Execute', passing it the
818 // already processed arguments.
819 if (cmd_args.GetArgumentCount() != 0 && m_arguments.empty()) {
820 result.AppendErrorWithFormatv("'{0}' doesn't take any arguments.",
822 Cleanup();
823 return;
824 }
826 DoExecute(cmd_args, result);
827 }
828 }
829
830 Cleanup();
831 }
832}
833
834void CommandObjectRaw::Execute(const char *args_string,
835 CommandReturnObject &result) {
836 bool handled = false;
837 if (HasOverrideCallback()) {
838 std::string full_command(GetCommandName());
839 full_command += ' ';
840 full_command += args_string;
841 const char *argv[2] = {nullptr, nullptr};
842 argv[0] = full_command.c_str();
843 handled = InvokeOverrideCallback(argv, result);
844 }
845 if (!handled) {
846 if (CheckRequirements(result))
847 DoExecute(args_string, result);
848
849 Cleanup();
850 }
851}
static CommandObject::CommandArgumentEntry OptSetFiltered(uint32_t opt_set_mask, CommandObject::CommandArgumentEntry &cmd_arg_entry)
static llvm::raw_ostream & error(Stream &strm)
A command line argument class.
Definition: Args.h:33
void AppendArguments(const Args &rhs)
Definition: Args.cpp:307
size_t GetArgumentCount() const
Gets the number of arguments left in this command object.
Definition: Args.h:120
void ReplaceArgumentAtIndex(size_t idx, llvm::StringRef arg_str, char quote_char='\0')
Replaces the argument value at index idx to arg_str if idx is a valid argument index.
Definition: Args.cpp:347
llvm::ArrayRef< ArgEntry > entries() const
Definition: Args.h:132
const char ** GetConstArgumentVector() const
Gets the argument vector.
Definition: Args.cpp:289
static bool InvokeCommonCompletionCallbacks(CommandInterpreter &interpreter, uint32_t completion_mask, lldb_private::CompletionRequest &request, SearchFilter *searcher)
void OutputFormattedHelpText(Stream &strm, llvm::StringRef prefix, llvm::StringRef help_text)
void IncreaseCommandUsage(const CommandObject &cmd_obj)
ExecutionContext GetExecutionContext() const
void OutputHelpText(Stream &stream, llvm::StringRef command_word, llvm::StringRef separator, llvm::StringRef help_text, uint32_t max_word_len)
Status PreprocessToken(std::string &token)
virtual void DoExecute(Args &command, CommandReturnObject &result)=0
void Execute(const char *args_string, CommandReturnObject &result) override
void Execute(const char *args_string, CommandReturnObject &result) override
virtual void DoExecute(llvm::StringRef command, CommandReturnObject &result)=0
std::vector< CommandArgumentData > CommandArgumentEntry
CommandArgumentEntry * GetArgumentEntryAtIndex(int idx)
virtual void SetHelpLong(llvm::StringRef str)
virtual bool WantsRawCommandString()=0
void GenerateHelpText(CommandReturnObject &result)
void AddSimpleArgumentList(lldb::CommandArgumentType arg_type, ArgumentRepetitionType repetition_type=eArgRepeatPlain)
std::unique_lock< std::recursive_mutex > m_api_locker
bool ParseOptionsAndNotify(Args &args, CommandReturnObject &result, OptionGroupOptions &group_options, ExecutionContext &exe_ctx)
virtual const char * GetInvalidProcessDescription()
virtual llvm::StringRef GetHelpLong()
static const ArgumentTableEntry * FindArgumentDataByType(lldb::CommandArgumentType arg_type)
llvm::StringRef GetCommandName() const
static std::optional< ArgumentRepetitionType > ArgRepetitionFromString(llvm::StringRef string)
static lldb::CommandArgumentType LookupArgumentName(llvm::StringRef arg_name)
void GetFormattedCommandArguments(Stream &str, uint32_t opt_set_mask=LLDB_OPT_SET_ALL)
bool HelpTextContainsWord(llvm::StringRef search_word, bool search_short_help=true, bool search_long_help=true, bool search_syntax=true, bool search_options=true)
ExecutionContext m_exe_ctx
virtual const char * GetInvalidTargetDescription()
std::vector< CommandArgumentEntry > m_arguments
virtual bool WantsCompletion()
void AddIDsArgumentData(IDType type)
CommandInterpreter & GetCommandInterpreter()
virtual bool IsDashDashCommand()
static const char * GetArgumentTypeAsCString(const lldb::CommandArgumentType arg_type)
CommandInterpreter & m_interpreter
virtual const char * GetInvalidRegContextDescription()
virtual Options * GetOptions()
void SetSyntax(llvm::StringRef str)
static const char * GetArgumentDescriptionAsCString(const lldb::CommandArgumentType arg_type)
CommandObject(CommandInterpreter &interpreter, llvm::StringRef name, llvm::StringRef help="", llvm::StringRef syntax="", uint32_t flags=0)
virtual const char * GetInvalidFrameDescription()
void SetCommandName(llvm::StringRef name)
Flags & GetFlags()
The flags accessor.
virtual void HandleArgumentCompletion(CompletionRequest &request, OptionElementVector &opt_element_vector)
The default version handles argument definitions that have only one argument type,...
bool ParseOptions(Args &args, CommandReturnObject &result)
void FormatLongHelpText(Stream &output_strm, llvm::StringRef long_help)
virtual llvm::StringRef GetSyntax()
virtual const char * GetInvalidThreadDescription()
static void GetArgumentHelp(Stream &str, lldb::CommandArgumentType arg_type, CommandInterpreter &interpreter)
bool CheckRequirements(CommandReturnObject &result)
Check the command to make sure anything required by this command is available.
virtual void HandleCompletion(CompletionRequest &request)
This default version handles calling option argument completions and then calls HandleArgumentComplet...
static bool IsPairType(ArgumentRepetitionType arg_repeat_type)
static const char * GetArgumentName(lldb::CommandArgumentType arg_type)
virtual llvm::StringRef GetHelp()
bool InvokeOverrideCallback(const char **argv, CommandReturnObject &result)
virtual void SetHelp(llvm::StringRef str)
void AppendErrorWithFormatv(const char *format, Args &&... args)
void void AppendError(llvm::StringRef in_string)
void SetStatus(lldb::ReturnStatus status)
"lldb/Utility/ArgCompletionRequest.h"
const Args & GetParsedLine() const
A class to manage flag bits.
Definition: Debugger.h:80
lldb::TargetSP GetSelectedTarget()
Definition: Debugger.h:185
bool GetUseColor() const
Definition: Debugger.cpp:421
Target & GetDummyTarget()
Definition: Debugger.h:496
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
bool HasThreadScope() const
Returns true the ExecutionContext object contains a valid target, process, and thread.
bool HasProcessScope() const
Returns true the ExecutionContext object contains a valid target and process.
void Clear()
Clear the object's state.
StackFrame * GetFramePtr() const
Returns a pointer to the frame object.
Target * GetTargetPtr() const
Returns a pointer to the target object.
bool HasTargetScope() const
Returns true the ExecutionContext object contains a valid target.
bool HasFrameScope() const
Returns true the ExecutionContext object contains a valid target, process, thread and frame.
Process * GetProcessPtr() const
Returns a pointer to the process object.
RegisterContext * GetRegisterContext() const
Thread * GetThreadPtr() const
Returns a pointer to the thread object.
ValueType Get() const
Get accessor for all flags.
Definition: Flags.h:40
A command line option parsing protocol class.
Definition: Options.h:58
bool VerifyOptions(CommandReturnObject &result)
Definition: Options.cpp:141
void GenerateOptionUsage(Stream &strm, CommandObject &cmd, uint32_t screen_width)
Definition: Options.cpp:427
uint32_t NumCommandOptions()
Definition: Options.cpp:235
Status NotifyOptionParsingFinished(ExecutionContext *execution_context)
Definition: Options.cpp:75
void NotifyOptionParsingStarting(ExecutionContext *execution_context)
Definition: Options.cpp:68
bool HandleOptionCompletion(lldb_private::CompletionRequest &request, OptionElementVector &option_map, CommandInterpreter &interpreter)
Handles the generic bits of figuring out whether we are in an option, and if so completing it.
Definition: Options.cpp:619
llvm::Expected< Args > Parse(const Args &args, ExecutionContext *execution_context, lldb::PlatformSP platform_sp, bool require_validation)
Parse the provided arguments.
Definition: Options.cpp:1277
OptionElementVector ParseForCompletion(const Args &args, uint32_t cursor_index)
Definition: Options.cpp:1099
A plug-in interface definition class for debugging a process.
Definition: Process.h:343
ThreadList & GetThreadList()
Definition: Process.h:2182
lldb::StateType GetState()
Get accessor for the current process state.
Definition: Process.cpp:1308
An error handling class.
Definition: Status.h:115
static Status FromError(llvm::Error error)
Avoid using this in new code. Migrate APIs to llvm::Expected instead.
Definition: Status.cpp:137
llvm::StringRef GetString() const
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
size_t Indent(llvm::StringRef s="")
Indent the current line in the stream.
Definition: Stream.cpp:157
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:134
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:65
size_t EOL()
Output and End of Line character to the stream.
Definition: Stream.cpp:155
void IndentLess(unsigned amount=2)
Decrement the current indentation level.
Definition: Stream.cpp:198
void IndentMore(unsigned amount=2)
Increment the current indentation level.
Definition: Stream.cpp:195
lldb::TraceSP GetTrace()
Get the Trace object containing processor trace information of this target.
Definition: Target.cpp:3489
const lldb::ProcessSP & GetProcessSP() const
Definition: Target.cpp:297
std::recursive_mutex & GetAPIMutex()
Definition: Target.cpp:5136
lldb::ThreadSP GetSelectedThread()
Definition: ThreadList.cpp:683
#define LLDB_OPT_SET_ALL
Definition: lldb-defines.h:110
A class that represents a running process on the host machine.
std::vector< OptionArgElement > OptionElementVector
Definition: Options.h:43
llvm::ArrayRef< OptionEnumValueElement > OptionEnumValues
static constexpr CommandObject::ArgumentTableEntry g_argument_table[]
Definition: SBAddress.h:15
StateType
Process and Thread States.
@ eStateUnloaded
Process is object is valid, but not currently loaded.
@ eStateConnected
Process is connected to remote debug services, but not launched or attached to anything yet.
@ eStateDetached
Process has been detached and can't be examined.
@ eStateStopped
Process or thread is stopped and can be examined.
@ eStateSuspended
Process or thread is in a suspended state as far as the debugger is concerned while other processes o...
@ eStateRunning
Process or thread is running and can't be examined.
@ eStateLaunching
Process is in the process of launching.
@ eStateAttaching
Process is currently trying to attach.
@ eStateExited
Process has exited and can't be examined.
@ eStateStepping
Process or thread is in the process of stepping and can not be examined.
@ eStateCrashed
Process or thread has crashed and can be examined.
@ eReturnStatusFailed
@ eReturnStatusSuccessFinishNoResult
@ eArgTypeBreakpointIDRange
@ eArgTypeBreakpointID
@ eArgTypeWatchpointID
@ eArgTypeWatchpointIDRange
std::shared_ptr< lldb_private::Target > TargetSP
Definition: lldb-forward.h:448
const char * c_str() const
Definition: Args.h:51
llvm::StringRef ref() const
Definition: Args.h:50
char GetQuoteChar() const
Definition: Args.h:55
Entries in the main argument information table.
Definition: CommandObject.h:85
Used to build individual command argument lists.
Definition: CommandObject.h:95