Home | History | Annotate | Line # | Download | only in gdb.python
      1 # Copyright 2020-2024 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.registers functionality.
     17 
     18 load_lib gdb-python.exp
     19 require allow_python_tests
     20 standard_testfile py-arch.c
     21 
     22 if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } {
     23     return -1
     24 }
     25 
     26 if ![runto_main] {
     27    return -1
     28 }
     29 
     30 # First, use 'info registers' to get a list of register names.
     31 set regs {}
     32 gdb_test_multiple "info registers general" "info registers general" {
     33     -re "^info registers general\r\n" {
     34 	exp_continue
     35     }
     36     -re "^(\[^ \t\]+)\[ \t\]+\[^\r\n\]+\r\n" {
     37 	set reg $expect_out(1,string)
     38 	lappend regs $reg
     39 	exp_continue
     40     }
     41     -re "^$gdb_prompt " {
     42     }
     43 }
     44 gdb_assert {[llength $regs] > 0} \
     45     "Found at least one register"
     46 
     47 # Now get the same register names using Python API.
     48 gdb_py_test_silent_cmd \
     49     "python frame = gdb.selected_frame()" "get frame" 0
     50 gdb_py_test_silent_cmd \
     51     "python arch = frame.architecture()" "get arch" 0
     52 gdb_py_test_silent_cmd \
     53     "python regs = list (arch.registers (\"general\"))" \
     54     "get general registers" 0
     55 gdb_py_test_silent_cmd \
     56     "python regs = map (lambda r : r.name, regs)" \
     57     "get names of general registers" 0
     58 
     59 set py_regs {}
     60 gdb_test_multiple "python print (\"\\n\".join (regs))" \
     61     "general register from python" {
     62 	-re "^python print \[^\r\n\]+\r\n" {
     63 	    exp_continue
     64 	}
     65 	-re "^(\[^\r\n\]+)\r\n" {
     66 	    set reg $expect_out(1,string)
     67 	    lappend py_regs $reg
     68 	    exp_continue
     69 	}
     70 	-re "^$gdb_prompt " {
     71 	}
     72     }
     73 
     74 gdb_assert {[llength $py_regs] > 0} \
     75     "Found at least one register from python"
     76 gdb_assert {[llength $py_regs] == [llength $regs]} \
     77     "Same numnber of registers found"
     78 
     79 set found_non_match 0
     80 for { set i 0 } { $i < [llength $regs] } { incr i } {
     81     if {[lindex $regs $i] != [lindex $py_regs $i]} {
     82 	set found_non_match 1
     83     }
     84 }
     85 gdb_assert { $found_non_match == 0 } "all registers match"
     86 
     87 # Check that we get the same register descriptors from two different
     88 # iterators.
     89 gdb_py_test_silent_cmd \
     90     "python iter1 = arch.registers ()" \
     91     "get first all register iterator" 0
     92 gdb_py_test_silent_cmd \
     93     "python iter2 = arch.registers ()" \
     94     "get second all register iterator" 0
     95 gdb_py_test_silent_cmd \
     96     [multi_line_input \
     97 	 "python" \
     98 	 "for r1, r2 in zip(iter1, iter2):" \
     99 	 "  if (r1.name != r2.name):"\
    100 	 "    raise gdb.GdbError (\"miss-matched names\")" \
    101 	 "  if (r1 != r2):" \
    102 	 "    raise gdb.GdbError (\"miss-matched objects\")" \
    103 	 "end" ] \
    104     "check names and objects match" 1
    105 
    106 # Ensure that the '.find' method on the iterator returns the same
    107 # Python object as we got from the iterator's list of descriptors.
    108 gdb_py_test_silent_cmd \
    109     [multi_line \
    110 	 "python" \
    111 	 "def check_regs (arch, regs):" \
    112 	 "   for r in (regs):" \
    113 	 "     if (arch.registers ().find (r.name) != r):" \
    114 	 "       raise gdb.GdbError (\"object miss-match\")" \
    115 	 "end" ] \
    116     "build check_obj function" 0
    117 gdb_py_test_silent_cmd \
    118     "python check_regs (arch, arch.registers (\"general\"))" \
    119     "ensure find gets expected descriptors" 1
    120