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