xref: /llvm-project/lldb/test/API/functionalities/memory/find/TestMemoryFind.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
199451b44SJordan Rupprecht"""
299451b44SJordan RupprechtTest the 'memory find' command.
399451b44SJordan Rupprecht"""
499451b44SJordan Rupprecht
599451b44SJordan Rupprecht
699451b44SJordan Rupprechtimport lldb
799451b44SJordan Rupprechtfrom lldbsuite.test.lldbtest import *
899451b44SJordan Rupprechtimport lldbsuite.test.lldbutil as lldbutil
999451b44SJordan Rupprechtfrom lldbsuite.test.decorators import *
1099451b44SJordan Rupprecht
1199451b44SJordan Rupprecht
1299451b44SJordan Rupprechtclass MemoryFindTestCase(TestBase):
1399451b44SJordan Rupprecht    def setUp(self):
1499451b44SJordan Rupprecht        # Call super's setUp().
1599451b44SJordan Rupprecht        TestBase.setUp(self)
1699451b44SJordan Rupprecht        # Find the line number to break inside main().
17*2238dcc3SJonas Devlieghere        self.line = line_number("main.cpp", "// break here")
1899451b44SJordan Rupprecht
1999451b44SJordan Rupprecht    def test_memory_find(self):
2099451b44SJordan Rupprecht        """Test the 'memory find' command."""
2199451b44SJordan Rupprecht        self.build()
2299451b44SJordan Rupprecht        exe = self.getBuildArtifact("a.out")
2399451b44SJordan Rupprecht        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
2499451b44SJordan Rupprecht
2599451b44SJordan Rupprecht        # Break in main() after the variables are assigned values.
2699451b44SJordan Rupprecht        lldbutil.run_break_set_by_file_and_line(
27*2238dcc3SJonas Devlieghere            self, "main.cpp", self.line, num_expected_locations=1, loc_exact=True
28*2238dcc3SJonas Devlieghere        )
2999451b44SJordan Rupprecht
3099451b44SJordan Rupprecht        self.runCmd("run", RUN_SUCCEEDED)
3199451b44SJordan Rupprecht
3299451b44SJordan Rupprecht        # The stop reason of the thread should be breakpoint.
33*2238dcc3SJonas Devlieghere        self.expect(
34*2238dcc3SJonas Devlieghere            "thread list",
35*2238dcc3SJonas Devlieghere            STOPPED_DUE_TO_BREAKPOINT,
36*2238dcc3SJonas Devlieghere            substrs=["stopped", "stop reason = breakpoint"],
37*2238dcc3SJonas Devlieghere        )
3899451b44SJordan Rupprecht
3999451b44SJordan Rupprecht        # The breakpoint should have a hit count of 1.
409f0b5f9aSSYNOPSYS\georgiev        lldbutil.check_breakpoint(self, bpno=1, expected_hit_count=1)
4199451b44SJordan Rupprecht
4299451b44SJordan Rupprecht        # Test the memory find commands.
4399451b44SJordan Rupprecht
4468e73eaeSDavid Spickett        # Empty search string should be handled.
45*2238dcc3SJonas Devlieghere        self.expect(
46*2238dcc3SJonas Devlieghere            'memory find -s "" `stringdata` `stringdata+16`',
4768e73eaeSDavid Spickett            error=True,
48*2238dcc3SJonas Devlieghere            substrs=["error: search string must have non-zero length."],
49*2238dcc3SJonas Devlieghere        )
5068e73eaeSDavid Spickett
5199451b44SJordan Rupprecht        self.expect(
5299451b44SJordan Rupprecht            'memory find -s "in const" `stringdata` `stringdata+(int)strlen(stringdata)`',
53*2238dcc3SJonas Devlieghere            substrs=["data found at location: 0x", "69 6e 20 63", "in const"],
54*2238dcc3SJonas Devlieghere        )
5599451b44SJordan Rupprecht
5668e73eaeSDavid Spickett        # Invalid expr is an error.
5768e73eaeSDavid Spickett        self.expect(
5868e73eaeSDavid Spickett            'memory find -e "not_a_symbol" `&bytedata[0]` `&bytedata[15]`',
5968e73eaeSDavid Spickett            error=True,
60*2238dcc3SJonas Devlieghere            substrs=["error: expression evaluation failed. pass a string instead"],
61*2238dcc3SJonas Devlieghere        )
6268e73eaeSDavid Spickett
6399451b44SJordan Rupprecht        self.expect(
6499451b44SJordan Rupprecht            'memory find -e "(uint8_t)0x22" `&bytedata[0]` `&bytedata[15]`',
65*2238dcc3SJonas Devlieghere            substrs=["data found at location: 0x", "22 33 44 55 66"],
66*2238dcc3SJonas Devlieghere        )
6799451b44SJordan Rupprecht
6899451b44SJordan Rupprecht        self.expect(
6999451b44SJordan Rupprecht            'memory find -e "(uint8_t)0x22" `&bytedata[0]` `&bytedata[2]`',
70*2238dcc3SJonas Devlieghere            substrs=["data not found within the range."],
71*2238dcc3SJonas Devlieghere        )
7299451b44SJordan Rupprecht
73*2238dcc3SJonas Devlieghere        self.expect(
74*2238dcc3SJonas Devlieghere            'memory find -s "nothere" `stringdata` `stringdata+5`',
75*2238dcc3SJonas Devlieghere            substrs=["data not found within the range."],
76*2238dcc3SJonas Devlieghere        )
7799451b44SJordan Rupprecht
78*2238dcc3SJonas Devlieghere        self.expect(
79*2238dcc3SJonas Devlieghere            'memory find -s "nothere" `stringdata` `stringdata+10`',
80*2238dcc3SJonas Devlieghere            substrs=["data not found within the range."],
81*2238dcc3SJonas Devlieghere        )
82