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
19
20using namespace lldb;
21using namespace lldb_private;
22
23// CommandObjectSettingsSet
24#define LLDB_OPTIONS_settings_set
25#include "CommandOptions.inc"
26
28public:
30 : CommandObjectRaw(interpreter, "settings set",
31 "Set the value of the specified debugger setting.") {
34 CommandArgumentData var_name_arg;
35 CommandArgumentData value_arg;
36
37 // Define the first (and only) variant of this arg.
39 var_name_arg.arg_repetition = eArgRepeatPlain;
40
41 // There is only one variant this argument could be; put it into the
42 // argument entry.
43 arg1.push_back(var_name_arg);
44
45 // Define the first (and only) variant of this arg.
46 value_arg.arg_type = eArgTypeValue;
48
49 // There is only one variant this argument could be; put it into the
50 // argument entry.
51 arg2.push_back(value_arg);
52
53 // Push the data for the first argument into the m_arguments vector.
54 m_arguments.push_back(arg1);
55 m_arguments.push_back(arg2);
56
58 "\nWhen setting a dictionary or array variable, you can set multiple entries \
59at once by giving the values to the set command. For example:"
60 R"(
61
62(lldb) settings set target.run-args value1 value2 value3
63(lldb) settings set target.env-vars MYPATH=~/.:/usr/bin SOME_ENV_VAR=12345
64
65(lldb) settings show target.run-args
66 [0]: 'value1'
67 [1]: 'value2'
68 [3]: 'value3'
69(lldb) settings show target.env-vars
70 'MYPATH=~/.:/usr/bin'
71 'SOME_ENV_VAR=12345'
72
73)"
74 "Warning: The 'set' command re-sets the entire array or dictionary. If you \
75just want to add, remove or update individual values (or add something to \
76the end), use one of the other settings sub-commands: append, replace, \
77insert-before or insert-after.");
78 }
80 ~CommandObjectSettingsSet() override = default;
82 // Overrides base class's behavior where WantsCompletion =
83 // !WantsRawCommandString.
84 bool WantsCompletion() override { return true; }
85
86 Options *GetOptions() override { return &m_options; }
87
88 class CommandOptions : public Options {
89 public:
90 CommandOptions() = default;
91
92 ~CommandOptions() override = default;
93
94 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
95 ExecutionContext *execution_context) override {
97 const int short_option = m_getopt_table[option_idx].val;
98
99 switch (short_option) {
100 case 'f':
101 m_force = true;
102 break;
103 case 'g':
104 m_global = true;
105 break;
106 case 'e':
107 m_exists = true;
108 break;
109 default:
110 llvm_unreachable("Unimplemented option");
111 }
112
113 return error;
116 void OptionParsingStarting(ExecutionContext *execution_context) override {
117 m_global = false;
118 m_force = false;
119 m_exists = false;
121
122 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
123 return llvm::ArrayRef(g_settings_set_options);
124 }
125
126 // Instance variables to hold the values for command options.
127 bool m_global = false;
128 bool m_force = false;
129 bool m_exists = false;
130 };
131
132 void
133 HandleArgumentCompletion(CompletionRequest &request,
134 OptionElementVector &opt_element_vector) override {
135
136 const size_t argc = request.GetParsedLine().GetArgumentCount();
137 const char *arg = nullptr;
138 size_t setting_var_idx;
139 for (setting_var_idx = 0; setting_var_idx < argc; ++setting_var_idx) {
140 arg = request.GetParsedLine().GetArgumentAtIndex(setting_var_idx);
141 if (arg && arg[0] != '-')
142 break; // We found our setting variable name index
143 }
144 if (request.GetCursorIndex() == setting_var_idx) {
145 // Attempting to complete setting variable name
148 nullptr);
149 return;
150 }
151 arg = request.GetParsedLine().GetArgumentAtIndex(request.GetCursorIndex());
152
153 if (!arg)
154 return;
155
156 // Complete option name
157 if (arg[0] == '-')
158 return;
160 // Complete setting value
161 const char *setting_var_name =
162 request.GetParsedLine().GetArgumentAtIndex(setting_var_idx);
164 lldb::OptionValueSP value_sp(
165 GetDebugger().GetPropertyValue(&m_exe_ctx, setting_var_name, error));
166 if (!value_sp)
167 return;
168 value_sp->AutoComplete(m_interpreter, request);
169 }
170
171protected:
172 void DoExecute(llvm::StringRef command,
173 CommandReturnObject &result) override {
174 Args cmd_args(command);
175
176 // Process possible options.
177 if (!ParseOptions(cmd_args, result))
178 return;
179
180 const size_t min_argc = m_options.m_force ? 1 : 2;
181 const size_t argc = cmd_args.GetArgumentCount();
182
183 if ((argc < min_argc) && (!m_options.m_global)) {
184 result.AppendError("'settings set' takes more arguments");
185 return;
186 }
187
188 const char *var_name = cmd_args.GetArgumentAtIndex(0);
189 if ((var_name == nullptr) || (var_name[0] == '\0')) {
190 result.AppendError(
191 "'settings set' command requires a valid variable name");
192 return;
193 }
194
195 // A missing value corresponds to clearing the setting when "force" is
196 // specified.
197 if (argc == 1 && m_options.m_force) {
198 Status error(GetDebugger().SetPropertyValue(
199 &m_exe_ctx, eVarSetOperationClear, var_name, llvm::StringRef()));
200 if (error.Fail()) {
201 result.AppendError(error.AsCString());
202 }
203 return;
204 }
205
206 // Split the raw command into var_name and value pair.
207 llvm::StringRef var_value(command);
208 var_value = var_value.split(var_name).second.ltrim();
209
211 if (m_options.m_global)
213 var_name, var_value);
214
215 if (error.Success()) {
216 // FIXME this is the same issue as the one in commands script import
217 // we could be setting target.load-script-from-symbol-file which would
218 // cause Python scripts to be loaded, which could run LLDB commands (e.g.
219 // settings set target.process.python-os-plugin-path) and cause a crash
220 // if we did not clear the command's exe_ctx first
221 ExecutionContext exe_ctx(m_exe_ctx);
222 m_exe_ctx.Clear();
224 var_name, var_value);
225 }
226
227 if (error.Fail() && !m_options.m_exists) {
228 result.AppendError(error.AsCString());
229 return;
230 }
231
233 }
234
235private:
237};
238
239// CommandObjectSettingsShow -- Show current values
240#define LLDB_OPTIONS_settings_show
241#include "CommandOptions.inc"
242
244public:
246 : CommandObjectParsed(interpreter, "settings show",
247 "Show matching debugger settings and their current "
248 "values. Defaults to showing all settings.") {
250 }
251
252 ~CommandObjectSettingsShow() override = default;
253
254 Options *GetOptions() override { return &m_options; }
255
256 class CommandOptions : public Options {
257 public:
258 ~CommandOptions() override = default;
259
260 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
261 ExecutionContext *execution_context) override {
262 const int short_option = m_getopt_table[option_idx].val;
263 switch (short_option) {
264 case 'd':
265 m_include_defaults = true;
266 break;
267 case 'c':
268 m_only_changed = true;
269 break;
270 default:
271 llvm_unreachable("Unimplemented option");
272 }
273 return {};
274 }
275
276 void OptionParsingStarting(ExecutionContext *execution_context) override {
277 m_include_defaults = false;
278 m_only_changed = false;
279 }
280
281 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
282 return g_settings_show_options;
283 }
284
285 bool m_include_defaults = false;
286 bool m_only_changed = false;
287 };
288
289protected:
290 void DoExecute(Args &args, CommandReturnObject &result) override {
292
293 uint32_t dump_mask = OptionValue::eDumpGroupValue;
294 if (m_options.m_include_defaults)
296 if (m_options.m_only_changed) {
299 }
300
301 if (!args.empty()) {
302 for (const auto &arg : args) {
303 if (m_options.m_only_changed) {
304 Status lookup_error;
306 &m_exe_ctx, arg.ref(), lookup_error);
307 if (value_sp && value_sp->IsDefault())
308 continue;
309 }
310 Status error(GetDebugger().DumpPropertyValue(
311 &m_exe_ctx, result.GetOutputStream(), arg.ref(), dump_mask));
312 if (error.Success()) {
313 result.GetOutputStream().EOL();
314 } else {
315 result.AppendError(error.AsCString());
316 }
317 }
318 } else {
320 dump_mask);
321 }
322 }
323
324private:
326};
327
328// CommandObjectSettingsWrite -- Write settings to file
329#define LLDB_OPTIONS_settings_write
330#include "CommandOptions.inc"
331
333public:
336 interpreter, "settings export",
337 "Write matching debugger settings and their "
338 "current values to a file that can be read in with "
339 "\"settings read\". Defaults to writing all settings.",
340 nullptr) {
342 }
343
344 ~CommandObjectSettingsWrite() override = default;
345
346 Options *GetOptions() override { return &m_options; }
347
348 class CommandOptions : public Options {
349 public:
350 CommandOptions() = default;
351
352 ~CommandOptions() override = default;
353
354 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
355 ExecutionContext *execution_context) override {
357 const int short_option = m_getopt_table[option_idx].val;
358
359 switch (short_option) {
360 case 'f':
361 m_filename.assign(std::string(option_arg));
362 break;
363 case 'a':
364 m_append = true;
365 break;
366 default:
367 llvm_unreachable("Unimplemented option");
368 }
369
370 return error;
371 }
372
373 void OptionParsingStarting(ExecutionContext *execution_context) override {
374 m_filename.clear();
375 m_append = false;
376 }
377
378 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
379 return llvm::ArrayRef(g_settings_write_options);
380 }
381
382 // Instance variables to hold the values for command options.
383 std::string m_filename;
384 bool m_append = false;
385 };
386
387protected:
388 void DoExecute(Args &args, CommandReturnObject &result) override {
389 FileSpec file_spec(m_options.m_filename);
390 FileSystem::Instance().Resolve(file_spec);
391 std::string path(file_spec.GetPath());
393 if (m_options.m_append)
394 options |= File::eOpenOptionAppend;
395 else
396 options |= File::eOpenOptionTruncate;
397
398 StreamFile out_file(path.c_str(), options,
399 lldb::eFilePermissionsFileDefault);
400
401 if (!out_file.GetFile().IsValid()) {
402 result.AppendErrorWithFormat("%s: unable to write to file", path.c_str());
403 return;
404 }
405
406 // Exporting should not be context sensitive.
407 ExecutionContext clean_ctx;
408
409 if (args.empty()) {
410 GetDebugger().DumpAllPropertyValues(&clean_ctx, out_file,
413 return;
414 }
415
416 for (const auto &arg : args) {
417 Status error(GetDebugger().DumpPropertyValue(
418 &clean_ctx, out_file, arg.ref(), OptionValue::eDumpGroupExport));
419 if (!error.Success()) {
420 result.AppendError(error.AsCString());
421 }
422 }
423 if (result.GetStatus() != eReturnStatusFailed)
425 }
426
427private:
429};
430
431// CommandObjectSettingsRead -- Read settings from file
432#define LLDB_OPTIONS_settings_read
433#include "CommandOptions.inc"
434
436public:
439 interpreter, "settings read",
440 "Read settings previously saved to a file with \"settings write\".",
441 nullptr) {}
442
443 ~CommandObjectSettingsRead() override = default;
444
445 Options *GetOptions() override { return &m_options; }
446
447 class CommandOptions : public Options {
448 public:
449 CommandOptions() = default;
450
451 ~CommandOptions() override = default;
452
453 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
454 ExecutionContext *execution_context) override {
456 const int short_option = m_getopt_table[option_idx].val;
457
458 switch (short_option) {
459 case 'f':
460 m_filename.assign(std::string(option_arg));
461 break;
462 default:
463 llvm_unreachable("Unimplemented option");
464 }
465
466 return error;
467 }
468
469 void OptionParsingStarting(ExecutionContext *execution_context) override {
470 m_filename.clear();
471 }
472
473 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
474 return llvm::ArrayRef(g_settings_read_options);
475 }
476
477 // Instance variables to hold the values for command options.
478 std::string m_filename;
479 };
480
481protected:
482 void DoExecute(Args &command, CommandReturnObject &result) override {
483 FileSpec file(m_options.m_filename);
486 options.SetAddToHistory(false);
487 options.SetEchoCommands(false);
488 options.SetPrintResults(true);
489 options.SetPrintErrors(true);
490 options.SetStopOnError(false);
491 m_interpreter.HandleCommandsFromFile(file, options, result);
492 }
493
494private:
496};
497
498// CommandObjectSettingsList -- List settable variables
499
501public:
503 : CommandObjectParsed(interpreter, "settings list",
504 "List and describe matching debugger settings. "
505 "Defaults to all listing all settings.",
506 nullptr) {
508 CommandArgumentData var_name_arg;
509 CommandArgumentData prefix_name_arg;
510
511 // Define the first variant of this arg.
513 var_name_arg.arg_repetition = eArgRepeatOptional;
514
515 // Define the second variant of this arg.
516 prefix_name_arg.arg_type = eArgTypeSettingPrefix;
517 prefix_name_arg.arg_repetition = eArgRepeatOptional;
518
519 arg.push_back(var_name_arg);
520 arg.push_back(prefix_name_arg);
521
522 // Push the data for the first argument into the m_arguments vector.
523 m_arguments.push_back(arg);
524 }
525
526 ~CommandObjectSettingsList() override = default;
527
528 void
535
536protected:
537 void DoExecute(Args &args, CommandReturnObject &result) override {
539
540 const size_t argc = args.GetArgumentCount();
541 if (argc > 0) {
542 const bool dump_qualified_name = true;
543
544 for (const Args::ArgEntry &arg : args) {
545 const char *property_path = arg.c_str();
546
547 const Property *property =
548 GetDebugger().GetValueProperties()->GetPropertyAtPath(
549 &m_exe_ctx, property_path);
550
551 if (property) {
552 property->DumpDescription(m_interpreter, result.GetOutputStream(), 0,
553 dump_qualified_name);
554 } else {
555 result.AppendErrorWithFormat("invalid property path '%s'",
556 property_path);
557 }
558 }
559 } else {
561 result.GetOutputStream());
562 }
563 }
564};
565
566// CommandObjectSettingsRemove
567
569public:
571 : CommandObjectRaw(interpreter, "settings remove",
572 "Remove a value from a setting, specified by array "
573 "index or dictionary key.") {
576 CommandArgumentData var_name_arg;
577 CommandArgumentData index_arg;
578 CommandArgumentData key_arg;
579
580 // Define the first (and only) variant of this arg.
582 var_name_arg.arg_repetition = eArgRepeatPlain;
583
584 // There is only one variant this argument could be; put it into the
585 // argument entry.
586 arg1.push_back(var_name_arg);
587
588 // Define the first variant of this arg.
589 index_arg.arg_type = eArgTypeSettingIndex;
591
592 // Define the second variant of this arg.
595
596 // Push both variants into this arg
597 arg2.push_back(index_arg);
598 arg2.push_back(key_arg);
599
600 // Push the data for the first argument into the m_arguments vector.
601 m_arguments.push_back(arg1);
602 m_arguments.push_back(arg2);
603 }
604
605 ~CommandObjectSettingsRemove() override = default;
606
607 bool WantsCompletion() override { return true; }
608
609 void
617
618protected:
619 void DoExecute(llvm::StringRef command,
620 CommandReturnObject &result) override {
622
623 Args cmd_args(command);
624
625 // Process possible options.
626 if (!ParseOptions(cmd_args, result))
627 return;
628
629 const size_t argc = cmd_args.GetArgumentCount();
630 if (argc == 0) {
631 result.AppendError("'settings remove' takes an array or dictionary item, "
632 "or an array followed by one or more indexes, or a "
633 "dictionary followed by one or more key names to "
634 "remove");
635 return;
636 }
637
638 const char *var_name = cmd_args.GetArgumentAtIndex(0);
639 if ((var_name == nullptr) || (var_name[0] == '\0')) {
640 result.AppendError(
641 "'settings remove' command requires a valid variable name");
642 return;
643 }
644
645 // Split the raw command into var_name and value pair.
646 llvm::StringRef var_value(command);
647 var_value = var_value.split(var_name).second.trim();
648
649 Status error(GetDebugger().SetPropertyValue(
650 &m_exe_ctx, eVarSetOperationRemove, var_name, var_value));
651 if (error.Fail()) {
652 result.AppendError(error.AsCString());
653 }
654 }
655};
656
657// CommandObjectSettingsReplace
658
660public:
662 : CommandObjectRaw(interpreter, "settings replace",
663 "Replace the debugger setting value specified by "
664 "array index or dictionary key.") {
668 CommandArgumentData var_name_arg;
669 CommandArgumentData index_arg;
670 CommandArgumentData key_arg;
671 CommandArgumentData value_arg;
672
673 // Define the first (and only) variant of this arg.
675 var_name_arg.arg_repetition = eArgRepeatPlain;
676
677 // There is only one variant this argument could be; put it into the
678 // argument entry.
679 arg1.push_back(var_name_arg);
680
681 // Define the first (variant of this arg.
682 index_arg.arg_type = eArgTypeSettingIndex;
684
685 // Define the second (variant of this arg.
688
689 // Put both variants into this arg
690 arg2.push_back(index_arg);
691 arg2.push_back(key_arg);
692
693 // Define the first (and only) variant of this arg.
694 value_arg.arg_type = eArgTypeValue;
696
697 // There is only one variant this argument could be; put it into the
698 // argument entry.
699 arg3.push_back(value_arg);
700
701 // Push the data for the first argument into the m_arguments vector.
702 m_arguments.push_back(arg1);
703 m_arguments.push_back(arg2);
704 m_arguments.push_back(arg3);
705 }
706
707 ~CommandObjectSettingsReplace() override = default;
708
709 // Overrides base class's behavior where WantsCompletion =
710 // !WantsRawCommandString.
711 bool WantsCompletion() override { return true; }
712
713 void
715 OptionElementVector &opt_element_vector) override {
716 // Attempting to complete variable name
717 if (request.GetCursorIndex() < 2)
720 nullptr);
721 }
722
723protected:
724 void DoExecute(llvm::StringRef command,
725 CommandReturnObject &result) override {
727
728 Args cmd_args(command);
729 const char *var_name = cmd_args.GetArgumentAtIndex(0);
730 if ((var_name == nullptr) || (var_name[0] == '\0')) {
731 result.AppendError("'settings replace' command requires a valid variable "
732 "name; No value supplied");
733 return;
734 }
735
736 // Split the raw command into var_name, index_value, and value triple.
737 llvm::StringRef var_value(command);
738 var_value = var_value.split(var_name).second.trim();
739
740 Status error(GetDebugger().SetPropertyValue(
741 &m_exe_ctx, eVarSetOperationReplace, var_name, var_value));
742 if (error.Fail()) {
743 result.AppendError(error.AsCString());
744 } else {
746 }
747 }
748};
749
750// CommandObjectSettingsInsertBefore
751
753public:
755 : CommandObjectRaw(interpreter, "settings insert-before",
756 "Insert one or more values into an debugger array "
757 "setting immediately before the specified element "
758 "index.") {
762 CommandArgumentData var_name_arg;
763 CommandArgumentData index_arg;
764 CommandArgumentData value_arg;
765
766 // Define the first (and only) variant of this arg.
768 var_name_arg.arg_repetition = eArgRepeatPlain;
769
770 // There is only one variant this argument could be; put it into the
771 // argument entry.
772 arg1.push_back(var_name_arg);
773
774 // Define the first (variant of this arg.
775 index_arg.arg_type = eArgTypeSettingIndex;
777
778 // There is only one variant this argument could be; put it into the
779 // argument entry.
780 arg2.push_back(index_arg);
781
782 // Define the first (and only) variant of this arg.
783 value_arg.arg_type = eArgTypeValue;
785
786 // There is only one variant this argument could be; put it into the
787 // argument entry.
788 arg3.push_back(value_arg);
789
790 // Push the data for the first argument into the m_arguments vector.
791 m_arguments.push_back(arg1);
792 m_arguments.push_back(arg2);
793 m_arguments.push_back(arg3);
794 }
795
797
798 // Overrides base class's behavior where WantsCompletion =
799 // !WantsRawCommandString.
800 bool WantsCompletion() override { return true; }
801
802 void
804 OptionElementVector &opt_element_vector) override {
805 // Attempting to complete variable name
806 if (request.GetCursorIndex() < 2)
809 nullptr);
810 }
811
812protected:
813 void DoExecute(llvm::StringRef command,
814 CommandReturnObject &result) override {
816
817 Args cmd_args(command);
818 const size_t argc = cmd_args.GetArgumentCount();
819
820 if (argc < 3) {
821 result.AppendError("'settings insert-before' takes more arguments");
822 return;
823 }
824
825 const char *var_name = cmd_args.GetArgumentAtIndex(0);
826 if ((var_name == nullptr) || (var_name[0] == '\0')) {
827 result.AppendError("'settings insert-before' command requires a valid "
828 "variable name; No value supplied");
829 return;
830 }
831
832 // Split the raw command into var_name, index_value, and value triple.
833 llvm::StringRef var_value(command);
834 var_value = var_value.split(var_name).second.trim();
835
836 Status error(GetDebugger().SetPropertyValue(
837 &m_exe_ctx, eVarSetOperationInsertBefore, var_name, var_value));
838 if (error.Fail()) {
839 result.AppendError(error.AsCString());
840 }
841 }
842};
843
844// CommandObjectSettingInsertAfter
845
847public:
849 : CommandObjectRaw(interpreter, "settings insert-after",
850 "Insert one or more values into a debugger array "
851 "settings after the specified element index.") {
855 CommandArgumentData var_name_arg;
856 CommandArgumentData index_arg;
857 CommandArgumentData value_arg;
858
859 // Define the first (and only) variant of this arg.
861 var_name_arg.arg_repetition = eArgRepeatPlain;
862
863 // There is only one variant this argument could be; put it into the
864 // argument entry.
865 arg1.push_back(var_name_arg);
866
867 // Define the first (variant of this arg.
868 index_arg.arg_type = eArgTypeSettingIndex;
870
871 // There is only one variant this argument could be; put it into the
872 // argument entry.
873 arg2.push_back(index_arg);
874
875 // Define the first (and only) variant of this arg.
876 value_arg.arg_type = eArgTypeValue;
878
879 // There is only one variant this argument could be; put it into the
880 // argument entry.
881 arg3.push_back(value_arg);
882
883 // Push the data for the first argument into the m_arguments vector.
884 m_arguments.push_back(arg1);
885 m_arguments.push_back(arg2);
886 m_arguments.push_back(arg3);
887 }
888
890
891 // Overrides base class's behavior where WantsCompletion =
892 // !WantsRawCommandString.
893 bool WantsCompletion() override { return true; }
894
895 void
897 OptionElementVector &opt_element_vector) override {
898 // Attempting to complete variable name
899 if (request.GetCursorIndex() < 2)
902 nullptr);
903 }
904
905protected:
906 void DoExecute(llvm::StringRef command,
907 CommandReturnObject &result) override {
909
910 Args cmd_args(command);
911 const size_t argc = cmd_args.GetArgumentCount();
912
913 if (argc < 3) {
914 result.AppendError("'settings insert-after' takes more arguments");
915 return;
916 }
917
918 const char *var_name = cmd_args.GetArgumentAtIndex(0);
919 if ((var_name == nullptr) || (var_name[0] == '\0')) {
920 result.AppendError("'settings insert-after' command requires a valid "
921 "variable name; No value supplied");
922 return;
923 }
924
925 // Split the raw command into var_name, index_value, and value triple.
926 llvm::StringRef var_value(command);
927 var_value = var_value.split(var_name).second.trim();
928
929 Status error(GetDebugger().SetPropertyValue(
930 &m_exe_ctx, eVarSetOperationInsertAfter, var_name, var_value));
931 if (error.Fail()) {
932 result.AppendError(error.AsCString());
933 }
934 }
935};
936
937// CommandObjectSettingsAppend
938
940public:
942 : CommandObjectRaw(interpreter, "settings append",
943 "Append one or more values to a debugger array, "
944 "dictionary, or string setting.") {
947 CommandArgumentData var_name_arg;
948 CommandArgumentData value_arg;
949
950 // Define the first (and only) variant of this arg.
952 var_name_arg.arg_repetition = eArgRepeatPlain;
953
954 // There is only one variant this argument could be; put it into the
955 // argument entry.
956 arg1.push_back(var_name_arg);
957
958 // Define the first (and only) variant of this arg.
959 value_arg.arg_type = eArgTypeValue;
961
962 // There is only one variant this argument could be; put it into the
963 // argument entry.
964 arg2.push_back(value_arg);
965
966 // Push the data for the first argument into the m_arguments vector.
967 m_arguments.push_back(arg1);
968 m_arguments.push_back(arg2);
969 }
970
971 ~CommandObjectSettingsAppend() override = default;
972
973 // Overrides base class's behavior where WantsCompletion =
974 // !WantsRawCommandString.
975 bool WantsCompletion() override { return true; }
976
977 void
979 OptionElementVector &opt_element_vector) override {
980 // Attempting to complete variable name
981 if (request.GetCursorIndex() < 2)
984 nullptr);
985 }
986
987protected:
988 void DoExecute(llvm::StringRef command,
989 CommandReturnObject &result) override {
991 Args cmd_args(command);
992 const size_t argc = cmd_args.GetArgumentCount();
993
994 if (argc < 2) {
995 result.AppendError("'settings append' takes more arguments");
996 return;
997 }
998
999 const char *var_name = cmd_args.GetArgumentAtIndex(0);
1000 if ((var_name == nullptr) || (var_name[0] == '\0')) {
1001 result.AppendError("'settings append' command requires a valid variable "
1002 "name; No value supplied");
1003 return;
1004 }
1005
1006 // Do not perform cmd_args.Shift() since StringRef is manipulating the raw
1007 // character string later on.
1008
1009 // Split the raw command into var_name and value pair.
1010 llvm::StringRef var_value(command);
1011 var_value = var_value.split(var_name).second.trim();
1012
1013 Status error(GetDebugger().SetPropertyValue(
1014 &m_exe_ctx, eVarSetOperationAppend, var_name, var_value));
1015 if (error.Fail()) {
1016 result.AppendError(error.AsCString());
1017 }
1018 }
1019};
1020
1021// CommandObjectSettingsClear
1022#define LLDB_OPTIONS_settings_clear
1023#include "CommandOptions.inc"
1024
1026public:
1029 interpreter, "settings clear",
1030 "Clear a debugger setting array, dictionary, or string. "
1031 "If '-a' option is specified, it clears all settings.", nullptr) {
1033 }
1034
1035 ~CommandObjectSettingsClear() override = default;
1036
1037 void
1039 OptionElementVector &opt_element_vector) override {
1040 // Attempting to complete variable name
1041 if (request.GetCursorIndex() < 2)
1044 nullptr);
1045 }
1046
1047 Options *GetOptions() override { return &m_options; }
1048
1049 class CommandOptions : public Options {
1050 public:
1051 CommandOptions() = default;
1052
1053 ~CommandOptions() override = default;
1054
1055 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1056 ExecutionContext *execution_context) override {
1057 const int short_option = m_getopt_table[option_idx].val;
1058 switch (short_option) {
1059 case 'a':
1060 m_clear_all = true;
1061 break;
1062 default:
1063 llvm_unreachable("Unimplemented option");
1064 }
1065 return Status();
1066 }
1067
1068 void OptionParsingStarting(ExecutionContext *execution_context) override {
1069 m_clear_all = false;
1070 }
1071
1072 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
1073 return llvm::ArrayRef(g_settings_clear_options);
1074 }
1075
1076 bool m_clear_all = false;
1077 };
1078
1079protected:
1080 void DoExecute(Args &command, CommandReturnObject &result) override {
1082 const size_t argc = command.GetArgumentCount();
1083
1084 if (m_options.m_clear_all) {
1085 if (argc != 0) {
1086 result.AppendError("'settings clear --all' doesn't take any arguments");
1087 return;
1088 }
1089 GetDebugger().GetValueProperties()->Clear();
1090 return;
1091 }
1092
1093 if (argc != 1) {
1094 result.AppendError("'settings clear' takes exactly one argument");
1095 return;
1096 }
1097
1098 const char *var_name = command.GetArgumentAtIndex(0);
1099 if ((var_name == nullptr) || (var_name[0] == '\0')) {
1100 result.AppendError("'settings clear' command requires a valid variable "
1101 "name; No value supplied");
1102 return;
1103 }
1104
1105 Status error(GetDebugger().SetPropertyValue(
1106 &m_exe_ctx, eVarSetOperationClear, var_name, llvm::StringRef()));
1107 if (error.Fail()) {
1108 result.AppendError(error.AsCString());
1109 }
1110 }
1111
1112 private:
1114};
1115
1116// CommandObjectMultiwordSettings
1117
1119 CommandInterpreter &interpreter)
1120 : CommandObjectMultiword(interpreter, "settings",
1121 "Commands for managing LLDB settings.",
1122 "settings <subcommand> [<command-options>]") {
1123 LoadSubCommand("set",
1124 CommandObjectSP(new CommandObjectSettingsSet(interpreter)));
1125 LoadSubCommand("show",
1126 CommandObjectSP(new CommandObjectSettingsShow(interpreter)));
1127 LoadSubCommand("list",
1128 CommandObjectSP(new CommandObjectSettingsList(interpreter)));
1129 LoadSubCommand("remove",
1132 new CommandObjectSettingsReplace(interpreter)));
1134 "insert-before",
1137 "insert-after",
1139 LoadSubCommand("append",
1141 LoadSubCommand("clear",
1143 LoadSubCommand("write",
1145 LoadSubCommand("read",
1146 CommandObjectSP(new CommandObjectSettingsRead(interpreter)));
1147}
1148
static llvm::raw_ostream & error(Stream &strm)
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
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
"lldb/Utility/ArgCompletionRequest.h"
Status SetPropertyValue(const ExecutionContext *exe_ctx, VarSetOperationType op, llvm::StringRef property_path, llvm::StringRef value) override
Definition Debugger.cpp:278
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
A file utility class.
Definition FileSpec.h:57
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition FileSpec.cpp:374
static FileSystem & Instance()
void Resolve(llvm::SmallVectorImpl< char > &path, bool force_make_absolute=false)
Resolve path to make it canonical.
@ eOpenOptionWriteOnly
Definition File.h:52
@ eOpenOptionCanCreate
Definition File.h:56
@ eOpenOptionTruncate
Definition File.h:57
bool IsValid() const override
IsValid.
Definition File.cpp:113
A command line option parsing protocol class.
Definition Options.h:58
std::vector< Option > m_getopt_table
Definition Options.h:198
virtual void DumpAllDescriptions(CommandInterpreter &interpreter, Stream &strm) const
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
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.
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.