1# Copyright 2020-2023 Free Software Foundation, Inc.
2
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 3 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16# Check the gdb.Architecture.register_groups functionality.
17
18load_lib gdb-python.exp
19standard_testfile py-arch.c
20
21if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } {
22    return -1
23}
24
25# Skip all tests if Python scripting is not enabled.
26if { [skip_python_tests] } { continue }
27
28if ![runto_main] {
29   return -1
30}
31
32# First, use 'maint print reggroups' to get a list of all register
33# groups.
34set groups {}
35set test "maint print reggroups"
36gdb_test_multiple $test $test {
37    -re ".*Group\[ \t\]+Type\[ \t\]+\r\n" {
38	exp_continue
39    }
40    -re "^ (\[^ \t\]+)\[ \t\]+\[^\r\n\]+\r\n" {
41	lappend groups $expect_out(1,string)
42	exp_continue
43    }
44    -re "^$gdb_prompt " {
45    }
46}
47gdb_assert {[llength $groups] > 0} \
48    "Found at least one register group"
49
50# Now get the same register names using Python API.
51gdb_py_test_silent_cmd \
52    "python frame = gdb.selected_frame()" "get frame" 0
53gdb_py_test_silent_cmd \
54    "python arch = frame.architecture()" "get arch" 0
55gdb_py_test_silent_cmd \
56    "python groups = list (arch.register_groups ())" \
57    "get register groups" 0
58gdb_py_test_silent_cmd \
59    "python groups = map (lambda obj: obj.name, groups)" \
60    "convert groups to names" 0
61
62set py_groups {}
63gdb_test_multiple "python print (\"\\n\".join (groups))" \
64    "register groups from python" {
65	-re "^python print \[^\r\n\]+\r\n" {
66	    exp_continue
67	}
68	-re "^(\[^\r\n\]+)\r\n" {
69	    lappend py_groups $expect_out(1,string)
70	    exp_continue
71	}
72	-re "^$gdb_prompt " {
73	}
74    }
75
76gdb_assert {[llength $py_groups] > 0} \
77    "Found at least one register group from python"
78gdb_assert {[llength $py_groups] == [llength $groups]} \
79    "Same numnber of registers groups found"
80
81set found_non_match 0
82for { set i 0 } { $i < [llength $groups] } { incr i } {
83    if {[lindex $groups $i] != [lindex $py_groups $i]} {
84	set found_non_match 1
85    }
86}
87gdb_assert { $found_non_match == 0 } "all register groups match"
88
89# Check that we get the same register group descriptors from two
90# different iterators.
91gdb_py_test_silent_cmd \
92    "python iter1 = arch.register_groups ()" \
93    "get first all register group iterator" 0
94gdb_py_test_silent_cmd \
95    "python iter2 = arch.register_groups ()" \
96    "get second all register group iterator" 0
97gdb_py_test_silent_cmd \
98    [multi_line_input \
99	 "python" \
100	 "for r1, r2 in zip(iter1, iter2):" \
101	 "  if (r1.name != r2.name):"\
102	 "    raise gdb.GdbError (\"miss-matched names\")" \
103	 "  if (r1 != r2):" \
104	 "    raise gdb.GdbError (\"miss-matched objects\")" \
105	 "end" ] \
106    "check names and objects match" 1
107