Automatic date update in version.in
[binutils-gdb.git] / gdb / testsuite / gdb.guile / scm-disasm.exp
blob68c19118909e11abbea57a9cda4a6e3155f490e1
1 # Copyright 2014-2024 Free Software Foundation, Inc.
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.
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.
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/>.
16 load_lib gdb-guile.exp
18 require allow_guile_tests
20 standard_testfile
22 if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } {
23     return
26 if ![gdb_guile_runto_main] {
27    return
30 # Disassemble one instruction at pc and verify the result.
32 proc test_disassemble_1 { name address extra_args } {
33     with_test_prefix $name {
34         gdb_scm_test_silent_cmd "guile (define insn-list (arch-disassemble arch $address $extra_args #:size 1 #:count 1))" \
35             "disassemble"
37         gdb_test "guile (print (length insn-list))" \
38             "= 1" "test number of instructions"
39         gdb_scm_test_silent_cmd "guile (define insn (car insn-list))" \
40             "get instruction"
42         # Verify all the fields are present.
43         gdb_test "guile (print (->bool (assq-ref insn 'address)))" \
44             "= #t" "test key address"
45         gdb_test "guile (print (->bool (assq-ref insn 'asm)))" \
46             "= #t" "test key asm"
47         gdb_test "guile (print (->bool (assq-ref insn 'length)))" \
48             "= #t" "test key length"
50         # Verify the correct address is used.
51         gdb_test "guile (print (= $address (assq-ref insn 'address)))" \
52             "= #t" "verify correct address"
53     }
56 gdb_scm_test_silent_cmd "guile (define frame (selected-frame))" "get frame"
57 gdb_scm_test_silent_cmd "guile (define arch (frame-arch frame))" "get arch"
58 gdb_scm_test_silent_cmd "guile (define pc (frame-pc frame))" "get pc"
60 gdb_test "guile (print (arch-disassemble arch pc #:size 0))" \
61     "= \\(\\)" "disassemble, zero size"
62 gdb_test "guile (print (arch-disassemble arch pc #:count 0))" \
63     "= \\(\\)" "disassemble, zero count"
65 gdb_scm_test_silent_cmd "guile (define insn-list1 (arch-disassemble arch pc #:size 1 #:count 1))" \
66     "disassemble"
67 gdb_scm_test_silent_cmd "guile (define insn-list2 (arch-disassemble arch pc #:size 1))" \
68     "disassemble, no count"
69 gdb_scm_test_silent_cmd "guile (define insn-list3 (arch-disassemble arch pc #:count 1))" \
70     "disassemble, no end"
71 gdb_scm_test_silent_cmd "guile (define insn-list4 (arch-disassemble arch pc))" \
72     "disassemble, no end no count"
74 gdb_test "guile (print (length insn-list1))" \
75     "= 1" "test number of instructions 1"
76 gdb_test "guile (print (length insn-list2))" \
77     "= 1" "test number of instructions 2"
78 gdb_test "guile (print (length insn-list3))" \
79     "= 1" "test number of instructions 3"
80 gdb_test "guile (print (length insn-list4))" \
81     "= 1" "test number of instructions 4"
83 test_disassemble_1 "basic" "pc" ""
85 if { ![is_address_zero_readable] } {
86     # Negative test
87     gdb_test "guile (arch-disassemble arch 0 #:size 1)" \
88         "ERROR: Cannot access memory at address 0x.*" "test bad memory access"
91 # Test disassembly through a port.
93 gdb_scm_test_silent_cmd "guile (define mem (open-memory))" \
94     "open memory port"
96 test_disassemble_1 "memory-port" "pc" "#:port mem"
98 gdb_scm_test_silent_cmd "guile (define insn-list-mem (arch-disassemble arch pc #:port mem #:size 1 #:count 1))" \
99     "disassemble via memory port"
101 # Test memory error reading from port.
103 gdb_scm_test_silent_cmd "guile (define mem1 (open-memory #:start pc #:size 4))" \
104     "open restricted range memory port"
106 # The x86 disassembler tries to be clever and will print "byte 0x42" if
107 # there is insufficient memory for the entire instruction.
108 # So we pass "#:count 5" to ensure the disassembler tries to read beyond
109 # the end of the memory range.
110 gdb_test "guile (arch-disassemble arch pc #:port mem1 #:count 5 #:offset pc)" \
111     "ERROR: Cannot access memory at address 0x.*" \
112     "test bad memory access from port"
114 # Test disassembly of a bytevector.
116 gdb_scm_test_silent_cmd "guile (use-modules (rnrs io ports))" \
117     "import (rnrs io ports)"
119 # First fetch the length of the instruction at $pc.
120 gdb_scm_test_silent_cmd "guile (define insn-list-for-bv (arch-disassemble arch pc))" \
121     "get insn for bytevector"
122 gdb_test_no_output "guile (define insn-length (assq-ref (car insn-list-for-bv) 'length))" \
123     "get insn length for bytevector"
125 # Read the insn into a bytevector.
126 gdb_test_no_output "guile (define insn-bv (get-bytevector-n (open-memory #:start pc #:size insn-length) insn-length))" \
127     "read insn into bytevector"
129 # Disassemble the bytevector.
130 gdb_scm_test_silent_cmd "guile (define insn-list-from-bv (arch-disassemble arch pc #:port (open-bytevector-input-port insn-bv) #:offset pc))" \
131     "disassemble bytevector"
133 gdb_test "guile (print (equal? insn-list-for-bv insn-list-from-bv))" \
134     "= #t" "verify bytevector disassembly"