LLDB mainline
CommandObjectSettings.cpp
Go to the documentation of this file.
1//===-- CommandObjectSettings.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 "llvm/ADT/StringRef.h"
12
13#include "lldb/Host/Host.h"
20
21using namespace lldb;
22using namespace lldb_private;
23
24/// Warn when \a var_name is a deprecated setting, so that users can stop
25/// relying on it before it's removed.
26static void WarnIfDeprecated(Debugger &debugger, llvm::StringRef var_name,
27 CommandReturnObject &result) {
28 if (std::optional<std::string> warning =
29 debugger.GetDeprecationWarning(var_name))
30 result.AppendWarning(*warning);
31}
32
33// CommandObjectSettingsSet
34#define LLDB_OPTIONS_settings_set
35#include "CommandOptions.inc"
36
38public:
40 : CommandObjectRaw(interpreter, "settings set",
41 "Set the value of the specified debugger setting.") {
44 CommandArgumentData var_name_arg;
45 CommandArgumentData value_arg;
46
47 // Define the first (and only) variant of this arg.
49 var_name_arg.arg_repetition = eArgRepeatPlain;
50
51 // There is only one variant this argument could be; put it into the
52 // argument entry.
53 arg1.push_back(var_name_arg);
54
55 // Define the first (and only) variant of this arg.
56 value_arg.arg_type = eArgTypeValue;
58
59 // There is only one variant this argument could be; put it into the
60 // argument entry.
61 arg2.push_back(value_arg);
62
63 // Push the data for the first argument into the m_arguments vector.
64 m_arguments.push_back(arg1);
65 m_arguments.push_back(arg2);
66
68 "\nWhen setting a dictionary or array variable, you can set multiple entries \
69at once by giving the values to the set command. For example:"
70 R"(
71
72(lldb) settings set target.run-args value1 value2 value3
73(lldb) settings set target.env-vars MYPATH=~/.:/usr/bin SOME_ENV_VAR=12345
74
75(lldb) settings show target.run-args
76 [0]: 'value1'
77 [1]: 'value2'
78 [3]: 'value3'
79(lldb) settings show target.env-vars
80 'MYPATH=~/.:/usr/bin'
81 'SOME_ENV_VAR=12345'
82
83)"
84 "Warning: The 'set' command re-sets the entire array or dictionary. If you \
85just want to add, remove or update individual values (or add something to \
86the end), use one of the other settings sub-commands: append, replace, \
87insert-before or insert-after.");
88 }
90 ~CommandObjectSettingsSet() override = default;
92 // Overrides base class's behavior where WantsCompletion =
93 // !WantsRawCommandString.
94 bool WantsCompletion() override { return true; }
95
96 Options *GetOptions() override { return &m_options; }
97
98 class CommandOptions : public Options {
99 public:
100 CommandOptions() = default;
101
102 ~CommandOptions() override = default;
103
104 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
105 ExecutionContext *execution_context) override {
107 const int short_option = m_getopt_table[option_idx].val;
108
109 switch (short_option) {
110 case 'f':
111 m_force = true;
112 break;
113 case 'g':
114 m_global = true;
115 break;
116 case 'e':
117 m_exists = true;
118 break;
119 default:
120 llvm_unreachable("Unimplemented option");
121 }
122
123 return error;
126 void OptionParsingStarting(ExecutionContext *execution_context) override {
127 m_global = false;
128 m_force = false;
129 m_exists = false;
131
132 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
133 return llvm::ArrayRef(g_settings_set_options);
134 }
135
136 // Instance variables to hold the values for command options.
137 bool m_global = false;
138 bool m_force = false;
139 bool m_exists = false;
140 };
141
142 void
143 HandleArgumentCompletion(CompletionRequest &request,
144 OptionElementVector &opt_element_vector) override {
145
146 const size_t argc = request.GetParsedLine().GetArgumentCount();
147 const char *arg = nullptr;
148 size_t setting_var_idx;
149 for (setting_var_idx = 0; setting_var_idx < argc; ++setting_var_idx) {
150 arg = request.GetParsedLine().GetArgumentAtIndex(setting_var_idx);
151 if (arg && arg[0] != '-')
152 break; // We found our setting variable name index
153 }
154 if (request.GetCursorIndex() == setting_var_idx) {
155 // Attempting to complete setting variable name
158 nullptr);
159 return;
160 }
161 arg = request.GetParsedLine().GetArgumentAtIndex(request.GetCursorIndex());
162
163 if (!arg)
164 return;
165
166 // Complete option name
167 if (arg[0] == '-')
168 return;
170 // Complete setting value
171 const char *setting_var_name =
172 request.GetParsedLine().GetArgumentAtIndex(setting_var_idx);
174 lldb::OptionValueSP value_sp(
175 GetDebugger().GetPropertyValue(&m_exe_ctx, setting_var_name, error));
176 if (!value_sp)
177 return;
178 value_sp->AutoComplete(m_interpreter, request);
179 }
180
181protected:
182 void DoExecute(llvm::StringRef command,
183 CommandReturnObject &result) override {
184 Args cmd_args(command);
185
186 // Process possible options.
187 if (!ParseOptions(cmd_args, result))
188 return;
189
190 const size_t min_argc = m_options.m_force ? 1 : 2;
191 const size_t argc = cmd_args.GetArgumentCount();
192
193 if ((argc < min_argc) && (!m_options.m_global)) {
194 result.AppendError("'settings set' takes more arguments");
195 return;
196 }
197
198 const char *var_name = cmd_args.GetArgumentAtIndex(0);
199 if ((var_name == nullptr) || (var_name[0] == '\0')) {
200 result.AppendError(
201 "'settings set' command requires a valid variable name");
202 return;
203 }
204
205 LLDB_LOG(GetLog(SystemLog::System), "settings set {0}", command);
206
207 // A missing value corresponds to clearing the setting when "force" is
208 // specified.
209 if (argc == 1 && m_options.m_force) {
210 Status error(GetDebugger().SetPropertyValue(
211 &m_exe_ctx, eVarSetOperationClear, var_name, llvm::StringRef()));
212 if (error.Fail()) {
213 result.AppendError(error.AsCString());
214 }
215 return;
216 }
217
218 // Split the raw command into var_name and value pair.
219 llvm::StringRef var_value(command);
220 var_value = var_value.split(var_name).second.ltrim();
221
223 if (m_options.m_global)
225 var_name, var_value);
226
227 if (error.Success()) {
228 // FIXME this is the same issue as the one in commands script import
229 // we could be setting target.load-script-from-symbol-file which would
230 // cause Python scripts to be loaded, which could run LLDB commands (e.g.
231 // settings set target.process.python-os-plugin-path) and cause a crash
232 // if we did not clear the command's exe_ctx first
233 ExecutionContext exe_ctx(m_exe_ctx);
234 m_exe_ctx.Clear();
236 var_name, var_value);
238
239 if (error.Fail() && !m_options.m_exists) {
240 result.AppendError(error.AsCString());
241 return;
242 }
243
244 if (error.Success())
245 WarnIfDeprecated(GetDebugger(), var_name, result);
247 }
248
249private:
251};
252
253// CommandObjectSettingsShow -- Show current values
254#define LLDB_OPTIONS_settings_show
255#include "CommandOptions.inc"
256
258public:
260 : CommandObjectParsed(interpreter, "settings show",
261 "Show matching debugger settings and their current "
262 "values. Defaults to showing all settings.") {
264 }
265
266 ~CommandObjectSettingsShow() override = default;
267
268 Options *GetOptions() override { return &m_options; }
269
270 class CommandOptions : public Options {
271 public:
272 ~CommandOptions() override = default;
273
274 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
275 ExecutionContext *execution_context) override {
276 const int short_option = m_getopt_table[option_idx].val;
277 switch (short_option) {
278 case 'd':
279 m_include_defaults = true;
280 break;
281 case 'c':
282 m_only_changed = true;
283 break;
284 case 'D':
286 break;
287 default:
288 llvm_unreachable("Unimplemented option");
289 }
290 return {};
291 }
292
293 void OptionParsingStarting(ExecutionContext *execution_context) override {
294 m_include_defaults = false;
295 m_only_changed = false;
296 m_include_deprecated = false;
297 }
298
299 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
300 return g_settings_show_options;
301 }
302
303 bool m_include_defaults = false;
304 bool m_only_changed = false;
306 };
307
308protected:
309 void DoExecute(Args &args, CommandReturnObject &result) override {
311
312 uint32_t dump_mask = OptionValue::eDumpGroupValue;
313 if (m_options.m_include_defaults)
315 if (m_options.m_only_changed) {
318 }
319 if (m_options.m_include_deprecated)
321
322 if (!args.empty()) {
323 for (const auto &arg : args) {
324 if (m_options.m_only_changed) {
325 Status lookup_error;
327 &m_exe_ctx, arg.ref(), lookup_error);
328 if (value_sp && value_sp->IsDefault())
329 continue;
330 }
331 Status error(GetDebugger().DumpPropertyValue(
332 &m_exe_ctx, result.GetOutputStream(), arg.ref(), dump_mask));
333 if (error.Success()) {
334 result.GetOutputStream().EOL();
335 } else {
336 result.AppendError(error.AsCString());
337 }
338 }
339 } else {
341 dump_mask);
342 }
343 }
344
345private:
347};
348
349// CommandObjectSettingsWrite -- Write settings to file
350#define LLDB_OPTIONS_settings_write
351#include "CommandOptions.inc"
352
354public:
357 interpreter, "settings export",
358 "Write matching debugger settings and their "
359 "current values to a file that can be read in with "
360 "\"settings read\". Defaults to writing all settings.",
361 nullptr) {
363 }
364
365 ~CommandObjectSettingsWrite() override = default;
366
367 Options *GetOptions() override { return &m_options; }
368
369 class CommandOptions : public Options {
370 public:
371 CommandOptions() = default;
372
373 ~CommandOptions() override = default;
374
375 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
376 ExecutionContext *execution_context) override {
378 const int short_option = m_getopt_table[option_idx].val;
379
380 switch (short_option) {
381 case 'f':
382 m_filename.assign(std::string(option_arg));
383 break;
384 case 'a':
385 m_append = true;
386 break;
387 default:
388 llvm_unreachable("Unimplemented option");
389 }
390
391 return error;
392 }
393
394 void OptionParsingStarting(ExecutionContext *execution_context) override {
395 m_filename.clear();
396 m_append = false;
397 }
398
399 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
400 return llvm::ArrayRef(g_settings_write_options);
401 }
402
403 // Instance variables to hold the values for command options.
404 std::string m_filename;
405 bool m_append = false;
406 };
407
408protected:
409 void DoExecute(Args &args, CommandReturnObject &result) override {
410 FileSpec file_spec(m_options.m_filename);
411 FileSystem::Instance().Resolve(file_spec);
412 std::string path(file_spec.GetPath());
414 if (m_options.m_append)
415 options |= File::eOpenOptionAppend;
416 else
417 options |= File::eOpenOptionTruncate;
418
419 StreamFile out_file(path.c_str(), options,
420 lldb::eFilePermissionsFileDefault);
421
422 if (!out_file.GetFile().IsValid()) {
423 result.AppendErrorWithFormat("%s: unable to write to file", path.c_str());
424 return;
425 }
426
427 // Exporting should not be context sensitive.
428 ExecutionContext clean_ctx;
429
430 if (args.empty()) {
431 GetDebugger().DumpAllPropertyValues(&clean_ctx, out_file,
434 return;
435 }
436
437 for (const auto &arg : args) {
438 Status error(GetDebugger().DumpPropertyValue(
439 &clean_ctx, out_file, arg.ref(), OptionValue::eDumpGroupExport));
440 if (!error.Success()) {
441 result.AppendError(error.AsCString());
442 }
443 }
444 if (result.GetStatus() != eReturnStatusFailed)
446 }
447
448private:
450};
451
452// CommandObjectSettingsRead -- Read settings from file
453#define LLDB_OPTIONS_settings_read
454#include "CommandOptions.inc"
455
457public:
460 interpreter, "settings read",
461 "Read settings previously saved to a file with \"settings write\".",
462 nullptr) {}
463
464 ~CommandObjectSettingsRead() override = default;
465
466 Options *GetOptions() override { return &m_options; }
467
468 class CommandOptions : public Options {
469 public:
470 CommandOptions() = default;
471
472 ~CommandOptions() override = default;
473
474 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
475 ExecutionContext *execution_context) override {
477 const int short_option = m_getopt_table[option_idx].val;
478
479 switch (short_option) {
480 case 'f':
481 m_filename.assign(std::string(option_arg));
482 break;
483 default:
484 llvm_unreachable("Unimplemented option");
485 }
486
487 return error;
488 }
489
490 void OptionParsingStarting(ExecutionContext *execution_context) override {
491 m_filename.clear();
492 }
493
494 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
495 return llvm::ArrayRef(g_settings_read_options);
496 }
497
498 // Instance variables to hold the values for command options.
499 std::string m_filename;
500 };
501
502protected:
503 void DoExecute(Args &command, CommandReturnObject &result) override {
504 FileSpec file(m_options.m_filename);
507 options.SetAddToHistory(false);
508 options.SetEchoCommands(false);
509 options.SetPrintResults(true);
510 options.SetPrintErrors(true);
511 options.SetStopOnError(false);
512 m_interpreter.HandleCommandsFromFile(file, options, result);
513 }
514
515private:
517};
518
519// CommandObjectSettingsList -- List settable variables
520#define LLDB_OPTIONS_settings_list
521#include "CommandOptions.inc"
522
524public:
526 : CommandObjectParsed(interpreter, "settings list",
527 "List and describe matching debugger settings. "
528 "Defaults to all listing all settings.",
529 nullptr) {
531 CommandArgumentData var_name_arg;
532 CommandArgumentData prefix_name_arg;
533
534 // Define the first variant of this arg.
536 var_name_arg.arg_repetition = eArgRepeatOptional;
537
538 // Define the second variant of this arg.
539 prefix_name_arg.arg_type = eArgTypeSettingPrefix;
540 prefix_name_arg.arg_repetition = eArgRepeatOptional;
541
542 arg.push_back(var_name_arg);
543 arg.push_back(prefix_name_arg);
544
545 // Push the data for the first argument into the m_arguments vector.
546 m_arguments.push_back(arg);
547 }
548
549 ~CommandObjectSettingsList() override = default;
550
551 Options *GetOptions() override { return &m_options; }
552
553 class CommandOptions : public Options {
554 public:
555 ~CommandOptions() override = default;
556
557 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
558 ExecutionContext *execution_context) override {
559 const int short_option = m_getopt_table[option_idx].val;
560 switch (short_option) {
561 case 'D':
563 break;
564 default:
565 llvm_unreachable("Unimplemented option");
566 }
567 return {};
568 }
569
570 void OptionParsingStarting(ExecutionContext *execution_context) override {
571 m_include_deprecated = false;
572 }
573
574 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
575 return g_settings_list_options;
576 }
577
579 };
580
581 void
588
589protected:
590 void DoExecute(Args &args, CommandReturnObject &result) override {
592
593 const bool include_deprecated = m_options.m_include_deprecated;
594 const size_t argc = args.GetArgumentCount();
595 if (argc > 0) {
596 const bool dump_qualified_name = true;
597
598 for (const Args::ArgEntry &arg : args) {
599 const char *property_path = arg.c_str();
600
601 const Property *property =
602 GetDebugger().GetValueProperties()->GetPropertyAtPath(
603 &m_exe_ctx, property_path);
604
605 if (property) {
606 property->DumpDescription(m_interpreter, result.GetOutputStream(), 0,
607 dump_qualified_name, include_deprecated);
608 } else {
609 result.AppendErrorWithFormat("invalid property path '%s'",
610 property_path);
611 }
612 }
613 } else {
615 include_deprecated);
616 }
617 }
618
619private:
621};
622
623// CommandObjectSettingsRemove
624
626public:
628 : CommandObjectRaw(interpreter, "settings remove",
629 "Remove a value from a setting, specified by array "
630 "index or dictionary key.") {
633 CommandArgumentData var_name_arg;
634 CommandArgumentData index_arg;
635 CommandArgumentData key_arg;
636
637 // Define the first (and only) variant of this arg.
639 var_name_arg.arg_repetition = eArgRepeatPlain;
640
641 // There is only one variant this argument could be; put it into the
642 // argument entry.
643 arg1.push_back(var_name_arg);
644
645 // Define the first variant of this arg.
646 index_arg.arg_type = eArgTypeSettingIndex;
648
649 // Define the second variant of this arg.
652
653 // Push both variants into this arg
654 arg2.push_back(index_arg);
655 arg2.push_back(key_arg);
656
657 // Push the data for the first argument into the m_arguments vector.
658 m_arguments.push_back(arg1);
659 m_arguments.push_back(arg2);
660 }
661
662 ~CommandObjectSettingsRemove() override = default;
663
664 bool WantsCompletion() override { return true; }
665
666 void
674
675protected:
676 void DoExecute(llvm::StringRef command,
677 CommandReturnObject &result) override {
679
680 Args cmd_args(command);
681
682 // Process possible options.
683 if (!ParseOptions(cmd_args, result))
684 return;
685
686 const size_t argc = cmd_args.GetArgumentCount();
687 if (argc == 0) {
688 result.AppendError("'settings remove' takes an array or dictionary item, "
689 "or an array followed by one or more indexes, or a "
690 "dictionary followed by one or more key names to "
691 "remove");
692 return;
693 }
694
695 const char *var_name = cmd_args.GetArgumentAtIndex(0);
696 if ((var_name == nullptr) || (var_name[0] == '\0')) {
697 result.AppendError(
698 "'settings remove' command requires a valid variable name");
699 return;
700 }
701
702 // Split the raw command into var_name and value pair.
703 llvm::StringRef var_value(command);
704 var_value = var_value.split(var_name).second.trim();
705
706 Status error(GetDebugger().SetPropertyValue(
707 &m_exe_ctx, eVarSetOperationRemove, var_name, var_value));
708 if (error.Fail()) {
709 result.AppendError(error.AsCString());
710 }
711 }
712};
713
714// CommandObjectSettingsReplace
715
717public:
719 : CommandObjectRaw(interpreter, "settings replace",
720 "Replace the debugger setting value specified by "
721 "array index or dictionary key.") {
725 CommandArgumentData var_name_arg;
726 CommandArgumentData index_arg;
727 CommandArgumentData key_arg;
728 CommandArgumentData value_arg;
729
730 // Define the first (and only) variant of this arg.
732 var_name_arg.arg_repetition = eArgRepeatPlain;
733
734 // There is only one variant this argument could be; put it into the
735 // argument entry.
736 arg1.push_back(var_name_arg);
737
738 // Define the first (variant of this arg.
739 index_arg.arg_type = eArgTypeSettingIndex;
741
742 // Define the second (variant of this arg.
745
746 // Put both variants into this arg
747 arg2.push_back(index_arg);
748 arg2.push_back(key_arg);
749
750 // Define the first (and only) variant of this arg.
751 value_arg.arg_type = eArgTypeValue;
753
754 // There is only one variant this argument could be; put it into the
755 // argument entry.
756 arg3.push_back(value_arg);
757
758 // Push the data for the first argument into the m_arguments vector.
759 m_arguments.push_back(arg1);
760 m_arguments.push_back(arg2);
761 m_arguments.push_back(arg3);
762 }
763
764 ~CommandObjectSettingsReplace() override = default;
765
766 // Overrides base class's behavior where WantsCompletion =
767 // !WantsRawCommandString.
768 bool WantsCompletion() override { return true; }
769
770 void
772 OptionElementVector &opt_element_vector) override {
773 // Attempting to complete variable name
774 if (request.GetCursorIndex() < 2)
777 nullptr);
778 }
779
780protected:
781 void DoExecute(llvm::StringRef command,
782 CommandReturnObject &result) override {
784
785 Args cmd_args(command);
786 const char *var_name = cmd_args.GetArgumentAtIndex(0);
787 if ((var_name == nullptr) || (var_name[0] == '\0')) {
788 result.AppendError("'settings replace' command requires a valid variable "
789 "name; No value supplied");
790 return;
791 }
792
793 // Split the raw command into var_name, index_value, and value triple.
794 llvm::StringRef var_value(command);
795 var_value = var_value.split(var_name).second.trim();
796
797 Status error(GetDebugger().SetPropertyValue(
798 &m_exe_ctx, eVarSetOperationReplace, var_name, var_value));
799 if (error.Fail()) {
800 result.AppendError(error.AsCString());
801 } else {
802 WarnIfDeprecated(GetDebugger(), var_name, result);
804 }
805 }
806};
807
808// CommandObjectSettingsInsertBefore
809
811public:
813 : CommandObjectRaw(interpreter, "settings insert-before",
814 "Insert one or more values into an debugger array "
815 "setting immediately before the specified element "
816 "index.") {
820 CommandArgumentData var_name_arg;
821 CommandArgumentData index_arg;
822 CommandArgumentData value_arg;
823
824 // Define the first (and only) variant of this arg.
826 var_name_arg.arg_repetition = eArgRepeatPlain;
827
828 // There is only one variant this argument could be; put it into the
829 // argument entry.
830 arg1.push_back(var_name_arg);
831
832 // Define the first (variant of this arg.
833 index_arg.arg_type = eArgTypeSettingIndex;
835
836 // There is only one variant this argument could be; put it into the
837 // argument entry.
838 arg2.push_back(index_arg);
839
840 // Define the first (and only) variant of this arg.
841 value_arg.arg_type = eArgTypeValue;
843
844 // There is only one variant this argument could be; put it into the
845 // argument entry.
846 arg3.push_back(value_arg);
847
848 // Push the data for the first argument into the m_arguments vector.
849 m_arguments.push_back(arg1);
850 m_arguments.push_back(arg2);
851 m_arguments.push_back(arg3);
852 }
853
855
856 // Overrides base class's behavior where WantsCompletion =
857 // !WantsRawCommandString.
858 bool WantsCompletion() override { return true; }
859
860 void
862 OptionElementVector &opt_element_vector) override {
863 // Attempting to complete variable name
864 if (request.GetCursorIndex() < 2)
867 nullptr);
868 }
869
870protected:
871 void DoExecute(llvm::StringRef command,
872 CommandReturnObject &result) override {
874
875 Args cmd_args(command);
876 const size_t argc = cmd_args.GetArgumentCount();
877
878 if (argc < 3) {
879 result.AppendError("'settings insert-before' takes more arguments");
880 return;
881 }
882
883 const char *var_name = cmd_args.GetArgumentAtIndex(0);
884 if ((var_name == nullptr) || (var_name[0] == '\0')) {
885 result.AppendError("'settings insert-before' command requires a valid "
886 "variable name; No value supplied");
887 return;
888 }
889
890 // Split the raw command into var_name, index_value, and value triple.
891 llvm::StringRef var_value(command);
892 var_value = var_value.split(var_name).second.trim();
893
894 Status error(GetDebugger().SetPropertyValue(
895 &m_exe_ctx, eVarSetOperationInsertBefore, var_name, var_value));
896 if (error.Fail()) {
897 result.AppendError(error.AsCString());
898 } else {
899 WarnIfDeprecated(GetDebugger(), var_name, result);
900 }
901 }
902};
903
904// CommandObjectSettingInsertAfter
905
907public:
909 : CommandObjectRaw(interpreter, "settings insert-after",
910 "Insert one or more values into a debugger array "
911 "settings after the specified element index.") {
915 CommandArgumentData var_name_arg;
916 CommandArgumentData index_arg;
917 CommandArgumentData value_arg;
918
919 // Define the first (and only) variant of this arg.
921 var_name_arg.arg_repetition = eArgRepeatPlain;
922
923 // There is only one variant this argument could be; put it into the
924 // argument entry.
925 arg1.push_back(var_name_arg);
926
927 // Define the first (variant of this arg.
928 index_arg.arg_type = eArgTypeSettingIndex;
930
931 // There is only one variant this argument could be; put it into the
932 // argument entry.
933 arg2.push_back(index_arg);
934
935 // Define the first (and only) variant of this arg.
936 value_arg.arg_type = eArgTypeValue;
938
939 // There is only one variant this argument could be; put it into the
940 // argument entry.
941 arg3.push_back(value_arg);
942
943 // Push the data for the first argument into the m_arguments vector.
944 m_arguments.push_back(arg1);
945 m_arguments.push_back(arg2);
946 m_arguments.push_back(arg3);
947 }
948
950
951 // Overrides base class's behavior where WantsCompletion =
952 // !WantsRawCommandString.
953 bool WantsCompletion() override { return true; }
954
955 void
957 OptionElementVector &opt_element_vector) override {
958 // Attempting to complete variable name
959 if (request.GetCursorIndex() < 2)
962 nullptr);
963 }
964
965protected:
966 void DoExecute(llvm::StringRef command,
967 CommandReturnObject &result) override {
969
970 Args cmd_args(command);
971 const size_t argc = cmd_args.GetArgumentCount();
972
973 if (argc < 3) {
974 result.AppendError("'settings insert-after' takes more arguments");
975 return;
976 }
977
978 const char *var_name = cmd_args.GetArgumentAtIndex(0);
979 if ((var_name == nullptr) || (var_name[0] == '\0')) {
980 result.AppendError("'settings insert-after' command requires a valid "
981 "variable name; No value supplied");
982 return;
983 }
984
985 // Split the raw command into var_name, index_value, and value triple.
986 llvm::StringRef var_value(command);
987 var_value = var_value.split(var_name).second.trim();
988
989 Status error(GetDebugger().SetPropertyValue(
990 &m_exe_ctx, eVarSetOperationInsertAfter, var_name, var_value));
991 if (error.Fail()) {
992 result.AppendError(error.AsCString());
993 } else {
994 WarnIfDeprecated(GetDebugger(), var_name, result);
995 }
996 }
997};
998
999// CommandObjectSettingsAppend
1000
1002public:
1004 : CommandObjectRaw(interpreter, "settings append",
1005 "Append one or more values to a debugger array, "
1006 "dictionary, or string setting.") {
1009 CommandArgumentData var_name_arg;
1010 CommandArgumentData value_arg;
1011
1012 // Define the first (and only) variant of this arg.
1014 var_name_arg.arg_repetition = eArgRepeatPlain;
1015
1016 // There is only one variant this argument could be; put it into the
1017 // argument entry.
1018 arg1.push_back(var_name_arg);
1019
1020 // Define the first (and only) variant of this arg.
1021 value_arg.arg_type = eArgTypeValue;
1022 value_arg.arg_repetition = eArgRepeatPlain;
1023
1024 // There is only one variant this argument could be; put it into the
1025 // argument entry.
1026 arg2.push_back(value_arg);
1027
1028 // Push the data for the first argument into the m_arguments vector.
1029 m_arguments.push_back(arg1);
1030 m_arguments.push_back(arg2);
1031 }
1032
1033 ~CommandObjectSettingsAppend() override = default;
1034
1035 // Overrides base class's behavior where WantsCompletion =
1036 // !WantsRawCommandString.
1037 bool WantsCompletion() override { return true; }
1038
1039 void
1041 OptionElementVector &opt_element_vector) override {
1042 // Attempting to complete variable name
1043 if (request.GetCursorIndex() < 2)
1046 nullptr);
1047 }
1048
1049protected:
1050 void DoExecute(llvm::StringRef command,
1051 CommandReturnObject &result) override {
1053 Args cmd_args(command);
1054 const size_t argc = cmd_args.GetArgumentCount();
1055
1056 if (argc < 2) {
1057 result.AppendError("'settings append' takes more arguments");
1058 return;
1059 }
1060
1061 const char *var_name = cmd_args.GetArgumentAtIndex(0);
1062 if ((var_name == nullptr) || (var_name[0] == '\0')) {
1063 result.AppendError("'settings append' command requires a valid variable "
1064 "name; No value supplied");
1065 return;
1066 }
1067
1068 // Do not perform cmd_args.Shift() since StringRef is manipulating the raw
1069 // character string later on.
1070
1071 // Split the raw command into var_name and value pair.
1072 llvm::StringRef var_value(command);
1073 var_value = var_value.split(var_name).second.trim();
1074
1075 Status error(GetDebugger().SetPropertyValue(
1076 &m_exe_ctx, eVarSetOperationAppend, var_name, var_value));
1077 if (error.Fail()) {
1078 result.AppendError(error.AsCString());
1079 } else {
1080 WarnIfDeprecated(GetDebugger(), var_name, result);
1081 }
1082 }
1083};
1084
1085// CommandObjectSettingsClear
1086#define LLDB_OPTIONS_settings_clear
1087#include "CommandOptions.inc"
1088
1090public:
1093 interpreter, "settings clear",
1094 "Clear a debugger setting array, dictionary, or string. "
1095 "If '-a' option is specified, it clears all settings.", nullptr) {
1097 }
1098
1099 ~CommandObjectSettingsClear() override = default;
1100
1101 void
1103 OptionElementVector &opt_element_vector) override {
1104 // Attempting to complete variable name
1105 if (request.GetCursorIndex() < 2)
1108 nullptr);
1109 }
1110
1111 Options *GetOptions() override { return &m_options; }
1112
1113 class CommandOptions : public Options {
1114 public:
1115 CommandOptions() = default;
1116
1117 ~CommandOptions() override = default;
1118
1119 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1120 ExecutionContext *execution_context) override {
1121 const int short_option = m_getopt_table[option_idx].val;
1122 switch (short_option) {
1123 case 'a':
1124 m_clear_all = true;
1125 break;
1126 default:
1127 llvm_unreachable("Unimplemented option");
1128 }
1129 return Status();
1130 }
1131
1132 void OptionParsingStarting(ExecutionContext *execution_context) override {
1133 m_clear_all = false;
1134 }
1135
1136 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
1137 return llvm::ArrayRef(g_settings_clear_options);
1138 }
1139
1140 bool m_clear_all = false;
1141 };
1142
1143protected:
1144 void DoExecute(Args &command, CommandReturnObject &result) override {
1146 const size_t argc = command.GetArgumentCount();
1147
1148 if (m_options.m_clear_all) {
1149 if (argc != 0) {
1150 result.AppendError("'settings clear --all' doesn't take any arguments");
1151 return;
1152 }
1153 GetDebugger().GetValueProperties()->Clear();
1154 return;
1155 }
1156
1157 if (argc != 1) {
1158 result.AppendError("'settings clear' takes exactly one argument");
1159 return;
1160 }
1161
1162 const char *var_name = command.GetArgumentAtIndex(0);
1163 if ((var_name == nullptr) || (var_name[0] == '\0')) {
1164 result.AppendError("'settings clear' command requires a valid variable "
1165 "name; No value supplied");
1166 return;
1167 }
1168
1169 Status error(GetDebugger().SetPropertyValue(
1170 &m_exe_ctx, eVarSetOperationClear, var_name, llvm::StringRef()));
1171 if (error.Fail()) {
1172 result.AppendError(error.AsCString());
1173 }
1174 }
1175
1176 private:
1178};
1179
1180// CommandObjectMultiwordSettings
1181
1183 CommandInterpreter &interpreter)
1184 : CommandObjectMultiword(interpreter, "settings",
1185 "Commands for managing LLDB settings.",
1186 "settings <subcommand> [<command-options>]") {
1187 LoadSubCommand("set",
1188 CommandObjectSP(new CommandObjectSettingsSet(interpreter)));
1189 LoadSubCommand("show",
1190 CommandObjectSP(new CommandObjectSettingsShow(interpreter)));
1191 LoadSubCommand("list",
1192 CommandObjectSP(new CommandObjectSettingsList(interpreter)));
1193 LoadSubCommand("remove",
1196 new CommandObjectSettingsReplace(interpreter)));
1198 "insert-before",
1201 "insert-after",
1203 LoadSubCommand("append",
1205 LoadSubCommand("clear",
1207 LoadSubCommand("write",
1209 LoadSubCommand("read",
1210 CommandObjectSP(new CommandObjectSettingsRead(interpreter)));
1211}
1212
static void WarnIfDeprecated(Debugger &debugger, llvm::StringRef var_name, CommandReturnObject &result)
Warn when var_name is a deprecated setting, so that users can stop relying on it before it's removed.
static llvm::raw_ostream & error(Stream &strm)
static llvm::raw_ostream & warning(Stream &strm)
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition Log.h:375
void HandleArgumentCompletion(CompletionRequest &request, OptionElementVector &opt_element_vector) override
The default version handles argument definitions that have only one argument type,...
CommandObjectSettingsAppend(CommandInterpreter &interpreter)
~CommandObjectSettingsAppend() override=default
void DoExecute(llvm::StringRef command, CommandReturnObject &result) override
Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, ExecutionContext *execution_context) override
Set the value of an option.
void OptionParsingStarting(ExecutionContext *execution_context) override
llvm::ArrayRef< OptionDefinition > GetDefinitions() override
void HandleArgumentCompletion(CompletionRequest &request, OptionElementVector &opt_element_vector) override
The default version handles argument definitions that have only one argument type,...
~CommandObjectSettingsClear() override=default
void DoExecute(Args &command, CommandReturnObject &result) override
CommandObjectSettingsClear(CommandInterpreter &interpreter)
~CommandObjectSettingsInsertAfter() override=default
void DoExecute(llvm::StringRef command, CommandReturnObject &result) override
CommandObjectSettingsInsertAfter(CommandInterpreter &interpreter)
void HandleArgumentCompletion(CompletionRequest &request, OptionElementVector &opt_element_vector) override
The default version handles argument definitions that have only one argument type,...
~CommandObjectSettingsInsertBefore() override=default
CommandObjectSettingsInsertBefore(CommandInterpreter &interpreter)
void HandleArgumentCompletion(CompletionRequest &request, OptionElementVector &opt_element_vector) override
The default version handles argument definitions that have only one argument type,...
void DoExecute(llvm::StringRef command, CommandReturnObject &result) override
llvm::ArrayRef< OptionDefinition > GetDefinitions() override
Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, ExecutionContext *execution_context) override
Set the value of an option.
void OptionParsingStarting(ExecutionContext *execution_context) override
CommandObjectSettingsList(CommandInterpreter &interpreter)
void DoExecute(Args &args, CommandReturnObject &result) override
void HandleArgumentCompletion(CompletionRequest &request, OptionElementVector &opt_element_vector) override
The default version handles argument definitions that have only one argument type,...
~CommandObjectSettingsList() override=default
void OptionParsingStarting(ExecutionContext *execution_context) override
Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, ExecutionContext *execution_context) override
Set the value of an option.
llvm::ArrayRef< OptionDefinition > GetDefinitions() override
~CommandObjectSettingsRead() override=default
CommandObjectSettingsRead(CommandInterpreter &interpreter)
void DoExecute(Args &command, CommandReturnObject &result) override
void DoExecute(llvm::StringRef command, CommandReturnObject &result) override
CommandObjectSettingsRemove(CommandInterpreter &interpreter)
void HandleArgumentCompletion(CompletionRequest &request, OptionElementVector &opt_element_vector) override
The default version handles argument definitions that have only one argument type,...
~CommandObjectSettingsRemove() override=default
void HandleArgumentCompletion(CompletionRequest &request, OptionElementVector &opt_element_vector) override
The default version handles argument definitions that have only one argument type,...
void DoExecute(llvm::StringRef command, CommandReturnObject &result) override
CommandObjectSettingsReplace(CommandInterpreter &interpreter)
~CommandObjectSettingsReplace() override=default
Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, ExecutionContext *execution_context) override
Set the value of an option.
void OptionParsingStarting(ExecutionContext *execution_context) override
llvm::ArrayRef< OptionDefinition > GetDefinitions() override
void HandleArgumentCompletion(CompletionRequest &request, OptionElementVector &opt_element_vector) override
The default version handles argument definitions that have only one argument type,...
CommandObjectSettingsSet(CommandInterpreter &interpreter)
~CommandObjectSettingsSet() override=default
void DoExecute(llvm::StringRef command, CommandReturnObject &result) override
void OptionParsingStarting(ExecutionContext *execution_context) override
llvm::ArrayRef< OptionDefinition > GetDefinitions() override
Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, ExecutionContext *execution_context) override
Set the value of an option.
void DoExecute(Args &args, CommandReturnObject &result) override
CommandObjectSettingsShow(CommandInterpreter &interpreter)
~CommandObjectSettingsShow() override=default
llvm::ArrayRef< OptionDefinition > GetDefinitions() override
void OptionParsingStarting(ExecutionContext *execution_context) override
Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, ExecutionContext *execution_context) override
Set the value of an option.
void DoExecute(Args &args, CommandReturnObject &result) override
~CommandObjectSettingsWrite() override=default
CommandObjectSettingsWrite(CommandInterpreter &interpreter)
A command line argument class.
Definition Args.h:33
size_t GetArgumentCount() const
Gets the number of arguments left in this command object.
Definition Args.h:120
const char * GetArgumentAtIndex(size_t idx) const
Gets the null-terminated C string argument pointer for the argument at index idx.
Definition Args.cpp:273
bool empty() const
Definition Args.h:122
static bool InvokeCommonCompletionCallbacks(CommandInterpreter &interpreter, uint32_t completion_mask, lldb_private::CompletionRequest &request, SearchFilter *searcher)
CommandObjectMultiwordSettings(CommandInterpreter &interpreter)
bool LoadSubCommand(llvm::StringRef cmd_name, const lldb::CommandObjectSP &command_obj) override
CommandObjectMultiword(CommandInterpreter &interpreter, const char *name, const char *help=nullptr, const char *syntax=nullptr, uint32_t flags=0)
CommandObjectParsed(CommandInterpreter &interpreter, const char *name, const char *help=nullptr, const char *syntax=nullptr, uint32_t flags=0)
CommandObjectRaw(CommandInterpreter &interpreter, llvm::StringRef name, llvm::StringRef help="", llvm::StringRef syntax="", uint32_t flags=0)
std::vector< CommandArgumentData > CommandArgumentEntry
virtual void SetHelpLong(llvm::StringRef str)
void AddSimpleArgumentList(lldb::CommandArgumentType arg_type, ArgumentRepetitionType repetition_type=eArgRepeatPlain)
std::vector< CommandArgumentEntry > m_arguments
CommandInterpreter & GetCommandInterpreter()
CommandInterpreter & m_interpreter
bool ParseOptions(Args &args, CommandReturnObject &result)
void AppendError(llvm::StringRef in_string)
void SetStatus(lldb::ReturnStatus status)
void AppendErrorWithFormat(const char *format,...) __attribute__((format(printf
void AppendWarning(llvm::StringRef in_string)
"lldb/Utility/ArgCompletionRequest.h"
A class to manage flag bits.
Definition Debugger.h:106
Status SetPropertyValue(const ExecutionContext *exe_ctx, VarSetOperationType op, llvm::StringRef property_path, llvm::StringRef value) override
Definition Debugger.cpp:300
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
A file utility class.
Definition FileSpec.h:56
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition FileSpec.cpp:380
static FileSystem & Instance()
void Resolve(llvm::SmallVectorImpl< char > &path, bool force_make_absolute=false)
Resolve path to make it canonical.
bool IsValid() const override
IsValid.
Definition File.cpp:106
@ eDumpOptionIncludeDeprecated
Also dump deprecated settings that still have their default value.
Definition OptionValue.h:68
A command line option parsing protocol class.
Definition Options.h:58
std::vector< Option > m_getopt_table
Definition Options.h:198
std::optional< std::string > GetDeprecationWarning(llvm::StringRef property_path) const
The warning to show when the setting at property_path is changed, or std::nullopt if that setting isn...
virtual void DumpAllPropertyValues(const ExecutionContext *exe_ctx, Stream &strm, uint32_t dump_mask, bool is_json=false)
virtual lldb::OptionValueSP GetPropertyValue(const ExecutionContext *exe_ctx, llvm::StringRef property_path, Status &error) const
virtual void DumpAllDescriptions(CommandInterpreter &interpreter, Stream &strm, bool include_deprecated) const
lldb::OptionValuePropertiesSP GetValueProperties() const
An error handling class.
Definition Status.h:118
size_t EOL()
Output and End of Line character to the stream.
Definition Stream.cpp:155
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:338
std::vector< OptionArgElement > OptionElementVector
Definition Options.h:43
@ eSettingsNameCompletion
std::shared_ptr< lldb_private::CommandObject > CommandObjectSP
@ eReturnStatusFailed
@ eReturnStatusSuccessFinishResult
@ eReturnStatusSuccessFinishNoResult
@ eArgTypeSettingPrefix
@ eArgTypeSettingIndex
@ eArgTypeSettingVariableName
std::shared_ptr< lldb_private::OptionValue > OptionValueSP
Used to build individual command argument lists.