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/selftest.h"
21 #include "gdbsupport/search.h"
24 namespace search_memory_tests
{
29 size_t size
= 2 * SEARCH_CHUNK_SIZE
+ 1;
31 std::vector
<gdb_byte
> data (size
);
34 bool read_fully
= false;
35 bool read_off_end
= false;
36 auto read_memory
= [&] (CORE_ADDR from
, gdb_byte
*out
, size_t len
)
38 if (from
+ len
> data
.size ())
41 memcpy (out
, &data
[from
], len
);
42 if (from
+ len
== data
.size ())
47 gdb_byte pattern
= 'x';
50 int result
= simple_search_memory (read_memory
, 0, data
.size (),
52 /* In this case we don't care if read_fully was set or not. */
53 SELF_CHECK (result
== 1);
54 SELF_CHECK (!read_off_end
);
55 SELF_CHECK (addr
== size
- 1);
61 result
= simple_search_memory (read_memory
, 0, data
.size (),
63 SELF_CHECK (result
== 0);
64 SELF_CHECK (!read_off_end
);
65 SELF_CHECK (read_fully
);
66 SELF_CHECK (addr
== 0);
68 /* Setup from PR gdb/17756. */
70 data
= std::vector
<gdb_byte
> (size
);
71 const CORE_ADDR base_addr
= 0x08370000;
72 const gdb_byte wpattern
[] = { 0x90, 0x8b, 0x98, 0x8 };
73 const CORE_ADDR found_addr
= 0x837bac8;
74 memcpy (&data
[found_addr
- base_addr
], wpattern
, sizeof (wpattern
));
76 auto read_memory_2
= [&] (CORE_ADDR from
, gdb_byte
*out
, size_t len
)
78 memcpy (out
, &data
[from
- base_addr
], len
);
82 result
= simple_search_memory (read_memory_2
, base_addr
, data
.size (),
83 wpattern
, sizeof (wpattern
), &addr
);
84 SELF_CHECK (result
== 1);
85 SELF_CHECK (addr
== found_addr
);
88 } /* namespace search_memory_tests */
89 } /* namespace selftests */
92 void _initialize_search_memory_selftests ();
94 _initialize_search_memory_selftests ()
96 selftests::register_test ("search_memory",
97 selftests::search_memory_tests::run_tests
);