1"""
2Test lldb command aliases.
3"""
4
5import os, time
6import unittest2
7import lldb
8from lldbtest import *
9import lldbutil
10
11class AliasTestCase(TestBase):
12
13    mydir = os.path.join("functionalities", "alias")
14
15    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
16    @dsym_test
17    def test_with_dsym (self):
18        self.buildDsym ()
19        self.alias_tests ()
20
21    @expectedFailureFreeBSD('llvm.org/pr16697') # Expression fails with 'there is no JIT compiled function'
22    @dwarf_test
23    def test_with_dwarf (self):
24        self.buildDwarf ()
25        self.alias_tests ()
26
27    def alias_tests (self):
28        exe = os.path.join (os.getcwd(), "a.out")
29        self.expect("file " + exe,
30                    patterns = [ "Current executable set to .*a.out" ])
31
32
33        def cleanup():
34            self.runCmd('command unalias hello', check=False)
35            self.runCmd('command unalias python', check=False)
36            self.runCmd('command unalias pp', check=False)
37            self.runCmd('command unalias alias', check=False)
38            self.runCmd('command unalias unalias', check=False)
39            self.runCmd('command unalias myrun', check=False)
40            self.runCmd('command unalias bp', check=False)
41            self.runCmd('command unalias bpa', check=False)
42            self.runCmd('command unalias bpi', check=False)
43            self.runCmd('command unalias bfl', check=False)
44            self.runCmd('command unalias exprf', check=False)
45            self.runCmd('command unalias exprf2', check=False)
46
47        # Execute the cleanup function during test case tear down.
48        self.addTearDownHook(cleanup)
49
50        self.runCmd (r'''command alias hello expr (int) printf ("\n\nHello, anybody!\n\n")''')
51
52        self.runCmd ("command alias python script")
53
54        # We don't want to display the stdout if not in TraceOn() mode.
55        if not self.TraceOn():
56            self.HideStdout()
57
58        self.runCmd (r'''python print "\n\n\nWhoopee!\n\n\n"''')
59#        self.expect (r'''python print "\n\n\nWhoopee!\n\n\n"''',
60#                     substrs = [ "Whoopee!" ])
61
62        self.runCmd (r'''python print "\n\t\x68\x65\x6c\x6c\x6f\n"''')
63#        self.expect (r'''python print "\n\t\x68\x65\x6c\x6c\x6f\n"''',
64#                     substrs = [ "hello" ])
65
66        self.runCmd (r'''command alias pp python print "\n\t\x68\x65\x6c\x6c\x6f\n"''')
67        self.runCmd ("pp")
68#        self.expect ("pp",
69#                     substrs = [ "hello" ])
70
71
72        self.runCmd ("command alias alias command alias")
73        self.runCmd ("command alias unalias command unalias")
74
75        self.runCmd ("alias myrun process launch -t %1 --")
76        self.runCmd ("alias bp breakpoint")
77
78        self.expect ("alias bpa bp add",
79                     COMMAND_FAILED_AS_EXPECTED, error = True,
80                     substrs = [ "'add' is not a valid sub-command of 'bp'" ])
81
82        self.runCmd ("alias bpa bp command add")
83        self.runCmd ("alias bpi bp list")
84
85        break_results = lldbutil.run_break_set_command (self, "bp set -n foo")
86        lldbutil.check_breakpoint_result (self, break_results, num_locations=1, symbol_name='foo', symbol_match_exact=False)
87
88        break_results = lldbutil.run_break_set_command (self, "bp set -n sum")
89        lldbutil.check_breakpoint_result (self, break_results, num_locations=1, symbol_name='sum', symbol_match_exact=False)
90
91        self.runCmd ("alias bfl bp set -f %1 -l %2")
92
93        break_results = lldbutil.run_break_set_command (self, "bfl main.cpp 32")
94        lldbutil.check_breakpoint_result (self, break_results, num_locations=1, file_name='main.cpp', line_number=32)
95
96        self.expect ("bpi",
97                     startstr = "Current breakpoints:",
98                     substrs = [ "1: name = 'foo', locations = 1",
99                                 "2: name = 'sum', locations = 1",
100                                 "3: file = 'main.cpp', line = 32, locations = 1" ])
101
102        self.runCmd ("bpa -s python 1 -o 'print frame; print bp_loc'")
103        self.runCmd ("bpa -s command 2 -o 'frame variable b'")
104        self.expect ("bpi -f",
105                     substrs = [ "Current breakpoints:",
106                                 "1: name = 'foo', locations = 1",
107                                 "print frame; print bp_loc",
108                                 "2: name = 'sum', locations = 1",
109                                 "frame variable b" ])
110
111
112        self.expect ("help run",
113                     substrs = [ "'run' is an abbreviation for 'process launch -c /bin/bash --'" ])
114
115        self.expect ("help -a run",
116                     substrs = [ "'run' is an abbreviation for 'process launch -c /bin/bash --'" ])
117
118        self.expect ("help -a",
119                     substrs = [ 'run', 'process launch -c /bin/bash' ])
120
121        self.expect ("help", matching=False,
122                     substrs = [ "'run'", 'process launch -c /bin/bash' ])
123
124        self.expect ("run",
125                     patterns = [ "Process .* launched: .*a.out" ])
126
127        self.expect (r'''expression (int) printf("\x68\x65\x6c\x6c\x6f\n")''',
128                     substrs = [ "(int) $",
129                                 "= 6" ])
130
131        self.expect ("hello",
132                     substrs = [ "(int) $",
133                                 "= 19" ])
134
135        self.expect ("expr -f x -- 68",
136                     substrs = [ "(int) $",
137                                 "= 0x00000044" ])
138
139        self.runCmd ("alias exprf expr -f %1")
140        self.runCmd ("alias exprf2 expr --raw -f %1 --")
141        self.expect ("exprf x -- 1234",
142                     substrs = [ "(int) $",
143                                 "= 0x000004d2" ])
144
145        self.expect ('exprf2 c "Hi there!"',
146                     substrs = [ "[0] = 'H'",
147                                 "[1] = 'i'",
148                                 "[2] = ' '",
149                                 "[3] = 't'",
150                                 "[4] = 'h'",
151                                 "[5] = 'e'",
152                                 "[6] = 'r'",
153                                 "[7] = 'e'",
154                                 "[8] = '!'",
155                                 "[9] = '\\0'" ])
156
157
158        self.expect ("exprf x 1234",
159                     COMMAND_FAILED_AS_EXPECTED, error = True,
160                     substrs = [ "use of undeclared identifier 'f'",
161                                 "1 errors parsing expression" ])
162
163if __name__ == '__main__':
164    import atexit
165    lldb.SBDebugger.Initialize()
166    atexit.register(lambda: lldb.SBDebugger.Terminate())
167    unittest2.main()
168
169