1 /* Self tests for simple_search_memory for GDB, the GNU debugger.
3 Copyright (C) 2020-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "gdbsupport/common-defs.h"
21 #include "gdbsupport/selftest.h"
22 #include "gdbsupport/search.h"
25 namespace search_memory_tests
{
30 size_t size
= 2 * SEARCH_CHUNK_SIZE
+ 1;
32 std::vector
<gdb_byte
> data (size
);
35 bool read_fully
= false;
36 bool read_off_end
= false;
37 auto read_memory
= [&] (CORE_ADDR from
, gdb_byte
*out
, size_t len
)
39 if (from
+ len
> data
.size ())
42 memcpy (out
, &data
[from
], len
);
43 if (from
+ len
== data
.size ())
48 gdb_byte pattern
= 'x';
51 int result
= simple_search_memory (read_memory
, 0, data
.size (),
53 /* In this case we don't care if read_fully was set or not. */
54 SELF_CHECK (result
== 1);
55 SELF_CHECK (!read_off_end
);
56 SELF_CHECK (addr
== size
- 1);
62 result
= simple_search_memory (read_memory
, 0, data
.size (),
64 SELF_CHECK (result
== 0);
65 SELF_CHECK (!read_off_end
);
66 SELF_CHECK (read_fully
);
67 SELF_CHECK (addr
== 0);
69 /* Setup from PR gdb/17756. */
71 data
= std::vector
<gdb_byte
> (size
);
72 const CORE_ADDR base_addr
= 0x08370000;
73 const gdb_byte wpattern
[] = { 0x90, 0x8b, 0x98, 0x8 };
74 const CORE_ADDR found_addr
= 0x837bac8;
75 memcpy (&data
[found_addr
- base_addr
], wpattern
, sizeof (wpattern
));
77 auto read_memory_2
= [&] (CORE_ADDR from
, gdb_byte
*out
, size_t len
)
79 memcpy (out
, &data
[from
- base_addr
], len
);
83 result
= simple_search_memory (read_memory_2
, base_addr
, data
.size (),
84 wpattern
, sizeof (wpattern
), &addr
);
85 SELF_CHECK (result
== 1);
86 SELF_CHECK (addr
== found_addr
);
89 } /* namespace search_memory_tests */
90 } /* namespace selftests */
93 void _initialize_search_memory_selftests ();
95 _initialize_search_memory_selftests ()
97 selftests::register_test ("search_memory",
98 selftests::search_memory_tests::run_tests
);