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