]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
[gdb/testsuite] Add gdb.python/py-disasm-{exec,obj}.exp
authorTom de Vries <tdevries@suse.de>
Wed, 31 Jul 2024 11:24:20 +0000 (13:24 +0200)
committerTom de Vries <tdevries@suse.de>
Wed, 31 Jul 2024 11:24:20 +0000 (13:24 +0200)
I tried to reproduce a problem in test-case gdb.python/py-disasm.exp on a
s390x machine, but when running with target board unix/-m31 I saw that the
required libraries were missing, so I couldn't generate an executable.

However, I realized that I did have an object file, and the test-case should
mostly also work with an object file.

I've renamed gdb.python/py-disasm.exp to gdb.python/py-disasm.exp.tcl and
included it from two new minimal test-case wrappers:
- gdb.python/py-disasm-exec.exp, and
- gdb.python/py-disasm-obj.exp
where the former uses an executable as before, and the latter uses an object
file.

Using an object file required changing the info.read_memory calls in
gdb.python/py-disasm.py:
...
-            info.read_memory(1, -info.address + 2)
+            info.read_memory(1, -info.address - 1)
...
because reading from address 2 succeeds.  Using address -1 instead does
generate the expected gdb.MemoryError.

Tested on x86_64-linux.

gdb/testsuite/gdb.python/py-disasm-exec.exp [new file with mode: 0644]
gdb/testsuite/gdb.python/py-disasm-obj.exp [new file with mode: 0644]
gdb/testsuite/gdb.python/py-disasm.exp.tcl [moved from gdb/testsuite/gdb.python/py-disasm.exp with 92% similarity]
gdb/testsuite/gdb.python/py-disasm.py

diff --git a/gdb/testsuite/gdb.python/py-disasm-exec.exp b/gdb/testsuite/gdb.python/py-disasm-exec.exp
new file mode 100644 (file)
index 0000000..546c4b8
--- /dev/null
@@ -0,0 +1,21 @@
+# Copyright (C) 2024 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# This file is part of the GDB testsuite.  It validates the Python
+# disassembler API.
+
+set kind exec
+
+source $srcdir/$subdir/py-disasm.exp.tcl
diff --git a/gdb/testsuite/gdb.python/py-disasm-obj.exp b/gdb/testsuite/gdb.python/py-disasm-obj.exp
new file mode 100644 (file)
index 0000000..37ff965
--- /dev/null
@@ -0,0 +1,21 @@
+# Copyright (C) 2024 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# This file is part of the GDB testsuite.  It validates the Python
+# disassembler API.
+
+set kind obj
+
+source $srcdir/$subdir/py-disasm.exp.tcl
similarity index 92%
rename from gdb/testsuite/gdb.python/py-disasm.exp
rename to gdb/testsuite/gdb.python/py-disasm.exp.tcl
index 5d7d922116900d53e1a26144f681377b99d6ce1e..8ed634e6f85d05e1db3c9161232dfc630422ebfa 100644 (file)
@@ -20,26 +20,54 @@ load_lib gdb-python.exp
 
 require allow_python_tests
 
-standard_testfile
+standard_testfile py-disasm.c
 
-if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile} "debug"] } {
-    return -1
-}
+if { $kind == "obj" } {
+
+    set obj [standard_output_file ${gdb_test_file_name}.o]
+
+    if { [gdb_compile "$srcdir/$subdir/$srcfile" $obj object "debug"] != "" } {
+       untested "failed to compile object file [file tail $obj]"
+       return -1
+    }
+
+    clean_restart $obj
+
+} else {
+
+    if { [prepare_for_testing "failed to prepare" $testfile $srcfile] } {
+       return -1
+    }
+
+    if { ![runto_main] } {
+       fail "can't run to main"
+       return 0
+    }
 
-if {![runto_main]} {
-    fail "can't run to main"
-    return 0
 }
 
-set pyfile [gdb_remote_download host ${srcdir}/${subdir}/${testfile}.py]
+set pyfile [gdb_remote_download host ${srcdir}/${subdir}/py-disasm.py]
 
 gdb_test "source ${pyfile}" "Python script imported" \
          "import python scripts"
 
-gdb_breakpoint [gdb_get_line_number "Break here."]
-gdb_continue_to_breakpoint "Break here."
+set line [gdb_get_line_number "Break here."]
 
-set curr_pc [get_valueof "/x" "\$pc" "*unknown*"]
+if { $kind == "obj" } {
+    set curr_pc "*unknown*"
+    set line [gdb_get_line_number "Break here."]
+    gdb_test_multiple "info line $line" "" {
+       -re -wrap "starts at address ($hex) \[^\r\n\]*" {
+           set curr_pc $expect_out(1,string)
+           pass $gdb_test_name
+       }
+    }
+} else {
+    gdb_breakpoint $line
+    gdb_continue_to_breakpoint "Break here."
+
+    set curr_pc [get_valueof "/x" "\$pc" "*unknown*"]
+}
 
 gdb_test_no_output "python current_pc = ${curr_pc}"
 
@@ -67,7 +95,11 @@ proc py_remove_all_disassemblers {} {
 # Python disassembler API.
 set nop "(nop|nop\t0|[string_to_regexp nop\t{0}])"
 set unknown_error_pattern "unknown disassembler error \\(error = -1\\)"
-set addr_pattern "\r\n=> ${curr_pc_pattern} <\[^>\]+>:\\s+"
+if { $kind == "obj" } {
+    set addr_pattern "\r\n   ${curr_pc_pattern} <\[^>\]+>:\\s+"
+} else {
+    set addr_pattern "\r\n=> ${curr_pc_pattern} <\[^>\]+>:\\s+"
+}
 set base_pattern "${addr_pattern}${nop}"
 
 # Helper proc to format a Python exception of TYPE with MSG.
index 2741fdb6c19576f0c639f210330961aff5ab2459..f105657f53ea5bf5b77c3876e89ee9d6424567c6 100644 (file)
@@ -252,7 +252,7 @@ class MemoryErrorEarlyDisassembler(TestDisassembler):
     def disassemble(self, info):
         tag = "## FAIL"
         try:
-            info.read_memory(1, -info.address + 2)
+            info.read_memory(1, -info.address - 1)
         except gdb.MemoryError:
             tag = "## AFTER ERROR"
         result = builtin_disassemble_wrapper(info)
@@ -267,7 +267,7 @@ class MemoryErrorLateDisassembler(TestDisassembler):
     def disassemble(self, info):
         result = builtin_disassemble_wrapper(info)
         # The following read will throw an error.
-        info.read_memory(1, -info.address + 2)
+        info.read_memory(1, -info.address - 1)
         return DisassemblerResult(1, "BAD")
 
 
@@ -276,9 +276,9 @@ class RethrowMemoryErrorDisassembler(TestDisassembler):
 
     def disassemble(self, info):
         try:
-            info.read_memory(1, -info.address + 2)
+            info.read_memory(1, -info.address - 1)
         except gdb.MemoryError as e:
-            raise gdb.MemoryError("cannot read code at address 0x2")
+            raise gdb.MemoryError("cannot read code at address -1")
         return DisassemblerResult(1, "BAD")