xref: /llvm-project/lldb/test/API/commands/help/TestHelp.py (revision 563ef306017a47d387f1c36dd562b172c1ad0626)
1"""
2Test some lldb help commands.
3
4See also CommandInterpreter::OutputFormattedHelpText().
5"""
6
7
8import os
9import lldb
10from lldbsuite.test.decorators import *
11from lldbsuite.test.lldbtest import *
12from lldbsuite.test import lldbutil
13
14
15class HelpCommandTestCase(TestBase):
16    @no_debug_info_test
17    def test_simplehelp(self):
18        """A simple test of 'help' command and its output."""
19        self.expect("help", startstr="Debugger commands:")
20
21        self.expect("help -a", matching=False, substrs=["next"])
22
23        self.expect("help", matching=True, substrs=["next"])
24
25    @no_debug_info_test
26    def test_help_on_help(self):
27        """Testing the help on the help facility."""
28        self.expect(
29            "help help",
30            matching=True,
31            substrs=["--hide-aliases", "--hide-user-commands"],
32        )
33
34    @no_debug_info_test
35    def test_help_arch(self):
36        """Test 'help arch' which should list of supported architectures."""
37        self.expect("help arch", substrs=["arm", "i386", "x86_64"])
38
39    @no_debug_info_test
40    def test_help_version(self):
41        """Test 'help version' and 'version' commands."""
42        self.expect("help version", substrs=["Show the LLDB debugger version."])
43        self.expect("version", patterns=["lldb( version|-[0-9]+).*\n"])
44
45    @no_debug_info_test
46    def test_help_should_not_crash_lldb(self):
47        """Command 'help disasm' should not crash lldb."""
48        self.runCmd("help disasm", check=False)
49        self.runCmd("help unsigned-integer")
50
51    @no_debug_info_test
52    def test_help_memory_read_should_not_crash_lldb(self):
53        """Command 'help memory read' should not crash lldb."""
54        self.runCmd("help memory read", check=False)
55
56    @no_debug_info_test
57    def test_help_should_not_hang_emacsshell(self):
58        """Command 'settings set term-width 0' should not hang the help command."""
59        self.expect(
60            "settings set term-width 0",
61            COMMAND_FAILED_AS_EXPECTED,
62            error=True,
63            substrs=["error: 0 is out of range, valid values must be between"],
64        )
65        # self.runCmd("settings set term-width 0")
66        self.expect("help", startstr="Debugger commands:")
67
68    @no_debug_info_test
69    def test_help_breakpoint_set(self):
70        """Test that 'help breakpoint set' does not print out redundant lines of:
71        'breakpoint set [-s <shlib-name>] ...'."""
72        self.expect(
73            "help breakpoint set",
74            matching=False,
75            substrs=["breakpoint set [-s <shlib-name>]"],
76        )
77
78    @no_debug_info_test
79    def test_help_image_dump_symtab_should_not_crash(self):
80        """Command 'help image dump symtab' should not crash lldb."""
81        # 'image' is an alias for 'target modules'.
82        self.expect("help image dump symtab", substrs=["dump symtab", "sort-order"])
83
84    @no_debug_info_test
85    def test_help_image_du_sym_is_ambiguous(self):
86        """Command 'help image du sym' is ambiguous and spits out the list of candidates."""
87        self.expect(
88            "help image du sym",
89            COMMAND_FAILED_AS_EXPECTED,
90            error=True,
91            substrs=["error: ambiguous command image du sym", "symfile", "symtab"],
92        )
93
94    @no_debug_info_test
95    def test_help_image_du_line_should_work(self):
96        """Command 'help image du line-table' is not ambiguous and should work."""
97        # 'image' is an alias for 'target modules'.
98        self.expect(
99            "help image du line",
100            substrs=["Dump the line table for one or more compilation units"],
101        )
102
103    @no_debug_info_test
104    def test_help_image_list_shows_positional_args(self):
105        """Command 'help image list' should describe positional args."""
106        # 'image' is an alias for 'target modules'.
107        self.expect("help image list", substrs=["<module> [...]"])
108
109    @no_debug_info_test
110    def test_help_target_variable_syntax(self):
111        """Command 'help target variable' should display <variable-name> ..."""
112        self.expect(
113            "help target variable", substrs=["<variable-name> [<variable-name> [...]]"]
114        )
115
116    @no_debug_info_test
117    def test_help_watchpoint_and_its_args(self):
118        """Command 'help watchpoint', 'help watchpt-id', and 'help watchpt-id-list' should work."""
119        self.expect("help watchpoint", substrs=["delete", "disable", "enable", "list"])
120        self.expect("help watchpt-id", substrs=["<watchpt-id>"])
121        self.expect("help watchpt-id-list", substrs=["<watchpt-id-list>"])
122
123    @no_debug_info_test
124    def test_help_watchpoint_set(self):
125        """Test that 'help watchpoint set' prints out 'expression' and 'variable'
126        as the possible subcommands."""
127        self.expect(
128            "help watchpoint set",
129            substrs=["The following subcommands are supported:"],
130            patterns=["expression +--", "variable +--"],
131        )
132
133    @no_debug_info_test
134    def test_help_po_hides_options(self):
135        """Test that 'help po' does not show all the options for expression"""
136        self.expect(
137            "help po",
138            substrs=["--show-all-children", "--object-description"],
139            matching=False,
140        )
141
142    @no_debug_info_test
143    def test_help_run_hides_options(self):
144        """Test that 'help run' does not show all the options for process launch"""
145        self.expect("help run", substrs=["--arch", "--environment"], matching=False)
146
147    @no_debug_info_test
148    def test_help_next_shows_options(self):
149        """Test that 'help next' shows all the options for thread step-over"""
150        self.expect(
151            "help next",
152            substrs=["--step-out-avoids-no-debug", "--run-mode"],
153            matching=True,
154        )
155
156    @no_debug_info_test
157    def test_help_provides_alternatives(self):
158        """Test that help on commands that don't exist provides information on additional help avenues"""
159        self.expect(
160            "help thisisnotadebuggercommand",
161            substrs=[
162                "'thisisnotadebuggercommand' is not a known command.",
163                "Try 'help' to see a current list of commands.",
164                "Try 'apropos thisisnotadebuggercommand' for a list of related commands.",
165                "Try 'type lookup thisisnotadebuggercommand' for information on types, methods, functions, modules, etc.",
166            ],
167            error=True,
168        )
169
170        self.expect(
171            "help process thisisnotadebuggercommand",
172            substrs=[
173                "'process thisisnotadebuggercommand' is not a known command.",
174                "Try 'help' to see a current list of commands.",
175                "Try 'apropos thisisnotadebuggercommand' for a list of related commands.",
176                "Try 'type lookup thisisnotadebuggercommand' for information on types, methods, functions, modules, etc.",
177            ],
178        )
179
180    @no_debug_info_test
181    def test_custom_help_alias(self):
182        """Test that aliases pick up custom help text."""
183
184        def cleanup():
185            self.runCmd("command unalias afriendlyalias", check=False)
186            self.runCmd("command unalias averyfriendlyalias", check=False)
187
188        self.addTearDownHook(cleanup)
189        self.runCmd(
190            'command alias --help "I am a friendly alias" -- afriendlyalias help'
191        )
192        self.expect(
193            "help afriendlyalias", matching=True, substrs=["I am a friendly alias"]
194        )
195        self.runCmd(
196            'command alias --long-help "I am a very friendly alias" -- averyfriendlyalias help'
197        )
198        self.expect(
199            "help averyfriendlyalias",
200            matching=True,
201            substrs=["I am a very friendly alias"],
202        )
203
204    @no_debug_info_test
205    def test_alias_prints_origin(self):
206        """Test that 'help <unique_match_to_alias>' prints the alias origin."""
207
208        def cleanup():
209            self.runCmd("command unalias alongaliasname", check=False)
210
211        self.addTearDownHook(cleanup)
212        self.runCmd("command alias alongaliasname help")
213        self.expect(
214            "help alongaliasna",
215            matching=True,
216            substrs=["'alongaliasna' is an abbreviation for 'help'"],
217        )
218
219    @no_debug_info_test
220    def test_hidden_help(self):
221        self.expect("help -h", substrs=["_regexp-bt"])
222
223    @no_debug_info_test
224    def test_help_ambiguous(self):
225        self.expect(
226            "help g",
227            substrs=[
228                "Help requested with ambiguous command name, possible completions:",
229                "gdb-remote",
230                "gui",
231            ],
232        )
233
234    @no_debug_info_test
235    def test_help_unknown_flag(self):
236        self.expect("help -z", error=True, substrs=["unknown or ambiguous option"])
237
238    @no_debug_info_test
239    def test_help_format_output(self):
240        """Test that help output reaches TerminalWidth."""
241        self.runCmd("settings set term-width 108")
242        self.expect(
243            "help format",
244            matching=True,
245            substrs=["<format> -- One of the format names"],
246        )
247
248    @no_debug_info_test
249    def test_help_option_group_format_options_usage(self):
250        """Test that help on commands that use OptionGroupFormat options provide relevant help specific to that command."""
251        self.expect(
252            "help memory read",
253            matching=True,
254            substrs=[
255                "-f <format> ( --format <format> )",
256                "Specify a format to be used for display.",
257                "-s <byte-size> ( --size <byte-size> )",
258                "The size in bytes to use when displaying with the selected format.",
259            ],
260        )
261
262        self.expect(
263            "help memory write",
264            matching=True,
265            substrs=[
266                "-f <format> ( --format <format> )",
267                "The format to use for each of the value to be written.",
268                "-s <byte-size> ( --size <byte-size> )",
269                "The size in bytes to write from input file or each value.",
270            ],
271        )
272
273    @no_debug_info_test
274    def test_help_shows_optional_short_options(self):
275        """Test that optional short options are printed and that they are in
276        alphabetical order with upper case options first."""
277        self.expect(
278            "help memory read", substrs=["memory read [-br]", "memory read [-AFLORTr]"]
279        )
280        self.expect(
281            "help target modules lookup", substrs=["target modules lookup [-Airv]"]
282        )
283
284    @no_debug_info_test
285    def test_help_shows_command_options_usage(self):
286        """Test that we start the usage section with a specific line."""
287        self.expect(
288            "help memory read", substrs=["Command Options Usage:\n  memory read"]
289        )
290
291    @no_debug_info_test
292    def test_help_detailed_information_spacing(self):
293        """Test that we put a break between the usage and the options help lines,
294        and between the options themselves."""
295        self.expect(
296            "help memory read",
297            substrs=[
298                "[<address-expression>]\n\n       -A ( --show-all-children )",
299                # Starts with the end of the show-all-children line
300                "to show.\n\n       -D",
301            ],
302        )
303
304    @no_debug_info_test
305    def test_help_detailed_information_ordering(self):
306        """Test that we order options alphabetically, upper case first."""
307        # You could test this with a simple regex like:
308        # <upper case>.*<later upper case>.*<lower case>.*<later lower case>
309        # Except that that could pass sometimes even with shuffled output.
310        # This makes sure that doesn't happen.
311
312        self.runCmd("help memory read")
313        got = self.res.GetOutput()
314        _, options_lines = got.split("Command Options Usage:")
315        options_lines = options_lines.lstrip().splitlines()
316
317        # Skip over "memory read [-xyz] lines.
318        while "memory read" in options_lines[0]:
319            options_lines.pop(0)
320        # Plus the newline after that.
321        options_lines.pop(0)
322
323        short_options = []
324        for line in options_lines:
325            # Ignore line breaks and descriptions.
326            # (not stripping the line here in case some line of the descriptions
327            # happens to start with "-")
328            if not line or not line.startswith("       -"):
329                continue
330            # This apears at the end after the options.
331            if "This command takes options and free form arguments." in line:
332                break
333            line = line.strip()
334            # The order of -- only options is not enforced so ignore their position.
335            if not line.startswith("--"):
336                # Save its short char name.
337                short_options.append(line[1])
338
339        self.assertEqual(
340            sorted(short_options),
341            short_options,
342            "Short option help displayed in an incorrect order!",
343        )
344
345    @no_debug_info_test
346    def test_help_show_tags(self):
347        """Check that memory find and memory read have the --show-tags option
348        but only memory read mentions binary output."""
349        self.expect(
350            "help memory read",
351            patterns=[
352                "--show-tags\n\s+Include memory tags in output "
353                "\(does not apply to binary output\)."
354            ],
355        )
356        self.expect(
357            "help memory find",
358            patterns=["--show-tags\n\s+Include memory tags in output."],
359        )
360
361    @no_debug_info_test
362    def test_help_show_enum_values(self):
363        """Check the help output for a argument type contains the enum values
364        and their descriptions."""
365        self.expect(
366            "help <log-handler>",
367            substrs=[
368                "The log handle that will be used to write out log messages.",
369                "default",
370                "Use the default (stream) log handler",
371                "stream",
372                "Write log messages to the debugger output stream",
373                "circular",
374                "Write log messages to a fixed size circular buffer",
375                "os",
376                "Write log messages to the operating system log",
377            ],
378        )
379