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