xref: /llvm-project/lldb/test/API/commands/command/backticks/TestBackticksInAlias.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
1"""
2Test that an alias can contain active backticks
3"""
4
5
6import lldb
7from lldbsuite.test.lldbtest import *
8import lldbsuite.test.lldbutil as lldbutil
9
10
11class TestBackticksInAlias(TestBase):
12    NO_DEBUG_INFO_TESTCASE = True
13
14    def test_backticks_in_alias(self):
15        """Test that an alias can contain active backticks."""
16        self.build()
17        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
18            self, "break here", lldb.SBFileSpec("main.c")
19        )
20        interp = self.dbg.GetCommandInterpreter()
21        result = lldb.SBCommandReturnObject()
22        interp.HandleCommand(
23            "command alias _test-argv-cmd expression -Z \`argc\` -- argv", result
24        )
25        self.assertCommandReturn(result, "Made the alias")
26        interp.HandleCommand("_test-argv-cmd", result)
27        self.assertCommandReturn(result, "The alias worked")
28
29        # Now try a harder case where we create this using an alias:
30        interp.HandleCommand(
31            "command alias _test-argv-parray-cmd parray \`argc\` argv", result
32        )
33        self.assertCommandReturn(result, "Made the alias")
34        interp.HandleCommand("_test-argv-parray-cmd", result)
35        self.assertFalse(
36            result.Succeeded(),
37            "CommandAlias::Desugar currently fails if a alias substitutes %N arguments in another alias",
38        )
39
40    def test_backticks_in_parsed_cmd_argument(self):
41        """break list is a parsed command, use a variable for the breakpoint number
42        and make sure that and the direct use of the ID get the same result."""
43        self.build()
44        target, process, thread, bkpt = lldbutil.run_to_source_breakpoint(
45            self, "break here", lldb.SBFileSpec("main.c")
46        )
47        # Make a second breakpoint so that if the backtick part -> nothing we'll print too much:
48        # It doesn't need to resolve to anything.
49        dummy_bkpt = target.BreakpointCreateByName("dont_really_care_if_this_exists")
50
51        bkpt_id = bkpt.GetID()
52        self.runCmd(f"expr int $number = {bkpt_id}")
53        direct_result = lldb.SBCommandReturnObject()
54        backtick_result = lldb.SBCommandReturnObject()
55        interp = self.dbg.GetCommandInterpreter()
56        interp.HandleCommand(f"break list {bkpt_id}", direct_result)
57        self.assertTrue(direct_result.Succeeded(), "Break list with id works")
58        interp.HandleCommand("break list `$number`", backtick_result)
59        self.assertTrue(direct_result.Succeeded(), "Break list with backtick works")
60        self.assertEqual(
61            direct_result.GetOutput(), backtick_result.GetOutput(), "Output is the same"
62        )
63
64    def test_backticks_in_parsed_cmd_option(self):
65        # The script interpreter is a raw command, so try that one:
66        self.build()
67        target, process, thread, bkpt = lldbutil.run_to_source_breakpoint(
68            self, "break here", lldb.SBFileSpec("main.c")
69        )
70
71        self.runCmd(f"expr int $number = 2")
72        direct_result = lldb.SBCommandReturnObject()
73        backtick_result = lldb.SBCommandReturnObject()
74        interp = self.dbg.GetCommandInterpreter()
75        interp.HandleCommand(f"memory read --count 2 argv", direct_result)
76        self.assertTrue(
77            direct_result.Succeeded(), "memory read with direct count works"
78        )
79        interp.HandleCommand("memory read --count `$number` argv", backtick_result)
80        self.assertTrue(direct_result.Succeeded(), "memory read with backtick works")
81        self.assertEqual(
82            direct_result.GetOutput(), backtick_result.GetOutput(), "Output is the same"
83        )
84
85    def test_backticks_in_raw_cmd(self):
86        # The script interpreter is a raw command, so try that one:
87        self.build()
88        target, process, thread, bkpt = lldbutil.run_to_source_breakpoint(
89            self, "break here", lldb.SBFileSpec("main.c")
90        )
91        argc_valobj = thread.frames[0].FindVariable("argc")
92        self.assertTrue(argc_valobj.GetError().Success(), "Made argc valobj")
93        argc_value = argc_valobj.GetValueAsUnsigned(0)
94        self.assertNotEqual(argc_value, 0, "Got a value for argc")
95        result = lldb.SBCommandReturnObject()
96        interp = self.dbg.GetCommandInterpreter()
97        interp.HandleCommand(f"script {argc_value} - `argc`", result)
98        self.assertTrue(result.Succeeded(), "Command succeeded")
99        fixed_output = result.GetOutput().rstrip()
100        self.assertEqual("0", fixed_output, "Substitution worked")
101