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