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