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 default:
268 llvm_unreachable("Unimplemented option");
269 }
270 return {};
271 }
272
273 void OptionParsingStarting(ExecutionContext *execution_context) override {
274 m_include_defaults = false;
275 }
276
277 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
278 return g_settings_show_options;
279 }
280
281 bool m_include_defaults = false;
282 };
283
284protected:
285 void DoExecute(Args &args, CommandReturnObject &result) override {
287
288 uint32_t dump_mask = OptionValue::eDumpGroupValue;
289 if (m_options.m_include_defaults)
291
292 if (!args.empty()) {
293 for (const auto &arg : args) {
294 Status error(GetDebugger().DumpPropertyValue(
295 &m_exe_ctx, result.GetOutputStream(), arg.ref(), dump_mask));
296 if (error.Success()) {
297 result.GetOutputStream().EOL();
298 } else {
299 result.AppendError(error.AsCString());
300 }
301 }
302 } else {
304 dump_mask);
305 }
306 }
307
308private:
310};
311
312// CommandObjectSettingsWrite -- Write settings to file
313#define LLDB_OPTIONS_settings_write
314#include "CommandOptions.inc"
315
317public:
320 interpreter, "settings export",
321 "Write matching debugger settings and their "
322 "current values to a file that can be read in with "
323 "\"settings read\". Defaults to writing all settings.",
324 nullptr) {
326 }
327
328 ~CommandObjectSettingsWrite() override = default;
329
330 Options *GetOptions() override { return &m_options; }
331
332 class CommandOptions : public Options {
333 public:
334 CommandOptions() = default;
335
336 ~CommandOptions() override = default;
337
338 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
339 ExecutionContext *execution_context) override {
341 const int short_option = m_getopt_table[option_idx].val;
342
343 switch (short_option) {
344 case 'f':
345 m_filename.assign(std::string(option_arg));
346 break;
347 case 'a':
348 m_append = true;
349 break;
350 default:
351 llvm_unreachable("Unimplemented option");
352 }
353
354 return error;
355 }
356
357 void OptionParsingStarting(ExecutionContext *execution_context) override {
358 m_filename.clear();
359 m_append = false;
360 }
361
362 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
363 return llvm::ArrayRef(g_settings_write_options);
364 }
365
366 // Instance variables to hold the values for command options.
367 std::string m_filename;
368 bool m_append = false;
369 };
370
371protected:
372 void DoExecute(Args &args, CommandReturnObject &result) override {
373 FileSpec file_spec(m_options.m_filename);
374 FileSystem::Instance().Resolve(file_spec);
375 std::string path(file_spec.GetPath());
377 if (m_options.m_append)
378 options |= File::eOpenOptionAppend;
379 else
380 options |= File::eOpenOptionTruncate;
381
382 StreamFile out_file(path.c_str(), options,
383 lldb::eFilePermissionsFileDefault);
384
385 if (!out_file.GetFile().IsValid()) {
386 result.AppendErrorWithFormat("%s: unable to write to file", path.c_str());
387 return;
388 }
389
390 // Exporting should not be context sensitive.
391 ExecutionContext clean_ctx;
392
393 if (args.empty()) {
394 GetDebugger().DumpAllPropertyValues(&clean_ctx, out_file,
396 return;
397 }
398
399 for (const auto &arg : args) {
400 Status error(GetDebugger().DumpPropertyValue(
401 &clean_ctx, out_file, arg.ref(), OptionValue::eDumpGroupExport));
402 if (!error.Success()) {
403 result.AppendError(error.AsCString());
404 }
405 }
406 }
407
408private:
410};
411
412// CommandObjectSettingsRead -- Read settings from file
413#define LLDB_OPTIONS_settings_read
414#include "CommandOptions.inc"
415
417public:
420 interpreter, "settings read",
421 "Read settings previously saved to a file with \"settings write\".",
422 nullptr) {}
423
424 ~CommandObjectSettingsRead() override = default;
425
426 Options *GetOptions() override { return &m_options; }
427
428 class CommandOptions : public Options {
429 public:
430 CommandOptions() = default;
431
432 ~CommandOptions() override = default;
433
434 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
435 ExecutionContext *execution_context) override {
437 const int short_option = m_getopt_table[option_idx].val;
438
439 switch (short_option) {
440 case 'f':
441 m_filename.assign(std::string(option_arg));
442 break;
443 default:
444 llvm_unreachable("Unimplemented option");
445 }
446
447 return error;
448 }
449
450 void OptionParsingStarting(ExecutionContext *execution_context) override {
451 m_filename.clear();
452 }
453
454 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
455 return llvm::ArrayRef(g_settings_read_options);
456 }
457
458 // Instance variables to hold the values for command options.
459 std::string m_filename;
460 };
461
462protected:
463 void DoExecute(Args &command, CommandReturnObject &result) override {
464 FileSpec file(m_options.m_filename);
467 options.SetAddToHistory(false);
468 options.SetEchoCommands(false);
469 options.SetPrintResults(true);
470 options.SetPrintErrors(true);
471 options.SetStopOnError(false);
472 m_interpreter.HandleCommandsFromFile(file, options, result);
473 }
474
475private:
477};
478
479// CommandObjectSettingsList -- List settable variables
480
482public:
484 : CommandObjectParsed(interpreter, "settings list",
485 "List and describe matching debugger settings. "
486 "Defaults to all listing all settings.",
487 nullptr) {
489 CommandArgumentData var_name_arg;
490 CommandArgumentData prefix_name_arg;
491
492 // Define the first variant of this arg.
494 var_name_arg.arg_repetition = eArgRepeatOptional;
495
496 // Define the second variant of this arg.
497 prefix_name_arg.arg_type = eArgTypeSettingPrefix;
498 prefix_name_arg.arg_repetition = eArgRepeatOptional;
499
500 arg.push_back(var_name_arg);
501 arg.push_back(prefix_name_arg);
502
503 // Push the data for the first argument into the m_arguments vector.
504 m_arguments.push_back(arg);
505 }
506
507 ~CommandObjectSettingsList() override = default;
508
509 void
516
517protected:
518 void DoExecute(Args &args, CommandReturnObject &result) override {
520
521 const size_t argc = args.GetArgumentCount();
522 if (argc > 0) {
523 const bool dump_qualified_name = true;
524
525 for (const Args::ArgEntry &arg : args) {
526 const char *property_path = arg.c_str();
527
528 const Property *property =
529 GetDebugger().GetValueProperties()->GetPropertyAtPath(
530 &m_exe_ctx, property_path);
531
532 if (property) {
533 property->DumpDescription(m_interpreter, result.GetOutputStream(), 0,
534 dump_qualified_name);
535 } else {
536 result.AppendErrorWithFormat("invalid property path '%s'",
537 property_path);
538 }
539 }
540 } else {
542 result.GetOutputStream());
543 }
544 }
545};
546
547// CommandObjectSettingsRemove
548
550public:
552 : CommandObjectRaw(interpreter, "settings remove",
553 "Remove a value from a setting, specified by array "
554 "index or dictionary key.") {
557 CommandArgumentData var_name_arg;
558 CommandArgumentData index_arg;
559 CommandArgumentData key_arg;
560
561 // Define the first (and only) variant of this arg.
563 var_name_arg.arg_repetition = eArgRepeatPlain;
564
565 // There is only one variant this argument could be; put it into the
566 // argument entry.
567 arg1.push_back(var_name_arg);
568
569 // Define the first variant of this arg.
570 index_arg.arg_type = eArgTypeSettingIndex;
572
573 // Define the second variant of this arg.
576
577 // Push both variants into this arg
578 arg2.push_back(index_arg);
579 arg2.push_back(key_arg);
580
581 // Push the data for the first argument into the m_arguments vector.
582 m_arguments.push_back(arg1);
583 m_arguments.push_back(arg2);
584 }
585
586 ~CommandObjectSettingsRemove() override = default;
587
588 bool WantsCompletion() override { return true; }
589
590 void
598
599protected:
600 void DoExecute(llvm::StringRef command,
601 CommandReturnObject &result) override {
603
604 Args cmd_args(command);
605
606 // Process possible options.
607 if (!ParseOptions(cmd_args, result))
608 return;
609
610 const size_t argc = cmd_args.GetArgumentCount();
611 if (argc == 0) {
612 result.AppendError("'settings remove' takes an array or dictionary item, "
613 "or an array followed by one or more indexes, or a "
614 "dictionary followed by one or more key names to "
615 "remove");
616 return;
617 }
618
619 const char *var_name = cmd_args.GetArgumentAtIndex(0);
620 if ((var_name == nullptr) || (var_name[0] == '\0')) {
621 result.AppendError(
622 "'settings remove' command requires a valid variable name");
623 return;
624 }
625
626 // Split the raw command into var_name and value pair.
627 llvm::StringRef var_value(command);
628 var_value = var_value.split(var_name).second.trim();
629
630 Status error(GetDebugger().SetPropertyValue(
631 &m_exe_ctx, eVarSetOperationRemove, var_name, var_value));
632 if (error.Fail()) {
633 result.AppendError(error.AsCString());
634 }
635 }
636};
637
638// CommandObjectSettingsReplace
639
641public:
643 : CommandObjectRaw(interpreter, "settings replace",
644 "Replace the debugger setting value specified by "
645 "array index or dictionary key.") {
649 CommandArgumentData var_name_arg;
650 CommandArgumentData index_arg;
651 CommandArgumentData key_arg;
652 CommandArgumentData value_arg;
653
654 // Define the first (and only) variant of this arg.
656 var_name_arg.arg_repetition = eArgRepeatPlain;
657
658 // There is only one variant this argument could be; put it into the
659 // argument entry.
660 arg1.push_back(var_name_arg);
661
662 // Define the first (variant of this arg.
663 index_arg.arg_type = eArgTypeSettingIndex;
665
666 // Define the second (variant of this arg.
669
670 // Put both variants into this arg
671 arg2.push_back(index_arg);
672 arg2.push_back(key_arg);
673
674 // Define the first (and only) variant of this arg.
675 value_arg.arg_type = eArgTypeValue;
677
678 // There is only one variant this argument could be; put it into the
679 // argument entry.
680 arg3.push_back(value_arg);
681
682 // Push the data for the first argument into the m_arguments vector.
683 m_arguments.push_back(arg1);
684 m_arguments.push_back(arg2);
685 m_arguments.push_back(arg3);
686 }
687
688 ~CommandObjectSettingsReplace() override = default;
689
690 // Overrides base class's behavior where WantsCompletion =
691 // !WantsRawCommandString.
692 bool WantsCompletion() override { return true; }
693
694 void
696 OptionElementVector &opt_element_vector) override {
697 // Attempting to complete variable name
698 if (request.GetCursorIndex() < 2)
701 nullptr);
702 }
703
704protected:
705 void DoExecute(llvm::StringRef command,
706 CommandReturnObject &result) override {
708
709 Args cmd_args(command);
710 const char *var_name = cmd_args.GetArgumentAtIndex(0);
711 if ((var_name == nullptr) || (var_name[0] == '\0')) {
712 result.AppendError("'settings replace' command requires a valid variable "
713 "name; No value supplied");
714 return;
715 }
716
717 // Split the raw command into var_name, index_value, and value triple.
718 llvm::StringRef var_value(command);
719 var_value = var_value.split(var_name).second.trim();
720
721 Status error(GetDebugger().SetPropertyValue(
722 &m_exe_ctx, eVarSetOperationReplace, var_name, var_value));
723 if (error.Fail()) {
724 result.AppendError(error.AsCString());
725 } else {
727 }
728 }
729};
730
731// CommandObjectSettingsInsertBefore
732
734public:
736 : CommandObjectRaw(interpreter, "settings insert-before",
737 "Insert one or more values into an debugger array "
738 "setting immediately before the specified element "
739 "index.") {
743 CommandArgumentData var_name_arg;
744 CommandArgumentData index_arg;
745 CommandArgumentData value_arg;
746
747 // Define the first (and only) variant of this arg.
749 var_name_arg.arg_repetition = eArgRepeatPlain;
750
751 // There is only one variant this argument could be; put it into the
752 // argument entry.
753 arg1.push_back(var_name_arg);
754
755 // Define the first (variant of this arg.
756 index_arg.arg_type = eArgTypeSettingIndex;
758
759 // There is only one variant this argument could be; put it into the
760 // argument entry.
761 arg2.push_back(index_arg);
762
763 // Define the first (and only) variant of this arg.
764 value_arg.arg_type = eArgTypeValue;
766
767 // There is only one variant this argument could be; put it into the
768 // argument entry.
769 arg3.push_back(value_arg);
770
771 // Push the data for the first argument into the m_arguments vector.
772 m_arguments.push_back(arg1);
773 m_arguments.push_back(arg2);
774 m_arguments.push_back(arg3);
775 }
776
778
779 // Overrides base class's behavior where WantsCompletion =
780 // !WantsRawCommandString.
781 bool WantsCompletion() override { return true; }
782
783 void
785 OptionElementVector &opt_element_vector) override {
786 // Attempting to complete variable name
787 if (request.GetCursorIndex() < 2)
790 nullptr);
791 }
792
793protected:
794 void DoExecute(llvm::StringRef command,
795 CommandReturnObject &result) override {
797
798 Args cmd_args(command);
799 const size_t argc = cmd_args.GetArgumentCount();
800
801 if (argc < 3) {
802 result.AppendError("'settings insert-before' takes more arguments");
803 return;
804 }
805
806 const char *var_name = cmd_args.GetArgumentAtIndex(0);
807 if ((var_name == nullptr) || (var_name[0] == '\0')) {
808 result.AppendError("'settings insert-before' command requires a valid "
809 "variable name; No value supplied");
810 return;
811 }
812
813 // Split the raw command into var_name, index_value, and value triple.
814 llvm::StringRef var_value(command);
815 var_value = var_value.split(var_name).second.trim();
816
817 Status error(GetDebugger().SetPropertyValue(
818 &m_exe_ctx, eVarSetOperationInsertBefore, var_name, var_value));
819 if (error.Fail()) {
820 result.AppendError(error.AsCString());
821 }
822 }
823};
824
825// CommandObjectSettingInsertAfter
826
828public:
830 : CommandObjectRaw(interpreter, "settings insert-after",
831 "Insert one or more values into a debugger array "
832 "settings after the specified element index.") {
836 CommandArgumentData var_name_arg;
837 CommandArgumentData index_arg;
838 CommandArgumentData value_arg;
839
840 // Define the first (and only) variant of this arg.
842 var_name_arg.arg_repetition = eArgRepeatPlain;
843
844 // There is only one variant this argument could be; put it into the
845 // argument entry.
846 arg1.push_back(var_name_arg);
847
848 // Define the first (variant of this arg.
849 index_arg.arg_type = eArgTypeSettingIndex;
851
852 // There is only one variant this argument could be; put it into the
853 // argument entry.
854 arg2.push_back(index_arg);
855
856 // Define the first (and only) variant of this arg.
857 value_arg.arg_type = eArgTypeValue;
859
860 // There is only one variant this argument could be; put it into the
861 // argument entry.
862 arg3.push_back(value_arg);
863
864 // Push the data for the first argument into the m_arguments vector.
865 m_arguments.push_back(arg1);
866 m_arguments.push_back(arg2);
867 m_arguments.push_back(arg3);
868 }
869
871
872 // Overrides base class's behavior where WantsCompletion =
873 // !WantsRawCommandString.
874 bool WantsCompletion() override { return true; }
875
876 void
878 OptionElementVector &opt_element_vector) override {
879 // Attempting to complete variable name
880 if (request.GetCursorIndex() < 2)
883 nullptr);
884 }
885
886protected:
887 void DoExecute(llvm::StringRef command,
888 CommandReturnObject &result) override {
890
891 Args cmd_args(command);
892 const size_t argc = cmd_args.GetArgumentCount();
893
894 if (argc < 3) {
895 result.AppendError("'settings insert-after' takes more arguments");
896 return;
897 }
898
899 const char *var_name = cmd_args.GetArgumentAtIndex(0);
900 if ((var_name == nullptr) || (var_name[0] == '\0')) {
901 result.AppendError("'settings insert-after' command requires a valid "
902 "variable name; No value supplied");
903 return;
904 }
905
906 // Split the raw command into var_name, index_value, and value triple.
907 llvm::StringRef var_value(command);
908 var_value = var_value.split(var_name).second.trim();
909
910 Status error(GetDebugger().SetPropertyValue(
911 &m_exe_ctx, eVarSetOperationInsertAfter, var_name, var_value));
912 if (error.Fail()) {
913 result.AppendError(error.AsCString());
914 }
915 }
916};
917
918// CommandObjectSettingsAppend
919
921public:
923 : CommandObjectRaw(interpreter, "settings append",
924 "Append one or more values to a debugger array, "
925 "dictionary, or string setting.") {
928 CommandArgumentData var_name_arg;
929 CommandArgumentData value_arg;
930
931 // Define the first (and only) variant of this arg.
933 var_name_arg.arg_repetition = eArgRepeatPlain;
934
935 // There is only one variant this argument could be; put it into the
936 // argument entry.
937 arg1.push_back(var_name_arg);
938
939 // Define the first (and only) variant of this arg.
940 value_arg.arg_type = eArgTypeValue;
942
943 // There is only one variant this argument could be; put it into the
944 // argument entry.
945 arg2.push_back(value_arg);
946
947 // Push the data for the first argument into the m_arguments vector.
948 m_arguments.push_back(arg1);
949 m_arguments.push_back(arg2);
950 }
951
952 ~CommandObjectSettingsAppend() override = default;
953
954 // Overrides base class's behavior where WantsCompletion =
955 // !WantsRawCommandString.
956 bool WantsCompletion() override { return true; }
957
958 void
960 OptionElementVector &opt_element_vector) override {
961 // Attempting to complete variable name
962 if (request.GetCursorIndex() < 2)
965 nullptr);
966 }
967
968protected:
969 void DoExecute(llvm::StringRef command,
970 CommandReturnObject &result) override {
972 Args cmd_args(command);
973 const size_t argc = cmd_args.GetArgumentCount();
974
975 if (argc < 2) {
976 result.AppendError("'settings append' takes more arguments");
977 return;
978 }
979
980 const char *var_name = cmd_args.GetArgumentAtIndex(0);
981 if ((var_name == nullptr) || (var_name[0] == '\0')) {
982 result.AppendError("'settings append' command requires a valid variable "
983 "name; No value supplied");
984 return;
985 }
986
987 // Do not perform cmd_args.Shift() since StringRef is manipulating the raw
988 // character string later on.
989
990 // Split the raw command into var_name and value pair.
991 llvm::StringRef var_value(command);
992 var_value = var_value.split(var_name).second.trim();
993
994 Status error(GetDebugger().SetPropertyValue(
995 &m_exe_ctx, eVarSetOperationAppend, var_name, var_value));
996 if (error.Fail()) {
997 result.AppendError(error.AsCString());
998 }
999 }
1000};
1001
1002// CommandObjectSettingsClear
1003#define LLDB_OPTIONS_settings_clear
1004#include "CommandOptions.inc"
1005
1007public:
1010 interpreter, "settings clear",
1011 "Clear a debugger setting array, dictionary, or string. "
1012 "If '-a' option is specified, it clears all settings.", nullptr) {
1014 }
1015
1016 ~CommandObjectSettingsClear() override = default;
1017
1018 void
1020 OptionElementVector &opt_element_vector) override {
1021 // Attempting to complete variable name
1022 if (request.GetCursorIndex() < 2)
1025 nullptr);
1026 }
1027
1028 Options *GetOptions() override { return &m_options; }
1029
1030 class CommandOptions : public Options {
1031 public:
1032 CommandOptions() = default;
1033
1034 ~CommandOptions() override = default;
1035
1036 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
1037 ExecutionContext *execution_context) override {
1038 const int short_option = m_getopt_table[option_idx].val;
1039 switch (short_option) {
1040 case 'a':
1041 m_clear_all = true;
1042 break;
1043 default:
1044 llvm_unreachable("Unimplemented option");
1045 }
1046 return Status();
1047 }
1048
1049 void OptionParsingStarting(ExecutionContext *execution_context) override {
1050 m_clear_all = false;
1051 }
1052
1053 llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
1054 return llvm::ArrayRef(g_settings_clear_options);
1055 }
1056
1057 bool m_clear_all = false;
1058 };
1059
1060protected:
1061 void DoExecute(Args &command, CommandReturnObject &result) override {
1063 const size_t argc = command.GetArgumentCount();
1064
1065 if (m_options.m_clear_all) {
1066 if (argc != 0) {
1067 result.AppendError("'settings clear --all' doesn't take any arguments");
1068 return;
1069 }
1070 GetDebugger().GetValueProperties()->Clear();
1071 return;
1072 }
1073
1074 if (argc != 1) {
1075 result.AppendError("'settings clear' takes exactly one argument");
1076 return;
1077 }
1078
1079 const char *var_name = command.GetArgumentAtIndex(0);
1080 if ((var_name == nullptr) || (var_name[0] == '\0')) {
1081 result.AppendError("'settings clear' command requires a valid variable "
1082 "name; No value supplied");
1083 return;
1084 }
1085
1086 Status error(GetDebugger().SetPropertyValue(
1087 &m_exe_ctx, eVarSetOperationClear, var_name, llvm::StringRef()));
1088 if (error.Fail()) {
1089 result.AppendError(error.AsCString());
1090 }
1091 }
1092
1093 private:
1095};
1096
1097// CommandObjectMultiwordSettings
1098
1100 CommandInterpreter &interpreter)
1101 : CommandObjectMultiword(interpreter, "settings",
1102 "Commands for managing LLDB settings.",
1103 "settings <subcommand> [<command-options>]") {
1104 LoadSubCommand("set",
1105 CommandObjectSP(new CommandObjectSettingsSet(interpreter)));
1106 LoadSubCommand("show",
1107 CommandObjectSP(new CommandObjectSettingsShow(interpreter)));
1108 LoadSubCommand("list",
1109 CommandObjectSP(new CommandObjectSettingsList(interpreter)));
1110 LoadSubCommand("remove",
1113 new CommandObjectSettingsReplace(interpreter)));
1115 "insert-before",
1118 "insert-after",
1120 LoadSubCommand("append",
1122 LoadSubCommand("clear",
1124 LoadSubCommand("write",
1126 LoadSubCommand("read",
1127 CommandObjectSP(new CommandObjectSettingsRead(interpreter)));
1128}
1129
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 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:205
"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
void Resolve(llvm::SmallVectorImpl< char > &path)
Resolve path to make it canonical.
static FileSystem & Instance()
@ eOpenOptionWriteOnly
Definition File.h:52
@ eOpenOptionCanCreate
Definition File.h:56
@ eOpenOptionTruncate
Definition File.h:57
bool IsValid() const override
IsValid.
Definition File.cpp:114
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)
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
@ eReturnStatusSuccessFinishResult
@ eReturnStatusSuccessFinishNoResult
@ eArgTypeSettingPrefix
@ eArgTypeSettingIndex
@ eArgTypeSettingVariableName
std::shared_ptr< lldb_private::OptionValue > OptionValueSP
Used to build individual command argument lists.