summaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.guile/scm-disasm.exp
blob: 6609c746756671d284d3af3ebea247182c4618d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# Copyright 2014-2023 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/>.

load_lib gdb-guile.exp

standard_testfile

if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } {
    return
}

# Skip all tests if Guile scripting is not enabled.
if { [skip_guile_tests] } { continue }

if ![gdb_guile_runto_main] {
   return
}

# Disassemble one instruction at pc and verify the result.

proc test_disassemble_1 { name address extra_args } {
    with_test_prefix $name {
	gdb_scm_test_silent_cmd "guile (define insn-list (arch-disassemble arch $address $extra_args #:size 1 #:count 1))" \
	    "disassemble"

	gdb_test "guile (print (length insn-list))" \
	    "= 1" "test number of instructions"
	gdb_scm_test_silent_cmd "guile (define insn (car insn-list))" \
	    "get instruction"

	# Verify all the fields are present.
	gdb_test "guile (print (->bool (assq-ref insn 'address)))" \
	    "= #t" "test key address"
	gdb_test "guile (print (->bool (assq-ref insn 'asm)))" \
	    "= #t" "test key asm"
	gdb_test "guile (print (->bool (assq-ref insn 'length)))" \
	    "= #t" "test key length"

	# Verify the correct address is used.
	gdb_test "guile (print (= $address (assq-ref insn 'address)))" \
	    "= #t" "verify correct address"
    }
}

gdb_scm_test_silent_cmd "guile (define frame (selected-frame))" "get frame"
gdb_scm_test_silent_cmd "guile (define arch (frame-arch frame))" "get arch"
gdb_scm_test_silent_cmd "guile (define pc (frame-pc frame))" "get pc"

gdb_test "guile (print (arch-disassemble arch pc #:size 0))" \
    "= \\(\\)" "disassemble, zero size"
gdb_test "guile (print (arch-disassemble arch pc #:count 0))" \
    "= \\(\\)" "disassemble, zero count"

gdb_scm_test_silent_cmd "guile (define insn-list1 (arch-disassemble arch pc #:size 1 #:count 1))" \
    "disassemble"
gdb_scm_test_silent_cmd "guile (define insn-list2 (arch-disassemble arch pc #:size 1))" \
    "disassemble, no count"
gdb_scm_test_silent_cmd "guile (define insn-list3 (arch-disassemble arch pc #:count 1))" \
    "disassemble, no end"
gdb_scm_test_silent_cmd "guile (define insn-list4 (arch-disassemble arch pc))" \
    "disassemble, no end no count"

gdb_test "guile (print (length insn-list1))" \
    "= 1" "test number of instructions 1"
gdb_test "guile (print (length insn-list2))" \
    "= 1" "test number of instructions 2"
gdb_test "guile (print (length insn-list3))" \
    "= 1" "test number of instructions 3"
gdb_test "guile (print (length insn-list4))" \
    "= 1" "test number of instructions 4"

test_disassemble_1 "basic" "pc" ""

if { ![is_address_zero_readable] } {
    # Negative test
    gdb_test "guile (arch-disassemble arch 0 #:size 1)" \
	"ERROR: Cannot access memory at address 0x.*" "test bad memory access"
}

# Test disassembly through a port.

gdb_scm_test_silent_cmd "guile (define mem (open-memory))" \
    "open memory port"

test_disassemble_1 "memory-port" "pc" "#:port mem"

gdb_scm_test_silent_cmd "guile (define insn-list-mem (arch-disassemble arch pc #:port mem #:size 1 #:count 1))" \
    "disassemble via memory port"

# Test memory error reading from port.

gdb_scm_test_silent_cmd "guile (define mem1 (open-memory #:start pc #:size 4))" \
    "open restricted range memory port"

# The x86 disassembler tries to be clever and will print "byte 0x42" if
# there is insufficient memory for the entire instruction.
# So we pass "#:count 5" to ensure the disassembler tries to read beyond
# the end of the memory range.
gdb_test "guile (arch-disassemble arch pc #:port mem1 #:count 5 #:offset pc)" \
    "ERROR: Cannot access memory at address 0x.*" \
    "test bad memory access from port"

# Test disassembly of a bytevector.

gdb_scm_test_silent_cmd "guile (use-modules (rnrs io ports))" \
    "import (rnrs io ports)"

# First fetch the length of the instruction at $pc.
gdb_scm_test_silent_cmd "guile (define insn-list-for-bv (arch-disassemble arch pc))" \
    "get insn for bytevector"
gdb_test_no_output "guile (define insn-length (assq-ref (car insn-list-for-bv) 'length))" \
    "get insn length for bytevector"

# Read the insn into a bytevector.
gdb_test_no_output "guile (define insn-bv (get-bytevector-n (open-memory #:start pc #:size insn-length) insn-length))" \
    "read insn into bytevector"

# Disassemble the bytevector.
gdb_scm_test_silent_cmd "guile (define insn-list-from-bv (arch-disassemble arch pc #:port (open-bytevector-input-port insn-bv) #:offset pc))" \
    "disassemble bytevector"

gdb_test "guile (print (equal? insn-list-for-bv insn-list-from-bv))" \
    "= #t" "verify bytevector disassembly"