]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.guile/scm-disasm.exp
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.guile / scm-disasm.exp
1 # Copyright 2014-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 load_lib gdb-guile.exp
17
18 require allow_guile_tests
19
20 standard_testfile
21
22 if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } {
23 return
24 }
25
26 if ![gdb_guile_runto_main] {
27 return
28 }
29
30 # Disassemble one instruction at pc and verify the result.
31
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"
36
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"
41
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"
49
50 # Verify the correct address is used.
51 gdb_test "guile (print (= $address (assq-ref insn 'address)))" \
52 "= #t" "verify correct address"
53 }
54 }
55
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"
59
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"
64
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"
73
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"
82
83 test_disassemble_1 "basic" "pc" ""
84
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"
89 }
90
91 # Test disassembly through a port.
92
93 gdb_scm_test_silent_cmd "guile (define mem (open-memory))" \
94 "open memory port"
95
96 test_disassemble_1 "memory-port" "pc" "#:port mem"
97
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"
100
101 # Test memory error reading from port.
102
103 gdb_scm_test_silent_cmd "guile (define mem1 (open-memory #:start pc #:size 4))" \
104 "open restricted range memory port"
105
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"
113
114 # Test disassembly of a bytevector.
115
116 gdb_scm_test_silent_cmd "guile (use-modules (rnrs io ports))" \
117 "import (rnrs io ports)"
118
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"
124
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"
128
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"
132
133 gdb_test "guile (print (equal? insn-list-for-bv insn-list-from-bv))" \
134 "= #t" "verify bytevector disassembly"