1"""
2Test lldb settings command.
3"""
4
5
6
7import os
8import re
9import lldb
10from lldbsuite.test.decorators import *
11from lldbsuite.test.lldbtest import *
12from lldbsuite.test import lldbutil
13
14
15class SettingsCommandTestCase(TestBase):
16
17    mydir = TestBase.compute_mydir(__file__)
18    NO_DEBUG_INFO_TESTCASE = True
19
20    def test_apropos_should_also_search_settings_description(self):
21        """Test that 'apropos' command should also search descriptions for the settings variables."""
22
23        self.expect("apropos 'environment variable'",
24                    substrs=["target.env-vars",
25                             "environment variables",
26                             "executable's environment"])
27
28    def test_append_target_env_vars(self):
29        """Test that 'append target.run-args' works."""
30        # Append the env-vars.
31        self.runCmd('settings append target.env-vars MY_ENV_VAR=YES')
32        # And add hooks to restore the settings during tearDown().
33        self.addTearDownHook(
34            lambda: self.runCmd("settings clear target.env-vars"))
35
36        # Check it immediately!
37        self.expect('settings show target.env-vars',
38                    substrs=['MY_ENV_VAR=YES'])
39
40    def test_insert_before_and_after_target_run_args(self):
41        """Test that 'insert-before/after target.run-args' works."""
42        # Set the run-args first.
43        self.runCmd('settings set target.run-args a b c')
44        # And add hooks to restore the settings during tearDown().
45        self.addTearDownHook(
46            lambda: self.runCmd("settings clear target.run-args"))
47
48        # Now insert-before the index-0 element with '__a__'.
49        self.runCmd('settings insert-before target.run-args 0 __a__')
50        # And insert-after the index-1 element with '__A__'.
51        self.runCmd('settings insert-after target.run-args 1 __A__')
52        # Check it immediately!
53        self.expect('settings show target.run-args',
54                    substrs=['target.run-args',
55                             '[0]: "__a__"',
56                             '[1]: "a"',
57                             '[2]: "__A__"',
58                             '[3]: "b"',
59                             '[4]: "c"'])
60
61    def test_replace_target_run_args(self):
62        """Test that 'replace target.run-args' works."""
63        # Set the run-args and then replace the index-0 element.
64        self.runCmd('settings set target.run-args a b c')
65        # And add hooks to restore the settings during tearDown().
66        self.addTearDownHook(
67            lambda: self.runCmd("settings clear target.run-args"))
68
69        # Now replace the index-0 element with 'A', instead.
70        self.runCmd('settings replace target.run-args 0 A')
71        # Check it immediately!
72        self.expect('settings show target.run-args',
73                    substrs=['target.run-args (arguments) =',
74                             '[0]: "A"',
75                             '[1]: "b"',
76                             '[2]: "c"'])
77
78    def test_set_prompt(self):
79        """Test that 'set prompt' actually changes the prompt."""
80
81        # Set prompt to 'lldb2'.
82        self.runCmd("settings set prompt 'lldb2 '")
83
84        # Immediately test the setting.
85        self.expect("settings show prompt", SETTING_MSG("prompt"),
86                    startstr='prompt (string) = "lldb2 "')
87
88        # The overall display should also reflect the new setting.
89        self.expect("settings show", SETTING_MSG("prompt"),
90                    substrs=['prompt (string) = "lldb2 "'])
91
92        # Use '-r' option to reset to the original default prompt.
93        self.runCmd("settings clear prompt")
94
95    def test_set_term_width(self):
96        """Test that 'set term-width' actually changes the term-width."""
97
98        self.runCmd("settings set term-width 70")
99
100        # Immediately test the setting.
101        self.expect("settings show term-width", SETTING_MSG("term-width"),
102                    startstr="term-width (int) = 70")
103
104        # The overall display should also reflect the new setting.
105        self.expect("settings show", SETTING_MSG("term-width"),
106                    substrs=["term-width (int) = 70"])
107
108    # rdar://problem/10712130
109    @skipIf(oslist=["windows"], bugnumber="llvm.org/pr44431")
110    def test_set_frame_format(self):
111        """Test that 'set frame-format' with a backtick char in the format string works as well as fullpath."""
112        self.build()
113
114        exe = self.getBuildArtifact("a.out")
115        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
116
117        def cleanup():
118            self.runCmd(
119                "settings set frame-format %s" %
120                self.format_string, check=False)
121
122        # Execute the cleanup function during test case tear down.
123        self.addTearDownHook(cleanup)
124
125        self.runCmd("settings show frame-format")
126        m = re.match(
127            '^frame-format \(format-string\) = "(.*)\"$',
128            self.res.GetOutput())
129        self.assertTrue(m, "Bad settings string")
130        self.format_string = m.group(1)
131
132        # Change the default format to print function.name rather than
133        # function.name-with-args
134        format_string = "frame #${frame.index}: ${frame.pc}{ ${module.file.basename}\`${function.name}{${function.pc-offset}}}{ at ${line.file.fullpath}:${line.number}}{, lang=${language}}\n"
135        self.runCmd("settings set frame-format %s" % format_string)
136
137        # Immediately test the setting.
138        self.expect("settings show frame-format", SETTING_MSG("frame-format"),
139                    substrs=[format_string])
140
141        self.runCmd("breakpoint set -n main")
142        self.runCmd("process launch --working-dir '{0}'".format(self.get_process_working_directory()),
143                RUN_SUCCEEDED)
144        self.expect("thread backtrace",
145                    substrs=["`main", self.getSourceDir()])
146
147    def test_set_auto_confirm(self):
148        """Test that after 'set auto-confirm true', manual confirmation should not kick in."""
149        self.build()
150
151        exe = self.getBuildArtifact("a.out")
152        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
153
154        self.runCmd("settings set auto-confirm true")
155
156        # Immediately test the setting.
157        self.expect("settings show auto-confirm", SETTING_MSG("auto-confirm"),
158                    startstr="auto-confirm (boolean) = true")
159
160        # Now 'breakpoint delete' should just work fine without confirmation
161        # prompt from the command interpreter.
162        self.runCmd("breakpoint set -n main")
163        self.expect("breakpoint delete",
164                    startstr="All breakpoints removed")
165
166        # Restore the original setting of auto-confirm.
167        self.runCmd("settings clear auto-confirm")
168        self.expect("settings show auto-confirm", SETTING_MSG("auto-confirm"),
169                    startstr="auto-confirm (boolean) = false")
170
171    @skipIf(archs=no_match(['x86_64', 'i386', 'i686']))
172    def test_disassembler_settings(self):
173        """Test that user options for the disassembler take effect."""
174        self.build()
175
176        exe = self.getBuildArtifact("a.out")
177        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
178
179        # AT&T syntax
180        self.runCmd("settings set target.x86-disassembly-flavor att")
181        self.runCmd("settings set target.use-hex-immediates false")
182        self.expect("disassemble -n numberfn",
183                    substrs=["$90"])
184        self.runCmd("settings set target.use-hex-immediates true")
185        self.runCmd("settings set target.hex-immediate-style c")
186        self.expect("disassemble -n numberfn",
187                    substrs=["$0x5a"])
188        self.runCmd("settings set target.hex-immediate-style asm")
189        self.expect("disassemble -n numberfn",
190                    substrs=["$5ah"])
191
192        # Intel syntax
193        self.runCmd("settings set target.x86-disassembly-flavor intel")
194        self.runCmd("settings set target.use-hex-immediates false")
195        self.expect("disassemble -n numberfn",
196                    substrs=["90"])
197        self.runCmd("settings set target.use-hex-immediates true")
198        self.runCmd("settings set target.hex-immediate-style c")
199        self.expect("disassemble -n numberfn",
200                    substrs=["0x5a"])
201        self.runCmd("settings set target.hex-immediate-style asm")
202        self.expect("disassemble -n numberfn",
203                    substrs=["5ah"])
204
205    @skipIfDarwinEmbedded   # <rdar://problem/34446098> debugserver on ios etc can't write files
206    def test_run_args_and_env_vars(self):
207        self.do_test_run_args_and_env_vars(use_launchsimple=False)
208
209    @skipIfDarwinEmbedded   # <rdar://problem/34446098> debugserver on ios etc can't write files
210    def test_launchsimple_args_and_env_vars(self):
211        self.do_test_run_args_and_env_vars(use_launchsimple=True)
212
213    def do_test_run_args_and_env_vars(self, use_launchsimple):
214        """Test that run-args and env-vars are passed to the launched process."""
215        self.build()
216
217        # Set the run-args and the env-vars.
218        # And add hooks to restore the settings during tearDown().
219        self.runCmd('settings set target.run-args A B C')
220        self.addTearDownHook(
221            lambda: self.runCmd("settings clear target.run-args"))
222        self.runCmd('settings set target.env-vars ["MY_ENV_VAR"]=YES')
223        self.addTearDownHook(
224            lambda: self.runCmd("settings clear target.env-vars"))
225
226        exe = self.getBuildArtifact("a.out")
227        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
228
229        target = self.dbg.GetTargetAtIndex(0)
230        launch_info = target.GetLaunchInfo()
231        found_env_var = False
232        for i in range(0, launch_info.GetNumEnvironmentEntries()):
233            if launch_info.GetEnvironmentEntryAtIndex(i) == "MY_ENV_VAR=YES":
234                found_env_var = True
235                break
236        self.assertTrue(found_env_var,
237                        "MY_ENV_VAR was not set in LunchInfo object")
238
239        self.expect(
240            'target show-launch-environment',
241            substrs=["MY_ENV_VAR=YES"])
242
243        wd = self.get_process_working_directory()
244        if use_launchsimple:
245            process = target.LaunchSimple(None, None, wd)
246            self.assertTrue(process)
247        else:
248            self.runCmd("process launch --working-dir '{0}'".format(wd),
249                RUN_SUCCEEDED)
250
251        # Read the output file produced by running the program.
252        output = lldbutil.read_file_from_process_wd(self, "output2.txt")
253
254        self.expect(
255            output,
256            exe=False,
257            substrs=[
258                "argv[1] matches",
259                "argv[2] matches",
260                "argv[3] matches",
261                "Environment variable 'MY_ENV_VAR' successfully passed."])
262
263        # Check that env-vars overrides unset-env-vars.
264        self.runCmd('settings set target.unset-env-vars MY_ENV_VAR')
265
266        self.expect(
267            'target show-launch-environment',
268            'env-vars overrides unset-env-vars',
269            substrs=["MY_ENV_VAR=YES"])
270
271        wd = self.get_process_working_directory()
272        if use_launchsimple:
273            process = target.LaunchSimple(None, None, wd)
274            self.assertTrue(process)
275        else:
276            self.runCmd("process launch --working-dir '{0}'".format(wd),
277                RUN_SUCCEEDED)
278
279        # Read the output file produced by running the program.
280        output = lldbutil.read_file_from_process_wd(self, "output2.txt")
281
282        self.expect(
283            output,
284            exe=False,
285            substrs=[
286                "Environment variable 'MY_ENV_VAR' successfully passed."])
287
288    @skipIfRemote  # it doesn't make sense to send host env to remote target
289    @skipIf(oslist=["windows"])
290    def test_pass_host_env_vars(self):
291        """Test that the host env vars are passed to the launched process."""
292        self.build()
293
294        # Set some host environment variables now.
295        os.environ["MY_HOST_ENV_VAR1"] = "VAR1"
296        os.environ["MY_HOST_ENV_VAR2"] = "VAR2"
297
298        # This is the function to unset the two env variables set above.
299        def unset_env_variables():
300            os.environ.pop("MY_HOST_ENV_VAR1")
301            os.environ.pop("MY_HOST_ENV_VAR2")
302        self.addTearDownHook(unset_env_variables)
303
304        exe = self.getBuildArtifact("a.out")
305        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
306
307        # By default, inherit-env is 'true'.
308        self.expect(
309            'settings show target.inherit-env',
310            "Default inherit-env is 'true'",
311            startstr="target.inherit-env (boolean) = true")
312
313        self.expect(
314            'target show-launch-environment',
315            'Host environment is passed correctly',
316            substrs=['MY_HOST_ENV_VAR1=VAR1', 'MY_HOST_ENV_VAR2=VAR2'])
317        self.runCmd("process launch --working-dir '{0}'".format(self.get_process_working_directory()),
318                RUN_SUCCEEDED)
319
320        # Read the output file produced by running the program.
321        output = lldbutil.read_file_from_process_wd(self, "output1.txt")
322
323        self.expect(
324            output,
325            exe=False,
326            substrs=[
327                "The host environment variable 'MY_HOST_ENV_VAR1' successfully passed.",
328                "The host environment variable 'MY_HOST_ENV_VAR2' successfully passed."])
329
330        # Now test that we can prevent the inferior from inheriting the
331        # environment.
332        self.runCmd('settings set target.inherit-env false')
333
334        self.expect(
335            'target show-launch-environment',
336            'target.inherit-env affects `target show-launch-environment`',
337            matching=False,
338            substrs = ['MY_HOST_ENV_VAR1=VAR1', 'MY_HOST_ENV_VAR2=VAR2'])
339
340        self.runCmd("process launch --working-dir '{0}'".format(self.get_process_working_directory()),
341                RUN_SUCCEEDED)
342
343        # Read the output file produced by running the program.
344        output = lldbutil.read_file_from_process_wd(self, "output1.txt")
345
346        self.expect(
347            output,
348            exe=False,
349            matching=False,
350            substrs=[
351                "The host environment variable 'MY_HOST_ENV_VAR1' successfully passed.",
352                "The host environment variable 'MY_HOST_ENV_VAR2' successfully passed."])
353
354        # Now test that we can unset variables from the inherited environment.
355        self.runCmd('settings set target.inherit-env true')
356        self.runCmd('settings set target.unset-env-vars MY_HOST_ENV_VAR1')
357        self.runCmd("process launch --working-dir '{0}'".format(self.get_process_working_directory()),
358                RUN_SUCCEEDED)
359
360        # Read the output file produced by running the program.
361        output = lldbutil.read_file_from_process_wd(self, "output1.txt")
362
363        self.expect(
364            'target show-launch-environment',
365            'MY_HOST_ENV_VAR1 is unset, it shouldn\'t be in `target show-launch-environment`',
366            matching=False,
367            substrs = ['MY_HOST_ENV_VAR1=VAR1'])
368        self.expect(
369            'target show-launch-environment',
370            'MY_HOST_ENV_VAR2 shouldn be in `target show-launch-environment`',
371            substrs = ['MY_HOST_ENV_VAR2=VAR2'])
372
373        self.expect(
374            output,
375            exe=False,
376            matching=False,
377            substrs=[
378                "The host environment variable 'MY_HOST_ENV_VAR1' successfully passed."])
379        self.expect(
380            output,
381            exe=False,
382            substrs=[
383                "The host environment variable 'MY_HOST_ENV_VAR2' successfully passed."])
384
385    @skipIfDarwinEmbedded   # <rdar://problem/34446098> debugserver on ios etc can't write files
386    def test_set_error_output_path(self):
387        """Test that setting target.error/output-path for the launched process works."""
388        self.build()
389
390        exe = self.getBuildArtifact("a.out")
391        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
392
393        # Set the error-path and output-path and verify both are set.
394        self.runCmd("settings set target.error-path '{0}'".format(
395            lldbutil.append_to_process_working_directory(self, "stderr.txt")))
396        self.runCmd("settings set target.output-path '{0}".format(
397            lldbutil.append_to_process_working_directory(self, "stdout.txt")))
398        # And add hooks to restore the original settings during tearDown().
399        self.addTearDownHook(
400            lambda: self.runCmd("settings clear target.output-path"))
401        self.addTearDownHook(
402            lambda: self.runCmd("settings clear target.error-path"))
403
404        self.expect("settings show target.error-path",
405                    SETTING_MSG("target.error-path"),
406                    substrs=['target.error-path (file)', 'stderr.txt"'])
407
408        self.expect("settings show target.output-path",
409                    SETTING_MSG("target.output-path"),
410                    substrs=['target.output-path (file)', 'stdout.txt"'])
411
412        self.runCmd("process launch --working-dir '{0}'".format(self.get_process_working_directory()),
413                RUN_SUCCEEDED)
414
415        output = lldbutil.read_file_from_process_wd(self, "stderr.txt")
416        message = "This message should go to standard error."
417        if lldbplatformutil.hasChattyStderr(self):
418            self.expect(output, exe=False, substrs=[message])
419        else:
420            self.expect(output, exe=False, startstr=message)
421
422        output = lldbutil.read_file_from_process_wd(self, "stdout.txt")
423        self.expect(output, exe=False,
424                    startstr="This message should go to standard out.")
425
426    def test_print_dictionary_setting(self):
427        self.runCmd("settings clear target.env-vars")
428        self.runCmd("settings set target.env-vars [\"MY_VAR\"]=some-value")
429        self.expect("settings show target.env-vars",
430                    substrs=["MY_VAR=some-value"])
431        self.runCmd("settings clear target.env-vars")
432
433    def test_print_array_setting(self):
434        self.runCmd("settings clear target.run-args")
435        self.runCmd("settings set target.run-args gobbledy-gook")
436        self.expect("settings show target.run-args",
437                    substrs=['[0]: "gobbledy-gook"'])
438        self.runCmd("settings clear target.run-args")
439
440    def test_settings_with_quotes(self):
441        self.runCmd("settings clear target.run-args")
442        self.runCmd("settings set target.run-args a b c")
443        self.expect("settings show target.run-args",
444                    substrs=['[0]: "a"',
445                             '[1]: "b"',
446                             '[2]: "c"'])
447        self.runCmd("settings set target.run-args 'a b c'")
448        self.expect("settings show target.run-args",
449                    substrs=['[0]: "a b c"'])
450        self.runCmd("settings clear target.run-args")
451        self.runCmd("settings clear target.env-vars")
452        self.runCmd(
453            'settings set target.env-vars ["MY_FILE"]="this is a file name with spaces.txt"')
454        self.expect("settings show target.env-vars",
455                    substrs=['MY_FILE=this is a file name with spaces.txt'])
456        self.runCmd("settings clear target.env-vars")
457        # Test and make sure that setting "format-string" settings obeys quotes
458        # if they are provided
459        self.runCmd("settings set thread-format    'abc def'   ")
460        self.expect("settings show thread-format",
461                    'thread-format (format-string) = "abc def"')
462        self.runCmd('settings set thread-format    "abc def"   ')
463        self.expect("settings show thread-format",
464                    'thread-format (format-string) = "abc def"')
465        # Make sure when no quotes are provided that we maintain any trailing
466        # spaces
467        self.runCmd('settings set thread-format abc def   ')
468        self.expect("settings show thread-format",
469                    'thread-format (format-string) = "abc def   "')
470        self.runCmd('settings clear thread-format')
471
472    def test_settings_with_trailing_whitespace(self):
473
474        # boolean
475        # Set to known value
476        self.runCmd("settings set target.skip-prologue true")
477        # Set to new value with trailing whitespace
478        self.runCmd("settings set target.skip-prologue false ")
479        # Make sure the setting was correctly set to "false"
480        self.expect(
481            "settings show target.skip-prologue",
482            SETTING_MSG("target.skip-prologue"),
483            startstr="target.skip-prologue (boolean) = false")
484        self.runCmd("settings clear target.skip-prologue", check=False)
485        # integer
486        self.runCmd("settings set term-width 70")      # Set to known value
487        # Set to new value with trailing whitespaces
488        self.runCmd("settings set term-width 60 \t")
489        self.expect("settings show term-width", SETTING_MSG("term-width"),
490                    startstr="term-width (int) = 60")
491        self.runCmd("settings clear term-width", check=False)
492        # string
493        self.runCmd("settings set target.arg0 abc")    # Set to known value
494        # Set to new value with trailing whitespaces
495        self.runCmd("settings set target.arg0 cde\t ")
496        self.expect("settings show target.arg0", SETTING_MSG("target.arg0"),
497                    startstr='target.arg0 (string) = "cde"')
498        self.runCmd("settings clear target.arg0", check=False)
499        # file
500        path1 = self.getBuildArtifact("path1.txt")
501        path2 = self.getBuildArtifact("path2.txt")
502        self.runCmd(
503            "settings set target.output-path %s" %
504            path1)   # Set to known value
505        self.expect(
506            "settings show target.output-path",
507            SETTING_MSG("target.output-path"),
508            startstr='target.output-path (file) = ',
509            substrs=[path1])
510        self.runCmd("settings set target.output-path %s " %
511                    path2)  # Set to new value with trailing whitespaces
512        self.expect(
513            "settings show target.output-path",
514            SETTING_MSG("target.output-path"),
515            startstr='target.output-path (file) = ',
516            substrs=[path2])
517        self.runCmd("settings clear target.output-path", check=False)
518        # enum
519        # Set to known value
520        self.runCmd("settings set stop-disassembly-display never")
521        # Set to new value with trailing whitespaces
522        self.runCmd("settings set stop-disassembly-display always ")
523        self.expect(
524            "settings show stop-disassembly-display",
525            SETTING_MSG("stop-disassembly-display"),
526            startstr='stop-disassembly-display (enum) = always')
527        self.runCmd("settings clear stop-disassembly-display", check=False)
528        # language
529        # Set to known value
530        self.runCmd("settings set target.language c89")
531        # Set to new value with trailing whitespace
532        self.runCmd("settings set target.language c11 ")
533        self.expect(
534            "settings show target.language",
535            SETTING_MSG("target.language"),
536            startstr="target.language (language) = c11")
537        self.runCmd("settings clear target.language", check=False)
538        # arguments
539        self.runCmd("settings set target.run-args 1 2 3")  # Set to known value
540        # Set to new value with trailing whitespaces
541        self.runCmd("settings set target.run-args 3 4 5 ")
542        self.expect(
543            "settings show target.run-args",
544            SETTING_MSG("target.run-args"),
545            substrs=[
546                'target.run-args (arguments) =',
547                '[0]: "3"',
548                '[1]: "4"',
549                '[2]: "5"'])
550        self.runCmd("settings set target.run-args 1 2 3")  # Set to known value
551        # Set to new value with trailing whitespaces
552        self.runCmd("settings set target.run-args 3 \  \ ")
553        self.expect(
554            "settings show target.run-args",
555            SETTING_MSG("target.run-args"),
556            substrs=[
557                'target.run-args (arguments) =',
558                '[0]: "3"',
559                '[1]: " "',
560                '[2]: " "'])
561        self.runCmd("settings clear target.run-args", check=False)
562        # dictionaries
563        self.runCmd("settings clear target.env-vars")  # Set to known value
564        # Set to new value with trailing whitespaces
565        self.runCmd("settings set target.env-vars A=B C=D\t ")
566        self.expect(
567            "settings show target.env-vars",
568            SETTING_MSG("target.env-vars"),
569            substrs=[
570                'target.env-vars (dictionary of strings) =',
571                'A=B',
572                'C=D'])
573        self.runCmd("settings clear target.env-vars", check=False)
574        # regex
575        # Set to known value
576        self.runCmd("settings clear target.process.thread.step-avoid-regexp")
577        # Set to new value with trailing whitespaces
578        self.runCmd(
579            "settings set target.process.thread.step-avoid-regexp foo\\ ")
580        self.expect(
581            "settings show target.process.thread.step-avoid-regexp",
582            SETTING_MSG("target.process.thread.step-avoid-regexp"),
583            substrs=['target.process.thread.step-avoid-regexp (regex) = foo\\ '])
584        self.runCmd(
585            "settings clear target.process.thread.step-avoid-regexp",
586            check=False)
587        # format-string
588        self.runCmd("settings clear disassembly-format")  # Set to known value
589        # Set to new value with trailing whitespaces
590        self.runCmd("settings set disassembly-format foo ")
591        self.expect("settings show disassembly-format",
592                    SETTING_MSG("disassembly-format"),
593                    substrs=['disassembly-format (format-string) = "foo "'])
594        self.runCmd("settings clear disassembly-format", check=False)
595
596    def test_settings_list(self):
597        # List settings (and optionally test the filter to only show 'target' settings).
598        self.expect("settings list target", substrs=["arg0", "detach-on-error", "language"])
599        self.expect("settings list target", matching=False, substrs=["packet-timeout"])
600        self.expect("settings list", substrs=["language", "arg0", "detach-on-error", "packet-timeout"])
601
602    def test_settings_remove_single(self):
603        # Set some environment variables and use 'remove' to delete them.
604        self.runCmd("settings set target.env-vars a=b c=d")
605        self.expect("settings show target.env-vars", substrs=["a=b", "c=d"])
606        self.runCmd("settings remove target.env-vars a")
607        self.expect("settings show target.env-vars", matching=False, substrs=["a=b"])
608        self.expect("settings show target.env-vars", substrs=["c=d"])
609        self.runCmd("settings remove target.env-vars c")
610        self.expect("settings show target.env-vars", matching=False, substrs=["a=b", "c=d"])
611
612    def test_settings_remove_multiple(self):
613        self.runCmd("settings set target.env-vars a=b c=d e=f")
614        self.expect("settings show target.env-vars", substrs=["a=b", "c=d", "e=f"])
615        self.runCmd("settings remove target.env-vars a e")
616        self.expect("settings show target.env-vars", matching=False, substrs=["a=b", "e=f"])
617        self.expect("settings show target.env-vars", substrs=["c=d"])
618
619    def test_settings_remove_nonexistent_value(self):
620        self.expect("settings remove target.env-vars doesntexist", error=True,
621                    substrs=["no value found named 'doesntexist'"])
622
623    def test_settings_remove_nonexistent_settings(self):
624        self.expect("settings remove doesntexist alsodoesntexist", error=True,
625                    substrs=["error: invalid value path 'doesntexist'"])
626
627    def test_settings_remove_missing_arg(self):
628        self.expect("settings remove", error=True,
629                    substrs=["'settings remove' takes an array or dictionary item, or"])
630
631    def test_settings_remove_empty_arg(self):
632        self.expect("settings remove ''", error=True,
633                    substrs=["'settings remove' command requires a valid variable name"])
634
635    def test_settings_clear_all(self):
636        # Change a dictionary.
637        self.runCmd("settings set target.env-vars a=1 b=2 c=3")
638        # Change an array.
639        self.runCmd("settings set target.run-args a1 b2 c3")
640        # Change a single boolean value.
641        self.runCmd("settings set auto-confirm true")
642        # Change a single integer value.
643        self.runCmd("settings set tab-size 2")
644
645        # Clear everything.
646        self.runCmd("settings clear --all")
647
648        # Check that settings have their default values after clearing.
649        self.expect("settings show target.env-vars", patterns=['^target.env-vars \(dictionary of strings\) =\s*$'])
650        self.expect("settings show target.run-args", patterns=['^target.run-args \(arguments\) =\s*$'])
651        self.expect("settings show auto-confirm", substrs=["false"])
652        self.expect("settings show tab-size", substrs=["4"])
653
654        # Check that the command fails if we combine '--all' option with any arguments.
655        self.expect(
656            "settings clear --all auto-confirm",
657            COMMAND_FAILED_AS_EXPECTED,
658            error=True,
659            substrs=["'settings clear --all' doesn't take any arguments"])
660
661    def test_all_settings_exist(self):
662        self.expect("settings show",
663                    substrs=["auto-confirm",
664                             "frame-format",
665                             "notify-void",
666                             "prompt",
667                             "script-lang",
668                             "stop-disassembly-count",
669                             "stop-disassembly-display",
670                             "stop-line-count-after",
671                             "stop-line-count-before",
672                             "stop-show-column",
673                             "term-width",
674                             "thread-format",
675                             "use-external-editor",
676                             "target.breakpoints-use-platform-avoid-list",
677                             "target.default-arch",
678                             "target.disable-aslr",
679                             "target.disable-stdio",
680                             "target.x86-disassembly-flavor",
681                             "target.enable-synthetic-value",
682                             "target.env-vars",
683                             "target.error-path",
684                             "target.exec-search-paths",
685                             "target.expr-prefix",
686                             "target.hex-immediate-style",
687                             "target.inherit-env",
688                             "target.input-path",
689                             "target.language",
690                             "target.max-children-count",
691                             "target.max-string-summary-length",
692                             "target.move-to-nearest-code",
693                             "target.output-path",
694                             "target.prefer-dynamic-value",
695                             "target.run-args",
696                             "target.skip-prologue",
697                             "target.source-map",
698                             "target.use-hex-immediates",
699                             "target.process.disable-memory-cache",
700                             "target.process.extra-startup-command",
701                             "target.process.thread.trace-thread",
702                             "target.process.thread.step-avoid-regexp",
703                             ])
704
705    # settings under an ".experimental" domain should have two properties:
706    #   1. If the name does not exist with "experimental" in the name path,
707    #      the name lookup should try to find it without "experimental".  So
708    #      a previously-experimental setting that has been promoted to a
709    #      "real" setting will still be set by the original name.
710    #   2. Changing a setting with .experimental., name, where the setting
711    #      does not exist either with ".experimental." or without, should
712    #      not generate an error.  So if an experimental setting is removed,
713    #      people who may have that in their ~/.lldbinit files should not see
714    #      any errors.
715    def test_experimental_settings(self):
716        cmdinterp = self.dbg.GetCommandInterpreter()
717        result = lldb.SBCommandReturnObject()
718
719        # Set target.arg0 to a known value, check that we can retrieve it via
720        # the actual name and via .experimental.
721        self.expect('settings set target.arg0 first-value')
722        self.expect('settings show target.arg0', substrs=['first-value'])
723        self.expect('settings show target.experimental.arg0', substrs=['first-value'], error=False)
724
725        # Set target.arg0 to a new value via a target.experimental.arg0 name,
726        # verify that we can read it back via both .experimental., and not.
727        self.expect('settings set target.experimental.arg0 second-value', error=False)
728        self.expect('settings show target.arg0', substrs=['second-value'])
729        self.expect('settings show target.experimental.arg0', substrs=['second-value'], error=False)
730
731        # showing & setting an undefined .experimental. setting should generate no errors.
732        self.expect('settings show target.experimental.setting-which-does-not-exist', patterns=['^\s$'], error=False)
733        self.expect('settings set target.experimental.setting-which-does-not-exist true', error=False)
734
735        # A domain component before .experimental. which does not exist should give an error
736        # But the code does not yet do that.
737        # self.expect('settings set target.setting-which-does-not-exist.experimental.arg0 true', error=True)
738
739        # finally, confirm that trying to set a setting that does not exist still fails.
740        # (SHOWING a setting that does not exist does not currently yield an error.)
741        self.expect('settings set target.setting-which-does-not-exist true', error=True)
742