aboutsummaryrefslogtreecommitdiff
path: root/test/settings/TestSettings.py
blob: 09a68608b775110bdd536865df7e900f258aac42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
"""
Test lldb settings command.
"""

import os, time
import unittest2
import lldb
from lldbtest import *

class SettingsCommandTestCase(TestBase):

    mydir = "settings"

    @classmethod
    def classCleanup(cls):
        """Cleanup the test byproducts."""
        cls.RemoveTempFile("output1.txt")
        cls.RemoveTempFile("output2.txt")
        cls.RemoveTempFile("stderr.txt")
        cls.RemoveTempFile("stdout.txt")

    def test_apropos_should_also_search_settings_description(self):
        """Test that 'apropos' command should also search descriptions for the settings variables."""

        self.expect("apropos 'environment variable'",
            substrs = ["target.env-vars",
                       "environment variables",
                       "executable's environment"])

    def test_append_target_env_vars(self):
        """Test that 'append target.run-args' works."""
        # Append the env-vars.
        self.runCmd('settings append target.env-vars MY_ENV_VAR=YES')
        # And add hooks to restore the settings during tearDown().
        self.addTearDownHook(
            lambda: self.runCmd("settings clear target.env-vars"))

        # Check it immediately!
        self.expect('settings show target.env-vars',
            substrs = ['MY_ENV_VAR=YES'])

    def test_insert_before_and_after_target_run_args(self):
        """Test that 'insert-before/after target.run-args' works."""
        # Set the run-args first.
        self.runCmd('settings set target.run-args a b c')
        # And add hooks to restore the settings during tearDown().
        self.addTearDownHook(
            lambda: self.runCmd("settings clear target.run-args"))

        # Now insert-before the index-0 element with '__a__'.
        self.runCmd('settings insert-before target.run-args 0 __a__')
        # And insert-after the index-1 element with '__A__'.
        self.runCmd('settings insert-after target.run-args 1 __A__')
        # Check it immediately!
        self.expect('settings show target.run-args',
            substrs = ['target.run-args',
                       '[0]: "__a__"',
                       '[1]: "a"',
                       '[2]: "__A__"',
                       '[3]: "b"',
                       '[4]: "c"'])

    def test_replace_target_run_args(self):
        """Test that 'replace target.run-args' works."""
        # Set the run-args and then replace the index-0 element.
        self.runCmd('settings set target.run-args a b c')
        # And add hooks to restore the settings during tearDown().
        self.addTearDownHook(
            lambda: self.runCmd("settings clear target.run-args"))

        # Now replace the index-0 element with 'A', instead.
        self.runCmd('settings replace target.run-args 0 A')
        # Check it immediately!
        self.expect('settings show target.run-args',
            substrs = ['target.run-args (arguments) =',
                       '[0]: "A"',
                       '[1]: "b"',
                       '[2]: "c"'])

    def test_set_prompt(self):
        """Test that 'set prompt' actually changes the prompt."""

        # Set prompt to 'lldb2'.
        self.runCmd("settings set prompt 'lldb2 '")

        # Immediately test the setting.
        self.expect("settings show prompt", SETTING_MSG("prompt"),
            startstr = 'prompt (string) = "lldb2 "')

        # The overall display should also reflect the new setting.
        self.expect("settings show", SETTING_MSG("prompt"),
            substrs = ['prompt (string) = "lldb2 "'])

        # Use '-r' option to reset to the original default prompt.
        self.runCmd("settings clear prompt")

    def test_set_term_width(self):
        """Test that 'set term-width' actually changes the term-width."""

        self.runCmd("settings set term-width 70")

        # Immediately test the setting.
        self.expect("settings show term-width", SETTING_MSG("term-width"),
            startstr = "term-width (int) = 70")

        # The overall display should also reflect the new setting.
        self.expect("settings show", SETTING_MSG("term-width"),
            substrs = ["term-width (int) = 70"])

    #rdar://problem/10712130
    def test_set_frame_format(self):
        """Test that 'set frame-format' with a backtick char in the format string works as well as fullpath."""
        self.buildDefault()

        exe = os.path.join(os.getcwd(), "a.out")
        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)

        def cleanup():
            format_string = "frame #${frame.index}: ${frame.pc}{ ${module.file.basename}{`${function.name}${function.pc-offset}}}{ at ${line.file.basename}:${line.number}}\n"
            self.runCmd("settings set frame-format %s" % format_string, check=False)

        # Execute the cleanup function during test case tear down.
        self.addTearDownHook(cleanup)

        format_string = "frame #${frame.index}: ${frame.pc}{ ${module.file.basename}`${function.name-with-args}{${function.pc-offset}}}{ at ${line.file.fullpath}:${line.number}}\n"
        self.runCmd("settings set frame-format %s" % format_string)

        # Immediately test the setting.
        self.expect("settings show frame-format", SETTING_MSG("frame-format"),
            substrs = [format_string])

        self.runCmd("breakpoint set -n main")
        self.runCmd("run")
        self.expect("thread backtrace",
            substrs = ["`main", os.getcwd()])

    def test_set_auto_confirm(self):
        """Test that after 'set auto-confirm true', manual confirmation should not kick in."""
        self.buildDefault()

        exe = os.path.join(os.getcwd(), "a.out")
        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)

        self.runCmd("settings set auto-confirm true")

        # Immediately test the setting.
        self.expect("settings show auto-confirm", SETTING_MSG("auto-confirm"),
            startstr = "auto-confirm (boolean) = true")

        # Now 'breakpoint delete' should just work fine without confirmation
        # prompt from the command interpreter.
        self.runCmd("breakpoint set -n main")
        self.expect("breakpoint delete",
            startstr = "All breakpoints removed")

        # Restore the original setting of auto-confirm.
        self.runCmd("settings clear auto-confirm")
        self.expect("settings show auto-confirm", SETTING_MSG("auto-confirm"),
            startstr = "auto-confirm (boolean) = false")

    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
    @dsym_test
    def test_run_args_and_env_vars_with_dsym(self):
        """Test that run-args and env-vars are passed to the launched process."""
        self.buildDsym()
        self.pass_run_args_and_env_vars()

    @dwarf_test
    def test_run_args_and_env_vars_with_dwarf(self):
        """Test that run-args and env-vars are passed to the launched process."""
        self.buildDwarf()
        self.pass_run_args_and_env_vars()

    def pass_run_args_and_env_vars(self):
        """Test that run-args and env-vars are passed to the launched process."""
        exe = os.path.join(os.getcwd(), "a.out")
        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)

        # Set the run-args and the env-vars.
        # And add hooks to restore the settings during tearDown().
        self.runCmd('settings set target.run-args A B C')
        self.addTearDownHook(
            lambda: self.runCmd("settings clear target.run-args"))
        self.runCmd('settings set target.env-vars ["MY_ENV_VAR"]=YES')
        self.addTearDownHook(
            lambda: self.runCmd("settings clear target.env-vars"))

        self.runCmd("run", RUN_SUCCEEDED)

        # Read the output file produced by running the program.
        with open('output2.txt', 'r') as f:
            output = f.read()

        self.expect(output, exe=False,
            substrs = ["argv[1] matches",
                       "argv[2] matches",
                       "argv[3] matches",
                       "Environment variable 'MY_ENV_VAR' successfully passed."])

    def test_pass_host_env_vars(self):
        """Test that the host env vars are passed to the launched process."""
        self.buildDefault()

        exe = os.path.join(os.getcwd(), "a.out")
        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)

        # By default, inherit-env is 'true'.
        self.expect('settings show target.inherit-env', "Default inherit-env is 'true'",
            startstr = "target.inherit-env (boolean) = true")

        # Set some host environment variables now.
        os.environ["MY_HOST_ENV_VAR1"] = "VAR1"
        os.environ["MY_HOST_ENV_VAR2"] = "VAR2"

        # This is the function to unset the two env variables set above.
        def unset_env_variables():
            os.environ.pop("MY_HOST_ENV_VAR1")
            os.environ.pop("MY_HOST_ENV_VAR2")

        self.addTearDownHook(unset_env_variables)
        self.runCmd("run", RUN_SUCCEEDED)

        # Read the output file produced by running the program.
        with open('output1.txt', 'r') as f:
            output = f.read()

        self.expect(output, exe=False,
            substrs = ["The host environment variable 'MY_HOST_ENV_VAR1' successfully passed.",
                       "The host environment variable 'MY_HOST_ENV_VAR2' successfully passed."])

    def test_set_error_output_path(self):
        """Test that setting target.error/output-path for the launched process works."""
        self.buildDefault()

        exe = os.path.join(os.getcwd(), "a.out")
        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)

        # Set the error-path and output-path and verify both are set.
        self.runCmd("settings set target.error-path stderr.txt")
        self.runCmd("settings set target.output-path stdout.txt")
        # And add hooks to restore the original settings during tearDown().
        self.addTearDownHook(
            lambda: self.runCmd("settings clear target.output-path"))
        self.addTearDownHook(
            lambda: self.runCmd("settings clear target.error-path"))

        self.expect("settings show target.error-path",
                    SETTING_MSG("target.error-path"),
            startstr = 'target.error-path (file) = "stderr.txt"')

        self.expect("settings show target.output-path",
                    SETTING_MSG("target.output-path"),
            startstr = 'target.output-path (file) = "stdout.txt"')

        self.runCmd("run", RUN_SUCCEEDED)

        # The 'stderr.txt' file should now exist.
        self.assertTrue(os.path.isfile("stderr.txt"),
                        "'stderr.txt' exists due to target.error-path.")

        # Read the output file produced by running the program.
        with open('stderr.txt', 'r') as f:
            output = f.read()

        self.expect(output, exe=False,
            startstr = "This message should go to standard error.")

        # The 'stdout.txt' file should now exist.
        self.assertTrue(os.path.isfile("stdout.txt"),
                        "'stdout.txt' exists due to target.output-path.")

        # Read the output file produced by running the program.
        with open('stdout.txt', 'r') as f:
            output = f.read()

        self.expect(output, exe=False,
            startstr = "This message should go to standard out.")

    def test_print_dictionary_setting(self):
        self.runCmd ("settings clear target.env-vars")
        self.runCmd ("settings set target.env-vars [\"MY_VAR\"]=some-value")
        self.expect ("settings show target.env-vars",
                     substrs = [ "MY_VAR=some-value" ])
        self.runCmd ("settings clear target.env-vars")

    def test_print_array_setting(self):
        self.runCmd ("settings clear target.run-args")
        self.runCmd ("settings set target.run-args gobbledy-gook")
        self.expect ("settings show target.run-args",
                     substrs = [ '[0]: "gobbledy-gook"' ])
        self.runCmd ("settings clear target.run-args")

    def test_settings_with_quotes (self):
        self.runCmd ("settings clear target.run-args")
        self.runCmd ("settings set target.run-args a b c")
        self.expect ("settings show target.run-args",
                     substrs = [ '[0]: "a"',
                                 '[1]: "b"',
                                 '[2]: "c"' ])
        self.runCmd ("settings set target.run-args 'a b c'")
        self.expect ("settings show target.run-args",
                     substrs = [ '[0]: "a b c"' ])
        self.runCmd ("settings clear target.run-args")
        self.runCmd ("settings clear target.env-vars")
        self.runCmd ('settings set target.env-vars ["MY_FILE"]="this is a file name with spaces.txt"')
        self.expect ("settings show target.env-vars",
                     substrs = [ 'MY_FILE=this is a file name with spaces.txt' ])
        self.runCmd ("settings clear target.env-vars")

    def test_settings_with_trailing_whitespace (self):
        
        # boolean
        self.runCmd ("settings set target.skip-prologue true")      # Set to known value
        self.runCmd ("settings set target.skip-prologue false ")    # Set to new value with trailing whitespace
        # Make sure the setting was correctly set to "false"
        self.expect ("settings show target.skip-prologue", SETTING_MSG("target.skip-prologue"),
            startstr = "target.skip-prologue (boolean) = false")
        self.runCmd("settings clear target.skip-prologue", check=False)
        # integer
        self.runCmd ("settings set term-width 70")      # Set to known value
        self.runCmd ("settings set term-width 60 \t")   # Set to new value with trailing whitespaces
        self.expect ("settings show term-width", SETTING_MSG("term-width"),
            startstr = "term-width (int) = 60")
        self.runCmd("settings clear term-width", check=False)
        # string
        self.runCmd ("settings set target.arg0 abc")    # Set to known value
        self.runCmd ("settings set target.arg0 cde\t ") # Set to new value with trailing whitespaces
        self.expect ("settings show target.arg0", SETTING_MSG("target.arg0"),
            startstr = 'target.arg0 (string) = "cde"')
        self.runCmd("settings clear target.arg0", check=False)
        # file
        self.runCmd ("settings set target.output-path /bin/ls")   # Set to known value
        self.runCmd ("settings set target.output-path /bin/cat ") # Set to new value with trailing whitespaces
        self.expect ("settings show target.output-path", SETTING_MSG("target.output-path"),
            startstr = 'target.output-path (file) = ', substrs=['/bin/cat"'])
        self.runCmd("settings clear target.output-path", check=False)
        # enum
        self.runCmd ("settings set stop-disassembly-display never")   # Set to known value
        self.runCmd ("settings set stop-disassembly-display always ") # Set to new value with trailing whitespaces
        self.expect ("settings show stop-disassembly-display", SETTING_MSG("stop-disassembly-display"),
            startstr = 'stop-disassembly-display (enum) = always')
        self.runCmd("settings clear stop-disassembly-display", check=False)        
        # arguments
        self.runCmd ("settings set target.run-args 1 2 3")  # Set to known value
        self.runCmd ("settings set target.run-args 3 4 5 ") # Set to new value with trailing whitespaces
        self.expect ("settings show target.run-args", SETTING_MSG("target.run-args"),
            substrs = [ 'target.run-args (arguments) =', 
                        '[0]: "3"', 
                        '[1]: "4"',
                        '[2]: "5"' ])
        self.runCmd("settings clear target.run-args", check=False)        
        # dictionaries
        self.runCmd ("settings clear target.env-vars")  # Set to known value
        self.runCmd ("settings set target.env-vars A=B C=D\t ") # Set to new value with trailing whitespaces
        self.expect ("settings show target.env-vars", SETTING_MSG("target.env-vars"),
            substrs = [ 'target.env-vars (dictionary of strings) =', 
                        'A=B', 
                        'C=D'])
        self.runCmd("settings clear target.env-vars", check=False)        
        
    def test_all_settings_exist (self):
        self.expect ("settings show",
                     substrs = [ "auto-confirm",
                                 "frame-format",
                                 "notify-void",
                                 "prompt",
                                 "script-lang",
                                 "stop-disassembly-count",
                                 "stop-disassembly-display",
                                 "stop-line-count-after",
                                 "stop-line-count-before",
                                 "term-width",
                                 "thread-format",
                                 "use-external-editor",
                                 "target.default-arch",
                                 "target.expr-prefix",
                                 "target.prefer-dynamic-value",
                                 "target.enable-synthetic-value",
                                 "target.skip-prologue",
                                 "target.source-map",
                                 "target.exec-search-paths",
                                 "target.max-children-count",
                                 "target.max-string-summary-length",
                                 "target.breakpoints-use-platform-avoid-list",
                                 "target.run-args",
                                 "target.env-vars",
                                 "target.inherit-env",
                                 "target.input-path",
                                 "target.output-path",
                                 "target.error-path",
                                 "target.disable-aslr",
                                 "target.disable-stdio",
                                 "target.process.disable-memory-cache",
                                 "target.process.extra-startup-command",
                                 "target.process.thread.step-avoid-regexp",
                                 "target.process.thread.trace-thread"])
                                 
        

if __name__ == '__main__':
    import atexit
    lldb.SBDebugger.Initialize()
    atexit.register(lambda: lldb.SBDebugger.Terminate())
    unittest2.main()